Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

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

## [2.0.0-pre.4] - 2024-08-21

### Added

- Added `NetworkVariable.CheckDirtyState` that is to be used in tandem with collections in order to detect whether the collection or an item within the collection has changed. (#3004)

### Fixed

- Fixed issue where nested `NetworkTransform` components were not getting updated. (#3016)
- Fixed issue by adding null checks in `NetworkVariableBase.CanClientRead` and `NetworkVariableBase.CanClientWrite` methods to ensure safe access to `NetworkBehaviour`. (#3012)
- Fixed issue where `FixedStringSerializer<T>` was using `NetworkVariableSerialization<byte>.AreEqual` to determine if two bytes were equal causes an exception to be thrown due to no byte serializer having been defined. (#3009)
- Fixed Issue where a state with dual triggers, inbound and outbound, could cause a false layer to layer state transition message to be sent to non-authority `NetworkAnimator` instances and cause a warning message to be logged. (#3008)
- Fixed issue using collections within `NetworkVariable` where the collection would not detect changes to items or nested items. (#3004)
- Fixed issue where `List`, `Dictionary`, and `HashSet` collections would not uniquely duplicate nested collections. (#3004)
- Fixed issue where `NotAuthorityTarget` would include the service observer in the list of targets to send the RPC to as opposed to excluding the service observer as it should. (#3000)
- Fixed issue where `ProxyRpcTargetGroup` could attempt to send a message if there were no targets to send to. (#3000)

### Changed

- Changed `NetworkAnimator` to automatically switch to owner authoritative mode when using a distributed authority network topology. (#3021)
- Changed permissions exception thrown in `NetworkList` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)
- Changed permissions exception thrown in `NetworkVariable.Value` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)
  • Loading branch information
Unity Technologies committed Aug 21, 2024
1 parent a813ba0 commit eab996f
Show file tree
Hide file tree
Showing 41 changed files with 4,107 additions and 893 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,30 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

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

## [2.0.0-pre.4] - 2024-08-21

### Added

- Added `NetworkVariable.CheckDirtyState` that is to be used in tandem with collections in order to detect whether the collection or an item within the collection has changed. (#3004)

### Fixed

- Fixed issue where nested `NetworkTransform` components were not getting updated. (#3016)
- Fixed issue by adding null checks in `NetworkVariableBase.CanClientRead` and `NetworkVariableBase.CanClientWrite` methods to ensure safe access to `NetworkBehaviour`. (#3012)
- Fixed issue where `FixedStringSerializer<T>` was using `NetworkVariableSerialization<byte>.AreEqual` to determine if two bytes were equal causes an exception to be thrown due to no byte serializer having been defined. (#3009)
- Fixed Issue where a state with dual triggers, inbound and outbound, could cause a false layer to layer state transition message to be sent to non-authority `NetworkAnimator` instances and cause a warning message to be logged. (#3008)
- Fixed issue using collections within `NetworkVariable` where the collection would not detect changes to items or nested items. (#3004)
- Fixed issue where `List`, `Dictionary`, and `HashSet` collections would not uniquely duplicate nested collections. (#3004)
- Fixed issue where `NotAuthorityTarget` would include the service observer in the list of targets to send the RPC to as opposed to excluding the service observer as it should. (#3000)
- Fixed issue where `ProxyRpcTargetGroup` could attempt to send a message if there were no targets to send to. (#3000)

### Changed

- Changed `NetworkAnimator` to automatically switch to owner authoritative mode when using a distributed authority network topology. (#3021)
- Changed permissions exception thrown in `NetworkList` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)
- Changed permissions exception thrown in `NetworkVariable.Value` to exiting early with a logged error that is now a unified permissions message within `NetworkVariableBase`. (#3004)


## [2.0.0-pre.3] - 2024-07-23

### Added
Expand Down
17 changes: 15 additions & 2 deletions Runtime/Components/NetworkAnimator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,13 @@ internal bool IsServerAuthoritative()
/// <summary>
/// Override this method and return false to switch to owner authoritative mode
/// </summary>
/// <remarks>
/// When using a distributed authority network topology, this will default to
/// owner authoritative.
/// </remarks>
protected virtual bool OnIsServerAuthoritative()
{
return true;
return NetworkManager ? !NetworkManager.DistributedAuthorityMode : true;
}

// Animators only support up to 32 parameters
Expand Down Expand Up @@ -851,7 +855,12 @@ private void CheckForStateChange(int layer)
stateChangeDetected = true;
//Debug.Log($"[Cross-Fade] To-Hash: {nt.fullPathHash} | TI-Duration: ({tt.duration}) | TI-Norm: ({tt.normalizedTime}) | From-Hash: ({m_AnimationHash[layer]}) | SI-FPHash: ({st.fullPathHash}) | SI-Norm: ({st.normalizedTime})");
}
else if (!tt.anyState && tt.fullPathHash != m_TransitionHash[layer])
// If we are not transitioned into the "any state" and the animator transition isn't a full path hash (layer to layer) and our pre-built destination state to transition does not contain the
// current layer (i.e. transitioning into a state from another layer) =or= we do contain the layer and the layer contains state to transition to is contained within our pre-built destination
// state then we can handle this transition as a non-cross fade state transition between layers.
// Otherwise, if we don't enter into this then this is a "trigger transition to some state that is now being transitioned back to the Idle state via trigger" or "Dual Triggers" IDLE<-->State.
else if (!tt.anyState && tt.fullPathHash != m_TransitionHash[layer] && (!m_DestinationStateToTransitioninfo.ContainsKey(layer) ||
(m_DestinationStateToTransitioninfo.ContainsKey(layer) && m_DestinationStateToTransitioninfo[layer].ContainsKey(nt.fullPathHash))))
{
// first time in this transition for this layer
m_TransitionHash[layer] = tt.fullPathHash;
Expand All @@ -860,6 +869,10 @@ private void CheckForStateChange(int layer)
animState.CrossFade = false;
animState.Transition = true;
animState.NormalizedTime = tt.normalizedTime;
if (m_DestinationStateToTransitioninfo.ContainsKey(layer) && m_DestinationStateToTransitioninfo[layer].ContainsKey(nt.fullPathHash))
{
animState.DestinationStateHash = nt.fullPathHash;
}
stateChangeDetected = true;
//Debug.Log($"[Transition] TI-Duration: ({tt.duration}) | TI-Norm: ({tt.normalizedTime}) | From-Hash: ({m_AnimationHash[layer]}) |SI-FPHash: ({st.fullPathHash}) | SI-Norm: ({st.normalizedTime})");
}
Expand Down
9 changes: 6 additions & 3 deletions Runtime/Components/NetworkTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,10 @@ private void CleanUpOnDestroyOrDespawn()
#else
var forUpdate = true;
#endif
NetworkManager?.NetworkTransformRegistration(this, forUpdate, false);
if (m_CachedNetworkObject != null)
{
NetworkManager?.NetworkTransformRegistration(m_CachedNetworkObject, forUpdate, false);
}
DeregisterForTickUpdate(this);
CanCommitToTransform = false;
}
Expand Down Expand Up @@ -3069,7 +3072,7 @@ private void InternalInitialization(bool isOwnershipChange = false)
if (CanCommitToTransform)
{
// Make sure authority doesn't get added to updates (no need to do this on the authority side)
m_CachedNetworkManager.NetworkTransformRegistration(this, forUpdate, false);
m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, false);
if (UseHalfFloatPrecision)
{
m_HalfPositionState = new NetworkDeltaPosition(currentPosition, m_CachedNetworkManager.ServerTime.Tick, math.bool3(SyncPositionX, SyncPositionY, SyncPositionZ));
Expand All @@ -3090,7 +3093,7 @@ private void InternalInitialization(bool isOwnershipChange = false)
else
{
// Non-authority needs to be added to updates for interpolation and applying state purposes
m_CachedNetworkManager.NetworkTransformRegistration(this, forUpdate, true);
m_CachedNetworkManager.NetworkTransformRegistration(NetworkObject, forUpdate, true);
// Remove this instance from the tick update
DeregisterForTickUpdate(this);
ResetInterpolatedStateToCurrentAuthoritativeState();
Expand Down
9 changes: 6 additions & 3 deletions Runtime/Core/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ internal void InternalOnNetworkDespawn()
}

/// <summary>
/// Gets called when the local client gains ownership of this object.
/// 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.
/// <para>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"/>.</para>
/// </summary>
public virtual void OnGainedOwnership() { }

Expand Down Expand Up @@ -834,7 +835,9 @@ internal void InternalOnOwnershipChanged(ulong previous, ulong current)
}

/// <summary>
/// Gets called when ownership of this object is lost.
/// In client-server contexts, this method is invoked on the local client when it loses ownership of the associated <see cref="Netcode.NetworkObject"/>
/// and on the server when any client loses ownership.
/// <para>In distributed authority contexts, this method is only invoked on the local client that has lost ownership of the associated <see cref="Netcode.NetworkObject"/>.</para>
/// </summary>
public virtual void OnLostOwnership() { }

Expand Down Expand Up @@ -1138,7 +1141,7 @@ internal void WriteNetworkVariableData(FastBufferWriter writer, ulong targetClie
// Distributed Authority: All clients have read permissions, always try to write the value.
if (NetworkVariableFields[j].CanClientRead(targetClientId))
{
// Write additional NetworkVariable information when length safety is enabled or when in distributed authority mode
// Write additional NetworkVariable information when length safety is enabled or when in distributed authority mode
if (ensureLengthSafety || distributedAuthority)
{
// Write the type being serialized for distributed authority (only for comb-server)
Expand Down
53 changes: 36 additions & 17 deletions Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#endif
using UnityEngine.SceneManagement;
using Debug = UnityEngine.Debug;
using Unity.Netcode.Components;

namespace Unity.Netcode
{
Expand Down Expand Up @@ -215,40 +214,40 @@ internal void PromoteSessionOwner(ulong clientId)
}
}

internal Dictionary<ulong, NetworkTransform> NetworkTransformUpdate = new Dictionary<ulong, NetworkTransform>();
internal Dictionary<ulong, NetworkObject> NetworkTransformUpdate = new Dictionary<ulong, NetworkObject>();
#if COM_UNITY_MODULES_PHYSICS
internal Dictionary<ulong, NetworkTransform> NetworkTransformFixedUpdate = new Dictionary<ulong, NetworkTransform>();
internal Dictionary<ulong, NetworkObject> NetworkTransformFixedUpdate = new Dictionary<ulong, NetworkObject>();
#endif

internal void NetworkTransformRegistration(NetworkTransform networkTransform, bool forUpdate = true, bool register = true)
internal void NetworkTransformRegistration(NetworkObject networkObject, bool onUpdate = true, bool register = true)
{
if (forUpdate)
if (onUpdate)
{
if (register)
{
if (!NetworkTransformUpdate.ContainsKey(networkTransform.NetworkObjectId))
if (!NetworkTransformUpdate.ContainsKey(networkObject.NetworkObjectId))
{
NetworkTransformUpdate.Add(networkTransform.NetworkObjectId, networkTransform);
NetworkTransformUpdate.Add(networkObject.NetworkObjectId, networkObject);
}
}
else
{
NetworkTransformUpdate.Remove(networkTransform.NetworkObjectId);
NetworkTransformUpdate.Remove(networkObject.NetworkObjectId);
}
}
#if COM_UNITY_MODULES_PHYSICS
else
{
if (register)
{
if (!NetworkTransformFixedUpdate.ContainsKey(networkTransform.NetworkObjectId))
if (!NetworkTransformFixedUpdate.ContainsKey(networkObject.NetworkObjectId))
{
NetworkTransformFixedUpdate.Add(networkTransform.NetworkObjectId, networkTransform);
NetworkTransformFixedUpdate.Add(networkObject.NetworkObjectId, networkObject);
}
}
else
{
NetworkTransformFixedUpdate.Remove(networkTransform.NetworkObjectId);
NetworkTransformFixedUpdate.Remove(networkObject.NetworkObjectId);
}
}
#endif
Expand Down Expand Up @@ -289,11 +288,21 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
#if COM_UNITY_MODULES_PHYSICS
case NetworkUpdateStage.FixedUpdate:
{
foreach (var networkTransformEntry in NetworkTransformFixedUpdate)
foreach (var networkObjectEntry in NetworkTransformFixedUpdate)
{
if (networkTransformEntry.Value.gameObject.activeInHierarchy && networkTransformEntry.Value.IsSpawned)
// if not active or not spawned then skip
if (!networkObjectEntry.Value.gameObject.activeInHierarchy || !networkObjectEntry.Value.IsSpawned)
{
networkTransformEntry.Value.OnFixedUpdate();
continue;
}

foreach (var networkTransformEntry in networkObjectEntry.Value.NetworkTransforms)
{
// only update if enabled
if (networkTransformEntry.enabled)
{
networkTransformEntry.OnFixedUpdate();
}
}
}
}
Expand All @@ -308,11 +317,21 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)
case NetworkUpdateStage.PreLateUpdate:
{
// Non-physics based non-authority NetworkTransforms update their states after all other components
foreach (var networkTransformEntry in NetworkTransformUpdate)
foreach (var networkObjectEntry in NetworkTransformUpdate)
{
if (networkTransformEntry.Value.gameObject.activeInHierarchy && networkTransformEntry.Value.IsSpawned)
// if not active or not spawned then skip
if (!networkObjectEntry.Value.gameObject.activeInHierarchy || !networkObjectEntry.Value.IsSpawned)
{
networkTransformEntry.Value.OnUpdate();
continue;
}

foreach (var networkTransformEntry in networkObjectEntry.Value.NetworkTransforms)
{
// only update if enabled
if (networkTransformEntry.enabled)
{
networkTransformEntry.OnUpdate();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2359,7 +2359,7 @@ internal List<NetworkBehaviour> ChildNetworkBehaviours
{
m_ChildNetworkBehaviours.Add(networkBehaviours[i]);
var type = networkBehaviours[i].GetType();
if (type.IsInstanceOfType(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform)))
if (type == typeof(NetworkTransform) || type.IsInstanceOfType(typeof(NetworkTransform)) || type.IsSubclassOf(typeof(NetworkTransform)))
{
if (NetworkTransforms == null)
{
Expand Down
8 changes: 0 additions & 8 deletions Runtime/Messaging/Messages/NetworkVariableDeltaMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ internal struct NetworkVariableDeltaMessage : INetworkMessage

private const string k_Name = "NetworkVariableDeltaMessage";

// DANGO-TODO: Made some modifications here that overlap/won't play nice with EnsureNetworkVariableLenghtSafety.
// Worth either merging or more cleanly separating these codepaths.
public void Serialize(FastBufferWriter writer, int targetVersion)
{
if (!writer.TryBeginWrite(FastBufferWriter.GetWriteSize(NetworkObjectId) + FastBufferWriter.GetWriteSize(NetworkBehaviourIndex)))
Expand Down Expand Up @@ -126,10 +124,6 @@ public void Serialize(FastBufferWriter writer, int targetVersion)
}
else
{
// DANGO-TODO:
// Complex types with custom type serialization (either registered custom types or INetworkSerializable implementations) will be problematic
// Non-complex types always provide a full state update per delta
// DANGO-TODO: Add NetworkListEvent<T>.EventType awareness to the cloud-state server
if (networkManager.DistributedAuthorityMode)
{
var size_marker = writer.Position;
Expand Down Expand Up @@ -167,8 +161,6 @@ public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int
return true;
}

// DANGO-TODO: Made some modifications here that overlap/won't play nice with EnsureNetworkVariableLenghtSafety.
// Worth either merging or more cleanly separating these codepaths.
public void Handle(ref NetworkContext context)
{
var networkManager = (NetworkManager)context.SystemOwner;
Expand Down
12 changes: 12 additions & 0 deletions Runtime/Messaging/RpcTargets/NotAuthorityRpcTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message,
{
continue;
}

// The CMB-Service holds ID 0 and should not be added to the targets
if (clientId == NetworkManager.ServerClientId && m_NetworkManager.CMBServiceConnection)
{
continue;
}
m_GroupSendTarget.Add(clientId);
}
}
Expand All @@ -41,6 +47,12 @@ internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message,
continue;
}

// The CMB-Service holds ID 0 and should not be added to the targets
if (clientId == NetworkManager.ServerClientId && m_NetworkManager.CMBServiceConnection)
{
continue;
}

if (clientId == m_NetworkManager.LocalClientId)
{
m_LocalSendRpcTarget.Send(behaviour, ref message, delivery, rpcParams);
Expand Down
5 changes: 5 additions & 0 deletions Runtime/Messaging/RpcTargets/ProxyRpcTargetGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ internal class ProxyRpcTargetGroup : BaseRpcTarget, IDisposable, IGroupRpcTarget

internal override void Send(NetworkBehaviour behaviour, ref RpcMessage message, NetworkDelivery delivery, RpcParams rpcParams)
{
// If there are no targets then don't attempt to send anything.
if (TargetClientIds.Length == 0 && Ids.Count == 0)
{
return;
}
var proxyMessage = new ProxyMessage { Delivery = delivery, TargetClientIds = TargetClientIds.AsArray(), WrappedMessage = message };
#if DEVELOPMENT_BUILD || UNITY_EDITOR || UNITY_MP_TOOLS_NET_STATS_MONITOR_ENABLED_IN_RELEASE
var size =
Expand Down
1 change: 0 additions & 1 deletion Runtime/NetworkVariable/AnticipatedNetworkVariable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public enum StaleDataHandling
#pragma warning restore IDE0001
[Serializable]
[GenerateSerializationForGenericParameter(0)]
[GenerateSerializationForType(typeof(byte))]
public class AnticipatedNetworkVariable<T> : NetworkVariableBase
{
[SerializeField]
Expand Down
Loading

0 comments on commit eab996f

Please sign in to comment.