2022-01-14 04:52:04 +00:00
|
|
|
|
using Mirror;
|
2021-12-11 07:28:42 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
namespace QSB.Messaging;
|
|
|
|
|
|
|
|
|
|
public abstract class QSBMessage
|
2021-12-11 07:28:42 +00:00
|
|
|
|
{
|
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)
|
2021-12-11 07:28:42 +00:00
|
|
|
|
{
|
2022-03-03 03:46:33 +00:00
|
|
|
|
writer.Write(From);
|
|
|
|
|
writer.Write(To);
|
|
|
|
|
}
|
2021-12-11 07:28:42 +00:00
|
|
|
|
|
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>();
|
|
|
|
|
}
|
2021-12-11 07:28:42 +00:00
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-03-11 01:39:00 +00:00
|
|
|
|
protected D Data { get; private set; }
|
2022-03-11 01:28:52 +00:00
|
|
|
|
protected QSBMessage(D data) => Data = data;
|
2021-12-11 07:28:42 +00:00
|
|
|
|
|
2022-03-03 03:46:33 +00:00
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
2022-02-25 06:04:54 +00:00
|
|
|
|
{
|
2022-03-03 03:46:33 +00:00
|
|
|
|
base.Serialize(writer);
|
|
|
|
|
writer.Write(Data);
|
|
|
|
|
}
|
2022-02-27 12:40:44 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
2022-03-11 01:28:52 +00:00
|
|
|
|
}
|