Skip to content

fix: unity collections 2-2-x support in 2022.3 with NGO v1.x.x #3033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ 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)

### Changed

## [1.11.0] - 2024-08-20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,13 @@ public static void WriteNativeListDelta<T>(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))
Expand Down Expand Up @@ -549,7 +554,11 @@ public static void ReadNativeListDelta<T>(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))
Expand All @@ -571,8 +580,13 @@ public static void ReadNativeListDelta<T>(FastBufferReader reader, ref NativeLis
public static unsafe void WriteNativeHashSetDelta<T>(FastBufferWriter writer, ref NativeHashSet<T> value, ref NativeHashSet<T> previousValue) where T : unmanaged, IEquatable<T>
{
// 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)
Expand All @@ -592,8 +606,11 @@ public static unsafe void WriteNativeHashSetDelta<T>(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);
Expand Down Expand Up @@ -643,9 +660,15 @@ public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(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<TKey, TVal>[value.Count];
var changed = stackalloc KVPair<TKey, TVal>[value.Count];
var removed = stackalloc KVPair<TKey, TVal>[previousValue.Count];
#else
var added = stackalloc KeyValue<TKey, TVal>[value.Count()];
var changed = stackalloc KeyValue<TKey, TVal>[value.Count()];
var removed = stackalloc KeyValue<TKey, TVal>[previousValue.Count()];
#endif
var addedCount = 0;
var changedCount = 0;
var removedCount = 0;
Expand All @@ -672,8 +695,11 @@ public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1728,8 +1728,13 @@ internal static unsafe bool ValueEqualsList<TValueType>(ref NativeList<TValueTyp
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
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(TValueType) * a.Length) == 0;
}
#endif
Expand Down Expand Up @@ -1857,9 +1862,14 @@ internal static unsafe bool EqualityEqualsNativeList<TValueType>(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]))
Expand All @@ -1883,7 +1893,11 @@ internal static bool EqualityEqualsNativeHashSet<TValueType>(ref NativeHashSet<T
return true;
}

#if UTP_TRANSPORT_2_0_ABOVE
if (a.Count != b.Count)
#else
if (a.Count() != b.Count())
#endif
{
return false;
}
Expand Down Expand Up @@ -1995,8 +2009,11 @@ internal static bool GenericEqualsNativeHashMap(ref NativeHashMap<TKey, TVal> a,
{
return true;
}

#if UTP_TRANSPORT_2_0_ABOVE
if (a.Count != b.Count)
#else
if (a.Count() != b.Count())
#endif
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,19 @@ public unsafe void NetworkSerialize<T>(BufferSerializer<T> 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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,11 @@ public unsafe void WriteBytes(NativeArray<byte> value, int size = -1, int offset
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void WriteBytes(NativeList<byte> 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);
}

Expand Down Expand Up @@ -816,7 +820,11 @@ public unsafe void WriteBytesSafe(NativeArray<byte> value, int size = -1, int of
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void WriteBytesSafe(NativeList<byte> 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);
}

Expand Down Expand Up @@ -985,7 +993,12 @@ internal unsafe void WriteUnmanagedSafe<T>(NativeArray<T> value) where T : unman
internal unsafe void WriteUnmanaged<T>(NativeList<T> 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);
Expand All @@ -995,7 +1008,11 @@ internal unsafe void WriteUnmanaged<T>(NativeList<T> value) where T : unmanaged
internal unsafe void WriteUnmanagedSafe<T>(NativeList<T> 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);
Expand Down Expand Up @@ -1193,7 +1210,11 @@ public void WriteValue<T>(NativeList<T> value, ForGeneric unused = default) wher
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void WriteValueSafe<T>(NativeHashSet<T> value) where T : unmanaged, IEquatable<T>
{
#if UTP_TRANSPORT_2_0_ABOVE
WriteUnmanagedSafe(value.Count);
#else
WriteUnmanagedSafe(value.Count());
#endif
foreach (var item in value)
{
var iReffable = item;
Expand All @@ -1206,7 +1227,11 @@ internal void WriteValueSafe<TKey, TVal>(NativeHashMap<TKey, TVal> value)
where TKey : unmanaged, IEquatable<TKey>
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);
Expand Down
Loading