-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPhotonExtensions.cs
More file actions
77 lines (56 loc) · 2.42 KB
/
Copy pathPhotonExtensions.cs
File metadata and controls
77 lines (56 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Meta.XR.Samples;
using ExitGames.Client.Photon;
using Photon.Pun;
using System;
using UnityEngine;
using MemoryMarshal = System.Runtime.InteropServices.MemoryMarshal;
[MetaCodeSample("SpaceSharing")]
public static class PhotonExtensions
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void RegisterAdditionalTypeSerialization()
{
const int kPose3Size = 7 * sizeof(float); // sizeof(Vector3) + sizeof(Quaternion)
Sampleton.Log($"AfterSceneLoad: {nameof(PhotonExtensions)}::{nameof(RegisterAdditionalTypeSerialization)}");
if (!Protocol.TryRegisterType(typeof(Guid), (byte)'G', guidWrite, guidRead))
Sampleton.Error($"Photon ERR: failed to register {nameof(Guid)} serde");
// note: capital 'P' was taken by Photon's own CustomTypes.cs, and it only won the race in Editor PlayMode.
if (!Protocol.TryRegisterType(typeof(Pose), (byte)'p', pose3Write, pose3Read))
Sampleton.Error($"Photon ERR: failed to register {nameof(Pose)} serde");
// default fallback username
PhotonNetwork.NickName = $"Anon{UnityEngine.Random.Range(0, 10000):0000}";
return;
static byte[] guidWrite(object box)
{
return box is Guid guid ? guid.ToByteArray() : new byte[16];
}
static object guidRead(byte[] bytes)
{
return bytes?.Length == 16 ? new Guid(bytes) : Guid.Empty;
}
static bool isValid(in Pose p)
{
var t = p.position;
return (t.x == 0f || float.IsNormal(t.x)) &&
(t.y == 0f || float.IsNormal(t.y)) &&
(t.z == 0f || float.IsNormal(t.z)) &&
Quaternion.Dot(p.rotation, p.rotation) is > 1f - Vector4.kEpsilon and < 1f + Vector4.kEpsilon;
}
static byte[] pose3Write(object box)
{
var bytes = new byte[kPose3Size];
if (box is not Pose p || !isValid(p))
p = Pose.identity;
MemoryMarshal.Write(bytes, ref p);
return bytes;
}
static object pose3Read(byte[] bytes)
{
if (bytes?.Length != kPose3Size)
return Pose.identity;
var p = MemoryMarshal.Read<Pose>(bytes);
return isValid(p) ? p : Pose.identity;
}
}
} // end static class PhotonExtensions