mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-29 18:32:45 +00:00
100 lines
2.1 KiB
C#
100 lines
2.1 KiB
C#
using Mirror;
|
|
using System;
|
|
|
|
namespace QSB.Messaging
|
|
{
|
|
public abstract class QSBMessage : NetworkMessage
|
|
{
|
|
/// <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)
|
|
{
|
|
writer.Write(From);
|
|
writer.Write(To);
|
|
}
|
|
|
|
/// <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>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// checked before calling either OnReceive
|
|
/// </summary>
|
|
public virtual bool ShouldReceive => true;
|
|
public virtual void OnReceiveLocal() { }
|
|
public virtual void OnReceiveRemote() { }
|
|
|
|
public override string ToString() => GetType().Name;
|
|
}
|
|
|
|
public abstract class QSBBoolMessage : QSBMessage
|
|
{
|
|
protected bool Value;
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
|
{
|
|
base.Serialize(writer);
|
|
writer.Write(Value);
|
|
}
|
|
|
|
public override void Deserialize(NetworkReader reader)
|
|
{
|
|
base.Deserialize(reader);
|
|
Value = reader.Read<bool>();
|
|
}
|
|
}
|
|
|
|
public abstract class QSBFloatMessage : QSBMessage
|
|
{
|
|
protected float Value;
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
|
{
|
|
base.Serialize(writer);
|
|
writer.Write(Value);
|
|
}
|
|
|
|
public override void Deserialize(NetworkReader reader)
|
|
{
|
|
base.Deserialize(reader);
|
|
Value = reader.Read<float>();
|
|
}
|
|
}
|
|
|
|
public abstract class QSBEnumMessage<E> : QSBMessage where E : Enum
|
|
{
|
|
protected E Value;
|
|
|
|
public override void Serialize(NetworkWriter writer)
|
|
{
|
|
base.Serialize(writer);
|
|
writer.Write((int)(object)Value);
|
|
}
|
|
|
|
public override void Deserialize(NetworkReader reader)
|
|
{
|
|
base.Deserialize(reader);
|
|
Value = (E)(object)reader.Read<int>();
|
|
}
|
|
}
|
|
}
|