2 Commits

Author SHA1 Message Date
Unity Technologies
18ffd5fdc8 com.unity.netcode.gameobjects@1.0.0
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).

## [1.0.0] - 2022-06-27

### Changed

- Changed version to 1.0.0. (#2046)
2022-06-27 00:00:00 +00:00
Unity Technologies
0f7a30d285 com.unity.netcode.gameobjects@1.0.0-pre.10
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).

## [1.0.0-pre.10] - 2022-06-21

### Added

- Added a new `OnTransportFailure` callback to `NetworkManager`. This callback is invoked when the manager's `NetworkTransport` encounters an unrecoverable error. Transport failures also cause the `NetworkManager` to shut down. Currently, this is only used by `UnityTransport` to signal a timeout of its connection to the Unity Relay servers. (#1994)
- Added `NetworkEvent.TransportFailure`, which can be used by implementations of `NetworkTransport` to signal to `NetworkManager` that an unrecoverable error was encountered. (#1994)
- Added test to ensure a warning occurs when nesting NetworkObjects in a NetworkPrefab (#1969)
- Added `NetworkManager.RemoveNetworkPrefab(...)` to remove a prefab from the prefabs list (#1950)

### Changed

- Updated `UnityTransport` dependency on `com.unity.transport` to 1.1.0. (#2025)
- (API Breaking) `ConnectionApprovalCallback` is no longer an `event` and will not allow more than 1 handler registered at a time. Also, `ConnectionApprovalCallback` is now a `Func<>` taking `ConnectionApprovalRequest` in and returning `ConnectionApprovalResponse` back out (#1972)

### Removed

### Fixed
- Fixed issue where dynamically spawned `NetworkObject`s could throw an exception if the scene of origin handle was zero (0) and the `NetworkObject` was already spawned. (#2017)
- Fixed issue where `NetworkObject.Observers` was not being cleared when despawned. (#2009)
- Fixed `NetworkAnimator` could not run in the server authoritative mode. (#2003)
- Fixed issue where late joining clients would get a soft synchronization error if any in-scene placed NetworkObjects were parented under another `NetworkObject`. (#1985)
- Fixed issue where `NetworkBehaviourReference` would throw a type cast exception if using `NetworkBehaviourReference.TryGet` and the component type was not found. (#1984)
- Fixed `NetworkSceneManager` was not sending scene event notifications for the currently active scene and any additively loaded scenes when loading a new scene in `LoadSceneMode.Single` mode. (#1975)
- Fixed issue where one or more clients disconnecting during a scene event would cause `LoadEventCompleted` or `UnloadEventCompleted` to wait until the `NetworkConfig.LoadSceneTimeOut` period before being triggered. (#1973)
- Fixed issues when multiple `ConnectionApprovalCallback`s were registered (#1972)
- Fixed a regression in serialization support: `FixedString`, `Vector2Int`, and `Vector3Int` types can now be used in NetworkVariables and RPCs again without requiring a `ForceNetworkSerializeByMemcpy<>` wrapper. (#1961)
- Fixed generic types that inherit from NetworkBehaviour causing crashes at compile time. (#1976)
- Fixed endless dialog boxes when adding a `NetworkBehaviour` to a `NetworkManager` or vice-versa. (#1947)
- Fixed `NetworkAnimator` issue where it was only synchronizing parameters if the layer or state changed or was transitioning between states. (#1946)
- Fixed `NetworkAnimator` issue where when it did detect a parameter had changed it would send all parameters as opposed to only the parameters that changed. (#1946)
- Fixed `NetworkAnimator` issue where it was not always disposing the `NativeArray` that is allocated when spawned. (#1946)
- Fixed `NetworkAnimator` issue where it was not taking the animation speed or state speed multiplier into consideration. (#1946)
- Fixed `NetworkAnimator` issue where it was not properly synchronizing late joining clients if they joined while `Animator` was transitioning between states. (#1946)
- Fixed `NetworkAnimator` issue where the server was not relaying changes to non-owner clients when a client was the owner. (#1946)
- Fixed issue where the `PacketLoss` metric for tools would return the packet loss over a connection lifetime instead of a single frame. (#2004)
2022-06-21 00:00:00 +00:00
86 changed files with 6421 additions and 1344 deletions

View File

@@ -6,6 +6,48 @@ 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).
## [1.0.0] - 2022-06-27
### Changed
- Changed version to 1.0.0. (#2046)
## [1.0.0-pre.10] - 2022-06-21
### Added
- Added a new `OnTransportFailure` callback to `NetworkManager`. This callback is invoked when the manager's `NetworkTransport` encounters an unrecoverable error. Transport failures also cause the `NetworkManager` to shut down. Currently, this is only used by `UnityTransport` to signal a timeout of its connection to the Unity Relay servers. (#1994)
- Added `NetworkEvent.TransportFailure`, which can be used by implementations of `NetworkTransport` to signal to `NetworkManager` that an unrecoverable error was encountered. (#1994)
- Added test to ensure a warning occurs when nesting NetworkObjects in a NetworkPrefab (#1969)
- Added `NetworkManager.RemoveNetworkPrefab(...)` to remove a prefab from the prefabs list (#1950)
### Changed
- Updated `UnityTransport` dependency on `com.unity.transport` to 1.1.0. (#2025)
- (API Breaking) `ConnectionApprovalCallback` is no longer an `event` and will not allow more than 1 handler registered at a time. Also, `ConnectionApprovalCallback` is now an `Action<>` taking a `ConnectionApprovalRequest` and a `ConnectionApprovalResponse` that the client code must fill (#1972) (#2002)
### Removed
### Fixed
- Fixed issue where dynamically spawned `NetworkObject`s could throw an exception if the scene of origin handle was zero (0) and the `NetworkObject` was already spawned. (#2017)
- Fixed issue where `NetworkObject.Observers` was not being cleared when despawned. (#2009)
- Fixed `NetworkAnimator` could not run in the server authoritative mode. (#2003)
- Fixed issue where late joining clients would get a soft synchronization error if any in-scene placed NetworkObjects were parented under another `NetworkObject`. (#1985)
- Fixed issue where `NetworkBehaviourReference` would throw a type cast exception if using `NetworkBehaviourReference.TryGet` and the component type was not found. (#1984)
- Fixed `NetworkSceneManager` was not sending scene event notifications for the currently active scene and any additively loaded scenes when loading a new scene in `LoadSceneMode.Single` mode. (#1975)
- Fixed issue where one or more clients disconnecting during a scene event would cause `LoadEventCompleted` or `UnloadEventCompleted` to wait until the `NetworkConfig.LoadSceneTimeOut` period before being triggered. (#1973)
- Fixed issues when multiple `ConnectionApprovalCallback`s were registered (#1972)
- Fixed a regression in serialization support: `FixedString`, `Vector2Int`, and `Vector3Int` types can now be used in NetworkVariables and RPCs again without requiring a `ForceNetworkSerializeByMemcpy<>` wrapper. (#1961)
- Fixed generic types that inherit from NetworkBehaviour causing crashes at compile time. (#1976)
- Fixed endless dialog boxes when adding a `NetworkBehaviour` to a `NetworkManager` or vice-versa. (#1947)
- Fixed `NetworkAnimator` issue where it was only synchronizing parameters if the layer or state changed or was transitioning between states. (#1946)
- Fixed `NetworkAnimator` issue where when it did detect a parameter had changed it would send all parameters as opposed to only the parameters that changed. (#1946)
- Fixed `NetworkAnimator` issue where it was not always disposing the `NativeArray` that is allocated when spawned. (#1946)
- Fixed `NetworkAnimator` issue where it was not taking the animation speed or state speed multiplier into consideration. (#1946)
- Fixed `NetworkAnimator` issue where it was not properly synchronizing late joining clients if they joined while `Animator` was transitioning between states. (#1946)
- Fixed `NetworkAnimator` issue where the server was not relaying changes to non-owner clients when a client was the owner. (#1946)
- Fixed issue where the `PacketLoss` metric for tools would return the packet loss over a connection lifetime instead of a single frame. (#2004)
## [1.0.0-pre.9] - 2022-05-10
### Fixed
@@ -19,14 +61,15 @@ Additional documentation and release notes are available at [Multiplayer Documen
### Changed
- `unmanaged` structs are no longer universally accepted as RPC parameters because some structs (i.e., structs with pointers in them, such as `NativeList<T>`) can't be supported by the default memcpy struct serializer. Structs that are intended to be serialized across the network must add `INetworkSerializeByMemcpy` to the interface list (i.e., `struct Foo : INetworkSerializeByMemcpy`). This interface is empty and just serves to mark the struct as compatible with memcpy serialization. For external structs you can't edit, you can pass them to RPCs by wrapping them in `ForceNetworkSerializeByMemcpy<T>`. (#1901)
- Changed requirement to register in-scene placed NetworkObjects with `NetworkManager` in order to respawn them. In-scene placed NetworkObjects are now automatically tracked during runtime and no longer need to be registered as a NetworkPrefab. (#1898)
### Removed
- Removed `SIPTransport` (#1870)
- Removed `ClientNetworkTransform` from the package samples and moved to Boss Room's Utilities package which can be found [here](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority/ClientNetworkTransform.cs) (#1912).
- Removed `ClientNetworkTransform` from the package samples and moved to Boss Room's Utilities package which can be found [here](https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop/blob/main/Packages/com.unity.multiplayer.samples.coop/Utilities/Net/ClientAuthority/ClientNetworkTransform.cs) (#1912)
### Fixed
- Fixed issue where `NetworkSceneManager` did not synchronize despawned in-scene placed NetworkObjects. (#1898)
- Fixed `NetworkTransform` generating false positive rotation delta checks when rolling over between 0 and 360 degrees. (#1890)
- Fixed client throwing an exception if it has messages in the outbound queue when processing the `NetworkEvent.Disconnect` event and is using UTP. (#1884)
- Fixed issue during client synchronization if 'ValidateSceneBeforeLoading' returned false it would halt the client synchronization process resulting in a client that was approved but not synchronized or fully connected with the server. (#1883)
@@ -38,6 +81,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
## [1.0.0-pre.7] - 2022-04-06
### Added
- Added editor only check prior to entering into play mode if the currently open and active scene is in the build list and if not displays a dialog box asking the user if they would like to automatically add it prior to entering into play mode. (#1828)
- Added `UnityTransport` implementation and `com.unity.transport` package dependency (#1823)
- Added `NetworkVariableWritePermission` to `NetworkVariableBase` and implemented `Owner` client writable netvars. (#1762)

View File

@@ -8,8 +8,10 @@ namespace Unity.Netcode
/// Solves for incoming values that are jittered
/// Partially solves for message loss. Unclamped lerping helps hide this, but not completely
/// </summary>
/// <typeparam name="T">The type of interpolated value</typeparam>
public abstract class BufferedLinearInterpolator<T> where T : struct
{
internal float MaxInterpolationBound = 3.0f;
private struct BufferedItem
{
public T Item;
@@ -23,7 +25,7 @@ namespace Unity.Netcode
}
/// <summary>
/// Theres two factors affecting interpolation: buffering (set in NetworkManagers NetworkTimeSystem) and interpolation time, which is the amount of time itll take to reach the target. This is to affect the second one.
/// There's two factors affecting interpolation: buffering (set in NetworkManager's NetworkTimeSystem) and interpolation time, which is the amount of time it'll take to reach the target. This is to affect the second one.
/// </summary>
public float MaximumInterpolationTime = 0.1f;
@@ -72,7 +74,7 @@ namespace Unity.Netcode
private bool InvalidState => m_Buffer.Count == 0 && m_LifetimeConsumedCount == 0;
/// <summary>
/// Resets Interpolator to initial state
/// Resets interpolator to initial state
/// </summary>
public void Clear()
{
@@ -84,6 +86,8 @@ namespace Unity.Netcode
/// <summary>
/// Teleports current interpolation value to targetValue.
/// </summary>
/// <param name="targetValue">The target value to teleport instantly</param>
/// <param name="serverTime">The current server time</param>
public void ResetTo(T targetValue, double serverTime)
{
m_LifetimeConsumedCount = 1;
@@ -158,6 +162,7 @@ namespace Unity.Netcode
/// </summary>
/// <param name="deltaTime">time since call</param>
/// <param name="serverTime">current server time</param>
/// <returns>The newly interpolated value of type 'T'</returns>
public T Update(float deltaTime, NetworkTime serverTime)
{
return Update(deltaTime, serverTime.TimeTicksAgo(1).Time, serverTime.Time);
@@ -169,6 +174,7 @@ namespace Unity.Netcode
/// <param name="deltaTime">time since last call</param>
/// <param name="renderTime">our current time</param>
/// <param name="serverTime">current server time</param>
/// <returns>The newly interpolated value of type 'T'</returns>
public T Update(float deltaTime, double renderTime, double serverTime)
{
TryConsumeFromBuffer(renderTime, serverTime);
@@ -203,10 +209,9 @@ namespace Unity.Netcode
t = 0.0f;
}
if (t > 3.0f) // max extrapolation
if (t > MaxInterpolationBound) // max extrapolation
{
// TODO this causes issues with teleport, investigate
// todo make this configurable
t = 1.0f;
}
}
@@ -222,6 +227,8 @@ namespace Unity.Netcode
/// <summary>
/// Add measurements to be used during interpolation. These will be buffered before being made available to be displayed as "latest value".
/// </summary>
/// <param name="newMeasurement">The new measurement value to use</param>
/// <param name="sentTime">The time to record for measurement</param>
public void AddMeasurement(T newMeasurement, double sentTime)
{
m_NbItemsReceivedThisFrame++;
@@ -251,6 +258,7 @@ namespace Unity.Netcode
/// <summary>
/// Gets latest value from the interpolator. This is updated every update as time goes by.
/// </summary>
/// <returns>The current interpolated value of type 'T'</returns>
public T GetInterpolatedValue()
{
return m_CurrentInterpValue;
@@ -259,33 +267,54 @@ namespace Unity.Netcode
/// <summary>
/// Method to override and adapted to the generic type. This assumes interpolation for that value will be clamped.
/// </summary>
/// <param name="start">The start value (min)</param>
/// <param name="end">The end value (max)</param>
/// <param name="time">The time value used to interpolate between start and end values (pos)</param>
/// <returns>The interpolated value</returns>
protected abstract T Interpolate(T start, T end, float time);
/// <summary>
/// Method to override and adapted to the generic type. This assumes interpolation for that value will not be clamped.
/// </summary>
/// <param name="start">The start value (min)</param>
/// <param name="end">The end value (max)</param>
/// <param name="time">The time value used to interpolate between start and end values (pos)</param>
/// <returns>The interpolated value</returns>
protected abstract T InterpolateUnclamped(T start, T end, float time);
}
/// <inheritdoc />
/// <remarks>
/// This is a buffered linear interpolator for a <see cref="float"/> type value
/// </remarks>
public class BufferedLinearInterpolatorFloat : BufferedLinearInterpolator<float>
{
/// <inheritdoc />
protected override float InterpolateUnclamped(float start, float end, float time)
{
return Mathf.LerpUnclamped(start, end, time);
}
/// <inheritdoc />
protected override float Interpolate(float start, float end, float time)
{
return Mathf.Lerp(start, end, time);
}
}
/// <inheritdoc />
/// <remarks>
/// This is a buffered linear interpolator for a <see cref="Quaternion"/> type value
/// </remarks>
public class BufferedLinearInterpolatorQuaternion : BufferedLinearInterpolator<Quaternion>
{
/// <inheritdoc />
protected override Quaternion InterpolateUnclamped(Quaternion start, Quaternion end, float time)
{
return Quaternion.SlerpUnclamped(start, end, time);
}
/// <inheritdoc />
protected override Quaternion Interpolate(Quaternion start, Quaternion end, float time)
{
return Quaternion.SlerpUnclamped(start, end, time);

View File

@@ -1,10 +1,158 @@
#if COM_UNITY_MODULES_ANIMATION
using System.Collections.Generic;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
namespace Unity.Netcode.Components
{
internal class NetworkAnimatorStateChangeHandler : INetworkUpdateSystem
{
private NetworkAnimator m_NetworkAnimator;
/// <summary>
/// This removes sending RPCs from within RPCs when the
/// server is forwarding updates from clients to clients
/// As well this handles newly connected client synchronization
/// of the existing Animator's state.
/// </summary>
private void FlushMessages()
{
foreach (var clientId in m_ClientsToSynchronize)
{
m_NetworkAnimator.ServerSynchronizeNewPlayer(clientId);
}
m_ClientsToSynchronize.Clear();
foreach (var sendEntry in m_SendParameterUpdates)
{
m_NetworkAnimator.SendParametersUpdateClientRpc(sendEntry.ParametersUpdateMessage, sendEntry.ClientRpcParams);
}
m_SendParameterUpdates.Clear();
foreach (var sendEntry in m_SendTriggerUpdates)
{
m_NetworkAnimator.SendAnimTriggerClientRpc(sendEntry.AnimationTriggerMessage, sendEntry.ClientRpcParams);
}
m_SendTriggerUpdates.Clear();
}
/// <inheritdoc />
public void NetworkUpdate(NetworkUpdateStage updateStage)
{
switch (updateStage)
{
case NetworkUpdateStage.PreUpdate:
{
// Only the server forwards messages and synchronizes players
if (m_NetworkAnimator.NetworkManager.IsServer)
{
// Flush any pending messages
FlushMessages();
}
// Everyone applies any parameters updated
foreach (var parameterUpdate in m_ProcessParameterUpdates)
{
m_NetworkAnimator.UpdateParameters(parameterUpdate);
}
m_ProcessParameterUpdates.Clear();
// Only owners check for Animator changes
if (m_NetworkAnimator.IsOwner && !m_NetworkAnimator.IsServerAuthoritative() || m_NetworkAnimator.IsServerAuthoritative() && m_NetworkAnimator.NetworkManager.IsServer)
{
m_NetworkAnimator.CheckForAnimatorChanges();
}
break;
}
}
}
/// <summary>
/// Clients that need to be synchronized to the relative Animator
/// </summary>
private List<ulong> m_ClientsToSynchronize = new List<ulong>();
/// <summary>
/// When a new client is connected, they are added to the
/// m_ClientsToSynchronize list.
/// </summary>
internal void SynchronizeClient(ulong clientId)
{
m_ClientsToSynchronize.Add(clientId);
}
/// <summary>
/// A pending outgoing Animation update for (n) clients
/// </summary>
private struct AnimationUpdate
{
public ClientRpcParams ClientRpcParams;
public NetworkAnimator.AnimationMessage AnimationMessage;
}
private List<AnimationUpdate> m_SendAnimationUpdates = new List<AnimationUpdate>();
/// <summary>
/// Invoked when a server needs to forwarding an update to the animation state
/// </summary>
internal void SendAnimationUpdate(NetworkAnimator.AnimationMessage animationMessage, ClientRpcParams clientRpcParams = default)
{
m_SendAnimationUpdates.Add(new AnimationUpdate() { ClientRpcParams = clientRpcParams, AnimationMessage = animationMessage });
}
private struct ParameterUpdate
{
public ClientRpcParams ClientRpcParams;
public NetworkAnimator.ParametersUpdateMessage ParametersUpdateMessage;
}
private List<ParameterUpdate> m_SendParameterUpdates = new List<ParameterUpdate>();
/// <summary>
/// Invoked when a server needs to forwarding an update to the parameter state
/// </summary>
internal void SendParameterUpdate(NetworkAnimator.ParametersUpdateMessage parametersUpdateMessage, ClientRpcParams clientRpcParams = default)
{
m_SendParameterUpdates.Add(new ParameterUpdate() { ClientRpcParams = clientRpcParams, ParametersUpdateMessage = parametersUpdateMessage });
}
private List<NetworkAnimator.ParametersUpdateMessage> m_ProcessParameterUpdates = new List<NetworkAnimator.ParametersUpdateMessage>();
internal void ProcessParameterUpdate(NetworkAnimator.ParametersUpdateMessage parametersUpdateMessage)
{
m_ProcessParameterUpdates.Add(parametersUpdateMessage);
}
private struct TriggerUpdate
{
public ClientRpcParams ClientRpcParams;
public NetworkAnimator.AnimationTriggerMessage AnimationTriggerMessage;
}
private List<TriggerUpdate> m_SendTriggerUpdates = new List<TriggerUpdate>();
/// <summary>
/// Invoked when a server needs to forward an update to a Trigger state
/// </summary>
internal void SendTriggerUpdate(NetworkAnimator.AnimationTriggerMessage animationTriggerMessage, ClientRpcParams clientRpcParams = default)
{
m_SendTriggerUpdates.Add(new TriggerUpdate() { ClientRpcParams = clientRpcParams, AnimationTriggerMessage = animationTriggerMessage });
}
internal void DeregisterUpdate()
{
NetworkUpdateLoop.UnregisterNetworkUpdate(this, NetworkUpdateStage.PreUpdate);
}
internal NetworkAnimatorStateChangeHandler(NetworkAnimator networkAnimator)
{
m_NetworkAnimator = networkAnimator;
NetworkUpdateLoop.RegisterNetworkUpdate(this, NetworkUpdateStage.PreUpdate);
}
}
/// <summary>
/// NetworkAnimator enables remote synchronization of <see cref="UnityEngine.Animator"/> state for on network objects.
/// </summary>
@@ -19,7 +167,6 @@ namespace Unity.Netcode.Components
internal float NormalizedTime;
internal int Layer;
internal float Weight;
internal byte[] Parameters;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
@@ -27,6 +174,14 @@ namespace Unity.Netcode.Components
serializer.SerializeValue(ref NormalizedTime);
serializer.SerializeValue(ref Layer);
serializer.SerializeValue(ref Weight);
}
}
internal struct ParametersUpdateMessage : INetworkSerializable
{
internal byte[] Parameters;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref Parameters);
}
}
@@ -34,12 +189,12 @@ namespace Unity.Netcode.Components
internal struct AnimationTriggerMessage : INetworkSerializable
{
internal int Hash;
internal bool Reset;
internal bool IsTriggerSet;
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref Hash);
serializer.SerializeValue(ref Reset);
serializer.SerializeValue(ref IsTriggerSet);
}
}
@@ -54,7 +209,18 @@ namespace Unity.Netcode.Components
}
}
private bool m_SendMessagesAllowed = false;
internal bool IsServerAuthoritative()
{
return OnIsServerAuthoritative();
}
/// <summary>
/// Override this method and return false to switch to owner authoritative mode
/// </summary>
protected virtual bool OnIsServerAuthoritative()
{
return true;
}
// Animators only support up to 32 params
private const int k_MaxAnimationParams = 32;
@@ -62,6 +228,8 @@ namespace Unity.Netcode.Components
private int[] m_TransitionHash;
private int[] m_AnimationHash;
private float[] m_LayerWeights;
private static byte[] s_EmptyArray = new byte[] { };
private NetworkAnimatorStateChangeHandler m_NetworkAnimatorStateChangeHandler;
private unsafe struct AnimatorParamCache
{
@@ -72,6 +240,7 @@ namespace Unity.Netcode.Components
// 128 bytes per Animator
private FastBufferWriter m_ParameterWriter = new FastBufferWriter(k_MaxAnimationParams * sizeof(float), Allocator.Persistent);
private NativeArray<AnimatorParamCache> m_CachedAnimatorParameters;
// We cache these values because UnsafeUtility.EnumToInt uses direct IL that allows a non-boxing conversion
@@ -80,40 +249,86 @@ namespace Unity.Netcode.Components
internal static readonly int AnimatorControllerParameterInt;
internal static readonly int AnimatorControllerParameterFloat;
internal static readonly int AnimatorControllerParameterBool;
internal static readonly int AnimatorControllerParameterTriggerBool;
static AnimationParamEnumWrapper()
{
AnimatorControllerParameterInt = UnsafeUtility.EnumToInt(AnimatorControllerParameterType.Int);
AnimatorControllerParameterFloat = UnsafeUtility.EnumToInt(AnimatorControllerParameterType.Float);
AnimatorControllerParameterBool = UnsafeUtility.EnumToInt(AnimatorControllerParameterType.Bool);
AnimatorControllerParameterTriggerBool = UnsafeUtility.EnumToInt(AnimatorControllerParameterType.Trigger);
}
}
private void Cleanup()
{
if (m_NetworkAnimatorStateChangeHandler != null)
{
m_NetworkAnimatorStateChangeHandler.DeregisterUpdate();
m_NetworkAnimatorStateChangeHandler = null;
}
if (IsServer)
{
NetworkManager.OnClientConnectedCallback -= OnClientConnectedCallback;
}
if (m_CachedAnimatorParameters != null && m_CachedAnimatorParameters.IsCreated)
{
m_CachedAnimatorParameters.Dispose();
}
if (m_ParameterWriter.IsInitialized)
{
m_ParameterWriter.Dispose();
}
}
public override void OnDestroy()
{
if (m_CachedAnimatorParameters.IsCreated)
{
m_CachedAnimatorParameters.Dispose();
}
m_ParameterWriter.Dispose();
Cleanup();
base.OnDestroy();
}
private List<int> m_ParametersToUpdate;
private List<ulong> m_ClientSendList;
private ClientRpcParams m_ClientRpcParams;
public override void OnNetworkSpawn()
{
if (IsServer)
if (IsOwner || IsServer)
{
m_SendMessagesAllowed = true;
int layers = m_Animator.layerCount;
m_TransitionHash = new int[layers];
m_AnimationHash = new int[layers];
m_LayerWeights = new float[layers];
if (IsServer)
{
NetworkManager.OnClientConnectedCallback += OnClientConnectedCallback;
}
// Store off our current layer weights
for (int layer = 0; layer < m_Animator.layerCount; layer++)
{
float layerWeightNow = m_Animator.GetLayerWeight(layer);
if (layerWeightNow != m_LayerWeights[layer])
{
m_LayerWeights[layer] = layerWeightNow;
}
}
if (IsServer)
{
m_ClientSendList = new List<ulong>(128);
m_ClientRpcParams = new ClientRpcParams();
m_ClientRpcParams.Send = new ClientRpcSendParams();
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
}
}
var parameters = m_Animator.parameters;
m_CachedAnimatorParameters = new NativeArray<AnimatorParamCache>(parameters.Length, Allocator.Persistent);
m_ParametersToUpdate = new List<int>(parameters.Length);
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
@@ -142,13 +357,11 @@ namespace Unity.Netcode.Components
case AnimatorControllerParameterType.Int:
var valueInt = m_Animator.GetInteger(cacheParam.Hash);
UnsafeUtility.WriteArrayElement(cacheParam.Value, 0, valueInt);
break;
case AnimatorControllerParameterType.Bool:
var valueBool = m_Animator.GetBool(cacheParam.Hash);
UnsafeUtility.WriteArrayElement(cacheParam.Value, 0, valueBool);
break;
case AnimatorControllerParameterType.Trigger:
default:
break;
}
@@ -156,24 +369,117 @@ namespace Unity.Netcode.Components
m_CachedAnimatorParameters[i] = cacheParam;
}
m_NetworkAnimatorStateChangeHandler = new NetworkAnimatorStateChangeHandler(this);
}
public override void OnNetworkDespawn()
{
m_SendMessagesAllowed = false;
Cleanup();
}
private void FixedUpdate()
/// <summary>
/// Synchronizes newly joined players
/// </summary>
internal void ServerSynchronizeNewPlayer(ulong playerId)
{
if (!m_SendMessagesAllowed || !m_Animator || !m_Animator.enabled)
m_ClientSendList.Clear();
m_ClientSendList.Add(playerId);
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
// With synchronization we send all parameters
m_ParametersToUpdate.Clear();
for (int i = 0; i < m_CachedAnimatorParameters.Length; i++)
{
m_ParametersToUpdate.Add(i);
}
SendParametersUpdate(m_ClientRpcParams);
for (int layer = 0; layer < m_Animator.layerCount; layer++)
{
AnimatorStateInfo st = m_Animator.GetCurrentAnimatorStateInfo(layer);
var stateHash = st.fullPathHash;
var normalizedTime = st.normalizedTime;
var totalSpeed = st.speed * st.speedMultiplier;
var adjustedNormalizedMaxTime = totalSpeed > 0.0f ? 1.0f / totalSpeed : 0.0f;
// NOTE:
// When synchronizing, for now we will just complete the transition and
// synchronize the player to the next state being transitioned into
if (m_Animator.IsInTransition(layer))
{
var tt = m_Animator.GetAnimatorTransitionInfo(layer);
var nextState = m_Animator.GetNextAnimatorStateInfo(layer);
if (nextState.length > 0)
{
var nextStateTotalSpeed = nextState.speed * nextState.speedMultiplier;
var nextStateAdjustedLength = nextState.length * nextStateTotalSpeed;
// TODO: We need to get the transition curve for the target state as well as some
// reasonable RTT estimate in order to get a more precise normalized synchronization time
var transitionTime = Mathf.Min(tt.duration, tt.duration * tt.normalizedTime) * 0.5f;
normalizedTime = Mathf.Min(1.0f, transitionTime > 0.0f ? transitionTime / nextStateAdjustedLength : 0.0f);
}
else
{
normalizedTime = 0.0f;
}
stateHash = nextState.fullPathHash;
}
else
if (st.normalizedTime >= adjustedNormalizedMaxTime)
{
continue;
}
var animMsg = new AnimationMessage
{
StateHash = stateHash,
NormalizedTime = normalizedTime,
Layer = layer,
Weight = m_LayerWeights[layer]
};
// Server always send via client RPC
SendAnimStateClientRpc(animMsg, m_ClientRpcParams);
}
}
private void OnClientConnectedCallback(ulong playerId)
{
m_NetworkAnimatorStateChangeHandler.SynchronizeClient(playerId);
}
internal void CheckForAnimatorChanges()
{
if (!IsOwner && !IsServerAuthoritative() || IsServerAuthoritative() && !IsServer)
{
return;
}
if (CheckParametersChanged())
{
SendParametersUpdate();
}
if (m_Animator.runtimeAnimatorController == null)
{
return;
}
int stateHash;
float normalizedTime;
// This sends updates only if a layer change or transition is happening
for (int layer = 0; layer < m_Animator.layerCount; layer++)
{
int stateHash;
float normalizedTime;
AnimatorStateInfo st = m_Animator.GetCurrentAnimatorStateInfo(layer);
var totalSpeed = st.speed * st.speedMultiplier;
var adjustedNormalizedMaxTime = totalSpeed > 0.0f ? 1.0f / totalSpeed : 0.0f;
// determine if we have reached the end of our state time, if so we can skip
if (st.normalizedTime >= adjustedNormalizedMaxTime)
{
continue;
}
if (!CheckAnimStateChanged(out stateHash, out normalizedTime, layer))
{
continue;
@@ -187,29 +493,120 @@ namespace Unity.Netcode.Components
Weight = m_LayerWeights[layer]
};
m_ParameterWriter.Seek(0);
m_ParameterWriter.Truncate();
WriteParameters(m_ParameterWriter);
animMsg.Parameters = m_ParameterWriter.ToArray();
SendAnimStateClientRpc(animMsg);
if (!IsServer && IsOwner)
{
SendAnimStateServerRpc(animMsg);
}
else
{
SendAnimStateClientRpc(animMsg);
}
}
}
private bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layer)
private void SendParametersUpdate(ClientRpcParams clientRpcParams = default, bool sendDirect = false)
{
m_ParameterWriter.Seek(0);
m_ParameterWriter.Truncate();
WriteParameters(m_ParameterWriter, sendDirect);
var parametersMessage = new ParametersUpdateMessage
{
Parameters = m_ParameterWriter.ToArray()
};
if (!IsServer)
{
SendParametersUpdateServerRpc(parametersMessage);
}
else
{
if (sendDirect)
{
SendParametersUpdateClientRpc(parametersMessage, clientRpcParams);
}
else
{
m_NetworkAnimatorStateChangeHandler.SendParameterUpdate(parametersMessage, clientRpcParams);
}
}
}
/// <summary>
/// Helper function to get the cached value
/// </summary>
unsafe private T GetValue<T>(ref AnimatorParamCache animatorParamCache)
{
T currentValue;
fixed (void* value = animatorParamCache.Value)
{
currentValue = UnsafeUtility.ReadArrayElement<T>(value, 0);
}
return currentValue;
}
/// <summary>
/// Checks if any of the Animator's parameters have changed
/// If so, it fills out m_ParametersToUpdate with the indices of the parameters
/// that have changed. Returns true if any parameters changed.
/// </summary>
unsafe private bool CheckParametersChanged()
{
m_ParametersToUpdate.Clear();
for (int i = 0; i < m_CachedAnimatorParameters.Length; i++)
{
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), i);
var hash = cacheValue.Hash;
if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterInt)
{
var valueInt = m_Animator.GetInteger(hash);
var currentValue = GetValue<int>(ref cacheValue);
if (currentValue != valueInt)
{
m_ParametersToUpdate.Add(i);
continue;
}
}
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool)
{
var valueBool = m_Animator.GetBool(hash);
var currentValue = GetValue<bool>(ref cacheValue);
if (currentValue != valueBool)
{
m_ParametersToUpdate.Add(i);
continue;
}
}
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
{
var valueFloat = m_Animator.GetFloat(hash);
var currentValue = GetValue<float>(ref cacheValue);
if (currentValue != valueFloat)
{
m_ParametersToUpdate.Add(i);
continue;
}
}
}
return m_ParametersToUpdate.Count > 0;
}
/// <summary>
/// Checks if any of the Animator's states have changed
/// </summary>
private unsafe bool CheckAnimStateChanged(out int stateHash, out float normalizedTime, int layer)
{
bool shouldUpdate = false;
stateHash = 0;
normalizedTime = 0;
float layerWeightNow = m_Animator.GetLayerWeight(layer);
if (!Mathf.Approximately(layerWeightNow, m_LayerWeights[layer]))
if (layerWeightNow != m_LayerWeights[layer])
{
m_LayerWeights[layer] = layerWeightNow;
shouldUpdate = true;
return true;
}
if (m_Animator.IsInTransition(layer))
{
AnimatorTransitionInfo tt = m_Animator.GetAnimatorTransitionInfo(layer);
@@ -218,7 +615,7 @@ namespace Unity.Netcode.Components
// first time in this transition for this layer
m_TransitionHash[layer] = tt.fullPathHash;
m_AnimationHash[layer] = 0;
shouldUpdate = true;
return true;
}
}
else
@@ -235,26 +632,26 @@ namespace Unity.Netcode.Components
}
m_TransitionHash[layer] = 0;
m_AnimationHash[layer] = st.fullPathHash;
shouldUpdate = true;
return true;
}
}
return shouldUpdate;
return false;
}
/* $AS TODO: Right now we are not checking for changed values this is because
the read side of this function doesn't have similar logic which would cause
an overflow read because it doesn't know if the value is there or not. So
there needs to be logic to track which indexes changed in order for there
to be proper value change checking. Will revist in 1.1.0.
*/
private unsafe void WriteParameters(FastBufferWriter writer)
/// <summary>
/// Writes all of the Animator's parameters
/// This uses the m_ParametersToUpdate list to write out only
/// the parameters that have changed
/// </summary>
private unsafe void WriteParameters(FastBufferWriter writer, bool sendCacheState)
{
for (int i = 0; i < m_CachedAnimatorParameters.Length; i++)
// Write how many parameter entries we are going to write
BytePacker.WriteValuePacked(writer, (uint)m_ParametersToUpdate.Count);
foreach (var parameterIndex in m_ParametersToUpdate)
{
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), i);
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), parameterIndex);
var hash = cacheValue.Hash;
BytePacker.WriteValuePacked(writer, (uint)parameterIndex);
if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterInt)
{
var valueInt = m_Animator.GetInteger(hash);
@@ -264,39 +661,46 @@ namespace Unity.Netcode.Components
BytePacker.WriteValuePacked(writer, (uint)valueInt);
}
}
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool)
else // Note: Triggers are treated like boolean values
if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool)
{
var valueBool = m_Animator.GetBool(hash);
fixed (void* value = cacheValue.Value)
{
UnsafeUtility.WriteArrayElement(value, 0, valueBool);
writer.WriteValueSafe(valueBool);
BytePacker.WriteValuePacked(writer, valueBool);
}
}
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
else
if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
{
var valueFloat = m_Animator.GetFloat(hash);
fixed (void* value = cacheValue.Value)
{
UnsafeUtility.WriteArrayElement(value, 0, valueFloat);
writer.WriteValueSafe(valueFloat);
BytePacker.WriteValuePacked(writer, valueFloat);
}
}
}
}
/// <summary>
/// Reads all parameters that were updated and applies the values
/// </summary>
private unsafe void ReadParameters(FastBufferReader reader)
{
for (int i = 0; i < m_CachedAnimatorParameters.Length; i++)
{
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), i);
var hash = cacheValue.Hash;
ByteUnpacker.ReadValuePacked(reader, out uint totalParametersToRead);
var totalParametersRead = 0;
while (totalParametersRead < totalParametersToRead)
{
ByteUnpacker.ReadValuePacked(reader, out uint parameterIndex);
ref var cacheValue = ref UnsafeUtility.ArrayElementAsRef<AnimatorParamCache>(m_CachedAnimatorParameters.GetUnsafePtr(), (int)parameterIndex);
var hash = cacheValue.Hash;
if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterInt)
{
ByteUnpacker.ReadValuePacked(reader, out int newValue);
m_Animator.SetInteger(hash, newValue);
ByteUnpacker.ReadValuePacked(reader, out uint newValue);
m_Animator.SetInteger(hash, (int)newValue);
fixed (void* value = cacheValue.Value)
{
UnsafeUtility.WriteArrayElement(value, 0, newValue);
@@ -304,7 +708,7 @@ namespace Unity.Netcode.Components
}
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterBool)
{
reader.ReadValueSafe(out bool newBoolValue);
ByteUnpacker.ReadValuePacked(reader, out bool newBoolValue);
m_Animator.SetBool(hash, newBoolValue);
fixed (void* value = cacheValue.Value)
{
@@ -313,42 +717,164 @@ namespace Unity.Netcode.Components
}
else if (cacheValue.Type == AnimationParamEnumWrapper.AnimatorControllerParameterFloat)
{
reader.ReadValueSafe(out float newFloatValue);
ByteUnpacker.ReadValuePacked(reader, out float newFloatValue);
m_Animator.SetFloat(hash, newFloatValue);
fixed (void* value = cacheValue.Value)
{
UnsafeUtility.WriteArrayElement(value, 0, newFloatValue);
}
}
totalParametersRead++;
}
}
/// <summary>
/// Internally-called RPC client receiving function to update some animation parameters on a client when
/// the server wants to update them
/// Applies the ParametersUpdateMessage state to the Animator
/// </summary>
/// <param name="animSnapshot">the payload containing the parameters to apply</param>
/// <param name="clientRpcParams">unused</param>
[ClientRpc]
private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, ClientRpcParams clientRpcParams = default)
internal unsafe void UpdateParameters(ParametersUpdateMessage parametersUpdate)
{
if (animSnapshot.StateHash != 0)
{
m_Animator.Play(animSnapshot.StateHash, animSnapshot.Layer, animSnapshot.NormalizedTime);
}
m_Animator.SetLayerWeight(animSnapshot.Layer, animSnapshot.Weight);
if (animSnapshot.Parameters != null && animSnapshot.Parameters.Length != 0)
if (parametersUpdate.Parameters != null && parametersUpdate.Parameters.Length != 0)
{
// We use a fixed value here to avoid the copy of data from the byte buffer since we own the data
fixed (byte* parameters = animSnapshot.Parameters)
fixed (byte* parameters = parametersUpdate.Parameters)
{
var reader = new FastBufferReader(parameters, Allocator.None, animSnapshot.Parameters.Length);
var reader = new FastBufferReader(parameters, Allocator.None, parametersUpdate.Parameters.Length);
ReadParameters(reader);
}
}
}
/// <summary>
/// Applies the AnimationMessage state to the Animator
/// </summary>
private unsafe void UpdateAnimationState(AnimationMessage animationState)
{
if (animationState.StateHash != 0)
{
m_Animator.Play(animationState.StateHash, animationState.Layer, animationState.NormalizedTime);
}
m_Animator.SetLayerWeight(animationState.Layer, animationState.Weight);
}
/// <summary>
/// Server-side animator parameter update request
/// The server sets its local parameters and then forwards the message to the remaining clients
/// </summary>
[ServerRpc]
private unsafe void SendParametersUpdateServerRpc(ParametersUpdateMessage parametersUpdate, ServerRpcParams serverRpcParams = default)
{
if (IsServerAuthoritative())
{
m_NetworkAnimatorStateChangeHandler.SendParameterUpdate(parametersUpdate);
}
else
{
if (serverRpcParams.Receive.SenderClientId != OwnerClientId)
{
return;
}
UpdateParameters(parametersUpdate);
if (NetworkManager.ConnectedClientsIds.Count - 2 > 0)
{
m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(serverRpcParams.Receive.SenderClientId);
m_ClientSendList.Remove(NetworkManager.ServerClientId);
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
m_NetworkAnimatorStateChangeHandler.SendParameterUpdate(parametersUpdate, m_ClientRpcParams);
}
}
}
/// <summary>
/// Updates the client's animator's parameters
/// </summary>
[ClientRpc]
internal unsafe void SendParametersUpdateClientRpc(ParametersUpdateMessage parametersUpdate, ClientRpcParams clientRpcParams = default)
{
var isServerAuthoritative = IsServerAuthoritative();
if (!isServerAuthoritative && !IsOwner || isServerAuthoritative)
{
m_NetworkAnimatorStateChangeHandler.ProcessParameterUpdate(parametersUpdate);
}
}
/// <summary>
/// Server-side animation state update request
/// The server sets its local state and then forwards the message to the remaining clients
/// </summary>
[ServerRpc]
private unsafe void SendAnimStateServerRpc(AnimationMessage animSnapshot, ServerRpcParams serverRpcParams = default)
{
if (IsServerAuthoritative())
{
m_NetworkAnimatorStateChangeHandler.SendAnimationUpdate(animSnapshot);
}
else
{
if (serverRpcParams.Receive.SenderClientId != OwnerClientId)
{
return;
}
UpdateAnimationState(animSnapshot);
if (NetworkManager.ConnectedClientsIds.Count - 2 > 0)
{
m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(serverRpcParams.Receive.SenderClientId);
m_ClientSendList.Remove(NetworkManager.ServerClientId);
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
m_NetworkAnimatorStateChangeHandler.SendAnimationUpdate(animSnapshot, m_ClientRpcParams);
}
}
}
/// <summary>
/// Internally-called RPC client receiving function to update some animation state on a client
/// </summary>
[ClientRpc]
private unsafe void SendAnimStateClientRpc(AnimationMessage animSnapshot, ClientRpcParams clientRpcParams = default)
{
var isServerAuthoritative = IsServerAuthoritative();
if (!isServerAuthoritative && !IsOwner || isServerAuthoritative)
{
UpdateAnimationState(animSnapshot);
}
}
/// <summary>
/// Server-side trigger state update request
/// The server sets its local state and then forwards the message to the remaining clients
/// </summary>
[ServerRpc]
private void SendAnimTriggerServerRpc(AnimationTriggerMessage animationTriggerMessage, ServerRpcParams serverRpcParams = default)
{
if (IsServerAuthoritative())
{
m_NetworkAnimatorStateChangeHandler.SendTriggerUpdate(animationTriggerMessage);
}
else
{
if (serverRpcParams.Receive.SenderClientId != OwnerClientId)
{
return;
}
// trigger the animation locally on the server...
m_Animator.SetBool(animationTriggerMessage.Hash, animationTriggerMessage.IsTriggerSet);
if (NetworkManager.ConnectedClientsIds.Count - 2 > 0)
{
m_ClientSendList.Clear();
m_ClientSendList.AddRange(NetworkManager.ConnectedClientsIds);
m_ClientSendList.Remove(serverRpcParams.Receive.SenderClientId);
m_ClientSendList.Remove(NetworkManager.ServerClientId);
m_ClientRpcParams.Send.TargetClientIds = m_ClientSendList;
m_NetworkAnimatorStateChangeHandler.SendTriggerUpdate(animationTriggerMessage, m_ClientRpcParams);
}
}
}
/// <summary>
/// Internally-called RPC client receiving function to update a trigger when the server wants to forward
/// a trigger for a client to play / reset
@@ -356,24 +882,17 @@ namespace Unity.Netcode.Components
/// <param name="animSnapshot">the payload containing the trigger data to apply</param>
/// <param name="clientRpcParams">unused</param>
[ClientRpc]
private void SendAnimTriggerClientRpc(AnimationTriggerMessage animSnapshot, ClientRpcParams clientRpcParams = default)
internal void SendAnimTriggerClientRpc(AnimationTriggerMessage animationTriggerMessage, ClientRpcParams clientRpcParams = default)
{
if (animSnapshot.Reset)
var isServerAuthoritative = IsServerAuthoritative();
if (!isServerAuthoritative && !IsOwner || isServerAuthoritative)
{
m_Animator.ResetTrigger(animSnapshot.Hash);
}
else
{
m_Animator.SetTrigger(animSnapshot.Hash);
m_Animator.SetBool(animationTriggerMessage.Hash, animationTriggerMessage.IsTriggerSet);
}
}
/// <summary>
/// Sets the trigger for the associated animation
/// Note, triggers are special vs other kinds of parameters. For all the other parameters we watch for changes
/// in FixedUpdate and users can just set them normally off of Animator. But because triggers are transitory
/// and likely to come and go between FixedUpdate calls, we require users to set them here to guarantee us to
/// catch it...then we forward it to the Animator component
/// </summary>
/// <param name="triggerName">The string name of the trigger to activate</param>
public void SetTrigger(string triggerName)
@@ -383,31 +902,23 @@ namespace Unity.Netcode.Components
/// <inheritdoc cref="SetTrigger(string)" />
/// <param name="hash">The hash for the trigger to activate</param>
/// <param name="reset">If true, resets the trigger</param>
public void SetTrigger(int hash, bool reset = false)
/// <param name="setTrigger">sets (true) or resets (false) the trigger. The default is to set it (true).</param>
public void SetTrigger(int hash, bool setTrigger = true)
{
var animMsg = new AnimationTriggerMessage();
animMsg.Hash = hash;
animMsg.Reset = reset;
if (IsServer)
var isServerAuthoritative = IsServerAuthoritative();
if (IsOwner && !isServerAuthoritative || IsServer && isServerAuthoritative)
{
// trigger the animation locally on the server...
if (reset)
var animTriggerMessage = new AnimationTriggerMessage() { Hash = hash, IsTriggerSet = setTrigger };
if (IsServer)
{
m_Animator.ResetTrigger(hash);
SendAnimTriggerClientRpc(animTriggerMessage);
}
else
{
m_Animator.SetTrigger(hash);
SendAnimTriggerServerRpc(animTriggerMessage);
}
// ...then tell all the clients to do the same
SendAnimTriggerClientRpc(animMsg);
}
else
{
Debug.LogWarning("Trying to call NetworkAnimator.SetTrigger on a client...ignoring");
// trigger the animation locally on the server...
m_Animator.SetBool(hash, setTrigger);
}
}
@@ -424,7 +935,7 @@ namespace Unity.Netcode.Components
/// <param name="hash">The hash for the trigger to activate</param>
public void ResetTrigger(int hash)
{
SetTrigger(hash, true);
SetTrigger(hash, false);
}
}
}

View File

@@ -1,25 +1,50 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Unity.Netcode.Components
{
/// <summary>
/// A component for syncing transforms
/// A component for syncing transforms.
/// NetworkTransform will read the underlying transform and replicate it to clients.
/// The replicated value will be automatically be interpolated (if active) and applied to the underlying GameObject's transform
/// The replicated value will be automatically be interpolated (if active) and applied to the underlying GameObject's transform.
/// </summary>
[DisallowMultipleComponent]
[AddComponentMenu("Netcode/" + nameof(NetworkTransform))]
[DefaultExecutionOrder(100000)] // this is needed to catch the update time after the transform was updated by user scripts
public class NetworkTransform : NetworkBehaviour
{
/// <summary>
/// The default position change threshold value.
/// Any changes above this threshold will be replicated.
/// </summary>
public const float PositionThresholdDefault = 0.001f;
/// <summary>
/// The default rotation angle change threshold value.
/// Any changes above this threshold will be replicated.
/// </summary>
public const float RotAngleThresholdDefault = 0.01f;
/// <summary>
/// The default scale change threshold value.
/// Any changes above this threshold will be replicated.
/// </summary>
public const float ScaleThresholdDefault = 0.01f;
/// <summary>
/// The handler delegate type that takes client requested changes and returns resulting changes handled by the server.
/// </summary>
/// <param name="pos">The position requested by the client.</param>
/// <param name="rot">The rotation requested by the client.</param>
/// <param name="scale">The scale requested by the client.</param>
/// <returns>The resulting position, rotation and scale changes after handling.</returns>
public delegate (Vector3 pos, Quaternion rotOut, Vector3 scale) OnClientRequestChangeDelegate(Vector3 pos, Quaternion rot, Vector3 scale);
/// <summary>
/// The handler that gets invoked when server receives a change from a client.
/// This handler would be useful for server to modify pos/rot/scale before applying client's request.
/// </summary>
public OnClientRequestChangeDelegate OnClientRequestChange;
internal struct NetworkTransformState : INetworkSerializable
@@ -245,15 +270,62 @@ namespace Unity.Netcode.Components
}
}
public bool SyncPositionX = true, SyncPositionY = true, SyncPositionZ = true;
public bool SyncRotAngleX = true, SyncRotAngleY = true, SyncRotAngleZ = true;
public bool SyncScaleX = true, SyncScaleY = true, SyncScaleZ = true;
/// <summary>
/// Whether or not x component of position will be replicated
/// </summary>
public bool SyncPositionX = true;
/// <summary>
/// Whether or not y component of position will be replicated
/// </summary>
public bool SyncPositionY = true;
/// <summary>
/// Whether or not z component of position will be replicated
/// </summary>
public bool SyncPositionZ = true;
/// <summary>
/// Whether or not x component of rotation will be replicated
/// </summary>
public bool SyncRotAngleX = true;
/// <summary>
/// Whether or not y component of rotation will be replicated
/// </summary>
public bool SyncRotAngleY = true;
/// <summary>
/// Whether or not z component of rotation will be replicated
/// </summary>
public bool SyncRotAngleZ = true;
/// <summary>
/// Whether or not x component of scale will be replicated
/// </summary>
public bool SyncScaleX = true;
/// <summary>
/// Whether or not y component of scale will be replicated
/// </summary>
public bool SyncScaleY = true;
/// <summary>
/// Whether or not z component of scale will be replicated
/// </summary>
public bool SyncScaleZ = true;
/// <summary>
/// The current position threshold value
/// Any changes to the position that exceeds the current threshold value will be replicated
/// </summary>
public float PositionThreshold = PositionThresholdDefault;
/// <summary>
/// The current rotation threshold value
/// Any changes to the rotation that exceeds the current threshold value will be replicated
/// Minimum Value: 0.001
/// Maximum Value: 360.0
/// </summary>
[Range(0.001f, 360.0f)]
public float RotAngleThreshold = RotAngleThresholdDefault;
/// <summary>
/// The current scale threshold value
/// Any changes to the scale that exceeds the current threshold value will be replicated
/// </summary>
public float ScaleThreshold = ScaleThresholdDefault;
/// <summary>
@@ -266,6 +338,10 @@ namespace Unity.Netcode.Components
public bool InLocalSpace = false;
private bool m_LastInterpolateLocal = false; // was the last frame local
/// <summary>
/// When enabled (default) interpolation is applied and when disabled no interpolation is applied
/// Note: can be changed during runtime.
/// </summary>
public bool Interpolate = true;
private bool m_LastInterpolate = true; // was the last frame interpolated
@@ -277,7 +353,17 @@ namespace Unity.Netcode.Components
/// </summary>
// This is public to make sure that users don't depend on this IsClient && IsOwner check in their code. If this logic changes in the future, we can make it invisible here
public bool CanCommitToTransform { get; protected set; }
/// <summary>
/// Internally used by <see cref="NetworkTransform"/> to keep track of whether this <see cref="NetworkBehaviour"/> derived class instance
/// was instantiated on the server side or not.
/// </summary>
protected bool m_CachedIsServer;
/// <summary>
/// Internally used by <see cref="NetworkTransform"/> to keep track of the <see cref="NetworkManager"/> instance assigned to this
/// this <see cref="NetworkBehaviour"/> derived class instance.
/// </summary>
protected NetworkManager m_CachedNetworkManager;
private readonly NetworkVariable<NetworkTransformState> m_ReplicatedNetworkState = new NetworkVariable<NetworkTransformState>(new NetworkTransformState());
@@ -705,8 +791,6 @@ namespace Unity.Netcode.Components
return;
}
Debug.DrawLine(newState.Position, newState.Position + Vector3.up + Vector3.left, Color.green, 10, false);
if (Interpolate)
{
AddInterpolatedState(newState, (newState.InLocalSpace != m_LastInterpolateLocal));
@@ -716,10 +800,27 @@ namespace Unity.Netcode.Components
if (m_CachedNetworkManager.LogLevel == LogLevel.Developer)
{
var pos = new Vector3(newState.PositionX, newState.PositionY, newState.PositionZ);
Debug.DrawLine(pos, pos + Vector3.up + Vector3.left * Random.Range(0.5f, 2f), Color.green, k_DebugDrawLineTime, false);
}
}
/// <summary>
/// Will set the maximum interpolation boundary for the interpolators of this <see cref="NetworkTransform"/> instance.
/// This value roughly translates to the maximum value of 't' in <see cref="Mathf.Lerp(float, float, float)"/> and
/// <see cref="Mathf.LerpUnclamped(float, float, float)"/> for all transform elements being monitored by
/// <see cref="NetworkTransform"/> (i.e. Position, Rotation, and Scale)
/// </summary>
/// <param name="maxInterpolationBound">Maximum time boundary that can be used in a frame when interpolating between two values</param>
public void SetMaxInterpolationBound(float maxInterpolationBound)
{
m_PositionXInterpolator.MaxInterpolationBound = maxInterpolationBound;
m_PositionYInterpolator.MaxInterpolationBound = maxInterpolationBound;
m_PositionZInterpolator.MaxInterpolationBound = maxInterpolationBound;
m_RotationInterpolator.MaxInterpolationBound = maxInterpolationBound;
m_ScaleXInterpolator.MaxInterpolationBound = maxInterpolationBound;
m_ScaleYInterpolator.MaxInterpolationBound = maxInterpolationBound;
m_ScaleZInterpolator.MaxInterpolationBound = maxInterpolationBound;
}
private void Awake()
{
// we only want to create our interpolators during Awake so that, when pooled, we do not create tons
@@ -743,6 +844,8 @@ namespace Unity.Netcode.Components
}
}
/// <inheritdoc/>
public override void OnNetworkSpawn()
{
// must set up m_Transform in OnNetworkSpawn because it's possible an object spawns but is disabled
@@ -766,16 +869,19 @@ namespace Unity.Netcode.Components
Initialize();
}
/// <inheritdoc/>
public override void OnNetworkDespawn()
{
m_ReplicatedNetworkState.OnValueChanged -= OnNetworkStateChanged;
}
/// <inheritdoc/>
public override void OnGainedOwnership()
{
Initialize();
}
/// <inheritdoc/>
public override void OnLostOwnership()
{
Initialize();
@@ -843,8 +949,7 @@ namespace Unity.Netcode.Components
[ServerRpc]
private void SetStateServerRpc(Vector3 pos, Quaternion rot, Vector3 scale, bool shouldTeleport)
{
// server has received this RPC request to move change transform. Give the server a chance to modify or
// even reject the move
// server has received this RPC request to move change transform. give the server a chance to modify or even reject the move
if (OnClientRequestChange != null)
{
(pos, rot, scale) = OnClientRequestChange(pos, rot, scale);
@@ -855,8 +960,9 @@ namespace Unity.Netcode.Components
m_LocalAuthoritativeNetworkState.IsTeleportingNextFrame = shouldTeleport;
}
// todo this is currently in update, to be able to catch any transform changes. A FixedUpdate mode could be added to be less intense, but it'd be
// todo: this is currently in update, to be able to catch any transform changes. A FixedUpdate mode could be added to be less intense, but it'd be
// conditional to users only making transform update changes in FixedUpdate.
/// <inheritdoc/>
protected virtual void Update()
{
if (!IsSpawned)
@@ -914,6 +1020,10 @@ namespace Unity.Netcode.Components
/// <summary>
/// Teleports the transform to the given values without interpolating
/// </summary>
/// <param name="newPosition"></param> new position to move to.
/// <param name="newRotation"></param> new rotation to rotate to.
/// <param name="newScale">new scale to scale to.</param>
/// <exception cref="Exception"></exception>
public void Teleport(Vector3 newPosition, Quaternion newRotation, Vector3 newScale)
{
if (!CanCommitToTransform)
@@ -936,10 +1046,10 @@ namespace Unity.Netcode.Components
}
/// <summary>
/// Override this and return false to follow the owner authoritative
/// Otherwise, it defaults to server authoritative
/// Override this method and return false to switch to owner authoritative mode
/// </summary>
protected virtual bool OnIsServerAuthoritatitive()
/// <returns>(<see cref="true"/> or <see cref="false"/>) where when false it runs as owner-client authoritative</returns>
protected virtual bool OnIsServerAuthoritative()
{
return true;
}
@@ -949,7 +1059,7 @@ namespace Unity.Netcode.Components
/// </summary>
internal bool IsServerAuthoritative()
{
return OnIsServerAuthoritatitive();
return OnIsServerAuthoritative();
}
}
}

View File

@@ -6,6 +6,7 @@ using System.Text;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using Unity.Collections;
using Unity.CompilationPipeline.Common.Diagnostics;
using Unity.CompilationPipeline.Common.ILPostProcessing;
using UnityEngine;
@@ -28,6 +29,7 @@ namespace Unity.Netcode.Editor.CodeGen
public static readonly string ServerRpcReceiveParams_FullName = typeof(ServerRpcReceiveParams).FullName;
public static readonly string INetworkSerializable_FullName = typeof(INetworkSerializable).FullName;
public static readonly string INetworkSerializeByMemcpy_FullName = typeof(INetworkSerializeByMemcpy).FullName;
public static readonly string IUTF8Bytes_FullName = typeof(IUTF8Bytes).FullName;
public static readonly string UnityColor_FullName = typeof(Color).FullName;
public static readonly string UnityColor32_FullName = typeof(Color32).FullName;
public static readonly string UnityVector2_FullName = typeof(Vector2).FullName;

View File

@@ -2,14 +2,11 @@ using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Reflection;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Rocks;
using Unity.CompilationPipeline.Common.Diagnostics;
using Unity.CompilationPipeline.Common.ILPostProcessing;
using ILPPInterface = Unity.CompilationPipeline.Common.ILPostProcessing.ILPostProcessor;
using MethodAttributes = Mono.Cecil.MethodAttributes;
namespace Unity.Netcode.Editor.CodeGen
{
@@ -71,134 +68,26 @@ namespace Unity.Netcode.Editor.CodeGen
{
try
{
if (ImportReferences(mainModule))
var structTypes = mainModule.GetTypes()
.Where(t => t.Resolve().HasInterface(CodeGenHelpers.INetworkSerializeByMemcpy_FullName) && !t.Resolve().IsAbstract && !t.Resolve().HasGenericParameters && t.Resolve().IsValueType)
.ToList();
foreach (var type in structTypes)
{
// Initialize all the delegates for various NetworkVariable types to ensure they can be serailized
// Find all types we know we're going to want to serialize.
// The list of these types includes:
// - Non-generic INetworkSerializable types
// - Non-Generic INetworkSerializeByMemcpy types
// - Enums that are not declared within generic types
// We can't process generic types because, to initialize a generic, we need a value
// for `T` to initialize it with.
var networkSerializableTypes = mainModule.GetTypes()
.Where(t => t.Resolve().HasInterface(CodeGenHelpers.INetworkSerializable_FullName) && !t.Resolve().IsAbstract && !t.Resolve().HasGenericParameters && t.Resolve().IsValueType)
.ToList();
var structTypes = mainModule.GetTypes()
.Where(t => t.Resolve().HasInterface(CodeGenHelpers.INetworkSerializeByMemcpy_FullName) && !t.Resolve().IsAbstract && !t.Resolve().HasGenericParameters && t.Resolve().IsValueType)
.ToList();
var enumTypes = mainModule.GetTypes()
.Where(t => t.Resolve().IsEnum && !t.Resolve().IsAbstract && !t.Resolve().HasGenericParameters && t.Resolve().IsValueType)
.ToList();
// Now, to support generics, we have to do an extra pass.
// We look for any type that's a NetworkBehaviour type
// Then we look through all the fields in that type, finding any field whose type is
// descended from `NetworkVariableSerialization`. Then we check `NetworkVariableSerialization`'s
// `T` value, and if it's a generic, then we know it was missed in the above sweep and needs
// to be initialized. Now we have a full generic instance rather than a generic definition,
// so we can validly generate an initializer for that particular instance of the generic type.
var networkSerializableTypesSet = new HashSet<TypeReference>(networkSerializableTypes);
var structTypesSet = new HashSet<TypeReference>(structTypes);
var enumTypesSet = new HashSet<TypeReference>(enumTypes);
var typeStack = new List<TypeReference>();
foreach (var type in mainModule.GetTypes())
// We'll avoid some confusion by ensuring users only choose one of the two
// serialization schemes - by method OR by memcpy, not both. We'll also do a cursory
// check that INetworkSerializeByMemcpy types are unmanaged.
if (type.HasInterface(CodeGenHelpers.INetworkSerializeByMemcpy_FullName))
{
// Check if it's a NetworkBehaviour
if (type.IsSubclassOf(CodeGenHelpers.NetworkBehaviour_FullName))
if (type.HasInterface(CodeGenHelpers.INetworkSerializable_FullName))
{
// Iterate fields looking for NetworkVariableSerialization fields
foreach (var field in type.Fields)
{
// Get the field type and its base type
var fieldType = field.FieldType;
var baseType = fieldType.Resolve().BaseType;
if (baseType == null)
{
continue;
}
// This type stack is used for resolving NetworkVariableSerialization's T value
// When looking at base types, we get the type definition rather than the
// type reference... which means that we get the generic definition with an
// undefined T rather than the instance with the type filled in.
// We then have to walk backward back down the type stack to resolve what T
// is.
typeStack.Clear();
typeStack.Add(fieldType);
// Iterate through the base types until we get to Object.
// Object is the base for everything so we'll stop when we hit that.
while (baseType.Name != mainModule.TypeSystem.Object.Name)
{
// If we've found a NetworkVariableSerialization type...
if (baseType.IsGenericInstance && baseType.Resolve() == m_NetworkVariableSerializationType)
{
// Then we need to figure out what T is
var genericType = (GenericInstanceType)baseType;
var underlyingType = genericType.GenericArguments[0];
if (underlyingType.Resolve() == null)
{
underlyingType = ResolveGenericType(underlyingType, typeStack);
}
// If T is generic...
if (underlyingType.IsGenericInstance)
{
// Then we pick the correct set to add it to and set it up
// for initialization.
if (underlyingType.HasInterface(CodeGenHelpers.INetworkSerializable_FullName))
{
networkSerializableTypesSet.Add(underlyingType);
}
if (underlyingType.HasInterface(CodeGenHelpers.INetworkSerializeByMemcpy_FullName))
{
structTypesSet.Add(underlyingType);
}
if (underlyingType.Resolve().IsEnum)
{
enumTypesSet.Add(underlyingType);
}
}
break;
}
typeStack.Add(baseType);
baseType = baseType.Resolve().BaseType;
}
}
m_Diagnostics.AddError($"{nameof(INetworkSerializeByMemcpy)} types may not implement {nameof(INetworkSerializable)} - choose one or the other.");
}
// We'll also avoid some confusion by ensuring users only choose one of the two
// serialization schemes - by method OR by memcpy, not both. We'll also do a cursory
// check that INetworkSerializeByMemcpy types are unmanaged.
else if (type.HasInterface(CodeGenHelpers.INetworkSerializeByMemcpy_FullName))
if (!type.IsValueType)
{
if (type.HasInterface(CodeGenHelpers.INetworkSerializable_FullName))
{
m_Diagnostics.AddError($"{nameof(INetworkSerializeByMemcpy)} types may not implement {nameof(INetworkSerializable)} - choose one or the other.");
}
if (!type.IsValueType)
{
m_Diagnostics.AddError($"{nameof(INetworkSerializeByMemcpy)} types must be unmanaged types.");
}
m_Diagnostics.AddError($"{nameof(INetworkSerializeByMemcpy)} types must be unmanaged types.");
}
}
if (networkSerializableTypes.Count + structTypes.Count + enumTypes.Count == 0)
{
return null;
}
// Finally we add to the module initializer some code to initialize the delegates in
// NetworkVariableSerialization<T> for all necessary values of T, by calling initialization
// methods in NetworkVariableHelpers.
CreateModuleInitializer(assemblyDefinition, networkSerializableTypesSet.ToList(), structTypesSet.ToList(), enumTypesSet.ToList());
}
else
{
m_Diagnostics.AddError($"Cannot import references into main module: {mainModule.Name}");
}
}
catch (Exception e)
@@ -228,102 +117,5 @@ namespace Unity.Netcode.Editor.CodeGen
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), m_Diagnostics);
}
private MethodReference m_InitializeDelegatesNetworkSerializable_MethodRef;
private MethodReference m_InitializeDelegatesStruct_MethodRef;
private MethodReference m_InitializeDelegatesEnum_MethodRef;
private TypeDefinition m_NetworkVariableSerializationType;
private const string k_InitializeNetworkSerializableMethodName = nameof(NetworkVariableHelper.InitializeDelegatesNetworkSerializable);
private const string k_InitializeStructMethodName = nameof(NetworkVariableHelper.InitializeDelegatesStruct);
private const string k_InitializeEnumMethodName = nameof(NetworkVariableHelper.InitializeDelegatesEnum);
private bool ImportReferences(ModuleDefinition moduleDefinition)
{
var helperType = typeof(NetworkVariableHelper);
foreach (var methodInfo in helperType.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
{
switch (methodInfo.Name)
{
case k_InitializeNetworkSerializableMethodName:
m_InitializeDelegatesNetworkSerializable_MethodRef = moduleDefinition.ImportReference(methodInfo);
break;
case k_InitializeStructMethodName:
m_InitializeDelegatesStruct_MethodRef = moduleDefinition.ImportReference(methodInfo);
break;
case k_InitializeEnumMethodName:
m_InitializeDelegatesEnum_MethodRef = moduleDefinition.ImportReference(methodInfo);
break;
}
}
m_NetworkVariableSerializationType = moduleDefinition.ImportReference(typeof(NetworkVariableSerialization<>)).Resolve();
return true;
}
private MethodDefinition GetOrCreateStaticConstructor(TypeDefinition typeDefinition)
{
var staticCtorMethodDef = typeDefinition.GetStaticConstructor();
if (staticCtorMethodDef == null)
{
staticCtorMethodDef = new MethodDefinition(
".cctor", // Static Constructor (constant-constructor)
MethodAttributes.HideBySig |
MethodAttributes.SpecialName |
MethodAttributes.RTSpecialName |
MethodAttributes.Static,
typeDefinition.Module.TypeSystem.Void);
staticCtorMethodDef.Body.Instructions.Add(Instruction.Create(OpCodes.Ret));
typeDefinition.Methods.Add(staticCtorMethodDef);
}
return staticCtorMethodDef;
}
// Creates a static module constructor (which is executed when the module is loaded) that registers all the
// message types in the assembly with MessagingSystem.
// This is the same behavior as annotating a static method with [ModuleInitializer] in standardized
// C# (that attribute doesn't exist in Unity, but the static module constructor still works)
// https://docs.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.moduleinitializerattribute?view=net-5.0
// https://web.archive.org/web/20100212140402/http://blogs.msdn.com/junfeng/archive/2005/11/19/494914.aspx
private void CreateModuleInitializer(AssemblyDefinition assembly, List<TypeReference> networkSerializableTypes, List<TypeReference> structTypes, List<TypeReference> enumTypes)
{
foreach (var typeDefinition in assembly.MainModule.Types)
{
if (typeDefinition.FullName == "<Module>")
{
var staticCtorMethodDef = GetOrCreateStaticConstructor(typeDefinition);
var processor = staticCtorMethodDef.Body.GetILProcessor();
var instructions = new List<Instruction>();
foreach (var type in structTypes)
{
var method = new GenericInstanceMethod(m_InitializeDelegatesStruct_MethodRef);
method.GenericArguments.Add(type);
instructions.Add(processor.Create(OpCodes.Call, method));
}
foreach (var type in networkSerializableTypes)
{
var method = new GenericInstanceMethod(m_InitializeDelegatesNetworkSerializable_MethodRef);
method.GenericArguments.Add(type);
instructions.Add(processor.Create(OpCodes.Call, method));
}
foreach (var type in enumTypes)
{
var method = new GenericInstanceMethod(m_InitializeDelegatesEnum_MethodRef);
method.GenericArguments.Add(type);
instructions.Add(processor.Create(OpCodes.Call, method));
}
instructions.ForEach(instruction => processor.Body.Instructions.Insert(processor.Body.Instructions.Count - 1, instruction));
break;
}
}
}
}
}

View File

@@ -609,11 +609,21 @@ namespace Unity.Netcode.Editor.CodeGen
{
#if CECIL_CONSTRAINTS_ARE_TYPE_REFERENCES
var resolvedConstraint = constraint.Resolve();
var constraintTypeRef = constraint;
#else
var resolvedConstraint = constraint.ConstraintType.Resolve();
var constraintTypeRef = constraint.ConstraintType;
#endif
var resolvedConstraintName = resolvedConstraint.FullNameWithGenericParameters(new[] { method.GenericParameters[0] }, new[] { checkType });
if (constraintTypeRef.IsGenericInstance)
{
var genericConstraint = (GenericInstanceType)constraintTypeRef;
if (genericConstraint.HasGenericArguments && genericConstraint.GenericArguments[0].Resolve() != null)
{
resolvedConstraintName = constraintTypeRef.FullName;
}
}
if ((resolvedConstraint.IsInterface && !checkType.HasInterface(resolvedConstraintName)) ||
(resolvedConstraint.IsClass && !checkType.Resolve().IsSubclassOf(resolvedConstraintName)) ||
(resolvedConstraint.Name == "ValueType" && !checkType.IsValueType))
@@ -743,12 +753,22 @@ namespace Unity.Netcode.Editor.CodeGen
{
#if CECIL_CONSTRAINTS_ARE_TYPE_REFERENCES
var resolvedConstraint = constraint.Resolve();
var constraintTypeRef = constraint;
#else
var resolvedConstraint = constraint.ConstraintType.Resolve();
var constraintTypeRef = constraint.ConstraintType;
#endif
var resolvedConstraintName = resolvedConstraint.FullNameWithGenericParameters(new[] { method.GenericParameters[0] }, new[] { checkType });
if (constraintTypeRef.IsGenericInstance)
{
var genericConstraint = (GenericInstanceType)constraintTypeRef;
if (genericConstraint.HasGenericArguments && genericConstraint.GenericArguments[0].Resolve() != null)
{
resolvedConstraintName = constraintTypeRef.FullName;
}
}
if ((resolvedConstraint.IsInterface && !checkType.HasInterface(resolvedConstraintName)) ||
(resolvedConstraint.IsClass && !checkType.Resolve().IsSubclassOf(resolvedConstraintName)) ||
@@ -1142,7 +1162,7 @@ namespace Unity.Netcode.Editor.CodeGen
}
else
{
m_Diagnostics.AddError(methodDefinition, $"Don't know how to serialize {paramType.Name} - implement {nameof(INetworkSerializable)}, tag memcpyable struct with {nameof(INetworkSerializeByMemcpy)}, or add an extension method for {nameof(FastBufferWriter)}.{k_WriteValueMethodName} to define serialization.");
m_Diagnostics.AddError(methodDefinition, $"{methodDefinition.Name} - Don't know how to serialize {paramType.Name}. RPC parameter types must either implement {nameof(INetworkSerializeByMemcpy)} or {nameof(INetworkSerializable)}. If this type is external and you are sure its memory layout makes it serializable by memcpy, you can replace {paramType} with {typeof(ForceNetworkSerializeByMemcpy<>).Name}<{paramType}>, or you can create extension methods for {nameof(FastBufferReader)}.{nameof(FastBufferReader.ReadValueSafe)}(this {nameof(FastBufferReader)}, out {paramType}) and {nameof(FastBufferWriter)}.{nameof(FastBufferWriter.WriteValueSafe)}(this {nameof(FastBufferWriter)}, in {paramType}) to define serialization for this type.");
continue;
}
@@ -1456,7 +1476,7 @@ namespace Unity.Netcode.Editor.CodeGen
}
else
{
m_Diagnostics.AddError(methodDefinition, $"Don't know how to serialize {paramType.Name} - implement {nameof(INetworkSerializable)}, tag memcpyable struct with {nameof(INetworkSerializeByMemcpy)}, or add an extension method for {nameof(FastBufferWriter)}.{k_WriteValueMethodName} to define serialization.");
m_Diagnostics.AddError(methodDefinition, $"{methodDefinition.Name} - Don't know how to serialize {paramType.Name}. RPC parameter types must either implement {nameof(INetworkSerializeByMemcpy)} or {nameof(INetworkSerializable)}. If this type is external and you are sure its memory layout makes it serializable by memcpy, you can replace {paramType} with {typeof(ForceNetworkSerializeByMemcpy<>).Name}<{paramType}>, or you can create extension methods for {nameof(FastBufferReader)}.{nameof(FastBufferReader.ReadValueSafe)}(this {nameof(FastBufferReader)}, out {paramType}) and {nameof(FastBufferWriter)}.{nameof(FastBufferWriter.WriteValueSafe)}(this {nameof(FastBufferWriter)}, in {paramType}) to define serialization for this type.");
continue;
}

View File

@@ -52,9 +52,6 @@ namespace Unity.Netcode.Editor.CodeGen
case nameof(NetworkBehaviour):
ProcessNetworkBehaviour(typeDefinition);
break;
case nameof(NetworkVariableHelper):
ProcessNetworkVariableHelper(typeDefinition);
break;
case nameof(__RpcParams):
typeDefinition.IsPublic = true;
break;
@@ -103,25 +100,6 @@ namespace Unity.Netcode.Editor.CodeGen
}
}
private void ProcessNetworkVariableHelper(TypeDefinition typeDefinition)
{
foreach (var methodDefinition in typeDefinition.Methods)
{
if (methodDefinition.Name == nameof(NetworkVariableHelper.InitializeDelegatesEnum))
{
methodDefinition.IsPublic = true;
}
if (methodDefinition.Name == nameof(NetworkVariableHelper.InitializeDelegatesStruct))
{
methodDefinition.IsPublic = true;
}
if (methodDefinition.Name == nameof(NetworkVariableHelper.InitializeDelegatesNetworkSerializable))
{
methodDefinition.IsPublic = true;
}
}
}
private void ProcessNetworkBehaviour(TypeDefinition typeDefinition)
{
foreach (var nestedType in typeDefinition.NestedTypes)

View File

@@ -2,7 +2,8 @@
"name": "Unity.Netcode.Editor.CodeGen",
"rootNamespace": "Unity.Netcode.Editor.CodeGen",
"references": [
"Unity.Netcode.Runtime"
"Unity.Netcode.Runtime",
"Unity.Collections"
],
"includePlatforms": [
"Editor"
@@ -26,4 +27,4 @@
}
],
"noEngineReferences": false
}
}

View File

@@ -151,6 +151,8 @@ namespace Unity.Netcode.Editor
}
}
/// <inheritdoc/>
public override void OnInspectorGUI()
{
if (!m_Initialized)
@@ -218,6 +220,11 @@ namespace Unity.Netcode.Editor
/// </summary>
private void OnEnable()
{
// This can be null and throw an exception when running test runner in the editor
if (target == null)
{
return;
}
// When we first add a NetworkBehaviour this editor will be enabled
// so we go ahead and check for an already existing NetworkObject here
CheckForNetworkObject((target as NetworkBehaviour).gameObject);
@@ -225,6 +232,11 @@ namespace Unity.Netcode.Editor
internal const string AutoAddNetworkObjectIfNoneExists = "AutoAdd-NetworkObject-When-None-Exist";
/// <summary>
/// Recursively finds the root parent of a <see cref="Transform"/>
/// </summary>
/// <param name="transform">The current <see cref="Transform"/> we are inspecting for a parent</param>
/// <returns>the root parent for the first <see cref="Transform"/> passed into the method</returns>
public static Transform GetRootParentTransform(Transform transform)
{
if (transform.parent == null || transform.parent == transform)
@@ -239,6 +251,8 @@ namespace Unity.Netcode.Editor
/// does not already have a NetworkObject component. If not it will notify
/// the user that NetworkBehaviours require a NetworkObject.
/// </summary>
/// <param name="gameObject"><see cref="GameObject"/> to start checking for a <see cref="NetworkObject"/></param>
/// <param name="networkObjectRemoved">used internally</param>
public static void CheckForNetworkObject(GameObject gameObject, bool networkObjectRemoved = false)
{
// If there are no NetworkBehaviours or no gameObject, then exit early
@@ -249,6 +263,39 @@ namespace Unity.Netcode.Editor
// Now get the root parent transform to the current GameObject (or itself)
var rootTransform = GetRootParentTransform(gameObject.transform);
var networkManager = rootTransform.GetComponent<NetworkManager>();
if (networkManager == null)
{
networkManager = rootTransform.GetComponentInChildren<NetworkManager>();
}
// If there is a NetworkManager, then notify the user that a NetworkManager cannot have NetworkBehaviour components
if (networkManager != null)
{
var networkBehaviours = networkManager.gameObject.GetComponents<NetworkBehaviour>();
var networkBehavioursChildren = networkManager.gameObject.GetComponentsInChildren<NetworkBehaviour>();
if (networkBehaviours.Length > 0 || networkBehavioursChildren.Length > 0)
{
if (EditorUtility.DisplayDialog("NetworkBehaviour or NetworkManager Cannot Be Added", $"{nameof(NetworkManager)}s cannot have {nameof(NetworkBehaviour)} components added to the root parent or any of its children." +
$" Would you like to remove the NetworkManager or NetworkBehaviour?", "NetworkManager", "NetworkBehaviour"))
{
DestroyImmediate(networkManager);
}
else
{
foreach (var networkBehaviour in networkBehaviours)
{
DestroyImmediate(networkBehaviour);
}
foreach (var networkBehaviour in networkBehaviours)
{
DestroyImmediate(networkBehaviour);
}
}
return;
}
}
// Otherwise, check to see if there is any NetworkObject from the root GameObject down to all children.
// If not, notify the user that NetworkBehaviours require that the relative GameObject has a NetworkObject component.

View File

@@ -6,6 +6,10 @@ using UnityEditorInternal;
namespace Unity.Netcode.Editor
{
/// <summary>
/// This <see cref="CustomEditor"/> handles the translation between the <see cref="NetworkConfig"/> and
/// the <see cref="NetworkManager"/> properties.
/// </summary>
[CustomEditor(typeof(NetworkManager), true)]
[CanEditMultipleObjects]
public class NetworkManagerEditor : UnityEditor.Editor
@@ -200,6 +204,7 @@ namespace Unity.Netcode.Editor
m_NetworkPrefabsList.drawHeaderCallback = rect => EditorGUI.LabelField(rect, "NetworkPrefabs");
}
/// <inheritdoc/>
public override void OnInspectorGUI()
{
Initialize();

View File

@@ -4,6 +4,9 @@ using UnityEditor;
namespace Unity.Netcode.Editor
{
/// <summary>
/// The <see cref="CustomEditor"/> for <see cref="NetworkObject"/>
/// </summary>
[CustomEditor(typeof(NetworkObject), true)]
[CanEditMultipleObjects]
public class NetworkObjectEditor : UnityEditor.Editor
@@ -23,6 +26,7 @@ namespace Unity.Netcode.Editor
m_NetworkObject = (NetworkObject)target;
}
/// <inheritdoc/>
public override void OnInspectorGUI()
{
Initialize();

View File

@@ -4,6 +4,9 @@ using Unity.Netcode.Components;
namespace Unity.Netcode.Editor
{
/// <summary>
/// The <see cref="CustomEditor"/> for <see cref="NetworkTransform"/>
/// </summary>
[CustomEditor(typeof(NetworkTransform), true)]
public class NetworkTransformEditor : UnityEditor.Editor
{
@@ -28,6 +31,7 @@ namespace Unity.Netcode.Editor
private static GUIContent s_RotationLabel = EditorGUIUtility.TrTextContent("Rotation");
private static GUIContent s_ScaleLabel = EditorGUIUtility.TrTextContent("Scale");
/// <inheritdoc/>
public void OnEnable()
{
m_SyncPositionXProperty = serializedObject.FindProperty(nameof(NetworkTransform.SyncPositionX));
@@ -46,6 +50,7 @@ namespace Unity.Netcode.Editor
m_InterpolateProperty = serializedObject.FindProperty(nameof(NetworkTransform.Interpolate));
}
/// <inheritdoc/>
public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("Syncing", EditorStyles.boldLabel);

View File

@@ -150,8 +150,16 @@ namespace Unity.Netcode
/// </summary>
public bool EnableNetworkLogs = true;
/// <summary>
/// The number of RTT samples that is kept as an average for calculations
/// </summary>
public const int RttAverageSamples = 5; // number of RTT to keep an average of (plus one)
/// <summary>
/// The number of slots used for RTT calculations. This is the maximum amount of in-flight messages
/// </summary>
public const int RttWindowSize = 64; // number of slots to use for RTT computations (max number of in-flight packets)
/// <summary>
/// Returns a base64 encoded version of the configuration
/// </summary>

View File

@@ -470,6 +470,7 @@ namespace Unity.Netcode
/// <summary>
/// Gets called when the parent NetworkObject of this NetworkBehaviour's NetworkObject has changed
/// </summary>
/// <param name="parentNetworkObject">the new <see cref="NetworkObject"/> parent</param>
public virtual void OnNetworkObjectParentChanged(NetworkObject parentNetworkObject) { }
private bool m_VarInit = false;
@@ -751,6 +752,11 @@ namespace Unity.Netcode
return NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject) ? networkObject : null;
}
/// <summary>
/// Invoked when the <see cref="GameObject"/> the <see cref="NetworkBehaviour"/> is attached to.
/// NOTE: If you override this, you will want to always invoke this base class version of this
/// <see cref="OnDestroy"/> method!!
/// </summary>
public virtual void OnDestroy()
{
// this seems odd to do here, but in fact especially in tests we can find ourselves

View File

@@ -3,6 +3,9 @@ using Unity.Profiling;
namespace Unity.Netcode
{
/// <summary>
/// An helper class that helps NetworkManager update NetworkBehaviours and replicate them down to connected clients.
/// </summary>
public class NetworkBehaviourUpdater
{
private HashSet<NetworkObject> m_Touched = new HashSet<NetworkObject>();

View File

@@ -60,6 +60,11 @@ namespace Unity.Netcode
private NetworkPrefabHandler m_PrefabHandler;
internal Dictionary<ulong, ConnectionApprovalResponse> ClientsToApprove = new Dictionary<ulong, ConnectionApprovalResponse>();
/// <summary>
/// The <see cref="NetworkPrefabHandler"/> instance created after starting the <see cref="NetworkManager"/>
/// </summary>
public NetworkPrefabHandler PrefabHandler
{
get
@@ -195,23 +200,38 @@ namespace Unity.Netcode
return gameObject;
}
/// <summary>
/// Accessor for the <see cref="NetworkTimeSystem"/> of the NetworkManager.
/// Prefer the use of the LocalTime and ServerTime properties
/// </summary>
public NetworkTimeSystem NetworkTimeSystem { get; private set; }
/// <summary>
/// Accessor for the <see cref="NetworkTickSystem"/> of the NetworkManager.
/// </summary>
public NetworkTickSystem NetworkTickSystem { get; private set; }
/// <summary>
/// The local <see cref="NetworkTime"/>
/// </summary>
public NetworkTime LocalTime => NetworkTickSystem?.LocalTime ?? default;
/// <summary>
/// The <see cref="NetworkTime"/> on the server
/// </summary>
public NetworkTime ServerTime => NetworkTickSystem?.ServerTime ?? default;
/// <summary>
/// Gets or sets if the application should be set to run in background
/// </summary>
[HideInInspector] public bool RunInBackground = true;
[HideInInspector]
public bool RunInBackground = true;
/// <summary>
/// The log level to use
/// </summary>
[HideInInspector] public LogLevel LogLevel = LogLevel.Normal;
[HideInInspector]
public LogLevel LogLevel = LogLevel.Normal;
/// <summary>
/// The singleton instance of the NetworkManager
@@ -225,10 +245,19 @@ namespace Unity.Netcode
internal IDeferredMessageManager DeferredMessageManager { get; private set; }
/// <summary>
/// Gets the CustomMessagingManager for this NetworkManager
/// </summary>
public CustomMessagingManager CustomMessagingManager { get; private set; }
/// <summary>
/// The <see cref="NetworkSceneManager"/> instance created after starting the <see cref="NetworkManager"/>
/// </summary>
public NetworkSceneManager SceneManager { get; private set; }
/// <summary>
/// The client id used to represent the server
/// </summary>
public const ulong ServerClientId = 0;
/// <summary>
@@ -337,7 +366,9 @@ namespace Unity.Netcode
/// </summary>
public bool IsConnectedClient { get; internal set; }
/// <summary>
/// Can be used to determine if the <see cref="NetworkManager"/> is currently shutting itself down
/// </summary>
public bool ShutdownInProgress { get { return m_ShuttingDown; } }
/// <summary>
@@ -358,26 +389,87 @@ namespace Unity.Netcode
public event Action OnServerStarted = null;
/// <summary>
/// Delegate type called when connection has been approved. This only has to be set on the server.
/// The callback to invoke if the <see cref="NetworkTransport"/> fails.
/// </summary>
/// <param name="createPlayerObject">If true, a player object will be created. Otherwise the client will have no object.</param>
/// <param name="playerPrefabHash">The prefabHash to use for the client. If createPlayerObject is false, this is ignored. If playerPrefabHash is null, the default player prefab is used.</param>
/// <param name="approved">Whether or not the client was approved</param>
/// <param name="position">The position to spawn the client at. If null, the prefab position is used.</param>
/// <param name="rotation">The rotation to spawn the client with. If null, the prefab position is used.</param>
public delegate void ConnectionApprovedDelegate(bool createPlayerObject, uint? playerPrefabHash, bool approved, Vector3? position, Quaternion? rotation);
/// <remarks>
/// A failure of the transport is always followed by the <see cref="NetworkManager"/> shutting down. Recovering
/// from a transport failure would normally entail reconfiguring the transport (e.g. re-authenticating, or
/// recreating a new service allocation depending on the transport) and restarting the client/server/host.
/// </remarks>
public event Action OnTransportFailure = null;
/// <summary>
/// The callback to invoke during connection approval
/// Connection Approval Response
/// </summary>
public event Action<byte[], ulong, ConnectionApprovedDelegate> ConnectionApprovalCallback = null;
public class ConnectionApprovalResponse
{
/// <summary>
/// Whether or not the client was approved
/// </summary>
public bool Approved;
/// <summary>
/// If true, a player object will be created. Otherwise the client will have no object.
/// </summary>
public bool CreatePlayerObject;
/// <summary>
/// The prefabHash to use for the client. If createPlayerObject is false, this is ignored. If playerPrefabHash is null, the default player prefab is used.
/// </summary>
public uint? PlayerPrefabHash;
/// <summary>
/// The position to spawn the client at. If null, the prefab position is used.
/// </summary>
public Vector3? Position;
/// <summary>
/// The rotation to spawn the client with. If null, the prefab position is used.
/// </summary>
public Quaternion? Rotation;
/// <summary>
/// If the Approval decision cannot be made immediately, the client code can set Pending to true, keep a reference to the ConnectionApprovalResponse object and write to it later. Client code must exercise care to setting all the members to the value it wants before marking Pending to false, to indicate completion. If the field is set as Pending = true, we'll monitor the object until it gets set to not pending anymore and use the parameters then.
/// </summary>
public bool Pending;
}
internal void InvokeConnectionApproval(byte[] payload, ulong clientId, ConnectionApprovedDelegate action) => ConnectionApprovalCallback?.Invoke(payload, clientId, action);
/// <summary>
/// Connection Approval Request
/// </summary>
public struct ConnectionApprovalRequest
{
/// <summary>
/// The connection data payload
/// </summary>
public byte[] Payload;
/// <summary>
/// The Network Id of the client we are about to handle
/// </summary>
public ulong ClientNetworkId;
}
/// <summary>
/// The callback to invoke during connection approval. Allows client code to decide whether or not to allow incoming client connection
/// </summary>
public Action<ConnectionApprovalRequest, ConnectionApprovalResponse> ConnectionApprovalCallback
{
get => m_ConnectionApprovalCallback;
set
{
if (value != null && value.GetInvocationList().Length > 1)
{
throw new InvalidOperationException($"Only one {nameof(ConnectionApprovalCallback)} can be registered at a time.");
}
else
{
m_ConnectionApprovalCallback = value;
}
}
}
private Action<ConnectionApprovalRequest, ConnectionApprovalResponse> m_ConnectionApprovalCallback;
/// <summary>
/// The current NetworkConfig
/// </summary>
[HideInInspector] public NetworkConfig NetworkConfig;
[HideInInspector]
public NetworkConfig NetworkConfig;
/// <summary>
/// The current host name we are connected to, used to validate certificate
@@ -389,7 +481,7 @@ namespace Unity.Netcode
internal static event Action OnSingletonReady;
#if UNITY_EDITOR
private void OnValidate()
internal void OnValidate()
{
if (NetworkConfig == null)
{
@@ -541,6 +633,47 @@ namespace Unity.Netcode
}
}
/// <summary>
/// Remove a prefab from the prefab list.
/// As with AddNetworkPrefab, this is specific to the client it's called on -
/// calling it on the server does not automatically remove anything on any of the
/// client processes.
///
/// Like AddNetworkPrefab, when NetworkConfig.ForceSamePrefabs is enabled,
/// this cannot be called after connecting.
/// </summary>
/// <param name="prefab"></param>
public void RemoveNetworkPrefab(GameObject prefab)
{
if (IsListening && NetworkConfig.ForceSamePrefabs)
{
throw new Exception($"Prefabs cannot be removed after starting {nameof(NetworkManager)} when {nameof(NetworkConfig.ForceSamePrefabs)} is enabled.");
}
var globalObjectIdHash = prefab.GetComponent<NetworkObject>().GlobalObjectIdHash;
for (var i = 0; i < NetworkConfig.NetworkPrefabs.Count; ++i)
{
if (NetworkConfig.NetworkPrefabs[i].Prefab.GetComponent<NetworkObject>().GlobalObjectIdHash == globalObjectIdHash)
{
NetworkConfig.NetworkPrefabs.RemoveAt(i);
break;
}
}
if (PrefabHandler.ContainsHandler(globalObjectIdHash))
{
PrefabHandler.RemoveHandler(globalObjectIdHash);
}
if (NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(globalObjectIdHash, out var targetPrefab))
{
NetworkConfig.NetworkPrefabOverrideLinks.Remove(globalObjectIdHash);
var targetHash = targetPrefab.Prefab.GetComponent<NetworkObject>().GlobalObjectIdHash;
if (NetworkConfig.OverrideToNetworkPrefab.ContainsKey(targetHash))
{
NetworkConfig.OverrideToNetworkPrefab.Remove(targetHash);
}
}
}
private bool ShouldAddPrefab(NetworkPrefab networkPrefab, out uint sourcePrefabGlobalObjectIdHash, out uint targetPrefabGlobalObjectIdHash, int index = -1)
{
sourcePrefabGlobalObjectIdHash = 0;
@@ -865,11 +998,13 @@ namespace Unity.Netcode
m_ConnectedClientIds.Clear();
LocalClient = null;
NetworkObject.OrphanChildren.Clear();
ClientsToApprove.Clear();
}
/// <summary>
/// Starts a server
/// </summary>
/// <returns>(<see cref="true"/>/<see cref="false"/>) returns true if <see cref="NetworkManager"/> started in server mode successfully.</returns>
public bool StartServer()
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
@@ -899,6 +1034,7 @@ namespace Unity.Netcode
else
{
Debug.LogError($"Server is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
OnTransportFailure?.Invoke();
Shutdown();
}
@@ -908,6 +1044,7 @@ namespace Unity.Netcode
/// <summary>
/// Starts a client
/// </summary>
/// <returns>(<see cref="true"/>/<see cref="false"/>) returns true if <see cref="NetworkManager"/> started in client mode successfully.</returns>
public bool StartClient()
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
@@ -926,6 +1063,7 @@ namespace Unity.Netcode
if (!NetworkConfig.NetworkTransport.StartClient())
{
Debug.LogError($"Client is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
OnTransportFailure?.Invoke();
Shutdown();
return false;
}
@@ -940,6 +1078,7 @@ namespace Unity.Netcode
/// <summary>
/// Starts a Host
/// </summary>
/// <returns>(<see cref="true"/>/<see cref="false"/>) returns true if <see cref="NetworkManager"/> started in host mode successfully.</returns>
public bool StartHost()
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Developer)
@@ -958,6 +1097,7 @@ namespace Unity.Netcode
if (!NetworkConfig.NetworkTransport.StartServer())
{
Debug.LogError($"Server is shutting down due to network transport start failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
OnTransportFailure?.Invoke();
Shutdown();
return false;
}
@@ -970,27 +1110,29 @@ namespace Unity.Netcode
IsClient = true;
IsListening = true;
if (NetworkConfig.ConnectionApproval)
if (NetworkConfig.ConnectionApproval && ConnectionApprovalCallback != null)
{
InvokeConnectionApproval(NetworkConfig.ConnectionData, ServerClientId,
(createPlayerObject, playerPrefabHash, approved, position, rotation) =>
var response = new ConnectionApprovalResponse();
ConnectionApprovalCallback(new ConnectionApprovalRequest { Payload = NetworkConfig.ConnectionData, ClientNetworkId = ServerClientId }, response);
if (!response.Approved)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
// You cannot decline the local server. Force approved to true
if (!approved)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning(
"You cannot decline the host connection. The connection was automatically approved.");
}
}
NetworkLog.LogWarning("You cannot decline the host connection. The connection was automatically approved.");
}
}
HandleApproval(ServerClientId, createPlayerObject, playerPrefabHash, true, position, rotation);
});
response.Approved = true;
HandleConnectionApproval(ServerClientId, response);
}
else
{
HandleApproval(ServerClientId, NetworkConfig.PlayerPrefab != null, null, true, null, null);
var response = new ConnectionApprovalResponse
{
Approved = true,
CreatePlayerObject = NetworkConfig.PlayerPrefab != null
};
HandleConnectionApproval(ServerClientId, response);
}
SpawnManager.ServerSpawnSceneObjectsOnStartSweep();
@@ -1048,6 +1190,9 @@ namespace Unity.Netcode
return true;
}
/// <summary>
/// Set this NetworkManager instance as the static NetworkManager singleton
/// </summary>
public void SetSingleton()
{
Singleton = this;
@@ -1076,7 +1221,6 @@ namespace Unity.Netcode
private void Awake()
{
UnityEngine.SceneManagement.SceneManager.sceneUnloaded += OnSceneUnloaded;
NetworkVariableHelper.InitializeAllBaseDelegates();
}
/// <summary>
@@ -1310,7 +1454,7 @@ namespace Unity.Netcode
ClearClients();
}
// INetworkUpdateSystem
/// <inheritdoc />
public void NetworkUpdate(NetworkUpdateStage updateStage)
{
switch (updateStage)
@@ -1327,6 +1471,43 @@ namespace Unity.Netcode
}
}
private void ProcessPendingApprovals()
{
List<ulong> senders = null;
foreach (var responsePair in ClientsToApprove)
{
var response = responsePair.Value;
var senderId = responsePair.Key;
if (!response.Pending)
{
try
{
HandleConnectionApproval(senderId, response);
if (senders == null)
{
senders = new List<ulong>();
}
senders.Add(senderId);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
}
if (senders != null)
{
foreach (var sender in senders)
{
ClientsToApprove.Remove(sender);
}
}
}
private void OnNetworkEarlyUpdate()
{
if (!IsListening)
@@ -1334,6 +1515,8 @@ namespace Unity.Netcode
return;
}
ProcessPendingApprovals();
#if DEVELOPMENT_BUILD || UNITY_EDITOR
s_TransportPoll.Begin();
#endif
@@ -1554,6 +1737,12 @@ namespace Unity.Netcode
s_TransportDisconnect.End();
#endif
break;
case NetworkEvent.TransportFailure:
Debug.LogError($"Shutting down due to network transport failure of {NetworkConfig.NetworkTransport.GetType().Name}!");
OnTransportFailure?.Invoke();
Shutdown(true);
break;
}
}
@@ -1818,15 +2007,11 @@ namespace Unity.Netcode
/// <summary>
/// Server Side: Handles the approval of a client
/// </summary>
/// <param name="ownerClientId">client being approved</param>
/// <param name="createPlayerObject">whether we want to create a player or not</param>
/// <param name="playerPrefabHash">the GlobalObjectIdHash value for the Network Prefab to create as the player</param>
/// <param name="approved">Is the player approved or not?</param>
/// <param name="position">Used when createPlayerObject is true, position of the player when spawned </param>
/// <param name="rotation">Used when createPlayerObject is true, rotation of the player when spawned</param>
internal void HandleApproval(ulong ownerClientId, bool createPlayerObject, uint? playerPrefabHash, bool approved, Vector3? position, Quaternion? rotation)
/// <param name="ownerClientId">The Network Id of the client being approved</param>
/// <param name="response">The response to allow the player in or not, with its parameters</param>
internal void HandleConnectionApproval(ulong ownerClientId, ConnectionApprovalResponse response)
{
if (approved)
if (response.Approved)
{
// Inform new client it got approved
PendingClients.Remove(ownerClientId);
@@ -1836,10 +2021,23 @@ namespace Unity.Netcode
m_ConnectedClientsList.Add(client);
m_ConnectedClientIds.Add(client.ClientId);
if (createPlayerObject)
if (response.CreatePlayerObject)
{
var networkObject = SpawnManager.CreateLocalNetworkObject(false, playerPrefabHash ?? NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash, ownerClientId, null, position, rotation);
SpawnManager.SpawnNetworkObjectLocally(networkObject, SpawnManager.GetNetworkObjectId(), false, true, ownerClientId, false);
var networkObject = SpawnManager.CreateLocalNetworkObject(
isSceneObject: false,
response.PlayerPrefabHash ?? NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash,
ownerClientId,
parentNetworkId: null,
networkSceneHandle: null,
response.Position,
response.Rotation);
SpawnManager.SpawnNetworkObjectLocally(
networkObject,
SpawnManager.GetNetworkObjectId(),
sceneObject: false,
playerObject: true,
ownerClientId,
destroyWithScene: false);
ConnectedClients[ownerClientId].PlayerObject = networkObject;
}
@@ -1879,13 +2077,13 @@ namespace Unity.Netcode
InvokeOnClientConnectedCallback(ownerClientId);
}
if (!createPlayerObject || (playerPrefabHash == null && NetworkConfig.PlayerPrefab == null))
if (!response.CreatePlayerObject || (response.PlayerPrefabHash == null && NetworkConfig.PlayerPrefab == null))
{
return;
}
// Separating this into a contained function call for potential further future separation of when this notification is sent.
ApprovedPlayerSpawn(ownerClientId, playerPrefabHash ?? NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash);
ApprovedPlayerSpawn(ownerClientId, response.PlayerPrefabHash ?? NetworkConfig.PlayerPrefab.GetComponent<NetworkObject>().GlobalObjectIdHash);
}
else
{

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace Unity.Netcode
{
@@ -75,7 +76,7 @@ namespace Unity.Netcode
public bool IsPlayerObject { get; internal set; }
/// <summary>
/// Gets if the object is the the personal clients player object
/// Gets if the object is the personal clients player object
/// </summary>
public bool IsLocalPlayer => NetworkManager != null && IsPlayerObject && OwnerClientId == NetworkManager.LocalClientId;
@@ -151,6 +152,7 @@ namespace Unity.Netcode
#endif
}
private readonly HashSet<ulong> m_EmptyULongHashSet = new HashSet<ulong>();
/// <summary>
/// Returns Observers enumerator
/// </summary>
@@ -159,7 +161,7 @@ namespace Unity.Netcode
{
if (!IsSpawned)
{
throw new SpawnStateException("Object is not spawned");
return m_EmptyULongHashSet.GetEnumerator();
}
return Observers.GetEnumerator();
@@ -174,15 +176,62 @@ namespace Unity.Netcode
{
if (!IsSpawned)
{
throw new SpawnStateException("Object is not spawned");
return false;
}
return Observers.Contains(clientId);
}
/// <summary>
/// In the event the scene of origin gets unloaded, we keep
/// the most important part to uniquely identify in-scene
/// placed NetworkObjects
/// </summary>
internal int SceneOriginHandle = 0;
private Scene m_SceneOrigin;
/// <summary>
/// The scene where the NetworkObject was first instantiated
/// Note: Primarily for in-scene placed NetworkObjects
/// We need to keep track of the original scene of origin for
/// the NetworkObject in order to be able to uniquely identify it
/// using the scene of origin's handle.
/// </summary>
internal Scene SceneOrigin
{
get
{
return m_SceneOrigin;
}
return Observers.Contains(clientId);
set
{
// The scene origin should only be set once.
// Once set, it should never change.
if (SceneOriginHandle == 0 && value.IsValid() && value.isLoaded)
{
m_SceneOrigin = value;
SceneOriginHandle = value.handle;
}
}
}
/// <summary>
/// Helper method to return the correct scene handle
/// Note: Do not use this within NetworkSpawnManager.SpawnNetworkObjectLocallyCommon
/// </summary>
internal int GetSceneOriginHandle()
{
if (SceneOriginHandle == 0 && IsSpawned && IsSceneObject != false)
{
throw new Exception($"{nameof(GetSceneOriginHandle)} called when {nameof(SceneOriginHandle)} is still zero but the {nameof(NetworkObject)} is already spawned!");
}
return SceneOriginHandle != 0 ? SceneOriginHandle : gameObject.scene.handle;
}
private void Awake()
{
SetCachedParent(transform.parent);
SceneOrigin = gameObject.scene;
}
/// <summary>
@@ -285,7 +334,8 @@ namespace Unity.Netcode
var message = new DestroyObjectMessage
{
NetworkObjectId = NetworkObjectId
NetworkObjectId = NetworkObjectId,
DestroyGameObject = true
};
// Send destroy call
var size = NetworkManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, clientId);
@@ -372,7 +422,7 @@ namespace Unity.Netcode
throw new NotServerException($"Only server can spawn {nameof(NetworkObject)}s");
}
NetworkManager.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.SpawnManager.GetNetworkObjectId(), false, playerObject, ownerClientId, destroyWithScene);
NetworkManager.SpawnManager.SpawnNetworkObjectLocally(this, NetworkManager.SpawnManager.GetNetworkObjectId(), IsSceneObject.HasValue && IsSceneObject.Value, playerObject, ownerClientId, destroyWithScene);
for (int i = 0; i < NetworkManager.ConnectedClientsList.Count; i++)
{
@@ -491,16 +541,34 @@ namespace Unity.Netcode
m_LatestParent = latestParent;
}
/// <summary>
/// Set the parent of the NetworkObject transform.
/// </summary>
/// <param name="parent">The new parent for this NetworkObject transform will be the child of.</param>
/// <param name="worldPositionStays">If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.</param>
/// <returns>Whether or not reparenting was successful.</returns>
public bool TrySetParent(Transform parent, bool worldPositionStays = true)
{
return TrySetParent(parent.GetComponent<NetworkObject>(), worldPositionStays);
}
/// <summary>
/// Set the parent of the NetworkObject transform.
/// </summary>
/// <param name="parent">The new parent for this NetworkObject transform will be the child of.</param>
/// <param name="worldPositionStays">If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.</param>
/// <returns>Whether or not reparenting was successful.</returns>
public bool TrySetParent(GameObject parent, bool worldPositionStays = true)
{
return TrySetParent(parent.GetComponent<NetworkObject>(), worldPositionStays);
}
/// <summary>
/// Set the parent of the NetworkObject transform.
/// </summary>
/// <param name="parent">The new parent for this NetworkObject transform will be the child of.</param>
/// <param name="worldPositionStays">If true, the parent-relative position, scale and rotation are modified such that the object keeps the same world space position, rotation and scale as before.</param>
/// <returns>Whether or not reparenting was successful.</returns>
public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
{
if (!AutoObjectParentSync)
@@ -763,8 +831,8 @@ namespace Unity.Netcode
// if and when we have different systems for where it is expected that orphans survive across ticks,
// then this warning will remind us that we need to revamp the system because then we can no longer simply
// spawn the orphan without its parent (at least, not when its transform is set to local coords mode)
// - because then youll have children popping at the wrong location not having their parents global position to root them
// - and then theyll pop to the correct location after they get the parent, and that would be not good
// - because then you'll have children popping at the wrong location not having their parent's global position to root them
// - and then they'll pop to the correct location after they get the parent, and that would be not good
internal static void VerifyParentingStatus()
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
@@ -865,16 +933,17 @@ namespace Unity.Netcode
public NetworkObject OwnerObject;
public ulong TargetClientId;
public int NetworkSceneHandle;
public unsafe void Serialize(FastBufferWriter writer)
{
if (!writer.TryBeginWrite(
sizeof(HeaderData) +
(Header.HasParent ? FastBufferWriter.GetWriteSize(ParentObjectId) : 0) +
(Header.HasTransform ? FastBufferWriter.GetWriteSize(Transform) : 0) +
(Header.IsReparented
? FastBufferWriter.GetWriteSize(IsLatestParentSet) +
(IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0)
: 0)))
var writeSize = sizeof(HeaderData);
writeSize += Header.HasParent ? FastBufferWriter.GetWriteSize(ParentObjectId) : 0;
writeSize += Header.HasTransform ? FastBufferWriter.GetWriteSize(Transform) : 0;
writeSize += Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) + (IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0) : 0;
writeSize += Header.IsSceneObject ? FastBufferWriter.GetWriteSize<int>() : 0;
if (!writer.TryBeginWrite(writeSize))
{
throw new OverflowException("Could not serialize SceneObject: Out of buffer space.");
}
@@ -900,6 +969,16 @@ namespace Unity.Netcode
}
}
// In-Scene NetworkObjects are uniquely identified NetworkPrefabs defined by their
// NetworkSceneHandle and GlobalObjectIdHash. Since each loaded scene has a unique
// handle, it provides us with a unique and persistent "scene prefab asset" instance.
// This is only set on in-scene placed NetworkObjects to reduce the over-all packet
// sizes for dynamically spawned NetworkObjects.
if (Header.IsSceneObject)
{
writer.WriteValue(OwnerObject.GetSceneOriginHandle());
}
OwnerObject.WriteNetworkVariableData(writer, TargetClientId);
}
@@ -910,10 +989,12 @@ namespace Unity.Netcode
throw new OverflowException("Could not deserialize SceneObject: Out of buffer space.");
}
reader.ReadValue(out Header);
if (!reader.TryBeginRead(
(Header.HasParent ? FastBufferWriter.GetWriteSize(ParentObjectId) : 0) +
(Header.HasTransform ? FastBufferWriter.GetWriteSize(Transform) : 0) +
(Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) : 0)))
var readSize = Header.HasParent ? FastBufferWriter.GetWriteSize(ParentObjectId) : 0;
readSize += Header.HasTransform ? FastBufferWriter.GetWriteSize(Transform) : 0;
readSize += Header.IsReparented ? FastBufferWriter.GetWriteSize(IsLatestParentSet) + (IsLatestParentSet ? FastBufferWriter.GetWriteSize<ulong>() : 0) : 0;
readSize += Header.IsSceneObject ? FastBufferWriter.GetWriteSize<int>() : 0;
if (!reader.TryBeginRead(readSize))
{
throw new OverflowException("Could not deserialize SceneObject: Out of buffer space.");
}
@@ -937,6 +1018,16 @@ namespace Unity.Netcode
LatestParent = latestParent;
}
}
// In-Scene NetworkObjects are uniquely identified NetworkPrefabs defined by their
// NetworkSceneHandle and GlobalObjectIdHash. Since each loaded scene has a unique
// handle, it provides us with a unique and persistent "scene prefab asset" instance.
// Client-side NetworkSceneManagers use this to locate their local instance of the
// NetworkObject instance.
if (Header.IsSceneObject)
{
reader.ReadValueSafe(out NetworkSceneHandle);
}
}
}
@@ -1006,6 +1097,7 @@ namespace Unity.Netcode
Vector3? position = null;
Quaternion? rotation = null;
ulong? parentNetworkId = null;
int? networkSceneHandle = null;
if (sceneObject.Header.HasTransform)
{
@@ -1018,10 +1110,15 @@ namespace Unity.Netcode
parentNetworkId = sceneObject.ParentObjectId;
}
if (sceneObject.Header.IsSceneObject)
{
networkSceneHandle = sceneObject.NetworkSceneHandle;
}
//Attempt to create a local NetworkObject
var networkObject = networkManager.SpawnManager.CreateLocalNetworkObject(
sceneObject.Header.IsSceneObject, sceneObject.Header.Hash,
sceneObject.Header.OwnerClientId, parentNetworkId, position, rotation, sceneObject.Header.IsReparented);
sceneObject.Header.OwnerClientId, parentNetworkId, networkSceneHandle, position, rotation, sceneObject.Header.IsReparented);
networkObject?.SetNetworkParenting(sceneObject.Header.IsReparented, sceneObject.LatestParent);

View File

@@ -7,25 +7,55 @@ using UnityEngine.PlayerLoop;
namespace Unity.Netcode
{
/// <summary>
/// Defines the required interface of a network update system being executed by the network update loop.
/// Defines the required interface of a network update system being executed by the <see cref="NetworkUpdateLoop"/>.
/// </summary>
public interface INetworkUpdateSystem
{
/// <summary>
/// The update method that is being executed in the context of related <see cref="NetworkUpdateStage"/>.
/// </summary>
/// <param name="updateStage">The <see cref="NetworkUpdateStage"/> that is being executed.</param>
void NetworkUpdate(NetworkUpdateStage updateStage);
}
/// <summary>
/// Defines network update stages being executed by the network update loop.
/// See for more details on update stages:
/// https://docs.unity3d.com/ScriptReference/PlayerLoop.Initialization.html
/// </summary>
public enum NetworkUpdateStage : byte
{
Unset = 0, // Default
/// <summary>
/// Default value
/// </summary>
Unset = 0,
/// <summary>
/// Very first initialization update
/// </summary>
Initialization = 1,
/// <summary>
/// Invoked before Fixed update
/// </summary>
EarlyUpdate = 2,
/// <summary>
/// Fixed Update (i.e. state machine, physics, animations, etc)
/// </summary>
FixedUpdate = 3,
/// <summary>
/// Updated before the Monobehaviour.Update for all components is invoked
/// </summary>
PreUpdate = 4,
/// <summary>
/// Updated when the Monobehaviour.Update for all components is invoked
/// </summary>
Update = 5,
/// <summary>
/// Updated before the Monobehaviour.LateUpdate for all components is invoked
/// </summary>
PreLateUpdate = 6,
/// <summary>
/// Updated after the Monobehaviour.LateUpdate for all components is invoked
/// </summary>
PostLateUpdate = 7
}
@@ -53,6 +83,7 @@ namespace Unity.Netcode
/// <summary>
/// Registers a network update system to be executed in all network update stages.
/// </summary>
/// <param name="updateSystem">The <see cref="INetworkUpdateSystem"/> implementation to register for all network updates</param>
public static void RegisterAllNetworkUpdates(this INetworkUpdateSystem updateSystem)
{
foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
@@ -64,6 +95,8 @@ namespace Unity.Netcode
/// <summary>
/// Registers a network update system to be executed in a specific network update stage.
/// </summary>
/// <param name="updateSystem">The <see cref="INetworkUpdateSystem"/> implementation to register for all network updates</param>
/// <param name="updateStage">The <see cref="NetworkUpdateStage"/> being registered for the <see cref="INetworkUpdateSystem"/> implementation</param>
public static void RegisterNetworkUpdate(this INetworkUpdateSystem updateSystem, NetworkUpdateStage updateStage = NetworkUpdateStage.Update)
{
var sysSet = s_UpdateSystem_Sets[updateStage];
@@ -94,6 +127,7 @@ namespace Unity.Netcode
/// <summary>
/// Unregisters a network update system from all network update stages.
/// </summary>
/// <param name="updateSystem">The <see cref="INetworkUpdateSystem"/> implementation to deregister from all network updates</param>
public static void UnregisterAllNetworkUpdates(this INetworkUpdateSystem updateSystem)
{
foreach (NetworkUpdateStage updateStage in Enum.GetValues(typeof(NetworkUpdateStage)))
@@ -105,6 +139,8 @@ namespace Unity.Netcode
/// <summary>
/// Unregisters a network update system from a specific network update stage.
/// </summary>
/// <param name="updateSystem">The <see cref="INetworkUpdateSystem"/> implementation to deregister from all network updates</param>
/// <param name="updateStage">The <see cref="NetworkUpdateStage"/> to be deregistered from the <see cref="INetworkUpdateSystem"/> implementation</param>
public static void UnregisterNetworkUpdate(this INetworkUpdateSystem updateSystem, NetworkUpdateStage updateStage = NetworkUpdateStage.Update)
{
var sysSet = s_UpdateSystem_Sets[updateStage];

View File

@@ -7,8 +7,18 @@ namespace Unity.Netcode
/// </summary>
public class InvalidParentException : Exception
{
/// <summary>
/// Constructor for <see cref="InvalidParentException"/>
/// </summary>
public InvalidParentException() { }
/// <inheritdoc/>
/// <param name="message"></param>
public InvalidParentException(string message) : base(message) { }
/// <inheritdoc/>
/// <param name="message"></param>
/// <param name="innerException"></param>
public InvalidParentException(string message, Exception innerException) : base(message, innerException) { }
}
}

View File

@@ -26,8 +26,15 @@ namespace Unity.Netcode
public SpawnStateException(string message, Exception inner) : base(message, inner) { }
}
/// <summary>
/// Exception thrown when a specified network channel is invalid
/// </summary>
public class InvalidChannelException : Exception
{
/// <summary>
/// Constructs an InvalidChannelException with a message
/// </summary>
/// <param name="message">the message</param>
public InvalidChannelException(string message) : base(message) { }
}
}

View File

@@ -14,8 +14,23 @@ namespace Unity.Netcode
public static LogLevel CurrentLogLevel => NetworkManager.Singleton == null ? LogLevel.Normal : NetworkManager.Singleton.LogLevel;
// internal logging
/// <summary>
/// Locally logs a info log with Netcode prefixing.
/// </summary>
/// <param name="message">The message to log</param>
public static void LogInfo(string message) => Debug.Log($"[Netcode] {message}");
/// <summary>
/// Locally logs a warning log with Netcode prefixing.
/// </summary>
/// <param name="message">The message to log</param>
public static void LogWarning(string message) => Debug.LogWarning($"[Netcode] {message}");
/// <summary>
/// Locally logs a error log with Netcode prefixing.
/// </summary>
/// <param name="message">The message to log</param>
public static void LogError(string message) => Debug.LogError($"[Netcode] {message}");
/// <summary>

View File

@@ -193,6 +193,7 @@ namespace Unity.Netcode
/// <summary>
/// Sends a named message to all clients
/// </summary>
/// <param name="messageName">The message name to send</param>
/// <param name="messageStream">The message stream containing the data</param>
/// <param name="networkDelivery">The delivery type (QoS) to send data with</param>
public void SendNamedMessageToAll(string messageName, FastBufferWriter messageStream, NetworkDelivery networkDelivery = NetworkDelivery.ReliableSequenced)
@@ -238,7 +239,7 @@ namespace Unity.Netcode
/// Sends the named message
/// </summary>
/// <param name="messageName">The message name to send</param>
/// <param name="clientIds">The clients to send to, sends to everyone if null</param>
/// <param name="clientIds">The clients to send to</param>
/// <param name="messageStream">The message stream containing the data</param>
/// <param name="networkDelivery">The delivery type (QoS) to send data with</param>
public void SendNamedMessage(string messageName, IReadOnlyList<ulong> clientIds, FastBufferWriter messageStream, NetworkDelivery networkDelivery = NetworkDelivery.ReliableSequenced)

View File

@@ -101,16 +101,24 @@ namespace Unity.Netcode
{
// Note: Delegate creation allocates.
// Note: ToArray() also allocates. :(
networkManager.InvokeConnectionApproval(ConnectionData, senderId,
(createPlayerObject, playerPrefabHash, approved, position, rotation) =>
var response = new NetworkManager.ConnectionApprovalResponse();
networkManager.ClientsToApprove[senderId] = response;
networkManager.ConnectionApprovalCallback(
new NetworkManager.ConnectionApprovalRequest
{
var localCreatePlayerObject = createPlayerObject;
networkManager.HandleApproval(senderId, localCreatePlayerObject, playerPrefabHash, approved, position, rotation);
});
Payload = ConnectionData,
ClientNetworkId = senderId
}, response);
}
else
{
networkManager.HandleApproval(senderId, networkManager.NetworkConfig.PlayerPrefab != null, null, true, null, null);
var response = new NetworkManager.ConnectionApprovalResponse
{
Approved = true,
CreatePlayerObject = networkManager.NetworkConfig.PlayerPrefab != null
};
networkManager.HandleConnectionApproval(senderId, response);
}
}
}

View File

@@ -19,7 +19,7 @@ namespace Unity.Netcode
}
ObjectInfo.Deserialize(reader);
if (!networkManager.NetworkConfig.ForceSamePrefabs && !networkManager.SpawnManager.HasPrefab(ObjectInfo.Header.IsSceneObject, ObjectInfo.Header.Hash))
if (!networkManager.NetworkConfig.ForceSamePrefabs && !networkManager.SpawnManager.HasPrefab(ObjectInfo))
{
networkManager.DeferredMessageManager.DeferMessage(IDeferredMessageManager.TriggerType.OnAddPrefab, ObjectInfo.Header.Hash, reader, ref context);
return false;

View File

@@ -3,6 +3,7 @@ namespace Unity.Netcode
internal struct DestroyObjectMessage : INetworkMessage, INetworkSerializeByMemcpy
{
public ulong NetworkObjectId;
public bool DestroyGameObject;
public void Serialize(FastBufferWriter writer)
{
@@ -37,7 +38,7 @@ namespace Unity.Netcode
}
networkManager.NetworkMetrics.TrackObjectDestroyReceived(context.SenderId, networkObject, context.MessageSize);
networkManager.SpawnManager.OnDespawnObject(networkObject, true);
networkManager.SpawnManager.OnDespawnObject(networkObject, DestroyGameObject);
}
}
}

View File

@@ -3,21 +3,52 @@ using Unity.Collections;
namespace Unity.Netcode
{
/// <summary>
/// Server-Side RPC
/// Place holder. <see cref="ServerRpcParams"/>
/// Note: Clients always send to one destination when sending RPCs to the server
/// so this structure is a place holder
/// </summary>
public struct ServerRpcSendParams
{
}
/// <summary>
/// The receive parameters for server-side remote procedure calls
/// </summary>
public struct ServerRpcReceiveParams
{
/// <summary>
/// Server-Side RPC
/// The client identifier of the sender
/// </summary>
public ulong SenderClientId;
}
/// <summary>
/// Server-Side RPC
/// Can be used with any sever-side remote procedure call
/// Note: typically this is use primarily for the <see cref="ServerRpcReceiveParams"/>
/// </summary>
public struct ServerRpcParams
{
/// <summary>
/// The server RPC send parameters (currently a place holder)
/// </summary>
public ServerRpcSendParams Send;
/// <summary>
/// The client RPC receive parameters provides you with the sender's identifier
/// </summary>
public ServerRpcReceiveParams Receive;
}
/// <summary>
/// Client-Side RPC
/// The send parameters, when sending client RPCs, provides you wil the ability to
/// target specific clients as a managed or unmanaged list:
/// <see cref="TargetClientIds"/> and <see cref="TargetClientIdsNativeArray"/>
/// </summary>
public struct ClientRpcSendParams
{
/// <summary>
@@ -34,13 +65,32 @@ namespace Unity.Netcode
public NativeArray<ulong>? TargetClientIdsNativeArray;
}
/// <summary>
/// Client-Side RPC
/// Place holder. <see cref="ServerRpcParams"/>
/// Note: Server will always be the sender, so this structure is a place holder
/// </summary>
public struct ClientRpcReceiveParams
{
}
/// <summary>
/// Client-Side RPC
/// Can be used with any client-side remote procedure call
/// Note: Typically this is used primarily for sending to a specific list
/// of clients as opposed to the default (all).
/// <see cref="ClientRpcSendParams"/>
/// </summary>
public struct ClientRpcParams
{
/// <summary>
/// The client RPC send parameters provides you with the ability to send to a specific list of clients
/// </summary>
public ClientRpcSendParams Send;
/// <summary>
/// The client RPC receive parameters (currently a place holder)
/// </summary>
public ClientRpcReceiveParams Receive;
}

View File

@@ -8,7 +8,7 @@ namespace Unity.Netcode
/// Event based NetworkVariable container for syncing Lists
/// </summary>
/// <typeparam name="T">The type for the list</typeparam>
public class NetworkList<T> : NetworkVariableSerialization<T> where T : unmanaged, IEquatable<T>
public class NetworkList<T> : NetworkVariableBase where T : unmanaged, IEquatable<T>
{
private NativeList<T> m_List = new NativeList<T>(64, Allocator.Persistent);
private NativeList<T> m_ListAtLastReset = new NativeList<T>(64, Allocator.Persistent);
@@ -25,8 +25,15 @@ namespace Unity.Netcode
/// </summary>
public event OnListChangedDelegate OnListChanged;
/// <summary>
/// Constructor method for <see cref="NetworkList"/>
/// </summary>
public NetworkList() { }
/// <inheritdoc/>
/// <param name="values"></param>
/// <param name="readPerm"></param>
/// <param name="writePerm"></param>
public NetworkList(IEnumerable<T> values = default,
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
@@ -72,34 +79,35 @@ namespace Unity.Netcode
writer.WriteValueSafe((ushort)m_DirtyEvents.Length);
for (int i = 0; i < m_DirtyEvents.Length; i++)
{
writer.WriteValueSafe(m_DirtyEvents[i].Type);
switch (m_DirtyEvents[i].Type)
var element = m_DirtyEvents.ElementAt(i);
writer.WriteValueSafe(element.Type);
switch (element.Type)
{
case NetworkListEvent<T>.EventType.Add:
{
Write(writer, m_DirtyEvents[i].Value);
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
}
break;
case NetworkListEvent<T>.EventType.Insert:
{
writer.WriteValueSafe(m_DirtyEvents[i].Index);
Write(writer, m_DirtyEvents[i].Value);
writer.WriteValueSafe(element.Index);
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
}
break;
case NetworkListEvent<T>.EventType.Remove:
{
Write(writer, m_DirtyEvents[i].Value);
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
}
break;
case NetworkListEvent<T>.EventType.RemoveAt:
{
writer.WriteValueSafe(m_DirtyEvents[i].Index);
writer.WriteValueSafe(element.Index);
}
break;
case NetworkListEvent<T>.EventType.Value:
{
writer.WriteValueSafe(m_DirtyEvents[i].Index);
Write(writer, m_DirtyEvents[i].Value);
writer.WriteValueSafe(element.Index);
NetworkVariableSerialization<T>.Write(writer, ref element.Value);
}
break;
case NetworkListEvent<T>.EventType.Clear:
@@ -117,7 +125,7 @@ namespace Unity.Netcode
writer.WriteValueSafe((ushort)m_ListAtLastReset.Length);
for (int i = 0; i < m_ListAtLastReset.Length; i++)
{
Write(writer, m_ListAtLastReset[i]);
NetworkVariableSerialization<T>.Write(writer, ref m_ListAtLastReset.ElementAt(i));
}
}
@@ -128,7 +136,7 @@ namespace Unity.Netcode
reader.ReadValueSafe(out ushort count);
for (int i = 0; i < count; i++)
{
Read(reader, out T value);
NetworkVariableSerialization<T>.Read(reader, out T value);
m_List.Add(value);
}
}
@@ -144,7 +152,7 @@ namespace Unity.Netcode
{
case NetworkListEvent<T>.EventType.Add:
{
Read(reader, out T value);
NetworkVariableSerialization<T>.Read(reader, out T value);
m_List.Add(value);
if (OnListChanged != null)
@@ -171,7 +179,7 @@ namespace Unity.Netcode
case NetworkListEvent<T>.EventType.Insert:
{
reader.ReadValueSafe(out int index);
Read(reader, out T value);
NetworkVariableSerialization<T>.Read(reader, out T value);
m_List.InsertRangeWithBeginEnd(index, index + 1);
m_List[index] = value;
@@ -198,7 +206,7 @@ namespace Unity.Netcode
break;
case NetworkListEvent<T>.EventType.Remove:
{
Read(reader, out T value);
NetworkVariableSerialization<T>.Read(reader, out T value);
int index = m_List.IndexOf(value);
if (index == -1)
{
@@ -258,7 +266,7 @@ namespace Unity.Netcode
case NetworkListEvent<T>.EventType.Value:
{
reader.ReadValueSafe(out int index);
Read(reader, out T value);
NetworkVariableSerialization<T>.Read(reader, out T value);
if (index >= m_List.Length)
{
throw new Exception("Shouldn't be here, index is higher than list length");
@@ -447,6 +455,9 @@ namespace Unity.Netcode
OnListChanged?.Invoke(listEvent);
}
/// <summary>
/// This is actually unused left-over from a previous interface
/// </summary>
public int LastModifiedTick
{
get
@@ -456,6 +467,11 @@ namespace Unity.Netcode
}
}
/// <summary>
/// Overridden <see cref="IDisposable"/> implementation.
/// CAUTION: If you derive from this class and override the <see cref="Dispose"/> method,
/// you **must** always invoke the base.Dispose() method!
/// </summary>
public override void Dispose()
{
m_List.Dispose();

View File

@@ -8,8 +8,9 @@ namespace Unity.Netcode
/// <summary>
/// A variable that can be synchronized over the network.
/// </summary>
/// <typeparam name="T">the unmanaged type for <see cref="NetworkVariable{T}"/> </typeparam>
[Serializable]
public class NetworkVariable<T> : NetworkVariableSerialization<T> where T : unmanaged
public class NetworkVariable<T> : NetworkVariableBase where T : unmanaged
{
/// <summary>
/// Delegate type for value changed event
@@ -22,7 +23,12 @@ namespace Unity.Netcode
/// </summary>
public OnValueChangedDelegate OnValueChanged;
/// <summary>
/// Constructor for <see cref="NetworkVariable{T}"/>
/// </summary>
/// <param name="value">initial value set that is of type T</param>
/// <param name="readPerm">the <see cref="NetworkVariableReadPermission"/> for this <see cref="NetworkVariable{T}"/></param>
/// <param name="writePerm">the <see cref="NetworkVariableWritePermission"/> for this <see cref="NetworkVariable{T}"/></param>
public NetworkVariable(T value = default,
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
@@ -31,6 +37,9 @@ namespace Unity.Netcode
m_InternalValue = value;
}
/// <summary>
/// The internal value of the NetworkVariable
/// </summary>
[SerializeField]
private protected T m_InternalValue;
@@ -58,7 +67,7 @@ namespace Unity.Netcode
}
// Compares two values of the same unmanaged type by underlying memory
// Ignoring any overriden value checks
// Ignoring any overridden value checks
// Size is fixed
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe bool ValueEquals(ref T a, ref T b)
@@ -71,7 +80,11 @@ namespace Unity.Netcode
return UnsafeUtility.MemCmp(aptr, bptr, sizeof(T)) == 0;
}
/// <summary>
/// Sets the <see cref="Value"/>, marks the <see cref="NetworkVariable{T}"/> dirty, and invokes the <see cref="OnValueChanged"/> callback
/// if there are subscribers to that event.
/// </summary>
/// <param name="value">the new value of type `T` to be set/></param>
private protected void Set(T value)
{
m_IsDirty = true;
@@ -102,7 +115,7 @@ namespace Unity.Netcode
// would be stored in different fields
T previousValue = m_InternalValue;
Read(reader, out m_InternalValue);
NetworkVariableSerialization<T>.Read(reader, out m_InternalValue);
if (keepDirtyDelta)
{
@@ -115,13 +128,13 @@ namespace Unity.Netcode
/// <inheritdoc />
public override void ReadField(FastBufferReader reader)
{
Read(reader, out m_InternalValue);
NetworkVariableSerialization<T>.Read(reader, out m_InternalValue);
}
/// <inheritdoc />
public override void WriteField(FastBufferWriter writer)
{
Write(writer, m_InternalValue);
NetworkVariableSerialization<T>.Write(writer, ref m_InternalValue);
}
}
}

View File

@@ -12,16 +12,36 @@ namespace Unity.Netcode
/// </summary>
internal const NetworkDelivery Delivery = NetworkDelivery.ReliableFragmentedSequenced;
/// <summary>
/// Maintains a link to the associated NetworkBehaviour
/// </summary>
private protected NetworkBehaviour m_NetworkBehaviour;
/// <summary>
/// Initializes the NetworkVariable
/// </summary>
/// <param name="networkBehaviour">The NetworkBehaviour the NetworkVariable belongs to</param>
public void Initialize(NetworkBehaviour networkBehaviour)
{
m_NetworkBehaviour = networkBehaviour;
}
/// <summary>
/// The default read permissions
/// </summary>
public const NetworkVariableReadPermission DefaultReadPerm = NetworkVariableReadPermission.Everyone;
/// <summary>
/// The default write permissions
/// </summary>
public const NetworkVariableWritePermission DefaultWritePerm = NetworkVariableWritePermission.Server;
/// <summary>
/// The default constructor for <see cref="NetworkVariableBase"/> that can be used to create a
/// custom NetworkVariable.
/// </summary>
/// <param name="readPerm">the <see cref="NetworkVariableReadPermission"/> access settings</param>
/// <param name="writePerm">the <see cref="NetworkVariableWritePermission"/> access settings</param>
protected NetworkVariableBase(
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
@@ -30,6 +50,10 @@ namespace Unity.Netcode
WritePerm = writePerm;
}
/// <summary>
/// The <see cref="m_IsDirty"/> property is used to determine if the
/// value of the `NetworkVariable` has changed.
/// </summary>
private protected bool m_IsDirty;
/// <summary>
@@ -43,11 +67,15 @@ namespace Unity.Netcode
/// </summary>
public readonly NetworkVariableReadPermission ReadPerm;
/// <summary>
/// The write permission for this var
/// </summary>
public readonly NetworkVariableWritePermission WritePerm;
/// <summary>
/// Sets whether or not the variable needs to be delta synced
/// </summary>
/// <param name="isDirty">Whether or not the var is dirty</param>
public virtual void SetDirty(bool isDirty)
{
m_IsDirty = isDirty;
@@ -70,6 +98,11 @@ namespace Unity.Netcode
return m_IsDirty;
}
/// <summary>
/// Gets if a specific client has permission to read the var or not
/// </summary>
/// <param name="clientId">The client id</param>
/// <returns>Whether or not the client has permission to read</returns>
public bool CanClientRead(ulong clientId)
{
switch (ReadPerm)
@@ -82,6 +115,11 @@ namespace Unity.Netcode
}
}
/// <summary>
/// Gets if a specific client has permission to write the var or not
/// </summary>
/// <param name="clientId">The client id</param>
/// <returns>Whether or not the client has permission to write</returns>
public bool CanClientWrite(ulong clientId)
{
switch (WritePerm)
@@ -127,6 +165,9 @@ namespace Unity.Netcode
/// <param name="keepDirtyDelta">Whether or not the delta should be kept as dirty or consumed</param>
public abstract void ReadDelta(FastBufferReader reader, bool keepDirtyDelta);
/// <summary>
/// Virtual <see cref="IDisposable"/> implementation
/// </summary>
public virtual void Dispose()
{
}

View File

@@ -1,77 +0,0 @@
using System;
using UnityEngine;
namespace Unity.Netcode
{
public class NetworkVariableHelper
{
// This is called by ILPP during module initialization for all unmanaged INetworkSerializable types
// This sets up NetworkVariable so that it properly calls NetworkSerialize() when wrapping an INetworkSerializable value
//
// The reason this is done is to avoid runtime reflection and boxing in NetworkVariable - without this,
// NetworkVariable would need to do a `var is INetworkSerializable` check, and then cast to INetworkSerializable,
// *both* of which would cause a boxing allocation. Alternatively, NetworkVariable could have been split into
// NetworkVariable and NetworkSerializableVariable or something like that, which would have caused a poor
// user experience and an API that's easier to get wrong than right. This is a bit ugly on the implementation
// side, but it gets the best achievable user experience and performance.
//
// RuntimeAccessModifiersILPP will make this `public`
internal static void InitializeDelegatesNetworkSerializable<T>() where T : unmanaged, INetworkSerializable
{
NetworkVariableSerialization<T>.SetWriteDelegate(NetworkVariableSerialization<T>.WriteNetworkSerializable);
NetworkVariableSerialization<T>.SetReadDelegate(NetworkVariableSerialization<T>.ReadNetworkSerializable);
}
internal static void InitializeDelegatesStruct<T>() where T : unmanaged, INetworkSerializeByMemcpy
{
NetworkVariableSerialization<T>.SetWriteDelegate(NetworkVariableSerialization<T>.WriteStruct);
NetworkVariableSerialization<T>.SetReadDelegate(NetworkVariableSerialization<T>.ReadStruct);
}
internal static void InitializeDelegatesEnum<T>() where T : unmanaged, Enum
{
NetworkVariableSerialization<T>.SetWriteDelegate(NetworkVariableSerialization<T>.WriteEnum);
NetworkVariableSerialization<T>.SetReadDelegate(NetworkVariableSerialization<T>.ReadEnum);
}
internal static void InitializeDelegatesPrimitive<T>() where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>
{
NetworkVariableSerialization<T>.SetWriteDelegate(NetworkVariableSerialization<T>.WritePrimitive);
NetworkVariableSerialization<T>.SetReadDelegate(NetworkVariableSerialization<T>.ReadPrimitive);
}
internal static void InitializeAllBaseDelegates()
{
// Built-in C# types, serialized through a generic method
InitializeDelegatesPrimitive<bool>();
InitializeDelegatesPrimitive<byte>();
InitializeDelegatesPrimitive<sbyte>();
InitializeDelegatesPrimitive<char>();
InitializeDelegatesPrimitive<decimal>();
InitializeDelegatesPrimitive<float>();
InitializeDelegatesPrimitive<double>();
InitializeDelegatesPrimitive<short>();
InitializeDelegatesPrimitive<ushort>();
InitializeDelegatesPrimitive<int>();
InitializeDelegatesPrimitive<uint>();
InitializeDelegatesPrimitive<long>();
InitializeDelegatesPrimitive<ulong>();
// Built-in Unity types, serialized with specific overloads because they're structs without ISerializeByMemcpy attached
NetworkVariableSerialization<Vector2>.SetWriteDelegate((FastBufferWriter writer, in Vector2 value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Vector3>.SetWriteDelegate((FastBufferWriter writer, in Vector3 value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Vector4>.SetWriteDelegate((FastBufferWriter writer, in Vector4 value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Quaternion>.SetWriteDelegate((FastBufferWriter writer, in Quaternion value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Color>.SetWriteDelegate((FastBufferWriter writer, in Color value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Color32>.SetWriteDelegate((FastBufferWriter writer, in Color32 value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Ray>.SetWriteDelegate((FastBufferWriter writer, in Ray value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Ray2D>.SetWriteDelegate((FastBufferWriter writer, in Ray2D value) => { writer.WriteValueSafe(value); });
NetworkVariableSerialization<Vector2>.SetReadDelegate((FastBufferReader reader, out Vector2 value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Vector3>.SetReadDelegate((FastBufferReader reader, out Vector3 value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Vector4>.SetReadDelegate((FastBufferReader reader, out Vector4 value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Quaternion>.SetReadDelegate((FastBufferReader reader, out Quaternion value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Color>.SetReadDelegate((FastBufferReader reader, out Color value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Color32>.SetReadDelegate((FastBufferReader reader, out Color32 value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Ray>.SetReadDelegate((FastBufferReader reader, out Ray value) => { reader.ReadValueSafe(out value); });
NetworkVariableSerialization<Ray2D>.SetReadDelegate((FastBufferReader reader, out Ray2D value) => { reader.ReadValueSafe(out value); });
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: e54b65208bd3bbe4eaf62ca0384ae21f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,14 +1,32 @@
namespace Unity.Netcode
{
/// <summary>
/// The permission types for reading a var
/// </summary>
public enum NetworkVariableReadPermission
{
/// <summary>
/// Everyone can read
/// </summary>
Everyone,
/// <summary>
/// Only the owner and the server can read
/// </summary>
Owner,
}
/// <summary>
/// The permission types for writing a var
/// </summary>
public enum NetworkVariableWritePermission
{
/// <summary>
/// Only the server can write
/// </summary>
Server,
/// <summary>
/// Only the owner can write
/// </summary>
Owner
}
}

View File

@@ -1,169 +1,267 @@
using System;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// Interface used by NetworkVariables to serialize them
/// </summary>
/// <typeparam name="T"></typeparam>
internal interface INetworkVariableSerializer<T>
{
// Write has to be taken by ref here because of INetworkSerializable
// Open Instance Delegates (pointers to methods without an instance attached to them)
// require the first parameter passed to them (the instance) to be passed by ref.
// So foo.Bar() becomes BarDelegate(ref foo);
// Taking T as an in parameter like we do in other places would require making a copy
// of it to pass it as a ref parameter.
public void Write(FastBufferWriter writer, ref T value);
public void Read(FastBufferReader reader, out T value);
}
/// <summary>
/// Basic serializer for unmanaged types.
/// This covers primitives, built-in unity types, and IForceSerializeByMemcpy
/// Since all of those ultimately end up calling WriteUnmanagedSafe, this simplifies things
/// by calling that directly - thus preventing us from having to have a specific T that meets
/// the specific constraints that the various generic WriteValue calls require.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class UnmanagedTypeSerializer<T> : INetworkVariableSerializer<T> where T : unmanaged
{
public void Write(FastBufferWriter writer, ref T value)
{
writer.WriteUnmanagedSafe(value);
}
public void Read(FastBufferReader reader, out T value)
{
reader.ReadUnmanagedSafe(out value);
}
}
/// <summary>
/// Serializer for FixedStrings, which does the same thing FastBufferWriter/FastBufferReader do,
/// but is implemented to get the data it needs using open instance delegates that are passed in
/// via reflection. This prevents needing T to meet any interface requirements (which isn't achievable
/// without incurring GC allocs on every call to Write or Read - reflection + Open Instance Delegates
/// circumvent that.)
///
/// Tests show that calling these delegates doesn't cause any GC allocations even though they're
/// obtained via reflection and Delegate.CreateDelegate() and called on types that, at compile time,
/// aren't known to actually contain those methods.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class FixedStringSerializer<T> : INetworkVariableSerializer<T> where T : unmanaged
{
internal delegate int GetLengthDelegate(ref T value);
internal delegate void SetLengthDelegate(ref T value, int length);
internal unsafe delegate byte* GetUnsafePtrDelegate(ref T value);
internal GetLengthDelegate GetLength;
internal SetLengthDelegate SetLength;
internal GetUnsafePtrDelegate GetUnsafePtr;
public unsafe void Write(FastBufferWriter writer, ref T value)
{
int length = GetLength(ref value);
byte* data = GetUnsafePtr(ref value);
writer.WriteUnmanagedSafe(length);
writer.WriteBytesSafe(data, length);
}
public unsafe void Read(FastBufferReader reader, out T value)
{
value = new T();
reader.ReadValueSafe(out int length);
SetLength(ref value, length);
reader.ReadBytesSafe(GetUnsafePtr(ref value), length);
}
}
/// <summary>
/// Serializer for INetworkSerializable types, which does the same thing
/// FastBufferWriter/FastBufferReader do, but is implemented to call the NetworkSerialize() method
/// via open instance delegates passed in via reflection. This prevents needing T to meet any interface
/// requirements (which isn't achievable without incurring GC allocs on every call to Write or Read -
/// reflection + Open Instance Delegates circumvent that.)
///
/// Tests show that calling these delegates doesn't cause any GC allocations even though they're
/// obtained via reflection and Delegate.CreateDelegate() and called on types that, at compile time,
/// aren't known to actually contain those methods.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class NetworkSerializableSerializer<T> : INetworkVariableSerializer<T> where T : unmanaged
{
internal delegate void WriteValueDelegate(ref T value, BufferSerializer<BufferSerializerWriter> serializer);
internal delegate void ReadValueDelegate(ref T value, BufferSerializer<BufferSerializerReader> serializer);
internal WriteValueDelegate WriteValue;
internal ReadValueDelegate ReadValue;
public void Write(FastBufferWriter writer, ref T value)
{
var bufferSerializer = new BufferSerializer<BufferSerializerWriter>(new BufferSerializerWriter(writer));
WriteValue(ref value, bufferSerializer);
}
public void Read(FastBufferReader reader, out T value)
{
value = new T();
var bufferSerializer = new BufferSerializer<BufferSerializerReader>(new BufferSerializerReader(reader));
ReadValue(ref value, bufferSerializer);
}
}
/// <summary>
/// This class is used to register user serialization with NetworkVariables for types
/// that are serialized via user serialization, such as with FastBufferReader and FastBufferWriter
/// extension methods. Finding those methods isn't achievable efficiently at runtime, so this allows
/// users to tell NetworkVariable about those extension methods (or simply pass in a lambda)
/// </summary>
/// <typeparam name="T"></typeparam>
public class UserNetworkVariableSerialization<T>
{
/// <summary>
/// The write value delegate handler definition
/// </summary>
/// <param name="writer">The <see cref="FastBufferWriter"/> to write the value of type `T`</param>
/// <param name="value">The value of type `T` to be written</param>
public delegate void WriteValueDelegate(FastBufferWriter writer, in T value);
/// <summary>
/// The read value delegate handler definition
/// </summary>
/// <param name="reader">The <see cref="FastBufferReader"/> to read the value of type `T`</param>
/// <param name="value">The value of type `T` to be read</param>
public delegate void ReadValueDelegate(FastBufferReader reader, out T value);
/// <summary>
/// The <see cref="WriteValueDelegate"/> delegate handler declaration
/// </summary>
public static WriteValueDelegate WriteValue;
/// <summary>
/// The <see cref="ReadValueDelegate"/> delegate handler declaration
/// </summary>
public static ReadValueDelegate ReadValue;
}
/// <summary>
/// This class is instantiated for types that we can't determine ahead of time are serializable - types
/// that don't meet any of the constraints for methods that are available on FastBufferReader and
/// FastBufferWriter. These types may or may not be serializable through extension methods. To ensure
/// the user has time to pass in the delegates to UserNetworkVariableSerialization, the existence
/// of user serialization isn't checked until it's used, so if no serialization is provided, this
/// will throw an exception when an object containing the relevant NetworkVariable is spawned.
/// </summary>
/// <typeparam name="T"></typeparam>
internal class FallbackSerializer<T> : INetworkVariableSerializer<T>
{
public void Write(FastBufferWriter writer, ref T value)
{
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null)
{
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)} and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
}
UserNetworkVariableSerialization<T>.WriteValue(writer, value);
}
public void Read(FastBufferReader reader, out T value)
{
if (UserNetworkVariableSerialization<T>.ReadValue == null || UserNetworkVariableSerialization<T>.WriteValue == null)
{
throw new ArgumentException($"Type {typeof(T).FullName} is not supported by {typeof(NetworkVariable<>).Name}. If this is a type you can change, then either implement {nameof(INetworkSerializable)} or mark it as serializable by memcpy by adding {nameof(INetworkSerializeByMemcpy)} to its interface list. If not, assign serialization code to {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.WriteValue)} and {nameof(UserNetworkVariableSerialization<T>)}.{nameof(UserNetworkVariableSerialization<T>.ReadValue)}, or if it's serializable by memcpy (contains no pointers), wrap it in {typeof(ForceNetworkSerializeByMemcpy<>).Name}.");
}
UserNetworkVariableSerialization<T>.ReadValue(reader, out value);
}
}
internal static class NetworkVariableSerializationTypes
{
internal static readonly HashSet<Type> BaseSupportedTypes = new HashSet<Type>
{
typeof(bool),
typeof(byte),
typeof(sbyte),
typeof(char),
typeof(decimal),
typeof(double),
typeof(float),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(short),
typeof(ushort),
typeof(Vector2),
typeof(Vector3),
typeof(Vector2Int),
typeof(Vector3Int),
typeof(Vector4),
typeof(Quaternion),
typeof(Color),
typeof(Color32),
typeof(Ray),
typeof(Ray2D)
};
}
/// <summary>
/// Support methods for reading/writing NetworkVariables
/// Because there are multiple overloads of WriteValue/ReadValue based on different generic constraints,
/// but there's no way to achieve the same thing with a class, this includes various read/write delegates
/// based on which constraints are met by `T`. These constraints are set up by `NetworkVariableHelpers`,
/// which is invoked by code generated by ILPP during module load.
/// (As it turns out, IL has support for a module initializer that C# doesn't expose.)
/// This installs the correct delegate for each `T` to ensure that each type is serialized properly.
///
/// Any type that inherits from `NetworkVariableSerialization<T>` will implicitly result in any `T`
/// passed to it being picked up and initialized by ILPP.
///
/// The methods here, despite being static, are `protected` specifically to ensure that anything that
/// wants access to them has to inherit from this base class, thus enabling ILPP to find and initialize it.
/// but there's no way to achieve the same thing with a class, this sets up various read/write schemes
/// based on which constraints are met by `T` using reflection, which is done at module load time.
/// </summary>
/// <typeparam name="T">The type the associated NetworkVariable is templated on</typeparam>
[Serializable]
public abstract class NetworkVariableSerialization<T> : NetworkVariableBase where T : unmanaged
public static class NetworkVariableSerialization<T> where T : unmanaged
{
// Functions that know how to serialize INetworkSerializable
internal static void WriteNetworkSerializable<TForMethod>(FastBufferWriter writer, in TForMethod value)
where TForMethod : unmanaged, INetworkSerializable
{
writer.WriteNetworkSerializable(value);
}
private static INetworkVariableSerializer<T> s_Serializer = GetSerializer();
internal static void ReadNetworkSerializable<TForMethod>(FastBufferReader reader, out TForMethod value)
where TForMethod : unmanaged, INetworkSerializable
private static INetworkVariableSerializer<T> GetSerializer()
{
reader.ReadNetworkSerializable(out value);
}
// Functions that serialize structs
internal static void WriteStruct<TForMethod>(FastBufferWriter writer, in TForMethod value)
where TForMethod : unmanaged, INetworkSerializeByMemcpy
{
writer.WriteValueSafe(value);
}
internal static void ReadStruct<TForMethod>(FastBufferReader reader, out TForMethod value)
where TForMethod : unmanaged, INetworkSerializeByMemcpy
{
reader.ReadValueSafe(out value);
}
// Functions that serialize enums
internal static void WriteEnum<TForMethod>(FastBufferWriter writer, in TForMethod value)
where TForMethod : unmanaged, Enum
{
writer.WriteValueSafe(value);
}
internal static void ReadEnum<TForMethod>(FastBufferReader reader, out TForMethod value)
where TForMethod : unmanaged, Enum
{
reader.ReadValueSafe(out value);
}
// Functions that serialize other types
internal static void WritePrimitive<TForMethod>(FastBufferWriter writer, in TForMethod value)
where TForMethod : unmanaged, IComparable, IConvertible, IComparable<TForMethod>, IEquatable<TForMethod>
{
writer.WriteValueSafe(value);
}
internal static void ReadPrimitive<TForMethod>(FastBufferReader reader, out TForMethod value)
where TForMethod : unmanaged, IComparable, IConvertible, IComparable<TForMethod>, IEquatable<TForMethod>
{
reader.ReadValueSafe(out value);
}
// Should never be reachable at runtime. All calls to this should be replaced with the correct
// call above by ILPP.
private static void WriteValue<TForMethod>(FastBufferWriter writer, in TForMethod value)
where TForMethod : unmanaged
{
if (value is INetworkSerializable)
if (NetworkVariableSerializationTypes.BaseSupportedTypes.Contains(typeof(T)))
{
typeof(NetworkVariableHelper).GetMethod(nameof(NetworkVariableHelper.InitializeDelegatesNetworkSerializable)).MakeGenericMethod(typeof(TForMethod)).Invoke(null, null);
return new UnmanagedTypeSerializer<T>();
}
else if (value is INetworkSerializeByMemcpy)
if (typeof(INetworkSerializeByMemcpy).IsAssignableFrom(typeof(T)))
{
typeof(NetworkVariableHelper).GetMethod(nameof(NetworkVariableHelper.InitializeDelegatesStruct)).MakeGenericMethod(typeof(TForMethod)).Invoke(null, null);
return new UnmanagedTypeSerializer<T>();
}
else if (value is Enum)
if (typeof(Enum).IsAssignableFrom(typeof(T)))
{
typeof(NetworkVariableHelper).GetMethod(nameof(NetworkVariableHelper.InitializeDelegatesEnum)).MakeGenericMethod(typeof(TForMethod)).Invoke(null, null);
return new UnmanagedTypeSerializer<T>();
}
else
{
throw new Exception($"Type {typeof(T).FullName} is not serializable - it must implement either INetworkSerializable or ISerializeByMemcpy");
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(T)))
{
// Obtains "Open Instance Delegates" for the type's NetworkSerialize() methods -
// one for an instance of the generic method taking BufferSerializerWriter as T,
// one for an instance of the generic method taking BufferSerializerReader as T
var writeMethod = (NetworkSerializableSerializer<T>.WriteValueDelegate)Delegate.CreateDelegate(typeof(NetworkSerializableSerializer<T>.WriteValueDelegate), null, typeof(T).GetMethod(nameof(INetworkSerializable.NetworkSerialize)).MakeGenericMethod(typeof(BufferSerializerWriter)));
var readMethod = (NetworkSerializableSerializer<T>.ReadValueDelegate)Delegate.CreateDelegate(typeof(NetworkSerializableSerializer<T>.ReadValueDelegate), null, typeof(T).GetMethod(nameof(INetworkSerializable.NetworkSerialize)).MakeGenericMethod(typeof(BufferSerializerReader)));
return new NetworkSerializableSerializer<T> { WriteValue = writeMethod, ReadValue = readMethod };
}
NetworkVariableSerialization<TForMethod>.Write(writer, value);
if (typeof(IUTF8Bytes).IsAssignableFrom(typeof(T)) && typeof(INativeList<byte>).IsAssignableFrom(typeof(T)))
{
// Get "OpenInstanceDelegates" for the Length property (get and set, which are prefixed
// with "get_" and "set_" under the hood and emitted as methods) and GetUnsafePtr()
var getLength = (FixedStringSerializer<T>.GetLengthDelegate)Delegate.CreateDelegate(typeof(FixedStringSerializer<T>.GetLengthDelegate), null, typeof(T).GetMethod("get_" + nameof(INativeList<byte>.Length)));
var setLength = (FixedStringSerializer<T>.SetLengthDelegate)Delegate.CreateDelegate(typeof(FixedStringSerializer<T>.SetLengthDelegate), null, typeof(T).GetMethod("set_" + nameof(INativeList<byte>.Length)));
var getUnsafePtr = (FixedStringSerializer<T>.GetUnsafePtrDelegate)Delegate.CreateDelegate(typeof(FixedStringSerializer<T>.GetUnsafePtrDelegate), null, typeof(T).GetMethod(nameof(IUTF8Bytes.GetUnsafePtr)));
return new FixedStringSerializer<T> { GetLength = getLength, SetLength = setLength, GetUnsafePtr = getUnsafePtr };
}
return new FallbackSerializer<T>();
}
private static void ReadValue<TForMethod>(FastBufferReader reader, out TForMethod value)
where TForMethod : unmanaged
internal static void Write(FastBufferWriter writer, ref T value)
{
if (typeof(INetworkSerializable).IsAssignableFrom(typeof(TForMethod)))
{
typeof(NetworkVariableHelper).GetMethod(nameof(NetworkVariableHelper.InitializeDelegatesNetworkSerializable)).MakeGenericMethod(typeof(TForMethod)).Invoke(null, null);
}
else if (typeof(INetworkSerializeByMemcpy).IsAssignableFrom(typeof(TForMethod)))
{
typeof(NetworkVariableHelper).GetMethod(nameof(NetworkVariableHelper.InitializeDelegatesStruct)).MakeGenericMethod(typeof(TForMethod)).Invoke(null, null);
}
else if (typeof(Enum).IsAssignableFrom(typeof(TForMethod)))
{
typeof(NetworkVariableHelper).GetMethod(nameof(NetworkVariableHelper.InitializeDelegatesEnum)).MakeGenericMethod(typeof(TForMethod)).Invoke(null, null);
}
else
{
throw new Exception($"Type {typeof(T).FullName} is not serializable - it must implement either INetworkSerializable or ISerializeByMemcpy");
}
NetworkVariableSerialization<TForMethod>.Read(reader, out value);
s_Serializer.Write(writer, ref value);
}
protected internal delegate void WriteDelegate<TForMethod>(FastBufferWriter writer, in TForMethod value);
protected internal delegate void ReadDelegate<TForMethod>(FastBufferReader reader, out TForMethod value);
// These static delegates provide the right implementation for writing and reading a particular network variable type.
// For most types, these default to WriteValue() and ReadValue(), which perform simple memcpy operations.
//
// INetworkSerializableILPP will generate startup code that will set it to WriteNetworkSerializable()
// and ReadNetworkSerializable() for INetworkSerializable types, which will call NetworkSerialize().
//
// In the future we may be able to use this to provide packing implementations for floats and integers to optimize bandwidth usage.
//
// The reason this is done is to avoid runtime reflection and boxing in NetworkVariable - without this,
// NetworkVariable would need to do a `var is INetworkSerializable` check, and then cast to INetworkSerializable,
// *both* of which would cause a boxing allocation. Alternatively, NetworkVariable could have been split into
// NetworkVariable and NetworkSerializableVariable or something like that, which would have caused a poor
// user experience and an API that's easier to get wrong than right. This is a bit ugly on the implementation
// side, but it gets the best achievable user experience and performance.
private static WriteDelegate<T> s_Write = WriteValue;
private static ReadDelegate<T> s_Read = ReadValue;
protected static void Write(FastBufferWriter writer, in T value)
{
s_Write(writer, value);
}
protected static void Read(FastBufferReader reader, out T value)
{
s_Read(reader, out value);
}
internal static void SetWriteDelegate(WriteDelegate<T> write)
{
s_Write = write;
}
internal static void SetReadDelegate(ReadDelegate<T> read)
{
s_Read = read;
}
protected NetworkVariableSerialization(
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
: base(readPerm, writePerm)
internal static void Read(FastBufferReader reader, out T value)
{
s_Serializer.Read(reader, out value);
}
}
}

View File

@@ -15,8 +15,14 @@ namespace Unity.Netcode
{
internal uint SceneEventId;
internal Action<uint> EventAction;
/// <summary>
/// Used server-side for integration testing in order to
/// invoke the SceneEventProgress once done loading
/// </summary>
internal Action Completed;
internal void Invoke()
{
Completed?.Invoke();
EventAction.Invoke(SceneEventId);
}
}

View File

@@ -12,6 +12,7 @@ namespace Unity.Netcode
/// delegate type <see cref="NetworkSceneManager.SceneEventDelegate"/> uses this class to provide
/// scene event status.<br/>
/// <em>Note: This is only when <see cref="NetworkConfig.EnableSceneManagement"/> is enabled.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// See also: <br/>
/// <seealso cref="SceneEventType"/>
/// </summary>
@@ -166,6 +167,7 @@ namespace Unity.Netcode
/// <item><term><see cref="OnUnloadComplete"/> Invoked only when an <see cref="SceneEventType.UnloadComplete"/> event is being processed</term></item>
/// <item><term><see cref="OnSynchronizeComplete"/> Invoked only when a <see cref="SceneEventType.SynchronizeComplete"/> event is being processed</term></item>
/// </list>
/// <em>Note: Do not start new scene events within NetworkSceneManager scene event notification callbacks.</em><br/>
/// </summary>
public event SceneEventDelegate OnSceneEvent;
@@ -239,13 +241,15 @@ namespace Unity.Netcode
/// <summary>
/// Invoked when a <see cref="SceneEventType.Load"/> event is started by the server.<br/>
/// <em>Note: The server and connected client(s) will always receive this notification.</em>
/// <em>Note: The server and connected client(s) will always receive this notification.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnLoadDelegateHandler OnLoad;
/// <summary>
/// Invoked when a <see cref="SceneEventType.Unload"/> event is started by the server.<br/>
/// <em>Note: The server and connected client(s) will always receive this notification.</em>
/// <em>Note: The server and connected client(s) will always receive this notification.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnUnloadDelegateHandler OnUnload;
@@ -254,7 +258,8 @@ namespace Unity.Netcode
/// after a client is approved for connection in order to synchronize the client with the currently loaded
/// scenes and NetworkObjects. This event signifies the beginning of the synchronization event.<br/>
/// <em>Note: The server and connected client(s) will always receive this notification.
/// This event is generated on a per newly connected and approved client basis.</em>
/// This event is generated on a per newly connected and approved client basis.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnSynchronizeDelegateHandler OnSynchronize;
@@ -263,7 +268,8 @@ namespace Unity.Netcode
/// This event signifies the end of an existing <see cref="SceneEventType.Load"/> event as it pertains
/// to all clients connected when the event was started. This event signifies that all clients (and server) have
/// finished the <see cref="SceneEventType.Load"/> event.<br/>
/// <em>Note: this is useful to know when all clients have loaded the same scene (single or additive mode)</em>
/// <em>Note: this is useful to know when all clients have loaded the same scene (single or additive mode)</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnEventCompletedDelegateHandler OnLoadEventCompleted;
@@ -273,21 +279,24 @@ namespace Unity.Netcode
/// to all clients connected when the event was started. This event signifies that all clients (and server) have
/// finished the <see cref="SceneEventType.Unload"/> event.<br/>
/// <em>Note: this is useful to know when all clients have unloaded a specific scene. The <see cref="LoadSceneMode"/> will
/// always be <see cref="LoadSceneMode.Additive"/> for this event.</em>
/// always be <see cref="LoadSceneMode.Additive"/> for this event.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnEventCompletedDelegateHandler OnUnloadEventCompleted;
/// <summary>
/// Invoked when a <see cref="SceneEventType.LoadComplete"/> event is generated by a client or server.<br/>
/// <em>Note: The server receives this message from all clients (including itself).
/// Each client receives their own notification sent to the server.</em>
/// Each client receives their own notification sent to the server.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnLoadCompleteDelegateHandler OnLoadComplete;
/// <summary>
/// Invoked when a <see cref="SceneEventType.UnloadComplete"/> event is generated by a client or server.<br/>
/// <em>Note: The server receives this message from all clients (including itself).
/// Each client receives their own notification sent to the server.</em>
/// Each client receives their own notification sent to the server.</em><br/>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnUnloadCompleteDelegateHandler OnUnloadComplete;
@@ -296,6 +305,7 @@ namespace Unity.Netcode
/// <em> Note: The server receives this message from the client, but will never generate this event for itself.
/// Each client receives their own notification sent to the server. This is useful to know that a client has
/// completed the entire connection sequence, loaded all scenes, and synchronized all NetworkObjects.</em>
/// <em>*** Do not start new scene events within scene event notification callbacks.</em><br/>
/// </summary>
public event OnSynchronizeCompleteDelegateHandler OnSynchronizeComplete;
@@ -319,8 +329,12 @@ namespace Unity.Netcode
public VerifySceneBeforeLoadingDelegateHandler VerifySceneBeforeLoading;
/// <summary>
/// Proof of concept: Interface version that allows for the decoupling from
/// the SceneManager's Load and Unload methods for testing purposes (potentially other future usage)
/// The SceneManagerHandler implementation
/// </summary>
internal ISceneManagerHandler SceneManagerHandler = new DefaultSceneManagerHandler();
/// <summary>
/// The default SceneManagerHandler that interfaces between the SceneManager and NetworkSceneManager
/// </summary>
private class DefaultSceneManagerHandler : ISceneManagerHandler
{
@@ -339,10 +353,6 @@ namespace Unity.Netcode
}
}
internal ISceneManagerHandler SceneManagerHandler = new DefaultSceneManagerHandler();
/// End of Proof of Concept
internal readonly Dictionary<Guid, SceneEventProgress> SceneEventProgressTracking = new Dictionary<Guid, SceneEventProgress>();
/// <summary>
@@ -425,6 +435,8 @@ namespace Unity.Netcode
/// </summary>
public void Dispose()
{
SceneUnloadEventHandler.Shutdown();
foreach (var keypair in SceneEventDataStore)
{
if (NetworkLog.CurrentLogLevel <= LogLevel.Normal)
@@ -632,6 +644,12 @@ namespace Unity.Netcode
return validated;
}
/// <summary>
/// Used for NetcodeIntegrationTest testing in order to properly
/// assign the right loaded scene to the right client's ScenesLoaded list
/// </summary>
internal Func<string, Scene> OverrideGetAndAddNewlyLoadedSceneByName;
/// <summary>
/// Since SceneManager.GetSceneByName only returns the first scene that matches the name
/// we must "find" a newly added scene by looking through all loaded scenes and determining
@@ -643,20 +661,27 @@ namespace Unity.Netcode
/// <returns></returns>
internal Scene GetAndAddNewlyLoadedSceneByName(string sceneName)
{
for (int i = 0; i < SceneManager.sceneCount; i++)
if (OverrideGetAndAddNewlyLoadedSceneByName != null)
{
var sceneLoaded = SceneManager.GetSceneAt(i);
if (sceneLoaded.name == sceneName)
return OverrideGetAndAddNewlyLoadedSceneByName.Invoke(sceneName);
}
else
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
if (!ScenesLoaded.ContainsKey(sceneLoaded.handle))
var sceneLoaded = SceneManager.GetSceneAt(i);
if (sceneLoaded.name == sceneName)
{
ScenesLoaded.Add(sceneLoaded.handle, sceneLoaded);
return sceneLoaded;
if (!ScenesLoaded.ContainsKey(sceneLoaded.handle))
{
ScenesLoaded.Add(sceneLoaded.handle, sceneLoaded);
return sceneLoaded;
}
}
}
}
throw new Exception($"Failed to find any loaded scene named {sceneName}!");
throw new Exception($"Failed to find any loaded scene named {sceneName}!");
}
}
/// <summary>
@@ -720,18 +745,18 @@ namespace Unity.Netcode
/// </summary>
/// <param name="globalObjectIdHash"></param>
/// <returns></returns>
internal NetworkObject GetSceneRelativeInSceneNetworkObject(uint globalObjectIdHash)
internal NetworkObject GetSceneRelativeInSceneNetworkObject(uint globalObjectIdHash, int? networkSceneHandle)
{
if (ScenePlacedObjects.ContainsKey(globalObjectIdHash))
{
if (ScenePlacedObjects[globalObjectIdHash].ContainsKey(SceneBeingSynchronized.handle))
var sceneHandle = SceneBeingSynchronized.handle;
if (networkSceneHandle.HasValue && networkSceneHandle.Value != 0)
{
var inScenePlacedNetworkObject = ScenePlacedObjects[globalObjectIdHash][SceneBeingSynchronized.handle];
// We can only have 1 duplicated globalObjectIdHash per scene instance, so remove it once it has been returned
ScenePlacedObjects[globalObjectIdHash].Remove(SceneBeingSynchronized.handle);
return inScenePlacedNetworkObject;
sceneHandle = ServerSceneHandleToClientSceneHandle[networkSceneHandle.Value];
}
if (ScenePlacedObjects[globalObjectIdHash].ContainsKey(sceneHandle))
{
return ScenePlacedObjects[globalObjectIdHash][sceneHandle];
}
}
return null;
@@ -859,7 +884,6 @@ namespace Unity.Netcode
/// Callback for the <see cref="SceneEventProgress.OnComplete"/> <see cref="SceneEventProgress.OnCompletedDelegate"/> handler
/// </summary>
/// <param name="sceneEventProgress"></param>
/// <returns></returns>
private bool OnSceneEventProgressCompleted(SceneEventProgress sceneEventProgress)
{
var sceneEventData = BeginSceneEvent();
@@ -868,7 +892,7 @@ namespace Unity.Netcode
sceneEventData.SceneEventType = sceneEventProgress.SceneEventType;
sceneEventData.ClientsCompleted = sceneEventProgress.DoneClients;
sceneEventData.LoadSceneMode = sceneEventProgress.LoadSceneMode;
sceneEventData.ClientsTimedOut = m_NetworkManager.ConnectedClients.Keys.Except(sceneEventProgress.DoneClients).ToList();
sceneEventData.ClientsTimedOut = sceneEventProgress.ClientsThatStartedSceneEvent.Except(sceneEventProgress.DoneClients).ToList();
var message = new SceneEventMessage
{
@@ -911,7 +935,7 @@ namespace Unity.Netcode
/// Unloads an additively loaded scene. If you want to unload a <see cref="LoadSceneMode.Single"/> mode loaded scene load another <see cref="LoadSceneMode.Single"/> scene.
/// When applicable, the <see cref="AsyncOperation"/> is delivered within the <see cref="SceneEvent"/> via the <see cref="OnSceneEvent"/>
/// </summary>
/// <param name="sceneName">scene name to unload</param>
/// <param name="scene"></param>
/// <returns><see cref="SceneEventProgressStatus"/> (<see cref="SceneEventProgressStatus.Started"/> means it was successful)</returns>
public SceneEventProgressStatus UnloadScene(Scene scene)
{
@@ -945,11 +969,18 @@ namespace Unity.Netcode
sceneEventProgress.SceneEventType = SceneEventType.UnloadEventCompleted;
ScenesLoaded.Remove(scene.handle);
var sceneEventAction = new ISceneManagerHandler.SceneEventAction() { SceneEventId = sceneEventData.SceneEventId, EventAction = OnSceneUnloaded };
var sceneUnload = SceneManagerHandler.UnloadSceneAsync(scene, sceneEventAction);
var sceneUnload = SceneManagerHandler.UnloadSceneAsync(scene,
new ISceneManagerHandler.SceneEventAction() { SceneEventId = sceneEventData.SceneEventId, EventAction = OnSceneUnloaded });
sceneEventProgress.SetSceneLoadOperation(sceneUnload);
// If integration testing, IntegrationTestSceneHandler returns null
if (sceneUnload == null)
{
sceneEventProgress.SetSceneLoadOperation(sceneEventAction);
}
else
{
sceneEventProgress.SetSceneLoadOperation(sceneUnload);
}
// Notify local server that a scene is going to be unloaded
OnSceneEvent?.Invoke(new SceneEvent()
@@ -1021,6 +1052,12 @@ namespace Unity.Netcode
/// </summary>
private void OnSceneUnloaded(uint sceneEventId)
{
// If we are shutdown or about to shutdown, then ignore this event
if (!m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress)
{
return;
}
var sceneEventData = SceneEventDataStore[sceneEventId];
// First thing we do, if we are a server, is to send the unload scene event.
if (m_NetworkManager.IsServer)
@@ -1064,7 +1101,7 @@ namespace Unity.Netcode
private void EmptySceneUnloadedOperation(uint sceneEventId)
{
// Do nothing (this is a stub call since it is only used to flush all currently loaded scenes)
// Do nothing (this is a stub call since it is only used to flush all additively loaded scenes)
}
/// <summary>
@@ -1074,6 +1111,7 @@ namespace Unity.Netcode
/// </summary>
internal void UnloadAdditivelyLoadedScenes(uint sceneEventId)
{
var sceneEventData = SceneEventDataStore[sceneEventId];
// Unload all additive scenes while making sure we don't try to unload the base scene ( loaded in single mode ).
var currentActiveScene = SceneManager.GetActiveScene();
foreach (var keyHandleEntry in ScenesLoaded)
@@ -1083,15 +1121,7 @@ namespace Unity.Netcode
{
var sceneUnload = SceneManagerHandler.UnloadSceneAsync(keyHandleEntry.Value,
new ISceneManagerHandler.SceneEventAction() { SceneEventId = sceneEventId, EventAction = EmptySceneUnloadedOperation });
OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = sceneUnload,
SceneEventType = SceneEventType.Unload,
SceneName = keyHandleEntry.Value.name,
LoadSceneMode = LoadSceneMode.Additive, // The only scenes unloaded are scenes that were additively loaded
ClientId = NetworkManager.ServerClientId
});
SceneUnloadEventHandler.RegisterScene(this, keyHandleEntry.Value, LoadSceneMode.Additive, sceneUnload);
}
}
// clear out our scenes loaded list
@@ -1104,6 +1134,7 @@ namespace Unity.Netcode
/// When applicable, the <see cref="AsyncOperation"/> is delivered within the <see cref="SceneEvent"/> via <see cref="OnSceneEvent"/>
/// </summary>
/// <param name="sceneName">the name of the scene to be loaded</param>
/// <param name="loadSceneMode">how the scene will be loaded (single or additive mode)</param>
/// <returns><see cref="SceneEventProgressStatus"/> (<see cref="SceneEventProgressStatus.Started"/> means it was successful)</returns>
public SceneEventProgressStatus LoadScene(string sceneName, LoadSceneMode loadSceneMode)
{
@@ -1124,12 +1155,12 @@ namespace Unity.Netcode
sceneEventData.SceneEventType = SceneEventType.Load;
sceneEventData.SceneHash = SceneHashFromNameOrPath(sceneName);
sceneEventData.LoadSceneMode = loadSceneMode;
var sceneEventId = sceneEventData.SceneEventId;
// This both checks to make sure the scene is valid and if not resets the active scene event
m_IsSceneEventActive = ValidateSceneBeforeLoading(sceneEventData.SceneHash, loadSceneMode);
if (!m_IsSceneEventActive)
{
EndSceneEvent(sceneEventData.SceneEventId);
EndSceneEvent(sceneEventId);
return SceneEventProgressStatus.SceneFailedVerification;
}
@@ -1142,14 +1173,24 @@ namespace Unity.Netcode
MoveObjectsToDontDestroyOnLoad();
// Now Unload all currently additively loaded scenes
UnloadAdditivelyLoadedScenes(sceneEventData.SceneEventId);
UnloadAdditivelyLoadedScenes(sceneEventId);
// Register the active scene for unload scene event notifications
SceneUnloadEventHandler.RegisterScene(this, SceneManager.GetActiveScene(), LoadSceneMode.Single);
}
// Now start loading the scene
var sceneLoad = SceneManagerHandler.LoadSceneAsync(sceneName, loadSceneMode,
new ISceneManagerHandler.SceneEventAction() { SceneEventId = sceneEventData.SceneEventId, EventAction = OnSceneLoaded });
sceneEventProgress.SetSceneLoadOperation(sceneLoad);
var sceneEventAction = new ISceneManagerHandler.SceneEventAction() { SceneEventId = sceneEventId, EventAction = OnSceneLoaded };
var sceneLoad = SceneManagerHandler.LoadSceneAsync(sceneName, loadSceneMode, sceneEventAction);
// If integration testing, IntegrationTestSceneHandler returns null
if (sceneLoad == null)
{
sceneEventProgress.SetSceneLoadOperation(sceneEventAction);
}
else
{
sceneEventProgress.SetSceneLoadOperation(sceneLoad);
}
// Notify the local server that a scene loading event has begun
OnSceneEvent?.Invoke(new SceneEvent()
@@ -1167,6 +1208,114 @@ namespace Unity.Netcode
return sceneEventProgress.Status;
}
/// <summary>
/// Helper class used to handle "odd ball" scene unload event notification scenarios
/// when scene switching.
/// </summary>
internal class SceneUnloadEventHandler
{
private static Dictionary<NetworkManager, List<SceneUnloadEventHandler>> s_Instances = new Dictionary<NetworkManager, List<SceneUnloadEventHandler>>();
internal static void RegisterScene(NetworkSceneManager networkSceneManager, Scene scene, LoadSceneMode loadSceneMode, AsyncOperation asyncOperation = null)
{
var networkManager = networkSceneManager.m_NetworkManager;
if (!s_Instances.ContainsKey(networkManager))
{
s_Instances.Add(networkManager, new List<SceneUnloadEventHandler>());
}
var clientId = networkManager.IsServer ? NetworkManager.ServerClientId : networkManager.LocalClientId;
s_Instances[networkManager].Add(new SceneUnloadEventHandler(networkSceneManager, scene, clientId, loadSceneMode, asyncOperation));
}
private static void SceneUnloadComplete(SceneUnloadEventHandler sceneUnloadEventHandler)
{
if (sceneUnloadEventHandler == null || sceneUnloadEventHandler.m_NetworkSceneManager == null || sceneUnloadEventHandler.m_NetworkSceneManager.m_NetworkManager == null)
{
return;
}
var networkManager = sceneUnloadEventHandler.m_NetworkSceneManager.m_NetworkManager;
if (s_Instances.ContainsKey(networkManager))
{
s_Instances[networkManager].Remove(sceneUnloadEventHandler);
if (s_Instances[networkManager].Count == 0)
{
s_Instances.Remove(networkManager);
}
}
}
/// <summary>
/// Called by NetworkSceneManager when it is disposing
/// </summary>
internal static void Shutdown()
{
foreach (var instanceEntry in s_Instances)
{
foreach (var instance in instanceEntry.Value)
{
instance.OnShutdown();
}
instanceEntry.Value.Clear();
}
s_Instances.Clear();
}
private NetworkSceneManager m_NetworkSceneManager;
private AsyncOperation m_AsyncOperation;
private LoadSceneMode m_LoadSceneMode;
private ulong m_ClientId;
private Scene m_Scene;
private bool m_ShuttingDown;
private void OnShutdown()
{
m_ShuttingDown = true;
SceneManager.sceneUnloaded -= SceneUnloaded;
}
private void SceneUnloaded(Scene scene)
{
if (m_Scene.handle == scene.handle && !m_ShuttingDown)
{
if (m_NetworkSceneManager != null && m_NetworkSceneManager.m_NetworkManager != null)
{
m_NetworkSceneManager.OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = m_AsyncOperation,
SceneEventType = SceneEventType.UnloadComplete,
SceneName = m_Scene.name,
LoadSceneMode = m_LoadSceneMode,
ClientId = m_ClientId
});
m_NetworkSceneManager.OnUnloadComplete?.Invoke(m_ClientId, m_Scene.name);
}
SceneManager.sceneUnloaded -= SceneUnloaded;
SceneUnloadComplete(this);
}
}
private SceneUnloadEventHandler(NetworkSceneManager networkSceneManager, Scene scene, ulong clientId, LoadSceneMode loadSceneMode, AsyncOperation asyncOperation = null)
{
m_LoadSceneMode = loadSceneMode;
m_AsyncOperation = asyncOperation;
m_NetworkSceneManager = networkSceneManager;
m_ClientId = clientId;
m_Scene = scene;
SceneManager.sceneUnloaded += SceneUnloaded;
// Send the initial unload event notification
m_NetworkSceneManager.OnSceneEvent?.Invoke(new SceneEvent()
{
AsyncOperation = m_AsyncOperation,
SceneEventType = SceneEventType.Unload,
SceneName = m_Scene.name,
LoadSceneMode = m_LoadSceneMode,
ClientId = clientId
});
m_NetworkSceneManager.OnUnload?.Invoke(networkSceneManager.m_NetworkManager.LocalClientId, m_Scene.name, null);
}
}
/// <summary>
/// Client Side:
/// Handles both forms of scene loading
@@ -1201,6 +1350,10 @@ namespace Unity.Netcode
if (sceneEventData.LoadSceneMode == LoadSceneMode.Single)
{
IsSpawnedObjectsPendingInDontDestroyOnLoad = true;
// Register the active scene for unload scene event notifications
SceneUnloadEventHandler.RegisterScene(this, SceneManager.GetActiveScene(), LoadSceneMode.Single);
}
var sceneLoad = SceneManagerHandler.LoadSceneAsync(sceneName, sceneEventData.LoadSceneMode,
@@ -1218,13 +1371,18 @@ namespace Unity.Netcode
OnLoad?.Invoke(m_NetworkManager.LocalClientId, sceneName, sceneEventData.LoadSceneMode, sceneLoad);
}
/// <summary>
/// Client and Server:
/// Generic on scene loaded callback method to be called upon a scene loading
/// </summary>
private void OnSceneLoaded(uint sceneEventId)
{
// If we are shutdown or about to shutdown, then ignore this event
if (!m_NetworkManager.IsListening || m_NetworkManager.ShutdownInProgress)
{
return;
}
var sceneEventData = SceneEventDataStore[sceneEventId];
var nextScene = GetAndAddNewlyLoadedSceneByName(SceneNameFromHash(sceneEventData.SceneHash));
if (!nextScene.isLoaded || !nextScene.IsValid())
@@ -1363,6 +1521,13 @@ namespace Unity.Netcode
EndSceneEvent(sceneEventId);
}
/// <summary>
/// Used for integration testing, due to the complexities of having all clients loading scenes
/// this is needed to "filter" out the scenes not loaded by NetworkSceneManager
/// (i.e. we don't want a late joining player to load all of the other client scenes)
/// </summary>
internal Func<Scene, bool> ExcludeSceneFromSychronization;
/// <summary>
/// Server Side:
/// This is used for players that have just had their connection approved and will assure they are synchronized
@@ -1389,6 +1554,13 @@ namespace Unity.Netcode
{
var scene = SceneManager.GetSceneAt(i);
// NetworkSceneManager does not synchronize scenes that are not loaded by NetworkSceneManager
// unless the scene in question is the currently active scene.
if (ExcludeSceneFromSychronization != null && !ExcludeSceneFromSychronization(scene))
{
continue;
}
var sceneHash = SceneHashFromNameOrPath(scene.path);
// This would depend upon whether we are additive or not
@@ -1406,11 +1578,11 @@ namespace Unity.Netcode
{
continue;
}
sceneEventData.AddSceneToSynchronize(sceneHash, scene.handle);
}
sceneEventData.AddSpawnedNetworkObjects();
sceneEventData.AddDespawnedInSceneNetworkObjects();
var message = new SceneEventMessage
{
@@ -1447,7 +1619,7 @@ namespace Unity.Netcode
var loadSceneMode = sceneHash == sceneEventData.SceneHash ? sceneEventData.LoadSceneMode : LoadSceneMode.Additive;
// Store the sceneHandle and hash
sceneEventData.ClientSceneHandle = sceneHandle;
sceneEventData.NetworkSceneHandle = sceneHandle;
sceneEventData.ClientSceneHash = sceneHash;
// If this is the beginning of the synchronization event, then send client a notification that synchronization has begun
@@ -1536,9 +1708,9 @@ namespace Unity.Netcode
SceneManager.SetActiveScene(nextScene);
}
if (!ServerSceneHandleToClientSceneHandle.ContainsKey(sceneEventData.ClientSceneHandle))
if (!ServerSceneHandleToClientSceneHandle.ContainsKey(sceneEventData.NetworkSceneHandle))
{
ServerSceneHandleToClientSceneHandle.Add(sceneEventData.ClientSceneHandle, nextScene.handle);
ServerSceneHandleToClientSceneHandle.Add(sceneEventData.NetworkSceneHandle, nextScene.handle);
}
else
{
@@ -1862,10 +2034,9 @@ namespace Unity.Netcode
foreach (var networkObjectInstance in networkObjects)
{
var globalObjectIdHash = networkObjectInstance.GlobalObjectIdHash;
var sceneHandle = networkObjectInstance.gameObject.scene.handle;
var sceneHandle = networkObjectInstance.GetSceneOriginHandle();
// We check to make sure the NetworkManager instance is the same one to be "NetcodeIntegrationTestHelpers" compatible and filter the list on a per scene basis (for additive scenes)
if (networkObjectInstance.IsSceneObject != false && networkObjectInstance.NetworkManager == m_NetworkManager && networkObjectInstance.gameObject.scene == sceneToFilterBy &&
sceneHandle == sceneToFilterBy.handle)
if (networkObjectInstance.IsSceneObject != false && networkObjectInstance.NetworkManager == m_NetworkManager && sceneHandle == sceneToFilterBy.handle)
{
if (!ScenePlacedObjects.ContainsKey(globalObjectIdHash))
{

View File

@@ -100,7 +100,7 @@ namespace Unity.Netcode
// Used by the client during synchronization
internal uint ClientSceneHash;
internal int ClientSceneHandle;
internal int NetworkSceneHandle;
/// Only used for <see cref="SceneEventType.Synchronize"/> scene events, this assures permissions when writing
/// NetworkVariable information. If that process changes, then we need to update this
@@ -118,6 +118,9 @@ namespace Unity.Netcode
/// </summary>
private List<NetworkObject> m_NetworkObjectsSync = new List<NetworkObject>();
private List<NetworkObject> m_DespawnedInSceneObjectsSync = new List<NetworkObject>();
private Dictionary<int, List<uint>> m_DespawnedInSceneObjects = new Dictionary<int, List<uint>>();
/// <summary>
/// Server Side Re-Synchronization:
/// If there happens to be NetworkObjects in the final Event_Sync_Complete message that are no longer spawned,
@@ -243,6 +246,19 @@ namespace Unity.Netcode
m_NetworkObjectsSync.Sort(SortNetworkObjects);
}
internal void AddDespawnedInSceneNetworkObjects()
{
m_DespawnedInSceneObjectsSync.Clear();
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>().Where((c) => c.NetworkManager == m_NetworkManager);
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.IsSceneObject.HasValue && sobj.IsSceneObject.Value && !sobj.IsSpawned)
{
m_DespawnedInSceneObjectsSync.Add(sobj);
}
}
}
/// <summary>
/// Server Side:
/// Used during the synchronization process to associate NetworkObjects with scenes
@@ -372,7 +388,6 @@ namespace Unity.Netcode
writer.WriteValueSafe(ScenesToSynchronize.ToArray());
writer.WriteValueSafe(SceneHandlesToSynchronize.ToArray());
// Store our current position in the stream to come back and say how much data we have written
var positionStart = writer.Position;
@@ -383,17 +398,31 @@ namespace Unity.Netcode
int totalBytes = 0;
// Write the number of NetworkObjects we are serializing
writer.WriteValueSafe(m_NetworkObjectsSync.Count());
BytePacker.WriteValuePacked(writer, m_NetworkObjectsSync.Count());
// Serialize all NetworkObjects that are spawned
for (var i = 0; i < m_NetworkObjectsSync.Count(); ++i)
{
var noStart = writer.Position;
var sceneObject = m_NetworkObjectsSync[i].GetMessageSceneObject(TargetClientId);
writer.WriteValueSafe(m_NetworkObjectsSync[i].gameObject.scene.handle);
BytePacker.WriteValuePacked(writer, m_NetworkObjectsSync[i].GetSceneOriginHandle());
sceneObject.Serialize(writer);
var noStop = writer.Position;
totalBytes += (int)(noStop - noStart);
}
// Write the number of despawned in-scene placed NetworkObjects
writer.WriteValueSafe(m_DespawnedInSceneObjectsSync.Count());
// Write the scene handle and GlobalObjectIdHash value
for (var i = 0; i < m_DespawnedInSceneObjectsSync.Count(); ++i)
{
var noStart = writer.Position;
var sceneObject = m_DespawnedInSceneObjectsSync[i].GetMessageSceneObject(TargetClientId);
BytePacker.WriteValuePacked(writer, m_DespawnedInSceneObjectsSync[i].GetSceneOriginHandle());
BytePacker.WriteValuePacked(writer, m_DespawnedInSceneObjectsSync[i].GlobalObjectIdHash);
var noStop = writer.Position;
totalBytes += (int)(noStop - noStart);
}
// Size Place Holder -- End
var positionEnd = writer.Position;
var bytesWritten = (uint)(positionEnd - (positionStart + sizeof(uint)));
@@ -683,15 +712,16 @@ namespace Unity.Netcode
{
try
{
// Process all NetworkObjects for this scene
InternalBuffer.ReadValueSafe(out int newObjectsCount);
// Process all spawned NetworkObjects for this network session
ByteUnpacker.ReadValuePacked(InternalBuffer, out int newObjectsCount);
for (int i = 0; i < newObjectsCount; i++)
{
// We want to make sure for each NetworkObject we have the appropriate scene selected as the scene that is
// currently being synchronized. This assures in-scene placed NetworkObjects will use the right NetworkObject
// from the list of populated <see cref="NetworkSceneManager.ScenePlacedObjects"/>
InternalBuffer.ReadValueSafe(out int handle);
ByteUnpacker.ReadValuePacked(InternalBuffer, out int handle);
m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(handle);
var sceneObject = new NetworkObject.SceneObject();
@@ -703,6 +733,73 @@ namespace Unity.Netcode
m_NetworkObjectsSync.Add(spawnedNetworkObject);
}
}
// Process all de-spawned in-scene NetworkObjects for this network session
m_DespawnedInSceneObjects.Clear();
InternalBuffer.ReadValueSafe(out int despawnedObjectsCount);
var sceneCache = new Dictionary<int, Dictionary<uint, NetworkObject>>();
for (int i = 0; i < despawnedObjectsCount; i++)
{
// We just need to get the scene
ByteUnpacker.ReadValuePacked(InternalBuffer, out int networkSceneHandle);
ByteUnpacker.ReadValuePacked(InternalBuffer, out uint globalObjectIdHash);
var sceneRelativeNetworkObjects = new Dictionary<uint, NetworkObject>();
if (!sceneCache.ContainsKey(networkSceneHandle))
{
if (m_NetworkManager.SceneManager.ServerSceneHandleToClientSceneHandle.ContainsKey(networkSceneHandle))
{
var localSceneHandle = m_NetworkManager.SceneManager.ServerSceneHandleToClientSceneHandle[networkSceneHandle];
if (m_NetworkManager.SceneManager.ScenesLoaded.ContainsKey(localSceneHandle))
{
var objectRelativeScene = m_NetworkManager.SceneManager.ScenesLoaded[localSceneHandle];
var inSceneNetworkObjects = UnityEngine.Object.FindObjectsOfType<NetworkObject>().Where((c) =>
c.GetSceneOriginHandle() == localSceneHandle && (c.IsSceneObject != false)).ToList();
foreach (var inSceneObject in inSceneNetworkObjects)
{
sceneRelativeNetworkObjects.Add(inSceneObject.GlobalObjectIdHash, inSceneObject);
}
// Add this to a cache so we don't have to run this potentially multiple times (nothing will spawn or despawn during this time
sceneCache.Add(networkSceneHandle, sceneRelativeNetworkObjects);
}
else
{
UnityEngine.Debug.LogError($"In-Scene NetworkObject GlobalObjectIdHash ({globalObjectIdHash}) cannot find its relative local scene handle {localSceneHandle}!");
}
}
else
{
UnityEngine.Debug.LogError($"In-Scene NetworkObject GlobalObjectIdHash ({globalObjectIdHash}) cannot find its relative NetworkSceneHandle {networkSceneHandle}!");
}
}
else // Use the cached NetworkObjects if they exist
{
sceneRelativeNetworkObjects = sceneCache[networkSceneHandle];
}
// Now find the in-scene NetworkObject with the current GlobalObjectIdHash we are looking for
if (sceneRelativeNetworkObjects.ContainsKey(globalObjectIdHash))
{
// Since this is a NetworkObject that was never spawned, we just need to send a notification
// out that it was despawned so users can make adjustments
sceneRelativeNetworkObjects[globalObjectIdHash].InvokeBehaviourNetworkDespawn();
if (!m_NetworkManager.SceneManager.ScenePlacedObjects.ContainsKey(globalObjectIdHash))
{
m_NetworkManager.SceneManager.ScenePlacedObjects.Add(globalObjectIdHash, new Dictionary<int, NetworkObject>());
}
if (!m_NetworkManager.SceneManager.ScenePlacedObjects[globalObjectIdHash].ContainsKey(sceneRelativeNetworkObjects[globalObjectIdHash].GetSceneOriginHandle()))
{
m_NetworkManager.SceneManager.ScenePlacedObjects[globalObjectIdHash].Add(sceneRelativeNetworkObjects[globalObjectIdHash].GetSceneOriginHandle(), sceneRelativeNetworkObjects[globalObjectIdHash]);
}
}
else
{
UnityEngine.Debug.LogError($"In-Scene NetworkObject GlobalObjectIdHash ({globalObjectIdHash}) could not be found!");
}
}
}
finally
{

View File

@@ -61,9 +61,9 @@ namespace Unity.Netcode
internal List<ulong> DoneClients { get; } = new List<ulong>();
/// <summary>
/// The NetworkTime at the moment the scene switch was initiated by the server.
/// The local time when the scene event was "roughly started"
/// </summary>
internal NetworkTime TimeAtInitiation { get; }
internal float TimeAtInitiation { get; }
/// <summary>
/// Delegate type for when the switch scene progress is completed. Either by all clients done loading the scene or by time out.
@@ -105,22 +105,40 @@ namespace Unity.Netcode
internal LoadSceneMode LoadSceneMode;
internal List<ulong> ClientsThatStartedSceneEvent;
internal SceneEventProgress(NetworkManager networkManager, SceneEventProgressStatus status = SceneEventProgressStatus.Started)
{
if (status == SceneEventProgressStatus.Started)
{
// Track the clients that were connected when we started this event
ClientsThatStartedSceneEvent = new List<ulong>(networkManager.ConnectedClientsIds);
m_NetworkManager = networkManager;
m_TimeOutCoroutine = m_NetworkManager.StartCoroutine(TimeOutSceneEventProgress());
TimeAtInitiation = networkManager.LocalTime;
TimeAtInitiation = Time.realtimeSinceStartup;
}
Status = status;
}
/// <summary>
/// Coroutine that checks to see if the scene event is complete every network tick period.
/// This will handle completing the scene event when one or more client(s) disconnect(s)
/// during a scene event and if it does not complete within the scene loading time out period
/// it will time out the scene event.
/// </summary>
internal IEnumerator TimeOutSceneEventProgress()
{
yield return new WaitForSecondsRealtime(m_NetworkManager.NetworkConfig.LoadSceneTimeOut);
TimedOut = true;
CheckCompletion();
var waitForNetworkTick = new WaitForSeconds(1.0f / m_NetworkManager.NetworkConfig.TickRate);
while (!TimedOut && !IsCompleted)
{
yield return waitForNetworkTick;
CheckCompletion();
if (!IsCompleted)
{
TimedOut = TimeAtInitiation - Time.realtimeSinceStartup >= m_NetworkManager.NetworkConfig.LoadSceneTimeOut;
}
}
}
internal void AddClientAsDone(ulong clientId)
@@ -141,19 +159,49 @@ namespace Unity.Netcode
m_SceneLoadOperation.completed += operation => CheckCompletion();
}
/// <summary>
/// Called only on the server-side during integration test (NetcodeIntegrationTest specific)
/// scene loading and unloading.
///
/// Note: During integration testing we must queue all scene loading and unloading requests for
/// both the server and all clients so they can be processed in a FIFO/linear fashion to avoid
/// conflicts when the <see cref="SceneManager.sceneLoaded"/> and <see cref="SceneManager.sceneUnloaded"/>
/// events are triggered. The Completed action simulates the <see cref="AsyncOperation.completed"/> event.
/// (See: Unity.Netcode.TestHelpers.Runtime.IntegrationTestSceneHandler)
/// </summary>
internal void SetSceneLoadOperation(ISceneManagerHandler.SceneEventAction sceneEventAction)
{
sceneEventAction.Completed = SetComplete;
}
/// <summary>
/// Finalizes the SceneEventProgress
/// </summary>
internal void SetComplete()
{
IsCompleted = true;
AreAllClientsDoneLoading = true;
// If OnComplete is not registered or it is and returns true then remove this from the progress tracking
if (OnComplete == null || (OnComplete != null && OnComplete.Invoke(this)))
{
m_NetworkManager.SceneManager.SceneEventProgressTracking.Remove(Guid);
}
m_NetworkManager.StopCoroutine(m_TimeOutCoroutine);
}
internal void CheckCompletion()
{
if ((!IsCompleted && DoneClients.Count == m_NetworkManager.ConnectedClientsList.Count && m_SceneLoadOperation.isDone) || (!IsCompleted && TimedOut))
try
{
IsCompleted = true;
AreAllClientsDoneLoading = true;
// If OnComplete is not registered or it is and returns true then remove this from the progress tracking
if (OnComplete == null || (OnComplete != null && OnComplete.Invoke(this)))
if ((!IsCompleted && DoneClients.Count == m_NetworkManager.ConnectedClientsList.Count && (m_SceneLoadOperation == null || m_SceneLoadOperation.isDone)) || (!IsCompleted && TimedOut))
{
m_NetworkManager.SceneManager.SceneEventProgressTracking.Remove(Guid);
SetComplete();
}
m_NetworkManager.StopCoroutine(m_TimeOutCoroutine);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}
}

View File

@@ -2,6 +2,9 @@ using System.Runtime.CompilerServices;
namespace Unity.Netcode
{
/// <summary>
/// Utility class to count the number of bytes or bits needed to serialize a value.
/// </summary>
public static class BitCounter
{
// Since we don't have access to BitOperations.LeadingZeroCount() (which would have been the fastest)

View File

@@ -1,4 +1,5 @@
using System;
using Unity.Collections;
using UnityEngine;
namespace Unity.Netcode
@@ -61,63 +62,526 @@ namespace Unity.Netcode
return m_Implementation.GetFastBufferWriter();
}
/// <summary>
/// Read or write a string
/// </summary>
/// <param name="s">The value to read/write</param>
/// <param name="oneByteChars">If true, characters will be limited to one-byte ASCII characters</param>
public void SerializeValue(ref string s, bool oneByteChars = false) => m_Implementation.SerializeValue(ref s, oneByteChars);
/// <summary>
/// Read or write a single byte
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref byte value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of primitive values (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an enum value
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of enum values
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a struct value implementing ISerializeByMemcpy
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of struct values implementing ISerializeByMemcpy
/// </summary>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a struct or class value implementing INetworkSerializable
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of struct or class values implementing INetworkSerializable
/// </summary>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Vector2 value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Vector2 value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Vector2 values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Vector2[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Vector3 value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Vector3 value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Vector3 values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Vector3[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Vector2Int value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Vector2Int value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Vector2Int values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Vector2Int[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Vector3Int value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Vector3Int value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Vector3Int values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Vector3Int[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Vector4 value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Vector4 value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Vector4 values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Vector4[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Quaternion value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Quaternion value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Quaternion values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Quaternion[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Color value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Color value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Color values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Color[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Color32 value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Color32 value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Color32 values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Color32[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Ray value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Ray value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Ray values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Ray[] value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a Ray2D value
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValue(ref Ray2D value) => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write an array of Ray2D values
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValue(ref Ray2D[] value) => m_Implementation.SerializeValue(ref value);
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
// INativeList<bool> provides the Length property
// IUTF8Bytes provides GetUnsafePtr()
// Those two are necessary to serialize FixedStrings efficiently
// - otherwise we'd just be memcpy'ing the whole thing even if
// most of it isn't used.
/// <summary>
/// Read or write a FixedString value
/// </summary>
/// <typeparam name="T">The network serializable type</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution of FixedStrings</param>
public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes => m_Implementation.SerializeValue(ref value);
/// <summary>
/// Read or write a NetworkSerializable value.
/// SerializeValue() is the preferred method to do this - this is provided for backward compatibility only.
/// </summary>
/// <typeparam name="T">The network serializable type</typeparam>
/// <param name="value">The values to read/write</param>
public void SerializeNetworkSerializable<T>(ref T value) where T : INetworkSerializable, new() => m_Implementation.SerializeNetworkSerializable(ref value);
/// <summary>
/// Performs an advance check to ensure space is available to read/write one or more values.
/// This provides a performance benefit for serializing multiple values using the
/// SerializeValuePreChecked methods. But note that the benefit is small and only likely to be
/// noticeable if serializing a very large number of items.
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
public bool PreCheck(int amount)
{
return m_Implementation.PreCheck(amount);
}
/// <summary>
/// Serialize a string, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="s">The value to read/write</param>
/// <param name="oneByteChars">If true, characters will be limited to one-byte ASCII characters</param>
public void SerializeValuePreChecked(ref string s, bool oneByteChars = false) => m_Implementation.SerializeValuePreChecked(ref s, oneByteChars);
/// <summary>
/// Serialize a byte, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref byte value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a primitive, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable type</typeparam>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize an array of primitives, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable types in an array</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution of primitives</param>
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize an enum, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable type</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution of enums</param>
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize an array of enums, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable types in an array</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution of enums</param>
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a struct, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable type</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution of structs</param>
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize an array of structs, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable types in an array</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution of structs</param>
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector2, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector2 value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector2 array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValuePreChecked(ref Vector2[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector3, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector3 value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector3 array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValuePreChecked(ref Vector3[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector2Int, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector2Int value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector2Int array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The values to read/write</param>
public void SerializeValuePreChecked(ref Vector2Int[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector3Int, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector3Int value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector3Int array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector3Int[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector4, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector4 value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Vector4Array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Vector4[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Quaternion, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Quaternion value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Quaternion array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Quaternion[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Color, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Color value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Color array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Color[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Color32, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Color32 value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Color32 array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Color32[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Ray, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Ray value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Ray array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Ray[] value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Ray2D, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Ray2D value) => m_Implementation.SerializeValuePreChecked(ref value);
/// <summary>
/// Serialize a Ray2D array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
public void SerializeValuePreChecked(ref Ray2D[] value) => m_Implementation.SerializeValuePreChecked(ref value);
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
// INativeList<bool> provides the Length property
// IUTF8Bytes provides GetUnsafePtr()
// Those two are necessary to serialize FixedStrings efficiently
// - otherwise we'd just be memcpying the whole thing even if
// most of it isn't used.
/// <summary>
/// Serialize a FixedString, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The network serializable type</typeparam>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution for fixed strings</param>
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes => m_Implementation.SerializeValuePreChecked(ref value);
}
}

View File

@@ -1,4 +1,5 @@
using System;
using Unity.Collections;
using UnityEngine;
namespace Unity.Netcode
@@ -35,10 +36,18 @@ namespace Unity.Netcode
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Reader.ReadValueSafe(out value);
public void SerializeValue<T>(ref T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Reader.ReadValue(out value);
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Reader.ReadValue(out value);
public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector2 value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector2[] value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector3 value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector3[] value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector2Int value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector2Int[] value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector3Int value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector3Int[] value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector4 value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Vector4[] value) => m_Reader.ReadValueSafe(out value);
public void SerializeValue(ref Quaternion value) => m_Reader.ReadValueSafe(out value);
@@ -67,10 +76,16 @@ namespace Unity.Netcode
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector2 value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector2[] value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector3 value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector3[] value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector2Int value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector2Int[] value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector3Int value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector3Int[] value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector4 value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Vector4[] value) => m_Reader.ReadValue(out value);
public void SerializeValuePreChecked(ref Quaternion value) => m_Reader.ReadValue(out value);

View File

@@ -1,4 +1,5 @@
using System;
using Unity.Collections;
using UnityEngine;
namespace Unity.Netcode
@@ -35,10 +36,17 @@ namespace Unity.Netcode
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Writer.WriteValueSafe(value);
public void SerializeValue<T>(ref T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Writer.WriteValue(value);
public void SerializeValue<T>(ref T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => m_Writer.WriteValue(value);
public void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector2 value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector2[] value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector3 value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector3[] value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector2Int value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector2Int[] value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector3Int value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector3Int[] value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector4 value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Vector4[] value) => m_Writer.WriteValueSafe(value);
public void SerializeValue(ref Quaternion value) => m_Writer.WriteValueSafe(value);
@@ -71,10 +79,17 @@ namespace Unity.Netcode
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => m_Writer.WriteValue(value);
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Writer.WriteValue(value);
public void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => m_Writer.WriteValue(value);
public void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector2 value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector2[] value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector3 value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector3[] value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector2Int value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector2Int[] value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector3Int value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector3Int[] value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector4 value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Vector4[] value) => m_Writer.WriteValue(value);
public void SerializeValuePreChecked(ref Quaternion value) => m_Writer.WriteValue(value);

View File

@@ -6,6 +6,7 @@ namespace Unity.Netcode
{
/// <summary>
/// Utility class for packing values in serialization.
/// <seealso cref="ByteUnpacker"/> to unpack packed values.
/// </summary>
public static class BytePacker
{
@@ -282,14 +283,49 @@ namespace Unity.Netcode
public void WriteValueBitPacked<T>(FastBufferWriter writer, T value) where T: unmanaged => writer.WriteValueSafe(value);
#else
/// <summary>
/// Maximum serializable value for a BitPacked ushort (minimum for unsigned is 0)
/// </summary>
public const ushort BitPackedUshortMax = (1 << 15) - 1;
/// <summary>
/// Maximum serializable value for a BitPacked short
/// </summary>
public const short BitPackedShortMax = (1 << 14) - 1;
/// <summary>
/// Minimum serializable value size for a BitPacked ushort
/// </summary>
public const short BitPackedShortMin = -(1 << 14);
/// <summary>
/// Maximum serializable value for a BitPacked uint (minimum for unsigned is 0)
/// </summary>
public const uint BitPackedUintMax = (1 << 30) - 1;
/// <summary>
/// Maximum serializable value for a BitPacked int
/// </summary>
public const int BitPackedIntMax = (1 << 29) - 1;
/// <summary>
/// Minimum serializable value size for a BitPacked int
/// </summary>
public const int BitPackedIntMin = -(1 << 29);
/// <summary>
/// Maximum serializable value for a BitPacked ulong (minimum for unsigned is 0)
/// </summary>
public const ulong BitPackedULongMax = (1L << 61) - 1;
/// <summary>
/// Maximum serializable value for a BitPacked long
/// </summary>
public const long BitPackedLongMax = (1L << 60) - 1;
/// <summary>
/// Minimum serializable value size for a BitPacked long
/// </summary>
public const long BitPackedLongMin = -(1L << 60);
/// <summary>

View File

@@ -4,14 +4,26 @@ using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// Byte Unpacking Helper Class
/// Use this class to unpack values during deserialization for values that were packed.
/// <seealso cref="BytePacker"/> to pack unpacked values
/// </summary>
public static class ByteUnpacker
{
#if UNITY_NETCODE_DEBUG_NO_PACKING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValuePacked<T>(FastBufferReader reader, out T value) where T: unmanaged => reader.ReadValueSafe(out value);
#else
/// <summary>
/// Read a packed enum value
/// </summary>
/// <param name="reader">The reader to read from</param>
/// <param name="value">The value that's read</param>
/// <typeparam name="TEnum">Type of enum to read</typeparam>
/// <exception cref="InvalidOperationException">Throws InvalidOperationException if an enum somehow ends up not being the size of a byte, short, int, or long (which should be impossible)</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void ReadValuePacked<TEnum>(FastBufferReader reader, out TEnum value) where TEnum : unmanaged, Enum
{
@@ -302,7 +314,7 @@ namespace Unity.Netcode
#endif
#if UNITY_NETCODE_DEBUG_NO_PACKING
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueBitPacked<T>(FastBufferReader reader, T value) where T: unmanaged => reader.ReadValueSafe(out value);
#else

View File

@@ -6,6 +6,12 @@ using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// Optimized class used for reading values from a byte stream
/// <seealso cref="FastBufferWriter"/>
/// <seealso cref="BytePacker"/>
/// <seealso cref="ByteUnpacker"/>
/// </summary>
public struct FastBufferReader : IDisposable
{
internal struct ReaderHandle
@@ -54,25 +60,29 @@ namespace Unity.Netcode
#endif
}
private static unsafe ReaderHandle* CreateHandle(byte* buffer, int length, int offset, Allocator allocator)
private static unsafe ReaderHandle* CreateHandle(byte* buffer, int length, int offset, Allocator copyAllocator, Allocator internalAllocator)
{
ReaderHandle* readerHandle = null;
if (allocator == Allocator.None)
if (copyAllocator == Allocator.None)
{
readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle) + length, UnsafeUtility.AlignOf<byte>(), Allocator.Temp);
readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle) + length, UnsafeUtility.AlignOf<byte>(), internalAllocator);
readerHandle->BufferPointer = buffer;
readerHandle->Position = offset;
}
else
{
readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle) + length, UnsafeUtility.AlignOf<byte>(), allocator);
readerHandle = (ReaderHandle*)UnsafeUtility.Malloc(sizeof(ReaderHandle) + length, UnsafeUtility.AlignOf<byte>(), copyAllocator);
UnsafeUtility.MemCpy(readerHandle + 1, buffer + offset, length);
readerHandle->BufferPointer = (byte*)(readerHandle + 1);
readerHandle->Position = 0;
}
readerHandle->Length = length;
readerHandle->Allocator = allocator;
// If the copyAllocator provided is Allocator.None, there is a chance that the internalAllocator was provided
// When we dispose, we are really only interested in disposing Allocator.Persistent and Allocator.TempJob
// as disposing Allocator.Temp and Allocator.None would do nothing. Therefore, make sure we dispose the readerHandle with the right Allocator label
readerHandle->Allocator = copyAllocator == Allocator.None ? internalAllocator : copyAllocator;
#if DEVELOPMENT_BUILD || UNITY_EDITOR
readerHandle->AllowedReadMark = 0;
readerHandle->InBitwiseContext = false;
@@ -83,23 +93,26 @@ namespace Unity.Netcode
/// <summary>
/// Create a FastBufferReader from a NativeArray.
///
/// A new buffer will be created using the given allocator and the value will be copied in.
/// A new buffer will be created using the given <param name="copyAllocator"></param> and the value will be copied in.
/// FastBufferReader will then own the data.
///
/// The exception to this is when the allocator passed in is Allocator.None. In this scenario,
/// The exception to this is when the <param name="copyAllocator"></param> passed in is Allocator.None. In this scenario,
/// ownership of the data remains with the caller and the reader will point at it directly.
/// When created with Allocator.None, FastBufferReader will allocate some internal data using
/// Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive
/// Allocator.Temp so it should be treated as if it's a ref struct and not allowed to outlive
/// the context in which it was created (it should neither be returned from that function nor
/// stored anywhere in heap memory).
/// stored anywhere in heap memory). This is true, unless the <param name="internalAllocator"></param> param is explicitly set
/// to i.e.: Allocator.Persistent in which case it would allow the internal data to Persist for longer, but the caller
/// should manually call Dispose() when it is no longer needed.
/// </summary>
/// <param name="buffer"></param>
/// <param name="allocator"></param>
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
/// <param name="length"></param>
/// <param name="offset"></param>
public unsafe FastBufferReader(NativeArray<byte> buffer, Allocator allocator, int length = -1, int offset = 0)
/// <param name="internalAllocator">The allocator type used for internal data when this reader points directly at a buffer owned by someone else</param>
public unsafe FastBufferReader(NativeArray<byte> buffer, Allocator copyAllocator, int length = -1, int offset = 0, Allocator internalAllocator = Allocator.Temp)
{
Handle = CreateHandle((byte*)buffer.GetUnsafePtr(), length == -1 ? buffer.Length : length, offset, allocator);
Handle = CreateHandle((byte*)buffer.GetUnsafePtr(), length == -1 ? buffer.Length : length, offset, copyAllocator, internalAllocator);
}
/// <summary>
@@ -112,18 +125,18 @@ namespace Unity.Netcode
/// and ensure the FastBufferReader isn't used outside that block.
/// </summary>
/// <param name="buffer">The buffer to copy from</param>
/// <param name="allocator">The allocator to use</param>
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
/// <param name="offset">The offset of the buffer to start copying from</param>
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator allocator, int length = -1, int offset = 0)
public unsafe FastBufferReader(ArraySegment<byte> buffer, Allocator copyAllocator, int length = -1, int offset = 0)
{
if (allocator == Allocator.None)
if (copyAllocator == Allocator.None)
{
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
}
fixed (byte* data = buffer.Array)
{
Handle = CreateHandle(data, length == -1 ? buffer.Count : length, offset, allocator);
Handle = CreateHandle(data, length == -1 ? buffer.Count : length, offset, copyAllocator, Allocator.Temp);
}
}
@@ -137,74 +150,80 @@ namespace Unity.Netcode
/// and ensure the FastBufferReader isn't used outside that block.
/// </summary>
/// <param name="buffer">The buffer to copy from</param>
/// <param name="allocator">The allocator to use</param>
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
/// <param name="offset">The offset of the buffer to start copying from</param>
public unsafe FastBufferReader(byte[] buffer, Allocator allocator, int length = -1, int offset = 0)
public unsafe FastBufferReader(byte[] buffer, Allocator copyAllocator, int length = -1, int offset = 0)
{
if (allocator == Allocator.None)
if (copyAllocator == Allocator.None)
{
throw new NotSupportedException("Allocator.None cannot be used with managed source buffers.");
}
fixed (byte* data = buffer)
{
Handle = CreateHandle(data, length == -1 ? buffer.Length : length, offset, allocator);
Handle = CreateHandle(data, length == -1 ? buffer.Length : length, offset, copyAllocator, Allocator.Temp);
}
}
/// <summary>
/// Create a FastBufferReader from an existing byte buffer.
///
/// A new buffer will be created using the given allocator and the value will be copied in.
/// A new buffer will be created using the given <param name="copyAllocator"></param> and the value will be copied in.
/// FastBufferReader will then own the data.
///
/// The exception to this is when the allocator passed in is Allocator.None. In this scenario,
/// The exception to this is when the <param name="copyAllocator"></param> passed in is Allocator.None. In this scenario,
/// ownership of the data remains with the caller and the reader will point at it directly.
/// When created with Allocator.None, FastBufferReader will allocate some internal data using
/// Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive
/// the context in which it was created (it should neither be returned from that function nor
/// stored anywhere in heap memory).
/// stored anywhere in heap memory). This is true, unless the <param name="internalAllocator"></param> param is explicitly set
/// to i.e.: Allocator.Persistent in which case it would allow the internal data to Persist for longer, but the caller
/// should manually call Dispose() when it is no longer needed.
/// </summary>
/// <param name="buffer">The buffer to copy from</param>
/// <param name="allocator">The allocator to use</param>
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
/// <param name="length">The number of bytes to copy</param>
/// <param name="offset">The offset of the buffer to start copying from</param>
public unsafe FastBufferReader(byte* buffer, Allocator allocator, int length, int offset = 0)
/// <param name="internalAllocator">The allocator type used for internal data when this reader points directly at a buffer owned by someone else</param>
public unsafe FastBufferReader(byte* buffer, Allocator copyAllocator, int length, int offset = 0, Allocator internalAllocator = Allocator.Temp)
{
Handle = CreateHandle(buffer, length, offset, allocator);
Handle = CreateHandle(buffer, length, offset, copyAllocator, internalAllocator);
}
/// <summary>
/// Create a FastBufferReader from a FastBufferWriter.
///
/// A new buffer will be created using the given allocator and the value will be copied in.
/// A new buffer will be created using the given <param name="copyAllocator"></param> and the value will be copied in.
/// FastBufferReader will then own the data.
///
/// The exception to this is when the allocator passed in is Allocator.None. In this scenario,
/// The exception to this is when the <param name="copyAllocator"></param> passed in is Allocator.None. In this scenario,
/// ownership of the data remains with the caller and the reader will point at it directly.
/// When created with Allocator.None, FastBufferReader will allocate some internal data using
/// Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive
/// the context in which it was created (it should neither be returned from that function nor
/// stored anywhere in heap memory).
/// stored anywhere in heap memory). This is true, unless the <param name="internalAllocator"></param> param is explicitly set
/// to i.e.: Allocator.Persistent in which case it would allow the internal data to Persist for longer, but the caller
/// should manually call Dispose() when it is no longer needed.
/// </summary>
/// <param name="writer">The writer to copy from</param>
/// <param name="allocator">The allocator to use</param>
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
/// <param name="offset">The offset of the buffer to start copying from</param>
public unsafe FastBufferReader(FastBufferWriter writer, Allocator allocator, int length = -1, int offset = 0)
/// <param name="internalAllocator">The allocator type used for internal data when this reader points directly at a buffer owned by someone else</param>
public unsafe FastBufferReader(FastBufferWriter writer, Allocator copyAllocator, int length = -1, int offset = 0, Allocator internalAllocator = Allocator.Temp)
{
Handle = CreateHandle(writer.GetUnsafePtr(), length == -1 ? writer.Length : length, offset, allocator);
Handle = CreateHandle(writer.GetUnsafePtr(), length == -1 ? writer.Length : length, offset, copyAllocator, internalAllocator);
}
/// <summary>
/// Create a FastBufferReader from another existing FastBufferReader. This is typically used when you
/// want to change the allocator that a reader is allocated to - for example, upgrading a Temp reader to
/// want to change the copyAllocator that a reader is allocated to - for example, upgrading a Temp reader to
/// a Persistent one to be processed later.
///
/// A new buffer will be created using the given allocator and the value will be copied in.
/// A new buffer will be created using the given <param name="copyAllocator"></param> and the value will be copied in.
/// FastBufferReader will then own the data.
///
/// The exception to this is when the allocator passed in is Allocator.None. In this scenario,
/// The exception to this is when the <param name="copyAllocator"></param> passed in is Allocator.None. In this scenario,
/// ownership of the data remains with the caller and the reader will point at it directly.
/// When created with Allocator.None, FastBufferReader will allocate some internal data using
/// Allocator.Temp, so it should be treated as if it's a ref struct and not allowed to outlive
@@ -212,16 +231,17 @@ namespace Unity.Netcode
/// stored anywhere in heap memory).
/// </summary>
/// <param name="reader">The reader to copy from</param>
/// <param name="allocator">The allocator to use</param>
/// <param name="copyAllocator">The allocator type used for internal data when copying an existing buffer if other than Allocator.None is specified, that memory will be owned by this FastBufferReader instance</param>
/// <param name="length">The number of bytes to copy (all if this is -1)</param>
/// <param name="offset">The offset of the buffer to start copying from</param>
public unsafe FastBufferReader(FastBufferReader reader, Allocator allocator, int length = -1, int offset = 0)
/// <param name="internalAllocator">The allocator type used for internal data when this reader points directly at a buffer owned by someone else</param>
public unsafe FastBufferReader(FastBufferReader reader, Allocator copyAllocator, int length = -1, int offset = 0, Allocator internalAllocator = Allocator.Temp)
{
Handle = CreateHandle(reader.GetUnsafePtr(), length == -1 ? reader.Length : length, offset, allocator);
Handle = CreateHandle(reader.GetUnsafePtr(), length == -1 ? reader.Length : length, offset, copyAllocator, internalAllocator);
}
/// <summary>
/// Frees the allocated buffer
/// <see cref="IDisposable"/> implementation that frees the allocated buffer
/// </summary>
public unsafe void Dispose()
{
@@ -321,6 +341,7 @@ namespace Unity.Netcode
/// for performance reasons, since the point of using TryBeginRead is to avoid bounds checking in the following
/// operations in release builds.
/// </summary>
/// <typeparam name="T">the type `T` of the value you are trying to read</typeparam>
/// <param name="value">The value you want to read</param>
/// <returns>True if the read is allowed, false otherwise</returns>
/// <exception cref="InvalidOperationException">If called while in a bitwise context</exception>
@@ -350,7 +371,7 @@ namespace Unity.Netcode
/// Differs from TryBeginRead only in that it won't ever move the AllowedReadMark backward.
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
/// <returns>true upon success</returns>
/// <exception cref="InvalidOperationException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal unsafe bool TryBeginReadInternal(int bytes)
@@ -379,7 +400,7 @@ namespace Unity.Netcode
/// Returns an array representation of the underlying byte buffer.
/// !!Allocates a new array!!
/// </summary>
/// <returns></returns>
/// <returns>byte array</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe byte[] ToArray()
{
@@ -394,7 +415,7 @@ namespace Unity.Netcode
/// <summary>
/// Gets a direct pointer to the underlying buffer
/// </summary>
/// <returns></returns>
/// <returns><see cref="byte"/> pointer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe byte* GetUnsafePtr()
{
@@ -404,7 +425,7 @@ namespace Unity.Netcode
/// <summary>
/// Gets a direct pointer to the underlying buffer at the current read position
/// </summary>
/// <returns></returns>
/// <returns><see cref="byte"/> pointer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe byte* GetUnsafePtrAtCurrentPosition()
{
@@ -414,8 +435,8 @@ namespace Unity.Netcode
/// <summary>
/// Read an INetworkSerializable
/// </summary>
/// <param name="value">INetworkSerializable instance</param>
/// <typeparam name="T"></typeparam>
/// <param name="value">INetworkSerializable instance</param>
/// <exception cref="NotImplementedException"></exception>
public void ReadNetworkSerializable<T>(out T value) where T : INetworkSerializable, new()
{
@@ -428,7 +449,7 @@ namespace Unity.Netcode
/// Read an array of INetworkSerializables
/// </summary>
/// <param name="value">INetworkSerializable instance</param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">the array to read the values of type `T` into</typeparam>
/// <exception cref="NotImplementedException"></exception>
public void ReadNetworkSerializable<T>(out T[] value) where T : INetworkSerializable, new()
{
@@ -523,7 +544,7 @@ namespace Unity.Netcode
/// <param name="value">Value to read</param>
/// <param name="bytesToRead">Number of bytes</param>
/// <param name="offsetBytes">Offset into the value to write the bytes</param>
/// <typeparam name="T"></typeparam>
/// <typeparam name="T">the type value to read the value into</typeparam>
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="OverflowException"></exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -682,7 +703,7 @@ namespace Unity.Netcode
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void ReadUnmanaged<T>(out T value) where T : unmanaged
internal unsafe void ReadUnmanaged<T>(out T value) where T : unmanaged
{
fixed (T* ptr = &value)
{
@@ -691,7 +712,7 @@ namespace Unity.Netcode
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void ReadUnmanagedSafe<T>(out T value) where T : unmanaged
internal unsafe void ReadUnmanagedSafe<T>(out T value) where T : unmanaged
{
fixed (T* ptr = &value)
{
@@ -700,7 +721,7 @@ namespace Unity.Netcode
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void ReadUnmanaged<T>(out T[] value) where T : unmanaged
internal unsafe void ReadUnmanaged<T>(out T[] value) where T : unmanaged
{
ReadUnmanaged(out int sizeInTs);
int sizeInBytes = sizeInTs * sizeof(T);
@@ -712,7 +733,7 @@ namespace Unity.Netcode
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void ReadUnmanagedSafe<T>(out T[] value) where T : unmanaged
internal unsafe void ReadUnmanagedSafe<T>(out T[] value) where T : unmanaged
{
ReadUnmanagedSafe(out int sizeInTs);
int sizeInBytes = sizeInTs * sizeof(T);
@@ -724,112 +745,570 @@ namespace Unity.Netcode
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanaged(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanaged(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanagedSafe(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanagedSafe(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanaged(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanaged(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanagedSafe(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanagedSafe(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanaged(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanaged(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanagedSafe(out value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a NetworkSerializable value
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => ReadNetworkSerializable(out value);
/// <summary>
/// Read a NetworkSerializable array
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => ReadNetworkSerializable(out value);
/// <summary>
/// Read a NetworkSerializable value
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => ReadNetworkSerializable(out value);
/// <summary>
/// Read a NetworkSerializable array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new() => ReadNetworkSerializable(out value);
/// <summary>
/// Read a struct
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanaged(out value);
/// <summary>
/// Read a struct array
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanaged(out value);
/// <summary>
/// Read a struct
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a struct array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanaged(out value);
/// <summary>
/// Read a primitive value array (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanaged(out value);
/// <summary>
/// Read a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => ReadUnmanagedSafe(out value);
/// <summary>
/// Read an enum value
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanaged(out value);
/// <summary>
/// Read an enum array
/// </summary>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue<T>(out T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanaged(out value);
/// <summary>
/// Read an enum value
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanagedSafe(out value);
/// <summary>
/// Read an enum array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe<T>(out T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector2
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector2 value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector2 array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector2[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector3
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector3 value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector3 array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector3[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector2Int
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector2Int value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector2Int array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector2Int[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector3Int
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector3Int value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector3Int array
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector3Int[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector4
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector4 value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector4
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Vector4[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Quaternion
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Quaternion value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Quaternion array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Quaternion[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Color
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Color value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Color array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Color[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Color32
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Color32 value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Color32 array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Color32[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Ray
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Ray value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Ray array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Ray[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Ray2D
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Ray2D value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Ray2D array
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValue(out Ray2D[] value) => ReadUnmanaged(out value);
/// <summary>
/// Read a Vector2
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector2 value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector2 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector2[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector3
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector3 value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector3 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector3[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector2Int
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector2Int value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector2Int array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector2Int[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector3Int
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector3Int value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector3Int array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector3Int[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector4
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector4 value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Vector4 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Vector4[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Quaternion
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Quaternion value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Quaternion array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Quaternion[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Color
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Color value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Collor array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Color[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Color32
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Color32 value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Color32 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Color32[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Ray
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Ray value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Ray array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Ray[] value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Ray2D
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Ray2D value) => ReadUnmanagedSafe(out value);
/// <summary>
/// Read a Ray2D array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the values to read</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReadValueSafe(out Ray2D[] value) => ReadUnmanagedSafe(out value);
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
// INativeList<bool> provides the Length property
// IUTF8Bytes provides GetUnsafePtr()
// Those two are necessary to serialize FixedStrings efficiently
// - otherwise we'd just be memcpying the whole thing even if
// most of it isn't used.
/// <summary>
/// Read a FixedString value.
/// This method is a little difficult to use, since you have to know the size of the string before
/// reading it, but is useful when the string is a known, fixed size. Note that the size of the
/// string is also encoded, so the size to call TryBeginRead on is actually the fixed size (in bytes)
/// plus sizeof(int)
/// </summary>
/// <param name="value">the value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void ReadValue<T>(out T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
ReadUnmanaged(out int length);
value = new T();
value.Length = length;
ReadBytes(value.GetUnsafePtr(), length);
}
/// <summary>
/// Read a FixedString value.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple reads at once by calling TryBeginRead.
/// </summary>
/// <param name="value">the value to read</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void ReadValueSafe<T>(out T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
ReadUnmanagedSafe(out int length);
value = new T();
value.Length = length;
ReadBytesSafe(value.GetUnsafePtr(), length);
}
}
}

View File

@@ -6,6 +6,12 @@ using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// Optimized class used for writing values into a byte stream
/// <seealso cref="FastBufferReader"/>
/// <seealso cref="BytePacker"/>
/// <seealso cref="ByteUnpacker"/>
/// </summary>
public struct FastBufferWriter : IDisposable
{
internal struct WriterHandle
@@ -108,7 +114,7 @@ namespace Unity.Netcode
}
/// <summary>
/// Frees the allocated buffer
/// <see cref="IDisposable"/> implementation that frees the allocated buffer
/// </summary>
public unsafe void Dispose()
{
@@ -267,7 +273,8 @@ namespace Unity.Netcode
/// operations in release builds. Instead, attempting to write past the marked position in release builds
/// will write to random memory and cause undefined behavior, likely including instability and crashes.
/// </summary>
/// <param name="value">The value you want to write</param>
/// <typeparam name="T">The value type to write</typeparam>
/// <param name="value">The value of the type `T` you want to write</param>
/// <returns>True if the write is allowed, false otherwise</returns>
/// <exception cref="InvalidOperationException">If called while in a bitwise context</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -716,15 +723,30 @@ namespace Unity.Netcode
}
/// <summary>
/// Get the size required to write an unmanaged value
/// Get the write size for any general unmanaged value
/// The ForStructs value here makes this the lowest-priority overload so other versions
/// will be prioritized over this if they match
/// </summary>
/// <param name="value"></param>
/// <param name="unused"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int GetWriteSize<T>(in T value, ForStructs unused = default) where T : unmanaged
{
return sizeof(T);
}
/// <summary>
/// Get the write size for a FixedString
/// </summary>
/// <param name="value"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe int GetWriteSize<T>(in T value) where T : unmanaged
public static int GetWriteSize<T>(in T value)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
return sizeof(T);
return value.Length + sizeof(int);
}
/// <summary>
@@ -738,7 +760,7 @@ namespace Unity.Netcode
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteUnmanaged<T>(in T value) where T : unmanaged
internal unsafe void WriteUnmanaged<T>(in T value) where T : unmanaged
{
fixed (T* ptr = &value)
{
@@ -747,7 +769,7 @@ namespace Unity.Netcode
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteUnmanagedSafe<T>(in T value) where T : unmanaged
internal unsafe void WriteUnmanagedSafe<T>(in T value) where T : unmanaged
{
fixed (T* ptr = &value)
{
@@ -757,7 +779,7 @@ namespace Unity.Netcode
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteUnmanaged<T>(T[] value) where T : unmanaged
internal unsafe void WriteUnmanaged<T>(T[] value) where T : unmanaged
{
WriteUnmanaged(value.Length);
fixed (T* ptr = value)
@@ -767,7 +789,7 @@ namespace Unity.Netcode
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe void WriteUnmanagedSafe<T>(T[] value) where T : unmanaged
internal unsafe void WriteUnmanagedSafe<T>(T[] value) where T : unmanaged
{
WriteUnmanagedSafe(value.Length);
fixed (T* ptr = value)
@@ -777,130 +799,641 @@ namespace Unity.Netcode
}
}
// These structs enable overloading of WriteValue with different generic constraints.
// The compiler's actually able to distinguish between overloads based on generic constraints.
// But at the bytecode level, the constraints aren't included in the method signature.
// By adding a second parameter with a defaulted value, the signatures of each generic are different,
// thus allowing overloads of methods based on the first parameter meeting constraints.
/// <summary>
/// This empty struct exists to allow overloading WriteValue based on generic constraints.
/// At the bytecode level, constraints aren't included in the method signature, so if multiple
/// methods exist with the same signature, it causes a compile error because they would end up
/// being emitted as the same method, even if the constraints are different.
/// Adding an empty struct with a default value gives them different signatures in the bytecode,
/// which then allows the compiler to do overload resolution based on the generic constraints
/// without the user having to pass the struct in themselves.
/// </summary>
public struct ForPrimitives
{
}
/// <summary>
/// This empty struct exists to allow overloading WriteValue based on generic constraints.
/// At the bytecode level, constraints aren't included in the method signature, so if multiple
/// methods exist with the same signature, it causes a compile error because they would end up
/// being emitted as the same method, even if the constraints are different.
/// Adding an empty struct with a default value gives them different signatures in the bytecode,
/// which then allows the compiler to do overload resolution based on the generic constraints
/// without the user having to pass the struct in themselves.
/// </summary>
public struct ForEnums
{
}
/// <summary>
/// This empty struct exists to allow overloading WriteValue based on generic constraints.
/// At the bytecode level, constraints aren't included in the method signature, so if multiple
/// methods exist with the same signature, it causes a compile error because they would end up
/// being emitted as the same method, even if the constraints are different.
/// Adding an empty struct with a default value gives them different signatures in the bytecode,
/// which then allows the compiler to do overload resolution based on the generic constraints
/// without the user having to pass the struct in themselves.
/// </summary>
public struct ForStructs
{
}
/// <summary>
/// This empty struct exists to allow overloading WriteValue based on generic constraints.
/// At the bytecode level, constraints aren't included in the method signature, so if multiple
/// methods exist with the same signature, it causes a compile error because they would end up
/// being emitted as the same method, even if the constraints are different.
/// Adding an empty struct with a default value gives them different signatures in the bytecode,
/// which then allows the compiler to do overload resolution based on the generic constraints
/// without the user having to pass the struct in themselves.
/// </summary>
public struct ForNetworkSerializable
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanaged(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanaged(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanagedSafe(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanagedSafe(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanaged(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanaged(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanagedSafe(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanagedSafe(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanaged(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanaged(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanagedSafe(value);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanagedSafe(value);
/// <summary>
/// This empty struct exists to allow overloading WriteValue based on generic constraints.
/// At the bytecode level, constraints aren't included in the method signature, so if multiple
/// methods exist with the same signature, it causes a compile error because they would end up
/// being emitted as the same method, even if the constraints are different.
/// Adding an empty struct with a default value gives them different signatures in the bytecode,
/// which then allows the compiler to do overload resolution based on the generic constraints
/// without the user having to pass the struct in themselves.
/// </summary>
public struct ForFixedStrings
{
}
/// <summary>
/// Write a NetworkSerializable value
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForNetworkSerializable unused = default) where T : INetworkSerializable => WriteNetworkSerializable(value);
/// <summary>
/// Write a NetworkSerializable array
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForNetworkSerializable unused = default) where T : INetworkSerializable => WriteNetworkSerializable(value);
/// <summary>
/// Write a NetworkSerializable value
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForNetworkSerializable unused = default) where T : INetworkSerializable => WriteNetworkSerializable(value);
/// <summary>
/// Write a NetworkSerializable array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForNetworkSerializable unused = default) where T : INetworkSerializable => WriteNetworkSerializable(value);
/// <summary>
/// Write a struct
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanaged(value);
/// <summary>
/// Write a struct array
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanaged(value);
/// <summary>
/// Write a struct
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanagedSafe(value);
/// <summary>
/// Write a struct array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy => WriteUnmanagedSafe(value);
/// <summary>
/// Write a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanaged(value);
/// <summary>
/// Write a primitive value array (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanaged(value);
/// <summary>
/// Write a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanagedSafe(value);
/// <summary>
/// Write a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T> => WriteUnmanagedSafe(value);
/// <summary>
/// Write an enum value
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(in T value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanaged(value);
/// <summary>
/// Write an enum array
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue<T>(T[] value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanaged(value);
/// <summary>
/// Write an enum value
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanagedSafe(value);
/// <summary>
/// Write an enum array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">The values to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(T[] value, ForEnums unused = default) where T : unmanaged, Enum => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector2
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Vector2 value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector2 array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Vector2[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector3
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Vector3 value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector3 array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Vector3[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector2Int
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Vector2Int value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector2Int array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Vector2Int[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector3Int
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Vector3Int value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector3Int array
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Vector3Int[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector4
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Vector4 value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector4
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Vector4[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Quaternion
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Quaternion value) => WriteUnmanaged(value);
/// <summary>
/// Write a Quaternion array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Quaternion[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Color
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Color value) => WriteUnmanaged(value);
/// <summary>
/// Write a Color array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Color[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Color32
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Color32 value) => WriteUnmanaged(value);
/// <summary>
/// Write a Color32 array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Color32[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Ray
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Ray value) => WriteUnmanaged(value);
/// <summary>
/// Write a Ray array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Ray[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Ray2D
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(in Ray2D value) => WriteUnmanaged(value);
/// <summary>
/// Write a Ray2D array
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValue(Ray2D[] value) => WriteUnmanaged(value);
/// <summary>
/// Write a Vector2
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Vector2 value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector2 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Vector2[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector3
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Vector3 value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector3 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Vector3[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector2Int
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Vector2Int value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector2Int array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Vector2Int[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector3Int
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Vector3Int value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector3Int array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Vector3Int[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector4
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Vector4 value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Vector4 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Vector4[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Quaternion
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Quaternion value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Quaternion array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Quaternion[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Color
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Color value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Collor array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Color[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Color32
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Color32 value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Color32 array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Color32[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Ray
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Ray value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Ray array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Ray[] value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Ray2D
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(in Ray2D value) => WriteUnmanagedSafe(value);
/// <summary>
/// Write a Ray2D array
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the values to write</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe(Ray2D[] value) => WriteUnmanagedSafe(value);
// There are many FixedString types, but all of them share the interfaces INativeList<bool> and IUTF8Bytes.
// INativeList<bool> provides the Length property
// IUTF8Bytes provides GetUnsafePtr()
// Those two are necessary to serialize FixedStrings efficiently
// - otherwise we'd just be memcpying the whole thing even if
// most of it isn't used.
/// <summary>
/// Write a FixedString value. Writes only the part of the string that's actually used.
/// When calling TryBeginWrite, ensure you calculate the write size correctly (preferably by calling
/// FastBufferWriter.GetWriteSize())
/// </summary>
/// <param name="value">the value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void WriteValue<T>(in T value, ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
WriteUnmanaged(value.Length);
// This avoids a copy on the string, which could be costly for FixedString4096Bytes
// Otherwise, GetUnsafePtr() is an impure function call and will result in a copy
// for `in` parameters.
fixed (T* ptr = &value)
{
WriteBytes(ptr->GetUnsafePtr(), value.Length);
}
}
/// <summary>
/// Write a FixedString value. Writes only the part of the string that's actually used.
///
/// "Safe" version - automatically performs bounds checking. Less efficient than bounds checking
/// for multiple writes at once by calling TryBeginWrite.
/// </summary>
/// <param name="value">the value to write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void WriteValueSafe<T>(in T value, ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
if (!TryBeginWriteInternal(sizeof(int) + value.Length))
{
throw new OverflowException("Writing past the end of the buffer");
}
WriteValue(value);
}
}
}

View File

@@ -9,26 +9,58 @@ namespace Unity.Netcode
/// <typeparam name="T"></typeparam>
public struct ForceNetworkSerializeByMemcpy<T> : INetworkSerializeByMemcpy, IEquatable<ForceNetworkSerializeByMemcpy<T>> where T : unmanaged, IEquatable<T>
{
/// <summary>
/// The wrapped value
/// </summary>
public T Value;
/// <summary>
/// The default constructor for <see cref="ForceNetworkSerializeByMemcpy{T}"/>
/// </summary>
/// <param name="value">sets the initial value of type `T`</param>
public ForceNetworkSerializeByMemcpy(T value)
{
Value = value;
}
/// <summary>
/// Convert implicitly from the ForceNetworkSerializeByMemcpy wrapper to the underlying value
/// </summary>
/// <param name="container">The wrapper</param>
/// <returns>The underlying value</returns>
public static implicit operator T(ForceNetworkSerializeByMemcpy<T> container) => container.Value;
/// <summary>
/// Convert implicitly from a T value to a ForceNetworkSerializeByMemcpy wrapper
/// </summary>
/// <param name="underlyingValue">the value</param>
/// <returns>a new wrapper</returns>
public static implicit operator ForceNetworkSerializeByMemcpy<T>(T underlyingValue) => new ForceNetworkSerializeByMemcpy<T> { Value = underlyingValue };
/// <summary>
/// Check if wrapped values are equal
/// </summary>
/// <param name="other">Other wrapper</param>
/// <returns>true if equal</returns>
public bool Equals(ForceNetworkSerializeByMemcpy<T> other)
{
return Value.Equals(other.Value);
}
/// <summary>
/// Check if this value is equal to a boxed object value
/// </summary>
/// <param name="obj">The boxed value to check against</param>
/// <returns>true if equal</returns>
public override bool Equals(object obj)
{
return obj is ForceNetworkSerializeByMemcpy<T> other && Equals(other);
}
/// <summary>
/// Obtains the wrapped value's hash code
/// </summary>
/// <returns>Wrapped value's hash code</returns>
public override int GetHashCode()
{
return Value.GetHashCode();

View File

@@ -6,7 +6,7 @@ namespace Unity.Netcode
/// by memcpy. It's up to the developer of the struct to analyze the struct's contents and ensure it
/// is actually serializable by memcpy. This requires all of the members of the struct to be
/// `unmanaged` Plain-Old-Data values - if your struct contains a pointer (or a type that contains a pointer,
/// like `NativeList<T>`), it should be serialized via `INetworkSerializable` or via
/// like `NativeList&lt;T&gt;`), it should be serialized via `INetworkSerializable` or via
/// `FastBufferReader`/`FastBufferWriter` extension methods.
/// </summary>
public interface INetworkSerializeByMemcpy

View File

@@ -1,71 +1,539 @@
using System;
using Unity.Collections;
using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// Interface for an implementation of one side of a two-way serializer
/// </summary>
public interface IReaderWriter
{
/// <summary>
/// Check whether this implementation is a "reader" - if it's been constructed to deserialize data
/// </summary>
bool IsReader { get; }
/// <summary>
/// Check whether this implementation is a "writer" - if it's been constructed to serialize data
/// </summary>
bool IsWriter { get; }
/// <summary>
/// Get the underlying FastBufferReader struct.
/// Only valid when IsReader == true
/// </summary>
/// <returns>underlying FastBufferReader</returns>
FastBufferReader GetFastBufferReader();
/// <summary>
/// Get the underlying FastBufferWriter struct.
/// Only valid when IsWriter == true
/// </summary>
/// <returns>underlying FastBufferWriter</returns>
FastBufferWriter GetFastBufferWriter();
/// <summary>
/// Read or write a string
/// </summary>
/// <param name="s">The value to read/write</param>
/// <param name="oneByteChars">If true, characters will be limited to one-byte ASCII characters</param>
void SerializeValue(ref string s, bool oneByteChars = false);
/// <summary>
/// Read or write a single byte
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref byte value);
/// <summary>
/// Read or write a primitive value (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>;
/// <summary>
/// Read or write an array of primitive values (int, bool, etc)
/// Accepts any value that implements the given interfaces, but is not guaranteed to work correctly
/// on values that are not primitives.
/// </summary>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>;
/// <summary>
/// Read or write an enum value
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum;
/// <summary>
/// Read or write an array of enum values
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum;
/// <summary>
/// Read or write a struct value implementing ISerializeByMemcpy
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy;
/// <summary>
/// Read or write an array of struct values implementing ISerializeByMemcpy
/// </summary>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy;
/// <summary>
/// Read or write a struct or class value implementing INetworkSerializable
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new();
/// <summary>
/// Read or write an array of struct or class values implementing INetworkSerializable
/// </summary>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T[] value, FastBufferWriter.ForNetworkSerializable unused = default) where T : INetworkSerializable, new();
/// <summary>
/// Read or write a FixedString value
/// </summary>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter used for enabling overload resolution based on generic constraints</param>
/// <typeparam name="T">The type being serialized</typeparam>
void SerializeValue<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes;
/// <summary>
/// Read or write a Vector2 value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Vector2 value);
/// <summary>
/// Read or write an array of Vector2 values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Vector2[] value);
/// <summary>
/// Read or write a Vector3 value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Vector3 value);
/// <summary>
/// Read or write an array of Vector3 values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Vector3[] value);
/// <summary>
/// Read or write a Vector2Int value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Vector2Int value);
/// <summary>
/// Read or write an array of Vector2Int values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Vector2Int[] value);
/// <summary>
/// Read or write a Vector3Int value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Vector3Int value);
/// <summary>
/// Read or write an array of Vector3Int values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Vector3Int[] value);
/// <summary>
/// Read or write a Vector4 value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Vector4 value);
/// <summary>
/// Read or write an array of Vector4 values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Vector4[] value);
/// <summary>
/// Read or write a Quaternion value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Quaternion value);
/// <summary>
/// Read or write an array of Quaternion values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Quaternion[] value);
/// <summary>
/// Read or write a Color value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Color value);
/// <summary>
/// Read or write an array of Color values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Color[] value);
/// <summary>
/// Read or write a Color32 value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Color32 value);
/// <summary>
/// Read or write an array of Color32 values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Color32[] value);
/// <summary>
/// Read or write a Ray value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Ray value);
/// <summary>
/// Read or write an array of Ray values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Ray[] value);
/// <summary>
/// Read or write a Ray2D value
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValue(ref Ray2D value);
/// <summary>
/// Read or write an array of Ray2D values
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValue(ref Ray2D[] value);
// Has to have a different name to avoid conflicting with "where T: unmananged"
/// <summary>
/// Read or write a NetworkSerializable value.
/// SerializeValue() is the preferred method to do this - this is provided for backward compatibility only.
/// </summary>
/// <param name="value">The value to read/write</param>
/// <typeparam name="T">The network serializable type</typeparam>
void SerializeNetworkSerializable<T>(ref T value) where T : INetworkSerializable, new();
/// <summary>
/// Performs an advance check to ensure space is available to read/write one or more values.
/// This provides a performance benefit for serializing multiple values using the
/// SerializeValuePreChecked methods. But note that the benefit is small and only likely to be
/// noticeable if serializing a very large number of items.
/// </summary>
/// <param name="amount"></param>
/// <returns></returns>
bool PreCheck(int amount);
/// <summary>
/// Serialize a string, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="s">The value to read/write</param>
/// <param name="oneByteChars">If true, characters will be limited to one-byte ASCII characters</param>
void SerializeValuePreChecked(ref string s, bool oneByteChars = false);
/// <summary>
/// Serialize a byte, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref byte value);
/// <summary>
/// Serialize a primitive, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>;
/// <summary>
/// Serialize an array of primitives, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForPrimitives unused = default) where T : unmanaged, IComparable, IConvertible, IComparable<T>, IEquatable<T>;
/// <summary>
/// Serialize an enum, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum;
/// <summary>
/// Serialize an array of enums, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForEnums unused = default) where T : unmanaged, Enum;
/// <summary>
/// Serialize a struct, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy;
/// <summary>
/// Serialize an array of structs, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The values to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T[] value, FastBufferWriter.ForStructs unused = default) where T : unmanaged, INetworkSerializeByMemcpy;
/// <summary>
/// Serialize a FixedString, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <typeparam name="T">The type being serialized</typeparam>
/// <param name="value">The value to read/write</param>
/// <param name="unused">An unused parameter that can be used for enabling overload resolution based on generic constraints</param>
void SerializeValuePreChecked<T>(ref T value, FastBufferWriter.ForFixedStrings unused = default)
where T : unmanaged, INativeList<byte>, IUTF8Bytes;
/// <summary>
/// Serialize a Vector2, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector2 value);
/// <summary>
/// Serialize a Vector2 array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValuePreChecked(ref Vector2[] value);
/// <summary>
/// Serialize a Vector3, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector3 value);
/// <summary>
/// Serialize a Vector3 array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValuePreChecked(ref Vector3[] value);
/// <summary>
/// Serialize a Vector2Int, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector2Int value);
/// <summary>
/// Serialize a Vector2Int array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The values to read/write</param>
void SerializeValuePreChecked(ref Vector2Int[] value);
/// <summary>
/// Serialize a Vector3Int, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector3Int value);
/// <summary>
/// Serialize a Vector3Int array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector3Int[] value);
/// <summary>
/// Serialize a Vector4, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector4 value);
/// <summary>
/// Serialize a Vector4Array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Vector4[] value);
/// <summary>
/// Serialize a Quaternion, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Quaternion value);
/// <summary>
/// Serialize a Quaternion array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Quaternion[] value);
/// <summary>
/// Serialize a Color, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Color value);
/// <summary>
/// Serialize a Color array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Color[] value);
/// <summary>
/// Serialize a Color32, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Color32 value);
/// <summary>
/// Serialize a Color32 array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Color32[] value);
/// <summary>
/// Serialize a Ray, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Ray value);
/// <summary>
/// Serialize a Ray array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Ray[] value);
/// <summary>
/// Serialize a Ray2D, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Ray2D value);
/// <summary>
/// Serialize a Ray2D array, "pre-checked", which skips buffer checks.
/// In debug and editor builds, a check is made to ensure you've called "PreCheck" before
/// calling this. In release builds, calling this without calling "PreCheck" may read or write
/// past the end of the buffer, which will cause memory corruption and undefined behavior.
/// </summary>
/// <param name="value">The value to read/write</param>
void SerializeValuePreChecked(ref Ray2D[] value);
}
}

View File

@@ -53,7 +53,7 @@ namespace Unity.Netcode
/// <returns>True if the <see cref="NetworkBehaviour"/> was found; False if the <see cref="NetworkBehaviour"/> was not found. This can happen if the corresponding <see cref="NetworkObject"/> has not been spawned yet. you can try getting the reference at a later point in time.</returns>
public bool TryGet<T>(out T networkBehaviour, NetworkManager networkManager = null) where T : NetworkBehaviour
{
networkBehaviour = (T)GetInternal(this, null);
networkBehaviour = GetInternal(this, null) as T;
return networkBehaviour != null;
}
@@ -96,8 +96,18 @@ namespace Unity.Netcode
serializer.SerializeValue(ref m_NetworkBehaviourId);
}
/// <summary>
/// Implicitly convert <see cref="NetworkBehaviourReference"/> to <see cref="NetworkBehaviour"/>.
/// </summary>
/// <param name="networkBehaviourRef">The <see cref="NetworkBehaviourReference"/> to convert from.</param>
/// <returns>The <see cref="NetworkBehaviour"/> this class is holding a reference to</returns>
public static implicit operator NetworkBehaviour(NetworkBehaviourReference networkBehaviourRef) => GetInternal(networkBehaviourRef);
/// <summary>
/// Implicitly convert <see cref="NetworkBehaviour"/> to <see cref="NetworkBehaviourReference"/>.
/// </summary>
/// <param name="networkBehaviour">The <see cref="NetworkBehaviour"/> to convert from.</param>
/// <returns>The <see cref="NetworkBehaviourReference"/> created from the <see cref="NetworkBehaviour"/> passed in as a parameter</returns>
public static implicit operator NetworkBehaviourReference(NetworkBehaviour networkBehaviour) => new NetworkBehaviourReference(networkBehaviour);
}
}

View File

@@ -120,12 +120,32 @@ namespace Unity.Netcode
serializer.SerializeValue(ref m_NetworkObjectId);
}
/// <summary>
/// Implicitly convert <see cref="NetworkObjectReference"/> to <see cref="NetworkObject"/>.
/// </summary>
/// <param name="networkObjectRef">The <see cref="NetworkObjectReference"/> to convert from.</param>
/// <returns>The <see cref="NetworkObject"/> the <see cref="NetworkObjectReference"/> is referencing</returns>
public static implicit operator NetworkObject(NetworkObjectReference networkObjectRef) => Resolve(networkObjectRef);
/// <summary>
/// Implicitly convert <see cref="NetworkObject"/> to <see cref="NetworkObjectReference"/>.
/// </summary>
/// <param name="networkObject">The <see cref="NetworkObject"/> to convert from.</param>
/// <returns>The <see cref="NetworkObjectReference"/> created from the <see cref="NetworkObject"/> parameter</returns>
public static implicit operator NetworkObjectReference(NetworkObject networkObject) => new NetworkObjectReference(networkObject);
/// <summary>
/// Implicitly convert <see cref="NetworkObjectReference"/> to <see cref="GameObject"/>.
/// </summary>
/// <param name="networkObjectRef">The <see cref="NetworkObjectReference"/> to convert from.</param>
/// <returns>This returns the <see cref="GameObject"/> that the <see cref="NetworkObject"/> is attached to and is referenced by the <see cref="NetworkObjectReference"/> passed in as a parameter</returns>
public static implicit operator GameObject(NetworkObjectReference networkObjectRef) => Resolve(networkObjectRef).gameObject;
/// <summary>
/// Implicitly convert <see cref="GameObject"/> to <see cref="NetworkObject"/>.
/// </summary>
/// <param name="gameObject">The <see cref="GameObject"/> to convert from.</param>
/// <returns>The <see cref="NetworkObjectReference"/> created from the <see cref="GameObject"/> parameter that has a <see cref="NetworkObject"/> component attached to it</returns>
public static implicit operator NetworkObjectReference(GameObject gameObject) => new NetworkObjectReference(gameObject);
}
}

View File

@@ -126,6 +126,7 @@ namespace Unity.Netcode
/// Returns a list of all NetworkObjects that belong to a client.
/// </summary>
/// <param name="clientId">the client's id <see cref="NetworkManager.LocalClientId"/></param>
/// <returns>returns the list of <see cref="NetworkObject"/>s owned by the client</returns>
public List<NetworkObject> GetClientOwnedObjects(ulong clientId)
{
if (!OwnershipToObjectsTable.ContainsKey(clientId))
@@ -172,9 +173,11 @@ namespace Unity.Netcode
return GetPlayerNetworkObject(NetworkManager.LocalClientId);
}
/// <summary>
/// Returns the player object with a given clientId or null if one does not exist. This is only valid server side.
/// </summary>
/// <param name="clientId">the client identifier of the player</param>
/// <returns>The player object with a given clientId or null if one does not exist</returns>
public NetworkObject GetPlayerNetworkObject(ulong clientId)
{
@@ -283,15 +286,15 @@ namespace Unity.Netcode
}
}
internal bool HasPrefab(bool isSceneObject, uint globalObjectIdHash)
internal bool HasPrefab(NetworkObject.SceneObject sceneObject)
{
if (!NetworkManager.NetworkConfig.EnableSceneManagement || !isSceneObject)
if (!NetworkManager.NetworkConfig.EnableSceneManagement || !sceneObject.Header.IsSceneObject)
{
if (NetworkManager.PrefabHandler.ContainsHandler(globalObjectIdHash))
if (NetworkManager.PrefabHandler.ContainsHandler(sceneObject.Header.Hash))
{
return true;
}
if (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(globalObjectIdHash, out var networkPrefab))
if (NetworkManager.NetworkConfig.NetworkPrefabOverrideLinks.TryGetValue(sceneObject.Header.Hash, out var networkPrefab))
{
switch (networkPrefab.Override)
{
@@ -306,14 +309,14 @@ namespace Unity.Netcode
return false;
}
var networkObject = NetworkManager.SceneManager.GetSceneRelativeInSceneNetworkObject(globalObjectIdHash);
var networkObject = NetworkManager.SceneManager.GetSceneRelativeInSceneNetworkObject(sceneObject.Header.Hash, sceneObject.NetworkSceneHandle);
return networkObject != null;
}
/// <summary>
/// Should only run on the client
/// </summary>
internal NetworkObject CreateLocalNetworkObject(bool isSceneObject, uint globalObjectIdHash, ulong ownerClientId, ulong? parentNetworkId, Vector3? position, Quaternion? rotation, bool isReparented = false)
internal NetworkObject CreateLocalNetworkObject(bool isSceneObject, uint globalObjectIdHash, ulong ownerClientId, ulong? parentNetworkId, int? networkSceneHandle, Vector3? position, Quaternion? rotation, bool isReparented = false)
{
NetworkObject parentNetworkObject = null;
@@ -404,7 +407,7 @@ namespace Unity.Netcode
}
else
{
var networkObject = NetworkManager.SceneManager.GetSceneRelativeInSceneNetworkObject(globalObjectIdHash);
var networkObject = NetworkManager.SceneManager.GetSceneRelativeInSceneNetworkObject(globalObjectIdHash, networkSceneHandle);
if (networkObject == null)
{
@@ -477,15 +480,23 @@ namespace Unity.Netcode
return;
}
// this initialization really should be at the bottom of the function
networkObject.IsSpawned = true;
// this initialization really should be at the top of this function. If and when we break the
// NetworkVariable dependency on NetworkBehaviour, this otherwise creates problems because
// SetNetworkVariableData above calls InitializeVariables, and the 'baked out' data isn't ready there;
// the current design banks on getting the network behaviour set and then only reading from it after the
// below initialization code. However cowardice compels me to hold off on moving this until that commit
networkObject.IsSceneObject = sceneObject;
// Always check to make sure our scene of origin is properly set for in-scene placed NetworkObjects
// Note: Always check SceneOriginHandle directly at this specific location.
if (networkObject.IsSceneObject != false && networkObject.SceneOriginHandle == 0)
{
networkObject.SceneOrigin = networkObject.gameObject.scene;
}
// For integration testing, this makes sure that the appropriate NetworkManager is assigned to
// the NetworkObject since it uses the NetworkManager.Singleton when not set
if (networkObject.NetworkManagerOwner != NetworkManager)
{
networkObject.NetworkManagerOwner = NetworkManager;
}
networkObject.NetworkObjectId = networkId;
networkObject.DestroyWithScene = sceneObject || destroyWithScene;
@@ -552,9 +563,8 @@ namespace Unity.Netcode
internal void SendSpawnCallForObject(ulong clientId, NetworkObject networkObject)
{
//Currently, if this is called and the clientId (destination) is the server's client Id, this case will be checked
// within the below Send function. To avoid unwarranted allocation of a PooledNetworkBuffer placing this check here. [NSS]
if (NetworkManager.IsServer && clientId == NetworkManager.ServerClientId)
// If we are a host and sending to the host's client id, then we can skip sending ourselves the spawn message.
if (clientId == NetworkManager.ServerClientId)
{
return;
}
@@ -785,7 +795,8 @@ namespace Unity.Netcode
var message = new DestroyObjectMessage
{
NetworkObjectId = networkObject.NetworkObjectId
NetworkObjectId = networkObject.NetworkObjectId,
DestroyGameObject = networkObject.IsSceneObject != false ? destroyGameObject : true
};
var size = NetworkManager.SendMessage(ref message, NetworkDelivery.ReliableSequenced, m_TargetClientIds);
foreach (var targetClientId in m_TargetClientIds)
@@ -803,6 +814,9 @@ namespace Unity.Netcode
SpawnedObjectsList.Remove(networkObject);
}
// Always clear out the observers list when despawned
networkObject.Observers.Clear();
var gobj = networkObject.gameObject;
if (destroyGameObject && gobj != null)
{

View File

@@ -3,6 +3,10 @@ using Unity.Profiling;
namespace Unity.Netcode
{
/// <summary>
/// Provides discretized time.
/// This is useful for games that require ticks happening at regular interval on the server and clients.
/// </summary>
public class NetworkTickSystem
{
#if DEVELOPMENT_BUILD || UNITY_EDITOR
@@ -69,6 +73,8 @@ namespace Unity.Netcode
/// <summary>
/// Called after advancing the time system to run ticks based on the difference in time.
/// </summary>
/// <param name="localTimeSec">The local time in seconds</param>
/// <param name="serverTimeSec">The server time in seconds</param>
public void UpdateTick(double localTimeSec, double serverTimeSec)
{
// store old local tick to know how many fixed ticks passed

View File

@@ -108,6 +108,11 @@ namespace Unity.Netcode
return new NetworkTime(m_TickRate, m_CachedTick);
}
/// <summary>
/// Returns the time a number of ticks in the past.
/// </summary>
/// <param name="ticks">The number of ticks ago we're querying the time</param>
/// <returns></returns>
public NetworkTime TimeTicksAgo(int ticks)
{
return this - new NetworkTime(TickRate, ticks);
@@ -132,16 +137,34 @@ namespace Unity.Netcode
}
}
/// <summary>
/// Computes the time difference between two ticks
/// </summary>
/// <param name="a">End time</param>
/// <param name="b">Start time</param>
/// <returns>The time difference between start and end</returns>
public static NetworkTime operator -(NetworkTime a, NetworkTime b)
{
return new NetworkTime(a.TickRate, a.Time - b.Time);
}
/// <summary>
/// Computes the sum of two times
/// </summary>
/// <param name="a">First time</param>
/// <param name="b">Second time</param>
/// <returns>The sum of the two times passed in</returns>
public static NetworkTime operator +(NetworkTime a, NetworkTime b)
{
return new NetworkTime(a.TickRate, a.Time + b.Time);
}
/// <summary>
/// Computes the time a number of seconds later
/// </summary>
/// <param name="a">The start time</param>
/// <param name="b">The number of seconds to add</param>
/// <returns>The resulting time</returns>
public static NetworkTime operator +(NetworkTime a, double b)
{
a.m_TimeSec += b;
@@ -149,6 +172,12 @@ namespace Unity.Netcode
return a;
}
/// <summary>
/// Computes the time a number of seconds before
/// </summary>
/// <param name="a">The start time</param>
/// <param name="b">The number of seconds to remove</param>
/// <returns>The resulting time</returns>
public static NetworkTime operator -(NetworkTime a, double b)
{
return a + -b;

View File

@@ -36,12 +36,27 @@ namespace Unity.Netcode
/// Gets or sets the ratio at which the NetworkTimeSystem speeds up or slows down time.
/// </summary>
public double AdjustmentRatio { get; set; }
/// <summary>
/// The current local time with the local time offset applied
/// </summary>
public double LocalTime => m_TimeSec + m_CurrentLocalTimeOffset;
/// <summary>
/// The current server time with the server time offset applied
/// </summary>
public double ServerTime => m_TimeSec + m_CurrentServerTimeOffset;
internal double LastSyncedServerTimeSec { get; private set; }
internal double LastSyncedRttSec { get; private set; }
/// <summary>
/// The constructor class for <see cref="NetworkTickSystem"/>
/// </summary>
/// <param name="localBufferSec">The amount of time, in seconds, the server should buffer incoming client messages.</param>
/// <param name="serverBufferSec">The amount of the time in seconds the client should buffer incoming messages from the server.</param>
/// <param name="hardResetThresholdSec">The threshold, in seconds, used to force a hard catchup of network time.</param>
/// <param name="adjustmentRatio">The ratio at which the NetworkTimeSystem speeds up or slows down time.</param>
public NetworkTimeSystem(double localBufferSec, double serverBufferSec, double hardResetThresholdSec, double adjustmentRatio = 0.01d)
{
LocalBufferSec = localBufferSec;

View File

@@ -20,6 +20,11 @@ namespace Unity.Netcode
/// </summary>
Disconnect,
/// <summary>
/// Transport has encountered an unrecoverable failure
/// </summary>
TransportFailure,
/// <summary>
/// No new event
/// </summary>

View File

@@ -3,6 +3,11 @@ using UnityEngine;
namespace Unity.Netcode
{
/// <summary>
/// The generic transport class all Netcode for GameObjects network transport implementations
/// derive from. Use this class to add a custom transport.
/// <seealso cref="Transports.UTP.UnityTransport"> for an example of how a transport is integrated</seealso>
/// </summary>
public abstract class NetworkTransport : MonoBehaviour
{
/// <summary>
@@ -45,7 +50,7 @@ namespace Unity.Netcode
}
/// <summary>
/// Send a payload to the specified clientId, data and channelName.
/// Send a payload to the specified clientId, data and networkDelivery.
/// </summary>
/// <param name="clientId">The clientId to send to</param>
/// <param name="payload">The data to send</param>
@@ -64,11 +69,13 @@ namespace Unity.Netcode
/// <summary>
/// Connects client to the server
/// </summary>
/// <returns>Returns success or failure</returns>
public abstract bool StartClient();
/// <summary>
/// Starts to listening for incoming clients
/// </summary>
/// <returns>Returns success or failure</returns>
public abstract bool StartServer();
/// <summary>

View File

@@ -1,8 +1,17 @@
namespace Unity.Netcode.Transports.UTP
{
/// <summary>
/// Caching structure to track network metrics related information.
/// </summary>
public struct NetworkMetricsContext
{
/// <summary>
/// The number of packet sent.
/// </summary>
public uint PacketSentCount;
/// <summary>
/// The number of packet received.
/// </summary>
public uint PacketReceivedCount;
}
}

View File

@@ -16,6 +16,14 @@ namespace Unity.Netcode.Transports.UTP
/// </summary>
public interface INetworkStreamDriverConstructor
{
/// <summary>
/// Creates the internal NetworkDriver
/// </summary>
/// <param name="transport">The owner transport</param>
/// <param name="driver">The driver</param>
/// <param name="unreliableFragmentedPipeline">The UnreliableFragmented NetworkPipeline</param>
/// <param name="unreliableSequencedFragmentedPipeline">The UnreliableSequencedFragmented NetworkPipeline</param>
/// <param name="reliableSequencedPipeline">The ReliableSequenced NetworkPipeline</param>
void CreateDriver(
UnityTransport transport,
out NetworkDriver driver,
@@ -24,6 +32,9 @@ namespace Unity.Netcode.Transports.UTP
out NetworkPipeline reliableSequencedPipeline);
}
/// <summary>
/// Helper utility class to convert <see cref="Networking.Transport"/> error codes to human readable error messages.
/// </summary>
public static class ErrorUtilities
{
private const string k_NetworkSuccess = "Success";
@@ -37,6 +48,12 @@ namespace Unity.Netcode.Transports.UTP
private const string k_NetworkSendHandleInvalid = "Invalid NetworkInterface Send Handle. Likely caused by pipeline send data corruption.";
private const string k_NetworkArgumentMismatch = "Invalid NetworkEndpoint Arguments.";
/// <summary>
/// Convert error code to human readable error message.
/// </summary>
/// <param name="error">Status code of the error</param>
/// <param name="connectionId">Subject connection ID of the error</param>
/// <returns>Human readable error message.</returns>
public static string ErrorToString(Networking.Transport.Error.StatusCode error, ulong connectionId)
{
switch (error)
@@ -67,11 +84,24 @@ namespace Unity.Netcode.Transports.UTP
}
}
/// <summary>
/// The Netcode for GameObjects NetworkTransport for UnityTransport.
/// Note: This is highly recommended to use over UNet.
/// </summary>
public partial class UnityTransport : NetworkTransport, INetworkStreamDriverConstructor
{
/// <summary>
/// Enum type stating the type of protocol
/// </summary>
public enum ProtocolType
{
/// <summary>
/// Unity Transport Protocol
/// </summary>
UnityTransport,
/// <summary>
/// Unity Transport Protocol over Relay
/// </summary>
RelayUnityTransport,
}
@@ -82,15 +112,34 @@ namespace Unity.Netcode.Transports.UTP
Connected,
}
/// <summary>
/// The default maximum (receive) packet queue size
/// </summary>
public const int InitialMaxPacketQueueSize = 128;
/// <summary>
/// The default maximum payload size
/// </summary>
public const int InitialMaxPayloadSize = 6 * 1024;
/// <summary>
/// The default maximum send queue size
/// </summary>
public const int InitialMaxSendQueueSize = 16 * InitialMaxPayloadSize;
private static ConnectionAddressData s_DefaultConnectionAddressData = new ConnectionAddressData { Address = "127.0.0.1", Port = 7777, ServerListenAddress = string.Empty };
#pragma warning disable IDE1006 // Naming Styles
/// <summary>
/// The global <see cref="INetworkStreamDriverConstructor"/> implementation
/// </summary>
public static INetworkStreamDriverConstructor s_DriverConstructor;
#pragma warning restore IDE1006 // Naming Styles
/// <summary>
/// Returns either the global <see cref="INetworkStreamDriverConstructor"/> implementation or the current <see cref="UnityTransport"/> instance
/// </summary>
public INetworkStreamDriverConstructor DriverConstructor => s_DriverConstructor ?? this;
[Tooltip("Which protocol should be selected (Relay/Non-Relay).")]
@@ -187,17 +236,29 @@ namespace Unity.Netcode.Transports.UTP
set => m_DisconnectTimeoutMS = value;
}
/// <summary>
/// Structure to store the address to connect to
/// </summary>
[Serializable]
public struct ConnectionAddressData
{
/// <summary>
/// IP address of the server (address to which clients will connect to).
/// </summary>
[Tooltip("IP address of the server (address to which clients will connect to).")]
[SerializeField]
public string Address;
/// <summary>
/// UDP port of the server.
/// </summary>
[Tooltip("UDP port of the server.")]
[SerializeField]
public ushort Port;
/// <summary>
/// IP address the server will listen on. If not provided, will use 'Address'.
/// </summary>
[Tooltip("IP address the server will listen on. If not provided, will use 'Address'.")]
[SerializeField]
public string ServerListenAddress;
@@ -213,29 +274,58 @@ namespace Unity.Netcode.Transports.UTP
return endpoint;
}
/// <summary>
/// Endpoint (IP address and port) clients will connect to.
/// </summary>
public NetworkEndPoint ServerEndPoint => ParseNetworkEndpoint(Address, Port);
/// <summary>
/// Endpoint (IP address and port) server will listen/bind on.
/// </summary>
public NetworkEndPoint ListenEndPoint => ParseNetworkEndpoint((ServerListenAddress == string.Empty) ? Address : ServerListenAddress, Port);
}
/// <summary>
/// The connection (address) data for this <see cref="UnityTransport"/> instance.
/// This is where you can change IP Address, Port, or server's listen address.
/// <see cref="ConnectionAddressData"/>
/// </summary>
public ConnectionAddressData ConnectionData = s_DefaultConnectionAddressData;
/// <summary>
/// Parameters for the Network Simulator
/// </summary>
[Serializable]
public struct SimulatorParameters
{
/// <summary>
/// Delay to add to every send and received packet (in milliseconds). Only applies in the editor and in development builds. The value is ignored in production builds.
/// </summary>
[Tooltip("Delay to add to every send and received packet (in milliseconds). Only applies in the editor and in development builds. The value is ignored in production builds.")]
[SerializeField]
public int PacketDelayMS;
/// <summary>
/// Jitter (random variation) to add/substract to the packet delay (in milliseconds). Only applies in the editor and in development builds. The value is ignored in production builds.
/// </summary>
[Tooltip("Jitter (random variation) to add/substract to the packet delay (in milliseconds). Only applies in the editor and in development builds. The value is ignored in production builds.")]
[SerializeField]
public int PacketJitterMS;
/// <summary>
/// Percentage of sent and received packets to drop. Only applies in the editor and in the editor and in developments builds.
/// </summary>
[Tooltip("Percentage of sent and received packets to drop. Only applies in the editor and in the editor and in developments builds.")]
[SerializeField]
public int PacketDropRate;
}
/// <summary>
/// Can be used to simulate poor network conditions such as:
/// - packet delay/latency
/// - packet jitter (variances in latency, see: https://en.wikipedia.org/wiki/Jitter)
/// - packet drop rate (packet loss)
/// </summary>
public SimulatorParameters DebugSimulator = new SimulatorParameters
{
PacketDelayMS = 0,
@@ -243,6 +333,15 @@ namespace Unity.Netcode.Transports.UTP
PacketDropRate = 0
};
private struct PacketLossCache
{
public int PacketsReceived;
public int PacketsDropped;
public float PacketLoss;
};
private PacketLossCache m_PacketLossCache = new PacketLossCache();
private State m_State = State.Disconnected;
private NetworkDriver m_Driver;
private NetworkSettings m_NetworkSettings;
@@ -252,8 +351,14 @@ namespace Unity.Netcode.Transports.UTP
private NetworkPipeline m_UnreliableSequencedFragmentedPipeline;
private NetworkPipeline m_ReliableSequencedPipeline;
/// <summary>
/// The client id used to represent the server.
/// </summary>
public override ulong ServerClientId => m_ServerClientId;
/// <summary>
/// The current ProtocolType used by the transport
/// </summary>
public ProtocolType Protocol => m_ProtocolType;
private RelayServerData m_RelayServerData;
@@ -419,6 +524,14 @@ namespace Unity.Netcode.Transports.UTP
m_ProtocolType = inProtocol;
}
/// <summary>Set the relay server data for the server.</summary>
/// <param name="ipv4Address">IP address of the relay server.</param>
/// <param name="port">UDP port of the relay server.</param>
/// <param name="allocationIdBytes">Allocation ID as a byte array.</param>
/// <param name="keyBytes">Allocation key as a byte array.</param>
/// <param name="connectionDataBytes">Connection data as a byte array.</param>
/// <param name="hostConnectionDataBytes">The HostConnectionData as a byte array.</param>
/// <param name="isSecure">Whether the connection is secure (uses DTLS).</param>
public void SetRelayServerData(string ipv4Address, ushort port, byte[] allocationIdBytes, byte[] keyBytes, byte[] connectionDataBytes, byte[] hostConnectionDataBytes = null, bool isSecure = false)
{
RelayConnectionData hostConnectionData;
@@ -480,6 +593,9 @@ namespace Unity.Netcode.Transports.UTP
/// <summary>
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
/// </summary>
/// <param name="ipv4Address">The remote IP address</param>
/// <param name="port">The remote port</param>
/// <param name="listenAddress">The local listen address</param>
public void SetConnectionData(string ipv4Address, ushort port, string listenAddress = null)
{
ConnectionData = new ConnectionAddressData
@@ -495,6 +611,8 @@ namespace Unity.Netcode.Transports.UTP
/// <summary>
/// Sets IP and Port information. This will be ignored if using the Unity Relay and you should call <see cref="SetRelayServerData"/>
/// </summary>
/// <param name="endPoint">The remote end point</param>
/// <param name="listenEndPoint">The local listen endpoint</param>
public void SetConnectionData(NetworkEndPoint endPoint, NetworkEndPoint listenEndPoint = default)
{
string serverAddress = endPoint.Address.Split(':')[0];
@@ -713,6 +831,15 @@ namespace Unity.Netcode.Transports.UTP
m_Driver.ScheduleUpdate().Complete();
if (m_ProtocolType == ProtocolType.RelayUnityTransport && m_Driver.GetRelayConnectionStatus() == RelayConnectionStatus.AllocationInvalid)
{
Debug.LogError("Transport failure! Relay allocation needs to be recreated, and NetworkManager restarted. " +
"Use NetworkManager.OnTransportFailure to be notified of such events programmatically.");
InvokeOnTransportEvent(NetcodeNetworkEvent.TransportFailure, 0, default, Time.realtimeSinceStartup);
return;
}
while (AcceptConnection() && m_Driver.IsCreated)
{
;
@@ -839,11 +966,22 @@ namespace Unity.Netcode.Transports.UTP
{
var sharedContext = (ReliableUtility.SharedContext*)sharedBuffer.GetUnsafePtr();
var packetReceived = (float)sharedContext->stats.PacketsReceived;
var packetDropped = (float)sharedContext->stats.PacketsDropped;
var packetLoss = packetReceived > 0 ? packetDropped / packetReceived : 0;
var packetReceivedDelta = (float)(sharedContext->stats.PacketsReceived - m_PacketLossCache.PacketsReceived);
var packetDroppedDelta = (float)(sharedContext->stats.PacketsDropped - m_PacketLossCache.PacketsDropped);
return packetLoss;
// There can be multiple update happening in a single frame where no packets have transitioned
// In those situation we want to return the last packet loss value instead of 0 to avoid invalid swings
if (packetDroppedDelta == 0 && packetReceivedDelta == 0)
{
return m_PacketLossCache.PacketLoss;
}
m_PacketLossCache.PacketsReceived = sharedContext->stats.PacketsReceived;
m_PacketLossCache.PacketsDropped = sharedContext->stats.PacketsDropped;
m_PacketLossCache.PacketLoss = packetReceivedDelta > 0 ? packetDroppedDelta / packetReceivedDelta : 0;
return m_PacketLossCache.PacketLoss;
}
}
@@ -887,6 +1025,9 @@ namespace Unity.Netcode.Transports.UTP
}
}
/// <summary>
/// Disconnects the local client from the remote
/// </summary>
public override void DisconnectLocalClient()
{
if (m_State == State.Connected)
@@ -911,6 +1052,10 @@ namespace Unity.Netcode.Transports.UTP
}
}
/// <summary>
/// Disconnects a remote client from the server
/// </summary>
/// <param name="clientId">The client to disconnect</param>
public override void DisconnectRemoteClient(ulong clientId)
{
Debug.Assert(m_State == State.Listening, "DisconnectRemoteClient should be called on a listening server");
@@ -930,6 +1075,11 @@ namespace Unity.Netcode.Transports.UTP
}
}
/// <summary>
/// Gets the current RTT for a specific client
/// </summary>
/// <param name="clientId">The client RTT to get</param>
/// <returns>The RTT</returns>
public override ulong GetCurrentRtt(ulong clientId)
{
// We don't know if this is getting called from inside NGO (which presumably knows to
@@ -950,6 +1100,10 @@ namespace Unity.Netcode.Transports.UTP
return (ulong)ExtractRtt(ParseClientId(clientId));
}
/// <summary>
/// Initializes the transport
/// </summary>
/// <param name="networkManager">The NetworkManager that initialized and owns the transport</param>
public override void Initialize(NetworkManager networkManager = null)
{
Debug.Assert(sizeof(ulong) == UnsafeUtility.SizeOf<NetworkConnection>(), "Netcode connection id size does not match UTP connection id size");
@@ -971,6 +1125,13 @@ namespace Unity.Netcode.Transports.UTP
#endif
}
/// <summary>
/// Polls for incoming events, with an extra output parameter to report the precise time the event was received.
/// </summary>
/// <param name="clientId">The clientId this event is for</param>
/// <param name="payload">The incoming data payload</param>
/// <param name="receiveTime">The time the event was received, as reported by Time.realtimeSinceStartup.</param>
/// <returns>Returns the event type</returns>
public override NetcodeNetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
{
clientId = default;
@@ -979,6 +1140,12 @@ namespace Unity.Netcode.Transports.UTP
return NetcodeNetworkEvent.Nothing;
}
/// <summary>
/// Send a payload to the specified clientId, data and networkDelivery.
/// </summary>
/// <param name="clientId">The clientId to send to</param>
/// <param name="payload">The data to send</param>
/// <param name="networkDelivery">The delivery type (QoS) to send data with</param>
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
{
if (payload.Count > m_MaxPayloadSize)
@@ -1040,6 +1207,14 @@ namespace Unity.Netcode.Transports.UTP
}
}
/// <summary>
/// Connects client to the server
/// Note:
/// When this method returns false it could mean:
/// - You are trying to start a client that is already started
/// - It failed during the initial port binding when attempting to begin to connect
/// </summary>
/// <returns>true if the client was started and false if it failed to start the client</returns>
public override bool StartClient()
{
if (m_Driver.IsCreated)
@@ -1055,6 +1230,14 @@ namespace Unity.Netcode.Transports.UTP
return succeeded;
}
/// <summary>
/// Starts to listening for incoming clients
/// Note:
/// When this method returns false it could mean:
/// - You are trying to start a client that is already started
/// - It failed during the initial port binding when attempting to begin to connect
/// </summary>
/// <returns>true if the server was started and false if it failed to start the server</returns>
public override bool StartServer()
{
if (m_Driver.IsCreated)
@@ -1084,6 +1267,9 @@ namespace Unity.Netcode.Transports.UTP
}
}
/// <summary>
/// Shuts down the transport
/// </summary>
public override void Shutdown()
{
if (!m_Driver.IsCreated)
@@ -1106,6 +1292,8 @@ namespace Unity.Netcode.Transports.UTP
DisposeInternals();
m_ReliableReceiveQueues.Clear();
// We must reset this to zero because UTP actually re-uses clientIds if there is a clean disconnect
m_ServerClientId = 0;
}
@@ -1121,6 +1309,14 @@ namespace Unity.Netcode.Transports.UTP
);
}
/// <summary>
/// Creates the internal NetworkDriver
/// </summary>
/// <param name="transport">The owner transport</param>
/// <param name="driver">The driver</param>
/// <param name="unreliableFragmentedPipeline">The UnreliableFragmented NetworkPipeline</param>
/// <param name="unreliableSequencedFragmentedPipeline">The UnreliableSequencedFragmented NetworkPipeline</param>
/// <param name="reliableSequencedPipeline">The ReliableSequenced NetworkPipeline</param>
public void CreateDriver(UnityTransport transport, out NetworkDriver driver,
out NetworkPipeline unreliableFragmentedPipeline,
out NetworkPipeline unreliableSequencedFragmentedPipeline,

View File

@@ -1,4 +1,4 @@
using System;
namespace Unity.Netcode.TestHelpers.Runtime
{
public class ObjectNameIdentifier : NetworkBehaviour
@@ -7,36 +7,46 @@ namespace Unity.Netcode.TestHelpers.Runtime
private ulong m_CurrentNetworkObjectId;
private bool m_IsRegistered;
private const char k_TagInfoStart = '{';
private const char k_TagInfoStop = '}';
/// <summary>
/// Keep a reference to the assigned NetworkObject
/// <see cref="OnDestroy"/>
/// </summary>
[NonSerialized]
private NetworkObject m_NetworkObject;
private string m_OriginalName;
public override void OnNetworkSpawn()
{
RegisterAndLabelNetworkObject();
}
protected void RegisterAndLabelNetworkObject()
{
if (!m_IsRegistered)
{
if (string.IsNullOrEmpty(m_OriginalName))
{
m_OriginalName = gameObject.name.Replace("(Clone)", "");
}
// This is required otherwise it will try to continue to update the NetworkBehaviour even if
// it has been destroyed.
m_NetworkObject = NetworkObject;
m_CurrentOwner = OwnerClientId;
m_CurrentNetworkObjectId = NetworkObjectId;
var objectOriginalName = gameObject.name.Replace("(Clone)", "");
var serverOrClient = IsServer ? "Server" : "Client";
if (NetworkObject.IsPlayerObject)
{
gameObject.name = NetworkManager.LocalClientId == OwnerClientId ? $"{objectOriginalName}({OwnerClientId})-Local{objectOriginalName}" :
$"{objectOriginalName}({OwnerClientId})-On{serverOrClient}({NetworkManager.LocalClientId})";
gameObject.name = NetworkManager.LocalClientId == OwnerClientId ? $"{m_OriginalName}-{k_TagInfoStart}{OwnerClientId}{k_TagInfoStop}-Local{m_OriginalName}" :
$"{m_OriginalName}-{k_TagInfoStart}{OwnerClientId}{k_TagInfoStop}- On{serverOrClient}{k_TagInfoStart}{NetworkManager.LocalClientId}{k_TagInfoStop}";
}
else
{
gameObject.name = $"{objectOriginalName}({NetworkObjectId})-On{serverOrClient}({NetworkManager.LocalClientId})";
gameObject.name = $"{m_OriginalName}{k_TagInfoStart}{NetworkObjectId}{k_TagInfoStop}-On{serverOrClient}{k_TagInfoStart}{NetworkManager.LocalClientId}{k_TagInfoStop}";
}
// Don't add the player objects to the global list of NetworkObjects

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
@@ -9,22 +10,51 @@ namespace Unity.Netcode.TestHelpers.Runtime
{
/// <summary>
/// The default SceneManagerHandler used for all NetcodeIntegrationTest derived children.
/// This enables clients to load scenes within the same scene hierarchy during integration
/// testing.
/// </summary>
internal class IntegrationTestSceneHandler : ISceneManagerHandler, IDisposable
{
internal CoroutineRunner CoroutineRunner;
// All IntegrationTestSceneHandler instances register their associated NetworkManager
internal static List<NetworkManager> NetworkManagers = new List<NetworkManager>();
// Default client simulated delay time
protected const float k_ClientLoadingSimulatedDelay = 0.02f;
internal static CoroutineRunner CoroutineRunner;
internal static Queue<QueuedSceneJob> QueuedSceneJobs = new Queue<QueuedSceneJob>();
internal List<Coroutine> CoroutinesRunning = new List<Coroutine>();
internal static Coroutine SceneJobProcessor;
internal static QueuedSceneJob CurrentQueuedSceneJob;
protected static WaitForSeconds s_WaitForSeconds;
// Controls the client simulated delay time
protected float m_ClientLoadingSimulatedDelay = k_ClientLoadingSimulatedDelay;
public delegate bool CanClientsLoadUnloadDelegateHandler();
public event CanClientsLoadUnloadDelegateHandler CanClientsLoad;
public event CanClientsLoadUnloadDelegateHandler CanClientsUnload;
public static event CanClientsLoadUnloadDelegateHandler CanClientsLoad;
public static event CanClientsLoadUnloadDelegateHandler CanClientsUnload;
internal List<Coroutine> CoroutinesRunning = new List<Coroutine>();
public static bool VerboseDebugMode;
/// <summary>
/// Used for loading scenes on the client-side during
/// an integration test
/// </summary>
internal class QueuedSceneJob
{
public enum JobTypes
{
Loading,
Unloading,
Completed
}
public JobTypes JobType;
public string SceneName;
public Scene Scene;
public ISceneManagerHandler.SceneEventAction SceneAction;
public IntegrationTestSceneHandler IntegrationTestSceneHandler;
}
internal NetworkManager NetworkManager;
internal string NetworkManagerName;
/// <summary>
/// Used to control when clients should attempt to fake-load a scene
@@ -44,19 +74,6 @@ namespace Unity.Netcode.TestHelpers.Runtime
return true;
}
/// <summary>
/// Fake-Loads a scene for a client
/// </summary>
internal IEnumerator ClientLoadSceneCoroutine(string sceneName, ISceneManagerHandler.SceneEventAction sceneEventAction)
{
yield return new WaitForSeconds(m_ClientLoadingSimulatedDelay);
while (!OnCanClientsLoad())
{
yield return new WaitForSeconds(m_ClientLoadingSimulatedDelay);
}
sceneEventAction.Invoke();
}
protected bool OnCanClientsUnload()
{
if (CanClientsUnload != null)
@@ -66,35 +83,298 @@ namespace Unity.Netcode.TestHelpers.Runtime
return true;
}
/// <summary>
/// Fake-Unloads a scene for a client
/// </summary>
internal IEnumerator ClientUnloadSceneCoroutine(ISceneManagerHandler.SceneEventAction sceneEventAction)
internal static void VerboseDebug(string message)
{
yield return new WaitForSeconds(m_ClientLoadingSimulatedDelay);
while (!OnCanClientsUnload())
if (VerboseDebugMode)
{
yield return new WaitForSeconds(m_ClientLoadingSimulatedDelay);
Debug.Log(message);
}
sceneEventAction.Invoke();
}
/// <summary>
/// Processes scene loading jobs
/// </summary>
/// <param name="queuedSceneJob">job to process</param>
static internal IEnumerator ProcessLoadingSceneJob(QueuedSceneJob queuedSceneJob)
{
var itegrationTestSceneHandler = queuedSceneJob.IntegrationTestSceneHandler;
while (!itegrationTestSceneHandler.OnCanClientsLoad())
{
yield return s_WaitForSeconds;
}
SceneManager.sceneLoaded += SceneManager_sceneLoaded;
// We always load additively for all scenes during integration tests
SceneManager.LoadSceneAsync(queuedSceneJob.SceneName, LoadSceneMode.Additive);
// Wait for it to finish
while (queuedSceneJob.JobType != QueuedSceneJob.JobTypes.Completed)
{
yield return s_WaitForSeconds;
}
yield return s_WaitForSeconds;
CurrentQueuedSceneJob.SceneAction.Invoke();
}
/// <summary>
/// Handles scene loading and assists with making sure the right NetworkManagerOwner
/// is assigned to newly instantiated NetworkObjects.
///
/// Note: Static property usage is OK since jobs are processed one at a time
/// </summary>
private static void SceneManager_sceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
if (CurrentQueuedSceneJob.JobType != QueuedSceneJob.JobTypes.Completed && CurrentQueuedSceneJob.SceneName == scene.name)
{
SceneManager.sceneLoaded -= SceneManager_sceneLoaded;
ProcessInSceneObjects(scene, CurrentQueuedSceneJob.IntegrationTestSceneHandler.NetworkManager);
CurrentQueuedSceneJob.JobType = QueuedSceneJob.JobTypes.Completed;
}
}
/// <summary>
/// Handles some pre-spawn processing of in-scene placed NetworkObjects
/// to make sure the appropriate NetworkManagerOwner is assigned. It
/// also makes sure that each in-scene placed NetworkObject has an
/// ObjectIdentifier component if one is not assigned to it or its
/// children.
/// </summary>
/// <param name="scene">the scenes that was just loaded</param>
/// <param name="networkManager">the relative NetworkManager</param>
private static void ProcessInSceneObjects(Scene scene, NetworkManager networkManager)
{
// Get all in-scene placed NeworkObjects that were instantiated when this scene loaded
var inSceneNetworkObjects = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsSceneObject != false && c.GetSceneOriginHandle() == scene.handle);
foreach (var sobj in inSceneNetworkObjects)
{
if (sobj.NetworkManagerOwner != networkManager)
{
sobj.NetworkManagerOwner = networkManager;
}
if (sobj.GetComponent<ObjectNameIdentifier>() == null && sobj.GetComponentInChildren<ObjectNameIdentifier>() == null)
{
sobj.gameObject.AddComponent<ObjectNameIdentifier>();
}
}
}
/// <summary>
/// Processes scene unloading jobs
/// </summary>
/// <param name="queuedSceneJob">job to process</param>
static internal IEnumerator ProcessUnloadingSceneJob(QueuedSceneJob queuedSceneJob)
{
var itegrationTestSceneHandler = queuedSceneJob.IntegrationTestSceneHandler;
while (!itegrationTestSceneHandler.OnCanClientsUnload())
{
yield return s_WaitForSeconds;
}
SceneManager.sceneUnloaded += SceneManager_sceneUnloaded;
if (queuedSceneJob.Scene.IsValid() && queuedSceneJob.Scene.isLoaded && !queuedSceneJob.Scene.name.Contains(NetcodeIntegrationTestHelpers.FirstPartOfTestRunnerSceneName))
{
SceneManager.UnloadSceneAsync(queuedSceneJob.Scene);
}
else
{
CurrentQueuedSceneJob.JobType = QueuedSceneJob.JobTypes.Completed;
}
// Wait for it to finish
while (queuedSceneJob.JobType != QueuedSceneJob.JobTypes.Completed)
{
yield return s_WaitForSeconds;
}
CurrentQueuedSceneJob.SceneAction.Invoke();
}
/// <summary>
/// Handles closing out scene unloading jobs
/// </summary>
private static void SceneManager_sceneUnloaded(Scene scene)
{
if (CurrentQueuedSceneJob.JobType != QueuedSceneJob.JobTypes.Completed && CurrentQueuedSceneJob.Scene.name == scene.name)
{
SceneManager.sceneUnloaded -= SceneManager_sceneUnloaded;
CurrentQueuedSceneJob.JobType = QueuedSceneJob.JobTypes.Completed;
}
}
/// <summary>
/// Processes all jobs within the queue.
/// When all jobs are finished, the coroutine stops.
/// </summary>
static internal IEnumerator JobQueueProcessor()
{
while (QueuedSceneJobs.Count != 0)
{
CurrentQueuedSceneJob = QueuedSceneJobs.Dequeue();
VerboseDebug($"[ITSH-START] {CurrentQueuedSceneJob.IntegrationTestSceneHandler.NetworkManagerName} processing {CurrentQueuedSceneJob.JobType} for scene {CurrentQueuedSceneJob.SceneName}.");
if (CurrentQueuedSceneJob.JobType == QueuedSceneJob.JobTypes.Loading)
{
yield return ProcessLoadingSceneJob(CurrentQueuedSceneJob);
}
else if (CurrentQueuedSceneJob.JobType == QueuedSceneJob.JobTypes.Unloading)
{
yield return ProcessUnloadingSceneJob(CurrentQueuedSceneJob);
}
VerboseDebug($"[ITSH-STOP] {CurrentQueuedSceneJob.IntegrationTestSceneHandler.NetworkManagerName} processing {CurrentQueuedSceneJob.JobType} for scene {CurrentQueuedSceneJob.SceneName}.");
}
SceneJobProcessor = null;
yield break;
}
/// <summary>
/// Adds a job to the job queue, and if the JobQueueProcessor coroutine
/// is not running then it will be started as well.
/// </summary>
/// <param name="queuedSceneJob">job to add to the queue</param>
private void AddJobToQueue(QueuedSceneJob queuedSceneJob)
{
QueuedSceneJobs.Enqueue(queuedSceneJob);
if (SceneJobProcessor == null)
{
SceneJobProcessor = CoroutineRunner.StartCoroutine(JobQueueProcessor());
}
}
private string m_ServerSceneBeingLoaded;
/// <summary>
/// Server always loads like it normally would
/// </summary>
public AsyncOperation GenericLoadSceneAsync(string sceneName, LoadSceneMode loadSceneMode, ISceneManagerHandler.SceneEventAction sceneEventAction)
{
m_ServerSceneBeingLoaded = sceneName;
if (NetcodeIntegrationTest.IsRunning)
{
SceneManager.sceneLoaded += Sever_SceneLoaded;
}
var operation = SceneManager.LoadSceneAsync(sceneName, loadSceneMode);
operation.completed += new Action<AsyncOperation>(asyncOp2 => { sceneEventAction.Invoke(); });
return operation;
}
private void Sever_SceneLoaded(Scene scene, LoadSceneMode arg1)
{
if (m_ServerSceneBeingLoaded == scene.name)
{
ProcessInSceneObjects(scene, NetworkManager);
SceneManager.sceneLoaded -= Sever_SceneLoaded;
}
}
/// <summary>
/// Server always unloads like it normally would
/// </summary>
public AsyncOperation GenericUnloadSceneAsync(Scene scene, ISceneManagerHandler.SceneEventAction sceneEventAction)
{
var operation = SceneManager.UnloadSceneAsync(scene);
operation.completed += new Action<AsyncOperation>(asyncOp2 => { sceneEventAction.Invoke(); });
return operation;
}
public AsyncOperation LoadSceneAsync(string sceneName, LoadSceneMode loadSceneMode, ISceneManagerHandler.SceneEventAction sceneEventAction)
{
CoroutinesRunning.Add(CoroutineRunner.StartCoroutine(ClientLoadSceneCoroutine(sceneName, sceneEventAction)));
// This is OK to return a "nothing" AsyncOperation since we are simulating client loading
return new AsyncOperation();
// Server and non NetcodeIntegrationTest tests use the generic load scene method
if (!NetcodeIntegrationTest.IsRunning)
{
return GenericLoadSceneAsync(sceneName, loadSceneMode, sceneEventAction);
}
else // NetcodeIntegrationTest Clients always get added to the jobs queue
{
AddJobToQueue(new QueuedSceneJob() { IntegrationTestSceneHandler = this, SceneName = sceneName, SceneAction = sceneEventAction, JobType = QueuedSceneJob.JobTypes.Loading });
}
return null;
}
public AsyncOperation UnloadSceneAsync(Scene scene, ISceneManagerHandler.SceneEventAction sceneEventAction)
{
CoroutinesRunning.Add(CoroutineRunner.StartCoroutine(ClientUnloadSceneCoroutine(sceneEventAction)));
// Server and non NetcodeIntegrationTest tests use the generic unload scene method
if (!NetcodeIntegrationTest.IsRunning)
{
return GenericUnloadSceneAsync(scene, sceneEventAction);
}
else // NetcodeIntegrationTest Clients always get added to the jobs queue
{
AddJobToQueue(new QueuedSceneJob() { IntegrationTestSceneHandler = this, Scene = scene, SceneAction = sceneEventAction, JobType = QueuedSceneJob.JobTypes.Unloading });
}
// This is OK to return a "nothing" AsyncOperation since we are simulating client loading
return new AsyncOperation();
return null;
}
public IntegrationTestSceneHandler()
/// <summary>
/// Replacement callback takes other NetworkManagers into consideration
/// </summary>
internal Scene GetAndAddNewlyLoadedSceneByName(string sceneName)
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var sceneLoaded = SceneManager.GetSceneAt(i);
if (sceneLoaded.name == sceneName)
{
var skip = false;
foreach (var networkManager in NetworkManagers)
{
if (NetworkManager.LocalClientId == networkManager.LocalClientId || !networkManager.IsListening)
{
continue;
}
if (networkManager.SceneManager.ScenesLoaded.ContainsKey(sceneLoaded.handle))
{
if (NetworkManager.LogLevel == LogLevel.Developer)
{
NetworkLog.LogInfo($"{NetworkManager.name}'s ScenesLoaded contains {sceneLoaded.name} with a handle of {sceneLoaded.handle}. Skipping over scene.");
}
skip = true;
break;
}
}
if (!skip && !NetworkManager.SceneManager.ScenesLoaded.ContainsKey(sceneLoaded.handle))
{
if (NetworkManager.LogLevel == LogLevel.Developer)
{
NetworkLog.LogInfo($"{NetworkManager.name} adding {sceneLoaded.name} with a handle of {sceneLoaded.handle} to its ScenesLoaded.");
}
NetworkManager.SceneManager.ScenesLoaded.Add(sceneLoaded.handle, sceneLoaded);
return sceneLoaded;
}
}
}
throw new Exception($"Failed to find any loaded scene named {sceneName}!");
}
private bool ExcludeSceneFromSynchronizationCheck(Scene scene)
{
if (!NetworkManager.SceneManager.ScenesLoaded.ContainsKey(scene.handle) && SceneManager.GetActiveScene().handle != scene.handle)
{
return false;
}
return true;
}
/// <summary>
/// Constructor now must take NetworkManager
/// </summary>
public IntegrationTestSceneHandler(NetworkManager networkManager)
{
networkManager.SceneManager.OverrideGetAndAddNewlyLoadedSceneByName = GetAndAddNewlyLoadedSceneByName;
networkManager.SceneManager.ExcludeSceneFromSychronization = ExcludeSceneFromSynchronizationCheck;
NetworkManagers.Add(networkManager);
NetworkManagerName = networkManager.name;
if (s_WaitForSeconds == null)
{
s_WaitForSeconds = new WaitForSeconds(1.0f / networkManager.NetworkConfig.TickRate);
}
NetworkManager = networkManager;
if (CoroutineRunner == null)
{
CoroutineRunner = new GameObject("UnitTestSceneHandlerCoroutine").AddComponent<CoroutineRunner>();
@@ -103,12 +383,29 @@ namespace Unity.Netcode.TestHelpers.Runtime
public void Dispose()
{
foreach (var coroutine in CoroutinesRunning)
NetworkManagers.Clear();
if (SceneJobProcessor != null)
{
CoroutineRunner.StopCoroutine(coroutine);
CoroutineRunner.StopCoroutine(SceneJobProcessor);
SceneJobProcessor = null;
}
CoroutineRunner.StopAllCoroutines();
foreach (var job in QueuedSceneJobs)
{
if (job.JobType != QueuedSceneJob.JobTypes.Completed)
{
if (job.JobType == QueuedSceneJob.JobTypes.Loading)
{
SceneManager.sceneLoaded -= SceneManager_sceneLoaded;
}
else
{
SceneManager.sceneUnloaded -= SceneManager_sceneUnloaded;
}
job.JobType = QueuedSceneJob.JobTypes.Completed;
}
}
QueuedSceneJobs.Clear();
Object.Destroy(CoroutineRunner.gameObject);
}
}

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
using System.Runtime.CompilerServices;
@@ -16,7 +17,12 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
public abstract class NetcodeIntegrationTest
{
protected static TimeoutHelper s_GlobalTimeoutHelper = new TimeoutHelper(4.0f);
/// <summary>
/// Used to determine if a NetcodeIntegrationTest is currently running to
/// determine how clients will load scenes
/// </summary>
internal static bool IsRunning { get; private set; }
protected static TimeoutHelper s_GlobalTimeoutHelper = new TimeoutHelper(8.0f);
protected static WaitForSeconds s_DefaultWaitForTick = new WaitForSeconds(1.0f / k_DefaultTickRate);
/// <summary>
@@ -74,9 +80,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
}
protected int TotalClients => m_UseHost ? NumberOfClients + 1 : NumberOfClients;
protected int TotalClients => m_UseHost ? m_NumberOfClients + 1 : m_NumberOfClients;
protected const uint k_DefaultTickRate = 30;
private int m_NumberOfClients;
protected abstract int NumberOfClients { get; }
/// <summary>
@@ -119,7 +127,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
private NetworkManagerInstatiationMode m_NetworkManagerInstatiationMode;
private bool m_EnableVerboseDebug;
protected bool m_EnableVerboseDebug { get; set; }
/// <summary>
/// Used to display the various integration test
@@ -165,8 +173,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
[OneTimeSetUp]
public void OneTimeSetup()
{
Application.runInBackground = true;
m_NumberOfClients = NumberOfClients;
IsRunning = true;
m_EnableVerboseDebug = OnSetVerboseDebug();
IntegrationTestSceneHandler.VerboseDebugMode = m_EnableVerboseDebug;
VerboseDebug($"Entering {nameof(OneTimeSetup)}");
m_NetworkManagerInstatiationMode = OnSetIntegrationTestMode();
@@ -248,6 +259,61 @@ namespace Unity.Netcode.TestHelpers.Runtime
CreateServerAndClients(NumberOfClients);
}
protected virtual void OnNewClientCreated(NetworkManager networkManager)
{
}
protected virtual void OnNewClientStartedAndConnected(NetworkManager networkManager)
{
}
private void AddRemoveNetworkManager(NetworkManager networkManager, bool addNetworkManager)
{
var clientNetworkManagersList = new List<NetworkManager>(m_ClientNetworkManagers);
if (addNetworkManager)
{
clientNetworkManagersList.Add(networkManager);
}
else
{
clientNetworkManagersList.Remove(networkManager);
}
m_ClientNetworkManagers = clientNetworkManagersList.ToArray();
m_NumberOfClients = clientNetworkManagersList.Count;
}
protected IEnumerator CreateAndStartNewClient()
{
var networkManager = NetcodeIntegrationTestHelpers.CreateNewClient(m_ClientNetworkManagers.Length);
networkManager.NetworkConfig.PlayerPrefab = m_PlayerPrefab;
// Notification that the new client (NetworkManager) has been created
// in the event any modifications need to be made before starting the client
OnNewClientCreated(networkManager);
NetcodeIntegrationTestHelpers.StartOneClient(networkManager);
AddRemoveNetworkManager(networkManager, true);
// Wait for the new client to connect
yield return WaitForClientsConnectedOrTimeOut();
if (s_GlobalTimeoutHelper.TimedOut)
{
AddRemoveNetworkManager(networkManager, false);
Object.Destroy(networkManager.gameObject);
}
AssertOnTimeout($"{nameof(CreateAndStartNewClient)} timed out waiting for the new client to be connected!");
ClientNetworkManagerPostStart(networkManager);
VerboseDebug($"[{networkManager.name}] Created and connected!");
}
protected IEnumerator StopOneClient(NetworkManager networkManager, bool destroy = false)
{
NetcodeIntegrationTestHelpers.StopOneClient(networkManager, destroy);
AddRemoveNetworkManager(networkManager, false);
yield return WaitForConditionOrTimeOut(() => !networkManager.IsConnectedClient);
}
/// <summary>
/// Creates the server and clients
/// </summary>
@@ -315,6 +381,54 @@ namespace Unity.Netcode.TestHelpers.Runtime
yield return null;
}
private void ClientNetworkManagerPostStart(NetworkManager networkManager)
{
networkManager.name = $"NetworkManager - Client - {networkManager.LocalClientId}";
Assert.NotNull(networkManager.LocalClient.PlayerObject, $"{nameof(StartServerAndClients)} detected that client {networkManager.LocalClientId} does not have an assigned player NetworkObject!");
// Get all player instances for the current client NetworkManager instance
var clientPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId);
// Add this player instance to each client player entry
foreach (var playerNetworkObject in clientPlayerClones)
{
// When the server is not the host this needs to be done
if (!m_PlayerNetworkObjects.ContainsKey(playerNetworkObject.NetworkManager.LocalClientId))
{
m_PlayerNetworkObjects.Add(playerNetworkObject.NetworkManager.LocalClientId, new Dictionary<ulong, NetworkObject>());
}
if (!m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].ContainsKey(networkManager.LocalClientId))
{
m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].Add(networkManager.LocalClientId, playerNetworkObject);
}
}
}
protected void ClientNetworkManagerPostStartInit()
{
// Creates a dictionary for all player instances client and server relative
// This provides a simpler way to get a specific player instance relative to a client instance
foreach (var networkManager in m_ClientNetworkManagers)
{
ClientNetworkManagerPostStart(networkManager);
}
if (m_UseHost)
{
var clientSideServerPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.OwnerClientId == m_ServerNetworkManager.LocalClientId);
foreach (var playerNetworkObject in clientSideServerPlayerClones)
{
// When the server is not the host this needs to be done
if (!m_PlayerNetworkObjects.ContainsKey(playerNetworkObject.NetworkManager.LocalClientId))
{
m_PlayerNetworkObjects.Add(playerNetworkObject.NetworkManager.LocalClientId, new Dictionary<ulong, NetworkObject>());
}
if (!m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].ContainsKey(m_ServerNetworkManager.LocalClientId))
{
m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].Add(m_ServerNetworkManager.LocalClientId, playerNetworkObject);
}
}
}
}
/// <summary>
/// This starts the server and clients as long as <see cref="CanStartServerAndClients"/>
/// returns true.
@@ -340,8 +454,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
// Wait for all clients to connect
yield return WaitForClientsConnectedOrTimeOut();
Assert.False(s_GlobalTimeoutHelper.TimedOut, $"{nameof(StartServerAndClients)} timed out waiting for all clients to be connected!");
AssertOnTimeout($"{nameof(StartServerAndClients)} timed out waiting for all clients to be connected!");
if (m_UseHost || m_ServerNetworkManager.IsHost)
{
@@ -357,25 +470,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
}
// Creates a dictionary for all player instances client and server relative
// This provides a simpler way to get a specific player instance relative to a client instance
foreach (var networkManager in m_ClientNetworkManagers)
{
Assert.NotNull(networkManager.LocalClient.PlayerObject, $"{nameof(StartServerAndClients)} detected that client {networkManager.LocalClientId} does not have an assigned player NetworkObject!");
// Get all player instances for the current client NetworkManager instance
var clientPlayerClones = Object.FindObjectsOfType<NetworkObject>().Where((c) => c.IsPlayerObject && c.OwnerClientId == networkManager.LocalClientId);
// Add this player instance to each client player entry
foreach (var playerNetworkObject in clientPlayerClones)
{
// When the server is not the host this needs to be done
if (!m_PlayerNetworkObjects.ContainsKey(playerNetworkObject.NetworkManager.LocalClientId))
{
m_PlayerNetworkObjects.Add(playerNetworkObject.NetworkManager.LocalClientId, new Dictionary<ulong, NetworkObject>());
}
m_PlayerNetworkObjects[playerNetworkObject.NetworkManager.LocalClientId].Add(networkManager.LocalClientId, playerNetworkObject);
}
}
ClientNetworkManagerPostStartInit();
// Notification that at this time the server and client(s) are instantiated,
// started, and connected on both sides.
@@ -409,11 +504,9 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
protected void DeRegisterSceneManagerHandler()
{
if (NetcodeIntegrationTestHelpers.ClientSceneHandler != null)
{
NetcodeIntegrationTestHelpers.ClientSceneHandler.CanClientsLoad -= ClientSceneHandler_CanClientsLoad;
NetcodeIntegrationTestHelpers.ClientSceneHandler.CanClientsUnload -= ClientSceneHandler_CanClientsUnload;
}
IntegrationTestSceneHandler.CanClientsLoad -= ClientSceneHandler_CanClientsLoad;
IntegrationTestSceneHandler.CanClientsUnload -= ClientSceneHandler_CanClientsUnload;
IntegrationTestSceneHandler.NetworkManagers.Clear();
}
/// <summary>
@@ -423,11 +516,9 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
protected void RegisterSceneManagerHandler()
{
if (NetcodeIntegrationTestHelpers.ClientSceneHandler != null)
{
NetcodeIntegrationTestHelpers.ClientSceneHandler.CanClientsLoad += ClientSceneHandler_CanClientsLoad;
NetcodeIntegrationTestHelpers.ClientSceneHandler.CanClientsUnload += ClientSceneHandler_CanClientsUnload;
}
IntegrationTestSceneHandler.CanClientsLoad += ClientSceneHandler_CanClientsLoad;
IntegrationTestSceneHandler.CanClientsUnload += ClientSceneHandler_CanClientsUnload;
NetcodeIntegrationTestHelpers.RegisterSceneManagerHandler(m_ServerNetworkManager, true);
}
private bool ClientSceneHandler_CanClientsUnload()
@@ -440,6 +531,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
return CanClientsLoad();
}
protected bool OnCanSceneCleanUpUnload(Scene scene)
{
return true;
}
/// <summary>
/// This shuts down all NetworkManager instances registered via the
/// <see cref="NetcodeIntegrationTestHelpers"/> class and cleans up
@@ -452,11 +548,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
// Shutdown and clean up both of our NetworkManager instances
try
{
if (NetcodeIntegrationTestHelpers.ClientSceneHandler != null)
{
NetcodeIntegrationTestHelpers.ClientSceneHandler.CanClientsLoad -= ClientSceneHandler_CanClientsLoad;
NetcodeIntegrationTestHelpers.ClientSceneHandler.CanClientsUnload -= ClientSceneHandler_CanClientsUnload;
}
DeRegisterSceneManagerHandler();
NetcodeIntegrationTestHelpers.Destroy();
@@ -476,6 +568,8 @@ namespace Unity.Netcode.TestHelpers.Runtime
// Cleanup any remaining NetworkObjects
DestroySceneNetworkObjects();
UnloadRemainingScenes();
// reset the m_ServerWaitForTick for the next test to initialize
s_DefaultWaitForTick = new WaitForSeconds(1.0f / k_DefaultTickRate);
VerboseDebug($"Exiting {nameof(ShutdownAndCleanUp)}");
@@ -517,6 +611,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
[OneTimeTearDown]
public void OneTimeTearDown()
{
IntegrationTestSceneHandler.VerboseDebugMode = false;
VerboseDebug($"Entering {nameof(OneTimeTearDown)}");
OnOneTimeTearDown();
@@ -528,7 +623,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
// Disable NetcodeIntegrationTest auto-label feature
NetcodeIntegrationTestHelpers.RegisterNetcodeIntegrationTest(false);
UnloadRemainingScenes();
VerboseDebug($"Exiting {nameof(OneTimeTearDown)}");
IsRunning = false;
}
/// <summary>
@@ -783,5 +882,32 @@ namespace Unity.Netcode.TestHelpers.Runtime
{
m_UseHost = hostOrServer == HostOrServer.Host ? true : false;
}
/// <summary>
/// Just a helper function to avoid having to write the entire assert just to check if you
/// timed out.
/// </summary>
protected void AssertOnTimeout(string timeOutErrorMessage, TimeoutHelper assignedTimeoutHelper = null)
{
var timeoutHelper = assignedTimeoutHelper != null ? assignedTimeoutHelper : s_GlobalTimeoutHelper;
Assert.False(timeoutHelper.TimedOut, timeOutErrorMessage);
}
private void UnloadRemainingScenes()
{
// Unload any remaining scenes loaded but the test runner scene
// Note: Some tests only unload the server-side instance, and this
// just assures no currently loaded scenes will impact the next test
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
if (!scene.IsValid() || !scene.isLoaded || scene.name.Contains(NetcodeIntegrationTestHelpers.FirstPartOfTestRunnerSceneName) || !OnCanSceneCleanUpUnload(scene))
{
continue;
}
VerboseDebug($"Unloading scene {scene.name}-{scene.handle}");
var asyncOperation = SceneManager.UnloadSceneAsync(scene);
}
}
}
}

View File

@@ -16,7 +16,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
public static class NetcodeIntegrationTestHelpers
{
public const int DefaultMinFrames = 1;
public const float DefaultTimeout = 1f;
public const float DefaultTimeout = 4f;
private static List<NetworkManager> s_NetworkManagerInstances = new List<NetworkManager>();
private static Dictionary<NetworkManager, MultiInstanceHooks> s_Hooks = new Dictionary<NetworkManager, MultiInstanceHooks>();
private static bool s_IsStarted;
@@ -118,25 +118,23 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
}
private const string k_FirstPartOfTestRunnerSceneName = "InitTestScene";
internal const string FirstPartOfTestRunnerSceneName = "InitTestScene";
public static List<NetworkManager> NetworkManagerInstances => s_NetworkManagerInstances;
internal static IntegrationTestSceneHandler ClientSceneHandler = null;
internal static List<IntegrationTestSceneHandler> ClientSceneHandlers = new List<IntegrationTestSceneHandler>();
/// <summary>
/// Registers the IntegrationTestSceneHandler for integration tests.
/// The default client behavior is to not load scenes on the client side.
/// </summary>
private static void RegisterSceneManagerHandler(NetworkManager networkManager)
internal static void RegisterSceneManagerHandler(NetworkManager networkManager, bool allowServer = false)
{
if (!networkManager.IsServer)
if (!networkManager.IsServer || networkManager.IsServer && allowServer)
{
if (ClientSceneHandler == null)
{
ClientSceneHandler = new IntegrationTestSceneHandler();
}
networkManager.SceneManager.SceneManagerHandler = ClientSceneHandler;
var handler = new IntegrationTestSceneHandler(networkManager);
ClientSceneHandlers.Add(handler);
networkManager.SceneManager.SceneManagerHandler = handler;
}
}
@@ -148,11 +146,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
public static void CleanUpHandlers()
{
if (ClientSceneHandler != null)
foreach (var handler in ClientSceneHandlers)
{
ClientSceneHandler.Dispose();
ClientSceneHandler = null;
handler.Dispose();
}
ClientSceneHandlers.Clear();
}
/// <summary>
@@ -171,17 +169,10 @@ namespace Unity.Netcode.TestHelpers.Runtime
}
}
public static NetworkManager CreateServer()
private static void AddUnityTransport(NetworkManager networkManager)
{
// Create gameObject
var go = new GameObject("NetworkManager - Server");
// Create networkManager component
var server = go.AddComponent<NetworkManager>();
NetworkManagerInstances.Insert(0, server);
// Create transport
var unityTransport = go.AddComponent<UnityTransport>();
var unityTransport = networkManager.gameObject.AddComponent<UnityTransport>();
// We need to increase this buffer size for tests that spawn a bunch of things
unityTransport.MaxPayloadSize = 256000;
unityTransport.MaxSendQueueSize = 1024 * 1024;
@@ -191,11 +182,22 @@ namespace Unity.Netcode.TestHelpers.Runtime
unityTransport.ConnectTimeoutMS = 500;
// Set the NetworkConfig
server.NetworkConfig = new NetworkConfig()
networkManager.NetworkConfig = new NetworkConfig()
{
// Set transport
NetworkTransport = unityTransport
};
}
public static NetworkManager CreateServer()
{
// Create gameObject
var go = new GameObject("NetworkManager - Server");
// Create networkManager component
var server = go.AddComponent<NetworkManager>();
NetworkManagerInstances.Insert(0, server);
AddUnityTransport(server);
return server;
}
@@ -229,6 +231,17 @@ namespace Unity.Netcode.TestHelpers.Runtime
return true;
}
internal static NetworkManager CreateNewClient(int identifier)
{
// Create gameObject
var go = new GameObject("NetworkManager - Client - " + identifier);
// Create networkManager component
var networkManager = go.AddComponent<NetworkManager>();
AddUnityTransport(networkManager);
return networkManager;
}
/// <summary>
/// Used to add a client to the already existing list of clients
/// </summary>
@@ -237,23 +250,10 @@ namespace Unity.Netcode.TestHelpers.Runtime
public static bool CreateNewClients(int clientCount, out NetworkManager[] clients)
{
clients = new NetworkManager[clientCount];
var activeSceneName = SceneManager.GetActiveScene().name;
for (int i = 0; i < clientCount; i++)
{
// Create gameObject
var go = new GameObject("NetworkManager - Client - " + i);
// Create networkManager component
clients[i] = go.AddComponent<NetworkManager>();
// Create transport
var unityTransport = go.AddComponent<UnityTransport>();
// Set the NetworkConfig
clients[i].NetworkConfig = new NetworkConfig()
{
// Set transport
NetworkTransport = unityTransport
};
clients[i] = CreateNewClient(i);
}
NetworkManagerInstances.AddRange(clients);
@@ -264,12 +264,32 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// Stops one single client and makes sure to cleanup any static variables in this helper
/// </summary>
/// <param name="clientToStop"></param>
public static void StopOneClient(NetworkManager clientToStop)
public static void StopOneClient(NetworkManager clientToStop, bool destroy = true)
{
clientToStop.Shutdown();
s_Hooks.Remove(clientToStop);
Object.Destroy(clientToStop.gameObject);
NetworkManagerInstances.Remove(clientToStop);
if (destroy)
{
Object.Destroy(clientToStop.gameObject);
NetworkManagerInstances.Remove(clientToStop);
}
}
/// <summary>
/// Starts one single client and makes sure to register the required hooks and handlers
/// </summary>
/// <param name="clientToStart"></param>
public static void StartOneClient(NetworkManager clientToStart)
{
clientToStart.StartClient();
s_Hooks[clientToStart] = new MultiInstanceHooks();
clientToStart.MessagingSystem.Hook(s_Hooks[clientToStart]);
if (!NetworkManagerInstances.Contains(clientToStart))
{
NetworkManagerInstances.Add(clientToStart);
}
// if set, then invoke this for the client
RegisterHandlers(clientToStart);
}
/// <summary>
@@ -315,7 +335,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
private static bool VerifySceneIsValidForClientsToLoad(int sceneIndex, string sceneName, LoadSceneMode loadSceneMode)
{
// exclude test runner scene
if (sceneName.StartsWith(k_FirstPartOfTestRunnerSceneName))
if (sceneName.StartsWith(FirstPartOfTestRunnerSceneName))
{
return false;
}
@@ -342,7 +362,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
// warning about using the currently active scene
var scene = SceneManager.GetActiveScene();
// As long as this is a test runner scene (or most likely a test runner scene)
if (scene.name.StartsWith(k_FirstPartOfTestRunnerSceneName))
if (scene.name.StartsWith(FirstPartOfTestRunnerSceneName))
{
// Register the test runner scene just so we avoid another warning about not being able to find the
// scene to synchronize NetworkObjects. Next, add the currently active test runner scene to the scenes
@@ -458,8 +478,11 @@ namespace Unity.Netcode.TestHelpers.Runtime
// this feature only works with NetcodeIntegrationTest derived classes
if (IsNetcodeIntegrationTestRunning)
{
// Add the object identifier component
networkObject.gameObject.AddComponent<ObjectNameIdentifier>();
if (networkObject.GetComponent<ObjectNameIdentifier>() == null && networkObject.GetComponentInChildren<ObjectNameIdentifier>() == null)
{
// Add the object identifier component
networkObject.gameObject.AddComponent<ObjectNameIdentifier>();
}
}
}
@@ -722,7 +745,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
/// <param name="result">The result. If null, it will fail if the predicate is not met</param>
/// <param name="timeout">The max time in seconds to wait for</param>
internal static IEnumerator WaitForMessageOfTypeReceived<T>(NetworkManager toBeReceivedBy, ResultWrapper<bool> result = null, float timeout = 0.5f) where T : INetworkMessage
internal static IEnumerator WaitForMessageOfTypeReceived<T>(NetworkManager toBeReceivedBy, ResultWrapper<bool> result = null, float timeout = DefaultTimeout) where T : INetworkMessage
{
var hooks = s_Hooks[toBeReceivedBy];
var check = new MessageReceiveCheckWithResult { CheckType = typeof(T) };
@@ -750,7 +773,7 @@ namespace Unity.Netcode.TestHelpers.Runtime
/// </summary>
/// <param name="result">The result. If null, it will fail if the predicate is not met</param>
/// <param name="timeout">The max time in seconds to wait for</param>
internal static IEnumerator WaitForMessageOfTypeHandled<T>(NetworkManager toBeReceivedBy, ResultWrapper<bool> result = null, float timeout = 0.5f) where T : INetworkMessage
internal static IEnumerator WaitForMessageOfTypeHandled<T>(NetworkManager toBeReceivedBy, ResultWrapper<bool> result = null, float timeout = DefaultTimeout) where T : INetworkMessage
{
var hooks = s_Hooks[toBeReceivedBy];
if (!hooks.HandleChecks.ContainsKey(typeof(T)))

View File

@@ -1,6 +1,8 @@
using NUnit.Framework;
using UnityEngine;
using Unity.Netcode.Editor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace Unity.Netcode.EditorTests
@@ -78,5 +80,37 @@ namespace Unity.Netcode.EditorTests
// Clean up
Object.DestroyImmediate(gameObject);
}
[Test]
public void NestedNetworkObjectPrefabCheck()
{
// Setup
var networkManagerObject = new GameObject(nameof(NestedNetworkObjectPrefabCheck));
var networkManager = networkManagerObject.AddComponent<NetworkManager>();
networkManager.NetworkConfig = new NetworkConfig();
var parent = new GameObject("Parent").AddComponent<NetworkObject>();
var child = new GameObject("Child").AddComponent<NetworkObject>();
// Set parent
child.transform.SetParent(parent.transform);
// Make it a prefab, warning only applies to prefabs
networkManager.AddNetworkPrefab(parent.gameObject);
// Mark scene as dirty to ensure OnValidate actually runs
EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
// Force OnValidate
networkManager.OnValidate();
// Expect a warning
LogAssert.Expect(LogType.Warning, $"[Netcode] {NetworkManager.PrefabDebugHelper(networkManager.NetworkConfig.NetworkPrefabs[0])} has child {nameof(NetworkObject)}(s) but they will not be spawned across the network (unsupported {nameof(NetworkPrefab)} setup)");
// Clean up
Object.DestroyImmediate(networkManagerObject);
Object.DestroyImmediate(parent);
}
}
}

View File

@@ -220,6 +220,14 @@ namespace Unity.Netcode.EditorTests
{
RunTestWithWriteType(new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()), writeType);
}
else if (testType == typeof(Vector2Int))
{
RunTestWithWriteType(new Vector2Int((int)random.NextDouble(), (int)random.NextDouble()), writeType);
}
else if (testType == typeof(Vector3Int))
{
RunTestWithWriteType(new Vector3Int((int)random.NextDouble(), (int)random.NextDouble(), (int)random.NextDouble()), writeType);
}
else if (testType == typeof(Vector4))
{
RunTestWithWriteType(new Vector4((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()), writeType);
@@ -495,6 +503,22 @@ namespace Unity.Netcode.EditorTests
new Vector3((float) random.NextDouble(), (float) random.NextDouble(), (float) random.NextDouble()),
}, writeType);
}
else if (testType == typeof(Vector2Int))
{
RunTypeTestLocal(new[]{
new Vector2Int((int) random.NextDouble(), (int) random.NextDouble()),
new Vector2Int((int) random.NextDouble(), (int) random.NextDouble()),
new Vector2Int((int) random.NextDouble(), (int) random.NextDouble()),
}, writeType);
}
else if (testType == typeof(Vector3Int))
{
RunTypeTestLocal(new[]{
new Vector3Int((int) random.NextDouble(), (int) random.NextDouble(), (int) random.NextDouble()),
new Vector3Int((int) random.NextDouble(), (int) random.NextDouble(), (int) random.NextDouble()),
new Vector3Int((int) random.NextDouble(), (int) random.NextDouble(), (int) random.NextDouble()),
}, writeType);
}
else if (testType == typeof(Vector4))
{
RunTypeTestLocal(new[]{

View File

@@ -351,8 +351,9 @@ namespace Unity.Netcode.EditorTests
[Values(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint),
typeof(long), typeof(ulong), typeof(bool), typeof(char), typeof(float), typeof(double),
typeof(ByteEnum), typeof(SByteEnum), typeof(ShortEnum), typeof(UShortEnum), typeof(IntEnum),
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3), typeof(Vector4),
typeof(Quaternion), typeof(Color), typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3),
typeof(Vector2Int), typeof(Vector3Int), typeof(Vector4), typeof(Quaternion), typeof(Color),
typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
Type testType,
[Values] WriteType writeType)
{
@@ -364,14 +365,156 @@ namespace Unity.Netcode.EditorTests
[Values(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint),
typeof(long), typeof(ulong), typeof(bool), typeof(char), typeof(float), typeof(double),
typeof(ByteEnum), typeof(SByteEnum), typeof(ShortEnum), typeof(UShortEnum), typeof(IntEnum),
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3), typeof(Vector4),
typeof(Quaternion), typeof(Color), typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3),
typeof(Vector2Int), typeof(Vector3Int), typeof(Vector4), typeof(Quaternion), typeof(Color),
typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
Type testType,
[Values] WriteType writeType)
{
BaseArrayTypeTest(testType, writeType);
}
public unsafe void RunFixedStringTest<T>(T fixedStringValue, int numBytesWritten, WriteType writeType) where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
fixedStringValue.Length = numBytesWritten;
var serializedValueSize = FastBufferWriter.GetWriteSize(fixedStringValue);
Assert.AreEqual(serializedValueSize, fixedStringValue.Length + sizeof(int));
var writer = new FastBufferWriter(serializedValueSize + 3, Allocator.Temp);
using (writer)
{
var offset = 0;
switch (writeType)
{
case WriteType.WriteDirect:
Assert.IsTrue(writer.TryBeginWrite(serializedValueSize + 2), "Writer denied write permission");
writer.WriteValue(fixedStringValue);
break;
case WriteType.WriteSafe:
writer.WriteValueSafe(fixedStringValue);
break;
}
WriteCheckBytes(writer, serializedValueSize + offset);
var reader = new FastBufferReader(writer, Allocator.Temp);
using (reader)
{
VerifyPositionAndLength(reader, writer.Length);
var result = new T();
reader.ReadValueSafe(out result);
Assert.AreEqual(fixedStringValue, result);
VerifyCheckBytes(reader, serializedValueSize);
}
}
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
public void WhenReadingFixedString32Bytes_ValueIsReadCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 29; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString32Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
public void WhenReadingFixedString64Bytes_ValueIsReadCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 61; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString64Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
[TestCase(125, WriteType.WriteSafe)]
public void WhenReadingFixedString128Bytes_ValueIsReadCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 125; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString128Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
[TestCase(125, WriteType.WriteSafe)]
[TestCase(509, WriteType.WriteSafe)]
public void WhenReadingFixedString512Bytes_ValueIsReadCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 509; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString512Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
[TestCase(125, WriteType.WriteSafe)]
[TestCase(509, WriteType.WriteSafe)]
[TestCase(4093, WriteType.WriteSafe)]
public void WhenReadingFixedString4096Bytes_ValueIsReadCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 4093; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString4096Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(false, WriteType.WriteDirect)]
[TestCase(false, WriteType.WriteSafe)]
[TestCase(true, WriteType.WriteDirect)]

View File

@@ -257,8 +257,9 @@ namespace Unity.Netcode.EditorTests
[Values(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint),
typeof(long), typeof(ulong), typeof(bool), typeof(char), typeof(float), typeof(double),
typeof(ByteEnum), typeof(SByteEnum), typeof(ShortEnum), typeof(UShortEnum), typeof(IntEnum),
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3), typeof(Vector4),
typeof(Quaternion), typeof(Color), typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3),
typeof(Vector2Int), typeof(Vector3Int), typeof(Vector4), typeof(Quaternion), typeof(Color),
typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
Type testType,
[Values] WriteType writeType)
{
@@ -270,8 +271,9 @@ namespace Unity.Netcode.EditorTests
[Values(typeof(byte), typeof(sbyte), typeof(short), typeof(ushort), typeof(int), typeof(uint),
typeof(long), typeof(ulong), typeof(bool), typeof(char), typeof(float), typeof(double),
typeof(ByteEnum), typeof(SByteEnum), typeof(ShortEnum), typeof(UShortEnum), typeof(IntEnum),
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3), typeof(Vector4),
typeof(Quaternion), typeof(Color), typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
typeof(UIntEnum), typeof(LongEnum), typeof(ULongEnum), typeof(Vector2), typeof(Vector3),
typeof(Vector2Int), typeof(Vector3Int), typeof(Vector4), typeof(Quaternion), typeof(Color),
typeof(Color32), typeof(Ray), typeof(Ray2D), typeof(TestStruct))]
Type testType,
[Values] WriteType writeType)
{
@@ -336,6 +338,147 @@ namespace Unity.Netcode.EditorTests
}
}
public unsafe void RunFixedStringTest<T>(T fixedStringValue, int numBytesWritten, WriteType writeType) where T : unmanaged, INativeList<byte>, IUTF8Bytes
{
fixedStringValue.Length = numBytesWritten;
var serializedValueSize = FastBufferWriter.GetWriteSize(fixedStringValue);
Assert.AreEqual(fixedStringValue.Length + sizeof(int), serializedValueSize);
var writer = new FastBufferWriter(serializedValueSize + 3, Allocator.Temp);
using (writer)
{
var offset = 0;
switch (writeType)
{
case WriteType.WriteDirect:
Assert.IsTrue(writer.TryBeginWrite(serializedValueSize + 2), "Writer denied write permission");
writer.WriteValue(fixedStringValue);
break;
case WriteType.WriteSafe:
writer.WriteValueSafe(fixedStringValue);
break;
}
VerifyPositionAndLength(writer, serializedValueSize + offset);
WriteCheckBytes(writer, serializedValueSize + offset);
int* sizeValue = (int*)(writer.GetUnsafePtr() + offset);
Assert.AreEqual(fixedStringValue.Length, *sizeValue);
byte* underlyingByteArray = writer.GetUnsafePtr() + sizeof(int) + offset;
for (var i = 0; i < fixedStringValue.Length; ++i)
{
Assert.AreEqual(fixedStringValue[i], underlyingByteArray[i]);
}
var underlyingArray = writer.ToArray();
VerifyCheckBytes(underlyingArray, serializedValueSize + offset);
}
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
public void WhenWritingFixedString32Bytes_ValueIsWrittenCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 29; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString32Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
public void WhenWritingFixedString64Bytes_ValueIsWrittenCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 61; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString64Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
[TestCase(125, WriteType.WriteSafe)]
public void WhenWritingFixedString128Bytes_ValueIsWrittenCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 125; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString128Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
[TestCase(125, WriteType.WriteSafe)]
[TestCase(509, WriteType.WriteSafe)]
public void WhenWritingFixedString512Bytes_ValueIsWrittenCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 509; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString512Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(3, WriteType.WriteDirect)]
[TestCase(5, WriteType.WriteSafe)]
[TestCase(16, WriteType.WriteDirect)]
[TestCase(29, WriteType.WriteSafe)]
[TestCase(61, WriteType.WriteSafe)]
[TestCase(125, WriteType.WriteSafe)]
[TestCase(509, WriteType.WriteSafe)]
[TestCase(4093, WriteType.WriteSafe)]
public void WhenWritingFixedString4096Bytes_ValueIsWrittenCorrectly(int numBytesWritten, WriteType writeType)
{
// Repeats 01234567890123456789...
string valueToTest = "";
for (var i = 0; i < 4093; ++i)
{
valueToTest += (i % 10).ToString();
}
var fixedStringValue = new FixedString4096Bytes(valueToTest);
RunFixedStringTest(fixedStringValue, numBytesWritten, writeType);
}
[TestCase(1, 0)]
[TestCase(2, 0)]
[TestCase(3, 0)]

View File

@@ -0,0 +1,136 @@
using System.Collections;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
using Object = UnityEngine.Object;
namespace Unity.Netcode.RuntimeTests
{
public class AddNetworkPrefabTest : NetcodeIntegrationTest
{
public class EmptyComponent : NetworkBehaviour
{
}
protected override int NumberOfClients => 1;
private GameObject m_Prefab;
protected override IEnumerator OnSetup()
{
// Host is irrelevant, messages don't get sent to the host "client"
m_UseHost = false;
m_Prefab = new GameObject("Object");
var networkObject = m_Prefab.AddComponent<NetworkObject>();
m_Prefab.AddComponent<EmptyComponent>();
// Make it a prefab
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObject);
yield return null;
}
protected override void OnServerAndClientsCreated()
{
m_ServerNetworkManager.NetworkConfig.SpawnTimeout = 0;
m_ServerNetworkManager.NetworkConfig.ForceSamePrefabs = false;
foreach (var client in m_ClientNetworkManagers)
{
client.NetworkConfig.SpawnTimeout = 0;
client.NetworkConfig.ForceSamePrefabs = false;
}
}
private EmptyComponent GetObjectForClient(ulong clientId)
{
foreach (var component in Object.FindObjectsOfType<EmptyComponent>())
{
if (component.IsSpawned && component.NetworkManager.LocalClientId == clientId)
{
return component;
}
}
return null;
}
private void RegisterPrefab()
{
m_ServerNetworkManager.AddNetworkPrefab(m_Prefab);
foreach (var client in m_ClientNetworkManagers)
{
client.AddNetworkPrefab(m_Prefab);
}
}
private void DeregisterPrefab()
{
m_ServerNetworkManager.RemoveNetworkPrefab(m_Prefab);
foreach (var client in m_ClientNetworkManagers)
{
client.RemoveNetworkPrefab(m_Prefab);
}
}
private static CoroutineRunner s_CoroutineRunner;
[UnityTest]
public IEnumerator WhenSpawningBeforeAddingPrefab_SpawnFails()
{
var serverObject = Object.Instantiate(m_Prefab);
serverObject.GetComponent<NetworkObject>().NetworkManagerOwner = m_ServerNetworkManager;
serverObject.GetComponent<NetworkObject>().Spawn();
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<CreateObjectMessage>(m_ClientNetworkManagers[0]);
Assert.IsNull(GetObjectForClient(m_ClientNetworkManagers[0].LocalClientId));
}
[UnityTest]
public IEnumerator WhenSpawningAfterAddingServerPrefabButBeforeAddingClientPrefab_SpawnFails()
{
m_ServerNetworkManager.AddNetworkPrefab(m_Prefab);
var serverObject = Object.Instantiate(m_Prefab);
serverObject.GetComponent<NetworkObject>().NetworkManagerOwner = m_ServerNetworkManager;
serverObject.GetComponent<NetworkObject>().Spawn();
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<CreateObjectMessage>(m_ClientNetworkManagers[0]);
Assert.IsNull(GetObjectForClient(m_ClientNetworkManagers[0].LocalClientId));
}
[UnityTest]
public IEnumerator WhenSpawningAfterAddingPrefabOnServerAndClient_SpawnSucceeds()
{
RegisterPrefab();
var serverObject = Object.Instantiate(m_Prefab);
serverObject.GetComponent<NetworkObject>().NetworkManagerOwner = m_ServerNetworkManager;
serverObject.GetComponent<NetworkObject>().Spawn();
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeHandled<CreateObjectMessage>(m_ClientNetworkManagers[0]);
Assert.IsNotNull(GetObjectForClient(m_ClientNetworkManagers[0].LocalClientId));
}
[UnityTest]
public IEnumerator WhenSpawningAfterRemovingPrefabOnClient_SpawnFails()
{
RegisterPrefab();
var serverObject = Object.Instantiate(m_Prefab);
serverObject.GetComponent<NetworkObject>().NetworkManagerOwner = m_ServerNetworkManager;
serverObject.GetComponent<NetworkObject>().Spawn();
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<CreateObjectMessage>(m_ClientNetworkManagers[0]);
Assert.IsNotNull(GetObjectForClient(m_ClientNetworkManagers[0].LocalClientId));
serverObject.GetComponent<NetworkObject>().Despawn();
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<DestroyObjectMessage>(m_ClientNetworkManagers[0]);
Assert.IsNull(GetObjectForClient(m_ClientNetworkManagers[0].LocalClientId));
DeregisterPrefab();
serverObject = Object.Instantiate(m_Prefab);
serverObject.GetComponent<NetworkObject>().NetworkManagerOwner = m_ServerNetworkManager;
serverObject.GetComponent<NetworkObject>().Spawn();
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<CreateObjectMessage>(m_ClientNetworkManagers[0]);
Assert.IsNull(GetObjectForClient(m_ClientNetworkManagers[0].LocalClientId));
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 67012fa25fe64aeda8fbf60340c1bc2f
timeCreated: 1652205947

View File

@@ -24,7 +24,7 @@ namespace Unity.Netcode.RuntimeTests
[UnityTest]
public IEnumerator ConnectionApproval()
{
NetworkManagerHelper.NetworkManagerObject.ConnectionApprovalCallback += NetworkManagerObject_ConnectionApprovalCallback;
NetworkManagerHelper.NetworkManagerObject.ConnectionApprovalCallback = NetworkManagerObject_ConnectionApprovalCallback;
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.ConnectionApproval = true;
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.PlayerPrefab = null;
NetworkManagerHelper.NetworkManagerObject.NetworkConfig.ConnectionData = Encoding.UTF8.GetBytes(m_ValidationToken.ToString());
@@ -47,14 +47,19 @@ namespace Unity.Netcode.RuntimeTests
Assert.True(m_IsValidated);
}
private void NetworkManagerObject_ConnectionApprovalCallback(byte[] connectionData, ulong clientId, NetworkManager.ConnectionApprovedDelegate callback)
private void NetworkManagerObject_ConnectionApprovalCallback(NetworkManager.ConnectionApprovalRequest request, NetworkManager.ConnectionApprovalResponse response)
{
var stringGuid = Encoding.UTF8.GetString(connectionData);
var stringGuid = Encoding.UTF8.GetString(request.Payload);
if (m_ValidationToken.ToString() == stringGuid)
{
m_IsValidated = true;
}
callback(false, null, m_IsValidated, null, null);
response.Approved = m_IsValidated;
response.CreatePlayerObject = false;
response.Position = null;
response.Rotation = null;
response.PlayerPrefabHash = null;
}
[TearDown]

View File

@@ -878,6 +878,7 @@ namespace Unity.Netcode.RuntimeTests
}
[UnityTest]
[Ignore("This test is unstable on standalones")]
public IEnumerator WhenMultipleMessagesForTheSameObjectAreDeferredForMoreThanTheConfiguredTime_TheyAreAllRemoved([Values(1, 2, 3)] int timeout)
{
RegisterClientPrefabs();

View File

@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
@@ -12,6 +13,8 @@ namespace Unity.Netcode.RuntimeTests
public class HiddenVariableObject : NetworkBehaviour
{
public static List<NetworkObject> ClientInstancesSpawned = new List<NetworkObject>();
public NetworkVariable<int> MyNetworkVariable = new NetworkVariable<int>();
public NetworkList<int> MyNetworkList = new NetworkList<int>();
@@ -21,6 +24,10 @@ namespace Unity.Netcode.RuntimeTests
public override void OnNetworkSpawn()
{
if (!IsServer)
{
ClientInstancesSpawned.Add(NetworkObject);
}
Debug.Log($"{nameof(HiddenVariableObject)}.{nameof(OnNetworkSpawn)}() with value {MyNetworkVariable.Value}");
MyNetworkVariable.OnValueChanged += Changed;
@@ -30,6 +37,15 @@ namespace Unity.Netcode.RuntimeTests
base.OnNetworkSpawn();
}
public override void OnNetworkDespawn()
{
if (!IsServer)
{
ClientInstancesSpawned.Remove(NetworkObject);
}
base.OnNetworkDespawn();
}
public void Changed(int before, int after)
{
Debug.Log($"Value changed from {before} to {after} on {NetworkManager.LocalClientId}");
@@ -71,50 +87,75 @@ namespace Unity.Netcode.RuntimeTests
}
}
public void VerifyLists()
public bool VerifyLists()
{
NetworkList<int> prev = null;
int numComparison = 0;
var prevObject = (NetworkObject)null;
// for all the instances of NetworkList
foreach (var gameObject in m_NetSpawnedObjectOnClient)
foreach (var networkObject in m_NetSpawnedObjectOnClient)
{
// this skips despawned/hidden objects
if (gameObject != null)
if (networkObject != null)
{
// if we've seen another one before
if (prev != null)
{
var curr = gameObject.GetComponent<HiddenVariableObject>().MyNetworkList;
var curr = networkObject.GetComponent<HiddenVariableObject>().MyNetworkList;
// check that the two lists are identical
Debug.Assert(curr.Count == prev.Count);
if (curr.Count != prev.Count)
{
return false;
}
for (int index = 0; index < curr.Count; index++)
{
Debug.Assert(curr[index] == prev[index]);
if (curr[index] != prev[index])
{
return false;
}
}
numComparison++;
}
prevObject = networkObject;
// store the list
prev = gameObject.GetComponent<HiddenVariableObject>().MyNetworkList;
prev = networkObject.GetComponent<HiddenVariableObject>().MyNetworkList;
}
}
Debug.Log($"{numComparison} comparisons done.");
return true;
}
public IEnumerator RefreshGameObects()
public IEnumerator RefreshGameObects(int numberToExpect)
{
m_NetSpawnedObjectOnClient.Clear();
yield return WaitForConditionOrTimeOut(() => numberToExpect == HiddenVariableObject.ClientInstancesSpawned.Count);
Assert.False(s_GlobalTimeoutHelper.TimedOut, $"Timed out waiting for total spawned count to reach {numberToExpect} but is currently {HiddenVariableObject.ClientInstancesSpawned.Count}");
m_NetSpawnedObjectOnClient = HiddenVariableObject.ClientInstancesSpawned;
}
foreach (var netMan in m_ClientNetworkManagers)
private bool CheckValueOnClient(ulong otherClientId, int value)
{
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
{
var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.NetworkObjectId == m_NetSpawnedObject.NetworkObjectId,
netMan,
serverClientPlayerResult);
m_NetSpawnedObjectOnClient.Add(serverClientPlayerResult.Result);
if (id != otherClientId)
{
if (!HiddenVariableObject.ValueOnClient.ContainsKey(id) || HiddenVariableObject.ValueOnClient[id] != value)
{
return false;
}
}
}
return true;
}
private IEnumerator SetAndCheckValueSet(ulong otherClientId, int value)
{
yield return WaitForConditionOrTimeOut(() => CheckValueOnClient(otherClientId, value));
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, $"Timed out waiting for all clients to have a value of {value}");
yield return WaitForConditionOrTimeOut(VerifyLists);
Assert.IsFalse(s_GlobalTimeoutHelper.TimedOut, "Timed out waiting for all clients to have identical values!");
Debug.Log("Value changed");
}
[UnityTest]
@@ -127,15 +168,14 @@ namespace Unity.Netcode.RuntimeTests
Debug.Log("Running test");
// ==== Spawn object with ownership on one client
var client = m_ServerNetworkManager.ConnectedClientsList[1];
var otherClient = m_ServerNetworkManager.ConnectedClientsList[2];
m_NetSpawnedObject = SpawnObject(m_TestNetworkPrefab, m_ClientNetworkManagers[1]).GetComponent<NetworkObject>();
yield return RefreshGameObects();
yield return RefreshGameObects(4);
// === Check spawn occured
// === Check spawn occurred
yield return WaitForSpawnCount(NumberOfClients + 1);
Debug.Assert(HiddenVariableObject.SpawnCount == NumberOfClients + 1);
Debug.Log("Objects spawned");
@@ -143,41 +183,22 @@ namespace Unity.Netcode.RuntimeTests
// ==== Set the NetworkVariable value to 2
HiddenVariableObject.ExpectedSize = 1;
HiddenVariableObject.SpawnCount = 0;
var currentValueSet = 2;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = 2;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(2);
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = currentValueSet;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(currentValueSet);
yield return new WaitForSeconds(1.0f);
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
{
Debug.Assert(HiddenVariableObject.ValueOnClient[id] == 2);
}
VerifyLists();
Debug.Log("Value changed");
yield return SetAndCheckValueSet(otherClient.ClientId, currentValueSet);
// ==== Hide our object to a different client
HiddenVariableObject.ExpectedSize = 2;
m_NetSpawnedObject.NetworkHide(otherClient.ClientId);
// ==== Change the NetworkVariable value
// we should get one less notification of value changing and no errors or exception
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = 3;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(3);
currentValueSet = 3;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = currentValueSet;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(currentValueSet);
yield return new WaitForSeconds(1.0f);
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
{
if (id != otherClient.ClientId)
{
Debug.Assert(HiddenVariableObject.ValueOnClient[id] == 3);
}
}
VerifyLists();
Debug.Log("Values changed");
yield return SetAndCheckValueSet(otherClient.ClientId, currentValueSet);
// ==== Show our object again to this client
HiddenVariableObject.ExpectedSize = 3;
@@ -189,22 +210,13 @@ namespace Unity.Netcode.RuntimeTests
Debug.Log("Object spawned");
// ==== We need a refresh for the newly re-spawned object
yield return RefreshGameObects();
yield return RefreshGameObects(4);
// ==== Change the NetworkVariable value
// we should get all notifications of value changing and no errors or exception
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = 4;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(4);
currentValueSet = 4;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkVariable.Value = currentValueSet;
m_NetSpawnedObject.GetComponent<HiddenVariableObject>().MyNetworkList.Add(currentValueSet);
yield return new WaitForSeconds(1.0f);
foreach (var id in m_ServerNetworkManager.ConnectedClientsIds)
{
Debug.Assert(HiddenVariableObject.ValueOnClient[id] == 4);
}
VerifyLists();
Debug.Log("Values changed");
yield return SetAndCheckValueSet(otherClient.ClientId, currentValueSet);
// ==== Hide our object to that different client again, and then destroy it
m_NetSpawnedObject.NetworkHide(otherClient.ClientId);

View File

@@ -18,7 +18,7 @@ namespace Unity.Netcode.RuntimeTests.Metrics
{
protected override int NumberOfClients => 1;
private readonly int m_PacketLossRate = 25;
private int m_DropInterval = 5;
private readonly int m_PacketLossRangeDelta = 5;
public PacketLossMetricsTests()
: base(HostOrServer.Server)
@@ -57,11 +57,12 @@ namespace Unity.Netcode.RuntimeTests.Metrics
[UnityTest]
public IEnumerator TrackPacketLossAsClient()
{
double packetLossRate = m_PacketLossRate/100d;
double packetLossRateMinRange = (m_PacketLossRate-m_PacketLossRangeDelta) / 100d;
double packetLossRateMaxrange = (m_PacketLossRate + m_PacketLossRangeDelta) / 100d;
var clientNetworkManager = m_ClientNetworkManagers[0];
var waitForPacketLossMetric = new WaitForGaugeMetricValues((clientNetworkManager.NetworkMetrics as NetworkMetrics).Dispatcher,
NetworkMetricTypes.PacketLoss,
metric => Math.Abs(metric - packetLossRate) < Double.Epsilon);
metric => packetLossRateMinRange <= metric && metric <= packetLossRateMaxrange);
for (int i = 0; i < 1000; ++i)
{
@@ -75,7 +76,7 @@ namespace Unity.Netcode.RuntimeTests.Metrics
yield return waitForPacketLossMetric.WaitForMetricsReceived();
var packetLossValue = waitForPacketLossMetric.AssertMetricValueHaveBeenFound();
Assert.AreEqual(packetLossRate, packetLossValue);
Assert.That(packetLossValue, Is.InRange(packetLossRateMinRange, packetLossRateMaxrange));
}
}
}

View File

@@ -3,131 +3,158 @@ using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using Object = UnityEngine.Object;
namespace Unity.Netcode.RuntimeTests
{
[TestFixture(HostOrServer.Host)]
[TestFixture(HostOrServer.Server)]
public class NetworkManagerTransportTests : NetcodeIntegrationTest
public class NetworkManagerTransportTests
{
protected override int NumberOfClients => 1;
private bool m_CanStartServerAndClients = false;
public NetworkManagerTransportTests(HostOrServer hostOrServer) : base(hostOrServer) { }
protected override IEnumerator OnSetup()
[Test]
public void ClientDoesNotStartWhenTransportFails()
{
m_CanStartServerAndClients = false;
return base.OnSetup();
bool callbackInvoked = false;
Action onTransportFailure = () => { callbackInvoked = true; };
var manager = new GameObject().AddComponent<NetworkManager>();
manager.OnTransportFailure += onTransportFailure;
var transport = manager.gameObject.AddComponent<FailedTransport>();
transport.FailOnStart = true;
manager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport };
LogAssert.Expect(LogType.Error, $"Client is shutting down due to network transport start failure of {transport.GetType().Name}!");
Assert.False(manager.StartClient());
Assert.False(manager.IsListening);
Assert.False(manager.IsConnectedClient);
Assert.True(callbackInvoked);
}
protected override bool CanStartServerAndClients()
[Test]
public void HostDoesNotStartWhenTransportFails()
{
return m_CanStartServerAndClients;
bool callbackInvoked = false;
Action onTransportFailure = () => { callbackInvoked = true; };
var manager = new GameObject().AddComponent<NetworkManager>();
manager.OnTransportFailure += onTransportFailure;
var transport = manager.gameObject.AddComponent<FailedTransport>();
transport.FailOnStart = true;
manager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport };
LogAssert.Expect(LogType.Error, $"Server is shutting down due to network transport start failure of {transport.GetType().Name}!");
Assert.False(manager.StartHost());
Assert.False(manager.IsListening);
Assert.True(callbackInvoked);
}
[Test]
public void ServerDoesNotStartWhenTransportFails()
{
bool callbackInvoked = false;
Action onTransportFailure = () => { callbackInvoked = true; };
var manager = new GameObject().AddComponent<NetworkManager>();
manager.OnTransportFailure += onTransportFailure;
var transport = manager.gameObject.AddComponent<FailedTransport>();
transport.FailOnStart = true;
manager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport };
LogAssert.Expect(LogType.Error, $"Server is shutting down due to network transport start failure of {transport.GetType().Name}!");
Assert.False(manager.StartServer());
Assert.False(manager.IsListening);
Assert.True(callbackInvoked);
}
[UnityTest]
public IEnumerator ShutsDownWhenTransportFails()
{
bool callbackInvoked = false;
Action onTransportFailure = () => { callbackInvoked = true; };
var manager = new GameObject().AddComponent<NetworkManager>();
manager.OnTransportFailure += onTransportFailure;
var transport = manager.gameObject.AddComponent<FailedTransport>();
transport.FailOnNextPoll = true;
manager.NetworkConfig = new NetworkConfig() { NetworkTransport = transport };
Assert.True(manager.StartServer());
Assert.True(manager.IsListening);
LogAssert.Expect(LogType.Error, $"Shutting down due to network transport failure of {transport.GetType().Name}!");
// Need two updates to actually shut down. First one to see the transport failing, which
// marks the NetworkManager as shutting down. Second one where actual shutdown occurs.
yield return null;
yield return null;
Assert.False(manager.IsListening);
Assert.True(callbackInvoked);
}
/// <summary>
/// Validate that if the NetworkTransport fails to start the NetworkManager
/// will not continue the startup process and will shut itself down.
/// Does nothing but simulate a transport that can fail at startup and/or when polling events.
/// </summary>
/// <param name="testClient">if true it will test the client side</param>
[UnityTest]
public IEnumerator DoesNotStartWhenTransportFails([Values] bool testClient)
public class FailedTransport : TestingNetworkTransport
{
// The error message we should expect
var messageToCheck = "";
if (!testClient)
{
Object.DestroyImmediate(m_ServerNetworkManager.NetworkConfig.NetworkTransport);
m_ServerNetworkManager.NetworkConfig.NetworkTransport = m_ServerNetworkManager.gameObject.AddComponent<FailedTransport>();
m_ServerNetworkManager.NetworkConfig.NetworkTransport.Initialize(m_ServerNetworkManager);
// The error message we should expect
messageToCheck = $"Server is shutting down due to network transport start failure of {m_ServerNetworkManager.NetworkConfig.NetworkTransport.GetType().Name}!";
}
else
{
foreach (var client in m_ClientNetworkManagers)
{
Object.DestroyImmediate(client.NetworkConfig.NetworkTransport);
client.NetworkConfig.NetworkTransport = client.gameObject.AddComponent<FailedTransport>();
client.NetworkConfig.NetworkTransport.Initialize(m_ServerNetworkManager);
}
// The error message we should expect
messageToCheck = $"Client is shutting down due to network transport start failure of {m_ClientNetworkManagers[0].NetworkConfig.NetworkTransport.GetType().Name}!";
}
public bool FailOnStart = false;
public bool FailOnNextPoll = false;
// Trap for the nested NetworkManager exception
LogAssert.Expect(LogType.Error, messageToCheck);
m_CanStartServerAndClients = true;
// Due to other errors, we must not send clients if testing the server-host side
// We can test both server and client(s) when testing client-side only
if (testClient)
public override bool StartClient() => !FailOnStart;
public override bool StartServer() => !FailOnStart;
public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
{
NetcodeIntegrationTestHelpers.Start(m_UseHost, m_ServerNetworkManager, m_ClientNetworkManagers);
yield return s_DefaultWaitForTick;
foreach (var client in m_ClientNetworkManagers)
clientId = 0;
payload = new ArraySegment<byte>();
receiveTime = 0;
if (FailOnNextPoll)
{
Assert.False(client.IsListening);
Assert.False(client.IsConnectedClient);
FailOnNextPoll = false;
return NetworkEvent.TransportFailure;
}
else
{
return NetworkEvent.Nothing;
}
}
else
public override ulong ServerClientId => 0;
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
{
NetcodeIntegrationTestHelpers.Start(m_UseHost, m_ServerNetworkManager, new NetworkManager[] { });
yield return s_DefaultWaitForTick;
Assert.False(m_ServerNetworkManager.IsListening);
}
}
}
/// <summary>
/// Does nothing but simulate a transport that failed to start
/// </summary>
public class FailedTransport : TestingNetworkTransport
{
public override void Shutdown()
{
}
public override void Initialize(NetworkManager networkManager = null)
{
}
public override ulong ServerClientId => 0;
public override void Shutdown()
{
}
public override NetworkEvent PollEvent(out ulong clientId, out ArraySegment<byte> payload, out float receiveTime)
{
clientId = 0;
payload = new ArraySegment<byte>();
receiveTime = 0;
return NetworkEvent.Nothing;
}
public override bool StartClient()
{
// Simulate failure, always return false
return false;
}
public override bool StartServer()
{
// Simulate failure, always return false
return false;
}
public override void Send(ulong clientId, ArraySegment<byte> payload, NetworkDelivery networkDelivery)
{
}
public override ulong GetCurrentRtt(ulong clientId) => 0;
public override void DisconnectRemoteClient(ulong clientId)
{
}
public override void DisconnectRemoteClient(ulong clientId)
{
}
public override void Initialize(NetworkManager networkManager = null)
{
}
public override ulong GetCurrentRtt(ulong clientId)
{
return 0;
}
public override void DisconnectLocalClient()
{
public override void DisconnectLocalClient()
{
}
}
}
}

View File

@@ -168,6 +168,23 @@ namespace Unity.Netcode.RuntimeTests
Assert.False(s_GlobalTimeoutHelper.TimedOut, "Timed out while waiting for client side despawns! (2nd pass)");
}
[Test]
public void DynamicallySpawnedNoSceneOriginException()
{
var gameObject = new GameObject();
var networkObject = gameObject.AddComponent<NetworkObject>();
networkObject.IsSpawned = true;
networkObject.SceneOriginHandle = 0;
networkObject.IsSceneObject = false;
// This validates invoking GetSceneOriginHandle will not throw an exception for a dynamically spawned NetworkObject
// when the scene of origin handle is zero
var sceneOriginHandle = networkObject.GetSceneOriginHandle();
// This validates that GetSceneOriginHandle will return the GameObject's scene handle that should be the currently active scene
var activeSceneHandle = UnityEngine.SceneManagement.SceneManager.GetActiveScene().handle;
Assert.IsTrue(sceneOriginHandle == activeSceneHandle, $"{nameof(NetworkObject)} should have returned the active scene handle of {activeSceneHandle} but returned {sceneOriginHandle}");
}
private class TrackOnSpawnFunctions : NetworkBehaviour
{
public int OnNetworkSpawnCalledCount { get; private set; }

View File

@@ -54,7 +54,7 @@ namespace Unity.Netcode.RuntimeTests
invalidNetworkObjects.Add(gameObject);
writer.WriteValueSafe((int)networkObject.gameObject.scene.handle);
writer.WriteValueSafe((int)networkObject.GetSceneOriginHandle());
// Serialize the invalid NetworkObject
var sceneObject = networkObject.GetMessageSceneObject(0);
var prePosition = writer.Position;
@@ -85,7 +85,7 @@ namespace Unity.Netcode.RuntimeTests
networkObjectsToTest.Add(gameObject);
writer.WriteValueSafe((int)networkObject.gameObject.scene.handle);
writer.WriteValueSafe(networkObject.GetSceneOriginHandle());
// Handle populating the scenes loaded list
var scene = networkObject.gameObject.scene;
@@ -96,13 +96,13 @@ namespace Unity.Netcode.RuntimeTests
NetworkManagerHelper.NetworkManagerObject.SceneManager.ScenesLoaded
.Add(scene.handle, scene);
}
var handle = networkObject.GetSceneOriginHandle();
// Since this is a unit test, we will fake the server to client handle lookup by just adding the same handle key and value
if (!NetworkManagerHelper.NetworkManagerObject.SceneManager.ServerSceneHandleToClientSceneHandle
.ContainsKey(networkObject.gameObject.scene.handle))
.ContainsKey(handle))
{
NetworkManagerHelper.NetworkManagerObject.SceneManager.ServerSceneHandleToClientSceneHandle
.Add(networkObject.gameObject.scene.handle, networkObject.gameObject.scene.handle);
.Add(handle, handle);
}
// Serialize the valid NetworkObject

View File

@@ -1,6 +1,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;
@@ -8,13 +8,46 @@ using Unity.Netcode.TestHelpers.Runtime;
namespace Unity.Netcode.RuntimeTests
{
public class NetworkShowHideTest : NetworkBehaviour
public class NetworkShowHideTestComponent : NetworkBehaviour
{
}
public class ShowHideObject : NetworkBehaviour
{
public static List<ShowHideObject> ClientTargetedNetworkObjects = new List<ShowHideObject>();
public static ulong ClientIdToTarget;
public static NetworkObject GetNetworkObjectById(ulong networkObjectId)
{
foreach (var entry in ClientTargetedNetworkObjects)
{
if (entry.NetworkObjectId == networkObjectId)
{
return entry.NetworkObject;
}
}
return null;
}
public override void OnNetworkSpawn()
{
if (NetworkManager.LocalClientId == ClientIdToTarget)
{
ClientTargetedNetworkObjects.Add(this);
}
base.OnNetworkSpawn();
}
public override void OnNetworkDespawn()
{
if (ClientTargetedNetworkObjects.Contains(this))
{
ClientTargetedNetworkObjects.Remove(this);
}
base.OnNetworkDespawn();
}
public NetworkVariable<int> MyNetworkVariable;
private void Start()
@@ -27,7 +60,6 @@ namespace Unity.Netcode.RuntimeTests
{
Debug.Log($"Value changed from {before} to {after}");
}
}
public class NetworkShowHideTests : NetcodeIntegrationTest
@@ -46,30 +78,17 @@ namespace Unity.Netcode.RuntimeTests
protected override void OnCreatePlayerPrefab()
{
var networkTransform = m_PlayerPrefab.AddComponent<NetworkShowHideTest>();
var networkTransform = m_PlayerPrefab.AddComponent<NetworkShowHideTestComponent>();
}
protected override void OnServerAndClientsCreated()
{
m_PrefabToSpawn = PreparePrefab(typeof(ShowHideObject));
}
public GameObject PreparePrefab(Type type)
{
var prefabToSpawn = new GameObject();
prefabToSpawn.AddComponent(type);
var networkObjectPrefab = prefabToSpawn.AddComponent<NetworkObject>();
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObjectPrefab);
m_ServerNetworkManager.NetworkConfig.NetworkPrefabs.Add(new NetworkPrefab() { Prefab = prefabToSpawn });
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.NetworkConfig.NetworkPrefabs.Add(new NetworkPrefab() { Prefab = prefabToSpawn });
}
return prefabToSpawn;
m_PrefabToSpawn = CreateNetworkObjectPrefab("ShowHideObject");
m_PrefabToSpawn.AddComponent<ShowHideObject>();
}
// Check that the first client see them, or not, as expected
private IEnumerator CheckVisible(bool target)
private IEnumerator CheckVisible(bool isVisible)
{
int count = 0;
do
@@ -83,21 +102,31 @@ namespace Unity.Netcode.RuntimeTests
Assert.Fail("timeout waiting for object to reach the expect visibility");
break;
}
} while (m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) != target ||
m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) != target ||
m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) != target ||
m_Object1OnClient0.IsSpawned != target ||
m_Object2OnClient0.IsSpawned != target ||
m_Object3OnClient0.IsSpawned != target
} while (m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) != isVisible ||
m_Object1OnClient0.IsSpawned != isVisible ||
m_Object2OnClient0.IsSpawned != isVisible ||
m_Object3OnClient0.IsSpawned != isVisible
);
Debug.Assert(m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) == target);
Debug.Assert(m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) == target);
Debug.Assert(m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) == target);
Debug.Assert(m_NetSpawnedObject1.IsNetworkVisibleTo(m_ClientId0) == isVisible);
Debug.Assert(m_NetSpawnedObject2.IsNetworkVisibleTo(m_ClientId0) == isVisible);
Debug.Assert(m_NetSpawnedObject3.IsNetworkVisibleTo(m_ClientId0) == isVisible);
Debug.Assert(m_Object1OnClient0.IsSpawned == target);
Debug.Assert(m_Object2OnClient0.IsSpawned == target);
Debug.Assert(m_Object3OnClient0.IsSpawned == target);
Debug.Assert(m_Object1OnClient0.IsSpawned == isVisible);
Debug.Assert(m_Object2OnClient0.IsSpawned == isVisible);
Debug.Assert(m_Object3OnClient0.IsSpawned == isVisible);
var clientNetworkManager = m_ClientNetworkManagers.Where((c) => c.LocalClientId == m_ClientId0).First();
if (isVisible)
{
Assert.True(ShowHideObject.ClientTargetedNetworkObjects.Count == 3, $"Client-{clientNetworkManager.LocalClientId} should have 3 instances visible but only has {ShowHideObject.ClientTargetedNetworkObjects.Count}!");
}
else
{
Assert.True(ShowHideObject.ClientTargetedNetworkObjects.Count == 0, $"Client-{clientNetworkManager.LocalClientId} should have no visible instances but still has {ShowHideObject.ClientTargetedNetworkObjects.Count}!");
}
}
// Set the 3 objects visibility
@@ -136,30 +165,19 @@ namespace Unity.Netcode.RuntimeTests
}
}
private IEnumerator RefreshNetworkObjects()
private bool RefreshNetworkObjects()
{
var serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.NetworkObjectId == m_NetSpawnedObject1.NetworkObjectId && x.IsSpawned,
m_ClientNetworkManagers[0],
serverClientPlayerResult);
m_Object1OnClient0 = serverClientPlayerResult.Result;
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.NetworkObjectId == m_NetSpawnedObject2.NetworkObjectId && x.IsSpawned,
m_ClientNetworkManagers[0],
serverClientPlayerResult);
m_Object2OnClient0 = serverClientPlayerResult.Result;
serverClientPlayerResult = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
yield return NetcodeIntegrationTestHelpers.GetNetworkObjectByRepresentation(
x => x.NetworkObjectId == m_NetSpawnedObject3.NetworkObjectId && x.IsSpawned,
m_ClientNetworkManagers[0],
serverClientPlayerResult);
m_Object3OnClient0 = serverClientPlayerResult.Result;
// make sure the objects are set with the right network manager
m_Object1OnClient0.NetworkManagerOwner = m_ClientNetworkManagers[0];
m_Object2OnClient0.NetworkManagerOwner = m_ClientNetworkManagers[0];
m_Object3OnClient0.NetworkManagerOwner = m_ClientNetworkManagers[0];
m_Object1OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject1.NetworkObjectId);
m_Object2OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject2.NetworkObjectId);
m_Object3OnClient0 = ShowHideObject.GetNetworkObjectById(m_NetSpawnedObject3.NetworkObjectId);
if (m_Object1OnClient0 == null || m_Object2OnClient0 == null || m_Object3OnClient0 == null)
{
return false;
}
Assert.True(m_Object1OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
Assert.True(m_Object2OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
Assert.True(m_Object3OnClient0.NetworkManagerOwner == m_ClientNetworkManagers[0]);
return true;
}
@@ -167,28 +185,23 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator NetworkShowHideTest()
{
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
ShowHideObject.ClientTargetedNetworkObjects.Clear();
ShowHideObject.ClientIdToTarget = m_ClientId0;
// create 3 objects
var spawnedObject1 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
var spawnedObject2 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
var spawnedObject3 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
m_NetSpawnedObject1.NetworkManagerOwner = m_ServerNetworkManager;
m_NetSpawnedObject2.NetworkManagerOwner = m_ServerNetworkManager;
m_NetSpawnedObject3.NetworkManagerOwner = m_ServerNetworkManager;
m_NetSpawnedObject1.Spawn();
m_NetSpawnedObject2.Spawn();
m_NetSpawnedObject3.Spawn();
for (int mode = 0; mode < 2; mode++)
{
// get the NetworkObject on a client instance
yield return RefreshNetworkObjects();
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
// check object start visible
yield return CheckVisible(true);
@@ -207,7 +220,8 @@ namespace Unity.Netcode.RuntimeTests
// show them to that client
Show(mode == 0, true);
yield return RefreshNetworkObjects();
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
// verify they become visible
yield return CheckVisible(true);
@@ -218,24 +232,21 @@ namespace Unity.Netcode.RuntimeTests
public IEnumerator NetworkShowHideQuickTest()
{
m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId;
ShowHideObject.ClientTargetedNetworkObjects.Clear();
ShowHideObject.ClientIdToTarget = m_ClientId0;
var spawnedObject1 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
var spawnedObject2 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
var spawnedObject3 = UnityEngine.Object.Instantiate(m_PrefabToSpawn);
var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject2 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
var spawnedObject3 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager);
m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>();
m_NetSpawnedObject2 = spawnedObject2.GetComponent<NetworkObject>();
m_NetSpawnedObject3 = spawnedObject3.GetComponent<NetworkObject>();
m_NetSpawnedObject1.NetworkManagerOwner = m_ServerNetworkManager;
m_NetSpawnedObject2.NetworkManagerOwner = m_ServerNetworkManager;
m_NetSpawnedObject3.NetworkManagerOwner = m_ServerNetworkManager;
m_NetSpawnedObject1.Spawn();
m_NetSpawnedObject2.Spawn();
m_NetSpawnedObject3.Spawn();
for (int mode = 0; mode < 2; mode++)
{
// get the NetworkObject on a client instance
yield return RefreshNetworkObjects();
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
// check object start visible
yield return CheckVisible(true);
@@ -245,7 +256,8 @@ namespace Unity.Netcode.RuntimeTests
Show(mode == 0, true);
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
yield return RefreshNetworkObjects();
yield return WaitForConditionOrTimeOut(RefreshNetworkObjects);
AssertOnTimeout($"Could not refresh all NetworkObjects!");
yield return NetcodeIntegrationTestHelpers.WaitForTicks(m_ServerNetworkManager, 5);
// verify they become visible

View File

@@ -267,7 +267,7 @@ namespace Unity.Netcode.RuntimeTests
}
}
protected override bool OnIsServerAuthoritatitive()
protected override bool OnIsServerAuthoritative()
{
return false;
}

View File

@@ -271,11 +271,18 @@ namespace Unity.Netcode.RuntimeTests
public class NetworkVariableTest : NetworkBehaviour
{
public enum SomeEnum
{
A,
B,
C
}
public readonly NetworkVariable<int> TheScalar = new NetworkVariable<int>();
public readonly NetworkVariable<SomeEnum> TheEnum = new NetworkVariable<SomeEnum>();
public readonly NetworkList<int> TheList = new NetworkList<int>();
public readonly NetworkList<ForceNetworkSerializeByMemcpy<FixedString128Bytes>> TheLargeList = new NetworkList<ForceNetworkSerializeByMemcpy<FixedString128Bytes>>();
public readonly NetworkList<FixedString128Bytes> TheLargeList = new NetworkList<FixedString128Bytes>();
public readonly NetworkVariable<ForceNetworkSerializeByMemcpy<FixedString32Bytes>> FixedString32 = new NetworkVariable<ForceNetworkSerializeByMemcpy<FixedString32Bytes>>();
public readonly NetworkVariable<FixedString32Bytes> FixedString32 = new NetworkVariable<FixedString32Bytes>();
private void ListChanged(NetworkListEvent<int> e)
{
@@ -365,6 +372,7 @@ namespace Unity.Netcode.RuntimeTests
// Wait for connection on client and server side
yield return WaitForClientsConnectedOrTimeOut();
AssertOnTimeout($"Timed-out waiting for all clients to connect!");
// These are the *SERVER VERSIONS* of the *CLIENT PLAYER 1 & 2*
var result = new NetcodeIntegrationTestHelpers.ResultWrapper<NetworkObject>();
@@ -597,7 +605,7 @@ namespace Unity.Netcode.RuntimeTests
bool VerifyStructure()
{
return m_Player1OnClient1.TheStruct.Value.SomeBool == m_Player1OnServer.TheStruct.Value.SomeBool &&
m_Player1OnClient1.TheStruct.Value.SomeInt == m_Player1OnServer.TheStruct.Value.SomeInt;
m_Player1OnClient1.TheStruct.Value.SomeInt == m_Player1OnServer.TheStruct.Value.SomeInt;
}
m_Player1OnServer.TheStruct.Value = new TestStruct() { SomeInt = k_TestUInt, SomeBool = false };
@@ -607,6 +615,23 @@ namespace Unity.Netcode.RuntimeTests
yield return WaitForConditionOrTimeOut(VerifyStructure);
}
[UnityTest]
public IEnumerator TestNetworkVariableEnum([Values(true, false)] bool useHost)
{
yield return InitializeServerAndClients(useHost);
bool VerifyStructure()
{
return m_Player1OnClient1.TheEnum.Value == NetworkVariableTest.SomeEnum.C;
}
m_Player1OnServer.TheEnum.Value = NetworkVariableTest.SomeEnum.C;
m_Player1OnServer.TheEnum.SetDirty(true);
// Wait for the client-side to notify it is finished initializing and spawning.
yield return WaitForConditionOrTimeOut(VerifyStructure);
}
[UnityTest]
public IEnumerator TestINetworkSerializableCallsNetworkSerialize([Values(true, false)] bool useHost)
{

View File

@@ -0,0 +1,213 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.TestTools;
using NUnit.Framework;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using Object = UnityEngine.Object;
namespace Unity.Netcode.RuntimeTests
{
public struct MyTypeOne
{
public int Value;
}
public struct MyTypeTwo
{
public int Value;
}
public struct MyTypeThree
{
public int Value;
}
/// <summary>
/// Used to help track instances of any child derived class
/// </summary>
public class WorkingUserNetworkVariableComponentBase : NetworkBehaviour
{
private static Dictionary<ulong, WorkingUserNetworkVariableComponentBase> s_Instances = new Dictionary<ulong, WorkingUserNetworkVariableComponentBase>();
internal static T GetRelativeInstance<T>(ulong clientId) where T : NetworkBehaviour
{
if (s_Instances.ContainsKey(clientId))
{
return s_Instances[clientId] as T;
}
return null;
}
public static void Reset()
{
s_Instances.Clear();
}
public override void OnNetworkSpawn()
{
if (!s_Instances.ContainsKey(NetworkManager.LocalClientId))
{
s_Instances.Add(NetworkManager.LocalClientId, this);
}
else
{
Debug.LogWarning($"{name} is spawned but client id {NetworkManager.LocalClientId} instance already exists!");
}
}
public override void OnNetworkDespawn()
{
if (s_Instances.ContainsKey(NetworkManager.LocalClientId))
{
s_Instances.Remove(NetworkManager.LocalClientId);
}
else
{
Debug.LogWarning($"{name} is was never spawned but client id {NetworkManager.LocalClientId} is trying to despawn it!");
}
}
}
public class WorkingUserNetworkVariableComponent : WorkingUserNetworkVariableComponentBase
{
public NetworkVariable<MyTypeOne> NetworkVariable = new NetworkVariable<MyTypeOne>();
}
public class WorkingUserNetworkVariableComponentUsingExtensionMethod : WorkingUserNetworkVariableComponentBase
{
public NetworkVariable<MyTypeTwo> NetworkVariable = new NetworkVariable<MyTypeTwo>();
}
public class NonWorkingUserNetworkVariableComponent : NetworkBehaviour
{
public NetworkVariable<MyTypeThree> NetworkVariable = new NetworkVariable<MyTypeThree>();
}
internal static class NetworkVariableUserSerializableTypesTestsExtensionMethods
{
public static void WriteValueSafe(this FastBufferWriter writer, in MyTypeTwo value)
{
writer.WriteValueSafe(value.Value);
}
public static void ReadValueSafe(this FastBufferReader reader, out MyTypeTwo value)
{
value = new MyTypeTwo();
reader.ReadValueSafe(out value.Value);
}
}
public class NetworkVariableUserSerializableTypesTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;
public NetworkVariableUserSerializableTypesTests()
: base(HostOrServer.Server)
{
}
private GameObject m_WorkingPrefab;
private GameObject m_ExtensionMethodPrefab;
private GameObject m_NonWorkingPrefab;
protected override IEnumerator OnSetup()
{
WorkingUserNetworkVariableComponentBase.Reset();
return base.OnSetup();
}
protected override void OnServerAndClientsCreated()
{
m_WorkingPrefab = CreateNetworkObjectPrefab($"[{nameof(NetworkVariableUserSerializableTypesTests)}.{nameof(m_WorkingPrefab)}]");
m_ExtensionMethodPrefab = CreateNetworkObjectPrefab($"[{nameof(NetworkVariableUserSerializableTypesTests)}.{nameof(m_ExtensionMethodPrefab)}]");
m_NonWorkingPrefab = CreateNetworkObjectPrefab($"[{nameof(NetworkVariableUserSerializableTypesTests)}.{nameof(m_NonWorkingPrefab)}]");
m_WorkingPrefab.AddComponent<WorkingUserNetworkVariableComponent>();
m_ExtensionMethodPrefab.AddComponent<WorkingUserNetworkVariableComponentUsingExtensionMethod>();
m_NonWorkingPrefab.AddComponent<NonWorkingUserNetworkVariableComponent>();
}
private bool CheckForClientInstance<T>() where T : WorkingUserNetworkVariableComponentBase
{
var instance = WorkingUserNetworkVariableComponentBase.GetRelativeInstance<T>(m_ClientNetworkManagers[0].LocalClientId);
return instance != null && instance.IsSpawned;
}
[UnityTest]
public IEnumerator WhenUsingAUserSerializableNetworkVariableWithUserSerialization_ReplicationWorks()
{
UserNetworkVariableSerialization<MyTypeOne>.WriteValue = (FastBufferWriter writer, in MyTypeOne value) =>
{
writer.WriteValueSafe(value.Value);
};
UserNetworkVariableSerialization<MyTypeOne>.ReadValue = (FastBufferReader reader, out MyTypeOne value) =>
{
value = new MyTypeOne();
reader.ReadValueSafe(out value.Value);
};
var serverObject = SpawnObject(m_WorkingPrefab, m_ServerNetworkManager);
var serverNetworkObject = serverObject.GetComponent<NetworkObject>();
// Wait for the client instance to be spawned, which removes the need to check for two NetworkVariableDeltaMessages
yield return WaitForConditionOrTimeOut(() => CheckForClientInstance<WorkingUserNetworkVariableComponent>());
AssertOnTimeout($"Timed out waiting for the client side object to spawn!");
// Get server and client instances of the test component
var clientInstance = WorkingUserNetworkVariableComponentBase.GetRelativeInstance<WorkingUserNetworkVariableComponent>(m_ClientNetworkManagers[0].LocalClientId);
var serverInstance = serverNetworkObject.GetComponent<WorkingUserNetworkVariableComponent>();
// Set the server side value
serverInstance.NetworkVariable.Value = new MyTypeOne { Value = 20 };
// Wait for the NetworkVariableDeltaMessage
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<NetworkVariableDeltaMessage>(m_ClientNetworkManagers[0]);
// Wait for the client side value to be updated to the server side value (can take an additional frame)
yield return WaitForConditionOrTimeOut(() => clientInstance.NetworkVariable.Value.Value == serverInstance.NetworkVariable.Value.Value);
Assert.AreEqual(serverNetworkObject.GetComponent<WorkingUserNetworkVariableComponent>().NetworkVariable.Value.Value, clientInstance.NetworkVariable.Value.Value);
Assert.AreEqual(20, clientInstance.NetworkVariable.Value.Value);
}
[UnityTest]
public IEnumerator WhenUsingAUserSerializableNetworkVariableWithUserSerializationViaExtensionMethod_ReplicationWorks()
{
UserNetworkVariableSerialization<MyTypeTwo>.WriteValue = NetworkVariableUserSerializableTypesTestsExtensionMethods.WriteValueSafe;
UserNetworkVariableSerialization<MyTypeTwo>.ReadValue = NetworkVariableUserSerializableTypesTestsExtensionMethods.ReadValueSafe;
var serverObject = SpawnObject(m_ExtensionMethodPrefab, m_ServerNetworkManager);
var serverNetworkObject = serverObject.GetComponent<NetworkObject>();
// Wait for the client instance to be spawned, which removes the need to check for two NetworkVariableDeltaMessages
yield return WaitForConditionOrTimeOut(() => CheckForClientInstance<WorkingUserNetworkVariableComponentUsingExtensionMethod>());
AssertOnTimeout($"Timed out waiting for the client side object to spawn!");
// Get server and client instances of the test component
var clientInstance = WorkingUserNetworkVariableComponentBase.GetRelativeInstance<WorkingUserNetworkVariableComponentUsingExtensionMethod>(m_ClientNetworkManagers[0].LocalClientId);
var serverInstance = serverNetworkObject.GetComponent<WorkingUserNetworkVariableComponentUsingExtensionMethod>();
// Set the server side value
serverInstance.NetworkVariable.Value = new MyTypeTwo { Value = 20 };
// Wait for the NetworkVariableDeltaMessage
yield return NetcodeIntegrationTestHelpers.WaitForMessageOfTypeReceived<NetworkVariableDeltaMessage>(m_ClientNetworkManagers[0]);
// Wait for the client side value to be updated to the server side value (can take an additional frame)
yield return WaitForConditionOrTimeOut(() => clientInstance.NetworkVariable.Value.Value == serverInstance.NetworkVariable.Value.Value);
AssertOnTimeout($"Timed out waiting for the client side object's value ({clientInstance.NetworkVariable.Value.Value}) to equal the server side objects value ({serverInstance.NetworkVariable.Value.Value})!");
Assert.AreEqual(serverInstance.NetworkVariable.Value.Value, clientInstance.NetworkVariable.Value.Value);
Assert.AreEqual(20, clientInstance.NetworkVariable.Value.Value);
}
[Test]
public void WhenUsingAUserSerializableNetworkVariableWithoutUserSerialization_ReplicationFails()
{
var serverObject = Object.Instantiate(m_NonWorkingPrefab);
var serverNetworkObject = serverObject.GetComponent<NetworkObject>();
serverNetworkObject.NetworkManagerOwner = m_ServerNetworkManager;
Assert.Throws<ArgumentException>(
() =>
{
serverNetworkObject.Spawn();
}
);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a1195640cf744e85b58c5af6d11eeb34
timeCreated: 1652907276

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Unity.Collections;
using UnityEngine.TestTools;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
@@ -14,7 +15,7 @@ namespace Unity.Netcode.RuntimeTests
public class RpcTestNB : NetworkBehaviour
{
public event Action<ulong, ServerRpcParams> OnServer_Rpc;
public event Action<Vector3, Vector3[]> OnTypedServer_Rpc;
public event Action<Vector3, Vector3[], FixedString32Bytes> OnTypedServer_Rpc;
public event Action OnClient_Rpc;
[ServerRpc]
@@ -30,9 +31,9 @@ namespace Unity.Netcode.RuntimeTests
}
[ServerRpc]
public void MyTypedServerRpc(Vector3 param1, Vector3[] param2)
public void MyTypedServerRpc(Vector3 param1, Vector3[] param2, FixedString32Bytes param3)
{
OnTypedServer_Rpc(param1, param2);
OnTypedServer_Rpc(param1, param2, param3);
}
}
@@ -87,13 +88,16 @@ namespace Unity.Netcode.RuntimeTests
hasReceivedClientRpcLocally = true;
};
serverClientRpcTestNB.OnTypedServer_Rpc += (param1, param2) =>
var str = new FixedString32Bytes("abcdefg");
serverClientRpcTestNB.OnTypedServer_Rpc += (param1, param2, param3) =>
{
Debug.Log("TypedServerRpc received on server object");
Assert.AreEqual(param1, vector3);
Assert.AreEqual(param2.Length, vector3s.Length);
Assert.AreEqual(param2[0], vector3s[0]);
Assert.AreEqual(param2[1], vector3s[1]);
Assert.AreEqual(param3, str);
hasReceivedTypedServerRpc = true;
};
@@ -101,7 +105,7 @@ namespace Unity.Netcode.RuntimeTests
localClienRpcTestNB.MyServerRpc(m_ClientNetworkManagers[0].LocalClientId);
// Send TypedServerRpc
localClienRpcTestNB.MyTypedServerRpc(vector3, vector3s);
localClienRpcTestNB.MyTypedServerRpc(vector3, vector3s, str);
// Send ClientRpc
serverClientRpcTestNB.MyClientRpc();

View File

@@ -57,6 +57,9 @@ namespace Unity.Netcode.RuntimeTests
Assert.AreEqual(testNetworkBehaviour, testNetworkBehaviour.RpcReceivedBehaviour);
}
[UnityTest]
public IEnumerator TestRpcImplicitNetworkBehaviour()
{
@@ -149,4 +152,44 @@ namespace Unity.Netcode.RuntimeTests
NetworkManagerHelper.StartNetworkManager(out _);
}
}
/// <summary>
/// Integration tests for NetworkBehaviourReference
/// </summary>
public class NetworkBehaviourReferenceIntegrationTests : NetcodeIntegrationTest
{
protected override int NumberOfClients => 1;
internal class FakeMissingComponent : NetworkBehaviour
{
}
internal class TestAddedComponent : NetworkBehaviour
{
}
protected override void OnCreatePlayerPrefab()
{
m_PlayerPrefab.AddComponent<TestAddedComponent>();
base.OnCreatePlayerPrefab();
}
/// <summary>
/// This test validates that if a component does not exist the NetworkBehaviourReference will not throw an
/// invalid cast exception.
/// (It is a full integration test to assure the NetworkObjects are spawned)
/// </summary>
[UnityTest]
public IEnumerator TestTryGetWithAndWithOutExistingComponent()
{
var networkBehaviourReference = new NetworkBehaviourReference(m_ClientNetworkManagers[0].LocalClient.PlayerObject.GetComponent<TestAddedComponent>());
var missingComponent = (FakeMissingComponent)null;
var testBehaviour = (TestAddedComponent)null;
Assert.IsFalse(networkBehaviourReference.TryGet(out missingComponent));
Assert.IsTrue(networkBehaviourReference.TryGet(out testBehaviour));
yield return null;
}
}
}

View File

@@ -145,6 +145,7 @@ namespace Unity.Netcode.RuntimeTests
// Wait for connection on client and server side
yield return WaitForClientsConnectedOrTimeOut();
AssertOnTimeout($"Timed-out waiting for all clients to connect!");
}
private readonly struct NetworkTimeState : IEquatable<NetworkTimeState>

View File

@@ -33,11 +33,7 @@ namespace Unity.Netcode.RuntimeTests
// Move the nested object on the server
if (IsMoving)
{
var y = Time.realtimeSinceStartup;
while (y > 10.0f)
{
y -= 10.0f;
}
var y = Time.realtimeSinceStartup % 10.0f;
// change the space between local and global every second
GetComponent<NetworkTransform>().InLocalSpace = ((int)y % 2 == 0);
@@ -119,6 +115,7 @@ namespace Unity.Netcode.RuntimeTests
baseObject.GetComponent<TransformInterpolationObject>().IsFixed = true;
spawnedObject.GetComponent<TransformInterpolationObject>().IsMoving = true;
spawnedObject.GetComponent<NetworkTransform>().SetMaxInterpolationBound(1.0f);
const float maxPlacementError = 0.01f;
@@ -131,6 +128,7 @@ namespace Unity.Netcode.RuntimeTests
yield return new WaitForSeconds(0.01f);
}
m_SpawnedObjectOnClient.GetComponent<NetworkTransform>().SetMaxInterpolationBound(1.0f);
m_SpawnedObjectOnClient.GetComponent<TransformInterpolationObject>().CheckPosition = true;
// Test that interpolation works correctly for 10 seconds

View File

@@ -2,22 +2,22 @@
"name": "com.unity.netcode.gameobjects",
"displayName": "Netcode for GameObjects",
"description": "Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.",
"version": "1.0.0-pre.9",
"version": "1.0.0",
"unity": "2020.3",
"dependencies": {
"com.unity.nuget.mono-cecil": "1.10.1",
"com.unity.transport": "1.0.0"
"com.unity.transport": "1.1.0"
},
"_upm": {
"changelog": "### Fixed\n\n- Fixed Hosting again after failing to host now works correctly (#1938)\n- Fixed NetworkManager to cleanup connected client lists after stopping (#1945)\n- Fixed NetworkHide followed by NetworkShow on the same frame works correctly (#1940)"
"changelog": "### Changed\n\n- Changed version to 1.0.0. (#2046)"
},
"upmCi": {
"footprint": "ed33b1bdf8c343399eadd7640f4e9caa4ed07776"
"footprint": "382d762a40cdcb42ebaf495e373effb00362baf1"
},
"repository": {
"url": "https://github.com/Unity-Technologies/com.unity.netcode.gameobjects.git",
"type": "git",
"revision": "cdca1eb844973e9f314f6a263bb4c0497d9e396f"
"revision": "fddb7cd920e1db9e49d44846d7121e38f59bd137"
},
"samples": [
{