com.unity.netcode.gameobjects@1.9.1
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.9.1] - 2024-04-18 ### Added - Added AnticipatedNetworkVariable<T>, which adds support for client anticipation of NetworkVariable values, allowing for more responsive gameplay (#2820) - Added AnticipatedNetworkTransform, which adds support for client anticipation of NetworkTransforms (#2820) - Added NetworkVariableBase.ExceedsDirtinessThreshold to allow network variables to throttle updates by only sending updates when the difference between the current and previous values exceeds a threshold. (This is exposed in NetworkVariable<T> with the callback NetworkVariable<T>.CheckExceedsDirtinessThreshold) (#2820) - Added NetworkVariableUpdateTraits, which add additional throttling support: MinSecondsBetweenUpdates will prevent the NetworkVariable from sending updates more often than the specified time period (even if it exceeds the dirtiness threshold), while MaxSecondsBetweenUpdates will force a dirty NetworkVariable to send an update after the specified time period even if it has not yet exceeded the dirtiness threshold. (#2820) - Added virtual method NetworkVariableBase.OnInitialize() which can be used by NetworkVariable subclasses to add initialization code (#2820) - Added virtual method NetworkVariableBase.Update(), which is called once per frame to support behaviors such as interpolation between an anticipated value and an authoritative one. (#2820) - Added NetworkTime.TickWithPartial, which represents the current tick as a double that includes the fractional/partial tick value. (#2820) - Added NetworkTickSystem.AnticipationTick, which can be helpful with implementation of client anticipation. This value represents the tick the current local client was at at the beginning of the most recent network round trip, which enables it to correlate server update ticks with the client tick that may have triggered them. (#2820) - `NetworkVariable` now includes built-in support for `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, and `Dictionary` (#2813) - `NetworkVariable` now includes delta compression for collection values (`NativeList`, `NativeArray`, `NativeHashSet`, `NativeHashMap`, `List`, `HashSet`, `Dictionary`, and `FixedString` types) to save bandwidth by only sending the values that changed. (Note: For `NativeList`, `NativeArray`, and `List`, this algorithm works differently than that used in `NetworkList`. This algorithm will use less bandwidth for "set" and "add" operations, but `NetworkList` is more bandwidth-efficient if you are performing frequent "insert" operations.) (#2813) - `UserNetworkVariableSerialization` now has optional callbacks for `WriteDelta` and `ReadDelta`. If both are provided, they will be used for all serialization operations on NetworkVariables of that type except for the first one for each client. If either is missing, the existing `Write` and `Read` will always be used. (#2813) - Network variables wrapping `INetworkSerializable` types can perform delta serialization by setting `UserNetworkVariableSerialization<T>.WriteDelta` and `UserNetworkVariableSerialization<T>.ReadDelta` for those types. The built-in `INetworkSerializable` serializer will continue to be used for all other serialization operations, but if those callbacks are set, it will call into them on all but the initial serialization to perform delta serialization. (This could be useful if you have a large struct where most values do not change regularly and you want to send only the fields that did change.) (#2813) ### Fixed - Fixed issue where NetworkTransformEditor would throw and exception if you excluded the physics package. (#2871) - Fixed issue where `NetworkTransform` could not properly synchronize its base position when using half float precision. (#2845) - Fixed issue where the host was not invoking `OnClientDisconnectCallback` for its own local client when internally shutting down. (#2822) - Fixed issue where NetworkTransform could potentially attempt to "unregister" a named message prior to it being registered. (#2807) - Fixed issue where in-scene placed `NetworkObject`s with complex nested children `NetworkObject`s (more than one child in depth) would not synchronize properly if WorldPositionStays was set to true. (#2796) ### Changed - Changed `NetworkObjectReference` and `NetworkBehaviourReference` to allow null references when constructing and serializing. (#2874) - Changed `NetworkAnimator` no longer requires the `Animator` component to exist on the same `GameObject`. (#2872) - Changed `NetworkTransform` to now use `NetworkTransformMessage` as opposed to named messages for NetworkTransformState updates. (#2810) - Changed `CustomMessageManager` so it no longer attempts to register or "unregister" a null or empty string and will log an error if this condition occurs. (#2807)
This commit is contained in:
392
Runtime/NetworkVariable/AnticipatedNetworkVariable.cs
Normal file
392
Runtime/NetworkVariable/AnticipatedNetworkVariable.cs
Normal file
@@ -0,0 +1,392 @@
|
||||
using System;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Unity.Netcode
|
||||
{
|
||||
|
||||
public enum StaleDataHandling
|
||||
{
|
||||
Ignore,
|
||||
Reanticipate
|
||||
}
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
/// <summary>
|
||||
/// A variable that can be synchronized over the network.
|
||||
/// This version supports basic client anticipation - the client can set a value on the belief that the server
|
||||
/// will update it to reflect the same value in a future update (i.e., as the result of an RPC call).
|
||||
/// This value can then be adjusted as new updates from the server come in, in three basic modes:
|
||||
///
|
||||
/// <list type="bullet">
|
||||
///
|
||||
/// <item><b>Snap:</b> In this mode (with <see cref="StaleDataHandling"/> set to
|
||||
/// <see cref="Netcode.StaleDataHandling.Ignore"/> and no <see cref="NetworkBehaviour.OnReanticipate"/> callback),
|
||||
/// the moment a more up-to-date value is received from the authority, it will simply replace the anticipated value,
|
||||
/// resulting in a "snap" to the new value if it is different from the anticipated value.</item>
|
||||
///
|
||||
/// <item><b>Smooth:</b> In this mode (with <see cref="StaleDataHandling"/> set to
|
||||
/// <see cref="Netcode.StaleDataHandling.Ignore"/> and an <see cref="NetworkBehaviour.OnReanticipate"/> callback that calls
|
||||
/// <see cref="Smooth"/> from the anticipated value to the authority value with an appropriate
|
||||
/// <see cref="Mathf.Lerp"/>-style smooth function), when a more up-to-date value is received from the authority,
|
||||
/// it will interpolate over time from an incorrect anticipated value to the correct authoritative value.</item>
|
||||
///
|
||||
/// <item><b>Constant Reanticipation:</b> In this mode (with <see cref="StaleDataHandling"/> set to
|
||||
/// <see cref="Netcode.StaleDataHandling.Reanticipate"/> and an <see cref="NetworkBehaviour.OnReanticipate"/> that calculates a
|
||||
/// new anticipated value based on the current authoritative value), when a more up-to-date value is received from
|
||||
/// the authority, user code calculates a new anticipated value, possibly calling <see cref="Smooth"/> to interpolate
|
||||
/// between the previous anticipation and the new anticipation. This is useful for values that change frequently and
|
||||
/// need to constantly be re-evaluated, as opposed to values that change only in response to user action and simply
|
||||
/// need a one-time anticipation when the user performs that action.</item>
|
||||
///
|
||||
/// </list>
|
||||
///
|
||||
/// Note that these three modes may be combined. For example, if an <see cref="NetworkBehaviour.OnReanticipate"/> callback
|
||||
/// does not call either <see cref="Smooth"/> or <see cref="Anticipate"/>, the result will be a snap to the
|
||||
/// authoritative value, enabling for a callback that may conditionally call <see cref="Smooth"/> when the
|
||||
/// difference between the anticipated and authoritative values is within some threshold, but fall back to
|
||||
/// snap behavior if the difference is too large.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">the unmanaged type for <see cref="NetworkVariable{T}"/> </typeparam>
|
||||
#pragma warning restore IDE0001
|
||||
[Serializable]
|
||||
[GenerateSerializationForGenericParameter(0)]
|
||||
public class AnticipatedNetworkVariable<T> : NetworkVariableBase
|
||||
{
|
||||
[SerializeField]
|
||||
private NetworkVariable<T> m_AuthoritativeValue;
|
||||
private T m_AnticipatedValue;
|
||||
private T m_PreviousAnticipatedValue;
|
||||
private ulong m_LastAuthorityUpdateCounter = 0;
|
||||
private ulong m_LastAnticipationCounter = 0;
|
||||
private bool m_IsDisposed = false;
|
||||
private bool m_SettingAuthoritativeValue = false;
|
||||
|
||||
private T m_SmoothFrom;
|
||||
private T m_SmoothTo;
|
||||
private float m_SmoothDuration;
|
||||
private float m_CurrentSmoothTime;
|
||||
private bool m_HasSmoothValues;
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
/// <summary>
|
||||
/// Defines what the behavior should be if we receive a value from the server with an earlier associated
|
||||
/// time value than the anticipation time value.
|
||||
/// <br/><br/>
|
||||
/// If this is <see cref="Netcode.StaleDataHandling.Ignore"/>, the stale data will be ignored and the authoritative
|
||||
/// value will not replace the anticipated value until the anticipation time is reached. <see cref="OnAuthoritativeValueChanged"/>
|
||||
/// and <see cref="NetworkBehaviour.OnReanticipate"/> will also not be invoked for this stale data.
|
||||
/// <br/><br/>
|
||||
/// If this is <see cref="Netcode.StaleDataHandling.Reanticipate"/>, the stale data will replace the anticipated data and
|
||||
/// <see cref="OnAuthoritativeValueChanged"/> and <see cref="NetworkBehaviour.OnReanticipate"/> will be invoked.
|
||||
/// In this case, the authoritativeTime value passed to <see cref="NetworkBehaviour.OnReanticipate"/> will be lower than
|
||||
/// the anticipationTime value, and that callback can be used to calculate a new anticipated value.
|
||||
/// </summary>
|
||||
#pragma warning restore IDE0001
|
||||
public StaleDataHandling StaleDataHandling;
|
||||
|
||||
public delegate void OnAuthoritativeValueChangedDelegate(AnticipatedNetworkVariable<T> variable, in T previousValue, in T newValue);
|
||||
|
||||
/// <summary>
|
||||
/// Invoked any time the authoritative value changes, even when the data is stale or has been changed locally.
|
||||
/// </summary>
|
||||
public OnAuthoritativeValueChangedDelegate OnAuthoritativeValueChanged = null;
|
||||
|
||||
/// <summary>
|
||||
/// Determines if the difference between the last serialized value and the current value is large enough
|
||||
/// to serialize it again.
|
||||
/// </summary>
|
||||
public event NetworkVariable<T>.CheckExceedsDirtinessThresholdDelegate CheckExceedsDirtinessThreshold
|
||||
{
|
||||
add => m_AuthoritativeValue.CheckExceedsDirtinessThreshold += value;
|
||||
remove => m_AuthoritativeValue.CheckExceedsDirtinessThreshold -= value;
|
||||
}
|
||||
|
||||
private class AnticipatedObject : IAnticipatedObject
|
||||
{
|
||||
public AnticipatedNetworkVariable<T> Variable;
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Variable.Update();
|
||||
}
|
||||
|
||||
public void ResetAnticipation()
|
||||
{
|
||||
Variable.ShouldReanticipate = false;
|
||||
}
|
||||
|
||||
public NetworkObject OwnerObject => Variable.m_NetworkBehaviour.NetworkObject;
|
||||
}
|
||||
|
||||
private AnticipatedObject m_AnticipatedObject;
|
||||
|
||||
public override void OnInitialize()
|
||||
{
|
||||
m_AuthoritativeValue.Initialize(m_NetworkBehaviour);
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AuthoritativeValue.Value, ref m_AnticipatedValue);
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
|
||||
if (m_NetworkBehaviour != null && m_NetworkBehaviour.NetworkManager != null && m_NetworkBehaviour.NetworkManager.AnticipationSystem != null)
|
||||
{
|
||||
m_AnticipatedObject = new AnticipatedObject { Variable = this };
|
||||
m_NetworkBehaviour.NetworkManager.AnticipationSystem.AllAnticipatedObjects.Add(m_AnticipatedObject);
|
||||
}
|
||||
}
|
||||
|
||||
public override bool ExceedsDirtinessThreshold()
|
||||
{
|
||||
return m_AuthoritativeValue.ExceedsDirtinessThreshold();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the current value for the variable.
|
||||
/// This is the "display value" for this variable, and is affected by <see cref="Anticipate"/> and
|
||||
/// <see cref="Smooth"/>, as well as by updates from the authority, depending on <see cref="StaleDataHandling"/>
|
||||
/// and the behavior of any <see cref="NetworkBehaviour.OnReanticipate"/> callbacks.
|
||||
/// <br /><br />
|
||||
/// When a server update arrives, this value will be overwritten
|
||||
/// by the new server value (unless stale data handling is set
|
||||
/// to "Ignore" and the update is determined to be stale).
|
||||
/// This value will be duplicated in
|
||||
/// <see cref="PreviousAnticipatedValue"/>, which
|
||||
/// will NOT be overwritten in server updates.
|
||||
/// </summary>
|
||||
public T Value => m_AnticipatedValue;
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether this variable currently needs
|
||||
/// reanticipation. If this is true, the anticipated value
|
||||
/// has been overwritten by the authoritative value from the
|
||||
/// server; the previous anticipated value is stored in <see cref="PreviousAnticipatedState"/>
|
||||
/// </summary>
|
||||
public bool ShouldReanticipate
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Holds the most recent anticipated value, whatever was
|
||||
/// most recently set using <see cref="Anticipate"/>. Unlike
|
||||
/// <see cref="Value"/>, this does not get overwritten
|
||||
/// when a server update arrives.
|
||||
/// </summary>
|
||||
public T PreviousAnticipatedValue => m_PreviousAnticipatedValue;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current value of the variable on the expectation that the authority will set the variable
|
||||
/// to the same value within one network round trip (i.e., in response to an RPC).
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
public void Anticipate(T value)
|
||||
{
|
||||
if (m_NetworkBehaviour.NetworkManager.ShutdownInProgress || !m_NetworkBehaviour.NetworkManager.IsListening)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_SmoothDuration = 0;
|
||||
m_CurrentSmoothTime = 0;
|
||||
m_LastAnticipationCounter = m_NetworkBehaviour.NetworkManager.AnticipationSystem.AnticipationCounter;
|
||||
m_AnticipatedValue = value;
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
|
||||
if (CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
|
||||
{
|
||||
AuthoritativeValue = value;
|
||||
}
|
||||
}
|
||||
|
||||
#pragma warning disable IDE0001
|
||||
/// <summary>
|
||||
/// Retrieves or sets the underlying authoritative value.
|
||||
/// Note that only a client or server with write permissions to this variable may set this value.
|
||||
/// When this variable has been anticipated, this value will alawys return the most recent authoritative
|
||||
/// state, which is updated even if <see cref="StaleDataHandling"/> is <see cref="Netcode.StaleDataHandling.Ignore"/>.
|
||||
/// </summary>
|
||||
#pragma warning restore IDE0001
|
||||
public T AuthoritativeValue
|
||||
{
|
||||
get => m_AuthoritativeValue.Value;
|
||||
set
|
||||
{
|
||||
m_SettingAuthoritativeValue = true;
|
||||
try
|
||||
{
|
||||
m_AuthoritativeValue.Value = value;
|
||||
m_AnticipatedValue = value;
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_SettingAuthoritativeValue = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A function to interpolate between two values based on a percentage.
|
||||
/// See <see cref="Mathf.Lerp"/>, <see cref="Vector3.Lerp"/>, <see cref="Vector3.Slerp"/>, and so on
|
||||
/// for examples.
|
||||
/// </summary>
|
||||
public delegate T SmoothDelegate(T authoritativeValue, T anticipatedValue, float amount);
|
||||
|
||||
private SmoothDelegate m_SmoothDelegate = null;
|
||||
|
||||
public AnticipatedNetworkVariable(T value = default,
|
||||
StaleDataHandling staleDataHandling = StaleDataHandling.Ignore)
|
||||
: base()
|
||||
{
|
||||
StaleDataHandling = staleDataHandling;
|
||||
m_AuthoritativeValue = new NetworkVariable<T>(value)
|
||||
{
|
||||
OnValueChanged = OnValueChangedInternal
|
||||
};
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (m_CurrentSmoothTime < m_SmoothDuration)
|
||||
{
|
||||
m_CurrentSmoothTime += m_NetworkBehaviour.NetworkManager.RealTimeProvider.DeltaTime;
|
||||
var pct = math.min(m_CurrentSmoothTime / m_SmoothDuration, 1f);
|
||||
m_AnticipatedValue = m_SmoothDelegate(m_SmoothFrom, m_SmoothTo, pct);
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (m_IsDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_NetworkBehaviour != null && m_NetworkBehaviour.NetworkManager != null && m_NetworkBehaviour.NetworkManager.AnticipationSystem != null)
|
||||
{
|
||||
if (m_AnticipatedObject != null)
|
||||
{
|
||||
m_NetworkBehaviour.NetworkManager.AnticipationSystem.AllAnticipatedObjects.Remove(m_AnticipatedObject);
|
||||
m_NetworkBehaviour.NetworkManager.AnticipationSystem.ObjectsToReanticipate.Remove(m_AnticipatedObject);
|
||||
m_AnticipatedObject = null;
|
||||
}
|
||||
}
|
||||
|
||||
m_IsDisposed = true;
|
||||
|
||||
m_AuthoritativeValue.Dispose();
|
||||
if (m_AnticipatedValue is IDisposable anticipatedValueDisposable)
|
||||
{
|
||||
anticipatedValueDisposable.Dispose();
|
||||
}
|
||||
|
||||
m_AnticipatedValue = default;
|
||||
if (m_PreviousAnticipatedValue is IDisposable previousValueDisposable)
|
||||
{
|
||||
previousValueDisposable.Dispose();
|
||||
m_PreviousAnticipatedValue = default;
|
||||
}
|
||||
|
||||
if (m_HasSmoothValues)
|
||||
{
|
||||
if (m_SmoothFrom is IDisposable smoothFromDisposable)
|
||||
{
|
||||
smoothFromDisposable.Dispose();
|
||||
m_SmoothFrom = default;
|
||||
}
|
||||
if (m_SmoothTo is IDisposable smoothToDisposable)
|
||||
{
|
||||
smoothToDisposable.Dispose();
|
||||
m_SmoothTo = default;
|
||||
}
|
||||
|
||||
m_HasSmoothValues = false;
|
||||
}
|
||||
}
|
||||
|
||||
~AnticipatedNetworkVariable()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
private void OnValueChangedInternal(T previousValue, T newValue)
|
||||
{
|
||||
if (!m_SettingAuthoritativeValue)
|
||||
{
|
||||
m_LastAuthorityUpdateCounter = m_NetworkBehaviour.NetworkManager.AnticipationSystem.LastAnticipationAck;
|
||||
if (StaleDataHandling == StaleDataHandling.Ignore && m_LastAnticipationCounter > m_LastAuthorityUpdateCounter)
|
||||
{
|
||||
// Keep the anticipated value unchanged because it is more recent than the authoritative one.
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ShouldReanticipate = true;
|
||||
m_NetworkBehaviour.NetworkManager.AnticipationSystem.ObjectsToReanticipate.Add(m_AnticipatedObject);
|
||||
}
|
||||
|
||||
NetworkVariableSerialization<T>.Duplicate(AuthoritativeValue, ref m_AnticipatedValue);
|
||||
|
||||
m_SmoothDuration = 0;
|
||||
m_CurrentSmoothTime = 0;
|
||||
OnAuthoritativeValueChanged?.Invoke(this, previousValue, newValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interpolate this variable from <see cref="from"/> to <see cref="to"/> over <see cref="durationSeconds"/> of
|
||||
/// real time. The duration uses <see cref="Time.deltaTime"/>, so it is affected by <see cref="Time.timeScale"/>.
|
||||
/// </summary>
|
||||
/// <param name="from"></param>
|
||||
/// <param name="to"></param>
|
||||
/// <param name="durationSeconds"></param>
|
||||
/// <param name="how"></param>
|
||||
public void Smooth(in T from, in T to, float durationSeconds, SmoothDelegate how)
|
||||
{
|
||||
if (durationSeconds <= 0)
|
||||
{
|
||||
NetworkVariableSerialization<T>.Duplicate(to, ref m_AnticipatedValue);
|
||||
m_SmoothDuration = 0;
|
||||
m_CurrentSmoothTime = 0;
|
||||
m_SmoothDelegate = null;
|
||||
return;
|
||||
}
|
||||
NetworkVariableSerialization<T>.Duplicate(from, ref m_AnticipatedValue);
|
||||
NetworkVariableSerialization<T>.Duplicate(from, ref m_SmoothFrom);
|
||||
NetworkVariableSerialization<T>.Duplicate(to, ref m_SmoothTo);
|
||||
m_SmoothDuration = durationSeconds;
|
||||
m_CurrentSmoothTime = 0;
|
||||
m_SmoothDelegate = how;
|
||||
m_HasSmoothValues = true;
|
||||
}
|
||||
|
||||
public override bool IsDirty()
|
||||
{
|
||||
return m_AuthoritativeValue.IsDirty();
|
||||
}
|
||||
|
||||
public override void ResetDirty()
|
||||
{
|
||||
m_AuthoritativeValue.ResetDirty();
|
||||
}
|
||||
|
||||
public override void WriteDelta(FastBufferWriter writer)
|
||||
{
|
||||
m_AuthoritativeValue.WriteDelta(writer);
|
||||
}
|
||||
|
||||
public override void WriteField(FastBufferWriter writer)
|
||||
{
|
||||
m_AuthoritativeValue.WriteField(writer);
|
||||
}
|
||||
|
||||
public override void ReadField(FastBufferReader reader)
|
||||
{
|
||||
m_AuthoritativeValue.ReadField(reader);
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AuthoritativeValue.Value, ref m_AnticipatedValue);
|
||||
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
|
||||
}
|
||||
|
||||
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
|
||||
{
|
||||
m_AuthoritativeValue.ReadDelta(reader, keepDirtyDelta);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 10f5188736b742d1993a2aad46a03e78
|
||||
timeCreated: 1705595868
|
||||
746
Runtime/NetworkVariable/CollectionSerializationUtility.cs
Normal file
746
Runtime/NetworkVariable/CollectionSerializationUtility.cs
Normal file
@@ -0,0 +1,746 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
using Unity.Mathematics;
|
||||
|
||||
namespace Unity.Netcode
|
||||
{
|
||||
internal static class CollectionSerializationUtility
|
||||
{
|
||||
public static void WriteNativeArrayDelta<T>(FastBufferWriter writer, ref NativeArray<T> value, ref NativeArray<T> previousValue) where T : unmanaged
|
||||
{
|
||||
// This bit vector serializes the list of which fields have changed using 1 bit per field.
|
||||
// This will always be 1 bit per field of the whole array (rounded up to the nearest 8 bits)
|
||||
// even if there is only one change, so as compared to serializing the index with each item,
|
||||
// this will use more bandwidth when the overall bandwidth usage is small and the array is large,
|
||||
// but less when the overall bandwidth usage is large. So it optimizes for the worst case while accepting
|
||||
// some reduction in efficiency in the best case.
|
||||
using var changes = new ResizableBitVector(Allocator.Temp);
|
||||
int minLength = math.min(value.Length, previousValue.Length);
|
||||
var numChanges = 0;
|
||||
// Iterate the array, checking which values have changed and marking that in the bit vector
|
||||
for (var i = 0; i < minLength; ++i)
|
||||
{
|
||||
var val = value[i];
|
||||
var prevVal = previousValue[i];
|
||||
if (!NetworkVariableSerialization<T>.AreEqual(ref val, ref prevVal))
|
||||
{
|
||||
++numChanges;
|
||||
changes.Set(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Mark any newly added items as well
|
||||
// We don't need to mark removed items because they are captured by serializing the length
|
||||
for (var i = previousValue.Length; i < value.Length; ++i)
|
||||
{
|
||||
++numChanges;
|
||||
changes.Set(i);
|
||||
}
|
||||
|
||||
// If the size of serializing the dela is greater than the size of serializing the whole array (i.e.,
|
||||
// because almost the entire array has changed and the overhead of the change set increases bandwidth),
|
||||
// then we just do a normal full serialization instead of a delta.
|
||||
if (changes.GetSerializedSize() + FastBufferWriter.GetWriteSize<T>() * numChanges > FastBufferWriter.GetWriteSize<T>() * value.Length)
|
||||
{
|
||||
// 1 = full serialization
|
||||
writer.WriteByteSafe(1);
|
||||
writer.WriteValueSafe(value);
|
||||
return;
|
||||
}
|
||||
// 0 = delta serialization
|
||||
writer.WriteByte(0);
|
||||
// Write the length, which will be used on the read side to resize the array
|
||||
BytePacker.WriteValuePacked(writer, value.Length);
|
||||
writer.WriteValueSafe(changes);
|
||||
unsafe
|
||||
{
|
||||
var ptr = (T*)value.GetUnsafePtr();
|
||||
var prevPtr = (T*)previousValue.GetUnsafePtr();
|
||||
for (int i = 0; i < value.Length; ++i)
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
if (i < previousValue.Length)
|
||||
{
|
||||
// If we have an item in the previous array for this index, we can do nested deltas!
|
||||
NetworkVariableSerialization<T>.WriteDelta(writer, ref ptr[i], ref prevPtr[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not, just write it normally
|
||||
NetworkVariableSerialization<T>.Write(writer, ref ptr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void ReadNativeArrayDelta<T>(FastBufferReader reader, ref NativeArray<T> value) where T : unmanaged
|
||||
{
|
||||
// 1 = full serialization, 0 = delta serialization
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full == 1)
|
||||
{
|
||||
// If we're doing full serialization, we fall back on reading the whole array.
|
||||
value.Dispose();
|
||||
reader.ReadValueSafe(out value, Allocator.Persistent);
|
||||
return;
|
||||
}
|
||||
// If not, first read the length and the change bits
|
||||
ByteUnpacker.ReadValuePacked(reader, out int length);
|
||||
var changes = new ResizableBitVector(Allocator.Temp);
|
||||
using var toDispose = changes;
|
||||
{
|
||||
reader.ReadNetworkSerializableInPlace(ref changes);
|
||||
|
||||
// If the length has changed, we need to resize.
|
||||
// NativeArray is not resizeable, so we have to dispose and allocate a new one.
|
||||
var previousLength = value.Length;
|
||||
if (length != value.Length)
|
||||
{
|
||||
var newArray = new NativeArray<T>(length, Allocator.Persistent);
|
||||
unsafe
|
||||
{
|
||||
UnsafeUtility.MemCpy(newArray.GetUnsafePtr(), value.GetUnsafePtr(), math.min(newArray.Length * sizeof(T), value.Length * sizeof(T)));
|
||||
}
|
||||
value.Dispose();
|
||||
value = newArray;
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
var ptr = (T*)value.GetUnsafePtr();
|
||||
for (var i = 0; i < value.Length; ++i)
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
if (i < previousLength)
|
||||
{
|
||||
// If we have an item to read a delta into, read it as a delta
|
||||
NetworkVariableSerialization<T>.ReadDelta(reader, ref ptr[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not, read as a standard element
|
||||
NetworkVariableSerialization<T>.Read(reader, ref ptr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void WriteListDelta<T>(FastBufferWriter writer, ref List<T> value, ref List<T> previousValue)
|
||||
{
|
||||
// Lists can be null, so we have to handle that case.
|
||||
// We do that by marking this as a full serialization and using the existing null handling logic
|
||||
// in NetworkVariableSerialization<List<T>>
|
||||
if (value == null || previousValue == null)
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
NetworkVariableSerialization<List<T>>.Write(writer, ref value);
|
||||
return;
|
||||
}
|
||||
// This bit vector serializes the list of which fields have changed using 1 bit per field.
|
||||
// This will always be 1 bit per field of the whole array (rounded up to the nearest 8 bits)
|
||||
// even if there is only one change, so as compared to serializing the index with each item,
|
||||
// this will use more bandwidth when the overall bandwidth usage is small and the array is large,
|
||||
// but less when the overall bandwidth usage is large. So it optimizes for the worst case while accepting
|
||||
// some reduction in efficiency in the best case.
|
||||
using var changes = new ResizableBitVector(Allocator.Temp);
|
||||
int minLength = math.min(value.Count, previousValue.Count);
|
||||
var numChanges = 0;
|
||||
// Iterate the list, checking which values have changed and marking that in the bit vector
|
||||
for (var i = 0; i < minLength; ++i)
|
||||
{
|
||||
var val = value[i];
|
||||
var prevVal = previousValue[i];
|
||||
if (!NetworkVariableSerialization<T>.AreEqual(ref val, ref prevVal))
|
||||
{
|
||||
++numChanges;
|
||||
changes.Set(i);
|
||||
}
|
||||
}
|
||||
|
||||
// Mark any newly added items as well
|
||||
// We don't need to mark removed items because they are captured by serializing the length
|
||||
for (var i = previousValue.Count; i < value.Count; ++i)
|
||||
{
|
||||
++numChanges;
|
||||
changes.Set(i);
|
||||
}
|
||||
|
||||
// If the size of serializing the dela is greater than the size of serializing the whole array (i.e.,
|
||||
// because almost the entire array has changed and the overhead of the change set increases bandwidth),
|
||||
// then we just do a normal full serialization instead of a delta.
|
||||
// In the case of List<T>, it's difficult to know exactly what the serialized size is going to be before
|
||||
// we serialize it, so we fudge it.
|
||||
if (numChanges >= value.Count * 0.9)
|
||||
{
|
||||
// 1 = full serialization
|
||||
writer.WriteByteSafe(1);
|
||||
NetworkVariableSerialization<List<T>>.Write(writer, ref value);
|
||||
return;
|
||||
}
|
||||
|
||||
// 0 = delta serialization
|
||||
writer.WriteByteSafe(0);
|
||||
// Write the length, which will be used on the read side to resize the list
|
||||
BytePacker.WriteValuePacked(writer, value.Count);
|
||||
writer.WriteValueSafe(changes);
|
||||
for (int i = 0; i < value.Count; ++i)
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
var reffable = value[i];
|
||||
if (i < previousValue.Count)
|
||||
{
|
||||
// If we have an item in the previous array for this index, we can do nested deltas!
|
||||
var prevReffable = previousValue[i];
|
||||
NetworkVariableSerialization<T>.WriteDelta(writer, ref reffable, ref prevReffable);
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not, just write it normally.
|
||||
NetworkVariableSerialization<T>.Write(writer, ref reffable);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void ReadListDelta<T>(FastBufferReader reader, ref List<T> value)
|
||||
{
|
||||
// 1 = full serialization, 0 = delta serialization
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full == 1)
|
||||
{
|
||||
// If we're doing full serialization, we fall back on reading the whole list.
|
||||
NetworkVariableSerialization<List<T>>.Read(reader, ref value);
|
||||
return;
|
||||
}
|
||||
// If not, first read the length and the change bits
|
||||
ByteUnpacker.ReadValuePacked(reader, out int length);
|
||||
var changes = new ResizableBitVector(Allocator.Temp);
|
||||
using var toDispose = changes;
|
||||
{
|
||||
reader.ReadNetworkSerializableInPlace(ref changes);
|
||||
|
||||
// If the list shrank, we need to resize it down.
|
||||
// List<T> has no method to reserve space for future elements,
|
||||
// so if we have to grow it, we just do that using Add() below.
|
||||
if (length < value.Count)
|
||||
{
|
||||
value.RemoveRange(length, value.Count - length);
|
||||
}
|
||||
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
if (i < value.Count)
|
||||
{
|
||||
// If we have an item to read a delta into, read it as a delta
|
||||
T item = value[i];
|
||||
NetworkVariableSerialization<T>.ReadDelta(reader, ref item);
|
||||
value[i] = item;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If not, just read it as a standard item.
|
||||
T item = default;
|
||||
NetworkVariableSerialization<T>.Read(reader, ref item);
|
||||
value.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// For HashSet and Dictionary, we need to have some local space to hold lists we need to serialize.
|
||||
// We don't want to do allocations all the time and we know each one needs a maximum of three lists,
|
||||
// so we're going to keep static lists that we can reuse in these methods.
|
||||
private static class ListCache<T>
|
||||
{
|
||||
private static List<T> s_AddedList = new List<T>();
|
||||
private static List<T> s_RemovedList = new List<T>();
|
||||
private static List<T> s_ChangedList = new List<T>();
|
||||
|
||||
public static List<T> GetAddedList()
|
||||
{
|
||||
s_AddedList.Clear();
|
||||
return s_AddedList;
|
||||
}
|
||||
public static List<T> GetRemovedList()
|
||||
{
|
||||
s_RemovedList.Clear();
|
||||
return s_RemovedList;
|
||||
}
|
||||
public static List<T> GetChangedList()
|
||||
{
|
||||
s_ChangedList.Clear();
|
||||
return s_ChangedList;
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteHashSetDelta<T>(FastBufferWriter writer, ref HashSet<T> value, ref HashSet<T> previousValue) where T : IEquatable<T>
|
||||
{
|
||||
// HashSets can be null, so we have to handle that case.
|
||||
// We do that by marking this as a full serialization and using the existing null handling logic
|
||||
// in NetworkVariableSerialization<HashSet<T>>
|
||||
if (value == null || previousValue == null)
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
NetworkVariableSerialization<HashSet<T>>.Write(writer, ref value);
|
||||
return;
|
||||
}
|
||||
// No changed array because a set can't have a "changed" element, only added and removed.
|
||||
var added = ListCache<T>.GetAddedList();
|
||||
var removed = ListCache<T>.GetRemovedList();
|
||||
// collect the new elements
|
||||
foreach (var item in value)
|
||||
{
|
||||
if (!previousValue.Contains(item))
|
||||
{
|
||||
added.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
// collect the removed elements
|
||||
foreach (var item in previousValue)
|
||||
{
|
||||
if (!value.Contains(item))
|
||||
{
|
||||
removed.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
// If we've got more changes than total items, we just do a full serialization
|
||||
if (added.Count + removed.Count >= value.Count)
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
NetworkVariableSerialization<HashSet<T>>.Write(writer, ref value);
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteByteSafe(0);
|
||||
// Write out the added and removed arrays.
|
||||
writer.WriteValueSafe(added.Count);
|
||||
for (var i = 0; i < added.Count; ++i)
|
||||
{
|
||||
var item = added[i];
|
||||
NetworkVariableSerialization<T>.Write(writer, ref item);
|
||||
}
|
||||
writer.WriteValueSafe(removed.Count);
|
||||
for (var i = 0; i < removed.Count; ++i)
|
||||
{
|
||||
var item = removed[i];
|
||||
NetworkVariableSerialization<T>.Write(writer, ref item);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReadHashSetDelta<T>(FastBufferReader reader, ref HashSet<T> value) where T : IEquatable<T>
|
||||
{
|
||||
// 1 = full serialization, 0 = delta serialization
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full != 0)
|
||||
{
|
||||
NetworkVariableSerialization<HashSet<T>>.Read(reader, ref value);
|
||||
return;
|
||||
}
|
||||
// Read in the added and removed values
|
||||
reader.ReadValueSafe(out int addedCount);
|
||||
for (var i = 0; i < addedCount; ++i)
|
||||
{
|
||||
T item = default;
|
||||
NetworkVariableSerialization<T>.Read(reader, ref item);
|
||||
value.Add(item);
|
||||
}
|
||||
reader.ReadValueSafe(out int removedCount);
|
||||
for (var i = 0; i < removedCount; ++i)
|
||||
{
|
||||
T item = default;
|
||||
NetworkVariableSerialization<T>.Read(reader, ref item);
|
||||
value.Remove(item);
|
||||
}
|
||||
}
|
||||
public static void WriteDictionaryDelta<TKey, TVal>(FastBufferWriter writer, ref Dictionary<TKey, TVal> value, ref Dictionary<TKey, TVal> previousValue)
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (value == null || previousValue == null)
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
NetworkVariableSerialization<Dictionary<TKey, TVal>>.Write(writer, ref value);
|
||||
return;
|
||||
}
|
||||
var added = ListCache<KeyValuePair<TKey, TVal>>.GetAddedList();
|
||||
var changed = ListCache<KeyValuePair<TKey, TVal>>.GetRemovedList();
|
||||
var removed = ListCache<KeyValuePair<TKey, TVal>>.GetChangedList();
|
||||
// Collect items that have been added or have changed
|
||||
foreach (var item in value)
|
||||
{
|
||||
var val = item.Value;
|
||||
var hasPrevVal = previousValue.TryGetValue(item.Key, out var prevVal);
|
||||
if (!hasPrevVal)
|
||||
{
|
||||
added.Add(item);
|
||||
}
|
||||
else if (!NetworkVariableSerialization<TVal>.AreEqual(ref val, ref prevVal))
|
||||
{
|
||||
changed.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
// collect the items that have been removed
|
||||
foreach (var item in previousValue)
|
||||
{
|
||||
if (!value.ContainsKey(item.Key))
|
||||
{
|
||||
removed.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
// If there are more changes than total values, just do a full serialization
|
||||
if (added.Count + removed.Count + changed.Count >= value.Count)
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
NetworkVariableSerialization<Dictionary<TKey, TVal>>.Write(writer, ref value);
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteByteSafe(0);
|
||||
// Else, write out the added, removed, and changed arrays
|
||||
writer.WriteValueSafe(added.Count);
|
||||
for (var i = 0; i < added.Count; ++i)
|
||||
{
|
||||
(var key, var val) = (added[i].Key, added[i].Value);
|
||||
NetworkVariableSerialization<TKey>.Write(writer, ref key);
|
||||
NetworkVariableSerialization<TVal>.Write(writer, ref val);
|
||||
}
|
||||
writer.WriteValueSafe(removed.Count);
|
||||
for (var i = 0; i < removed.Count; ++i)
|
||||
{
|
||||
var key = removed[i].Key;
|
||||
NetworkVariableSerialization<TKey>.Write(writer, ref key);
|
||||
}
|
||||
writer.WriteValueSafe(changed.Count);
|
||||
for (var i = 0; i < changed.Count; ++i)
|
||||
{
|
||||
(var key, var val) = (changed[i].Key, changed[i].Value);
|
||||
NetworkVariableSerialization<TKey>.Write(writer, ref key);
|
||||
NetworkVariableSerialization<TVal>.Write(writer, ref val);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReadDictionaryDelta<TKey, TVal>(FastBufferReader reader, ref Dictionary<TKey, TVal> value)
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
// 1 = full serialization, 0 = delta serialization
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full != 0)
|
||||
{
|
||||
NetworkVariableSerialization<Dictionary<TKey, TVal>>.Read(reader, ref value);
|
||||
return;
|
||||
}
|
||||
// Added
|
||||
reader.ReadValueSafe(out int length);
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
(TKey key, TVal val) = (default, default);
|
||||
NetworkVariableSerialization<TKey>.Read(reader, ref key);
|
||||
NetworkVariableSerialization<TVal>.Read(reader, ref val);
|
||||
value.Add(key, val);
|
||||
}
|
||||
// Removed
|
||||
reader.ReadValueSafe(out length);
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
TKey key = default;
|
||||
NetworkVariableSerialization<TKey>.Read(reader, ref key);
|
||||
value.Remove(key);
|
||||
}
|
||||
// Changed
|
||||
reader.ReadValueSafe(out length);
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
(TKey key, TVal val) = (default, default);
|
||||
NetworkVariableSerialization<TKey>.Read(reader, ref key);
|
||||
NetworkVariableSerialization<TVal>.Read(reader, ref val);
|
||||
value[key] = val;
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_NETCODE_NATIVE_COLLECTION_SUPPORT
|
||||
public static void WriteNativeListDelta<T>(FastBufferWriter writer, ref NativeList<T> value, ref NativeList<T> previousValue) where T : unmanaged
|
||||
{
|
||||
// See WriteListDelta and WriteNativeArrayDelta to understand most of this. It's basically the same,
|
||||
// just adjusted for the NativeList API
|
||||
using var changes = new ResizableBitVector(Allocator.Temp);
|
||||
int minLength = math.min(value.Length, previousValue.Length);
|
||||
var numChanges = 0;
|
||||
for (var i = 0; i < minLength; ++i)
|
||||
{
|
||||
var val = value[i];
|
||||
var prevVal = previousValue[i];
|
||||
if (!NetworkVariableSerialization<T>.AreEqual(ref val, ref prevVal))
|
||||
{
|
||||
++numChanges;
|
||||
changes.Set(i);
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = previousValue.Length; i < value.Length; ++i)
|
||||
{
|
||||
++numChanges;
|
||||
changes.Set(i);
|
||||
}
|
||||
|
||||
if (changes.GetSerializedSize() + FastBufferWriter.GetWriteSize<T>() * numChanges > FastBufferWriter.GetWriteSize<T>() * value.Length)
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
writer.WriteValueSafe(value);
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteByte(0);
|
||||
BytePacker.WriteValuePacked(writer, value.Length);
|
||||
writer.WriteValueSafe(changes);
|
||||
unsafe
|
||||
{
|
||||
var ptr = (T*)value.GetUnsafePtr();
|
||||
var prevPtr = (T*)previousValue.GetUnsafePtr();
|
||||
for (int i = 0; i < value.Length; ++i)
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
if (i < previousValue.Length)
|
||||
{
|
||||
NetworkVariableSerialization<T>.WriteDelta(writer, ref ptr[i], ref prevPtr[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
NetworkVariableSerialization<T>.Write(writer, ref ptr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void ReadNativeListDelta<T>(FastBufferReader reader, ref NativeList<T> value) where T : unmanaged
|
||||
{
|
||||
// See ReadListDelta and ReadNativeArrayDelta to understand most of this. It's basically the same,
|
||||
// just adjusted for the NativeList API
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full == 1)
|
||||
{
|
||||
reader.ReadValueSafeInPlace(ref value);
|
||||
return;
|
||||
}
|
||||
ByteUnpacker.ReadValuePacked(reader, out int length);
|
||||
var changes = new ResizableBitVector(Allocator.Temp);
|
||||
using var toDispose = changes;
|
||||
{
|
||||
reader.ReadNetworkSerializableInPlace(ref changes);
|
||||
|
||||
var previousLength = value.Length;
|
||||
// The one big difference between this and NativeArray/List is that NativeList supports
|
||||
// easy and fast resizing and reserving space.
|
||||
if (length != value.Length)
|
||||
{
|
||||
value.Resize(length, NativeArrayOptions.UninitializedMemory);
|
||||
}
|
||||
|
||||
unsafe
|
||||
{
|
||||
var ptr = (T*)value.GetUnsafePtr();
|
||||
for (var i = 0; i < value.Length; ++i)
|
||||
{
|
||||
if (changes.IsSet(i))
|
||||
{
|
||||
if (i < previousLength)
|
||||
{
|
||||
NetworkVariableSerialization<T>.ReadDelta(reader, ref ptr[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
NetworkVariableSerialization<T>.Read(reader, ref ptr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe void WriteNativeHashSetDelta<T>(FastBufferWriter writer, ref NativeHashSet<T> value, ref NativeHashSet<T> previousValue) where T : unmanaged, IEquatable<T>
|
||||
{
|
||||
// See WriteHashSet; this is the same algorithm, adjusted for the NativeHashSet API
|
||||
var added = stackalloc T[value.Count()];
|
||||
var removed = stackalloc T[previousValue.Count()];
|
||||
var addedCount = 0;
|
||||
var removedCount = 0;
|
||||
foreach (var item in value)
|
||||
{
|
||||
if (!previousValue.Contains(item))
|
||||
{
|
||||
added[addedCount] = item;
|
||||
++addedCount;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in previousValue)
|
||||
{
|
||||
if (!value.Contains(item))
|
||||
{
|
||||
removed[removedCount] = item;
|
||||
++removedCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (addedCount + removedCount >= value.Count())
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
writer.WriteValueSafe(value);
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteByteSafe(0);
|
||||
writer.WriteValueSafe(addedCount);
|
||||
for (var i = 0; i < addedCount; ++i)
|
||||
{
|
||||
NetworkVariableSerialization<T>.Write(writer, ref added[i]);
|
||||
}
|
||||
writer.WriteValueSafe(removedCount);
|
||||
for (var i = 0; i < removedCount; ++i)
|
||||
{
|
||||
NetworkVariableSerialization<T>.Write(writer, ref removed[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReadNativeHashSetDelta<T>(FastBufferReader reader, ref NativeHashSet<T> value) where T : unmanaged, IEquatable<T>
|
||||
{
|
||||
// See ReadHashSet; this is the same algorithm, adjusted for the NativeHashSet API
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full != 0)
|
||||
{
|
||||
reader.ReadValueSafeInPlace(ref value);
|
||||
return;
|
||||
}
|
||||
reader.ReadValueSafe(out int addedCount);
|
||||
for (var i = 0; i < addedCount; ++i)
|
||||
{
|
||||
T item = default;
|
||||
NetworkVariableSerialization<T>.Read(reader, ref item);
|
||||
value.Add(item);
|
||||
}
|
||||
reader.ReadValueSafe(out int removedCount);
|
||||
for (var i = 0; i < removedCount; ++i)
|
||||
{
|
||||
T item = default;
|
||||
NetworkVariableSerialization<T>.Read(reader, ref item);
|
||||
value.Remove(item);
|
||||
}
|
||||
}
|
||||
|
||||
public static unsafe void WriteNativeHashMapDelta<TKey, TVal>(FastBufferWriter writer, ref NativeHashMap<TKey, TVal> value, ref NativeHashMap<TKey, TVal> previousValue)
|
||||
where TKey : unmanaged, IEquatable<TKey>
|
||||
where TVal : unmanaged
|
||||
{
|
||||
// See WriteDictionary; this is the same algorithm, adjusted for the NativeHashMap API
|
||||
var added = stackalloc KeyValue<TKey, TVal>[value.Count()];
|
||||
var changed = stackalloc KeyValue<TKey, TVal>[value.Count()];
|
||||
var removed = stackalloc KeyValue<TKey, TVal>[previousValue.Count()];
|
||||
var addedCount = 0;
|
||||
var changedCount = 0;
|
||||
var removedCount = 0;
|
||||
foreach (var item in value)
|
||||
{
|
||||
var hasPrevVal = previousValue.TryGetValue(item.Key, out var prevVal);
|
||||
if (!hasPrevVal)
|
||||
{
|
||||
added[addedCount] = item;
|
||||
++addedCount;
|
||||
}
|
||||
else if (!NetworkVariableSerialization<TVal>.AreEqual(ref item.Value, ref prevVal))
|
||||
{
|
||||
changed[changedCount] = item;
|
||||
++changedCount;
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var item in previousValue)
|
||||
{
|
||||
if (!value.ContainsKey(item.Key))
|
||||
{
|
||||
removed[removedCount] = item;
|
||||
++removedCount;
|
||||
}
|
||||
}
|
||||
|
||||
if (addedCount + removedCount + changedCount >= value.Count())
|
||||
{
|
||||
writer.WriteByteSafe(1);
|
||||
writer.WriteValueSafe(value);
|
||||
return;
|
||||
}
|
||||
|
||||
writer.WriteByteSafe(0);
|
||||
writer.WriteValueSafe(addedCount);
|
||||
for (var i = 0; i < addedCount; ++i)
|
||||
{
|
||||
(var key, var val) = (added[i].Key, added[i].Value);
|
||||
NetworkVariableSerialization<TKey>.Write(writer, ref key);
|
||||
NetworkVariableSerialization<TVal>.Write(writer, ref val);
|
||||
}
|
||||
writer.WriteValueSafe(removedCount);
|
||||
for (var i = 0; i < removedCount; ++i)
|
||||
{
|
||||
var key = removed[i].Key;
|
||||
NetworkVariableSerialization<TKey>.Write(writer, ref key);
|
||||
}
|
||||
writer.WriteValueSafe(changedCount);
|
||||
for (var i = 0; i < changedCount; ++i)
|
||||
{
|
||||
(var key, var val) = (changed[i].Key, changed[i].Value);
|
||||
NetworkVariableSerialization<TKey>.Write(writer, ref key);
|
||||
NetworkVariableSerialization<TVal>.Write(writer, ref val);
|
||||
}
|
||||
}
|
||||
|
||||
public static void ReadNativeHashMapDelta<TKey, TVal>(FastBufferReader reader, ref NativeHashMap<TKey, TVal> value)
|
||||
where TKey : unmanaged, IEquatable<TKey>
|
||||
where TVal : unmanaged
|
||||
{
|
||||
// See ReadDictionary; this is the same algorithm, adjusted for the NativeHashMap API
|
||||
reader.ReadByteSafe(out byte full);
|
||||
if (full != 0)
|
||||
{
|
||||
reader.ReadValueSafeInPlace(ref value);
|
||||
return;
|
||||
}
|
||||
// Added
|
||||
reader.ReadValueSafe(out int length);
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
(TKey key, TVal val) = (default, default);
|
||||
NetworkVariableSerialization<TKey>.Read(reader, ref key);
|
||||
NetworkVariableSerialization<TVal>.Read(reader, ref val);
|
||||
value.Add(key, val);
|
||||
}
|
||||
// Removed
|
||||
reader.ReadValueSafe(out length);
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
TKey key = default;
|
||||
NetworkVariableSerialization<TKey>.Read(reader, ref key);
|
||||
value.Remove(key);
|
||||
}
|
||||
// Changed
|
||||
reader.ReadValueSafe(out length);
|
||||
for (var i = 0; i < length; ++i)
|
||||
{
|
||||
(TKey key, TVal val) = (default, default);
|
||||
NetworkVariableSerialization<TKey>.Read(reader, ref key);
|
||||
NetworkVariableSerialization<TVal>.Read(reader, ref val);
|
||||
value[key] = val;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c822ece4e24f4676861e07288a7f8526
|
||||
timeCreated: 1705437250
|
||||
@@ -22,6 +22,28 @@ namespace Unity.Netcode
|
||||
/// </summary>
|
||||
public OnValueChangedDelegate OnValueChanged;
|
||||
|
||||
public delegate bool CheckExceedsDirtinessThresholdDelegate(in T previousValue, in T newValue);
|
||||
|
||||
public CheckExceedsDirtinessThresholdDelegate CheckExceedsDirtinessThreshold;
|
||||
|
||||
public override bool ExceedsDirtinessThreshold()
|
||||
{
|
||||
if (CheckExceedsDirtinessThreshold != null && m_HasPreviousValue)
|
||||
{
|
||||
return CheckExceedsDirtinessThreshold(m_PreviousValue, m_InternalValue);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override void OnInitialize()
|
||||
{
|
||||
base.OnInitialize();
|
||||
|
||||
m_HasPreviousValue = true;
|
||||
NetworkVariableSerialization<T>.Duplicate(m_InternalValue, ref m_PreviousValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor for <see cref="NetworkVariable{T}"/>
|
||||
/// </summary>
|
||||
@@ -142,16 +164,16 @@ namespace Unity.Netcode
|
||||
/// </summary>
|
||||
public override void ResetDirty()
|
||||
{
|
||||
base.ResetDirty();
|
||||
// Resetting the dirty value declares that the current value is not dirty
|
||||
// Therefore, we set the m_PreviousValue field to a duplicate of the current
|
||||
// field, so that our next dirty check is made against the current "not dirty"
|
||||
// value.
|
||||
if (!m_HasPreviousValue || !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_PreviousValue))
|
||||
if (IsDirty())
|
||||
{
|
||||
m_HasPreviousValue = true;
|
||||
NetworkVariableSerialization<T>.Duplicate(m_InternalValue, ref m_PreviousValue);
|
||||
}
|
||||
base.ResetDirty();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -173,7 +195,7 @@ namespace Unity.Netcode
|
||||
/// <param name="writer">The stream to write the value to</param>
|
||||
public override void WriteDelta(FastBufferWriter writer)
|
||||
{
|
||||
WriteField(writer);
|
||||
NetworkVariableSerialization<T>.WriteDelta(writer, ref m_InternalValue, ref m_PreviousValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -189,7 +211,7 @@ namespace Unity.Netcode
|
||||
// would be stored in different fields
|
||||
|
||||
T previousValue = m_InternalValue;
|
||||
NetworkVariableSerialization<T>.Read(reader, ref m_InternalValue);
|
||||
NetworkVariableSerialization<T>.ReadDelta(reader, ref m_InternalValue);
|
||||
|
||||
if (keepDirtyDelta)
|
||||
{
|
||||
|
||||
@@ -3,11 +3,26 @@ using UnityEngine;
|
||||
|
||||
namespace Unity.Netcode
|
||||
{
|
||||
public struct NetworkVariableUpdateTraits
|
||||
{
|
||||
[Tooltip("The minimum amount of time that must pass between sending updates. If this amount of time has not passed since the last update, dirtiness will be ignored.")]
|
||||
public float MinSecondsBetweenUpdates;
|
||||
|
||||
[Tooltip("The maximum amount of time that a variable can be dirty without sending an update. If this amount of time has passed since the last update, an update will be sent even if the dirtiness threshold has not been met.")]
|
||||
public float MaxSecondsBetweenUpdates;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interface for network value containers
|
||||
/// </summary>
|
||||
public abstract class NetworkVariableBase : IDisposable
|
||||
{
|
||||
[SerializeField]
|
||||
internal NetworkVariableUpdateTraits UpdateTraits = default;
|
||||
|
||||
[NonSerialized]
|
||||
internal double LastUpdateSent;
|
||||
|
||||
/// <summary>
|
||||
/// The delivery type (QoS) to send data with
|
||||
/// </summary>
|
||||
@@ -30,6 +45,43 @@ namespace Unity.Netcode
|
||||
public void Initialize(NetworkBehaviour networkBehaviour)
|
||||
{
|
||||
m_NetworkBehaviour = networkBehaviour;
|
||||
if (m_NetworkBehaviour.NetworkManager)
|
||||
{
|
||||
if (m_NetworkBehaviour.NetworkManager.NetworkTimeSystem != null)
|
||||
{
|
||||
UpdateLastSentTime();
|
||||
}
|
||||
}
|
||||
|
||||
OnInitialize();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called on initialization
|
||||
/// </summary>
|
||||
public virtual void OnInitialize()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the update traits for this network variable to determine how frequently it will send updates.
|
||||
/// </summary>
|
||||
/// <param name="traits"></param>
|
||||
public void SetUpdateTraits(NetworkVariableUpdateTraits traits)
|
||||
{
|
||||
UpdateTraits = traits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check whether or not this variable has changed significantly enough to send an update.
|
||||
/// If not, no update will be sent even if the variable is dirty, unless the time since last update exceeds
|
||||
/// the <see cref="UpdateTraits"/>' <see cref="NetworkVariableUpdateTraits.MaxSecondsBetweenUpdates"/>.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool ExceedsDirtinessThreshold()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -92,6 +144,25 @@ namespace Unity.Netcode
|
||||
}
|
||||
}
|
||||
|
||||
internal bool CanSend()
|
||||
{
|
||||
var timeSinceLastUpdate = m_NetworkBehaviour.NetworkManager.NetworkTimeSystem.LocalTime - LastUpdateSent;
|
||||
return
|
||||
(
|
||||
UpdateTraits.MaxSecondsBetweenUpdates > 0 &&
|
||||
timeSinceLastUpdate >= UpdateTraits.MaxSecondsBetweenUpdates
|
||||
) ||
|
||||
(
|
||||
timeSinceLastUpdate >= UpdateTraits.MinSecondsBetweenUpdates &&
|
||||
ExceedsDirtinessThreshold()
|
||||
);
|
||||
}
|
||||
|
||||
internal void UpdateLastSentTime()
|
||||
{
|
||||
LastUpdateSent = m_NetworkBehaviour.NetworkManager.NetworkTimeSystem.LocalTime;
|
||||
}
|
||||
|
||||
protected void MarkNetworkBehaviourDirty()
|
||||
{
|
||||
if (m_NetworkBehaviour == null)
|
||||
@@ -109,6 +180,16 @@ namespace Unity.Netcode
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_NetworkBehaviour.NetworkManager.IsListening)
|
||||
{
|
||||
if (m_NetworkBehaviour.NetworkManager.LogLevel <= LogLevel.Developer)
|
||||
{
|
||||
Debug.LogWarning($"NetworkVariable is written to after the NetworkManager has already shutdown! " +
|
||||
"Are you modifying a NetworkVariable within a NetworkBehaviour.OnDestroy or NetworkBehaviour.OnDespawn method?");
|
||||
}
|
||||
return;
|
||||
}
|
||||
m_NetworkBehaviour.NetworkManager.BehaviourUpdater.AddForUpdate(m_NetworkBehaviour.NetworkObject);
|
||||
}
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
107
Runtime/NetworkVariable/ResizableBitVector.cs
Normal file
107
Runtime/NetworkVariable/ResizableBitVector.cs
Normal file
@@ -0,0 +1,107 @@
|
||||
using System;
|
||||
using Unity.Collections;
|
||||
using Unity.Collections.LowLevel.Unsafe;
|
||||
|
||||
namespace Unity.Netcode
|
||||
{
|
||||
/// <summary>
|
||||
/// This is a simple resizable bit vector - i.e., a list of flags that use 1 bit each and can
|
||||
/// grow to an indefinite size. This is backed by a NativeList<byte> instead of a single
|
||||
/// integer value, allowing it to contain any size of memory. Contains built-in serialization support.
|
||||
/// </summary>
|
||||
internal struct ResizableBitVector : INetworkSerializable, IDisposable
|
||||
{
|
||||
private NativeList<byte> m_Bits;
|
||||
private const int k_Divisor = sizeof(byte) * 8;
|
||||
|
||||
public ResizableBitVector(Allocator allocator)
|
||||
{
|
||||
m_Bits = new NativeList<byte>(allocator);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
m_Bits.Dispose();
|
||||
}
|
||||
|
||||
public int GetSerializedSize()
|
||||
{
|
||||
return sizeof(int) + m_Bits.Length;
|
||||
}
|
||||
|
||||
private (int, int) GetBitData(int i)
|
||||
{
|
||||
var index = i / k_Divisor;
|
||||
var bitWithinIndex = i % k_Divisor;
|
||||
return (index, bitWithinIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set bit 'i' - i.e., bit 0 is 00000001, bit 1 is 00000010, and so on.
|
||||
/// There is no upper bound on i except for the memory available in the system.
|
||||
/// </summary>
|
||||
/// <param name="i"></param>
|
||||
public void Set(int i)
|
||||
{
|
||||
var (index, bitWithinIndex) = GetBitData(i);
|
||||
if (index >= m_Bits.Length)
|
||||
{
|
||||
m_Bits.Resize(index + 1, NativeArrayOptions.ClearMemory);
|
||||
}
|
||||
|
||||
m_Bits[index] |= (byte)(1 << bitWithinIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unset bit 'i' - i.e., bit 0 is 00000001, bit 1 is 00000010, and so on.
|
||||
/// There is no upper bound on i except for the memory available in the system.
|
||||
/// Note that once a BitVector has grown to a certain size, it will not shrink back down,
|
||||
/// so if you set and unset every bit, it will still serialize at its high watermark size.
|
||||
/// </summary>
|
||||
/// <param name="i"></param>
|
||||
public void Unset(int i)
|
||||
{
|
||||
var (index, bitWithinIndex) = GetBitData(i);
|
||||
if (index >= m_Bits.Length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
m_Bits[index] &= (byte)~(1 << bitWithinIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if bit 'i' is set - i.e., bit 0 is 00000001, bit 1 is 00000010, and so on.
|
||||
/// There is no upper bound on i except for the memory available in the system.
|
||||
/// </summary>
|
||||
/// <param name="i"></param>
|
||||
public bool IsSet(int i)
|
||||
{
|
||||
var (index, bitWithinIndex) = GetBitData(i);
|
||||
if (index >= m_Bits.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return (m_Bits[index] & (byte)(1 << bitWithinIndex)) != 0;
|
||||
}
|
||||
|
||||
public unsafe void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
|
||||
{
|
||||
var length = m_Bits.Length;
|
||||
serializer.SerializeValue(ref length);
|
||||
m_Bits.ResizeUninitialized(length);
|
||||
var ptr = m_Bits.GetUnsafePtr();
|
||||
{
|
||||
if (serializer.IsReader)
|
||||
{
|
||||
serializer.GetFastBufferReader().ReadBytesSafe((byte*)ptr, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
serializer.GetFastBufferWriter().WriteBytesSafe((byte*)ptr, length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Runtime/NetworkVariable/ResizableBitVector.cs.meta
Normal file
3
Runtime/NetworkVariable/ResizableBitVector.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 664696a622e244dfa43b26628c05e4a6
|
||||
timeCreated: 1705437231
|
||||
Reference in New Issue
Block a user