Skip to content

Commit 98c86f0

Browse files
committed
feat: Add built-in serialization for UnityEngine.Pose (#2675)
1 parent e529c74 commit 98c86f0

19 files changed

+458
-23
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1313
- Added `NetworkManager.OnPreShutdown` which is called before the NetworkManager cleans up and shuts down. (#3358)
1414
- Added `FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator)` constructor that uses the `ArraySegment.Offset` as the `FastBufferReader` offset and the `ArraySegment.Count` as the `FastBufferReader` length. (#3320)
1515
- Added `FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator, int length = -1)` constructor that uses the `ArraySegment.Offset` as the `FastBufferReader` offset. (#3320)
16+
- Added serializer for `Pose` (#2675)
1617

1718
### Fixed
1819

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

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ internal static class CodeGenHelpers
4444
public static readonly string UnityVector3_FullName = typeof(Vector3).FullName;
4545
public static readonly string UnityVector4_FullName = typeof(Vector4).FullName;
4646
public static readonly string UnityQuaternion_FullName = typeof(Quaternion).FullName;
47+
public static readonly string UnityPose_FullName = typeof(Pose).FullName;
4748
public static readonly string UnityRay_FullName = typeof(Ray).FullName;
4849
public static readonly string UnityRay2D_FullName = typeof(Ray2D).FullName;
4950

@@ -308,6 +309,11 @@ public static bool IsSerializable(this TypeReference typeReference)
308309
return true;
309310
}
310311

312+
if (typeReference.FullName == UnityPose_FullName)
313+
{
314+
return true;
315+
}
316+
311317
if (typeReference.FullName == UnityRay_FullName)
312318
{
313319
return true;

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

+1
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
562562
typeof(Vector3Int),
563563
typeof(Vector4),
564564
typeof(Quaternion),
565+
typeof(Pose),
565566
typeof(Color),
566567
typeof(Color32),
567568
typeof(Ray),

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

+30
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ public FastBufferWriter GetFastBufferWriter()
231231
/// <param name="value">The values to read/write</param>
232232
public void SerializeValue(ref Quaternion[] value) => m_Implementation.SerializeValue(ref value);
233233

234+
/// <summary>
235+
/// Read or write a Pose value
236+
/// </summary>
237+
/// <param name="value">The value to read/write</param>
238+
public void SerializeValue(ref Pose value) => m_Implementation.SerializeValue(ref value);
239+
240+
/// <summary>
241+
/// Read or write an array of Pose values
242+
/// </summary>
243+
/// <param name="value">The values to read/write</param>
244+
public void SerializeValue(ref Pose[] value) => m_Implementation.SerializeValue(ref value);
245+
234246
/// <summary>
235247
/// Read or write a Color value
236248
/// </summary>
@@ -553,6 +565,24 @@ public bool PreCheck(int amount)
553565
/// <param name="value">The value to read/write</param>
554566
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Implementation.SerializeValuePreChecked(ref value);
555567

568+
/// <summary>
569+
/// Serialize a Pose, "pre-checked", which skips buffer checks.
570+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
571+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
572+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
573+
/// </summary>
574+
/// <param name="value">The value to read/write</param>
575+
public void SerializeValuePreChecked(ref Pose value) => m_Implementation.SerializeValuePreChecked(ref value);
576+
577+
/// <summary>
578+
/// Serialize a Pose array, "pre-checked", which skips buffer checks.
579+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
580+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
581+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
582+
/// </summary>
583+
/// <param name="value">The value to read/write</param>
584+
public void SerializeValuePreChecked(ref Pose[] value) => m_Implementation.SerializeValuePreChecked(ref value);
585+
556586
/// <summary>
557587
/// Serialize a Color, "pre-checked", which skips buffer checks.
558588
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before

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

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
7171
public void SerializeValue(ref Quaternion value) => m_Reader.ReadValueSafe(out value);
7272
public void SerializeValue(ref Quaternion[] value) => m_Reader.ReadValueSafe(out value);
7373

74+
public void SerializeValue(ref Pose value) => m_Reader.ReadValueSafe(out value);
75+
public void SerializeValue(ref Pose[] value) => m_Reader.ReadValueSafe(out value);
76+
7477
public void SerializeValue(ref Color value) => m_Reader.ReadValueSafe(out value);
7578
public void SerializeValue(ref Color[] value) => m_Reader.ReadValueSafe(out value);
7679

@@ -127,6 +130,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
127130
public void SerializeValuePreChecked(ref Quaternion value) => m_Reader.ReadValue(out value);
128131
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Reader.ReadValue(out value);
129132

133+
public void SerializeValuePreChecked(ref Pose value) => m_Reader.ReadValue(out value);
134+
public void SerializeValuePreChecked(ref Pose[] value) => m_Reader.ReadValue(out value);
135+
130136
public void SerializeValuePreChecked(ref Color value) => m_Reader.ReadValue(out value);
131137
public void SerializeValuePreChecked(ref Color[] value) => m_Reader.ReadValue(out value);
132138

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

+6
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unus
7070
public void SerializeValue(ref Quaternion value) => m_Writer.WriteValueSafe(value);
7171
public void SerializeValue(ref Quaternion[] value) => m_Writer.WriteValueSafe(value);
7272

73+
public void SerializeValue(ref Pose value) => m_Writer.WriteValueSafe(value);
74+
public void SerializeValue(ref Pose[] value) => m_Writer.WriteValueSafe(value);
75+
7376
public void SerializeValue(ref Color value) => m_Writer.WriteValueSafe(value);
7477
public void SerializeValue(ref Color[] value) => m_Writer.WriteValueSafe(value);
7578

@@ -128,6 +131,9 @@ public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedSt
128131
public void SerializeValuePreChecked(ref Quaternion value) => m_Writer.WriteValue(value);
129132
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Writer.WriteValue(value);
130133

134+
public void SerializeValuePreChecked(ref Pose value) => m_Writer.WriteValue(value);
135+
public void SerializeValuePreChecked(ref Pose[] value) => m_Writer.WriteValue(value);
136+
131137
public void SerializeValuePreChecked(ref Color value) => m_Writer.WriteValue(value);
132138
public void SerializeValuePreChecked(ref Color[] value) => m_Writer.WriteValue(value);
133139

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

+17
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,23 @@ public static void WriteValuePacked(FastBufferWriter writer, Quaternion rotation
259259
WriteValuePacked(writer, rotation.w);
260260
}
261261

262+
/// <summary>
263+
/// Writes the pose to the buffer.
264+
/// </summary>
265+
/// <param name="writer">The writer to write to</param>
266+
/// <param name="pose">Pose to write</param>
267+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
268+
public static void WriteValuePacked(FastBufferWriter writer, Pose pose)
269+
{
270+
WriteValuePacked(writer, pose.position.x);
271+
WriteValuePacked(writer, pose.position.y);
272+
WriteValuePacked(writer, pose.position.z);
273+
WriteValuePacked(writer, pose.rotation.x);
274+
WriteValuePacked(writer, pose.rotation.y);
275+
WriteValuePacked(writer, pose.rotation.z);
276+
WriteValuePacked(writer, pose.rotation.w);
277+
}
278+
262279
/// <summary>
263280
/// Writes a string in a packed format
264281
/// </summary>

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

+18
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,24 @@ public static void ReadValuePacked(FastBufferReader reader, out Quaternion rotat
278278
ReadValuePacked(reader, out rotation.w);
279279
}
280280

281+
/// <summary>
282+
/// Reads the pose from the stream.
283+
/// </summary>
284+
/// <param name="reader">The reader to read from</param>
285+
/// <param name="pose">Pose to read</param>
286+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
287+
public static void ReadValuePacked(FastBufferReader reader, out Pose pose)
288+
{
289+
pose = new Pose();
290+
ReadValuePacked(reader, out pose.position.x);
291+
ReadValuePacked(reader, out pose.position.y);
292+
ReadValuePacked(reader, out pose.position.z);
293+
ReadValuePacked(reader, out pose.rotation.x);
294+
ReadValuePacked(reader, out pose.rotation.y);
295+
ReadValuePacked(reader, out pose.rotation.z);
296+
ReadValuePacked(reader, out pose.rotation.w);
297+
}
298+
281299
/// <summary>
282300
/// Reads a string in a packed format
283301
/// </summary>

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

+34
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,20 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
13281328
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13291329
public void ReadValue(out Quaternion[] value) => ReadUnmanaged(out value);
13301330

1331+
/// <summary>
1332+
/// Read a Pose
1333+
/// </summary>
1334+
/// <param name="value">the value to read</param>
1335+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1336+
public void ReadValue(out Pose value) => ReadUnmanaged(out value);
1337+
1338+
/// <summary>
1339+
/// Read a Pose array
1340+
/// </summary>
1341+
/// <param name="value">the values to read</param>
1342+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1343+
public void ReadValue(out Pose[] value) => ReadUnmanaged(out value);
1344+
13311345
/// <summary>
13321346
/// Read a Color
13331347
/// </summary>
@@ -1505,6 +1519,26 @@ internal void ReadValueSafeInPlace<TKey, TVal>(ref NativeHashMap<TKey, TVal> val
15051519
[MethodImpl(MethodImplOptions.AggressiveInlining)]
15061520
public void ReadValueSafe(out Quaternion[] value) => ReadUnmanagedSafe(out value);
15071521

1522+
/// <summary>
1523+
/// Read a Pose
1524+
///
1525+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1526+
/// for multiple reads at once by calling TryBeginRead.
1527+
/// </summary>
1528+
/// <param name="value">the value to read</param>
1529+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1530+
public void ReadValueSafe(out Pose value) => ReadUnmanagedSafe(out value);
1531+
1532+
/// <summary>
1533+
/// Read a Pose array
1534+
///
1535+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1536+
/// for multiple reads at once by calling TryBeginRead.
1537+
/// </summary>
1538+
/// <param name="value">the values to read</param>
1539+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1540+
public void ReadValueSafe(out Pose[] value) => ReadUnmanagedSafe(out value);
1541+
15081542
/// <summary>
15091543
/// Read a Color
15101544
///

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

+34
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,20 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
14911491
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14921492
public void WriteValue(Quaternion[] value) => WriteUnmanaged(value);
14931493

1494+
/// <summary>
1495+
/// Write a Pose
1496+
/// </summary>
1497+
/// <param name="value">the value to write</param>
1498+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1499+
public void WriteValue(in Pose value) => WriteUnmanaged(value);
1500+
1501+
/// <summary>
1502+
/// Write a Pose array
1503+
/// </summary>
1504+
/// <param name="value">the values to write</param>
1505+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1506+
public void WriteValue(Pose[] value) => WriteUnmanaged(value);
1507+
14941508
/// <summary>
14951509
/// Write a Color
14961510
/// </summary>
@@ -1667,6 +1681,26 @@ public void WriteValueSafe<T>(NativeList<T> value, ForGeneric unused = default)
16671681
[MethodImpl(MethodImplOptions.AggressiveInlining)]
16681682
public void WriteValueSafe(Quaternion[] value) => WriteUnmanagedSafe(value);
16691683

1684+
/// <summary>
1685+
/// Write a Pose
1686+
///
1687+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1688+
/// for multiple writes at once by calling TryBeginWrite.
1689+
/// </summary>
1690+
/// <param name="value">the value to write</param>
1691+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1692+
public void WriteValueSafe(in Pose value) => WriteUnmanagedSafe(value);
1693+
1694+
/// <summary>
1695+
/// Write a Pose array
1696+
///
1697+
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
1698+
/// for multiple writes at once by calling TryBeginWrite.
1699+
/// </summary>
1700+
/// <param name="value">the values to write</param>
1701+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1702+
public void WriteValueSafe(Pose[] value) => WriteUnmanagedSafe(value);
1703+
16701704
/// <summary>
16711705
/// Write a Color
16721706
///

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

+30
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,18 @@ void SerializeValue<T>(ref NativeList<T> value)
231231
/// <param name="value">The values to read/write</param>
232232
void SerializeValue(ref Quaternion[] value);
233233

234+
/// <summary>
235+
/// Read or write a Pose value
236+
/// </summary>
237+
/// <param name="value">The value to read/write</param>
238+
void SerializeValue(ref Pose value);
239+
240+
/// <summary>
241+
/// Read or write an array of Pose values
242+
/// </summary>
243+
/// <param name="value">The values to read/write</param>
244+
void SerializeValue(ref Pose[] value);
245+
234246
/// <summary>
235247
/// Read or write a Color value
236248
/// </summary>
@@ -530,6 +542,24 @@ void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings u
530542
/// <param name="value">The value to read/write</param>
531543
void SerializeValuePreChecked(ref Quaternion[] value);
532544

545+
/// <summary>
546+
/// Serialize a Pose, "pre-checked", which skips buffer checks.
547+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
548+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
549+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
550+
/// </summary>
551+
/// <param name="value">The value to read/write</param>
552+
void SerializeValuePreChecked(ref Pose value);
553+
554+
/// <summary>
555+
/// Serialize a Pose array, "pre-checked", which skips buffer checks.
556+
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
557+
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
558+
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
559+
/// </summary>
560+
/// <param name="value">The value to read/write</param>
561+
void SerializeValuePreChecked(ref Pose[] value);
562+
533563
/// <summary>
534564
/// Serialize a Color, "pre-checked", which skips buffer checks.
535565
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before

0 commit comments

Comments
 (0)