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

89 lines
1.8 KiB
C#
Raw Normal View History

using System;
2021-12-21 20:38:43 +00:00
using QuantumUNET.Messages;
using QuantumUNET.Transport;
namespace QSB.Messaging
{
2021-12-21 20:38:43 +00:00
public abstract class QSBMessage : QMessageBase
{
2021-12-11 08:46:55 +00:00
/// set automatically when sending a message
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-11 08:46:55 +00:00
/// call the base method when overriding
public override void Serialize(QNetworkWriter writer)
{
writer.Write(From);
writer.Write(To);
}
2021-12-11 08:46:55 +00:00
/// call the base method when overriding
public override void Deserialize(QNetworkReader reader)
{
From = reader.ReadUInt32();
To = reader.ReadUInt32();
}
/// checked before calling either OnReceive
2021-12-21 20:38:43 +00:00
public virtual bool ShouldReceive => true;
public virtual void OnReceiveRemote() { }
public virtual void OnReceiveLocal() { }
}
public abstract class QSBBoolMessage : QSBMessage
{
2021-12-23 01:13:54 +00:00
protected bool Value;
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Value);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Value = reader.ReadBoolean();
}
}
public abstract class QSBFloatMessage : QSBMessage
{
2021-12-23 01:13:54 +00:00
protected float Value;
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(Value);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Value = reader.ReadSingle();
}
}
public abstract class QSBEnumMessage<E> : QSBMessage where E : Enum
{
2021-12-23 01:13:54 +00:00
protected E Value;
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write((int)(object)Value);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
Value = (E)(object)reader.ReadInt32();
}
}
}