Skip to content

Commit 5bff58f

Browse files
fix: post 2.0.0-pre.2 fixes and updates (#2962)
* chore: updating changelog and package.json (#2952) * updating changelog and package.json * adding date * fix: rotation not being 1.0 issue using rigid body for motion (#2954) * fix this fixes the issue where a quaternion could potentially be close to 1.0 but off by a very very very small amount. Under this scenario, we Normalize the quaternion prior to invoking MoveRotation. * feat: up-port of network variable traits, anticipation, serialized null references, and removal of animator component requirement (#2957) * update This contains the updates for PR-2820 (Anticipated NetworkVariable and NetworkTransform) with minor adjustments based on updates in the v2.0.0 branch. Updating the package version. Adding updated changelog entries. up-port of PR-2872 up-port of PR-2874. updating change log file * fix Even if the service specifies to synchronize, if scene management is disabled then do not synchronize. * fix Removing some of the logic to determine if session owner should synchronize a connecting client or not. * fix Fixing issue with spawning objects within OnInSceneObjectsSpawned and OnNetworkSessionSynchronized primarily on the session owner side. * update Reducing the notification down to just synchronization since in-scene notification is called on the spawn sweep. * test Adding test for preventing spawning prior to being approved. Adding tests for when spawning during NetworkBehaviour.OnInSceneObjectsSpawned or NetworkBehaviour.OnNetworkSessionSynchronized or both. * fix Up port of #2953 * update Adding change log entries. * style removing white space * update reducing GC overhead during deferred despawn * style Removing whitespaces * revert Reverting deferred spawn update. --------- Co-authored-by: Frank Luong <[email protected]>
1 parent 8b13c4e commit 5bff58f

17 files changed

+514
-45
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
88

9+
## Unreleased
10+
11+
### Added
12+
13+
### Fixed
14+
15+
- Fixed issue where internal delta serialization could not have a byte serializer defined when serializing deltas for other types. Added `[GenerateSerializationForType(typeof(byte))]` to both the `NetworkVariable` and `AnticipatedNetworkVariable` classes to assure a byte serializer is defined.(#2962)
16+
- Fixed issue when scene management was disabled and the session owner would still try to synchronize a late joining client. (#2962)
17+
- Fixed issue when using a distributed authority network topology where it would allow a session owner to spawn a `NetworkObject` prior to being approved. Now, an error message is logged and the `NetworkObject` will not be spawned prior to the client being approved. (#2962)
18+
- Fixed issue where attempting to spawn during `NetworkBehaviour.OnInSceneObjectsSpawned` and `NetworkBehaviour.OnNetworkSessionSynchronized` notifications would throw a collection modified exception. (#2962)
19+
20+
### Changed
21+
922
## [2.0.0-pre.2] - 2024-06-17
1023

1124
### Added

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

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,17 +1250,8 @@ public bool StartServer()
12501250
{
12511251
SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
12521252

1253-
// Notify the server that all in-scnee placed NetworkObjects are spawned at this time.
1254-
foreach (var networkObject in SpawnManager.SpawnedObjectsList)
1255-
{
1256-
networkObject.InternalInSceneNetworkObjectsSpawned();
1257-
}
1258-
12591253
// Notify the server that everything should be synchronized/spawned at this time.
1260-
foreach (var networkObject in SpawnManager.SpawnedObjectsList)
1261-
{
1262-
networkObject.InternalNetworkSessionSynchronized();
1263-
}
1254+
SpawnManager.NotifyNetworkObjectsSynchronized();
12641255
OnServerStarted?.Invoke();
12651256
ConnectionManager.LocalClient.IsApproved = true;
12661257
return true;
@@ -1407,17 +1398,9 @@ private void HostServerInitialize()
14071398
}
14081399

14091400
SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
1410-
// Notify the host that all in-scnee placed NetworkObjects are spawned at this time.
1411-
foreach (var networkObject in SpawnManager.SpawnedObjectsList)
1412-
{
1413-
networkObject.InternalInSceneNetworkObjectsSpawned();
1414-
}
14151401

14161402
// Notify the host that everything should be synchronized/spawned at this time.
1417-
foreach (var networkObject in SpawnManager.SpawnedObjectsList)
1418-
{
1419-
networkObject.InternalNetworkSessionSynchronized();
1420-
}
1403+
SpawnManager.NotifyNetworkObjectsSynchronized();
14211404

14221405
OnServerStarted?.Invoke();
14231406
OnClientStarted?.Invoke();

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,11 @@ internal void SpawnInternal(bool destroyWithScene, ulong ownerClientId, bool pla
15471547

15481548
if (NetworkManager.DistributedAuthorityMode)
15491549
{
1550+
if (NetworkManager.LocalClient == null || !NetworkManager.IsConnectedClient || !NetworkManager.ConnectionManager.LocalClient.IsApproved)
1551+
{
1552+
Debug.LogError($"Cannot spawn {name} until the client is fully connected to the session!");
1553+
return;
1554+
}
15501555
if (NetworkManager.NetworkConfig.EnableSceneManagement)
15511556
{
15521557
NetworkSceneHandle = NetworkManager.SceneManager.ClientSceneHandleToServerSceneHandle[gameObject.scene.handle];
@@ -2436,10 +2441,10 @@ internal bool SetNetworkVariableData(FastBufferReader reader, ulong clientId)
24362441
if (NetworkManager.DistributedAuthorityMode)
24372442
{
24382443
var readerPosition = reader.Position;
2439-
reader.ReadValueSafe(out ushort behaviorCount);
2440-
if (behaviorCount != ChildNetworkBehaviours.Count)
2444+
reader.ReadValueSafe(out ushort behaviourCount);
2445+
if (behaviourCount != ChildNetworkBehaviours.Count)
24412446
{
2442-
Debug.LogError($"Network Behavior Count Mismatch! [{readerPosition}][{reader.Position}]");
2447+
Debug.LogError($"[{name}] Network Behavior Count Mismatch! [In: {behaviourCount} vs Local: {ChildNetworkBehaviours.Count}][StartReaderPos: {readerPosition}] CurrentReaderPos: {reader.Position}]");
24432448
return false;
24442449
}
24452450
}

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ClientConnectedMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int
3131
public void Handle(ref NetworkContext context)
3232
{
3333
var networkManager = (NetworkManager)context.SystemOwner;
34-
if ((ShouldSynchronize || networkManager.CMBServiceConnection) && networkManager.DistributedAuthorityMode && networkManager.LocalClient.IsSessionOwner)
34+
if (ShouldSynchronize && networkManager.NetworkConfig.EnableSceneManagement && networkManager.DistributedAuthorityMode && networkManager.LocalClient.IsSessionOwner)
3535
{
3636
networkManager.SceneManager.SynchronizeNetworkObjects(ClientId);
3737
}

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/ConnectionApprovedMessage.cs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,7 @@ public void Handle(ref NetworkContext context)
224224
networkManager.ConnectionManager.InvokeOnClientConnectedCallback(context.SenderId);
225225

226226
// For convenience, notify all NetworkBehaviours that synchronization is complete.
227-
foreach (var networkObject in networkManager.SpawnManager.SpawnedObjectsList)
228-
{
229-
networkObject.InternalNetworkSessionSynchronized();
230-
}
227+
networkManager.SpawnManager.NotifyNetworkObjectsSynchronized();
231228
}
232229
else
233230
{
@@ -243,13 +240,6 @@ public void Handle(ref NetworkContext context)
243240
// Spawn any in-scene placed NetworkObjects
244241
networkManager.SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
245242

246-
// With scene management enabled and since the session owner doesn't send a Synchronize scene event synchronize itself,
247-
// we need to notify the session owner that all in-scnee placed NetworkObjects are spawned at this time.
248-
foreach (var networkObject in networkManager.SpawnManager.SpawnedObjectsList)
249-
{
250-
networkObject.InternalInSceneNetworkObjectsSpawned();
251-
}
252-
253243
// Spawn the local player of the session owner
254244
if (networkManager.AutoSpawnPlayerPrefabClientSide)
255245
{
@@ -261,10 +251,7 @@ public void Handle(ref NetworkContext context)
261251

262252
// With scene management enabled and since the session owner doesn't send a Synchronize scene event synchronize itself,
263253
// we need to notify the session owner that everything should be synchronized/spawned at this time.
264-
foreach (var networkObject in networkManager.SpawnManager.SpawnedObjectsList)
265-
{
266-
networkObject.InternalNetworkSessionSynchronized();
267-
}
254+
networkManager.SpawnManager.NotifyNetworkObjectsSynchronized();
268255

269256
// When scene management is enabled and since the session owner is synchronizing the service (i.e. acting like host),
270257
// we need to locallyh invoke the OnClientConnected callback at this point in time.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public enum StaleDataHandling
5151
#pragma warning restore IDE0001
5252
[Serializable]
5353
[GenerateSerializationForGenericParameter(0)]
54+
[GenerateSerializationForType(typeof(byte))]
5455
public class AnticipatedNetworkVariable<T> : NetworkVariableBase
5556
{
5657
[SerializeField]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Unity.Netcode
99
/// <typeparam name="T">the unmanaged type for <see cref="NetworkVariable{T}"/> </typeparam>
1010
[Serializable]
1111
[GenerateSerializationForGenericParameter(0)]
12+
[GenerateSerializationForType(typeof(byte))]
1213
public class NetworkVariable<T> : NetworkVariableBase
1314
{
1415
/// <summary>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ public void Duplicate(in NativeHashMap<TKey, TVal> value, ref NativeHashMap<TKey
680680
#endif
681681

682682
/// <summary>
683-
/// Serializer for FixedStrings
683+
/// Serializer for FixedStrings
684684
/// </summary>
685685
/// <typeparam name="T"></typeparam>
686686
internal class FixedStringSerializer<T> : INetworkVariableSerializer<T> where T : unmanaged, INativeList<byte>, IUTF8Bytes
@@ -731,7 +731,7 @@ public unsafe void WriteDelta(FastBufferWriter writer, ref T value, ref T previo
731731
return;
732732
}
733733

734-
writer.WriteByte(0); // Flag that we're sending a delta
734+
writer.WriteByteSafe(0); // Flag that we're sending a delta
735735
BytePacker.WriteValuePacked(writer, value.Length);
736736
writer.WriteValueSafe(changes);
737737
var ptr = value.GetUnsafePtr();
@@ -779,7 +779,7 @@ public unsafe void ReadDelta(FastBufferReader reader, ref T value)
779779
{
780780
if (changes.IsSet(i))
781781
{
782-
reader.ReadByte(out ptr[i]);
782+
reader.ReadByteSafe(out ptr[i]);
783783
}
784784
}
785785
}

com.unity.netcode.gameobjects/Runtime/SceneManagement/NetworkSceneManager.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,10 +2436,7 @@ private void HandleClientSceneEvent(uint sceneEventId)
24362436
NetworkLog.LogInfo($"[Client-{NetworkManager.LocalClientId}][Scene Management Enabled] Synchronization complete!");
24372437
}
24382438
// For convenience, notify all NetworkBehaviours that synchronization is complete.
2439-
foreach (var networkObject in NetworkManager.SpawnManager.SpawnedObjectsList)
2440-
{
2441-
networkObject.InternalNetworkSessionSynchronized();
2442-
}
2439+
NetworkManager.SpawnManager.NotifyNetworkObjectsSynchronized();
24432440

24442441
if (NetworkManager.DistributedAuthorityMode && HasSceneAuthority() && IsRestoringSession)
24452442
{

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,5 +1863,16 @@ internal void DeferredDespawnUpdate(NetworkTime serverTime)
18631863
DeferredDespawnObjects.Remove(deferredObjectEntry);
18641864
}
18651865
}
1866+
1867+
internal void NotifyNetworkObjectsSynchronized()
1868+
{
1869+
// Users could spawn NetworkObjects during these notifications.
1870+
// Create a separate list from the hashset to avoid list modification errors.
1871+
var spawnedObjects = SpawnedObjectsList.ToList();
1872+
foreach (var networkObject in spawnedObjects)
1873+
{
1874+
networkObject.InternalNetworkSessionSynchronized();
1875+
}
1876+
}
18661877
}
18671878
}

0 commit comments

Comments
 (0)