Skip to content

Commit 5d7118b

Browse files
committed
Add serializer for the SceneHandle
1 parent c282dfe commit 5d7118b

21 files changed

+956
-522
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/CodeGenHelpers.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
using Unity.CompilationPipeline.Common.Diagnostics;
1111
using Unity.CompilationPipeline.Common.ILPostProcessing;
1212
using UnityEngine;
13+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
14+
using UnityEngine.SceneManagement;
15+
#endif
1316
using Object = System.Object;
1417

1518
namespace Unity.Netcode.Editor.CodeGen
@@ -47,6 +50,7 @@ internal static class CodeGenHelpers
4750
public static readonly string UnityPose_FullName = typeof(Pose).FullName;
4851
public static readonly string UnityRay_FullName = typeof(Ray).FullName;
4952
public static readonly string UnityRay2D_FullName = typeof(Ray2D).FullName;
53+
public static readonly string UnitySceneHandle_FullName = typeof(SceneHandle).FullName;
5054

5155
public static uint Hash(this MethodDefinition methodDefinition)
5256
{
@@ -324,6 +328,11 @@ public static bool IsSerializable(this TypeReference typeReference)
324328
return true;
325329
}
326330

331+
if (typeReference.FullName == UnitySceneHandle_FullName)
332+
{
333+
return true;
334+
}
335+
327336
// Enum
328337
if (typeReference.GetEnumAsInt() != null)
329338
{

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
using UnityEditor;
1313
#endif
1414
using UnityEngine;
15+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
16+
using UnityEngine.SceneManagement;
17+
#endif
1518
using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor;
1619
using MethodAttributes = Mono.Cecil.MethodAttributes;
1720
using ParameterAttributes = Mono.Cecil.ParameterAttributes;
@@ -567,7 +570,8 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
567570
typeof(Color),
568571
typeof(Color32),
569572
typeof(Ray),
570-
typeof(Ray2D)
573+
typeof(Ray2D),
574+
typeof(SceneHandle)
571575
};
572576
internal static readonly Type[] SpecialCaseTypes = new[]
573577
{

com.unity.netcode.gameobjects/Editor/CodeGen/Unity.Netcode.Editor.CodeGen.asmdef

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"name": "com.unity.nuget.mono-cecil",
2525
"expression": "(0,1.11.4)",
2626
"define": "CECIL_CONSTRAINTS_ARE_TYPE_REFERENCES"
27+
},
28+
{
29+
"name": "Unity",
30+
"expression": "6000.3.0a2",
31+
"define": "SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE"
2732
}
2833
],
2934
"noEngineReferences": false

com.unity.netcode.gameobjects/Runtime/Serialization/BufferSerializer.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using Unity.Collections;
33
using UnityEngine;
4+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
5+
using UnityEngine.SceneManagement;
6+
#endif
47

