-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkSceneHandle.cs
More file actions
168 lines (148 loc) · 6.65 KB
/
NetworkSceneHandle.cs
File metadata and controls
168 lines (148 loc) · 6.65 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Runtime.CompilerServices;
using UnityEngine.SceneManagement;
namespace Unity.Netcode
{
/// <summary>
/// An internal to NGO representation of the Scene <see cref="Scene.handle"/>.
/// </summary>
//
// The underlying representation of the scene handle is changing in 6.3
// This allows us to wrap the change and hide it from the rest of the package.
internal struct NetworkSceneHandle : IEquatable<NetworkSceneHandle>, INetworkSerializable
{
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
private SceneHandle m_Handle;
#else
private int m_Handle;
#endif
/// <summary>
/// Whether this <see cref="NetworkSceneHandle"/> represents an uninitialized scene handle
/// </summary>
/// <returns>True if this scene handle represents the <see langword="default"/>; otherwise false.</returns>
public bool IsEmpty() => Equals(default(NetworkSceneHandle));
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
if (serializer.IsWriter)
{
var writer = serializer.GetFastBufferWriter();
writer.WriteValueSafe(GetRawData());
}
else
{
var reader = serializer.GetFastBufferReader();
// DANGO-TODO Rust needs to be updated to either handle this ulong or to remove the scene store.
#if SCENE_MANAGEMENT_SCENE_HANDLE_MUST_USE_ULONG
reader.ReadValue(out ulong rawData);
m_Handle = SceneHandle.FromRawData(rawData);
#elif SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION
reader.ReadValueSafe(out int rawData);
m_Handle = SceneHandle.FromRawData((ulong)rawData);
#else
reader.ReadValueSafe(out int rawData);
m_Handle = rawData;
#endif
}
}
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
internal NetworkSceneHandle(SceneHandle handle)
{
m_Handle = handle;
}
#else
internal NetworkSceneHandle(int handle)
{
m_Handle = handle;
}
#endif
/// <summary>
/// A separate constructor for using during tests.
/// This is required as the tests need to be able to create NetworkSceneHandles that represent only mock data.
/// </summary>
/// <param name="handle">The number to use as the underlying sceneHandle representation</param>
/// <param name="asMock">Empty parameter that ensures the tests use the mock constructor.</param>
#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION
internal NetworkSceneHandle(ulong handle, bool asMock)
{
m_Handle = SceneHandle.FromRawData(handle);
}
#else
internal NetworkSceneHandle(int handle, bool asMock)
{
m_Handle = handle;
}
#endif
#if SCENE_MANAGEMENT_SCENE_HANDLE_MUST_USE_ULONG
public ulong GetRawData() => m_Handle.GetRawData();
#elif SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION
public int GetRawData() => (int)m_Handle.GetRawData();
#else
public int GetRawData() => m_Handle;
#endif
#region Implicit conversions
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
/// <summary>
/// Implicit conversion from <see cref="SceneHandle"/> to <see cref="NetworkSceneHandle"/>.
/// </summary>
/// <param name="handle">The SceneHandle to covert</param>
public static implicit operator NetworkSceneHandle(SceneHandle handle) => new(handle);
/// <summary>
/// Implicit conversion from <see cref="NetworkSceneHandle"/> to <see cref="SceneHandle"/>.
/// </summary>
/// <param name="handle">The NetworkSceneHandle to convert</param>
public static implicit operator SceneHandle(NetworkSceneHandle handle) => handle.m_Handle;
#else
/// <summary>
/// Implicit conversion from <see langword="int"/> to <see cref="NetworkSceneHandle"/>.
/// </summary>
/// <param name="handle"></param>
public static implicit operator NetworkSceneHandle(int handle) => new(handle);
#endif
#endregion
#region Operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(NetworkSceneHandle other) => m_Handle == other.m_Handle;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object obj) => obj is NetworkSceneHandle other && Equals(other);
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool Equals(SceneHandle other) => m_Handle == other;
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool Equals(int other) => m_Handle == other;
#endif
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => m_Handle.GetHashCode();
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => m_Handle;
#endif
/// <summary>
/// Test for equality.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns>True if the two SceneHandles are the same</returns>
public static bool operator ==(NetworkSceneHandle left, NetworkSceneHandle right) => left.Equals(right);
/// <summary>
/// Test for inequality.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns>True if the two SceneHandles are different</returns>
public static bool operator !=(NetworkSceneHandle left, NetworkSceneHandle right) => !left.Equals(right);
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
public static bool operator ==(SceneHandle left, NetworkSceneHandle right) => left.Equals(right.m_Handle);
public static bool operator !=(SceneHandle left, NetworkSceneHandle right) => !left.Equals(right.m_Handle);
public static bool operator ==(NetworkSceneHandle left, SceneHandle right) => left.Equals(right);
public static bool operator !=(NetworkSceneHandle left, SceneHandle right) => !left.Equals(right);
#else
public static bool operator ==(int left, NetworkSceneHandle right) => left.Equals(right.m_Handle);
public static bool operator !=(int left, NetworkSceneHandle right) => !left.Equals(right.m_Handle);
public static bool operator ==(NetworkSceneHandle left, int right) => left.Equals(right);
public static bool operator !=(NetworkSceneHandle left, int right) => !left.Equals(right);
#endif
#endregion
}
}