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

104 lines
2.0 KiB
C#
Raw Normal View History

using System;
using QSB.Player;
using QuantumUNET.Transport;
namespace QSB.Messaging
{
public abstract class QSBMessage : QSBMessageRaw
{
2021-12-11 08:46:55 +00:00
/// set automatically when sending a message
public uint From;
2021-12-11 08:46:55 +00:00
/// <summary>
/// uint.MaxValue = send to everyone <br/>
/// 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();
}
public sealed override void OnReceive()
{
if (ShouldReceive)
{
if (From != QSBPlayerManager.LocalPlayerId)
{
OnReceiveRemote();
}
else
{
OnReceiveLocal();
}
}
}
2021-12-12 03:02:34 +00:00
/// checked before calling either OnReceive
protected virtual bool ShouldReceive => true;
protected virtual void OnReceiveRemote() { }
protected virtual void OnReceiveLocal() { }
}
public abstract class QSBBoolMessage : QSBMessage
{
public 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
{
public 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
{
public 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();
}
}
}