quantum-space-buddies/QSB/Messaging/QSBMessage.cs

63 lines
1.3 KiB
C#
Raw Normal View History

2022-01-14 04:52:04 +00:00
using Mirror;
2022-03-03 03:46:33 +00:00
namespace QSB.Messaging;
public abstract class QSBMessage
{
2022-03-03 03:46:33 +00:00
/// <summary>
/// set automatically by Send
/// </summary>
internal uint From;
/// <summary>
/// (default) uint.MaxValue = send to everyone <br/>
/// 0 = send to host
/// </summary>
public uint To = uint.MaxValue;
/// <summary>
/// call the base method when overriding
/// </summary>
public virtual void Serialize(NetworkWriter writer)
{
2022-03-03 03:46:33 +00:00
writer.Write(From);
writer.Write(To);
}
2022-03-03 03:46:33 +00:00
/// <summary>
/// call the base method when overriding
/// <para/>
/// note: no constructor is called before this,
/// so fields won't be initialized.
/// </summary>
public virtual void Deserialize(NetworkReader reader)
{
From = reader.Read<uint>();
To = reader.Read<uint>();
}
2022-03-03 03:46:33 +00:00
/// <summary>
/// checked before calling either OnReceive
/// </summary>
public virtual bool ShouldReceive => true;
public virtual void OnReceiveLocal() { }
public virtual void OnReceiveRemote() { }
}
2021-12-26 12:36:00 +00:00
2022-03-03 03:46:33 +00:00
public abstract class QSBMessage<D> : QSBMessage
{
protected D Data { get; private set; }
protected QSBMessage(D data) => Data = data;
2022-03-03 03:46:33 +00:00
public override void Serialize(NetworkWriter writer)
{
2022-03-03 03:46:33 +00:00
base.Serialize(writer);
writer.Write(Data);
}
2022-03-03 03:46:33 +00:00
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
Data = reader.Read<D>();
2022-02-28 15:37:36 +00:00
}
}