2022-01-13 20:52:04 -08:00
|
|
|
|
using Mirror;
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.Messaging;
|
|
|
|
|
|
|
|
|
|
public abstract class QSBMessage
|
2021-12-10 23:28:42 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// set automatically by Send
|
|
|
|
|
/// </summary>
|
2023-07-27 17:03:40 -07:00
|
|
|
|
// public so it can be accessed by a patch
|
2023-07-28 01:01:52 +01:00
|
|
|
|
public uint From;
|
2022-03-02 19:46:33 -08:00
|
|
|
|
/// <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-10 23:28:42 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
writer.Write(From);
|
|
|
|
|
writer.Write(To);
|
|
|
|
|
}
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08: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-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// checked before calling either OnReceive
|
|
|
|
|
/// </summary>
|
|
|
|
|
public virtual bool ShouldReceive => true;
|
|
|
|
|
public virtual void OnReceiveLocal() { }
|
|
|
|
|
public virtual void OnReceiveRemote() { }
|
|
|
|
|
}
|
2021-12-26 04:36:00 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public abstract class QSBMessage<D> : QSBMessage
|
|
|
|
|
{
|
2023-07-27 16:50:54 -07:00
|
|
|
|
// public so it can be accessed by a patch
|
2023-07-28 00:45:39 +01:00
|
|
|
|
public D Data { get; private set; }
|
2022-03-10 17:28:52 -08:00
|
|
|
|
protected QSBMessage(D data) => Data = data;
|
2021-12-10 23:28:42 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
2022-02-24 22:04:54 -08:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
base.Serialize(writer);
|
|
|
|
|
writer.Write(Data);
|
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override void Deserialize(NetworkReader reader)
|
|
|
|
|
{
|
|
|
|
|
base.Deserialize(reader);
|
|
|
|
|
Data = reader.Read<D>();
|
2022-02-28 15:37:36 +00:00
|
|
|
|
}
|
2022-03-10 17:28:52 -08:00
|
|
|
|
}
|