using System; 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; } /// /// Interface for network value containers /// public abstract class NetworkVariableBase : IDisposable { [SerializeField] internal NetworkVariableUpdateTraits UpdateTraits = default; [NonSerialized] internal double LastUpdateSent; /// /// The delivery type (QoS) to send data with /// internal const NetworkDelivery Delivery = NetworkDelivery.ReliableFragmentedSequenced; /// /// Maintains a link to the associated NetworkBehaviour /// private protected NetworkBehaviour m_NetworkBehaviour; public NetworkBehaviour GetBehaviour() { return m_NetworkBehaviour; } /// /// Initializes the NetworkVariable /// /// The NetworkBehaviour the NetworkVariable belongs to public void Initialize(NetworkBehaviour networkBehaviour) { m_NetworkBehaviour = networkBehaviour; if (m_NetworkBehaviour.NetworkManager) { if (m_NetworkBehaviour.NetworkManager.NetworkTimeSystem != null) { UpdateLastSentTime(); } } OnInitialize(); } /// /// Called on initialization /// public virtual void OnInitialize() { } /// /// Sets the update traits for this network variable to determine how frequently it will send updates. /// /// public void SetUpdateTraits(NetworkVariableUpdateTraits traits) { UpdateTraits = traits; } /// /// 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 ' . /// /// public virtual bool ExceedsDirtinessThreshold() { return true; } /// /// The default read permissions /// public const NetworkVariableReadPermission DefaultReadPerm = NetworkVariableReadPermission.Everyone; /// /// The default write permissions /// public const NetworkVariableWritePermission DefaultWritePerm = NetworkVariableWritePermission.Server; /// /// The default constructor for that can be used to create a /// custom NetworkVariable. /// /// the access settings /// the access settings protected NetworkVariableBase( NetworkVariableReadPermission readPerm = DefaultReadPerm, NetworkVariableWritePermission writePerm = DefaultWritePerm) { ReadPerm = readPerm; WritePerm = writePerm; } /// /// The property is used to determine if the /// value of the `NetworkVariable` has changed. /// private bool m_IsDirty; /// /// Gets or sets the name of the network variable's instance /// (MemberInfo) where it was declared. /// public string Name { get; internal set; } /// /// The read permission for this var /// public readonly NetworkVariableReadPermission ReadPerm; /// /// The write permission for this var /// public readonly NetworkVariableWritePermission WritePerm; /// /// Sets whether or not the variable needs to be delta synced /// /// Whether or not the var is dirty public virtual void SetDirty(bool isDirty) { m_IsDirty = isDirty; if (m_IsDirty) { MarkNetworkBehaviourDirty(); } } 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) { Debug.LogWarning($"NetworkVariable is written to, but doesn't know its NetworkBehaviour yet. " + "Are you modifying a NetworkVariable before the NetworkObject is spawned?"); return; } if (m_NetworkBehaviour.NetworkManager.ShutdownInProgress) { if (m_NetworkBehaviour.NetworkManager.LogLevel <= LogLevel.Developer) { Debug.LogWarning($"NetworkVariable is written to during the NetworkManager shutdown! " + "Are you modifying a NetworkVariable within a NetworkBehaviour.OnDestroy or NetworkBehaviour.OnDespawn method?"); } 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); } /// /// Resets the dirty state and marks the variable as synced / clean /// public virtual void ResetDirty() { m_IsDirty = false; } /// /// Gets Whether or not the container is dirty /// /// Whether or not the container is dirty public virtual bool IsDirty() { return m_IsDirty; } /// /// Gets if a specific client has permission to read the var or not /// /// The client id /// Whether or not the client has permission to read public bool CanClientRead(ulong clientId) { switch (ReadPerm) { default: case NetworkVariableReadPermission.Everyone: return true; case NetworkVariableReadPermission.Owner: return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId || NetworkManager.ServerClientId == clientId; } } /// /// Gets if a specific client has permission to write the var or not /// /// The client id /// Whether or not the client has permission to write public bool CanClientWrite(ulong clientId) { switch (WritePerm) { default: case NetworkVariableWritePermission.Server: return clientId == NetworkManager.ServerClientId; case NetworkVariableWritePermission.Owner: return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId; } } /// /// Returns the ClientId of the owning client /// internal ulong OwnerClientId() { return m_NetworkBehaviour.NetworkObject.OwnerClientId; } /// /// Writes the dirty changes, that is, the changes since the variable was last dirty, to the writer /// /// The stream to write the dirty changes to public abstract void WriteDelta(FastBufferWriter writer); /// /// Writes the complete state of the variable to the writer /// /// The stream to write the state to public abstract void WriteField(FastBufferWriter writer); /// /// Reads the complete state from the reader and applies it /// /// The stream to read the state from public abstract void ReadField(FastBufferReader reader); /// /// Reads delta from the reader and applies them to the internal value /// /// The stream to read the delta from /// Whether or not the delta should be kept as dirty or consumed public abstract void ReadDelta(FastBufferReader reader, bool keepDirtyDelta); /// /// Virtual implementation /// public virtual void Dispose() { } } }