using Mirror;
using System;
namespace QSB.Messaging
{
public abstract class QSBMessage : NetworkMessage
{
///
/// set automatically by Send
///
internal uint From;
///
/// (default) uint.MaxValue = send to everyone
/// 0 = send to host
///
public uint To = uint.MaxValue;
///
/// call the base method when overriding
///
public virtual void Serialize(NetworkWriter writer)
{
writer.Write(From);
writer.Write(To);
}
///
/// call the base method when overriding
///
/// note: no constructor is called before this,
/// so fields won't be initialized.
///
public virtual void Deserialize(NetworkReader reader)
{
From = reader.Read();
To = reader.Read();
}
///
/// checked before calling either OnReceive
///
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();
}
}
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();
}
}
public abstract class QSBEnumMessage : 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();
}
}
}