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.ReliableFragmentedSequenced;
private protected NetworkBehaviour m_NetworkBehaviour;
public void Initialize(NetworkBehaviour networkBehaviour)
{
m_NetworkBehaviour = networkBehaviour;
}
public const NetworkVariableReadPermission DefaultReadPerm = NetworkVariableReadPermission.Everyone;
public const NetworkVariableWritePermission DefaultWritePerm = NetworkVariableWritePermission.Server;
protected NetworkVariableBase(
NetworkVariableReadPermission readPerm = DefaultReadPerm,
NetworkVariableWritePermission writePerm = DefaultWritePerm)
{
ReadPerm = readPerm;
WritePerm = writePerm;
}
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;
public readonly NetworkVariableWritePermission WritePerm;
///
/// 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 bool CanClientRead(ulong clientId)
{
switch (ReadPerm)
{
default:
case NetworkVariableReadPermission.Everyone:
return true;
case NetworkVariableReadPermission.Owner:
return clientId == m_NetworkBehaviour.NetworkObject.OwnerClientId;
}
}
public bool CanClientWrite(ulong clientId)
{
switch (WritePerm)
{
default:
case NetworkVariableWritePermission.Server:
return clientId == NetworkManager.ServerClientId;
case NetworkVariableWritePermission.Owner:
return clientId == 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);
public virtual void Dispose()
{
}
}
}