58
namespace Unity.Netcode
69
{
@@ -291,6 +294,20 @@ public FastBufferWriter GetFastBufferWriter()
291294
/// <param name="value">The values to read/write</param>
292295
public void SerializeValue(ref Ray2D[] value) => m_Implementation.SerializeValue(ref value);
293296

297+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
298+
/// <summary>
299+
/// Read or write a SceneHandle value
300+
/// </summary>
301+
/// <param name="value">The value to read/write</param>
302+
public void SerializeValue(ref SceneHandle value) => m_Implementation.SerializeValue(ref value);
303+
304+
/// <summary>
305+
/// Read or write an array of SceneHandle values
306+
/// </summary>
307+
/// <param name="value">The values to read/write</param>
308+
public void SerializeValue(ref SceneHandle[] value) => m_Implementation.SerializeValue(ref value);
309+
#endif
310+
294311
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
295312
// INativeList<bool> provides the Length property
296313
// IUTF8Bytes provides GetUnsafePtr()
@@ -655,6 +672,26 @@ public bool PreCheck(int amount)
655672
/// <param name="value">The value to read/write</param>
656673
public void SerializeValuePreChecked(ref Ray2D[] value) => m_Implementation.SerializeValuePreChecked(ref value);
657674

675+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
676+
/// <summary>
677+
/// Serialize a SceneHandle, "pre-checked", which skips buffer checks.
678+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
679+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
680+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
681+
/// </summary>
682+
/// <param name="value">The value to read/write</param>
683+
public void SerializeValuePreChecked(ref SceneHandle value) => m_Implementation.SerializeValuePreChecked(ref value);
684+
685+
/// <summary>
686+
/// Serialize a SceneHandle array, "pre-checked", which skips buffer checks.
687+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
688+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
689+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
690+
/// </summary>
691+
/// <param name="value">The value to read/write</param>
692+
public void SerializeValuePreChecked(ref SceneHandle[] value) => m_Implementation.SerializeValuePreChecked(ref value);
693+
#endif
694+
658695
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
659696
// INativeList<bool> provides the Length property
660697
// IUTF8Bytes provides GetUnsafePtr()

com.unity.netcode.gameobjects/Runtime/Serialization/BufferSerializerReader.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using Unity.Collections;
33
using UnityEngine;
4+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
5+
using UnityEngine.SceneManagement;
6+
#endif
47

58
namespace Unity.Netcode
69
{
@@ -86,6 +89,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
8689
public void SerializeValue(ref Ray2D value) => m_Reader.ReadValueSafe(out value);
8790
public void SerializeValue(ref Ray2D[] value) => m_Reader.ReadValueSafe(out value);
8891

92+
public void SerializeValue(ref SceneHandle value) => m_Reader.ReadValueSafe(out value);
93+
public void SerializeValue(ref SceneHandle[] value) => m_Reader.ReadValueSafe(out value);
94+
8995
public void SerializeNetworkSerializable<T>(ref T value) where T : INetworkSerializable, new() => m_Reader.ReadNetworkSerializable(out value);
9096

9197
public bool PreCheck(int amount)
@@ -144,5 +150,8 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
144150

145151
public void SerializeValuePreChecked(ref Ray2D value) => m_Reader.ReadValue(out value);
146152
public void SerializeValuePreChecked(ref Ray2D[] value) => m_Reader.ReadValue(out value);
153+
154+
public void SerializeValuePreChecked(ref SceneHandle value) => m_Reader.ReadValue(out value);
155+
public void SerializeValuePreChecked(ref SceneHandle[] value) => m_Reader.ReadValue(out value);
147156
}
148157
}

com.unity.netcode.gameobjects/Runtime/Serialization/BufferSerializerWriter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using Unity.Collections;
33
using UnityEngine;
4+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
5+
using UnityEngine.SceneManagement;
6+
#endif
47

58
namespace Unity.Netcode
69
{
@@ -85,6 +88,11 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
8588
public void SerializeValue(ref Ray2D value) => m_Writer.WriteValueSafe(value);
8689
public void SerializeValue(ref Ray2D[] value) => m_Writer.WriteValueSafe(value);
8790

91+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
92+
public void SerializeValue(ref SceneHandle value) => m_Writer.WriteValueSafe(value);
93+
public void SerializeValue(ref SceneHandle[] value) => m_Writer.WriteValueSafe(value);
94+
#endif
95+
8896
public void SerializeNetworkSerializable<T>(ref T value) where T : INetworkSerializable, new()
8997
{
9098
m_Writer.WriteNetworkSerializable(value);
@@ -145,5 +153,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
145153

146154
public void SerializeValuePreChecked(ref Ray2D value) => m_Writer.WriteValue(value);
147155
public void SerializeValuePreChecked(ref Ray2D[] value) => m_Writer.WriteValue(value);
156+
157+
public void SerializeValuePreChecked(ref SceneHandle value) => m_Writer.WriteValue(value);
158+
public void SerializeValuePreChecked(ref SceneHandle[] value) => m_Writer.WriteValue(value);
159+
148160
}
149161
}

com.unity.netcode.gameobjects/Runtime/Serialization/BytePacker.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Runtime.CompilerServices;
33
using UnityEngine;
4+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
5+
using UnityEngine.SceneManagement;
6+
#endif
47

58
namespace Unity.Netcode
69
{
@@ -271,6 +274,25 @@ public static void WriteValuePacked(FastBufferWriter writer, Pose pose)
271274
WriteValuePacked(writer, pose.rotation);
272275
}
273276

277+
/// <summary>
278+
/// Writes the sceneHandle to the buffer.
279+
/// </summary>
280+
/// <param name="writer">The writer to write to</param>
281+
/// <param name="handle">SceneHandle to write</param>
282+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
283+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
284+
public static void WriteValuePacked(FastBufferWriter writer, SceneHandle handle)
285+
#else
286+
internal static void WriteValuePacked(FastBufferWriter writer, SceneHandle handle)
287+
#endif
288+
{
289+
#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION
290+
WriteValuePacked(writer, handle.GetRawData());
291+
#else
292+
WriteValuePacked(writer, handle);
293+
#endif
294+
}
295+
274296
/// <summary>
275297
/// Writes a string in a packed format
276298
/// </summary>

