using System; namespace Unity.Netcode { /// /// Interface for network value containers /// public abstract class NetworkVariableBase : IDisposable { /// /// The delivery type (QoS) to send data with /// internal const NetworkDelivery Delivery = NetworkDelivery.ReliableSequenced; private protected NetworkBehaviour m_NetworkBehaviour; public void Initialize(NetworkBehaviour networkBehaviour) { m_NetworkBehaviour = networkBehaviour; } protected NetworkVariableBase(NetworkVariableReadPermission readPermIn = NetworkVariableReadPermission.Everyone) { ReadPerm = readPermIn; } private protected 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; /// /// Sets whether or not the variable needs to be delta synced /// public virtual void SetDirty(bool isDirty) { m_IsDirty = isDirty; } /// /// 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; } public virtual bool ShouldWrite(ulong clientId, bool isServer) { return IsDirty() && isServer && CanClientRead(clientId); } /// /// Gets Whether or not a specific client can read to the varaible /// /// The clientId of the remote client /// Whether or not the client can read to the variable public bool CanClientRead(ulong clientId) { switch (ReadPerm) { case NetworkVariableReadPermission.Everyone: return true; case NetworkVariableReadPermission.OwnerOnly: return m_NetworkBehaviour.OwnerClientId == clientId; } return true; } /// /// 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); public virtual void Dispose() { } } }