diff --git a/com.unity.netcode.gameobjects/CHANGELOG.md b/com.unity.netcode.gameobjects/CHANGELOG.md index 4502f16caa..609399c947 100644 --- a/com.unity.netcode.gameobjects/CHANGELOG.md +++ b/com.unity.netcode.gameobjects/CHANGELOG.md @@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen ### Fixed +- Fixed issue where collections v2.2.x was not supported when using UTP v2.2.x within Unity v2022.3. (#3033) - Fixed issue where the `NetworkManagerHelper` was continuing to check for hierarchy changes when in play mode. (#3027) ### Changed diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/CollectionSerializationUtility.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/CollectionSerializationUtility.cs index 096b2a4c62..ae8cda6871 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/CollectionSerializationUtility.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/CollectionSerializationUtility.cs @@ -505,8 +505,13 @@ public static void WriteNativeListDelta(FastBufferWriter writer, ref NativeLi writer.WriteValueSafe(changes); unsafe { +#if UTP_TRANSPORT_2_0_ABOVE + var ptr = value.GetUnsafePtr(); + var prevPtr = previousValue.GetUnsafePtr(); +#else var ptr = (T*)value.GetUnsafePtr(); var prevPtr = (T*)previousValue.GetUnsafePtr(); +#endif for (int i = 0; i < value.Length; ++i) { if (changes.IsSet(i)) @@ -549,7 +554,11 @@ public static void ReadNativeListDelta(FastBufferReader reader, ref NativeLis unsafe { +#if UTP_TRANSPORT_2_0_ABOVE + var ptr = value.GetUnsafePtr(); +#else var ptr = (T*)value.GetUnsafePtr(); +#endif for (var i = 0; i < value.Length; ++i) { if (changes.IsSet(i)) @@ -571,8 +580,13 @@ public static void ReadNativeListDelta(FastBufferReader reader, ref NativeLis public static unsafe void WriteNativeHashSetDelta(FastBufferWriter writer, ref NativeHashSet value, ref NativeHashSet previousValue) where T : unmanaged, IEquatable { // See WriteHashSet; this is the same algorithm, adjusted for the NativeHashSet API +#if UTP_TRANSPORT_2_0_ABOVE + var added = stackalloc T[value.Count]; + var removed = stackalloc T[previousValue.Count]; +#else var added = stackalloc T[value.Count()]; var removed = stackalloc T[previousValue.Count()]; +#endif var addedCount = 0; var removedCount = 0; foreach (var item in value) @@ -592,8 +606,11 @@ public static unsafe void WriteNativeHashSetDelta(FastBufferWriter writer, re ++removedCount; } } - +#if UTP_TRANSPORT_2_0_ABOVE + if (addedCount + removedCount >= value.Count) +#else if (addedCount + removedCount >= value.Count()) +#endif { writer.WriteByteSafe(1); writer.WriteValueSafe(value); @@ -643,9 +660,15 @@ public static unsafe void WriteNativeHashMapDelta(FastBufferWriter w where TVal : unmanaged { // See WriteDictionary; this is the same algorithm, adjusted for the NativeHashMap API +#if UTP_TRANSPORT_2_0_ABOVE + var added = stackalloc KVPair[value.Count]; + var changed = stackalloc KVPair[value.Count]; + var removed = stackalloc KVPair[previousValue.Count]; +#else var added = stackalloc KeyValue[value.Count()]; var changed = stackalloc KeyValue[value.Count()]; var removed = stackalloc KeyValue[previousValue.Count()]; +#endif var addedCount = 0; var changedCount = 0; var removedCount = 0; @@ -672,8 +695,11 @@ public static unsafe void WriteNativeHashMapDelta(FastBufferWriter w ++removedCount; } } - +#if UTP_TRANSPORT_2_0_ABOVE + if (addedCount + removedCount + changedCount >= value.Count) +#else if (addedCount + removedCount + changedCount >= value.Count()) +#endif { writer.WriteByteSafe(1); writer.WriteValueSafe(value); diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs index 70c94cf3bb..2d77e568ad 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableSerialization.cs @@ -1728,8 +1728,13 @@ internal static unsafe bool ValueEqualsList(ref NativeList(ref NativeList< { return false; } - +#if UTP_TRANSPORT_2_0_ABOVE + var aptr = a.GetUnsafePtr(); + var bptr = b.GetUnsafePtr(); +#else var aptr = (TValueType*)a.GetUnsafePtr(); var bptr = (TValueType*)b.GetUnsafePtr(); +#endif + for (var i = 0; i < a.Length; ++i) { if (!EqualityEquals(ref aptr[i], ref bptr[i])) @@ -1883,7 +1893,11 @@ internal static bool EqualityEqualsNativeHashSet(ref NativeHashSet a, { return true; } - +#if UTP_TRANSPORT_2_0_ABOVE + if (a.Count != b.Count) +#else if (a.Count() != b.Count()) +#endif { return false; } diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ResizableBitVector.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ResizableBitVector.cs index 5b3ec1e7ca..b4b4b76e83 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ResizableBitVector.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/ResizableBitVector.cs @@ -95,11 +95,19 @@ public unsafe void NetworkSerialize(BufferSerializer serializer) where T : { if (serializer.IsReader) { +#if UTP_TRANSPORT_2_0_ABOVE + serializer.GetFastBufferReader().ReadBytesSafe(ptr, length); +#else serializer.GetFastBufferReader().ReadBytesSafe((byte*)ptr, length); +#endif } else { +#if UTP_TRANSPORT_2_0_ABOVE + serializer.GetFastBufferWriter().WriteBytesSafe(ptr, length); +#else serializer.GetFastBufferWriter().WriteBytesSafe((byte*)ptr, length); +#endif } } } diff --git a/com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs b/com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs index aa50ad6a9a..05c14eb1a2 100644 --- a/com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs +++ b/com.unity.netcode.gameobjects/Runtime/Serialization/FastBufferWriter.cs @@ -772,7 +772,11 @@ public unsafe void WriteBytes(NativeArray value, int size = -1, int offset [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteBytes(NativeList value, int size = -1, int offset = 0) { +#if UTP_TRANSPORT_2_0_ABOVE + byte* ptr = value.GetUnsafePtr(); +#else byte* ptr = (byte*)value.GetUnsafePtr(); +#endif WriteBytes(ptr, size == -1 ? value.Length : size, offset); } @@ -816,7 +820,11 @@ public unsafe void WriteBytesSafe(NativeArray value, int size = -1, int of [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void WriteBytesSafe(NativeList value, int size = -1, int offset = 0) { +#if UTP_TRANSPORT_2_0_ABOVE + byte* ptr = value.GetUnsafePtr(); +#else byte* ptr = (byte*)value.GetUnsafePtr(); +#endif WriteBytesSafe(ptr, size == -1 ? value.Length : size, offset); } @@ -985,7 +993,12 @@ internal unsafe void WriteUnmanagedSafe(NativeArray value) where T : unman internal unsafe void WriteUnmanaged(NativeList value) where T : unmanaged { WriteUnmanaged(value.Length); + +#if UTP_TRANSPORT_2_0_ABOVE + var ptr = value.GetUnsafePtr(); +#else var ptr = (T*)value.GetUnsafePtr(); +#endif { byte* bytes = (byte*)ptr; WriteBytes(bytes, sizeof(T) * value.Length); @@ -995,7 +1008,11 @@ internal unsafe void WriteUnmanaged(NativeList value) where T : unmanaged internal unsafe void WriteUnmanagedSafe(NativeList value) where T : unmanaged { WriteUnmanagedSafe(value.Length); +#if UTP_TRANSPORT_2_0_ABOVE + var ptr = value.GetUnsafePtr(); +#else var ptr = (T*)value.GetUnsafePtr(); +#endif { byte* bytes = (byte*)ptr; WriteBytesSafe(bytes, sizeof(T) * value.Length); @@ -1193,7 +1210,11 @@ public void WriteValue(NativeList value, ForGeneric unused = default) wher [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void WriteValueSafe(NativeHashSet value) where T : unmanaged, IEquatable { +#if UTP_TRANSPORT_2_0_ABOVE + WriteUnmanagedSafe(value.Count); +#else WriteUnmanagedSafe(value.Count()); +#endif foreach (var item in value) { var iReffable = item; @@ -1206,7 +1227,11 @@ internal void WriteValueSafe(NativeHashMap value) where TKey : unmanaged, IEquatable where TVal : unmanaged { +#if UTP_TRANSPORT_2_0_ABOVE + WriteUnmanagedSafe(value.Count); +#else WriteUnmanagedSafe(value.Count()); +#endif foreach (var item in value) { (var key, var val) = (item.Key, item.Value);