com.unity.netcode.gameobjects/Runtime/Serialization/ByteUnpacker.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Runtime.CompilerServices;
33
using UnityEngine;
4+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
5+
using UnityEngine.SceneManagement;
6+
#endif
47

58
namespace Unity.Netcode
69
{
@@ -291,6 +294,27 @@ public static void ReadValuePacked(FastBufferReader reader, out Pose pose)
291294
pose = new Pose(position, rotation);
292295
}
293296

297+
/// <summary>
298+
/// Reads the sceneHandle from the stream.
299+
/// </summary>
300+
/// <param name="reader">The reader to read from</param>
301+
/// <param name="sceneHandle">SceneHandle to read</param>
302+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
303+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
304+
public static void ReadValuePacked(FastBufferReader reader, out SceneHandle sceneHandle)
305+
#else
306+
internal static void ReadValuePacked(FastBufferReader reader, out SceneHandle sceneHandle)
307+
#endif
308+
{
309+
#if SCENE_MANAGEMENT_SCENE_HANDLE_NO_INT_CONVERSION
310+
ReadValuePacked(reader, out ulong rawHandle);
311+
sceneHandle = SceneHandle.FromRawData(rawHandle);
312+
#else
313+
ReadValuePacked(reader, out int rawHandle);
314+
sceneHandle = rawHandle;
315+
#endif
316+
}
317+
294318
/// <summary>
295319
/// Reads a string in a packed format
296320
/// </summary>

com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferReader.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
using Unity.Collections;
44
using Unity.Collections.LowLevel.Unsafe;
55
using UnityEngine;
6+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
7+
using UnityEngine.SceneManagement;
8+
#endif
69

710
namespace Unity.Netcode
811
{
@@ -1429,6 +1432,28 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
14291432
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14301433
public void ReadValue(out Ray2D[] value) => ReadUnmanaged(out value);
14311434

1435+
/// <summary>
1436+
/// Read a SceneHandle
1437+
/// </summary>
1438+
/// <param name="value">the value to read</param>
1439+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1440+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
1441+
public void ReadValue(out SceneHandle value) => ReadUnmanaged(out value);
1442+
#else
1443+
internal void ReadValue(out SceneHandle value) => ReadUnmanaged(out value);
1444+
#endif
1445+
1446+
/// <summary>
1447+
/// Read a SceneHandle array
1448+
/// </summary>
1449+
/// <param name="value">the values to read</param>
1450+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1451+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
1452+
public void ReadValue(out SceneHandle[] value) => ReadUnmanaged(out value);
1453+
#else
1454+
internal void ReadValue(out SceneHandle[] value) => ReadUnmanaged(out value);
1455+
#endif
1456+
14321457

14331458
/// <summary>
14341459
/// Read a Vector2
@@ -1650,6 +1675,34 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
16501675
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16511676
public void ReadValueSafe(out Ray2D[] value) => ReadUnmanagedSafe(out value);
16521677

1678+
/// <summary>
1679+
/// Read a SceneHandle
1680+
///
1681+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1682+
/// for multiple reads at once by calling TryBeginRead.
1683+
/// </summary>
1684+
/// <param name="value">the value to read</param>
1685+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1686+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
1687+
public void ReadValueSafe(out SceneHandle value) => ReadUnmanagedSafe(out value);
1688+
#else
1689+
internal void ReadValueSafe(out SceneHandle value) => ReadUnmanagedSafe(out value);
1690+
#endif
1691+
1692+
/// <summary>
1693+
/// Read a SceneHandle array
1694+
///
1695+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1696+
/// for multiple reads at once by calling TryBeginRead.
1697+
/// </summary>
1698+
/// <param name="value">the values to read</param>
1699+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1700+
#if SCENE_MANAGEMENT_SCENE_HANDLE_AVAILABLE
1701+
public void ReadValueSafe(out SceneHandle[] value) => ReadUnmanagedSafe(out value);
1702+
#else
1703+
internal void ReadValueSafe(out SceneHandle[] value) => ReadUnmanagedSafe(out value);
1704+
#endif
1705+
16531706
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
16541707
// INativeList<bool> provides the Length property
16551708
// IUTF8Bytes provides GetUnsafePtr()

0 commit comments

Comments
 (0)