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

65 lines
1.4 KiB
C#
Raw Normal View History

2022-01-14 04:52:04 +00:00
using Mirror;
namespace QSB.Messaging
{
2022-01-14 10:29:42 +00:00
public abstract class QSBMessage
{
/// <summary>
/// set automatically by Send
/// </summary>
2021-12-23 01:13:54 +00:00
internal uint From;
2021-12-11 08:46:55 +00:00
/// <summary>
2021-12-22 09:16:44 +00:00
/// (default) uint.MaxValue = send to everyone <br/>
2021-12-11 08:46:55 +00:00
/// 0 = send to host
/// </summary>
public uint To = uint.MaxValue;
2021-12-24 00:53:22 +00:00
/// <summary>
2021-12-11 08:46:55 +00:00
/// call the base method when overriding
2021-12-24 00:53:22 +00:00
/// </summary>
public virtual void Serialize(NetworkWriter writer)
{
writer.Write(From);
writer.Write(To);
}
2021-12-24 00:53:22 +00:00
/// <summary>
2021-12-11 08:46:55 +00:00
/// call the base method when overriding
2021-12-27 02:57:49 +00:00
/// <para/>
/// note: no constructor is called before this,
/// so fields won't be initialized.
2021-12-24 00:53:22 +00:00
/// </summary>
public virtual void Deserialize(NetworkReader reader)
{
From = reader.Read<uint>();
To = reader.Read<uint>();
}
2021-12-24 00:53:22 +00:00
/// <summary>
/// checked before calling either OnReceive
2021-12-24 00:53:22 +00:00
/// </summary>
2021-12-21 20:38:43 +00:00
public virtual bool ShouldReceive => true;
public virtual void OnReceiveLocal() { }
2021-12-27 04:12:50 +00:00
public virtual void OnReceiveRemote() { }
2021-12-26 12:36:00 +00:00
public override string ToString() => GetType().Name;
}
2022-01-27 04:28:59 +00:00
public abstract class QSBMessage<V> : QSBMessage
{
2022-01-27 04:28:59 +00:00
protected V Value;
public override void Serialize(NetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Value);
}
public override void Deserialize(NetworkReader reader)
{
base.Deserialize(reader);
2022-01-27 04:28:59 +00:00
Value = reader.Read<V>();
}
}
}