Mirror/Unity-Technologies-networking/Runtime/Messages.cs

475 lines
13 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
#if ENABLE_UNET
namespace UnityEngine.Networking
{
// This can't be an interface because users don't need to implement the
// serialization functions, we'll code generate it for them when they omit it.
public abstract class MessageBase
{
// De-serialize the contents of the reader into this message
public virtual void Deserialize(NetworkReader reader) {}
// Serialize the contents of this message into the writer
public virtual void Serialize(NetworkWriter writer) {}
}
}
namespace UnityEngine.Networking.NetworkSystem
{
// ---------- General Typed Messages -------------------
public class StringMessage : MessageBase
{
public string value;
public StringMessage()
{
}
public StringMessage(string v)
{
value = v;
}
public override void Deserialize(NetworkReader reader)
{
value = reader.ReadString();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(value);
}
}
public class IntegerMessage : MessageBase
{
public int value;
public IntegerMessage()
{
}
public IntegerMessage(int v)
{
value = v;
}
public override void Deserialize(NetworkReader reader)
{
value = (int)reader.ReadPackedUInt32();
}
public override void Serialize(NetworkWriter writer)
{
writer.WritePackedUInt32((uint)value);
}
}
public class EmptyMessage : MessageBase
{
public override void Deserialize(NetworkReader reader)
{
}
public override void Serialize(NetworkWriter writer)
{
}
}
// ---------- Public System Messages -------------------
public class ErrorMessage : MessageBase
{
public byte errorCode; // byte instead of int because NetworkServer uses byte anyway. saves bandwidth.
public override void Deserialize(NetworkReader reader)
{
errorCode = reader.ReadByte();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(errorCode);
}
}
public class ReadyMessage : EmptyMessage
{
}
public class NotReadyMessage : EmptyMessage
{
}
public class AddPlayerMessage : MessageBase
{
public short playerControllerId;
public byte[] msgData;
public override void Deserialize(NetworkReader reader)
{
playerControllerId = (short)reader.ReadPackedUInt32();
msgData = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.WritePackedUInt32((uint)playerControllerId);
writer.WriteBytesAndSize(msgData);
}
}
public class RemovePlayerMessage : MessageBase
{
public short playerControllerId;
public override void Deserialize(NetworkReader reader)
{
playerControllerId = (short)reader.ReadPackedUInt32();
}
public override void Serialize(NetworkWriter writer)
{
writer.WritePackedUInt32((uint)playerControllerId);
}
}
// ---------- System Messages requried for code gen path -------------------
class CommandMessage : MessageBase
{
public NetworkInstanceId netId;
public int cmdHash;
public byte[] payload; // the parameters for the Cmd function
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
cmdHash = reader.ReadInt32(); // hash is always 4 full bytes, WritePackedInt would send 1 extra byte here
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(cmdHash);
writer.WriteBytesAndSize(payload);
}
}
class RpcMessage : MessageBase
{
public NetworkInstanceId netId;
public int rpcHash;
public byte[] payload; // the parameters for the Rpc function
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
rpcHash = reader.ReadInt32(); // hash is always 4 full bytes, WritePackedInt would send 1 extra byte here
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(rpcHash);
writer.WriteBytesAndSize(payload);
}
}
class SyncEventMessage : MessageBase
{
public NetworkInstanceId netId;
public int eventHash;
public byte[] payload; // the parameters for the Rpc function
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
eventHash = reader.ReadInt32(); // hash is always 4 full bytes, WritePackedInt would send 1 extra byte here
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(eventHash);
writer.WriteBytesAndSize(payload);
}
}
class SyncListMessage : MessageBase
{
public NetworkInstanceId netId;
public int syncListHash;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
syncListHash = reader.ReadInt32(); // hash is always 4 full bytes, WritePackedInt would send 1 extra byte here
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(syncListHash);
writer.WriteBytesAndSize(payload);
}
}
/* These are not used directly but manually serialized, these are here for reference.
internal class SyncListMessage<T> where T: struct
{
public NetworkId netId;
public int cmdHash;
public byte operation;
public int itemIndex;
public T item;
}
*/
// ---------- Internal System Messages -------------------
class SpawnPrefabMessage : MessageBase
{
public NetworkInstanceId netId;
public NetworkHash128 assetId;
public Vector3 position;
public Quaternion rotation;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
assetId = reader.ReadNetworkHash128();
position = reader.ReadVector3();
rotation = reader.ReadQuaternion();
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(assetId);
writer.Write(position);
writer.Write(rotation);
writer.WriteBytesAndSize(payload);
}
}
class SpawnSceneObjectMessage : MessageBase
{
public NetworkInstanceId netId;
public NetworkSceneId sceneId;
public Vector3 position;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
sceneId = reader.ReadSceneId();
position = reader.ReadVector3();
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(sceneId);
writer.Write(position);
writer.WriteBytesAndSize(payload);
}
}
class ObjectSpawnFinishedMessage : MessageBase
{
public byte state; // byte because it's always 0 or 1
public override void Deserialize(NetworkReader reader)
{
state = reader.ReadByte();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(state);
}
}
class ObjectDestroyMessage : MessageBase
{
public NetworkInstanceId netId;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
}
}
class OwnerMessage : MessageBase
{
public NetworkInstanceId netId;
public short playerControllerId;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
playerControllerId = (short)reader.ReadPackedUInt32();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WritePackedUInt32((uint)playerControllerId);
}
}
class ClientAuthorityMessage : MessageBase
{
public NetworkInstanceId netId;
public bool authority;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
authority = reader.ReadBoolean();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.Write(authority);
}
}
class UpdateVarsMessage : MessageBase
{
public NetworkInstanceId netId;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WriteBytesAndSize(payload);
}
}
class AnimationMessage : MessageBase
{
public NetworkInstanceId netId;
public int stateHash; // if non-zero, then Play() this animation, skipping transitions
public float normalizedTime;
public byte[] parameters;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
stateHash = (int)reader.ReadPackedUInt32();
normalizedTime = reader.ReadSingle();
parameters = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WritePackedUInt32((uint)stateHash);
writer.Write(normalizedTime);
writer.WriteBytesAndSize(parameters);
}
}
class AnimationParametersMessage : MessageBase
{
public NetworkInstanceId netId;
public byte[] parameters;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
parameters = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WriteBytesAndSize(parameters);
}
}
class AnimationTriggerMessage : MessageBase
{
public NetworkInstanceId netId;
public int hash;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
hash = (int)reader.ReadPackedUInt32();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WritePackedUInt32((uint)hash);
}
}
class LocalChildTransformMessage : MessageBase
{
public NetworkInstanceId netId;
public uint childIndex;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
childIndex = reader.ReadPackedUInt32();
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WritePackedUInt32(childIndex);
writer.WriteBytesAndSize(payload);
}
}
class LocalPlayerTransformMessage : MessageBase
{
public NetworkInstanceId netId;
public byte[] payload;
public override void Deserialize(NetworkReader reader)
{
netId = reader.ReadNetworkId();
payload = reader.ReadBytesAndSize();
}
public override void Serialize(NetworkWriter writer)
{
writer.Write(netId);
writer.WriteBytesAndSize(payload);
}
}
}
#endif //ENABLE_UNET