Skip to content

Commit e3189a2

Browse files
refactor: Remove defines based on UTP version (#3362)
There used to be a time when NGO supported both UTP 1.X and 2.X, which led to the use of defines like `UTP_TRANSPORT_2_0_ABOVE` to gate features that were only usable with UTP 2.X (or by proxy to differentiate code that was different for collections 1.X verus 2.X). In some files (like `UnityTransport.cs`) these defines are used abundantly, to the point of impacting code readability. But NGO 2.X now depends on UTP 2.X, so these defines are now useless since they'll always be defined. This PR thus removes them. Functionally speaking this PR should change absolutely nothing since it just removes code that was never compiled anyway. ## Changelog N/A ## Testing and Documentation - No tests have been added (in fact some were removed). - No documentation changes or additions were necessary. --------- Co-authored-by: Noel Stephens <noel.stephens@unity3d.com>
1 parent c754a2c commit e3189a2

27 files changed

+25
-1235
lines changed

com.unity.netcode.gameobjects/Editor/NetworkManagerRelayIntegration.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#if UNITY_2022_3_OR_NEWER && (RELAY_SDK_INSTALLED && !UNITY_WEBGL ) || (RELAY_SDK_INSTALLED && UNITY_WEBGL && UTP_TRANSPORT_2_0_ABOVE)
1+
#if UNITY_2022_3_OR_NEWER && (RELAY_SDK_INSTALLED && !UNITY_WEBGL ) || (RELAY_SDK_INSTALLED && UNITY_WEBGL)
22
using System;
33
using System.Threading.Tasks;
44
using Unity.Netcode.Transports.UTP;
@@ -109,9 +109,7 @@ private static UnityTransport GetUnityTransport(NetworkManager networkManager, s
109109
{
110110
transport = networkManager.gameObject.AddComponent<UnityTransport>();
111111
}
112-
#if UTP_TRANSPORT_2_0_ABOVE
113112
transport.UseWebSockets = connectionType.StartsWith("ws"); // Probably should be part of SetRelayServerData, but not possible at this point
114-
#endif
115113
networkManager.NetworkConfig.NetworkTransport = transport; // Force using UnityTransport
116114
return transport;
117115
}

com.unity.netcode.gameobjects/Editor/com.unity.netcode.editor.asmdef

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@
4949
"expression": "1.0",
5050
"define": "RELAY_SDK_INSTALLED"
5151
},
52-
{
53-
"name": "com.unity.transport",
54-
"expression": "2.0",
55-
"define": "UTP_TRANSPORT_2_0_ABOVE"
56-
},
5752
{
5853
"name": "com.unity.services.multiplayer",
5954
"expression": "0.2.0",

com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,7 @@ internal unsafe int SendMessage<T>(ref T message, NetworkDelivery delivery, in N
808808
internal unsafe int SendMessage<T>(ref T message, NetworkDelivery delivery, in NativeList<ulong> clientIds)
809809
where T : INetworkMessage
810810
{
811-
#if UTP_TRANSPORT_2_0_ABOVE
812811
return SendMessage(ref message, delivery, new PointerListWrapper<ulong>(clientIds.GetUnsafePtr(), clientIds.Length));
813-
#else
814-
return SendMessage(ref message, delivery, new PointerListWrapper<ulong>((ulong*)clientIds.GetUnsafePtr(), clientIds.Length));
815-
#endif
816812
}
817813

818814
internal unsafe void ProcessSendQueues()

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

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,8 @@ public static void WriteNativeListDelta<T>(FastBufferWriter writer, ref NativeLi
505505
writer.WriteValueSafe(changes);
506506
unsafe
507507
{
508-
#if UTP_TRANSPORT_2_0_ABOVE
509508
var ptr = value.GetUnsafePtr();
510509
var prevPtr = previousValue.GetUnsafePtr();
511-
#else
512-
var ptr = (T*)value.GetUnsafePtr();
513-
var prevPtr = (T*)previousValue.GetUnsafePtr();
514-
#endif
515510
for (int i = 0; i < value.Length; ++i)
516511
{
517512
if (changes.IsSet(i))
@@ -554,11 +549,7 @@ public static void ReadNativeListDelta<T>(FastBufferReader reader, ref NativeLis
554549

555550
unsafe
556551
{
557-
#if UTP_TRANSPORT_2_0_ABOVE
558552
var ptr = value.GetUnsafePtr();
559-
#else
560-
var ptr = (T*)value.GetUnsafePtr();
561-
#endif
562553
for (var i = 0; i < value.Length; ++i)
563554
{
564555
if (changes.IsSet(i))
@@ -601,11 +592,8 @@ public static unsafe void WriteNativeHashSetDelta<T>(FastBufferWriter writer, re
601592
++removedCount;
602593
}
603594
}
604-
#if UTP_TRANSPORT_2_0_ABOVE
595+
605596
if (addedCount + removedCount >= value.Count)
606-
#else
607-
if (addedCount + removedCount >= value.Count())
608-
#endif
609597
{
610598
writer.WriteByteSafe(1);
611599
writer.WriteValueSafe(value);
@@ -655,15 +643,10 @@ public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(FastBufferWriter w
655643
where TVal : unmanaged
656644
{
657645
// See WriteDictionary; this is the same algorithm, adjusted for the NativeHashMap API
658-
#if UTP_TRANSPORT_2_0_ABOVE
659646
var added = stackalloc KVPair<TKey, TVal>[value.Count];
660647
var changed = stackalloc KVPair<TKey, TVal>[value.Count];
661648
var removed = stackalloc KVPair<TKey, TVal>[previousValue.Count];
662-
#else
663-
var added = stackalloc KeyValue<TKey, TVal>[value.Count()];
664-
var changed = stackalloc KeyValue<TKey, TVal>[value.Count()];
665-
var removed = stackalloc KeyValue<TKey, TVal>[previousValue.Count()];
666-
#endif
649+
667650
var addedCount = 0;
668651
var changedCount = 0;
669652
var removedCount = 0;
@@ -692,11 +675,7 @@ public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(FastBufferWriter w
692675
}
693676
}
694677

695-
#if UTP_TRANSPORT_2_0_ABOVE
696678
if (addedCount + removedCount + changedCount >= value.Count)
697-
#else
698-
if (addedCount + removedCount + changedCount >= value.Count())
699-
#endif
700679
{
701680
writer.WriteByteSafe(1);
702681
writer.WriteValueSafe(value);

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Serialization/NetworkVariableEquality.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,8 @@ internal static unsafe bool ValueEqualsList<TValueType>(ref NativeList<TValueTyp
4242
return false;
4343
}
4444

45-
#if UTP_TRANSPORT_2_0_ABOVE
4645
var aptr = a.GetUnsafePtr();
4746
var bptr = b.GetUnsafePtr();
48-
#else
49-
var aptr = (TValueType*)a.GetUnsafePtr();
50-
var bptr = (TValueType*)b.GetUnsafePtr();
51-
#endif
5247

5348
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(TValueType) * a.Length) == 0;
5449
}
@@ -216,13 +211,9 @@ internal static unsafe bool EqualityEqualsNativeList<TValueType>(ref NativeList<
216211
return false;
217212
}
218213

219-
#if UTP_TRANSPORT_2_0_ABOVE
220214
var aptr = a.GetUnsafePtr();
221215
var bptr = b.GetUnsafePtr();
222-
#else
223-
var aptr = (TValueType*)a.GetUnsafePtr();
224-
var bptr = (TValueType*)b.GetUnsafePtr();
225-
#endif
216+
226217
for (var i = 0; i < a.Length; ++i)
227218
{
228219
if (!EqualityEquals(ref aptr[i], ref bptr[i]))
@@ -246,11 +237,7 @@ internal static bool EqualityEqualsNativeHashSet<TValueType>(ref NativeHashSet<T
246237
return true;
247238
}
248239

249-
#if UTP_TRANSPORT_2_0_ABOVE
250240
if (a.Count != b.Count)
251-
#else
252-
if (a.Count() != b.Count())
253-
#endif
254241
{
255242
return false;
256243
}
@@ -333,11 +320,7 @@ internal static bool GenericEqualsNativeHashMap(ref NativeHashMap<TKey, TVal> a,
333320
return true;
334321
}
335322

336-
#if UTP_TRANSPORT_2_0_ABOVE
337323
if (a.Count != b.Count)
338-
#else
339-
if (a.Count() != b.Count())
340-
#endif
341324
{
342325
return false;
343326
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,11 @@ public unsafe void NetworkSerialize<T>(BufferSerializer<T> serializer) where T :
9595
{
9696
if (serializer.IsReader)
9797
{
98-
#if UTP_TRANSPORT_2_0_ABOVE
9998
serializer.GetFastBufferReader().ReadBytesSafe(ptr, length);
100-
#else
101-
serializer.GetFastBufferReader().ReadBytesSafe((byte*)ptr, length);
102-
#endif
10399
}
104100
else
105101
{
106-
#if UTP_TRANSPORT_2_0_ABOVE
107102
serializer.GetFastBufferWriter().WriteBytesSafe(ptr, length);
108-
#else
109-
serializer.GetFastBufferWriter().WriteBytesSafe((byte*)ptr, length);
110-
#endif
111103
}
112104
}
113105
}

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,7 @@ 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
776775
byte* ptr = value.GetUnsafePtr();
777-
#else
778-
byte* ptr = (byte*)value.GetUnsafePtr();
779-
#endif
780776
WriteBytes(ptr, size == -1 ? value.Length : size, offset);
781777
}
782778

@@ -820,11 +816,7 @@ public unsafe void WriteBytesSafe(NativeArray<byte> value, int size = -1, int of
820816
[MethodImpl(MethodImplOptions.AggressiveInlining)]
821817
public unsafe void WriteBytesSafe(NativeList<byte> value, int size = -1, int offset = 0)
822818
{
823-
#if UTP_TRANSPORT_2_0_ABOVE
824819
byte* ptr = value.GetUnsafePtr();
825-
#else
826-
byte* ptr = (byte*)value.GetUnsafePtr();
827-
#endif
828820
WriteBytesSafe(ptr, size == -1 ? value.Length : size, offset);
829821
}
830822

@@ -1015,11 +1007,7 @@ internal unsafe void WriteUnmanagedSafe<T>(NativeArray<T> value) where T : unman
10151007
internal unsafe void WriteUnmanaged<T>(NativeList<T> value) where T : unmanaged
10161008
{
10171009
WriteLength(value.Length);
1018-
#if UTP_TRANSPORT_2_0_ABOVE
10191010
var ptr = value.GetUnsafePtr();
1020-
#else
1021-
var ptr = (T*)value.GetUnsafePtr();
1022-
#endif
10231011
{
10241012
byte* bytes = (byte*)ptr;
10251013
WriteBytes(bytes, sizeof(T) * value.Length);
@@ -1029,11 +1017,7 @@ internal unsafe void WriteUnmanaged<T>(NativeList<T> value) where T : unmanaged
10291017
internal unsafe void WriteUnmanagedSafe<T>(NativeList<T> value) where T : unmanaged
10301018
{
10311019
WriteLengthSafe(value.Length);
1032-
#if UTP_TRANSPORT_2_0_ABOVE
10331020
var ptr = value.GetUnsafePtr();
1034-
#else
1035-
var ptr = (T*)value.GetUnsafePtr();
1036-
#endif
10371021
{
10381022
byte* bytes = (byte*)ptr;
10391023
WriteBytesSafe(bytes, sizeof(T) * value.Length);
@@ -1231,11 +1215,7 @@ public void WriteValue<T>(NativeList<T> value, ForGeneric unused = default) wher
12311215
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12321216
internal void WriteValueSafe<T>(NativeHashSet<T> value) where T : unmanaged, IEquatable<T>
12331217
{
1234-
#if UTP_TRANSPORT_2_0_ABOVE
12351218
WriteLengthSafe(value.Count);
1236-
#else
1237-
WriteLengthSafe(value.Count());
1238-
#endif
12391219
foreach (var item in value)
12401220
{
12411221
var iReffable = item;
@@ -1248,11 +1228,7 @@ internal void WriteValueSafe<TKey, TVal>(NativeHashMap<TKey, TVal> value)
12481228
where TKey : unmanaged, IEquatable<TKey>
12491229
where TVal : unmanaged
12501230
{
1251-
#if UTP_TRANSPORT_2_0_ABOVE
12521231
WriteLengthSafe(value.Count);
1253-
#else
1254-
WriteLengthSafe(value.Count());
1255-
#endif
12561232
foreach (var item in value)
12571233
{
12581234
(var key, var val) = (item.Key, item.Value);

com.unity.netcode.gameobjects/Runtime/Transports/UTP/BatchedReceiveQueue.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
2-
using Unity.Networking.Transport;
3-
#if UTP_TRANSPORT_2_0_ABOVE
42
using Unity.Collections;
53
using Unity.Collections.LowLevel.Unsafe;
6-
#endif
4+
using Unity.Networking.Transport;
75

86
namespace Unity.Netcode.Transports.UTP
97
{
@@ -29,11 +27,7 @@ public BatchedReceiveQueue(DataStreamReader reader)
2927
{
3028
fixed (byte* dataPtr = m_Data)
3129
{
32-
#if UTP_TRANSPORT_2_0_ABOVE
3330
reader.ReadBytesUnsafe(dataPtr, reader.Length);
34-
#else
35-
reader.ReadBytes(dataPtr, reader.Length);
36-
#endif
3731
}
3832
}
3933

@@ -70,11 +64,7 @@ public void PushReader(DataStreamReader reader)
7064
{
7165
fixed (byte* dataPtr = m_Data)
7266
{
73-
#if UTP_TRANSPORT_2_0_ABOVE
7467
reader.ReadBytesUnsafe(dataPtr + m_Offset + m_Length, reader.Length);
75-
#else
76-
reader.ReadBytes(dataPtr + m_Offset + m_Length, reader.Length);
77-
#endif
7868
}
7969
}
8070

com.unity.netcode.gameobjects/Runtime/Transports/UTP/BatchedSendQueue.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,15 @@ public void Dispose()
9494
/// <summary>Write a raw buffer to a DataStreamWriter.</summary>
9595
private unsafe void WriteBytes(ref DataStreamWriter writer, byte* data, int length)
9696
{
97-
#if UTP_TRANSPORT_2_0_ABOVE
9897
writer.WriteBytesUnsafe(data, length);
99-
#else
100-
writer.WriteBytes(data, length);
101-
#endif
10298
}
10399

104100
/// <summary>Append data at the tail of the queue. No safety checks.</summary>
105101
private void AppendDataAtTail(ArraySegment<byte> data)
106102
{
107103
unsafe
108104
{
109-
#if UTP_TRANSPORT_2_0_ABOVE
110105
var writer = new DataStreamWriter(m_Data.GetUnsafePtr() + TailIndex, Capacity - TailIndex);
111-
#else
112-
var writer = new DataStreamWriter((byte*)m_Data.GetUnsafePtr() + TailIndex, Capacity - TailIndex);
113-
#endif
114106

115107
writer.WriteInt(data.Count);
116108

@@ -149,11 +141,7 @@ public bool PushMessage(ArraySegment<byte> message)
149141
{
150142
unsafe
151143
{
152-
#if UTP_TRANSPORT_2_0_ABOVE
153144
UnsafeUtility.MemMove(m_Data.GetUnsafePtr(), m_Data.GetUnsafePtr() + HeadIndex, Length);
154-
#else
155-
UnsafeUtility.MemMove(m_Data.GetUnsafePtr(), (byte*)m_Data.GetUnsafePtr() + HeadIndex, Length);
156-
#endif
157145
}
158146

159147
TailIndex = Length;
@@ -239,11 +227,7 @@ public int FillWriterWithMessages(ref DataStreamWriter writer, int softMaxBytes
239227
if (bytesToWrite > softMaxBytes && bytesToWrite <= writer.Capacity)
240228
{
241229
writer.WriteInt(messageLength);
242-
#if UTP_TRANSPORT_2_0_ABOVE
243230
WriteBytes(ref writer, m_Data.GetUnsafePtr() + reader.GetBytesRead(), messageLength);
244-
#else
245-
WriteBytes(ref writer, (byte*)m_Data.GetUnsafePtr() + reader.GetBytesRead(), messageLength);
246-
#endif
247231

248232
return bytesToWrite;
249233
}
@@ -260,11 +244,7 @@ public int FillWriterWithMessages(ref DataStreamWriter writer, int softMaxBytes
260244
if (bytesWritten + bytesToWrite <= softMaxBytes)
261245
{
262246
writer.WriteInt(messageLength);
263-
#if UTP_TRANSPORT_2_0_ABOVE
264247
WriteBytes(ref writer, m_Data.GetUnsafePtr() + reader.GetBytesRead(), messageLength);
265-
#else
266-
WriteBytes(ref writer, (byte*)m_Data.GetUnsafePtr() + reader.GetBytesRead(), messageLength);
267-
#endif
268248

269249
readerOffset += bytesToWrite;
270250
bytesWritten += bytesToWrite;
@@ -308,11 +288,7 @@ public int FillWriterWithBytes(ref DataStreamWriter writer, int maxBytes = 0)
308288

309289
unsafe
310290
{
311-
#if UTP_TRANSPORT_2_0_ABOVE
312291
WriteBytes(ref writer, m_Data.GetUnsafePtr() + HeadIndex, copyLength);
313-
#else
314-
WriteBytes(ref writer, (byte*)m_Data.GetUnsafePtr() + HeadIndex, copyLength);
315-
#endif
316292
}
317293

318294
return copyLength;

0 commit comments

Comments
 (0)