Skip to content

Commit 6501996

Browse files
Merge branch 'develop-2.0.0' into renovate/develop-2.0.0-recipeengine.modules.wrench-0.x
2 parents b1dd4e2 + dfef393 commit 6501996

38 files changed

+244
-1253
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1010

1111
### Added
1212

13+
- Added `NetworkManager.OnPreShutdown` which is called before the NetworkManager cleans up and shuts down. (#3366)
1314
- Added interpolator types as an inspector view selection for position, rotation, and scale. (#3337)
1415
- Added a new smooth dampening interpolator type that provides a nice balance between precision and smoothing results. (#3337)
1516
- Added `NetworkTimeSystem.TickLatency` property that provides the average latency of a client. (#3337)
@@ -20,6 +21,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
2021

2122
- Fixed issue when using a distributed authority network topology and many clients attempt to connect simultaneously the session owner could max-out the maximum in-flight reliable messages allowed, start dropping packets, and some of the connecting clients would fail to fully synchronize. (#3350)
2223
- Fixed issue when using a distributed authority network topology and scene management was disabled clients would not be able to spawn any new network prefab instances until synchronization was complete. (#3350)
24+
- Fixed issue where an owner that changes ownership, when using a distributed authority network topology, could yield identical previous and current owner identifiers. This could also cause `NetworkTransform` to fail to change ownership which would leave the previous owner still subscribed to network tick events. (#3347)
2325
- Fixed issue where the `MaximumInterpolationTime` could not be modified from within the inspector view or runtime. (#3337)
2426
- Fixed `ChangeOwnership` changing ownership to clients that are not observers. This also happened with automated object distribution. (#3323)
2527
- Fixed issue where `AnticipatedNetworkVariable` previous value returned by `AnticipatedNetworkVariable.OnAuthoritativeValueChanged` is updated correctly on the non-authoritative side. (#3306)

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/Core/NetworkBehaviour.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ internal void InternalOnNetworkDespawn()
826826

827827
/// <summary>
828828
/// In client-server contexts, this method is invoked on both the server and the local client of the owner when <see cref="Netcode.NetworkObject"/> ownership is assigned.
829-
/// In distributed authority contexts, this method is only invoked on the local client that has been assigned ownership of the associated <see cref="Netcode.NetworkObject"/>.
829+
/// In distributed authority contexts, this method is invoked on all clients connected to the session.
830830
/// </summary>
831831
public virtual void OnGainedOwnership() { }
832832

@@ -863,7 +863,7 @@ internal void InternalOnOwnershipChanged(ulong previous, ulong current)
863863
/// <summary>
864864
/// In client-server contexts, this method is invoked on the local client when it loses ownership of the associated <see cref="Netcode.NetworkObject"/>
865865
/// and on the server when any client loses ownership.
866-
/// In distributed authority contexts, this method is only invoked on the local client that has lost ownership of the associated <see cref="Netcode.NetworkObject"/>.
866+
/// In distributed authority contexts, this method is invoked on all clients connected to the session.
867867
/// </summary>
868868
public virtual void OnLostOwnership() { }
869869

@@ -1185,14 +1185,23 @@ internal void MarkVariablesDirty(bool dirty)
11851185
}
11861186
}
11871187

1188-
internal void MarkOwnerReadVariablesDirty()
1188+
/// <summary>
1189+
/// For owner read permissions, when changing ownership we need to do a full synchronization
1190+
/// of all NetworkVariables that are owner read permission based since the owner is the only
1191+
/// instance that knows what the most current values are.
1192+
/// </summary>
1193+
internal void MarkOwnerReadDirtyAndCheckOwnerWriteIsDirty()
11891194
{
11901195
for (int j = 0; j < NetworkVariableFields.Count; j++)
11911196
{
11921197
if (NetworkVariableFields[j].ReadPerm == NetworkVariableReadPermission.Owner)
11931198
{
11941199
NetworkVariableFields[j].SetDirty(true);
11951200
}
1201+
if (NetworkVariableFields[j].WritePerm == NetworkVariableWritePermission.Owner)
1202+
{
1203+
NetworkVariableFields[j].OnCheckIsDirtyState();
1204+
}
11961205
}
11971206
}
11981207

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ internal void NetworkBehaviourUpdate(bool forceSend = false)
108108
foreach (var dirtyobj in m_DirtyNetworkObjects)
109109
{
110110
dirtyobj.PostNetworkVariableWrite(forceSend);
111-
// Once done processing, we set the previous owner id to the current owner id
112-
dirtyobj.PreviousOwnerId = dirtyobj.OwnerClientId;
113111
}
114112
m_DirtyNetworkObjects.Clear();
115113
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,12 @@ public struct ConnectionApprovalRequest
807807
/// </summary>
808808
public event Action OnClientStarted = null;
809809

810+
/// <summary>
811+
/// Subscribe to this event to get notifications before a <see cref="NetworkManager"/> instance is being destroyed.
812+
/// This is useful if you want to use the state of anything the NetworkManager cleans up during its shutdown.
813+
/// </summary>
814+
public event Action OnPreShutdown = null;
815+
810816
/// <summary>
811817
/// This callback is invoked once the local server is stopped.
812818
/// </summary>
@@ -1481,6 +1487,8 @@ internal void ShutdownInternal()
14811487
NetworkLog.LogInfo(nameof(ShutdownInternal));
14821488
}
14831489

1490+
OnPreShutdown?.Invoke();
1491+
14841492
this.UnregisterAllNetworkUpdates();
14851493

14861494
// Everything is shutdown in the order of their dependencies

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,12 +2567,39 @@ internal void MarkVariablesDirty(bool dirty)
25672567
}
25682568
}
25692569

2570-
internal void MarkOwnerReadVariablesDirty()
2570+
/// <summary>
2571+
/// Used when changing ownership, this will mark any owner read permission base NetworkVariables as dirty
2572+
/// and will check if any owner write permission NetworkVariables are dirty (primarily for collections) so
2573+
/// the new owner will get a full state update prior to changing ownership.
2574+
/// </summary>
2575+
/// <remarks>
2576+
/// We have to pass in the original owner and previous owner to "reset" back to the current state of this
2577+
/// NetworkObject in order to preserve the same ownership change flow. By the time this is invoked, the
2578+
/// new and previous owner ids have already been set.
2579+
/// </remarks>
2580+
/// <param name="originalOwnerId">the owner prior to beginning the change in ownership change.</param>
2581+
/// <param name="originalPreviousOwnerId">the previous owner prior to beginning the change in ownership change.</param>
2582+
internal void SynchronizeOwnerNetworkVariables(ulong originalOwnerId, ulong originalPreviousOwnerId)
25712583
{
2584+
var currentOwnerId = OwnerClientId;
2585+
OwnerClientId = originalOwnerId;
2586+
PreviousOwnerId = originalPreviousOwnerId;
25722587
for (int i = 0; i < ChildNetworkBehaviours.Count; i++)
25732588
{
2574-
ChildNetworkBehaviours[i].MarkOwnerReadVariablesDirty();
2589+
ChildNetworkBehaviours[i].MarkOwnerReadDirtyAndCheckOwnerWriteIsDirty();
25752590
}
2591+
2592+
// Now set the new owner and previous owner identifiers back to their original new values
2593+
// before we run the NetworkBehaviourUpdate. For owner read only permissions this order of
2594+
// operations is **particularly important** as we need to first (above) mark things as dirty
2595+
// from the context of the original owner and then second (below) we need to send the messages
2596+
// which requires the new owner to be set for owner read permission NetworkVariables.
2597+
OwnerClientId = currentOwnerId;
2598+
PreviousOwnerId = originalOwnerId;
2599+
2600+
// Force send a state update for all owner read NetworkVariables and any currently dirty
2601+
// owner write NetworkVariables.
2602+
NetworkManager.BehaviourUpdater.NetworkBehaviourUpdate(true);
25762603
}
25772604

25782605
// NGO currently guarantees that the client will receive spawn data for all objects in one network tick.

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,8 @@ private void HandleOwnershipChange(ref NetworkContext context)
381381

382382
if (originalOwner == networkManager.LocalClientId && !networkManager.DistributedAuthorityMode)
383383
{
384-
// Mark any owner read variables as dirty
385-
networkObject.MarkOwnerReadVariablesDirty();
386-
// Immediately queue any pending deltas and order the message before the
387-
// change in ownership message.
388-
networkManager.BehaviourUpdater.NetworkBehaviourUpdate(true);
384+
// Fully synchronize NetworkVariables with either read or write ownership permissions.
385+
networkObject.SynchronizeOwnerNetworkVariables(originalOwner, networkObject.PreviousOwnerId);
389386
}
390387

391388
// Always invoke ownership change notifications

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/NetworkVariable.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,13 @@ public bool CheckDirtyState(bool forceCheck = false)
166166
return isDirty;
167167
}
168168

169+
/// <inheritdoc/>
170+
internal override void OnCheckIsDirtyState()
171+
{
172+
CheckDirtyState();
173+
base.OnCheckIsDirtyState();
174+
}
175+
169176
internal ref T RefValue()
170177
{
171178
return ref m_InternalValue;

0 commit comments

Comments
 (0)