Skip to content

Commit f3a4021

Browse files
authored
chore: Merge release/1.3.0 into develop (#2470)
1 parent 9dbaf06 commit f3a4021

14 files changed

+582
-34
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
5151
- Fixed issue where a client would load duplicate scenes of already preloaded scenes during the initial client synchronization and `NetworkSceneManager.ClientSynchronizationMode` was set to `LoadSceneMode.Additive`. (#2383)
5252
- Fixed float NetworkVariables not being rendered properly in the inspector of NetworkObjects. (#2441)
5353

54-
## [1.3.0]
54+
## [1.3.0] - 2023-03-20
5555

5656
### Added
5757

@@ -60,7 +60,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
6060
### Changed
6161

6262
- The UTP component UI has been updated to be more user-friendly for new users by adding a simple toggle to switch between local-only (127.0.0.1) and remote (0.0.0.0) binding modes, using the toggle "Allow Remote Connections" (#2408)
63-
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.1.
63+
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.3.3. (#2450)
6464
- `NetworkShow()` of `NetworkObject`s are delayed until the end of the frame to ensure consistency of delta-driven variables like `NetworkList`.
6565
- Dirty `NetworkObject` are reset at end-of-frame and not at serialization time.
6666
- `NetworkHide()` of an object that was just `NetworkShow()`n produces a warning, as remote clients will _not_ get a spawn/despawn pair.

com.unity.netcode.gameobjects/Editor/Configuration/NetworkPrefabProcessor.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal set
2424
}
2525
}
2626
private static NetworkPrefabsList s_PrefabsList;
27+
private static Dictionary<string, NetworkPrefab> s_PrefabsListPath = new Dictionary<string, NetworkPrefab>();
2728

2829
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
2930
{
@@ -48,6 +49,21 @@ bool ProcessImportedAssets(string[] importedAssets1)
4849
var go = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
4950
if (go.TryGetComponent<NetworkObject>(out _))
5051
{
52+
// Make sure we are not duplicating an already existing entry
53+
if (s_PrefabsListPath.ContainsKey(assetPath))
54+
{
55+
// Is the imported asset different from the one we already have in the list?
56+
if (s_PrefabsListPath[assetPath].Prefab.GetHashCode() != go.GetHashCode())
57+
{
58+
// If so remove the one in the list and continue on to add the imported one
59+
s_PrefabsList.List.Remove(s_PrefabsListPath[assetPath]);
60+
}
61+
else // If they are identical, then just ignore the import
62+
{
63+
continue;
64+
}
65+
}
66+
5167
s_PrefabsList.List.Add(new NetworkPrefab { Prefab = go });
5268
dirty = true;
5369
}
@@ -103,7 +119,19 @@ bool ProcessDeletedAssets(string[] strings)
103119
return;
104120
}
105121
}
122+
// Clear our asset path to prefab table each time
123+
s_PrefabsListPath.Clear();
124+
125+
// Create our asst path to prefab table
126+
foreach (var prefabEntry in s_PrefabsList.List)
127+
{
128+
if (!s_PrefabsListPath.ContainsKey(AssetDatabase.GetAssetPath(prefabEntry.Prefab)))
129+
{
130+
s_PrefabsListPath.Add(AssetDatabase.GetAssetPath(prefabEntry.Prefab), prefabEntry);
131+
}
132+
}
106133

134+
// Process the imported and deleted assets
107135
var markDirty = ProcessImportedAssets(importedAssets);
108136
markDirty &= ProcessDeletedAssets(deletedAssets);
109137

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,55 @@ public bool OnVerifyCanSend(ulong destinationId, Type messageType, NetworkDelive
143143

144144
public bool OnVerifyCanReceive(ulong senderId, Type messageType, FastBufferReader messageContent, ref NetworkContext context)
145145
{
146-
if (m_NetworkManager.PendingClients.TryGetValue(senderId, out PendingClient client) &&
147-
(client.ConnectionState == PendingClient.State.PendingApproval || (client.ConnectionState == PendingClient.State.PendingConnection && messageType != typeof(ConnectionRequestMessage))))
146+
if (m_NetworkManager.IsServer)
148147
{
149-
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
148+
if (messageType == typeof(ConnectionApprovedMessage))
150149
{
151-
NetworkLog.LogWarning($"Message received from {nameof(senderId)}={senderId} before it has been accepted");
150+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
151+
{
152+
NetworkLog.LogError($"A {nameof(ConnectionApprovedMessage)} was received from a client on the server side. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
153+
}
154+
return false;
152155
}
156+
if (m_NetworkManager.PendingClients.TryGetValue(senderId, out PendingClient client) &&
157+
(client.ConnectionState == PendingClient.State.PendingApproval || (client.ConnectionState == PendingClient.State.PendingConnection && messageType != typeof(ConnectionRequestMessage))))
158+
{
159+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
160+
{
161+
NetworkLog.LogWarning($"Message received from {nameof(senderId)}={senderId} before it has been accepted");
162+
}
153163

154-
return false;
164+
return false;
165+
}
166+
167+
if (m_NetworkManager.ConnectedClients.TryGetValue(senderId, out NetworkClient connectedClient) && messageType == typeof(ConnectionRequestMessage))
168+
{
169+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
170+
{
171+
NetworkLog.LogError($"A {nameof(ConnectionRequestMessage)} was received from a client when the connection has already been established. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
172+
}
173+
174+
return false;
175+
}
176+
}
177+
else
178+
{
179+
if (messageType == typeof(ConnectionRequestMessage))
180+
{
181+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
182+
{
183+
NetworkLog.LogError($"A {nameof(ConnectionRequestMessage)} was received from the server on the client side. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
184+
}
185+
return false;
186+
}
187+
if (m_NetworkManager.IsConnectedClient && messageType == typeof(ConnectionApprovedMessage))
188+
{
189+
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
190+
{
191+
NetworkLog.LogError($"A {nameof(ConnectionApprovedMessage)} was received from the server when the connection has already been established. This should not happen. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Message Size: {messageContent.Length}. Message Content: {MessagingSystem.ByteArrayToString(messageContent.ToArray(), 0, messageContent.Length)}");
192+
}
193+
return false;
194+
}
155195
}
156196

157197
return !m_NetworkManager.m_StopProcessingMessages;

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,26 @@ namespace Unity.Netcode
55
/// </summary>
66
internal struct BatchHeader : INetworkSerializeByMemcpy
77
{
8+
internal const ushort MagicValue = 0x1160;
9+
/// <summary>
10+
/// A magic number to detect corrupt messages.
11+
/// Always set to k_MagicValue
12+
/// </summary>
13+
public ushort Magic;
14+
15+
/// <summary>
16+
/// Total number of bytes in the batch.
17+
/// </summary>
18+
public int BatchSize;
19+
20+
/// <summary>
21+
/// Hash of the message to detect corrupt messages.
22+
/// </summary>
23+
public ulong BatchHash;
24+
825
/// <summary>
926
/// Total number of messages in the batch.
1027
/// </summary>
11-
public ushort BatchSize;
28+
public ushort BatchCount;
1229
}
1330
}

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

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Runtime.CompilerServices;
5+
using System.Text;
56
using Unity.Collections;
67
using Unity.Collections.LowLevel.Unsafe;
78
using UnityEngine;
@@ -41,7 +42,7 @@ public SendQueueItem(NetworkDelivery delivery, int writerSize, Allocator writerA
4142
{
4243
Writer = new FastBufferWriter(writerSize, writerAllocator, maxWriterSize);
4344
NetworkDelivery = delivery;
44-
BatchHeader = default;
45+
BatchHeader = new BatchHeader { Magic = BatchHeader.MagicValue };
4546
}
4647
}
4748

@@ -204,6 +205,17 @@ public int GetLocalVersion(Type messageType)
204205
return m_LocalVersions[messageType];
205206
}
206207

208+
internal static string ByteArrayToString(byte[] ba, int offset, int count)
209+
{
210+
var hex = new StringBuilder(ba.Length * 2);
211+
for (int i = offset; i < offset + count; ++i)
212+
{
213+
hex.AppendFormat("{0:x2} ", ba[i]);
214+
}
215+
216+
return hex.ToString();
217+
}
218+
207219
internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float receiveTime)
208220
{
209221
unsafe
@@ -214,18 +226,38 @@ internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float
214226
new FastBufferReader(nativeData + data.Offset, Allocator.None, data.Count);
215227
if (!batchReader.TryBeginRead(sizeof(BatchHeader)))
216228
{
217-
NetworkLog.LogWarning("Received a packet too small to contain a BatchHeader. Ignoring it.");
229+
NetworkLog.LogError("Received a packet too small to contain a BatchHeader. Ignoring it.");
218230
return;
219231
}
220232

221233
batchReader.ReadValue(out BatchHeader batchHeader);
222234

235+
if (batchHeader.Magic != BatchHeader.MagicValue)
236+
{
237+
NetworkLog.LogError($"Received a packet with an invalid Magic Value. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Offset: {data.Offset}, Size: {data.Count}, Full receive array: {ByteArrayToString(data.Array, 0, data.Array.Length)}");
238+
return;
239+
}
240+
241+
if (batchHeader.BatchSize != data.Count)
242+
{
243+
NetworkLog.LogError($"Received a packet with an invalid Batch Size Value. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Offset: {data.Offset}, Size: {data.Count}, Expected Size: {batchHeader.BatchSize}, Full receive array: {ByteArrayToString(data.Array, 0, data.Array.Length)}");
244+
return;
245+
}
246+
247+
var hash = XXHash.Hash64(batchReader.GetUnsafePtrAtCurrentPosition(), batchReader.Length - batchReader.Position);
248+
249+
if (hash != batchHeader.BatchHash)
250+
{
251+
NetworkLog.LogError($"Received a packet with an invalid Hash Value. Please report this to the Netcode for GameObjects team at https://github.com/Unity-Technologies/com.unity.netcode.gameobjects/issues and include the following data: Received Hash: {batchHeader.BatchHash}, Calculated Hash: {hash}, Offset: {data.Offset}, Size: {data.Count}, Full receive array: {ByteArrayToString(data.Array, 0, data.Array.Length)}");
252+
return;
253+
}
254+
223255
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
224256
{
225-
m_Hooks[hookIdx].OnBeforeReceiveBatch(clientId, batchHeader.BatchSize, batchReader.Length);
257+
m_Hooks[hookIdx].OnBeforeReceiveBatch(clientId, batchHeader.BatchCount, batchReader.Length);
226258
}
227259

228-
for (var messageIdx = 0; messageIdx < batchHeader.BatchSize; ++messageIdx)
260+
for (var messageIdx = 0; messageIdx < batchHeader.BatchCount; ++messageIdx)
229261
{
230262

231263
var messageHeader = new MessageHeader();
@@ -237,15 +269,15 @@ internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float
237269
}
238270
catch (OverflowException)
239271
{
240-
NetworkLog.LogWarning("Received a batch that didn't have enough data for all of its batches, ending early!");
272+
NetworkLog.LogError("Received a batch that didn't have enough data for all of its batches, ending early!");
241273
throw;
242274
}
243275

244276
var receivedHeaderSize = batchReader.Position - position;
245277

246278
if (!batchReader.TryBeginRead((int)messageHeader.MessageSize))
247279
{
248-
NetworkLog.LogWarning("Received a message that claimed a size larger than the packet, ending early!");
280+
NetworkLog.LogError("Received a message that claimed a size larger than the packet, ending early!");
249281
return;
250282
}
251283
m_IncomingMessageQueue.Add(new ReceiveQueueItem
@@ -263,7 +295,7 @@ internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float
263295
}
264296
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
265297
{
266-
m_Hooks[hookIdx].OnAfterReceiveBatch(clientId, batchHeader.BatchSize, batchReader.Length);
298+
m_Hooks[hookIdx].OnAfterReceiveBatch(clientId, batchHeader.BatchCount, batchReader.Length);
267299
}
268300
}
269301
}
@@ -648,7 +680,7 @@ internal unsafe int SendPreSerializedMessage<TMessageType>(in FastBufferWriter t
648680

649681
writeQueueItem.Writer.WriteBytes(headerSerializer.GetUnsafePtr(), headerSerializer.Length);
650682
writeQueueItem.Writer.WriteBytes(tmpSerializer.GetUnsafePtr(), tmpSerializer.Length);
651-
writeQueueItem.BatchHeader.BatchSize++;
683+
writeQueueItem.BatchHeader.BatchCount++;
652684
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
653685
{
654686
m_Hooks[hookIdx].OnAfterSendMessage(clientId, ref message, delivery, tmpSerializer.Length + headerSerializer.Length);
@@ -743,31 +775,36 @@ internal unsafe void ProcessSendQueues()
743775
for (var i = 0; i < sendQueueItem.Length; ++i)
744776
{
745777
ref var queueItem = ref sendQueueItem.ElementAt(i);
746-
if (queueItem.BatchHeader.BatchSize == 0)
778+
if (queueItem.BatchHeader.BatchCount == 0)
747779
{
748780
queueItem.Writer.Dispose();
749781
continue;
750782
}
751783

752784
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
753785
{
754-
m_Hooks[hookIdx].OnBeforeSendBatch(clientId, queueItem.BatchHeader.BatchSize, queueItem.Writer.Length, queueItem.NetworkDelivery);
786+
m_Hooks[hookIdx].OnBeforeSendBatch(clientId, queueItem.BatchHeader.BatchCount, queueItem.Writer.Length, queueItem.NetworkDelivery);
755787
}
756788

757789
queueItem.Writer.Seek(0);
758790
#if UNITY_EDITOR || DEVELOPMENT_BUILD
759791
// Skipping the Verify and sneaking the write mark in because we know it's fine.
760-
queueItem.Writer.Handle->AllowedWriteMark = 2;
792+
queueItem.Writer.Handle->AllowedWriteMark = sizeof(BatchHeader);
761793
#endif
794+
queueItem.BatchHeader.BatchHash = XXHash.Hash64(queueItem.Writer.GetUnsafePtr() + sizeof(BatchHeader), queueItem.Writer.Length - sizeof(BatchHeader));
795+
796+
queueItem.BatchHeader.BatchSize = queueItem.Writer.Length;
797+
762798
queueItem.Writer.WriteValue(queueItem.BatchHeader);
763799

800+
764801
try
765802
{
766803
m_MessageSender.Send(clientId, queueItem.NetworkDelivery, queueItem.Writer);
767804

768805
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
769806
{
770-
m_Hooks[hookIdx].OnAfterSendBatch(clientId, queueItem.BatchHeader.BatchSize, queueItem.Writer.Length, queueItem.NetworkDelivery);
807+
m_Hooks[hookIdx].OnAfterSendBatch(clientId, queueItem.BatchHeader.BatchCount, queueItem.Writer.Length, queueItem.NetworkDelivery);
771808
}
772809
}
773810
finally

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ public void SetConnectionData(string ipv4Address, ushort port, string listenAddr
637637
{
638638
Address = ipv4Address,
639639
Port = port,
640-
ServerListenAddress = listenAddress ?? string.Empty
640+
ServerListenAddress = listenAddress ?? ipv4Address
641641
};
642642

643643
SetProtocol(ProtocolType.UnityTransport);

0 commit comments

Comments
 (0)