Skip to content

Commit ca23b3c

Browse files
fix: unity collections 2-2-x support in 2022.3 with NGO v1.x.x (#3033)
* update Adding support for changes to collections in v2.2.x. * update Adding support for changes to collections in v2.2.x. * update Adding changelog entry
1 parent 6d43f0a commit ca23b3c

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1212

1313
### Fixed
1414

15+
- Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033)
1516
- Fixed issue where the `NetworkManagerHelper` was continuing to check for hierarchy changes when in play mode. (#3027)
1617

1718
### Changed

com.unity.netcode.gameobjects/Runtime/NetworkVariable/CollectionSerializationUtility.cs

+28-2
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,13 @@ public static void WriteNativeListDelta<T>(FastBufferWriter writer, ref NativeLi
505505
writer.WriteValueSafe(changes);
506506
unsafe
507507
{
508+
#if UTP_TRANSPORT_2_0_ABOVE
509+
var ptr = value.GetUnsafePtr();
510+
var prevPtr = previousValue.GetUnsafePtr();
511+
#else
508512
var ptr = (T*)value.GetUnsafePtr();
509513
var prevPtr = (T*)previousValue.GetUnsafePtr();
514+
#endif
510515
for (int i = 0; i < value.Length; ++i)
511516
{
512517
if (changes.IsSet(i))
@@ -549,7 +554,11 @@ public static void ReadNativeListDelta<T>(FastBufferReader reader, ref NativeLis
549554

550555
unsafe
551556
{
557+
#if UTP_TRANSPORT_2_0_ABOVE
558+
var ptr = value.GetUnsafePtr();
559+
#else
552560
var ptr = (T*)value.GetUnsafePtr();
561+
#endif
553562
for (var i = 0; i < value.Length; ++i)
554563
{
555564
if (changes.IsSet(i))
@@ -571,8 +580,13 @@ public static void ReadNativeListDelta<T>(FastBufferReader reader, ref NativeLis
571580
public static unsafe void WriteNativeHashSetDelta<T>(FastBufferWriter writer, ref NativeHashSet<T> value, ref NativeHashSet<T> previousValue) where T : unmanaged, IEquatable<T>
572581
{
573582
// See WriteHashSet; this is the same algorithm, adjusted for the NativeHashSet API
583+
#if UTP_TRANSPORT_2_0_ABOVE
584+
var added = stackalloc T[value.Count];
585+
var removed = stackalloc T[previousValue.Count];
586+
#else
574587
var added = stackalloc T[value.Count()];
575588
var removed = stackalloc T[previousValue.Count()];
589+
#endif
576590
var addedCount = 0;
577591
var removedCount = 0;
578592
foreach (var item in value)
@@ -592,8 +606,11 @@ public static unsafe void WriteNativeHashSetDelta<T>(FastBufferWriter writer, re
592606
++removedCount;
593607
}
594608
}
595-
609+
#if UTP_TRANSPORT_2_0_ABOVE
610+
if (addedCount + removedCount >= value.Count)
611+
#else
596612
if (addedCount + removedCount >= value.Count())
613+
#endif
597614
{
598615
writer.WriteByteSafe(1);
599616
writer.WriteValueSafe(value);
@@ -643,9 +660,15 @@ public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(FastBufferWriter w
643660
where TVal : unmanaged
644661
{
645662
// See WriteDictionary; this is the same algorithm, adjusted for the NativeHashMap API
663+
#if UTP_TRANSPORT_2_0_ABOVE
664+
var added = stackalloc KVPair<TKey, TVal>[value.Count];
665+
var changed = stackalloc KVPair<TKey, TVal>[value.Count];
666+
var removed = stackalloc KVPair<TKey, TVal>[previousValue.Count];
667+
#else
646668
var added = stackalloc KeyValue<TKey, TVal>[value.Count()];
647669
var changed = stackalloc KeyValue<TKey, TVal>[value.Count()];
648670
var removed = stackalloc KeyValue<TKey, TVal>[previousValue.Count()];
671+
#endif
649672
var addedCount = 0;
650673
var changedCount = 0;
651674
var removedCount = 0;
@@ -672,8 +695,11 @@ public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(FastBufferWriter w
672695
++removedCount;
673696
}
674697
}
675-
698+
#if UTP_TRANSPORT_2_0_ABOVE
699+
if (addedCount + removedCount + changedCount >= value.Count)
700+
#else
676701
if (addedCount + removedCount + changedCount >= value.Count())
702+
#endif
677703
{
678704
writer.WriteByteSafe(1);
679705
writer.WriteValueSafe(value);

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1728,8 +1728,13 @@ internal static unsafe bool ValueEqualsList<TValueType>(ref NativeList<TValueTyp
17281728
return false;
17291729
}
17301730

1731+
#if UTP_TRANSPORT_2_0_ABOVE
1732+
var aptr = a.GetUnsafePtr();
1733+
var bptr = b.GetUnsafePtr();
1734+
#else
17311735
var aptr = (TValueType*)a.GetUnsafePtr();
17321736
var bptr = (TValueType*)b.GetUnsafePtr();
1737+
#endif
17331738
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(TValueType) * a.Length) == 0;
17341739
}
17351740
#endif
@@ -1857,9 +1862,14 @@ internal static unsafe bool EqualityEqualsNativeList<TValueType>(ref NativeList<
18571862
{
18581863
return false;
18591864
}
1860-
1865+
#if UTP_TRANSPORT_2_0_ABOVE
1866+
var aptr = a.GetUnsafePtr();
1867+
var bptr = b.GetUnsafePtr();
1868+
#else
18611869
var aptr = (TValueType*)a.GetUnsafePtr();
18621870
var bptr = (TValueType*)b.GetUnsafePtr();
1871+
#endif
1872+
18631873
for (var i = 0; i < a.Length; ++i)
18641874
{
18651875
if (!EqualityEquals(ref aptr[i], ref bptr[i]))
@@ -1883,7 +1893,11 @@ internal static bool EqualityEqualsNativeHashSet<TValueType>(ref NativeHashSet<T
18831893
return true;
18841894
}
18851895

1896+
#if UTP_TRANSPORT_2_0_ABOVE
1897+
if (a.Count != b.Count)
1898+
#else
18861899
if (a.Count() != b.Count())
1900+
#endif
18871901
{
18881902
return false;
18891903
}
@@ -1995,8 +2009,11 @@ internal static bool GenericEqualsNativeHashMap(ref NativeHashMap<TKey, TVal> a,
19952009
{
19962010
return true;
19972011
}
1998-
2012+
#if UTP_TRANSPORT_2_0_ABOVE
2013+
if (a.Count != b.Count)
2014+
#else
19992015
if (a.Count() != b.Count())
2016+
#endif
20002017
{
20012018
return false;
20022019
}

com.unity.netcode.gameobjects/Runtime/NetworkVariable/ResizableBitVector.cs

+8
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,19 @@ public unsafe void NetworkSerialize<T>(BufferSerializer<T> serializer) where T :
9595
{
9696
if (serializer.IsReader)
9797
{
98+
#if UTP_TRANSPORT_2_0_ABOVE
99+
serializer.GetFastBufferReader().ReadBytesSafe(ptr, length);
100+
#else
98101
serializer.GetFastBufferReader().ReadBytesSafe((byte*)ptr, length);
102+
#endif
99103
}
100104
else
101105
{
106+
#if UTP_TRANSPORT_2_0_ABOVE
107+
serializer.GetFastBufferWriter().WriteBytesSafe(ptr, length);
108+
#else
102109
serializer.GetFastBufferWriter().WriteBytesSafe((byte*)ptr, length);
110+
#endif
103111
}
104112
}
105113
}

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

+25
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,11 @@ public unsafe void WriteBytes(NativeArray<byte> value, int size = -1, int offset
772772
[MethodImpl(MethodImplOptions.AggressiveInlining)]
773773
public unsafe void WriteBytes(NativeList<byte> value, int size = -1, int offset = 0)
774774
{
775+
#if UTP_TRANSPORT_2_0_ABOVE
776+
byte* ptr = value.GetUnsafePtr();
777+
#else
775778
byte* ptr = (byte*)value.GetUnsafePtr();
779+
#endif
776780
WriteBytes(ptr, size == -1 ? value.Length : size, offset);
777781
}
778782

@@ -816,7 +820,11 @@ public unsafe void WriteBytesSafe(NativeArray<byte> value, int size = -1, int of
816820
[MethodImpl(MethodImplOptions.AggressiveInlining)]
817821
public unsafe void WriteBytesSafe(NativeList<byte> value, int size = -1, int offset = 0)
818822
{
823+
#if UTP_TRANSPORT_2_0_ABOVE
824+
byte* ptr = value.GetUnsafePtr();
825+
#else
819826
byte* ptr = (byte*)value.GetUnsafePtr();
827+
#endif
820828
WriteBytesSafe(ptr, size == -1 ? value.Length : size, offset);
821829
}
822830

@@ -985,7 +993,12 @@ internal unsafe void WriteUnmanagedSafe<T>(NativeArray<T> value) where T : unman
985993
internal unsafe void WriteUnmanaged<T>(NativeList<T> value) where T : unmanaged
986994
{
987995
WriteUnmanaged(value.Length);
996+
997+
#if UTP_TRANSPORT_2_0_ABOVE
998+
var ptr = value.GetUnsafePtr();
999+
#else
9881000
var ptr = (T*)value.GetUnsafePtr();
1001+
#endif
9891002
{
9901003
byte* bytes = (byte*)ptr;
9911004
WriteBytes(bytes, sizeof(T) * value.Length);
@@ -995,7 +1008,11 @@ internal unsafe void WriteUnmanaged<T>(NativeList<T> value) where T : unmanaged
9951008
internal unsafe void WriteUnmanagedSafe<T>(NativeList<T> value) where T : unmanaged
9961009
{
9971010
WriteUnmanagedSafe(value.Length);
1011+
#if UTP_TRANSPORT_2_0_ABOVE
1012+
var ptr = value.GetUnsafePtr();
1013+
#else
9981014
var ptr = (T*)value.GetUnsafePtr();
1015+
#endif
9991016
{
10001017
byte* bytes = (byte*)ptr;
10011018
WriteBytesSafe(bytes, sizeof(T) * value.Length);
@@ -1193,7 +1210,11 @@ public void WriteValue<T>(NativeList<T> value, ForGeneric unused = default) wher
11931210
[MethodImpl(MethodImplOptions.AggressiveInlining)]
11941211
internal void WriteValueSafe<T>(NativeHashSet<T> value) where T : unmanaged, IEquatable<T>
11951212
{
1213+
#if UTP_TRANSPORT_2_0_ABOVE
1214+
WriteUnmanagedSafe(value.Count);
1215+
#else
11961216
WriteUnmanagedSafe(value.Count());
1217+
#endif
11971218
foreach (var item in value)
11981219
{
11991220
var iReffable = item;
@@ -1206,7 +1227,11 @@ internal void WriteValueSafe<TKey, TVal>(NativeHashMap<TKey, TVal> value)
12061227
where TKey : unmanaged, IEquatable<TKey>
12071228
where TVal : unmanaged
12081229
{
1230+
#if UTP_TRANSPORT_2_0_ABOVE
1231+
WriteUnmanagedSafe(value.Count);
1232+
#else
12091233
WriteUnmanagedSafe(value.Count());
1234+
#endif
12101235
foreach (var item in value)
12111236
{
12121237
(var key, var val) = (item.Key, item.Value);

0 commit comments

Comments
 (0)