2017-11-25 11:26:55 +01:00
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
using Godot;
|
|
|
|
|
|
2017-11-25 11:29:48 +01:00
|
|
|
|
namespace Network.PacketHandling {
|
2017-11-25 14:21:06 +01:00
|
|
|
|
public abstract class Serializable : Reference {
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
public SerializableMetadata Metadata;
|
|
|
|
|
|
|
|
|
|
public Serializable() {
|
|
|
|
|
Metadata = new SerializableMetadata(Protocol.GetPacketTypeIDFor(this));
|
|
|
|
|
}
|
2017-11-25 11:26:55 +01:00
|
|
|
|
|
|
|
|
|
public void Serialize(PacketBuffer packetBuffer) {
|
2017-11-25 14:21:06 +01:00
|
|
|
|
Metadata.Serialize(packetBuffer);
|
2017-11-25 11:26:55 +01:00
|
|
|
|
WriteContents(packetBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deserialize(PacketBuffer packetBuffer) {
|
2017-11-25 14:21:06 +01:00
|
|
|
|
if (!Metadata.Deserialized) {
|
|
|
|
|
Metadata.Deserialize(packetBuffer);
|
|
|
|
|
}
|
2017-11-25 11:26:55 +01:00
|
|
|
|
ReadContents(packetBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 14:21:06 +01:00
|
|
|
|
public void DeserializeWithMetadata(SerializableMetadata metadata, PacketBuffer packetBuffer) {
|
|
|
|
|
Metadata = metadata;
|
|
|
|
|
Deserialize(packetBuffer);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 11:26:55 +01:00
|
|
|
|
public abstract void WriteContents(PacketBuffer packetBuffer);
|
|
|
|
|
public abstract void ReadContents(PacketBuffer packetBuffer);
|
|
|
|
|
|
|
|
|
|
}
|
2017-11-25 14:21:06 +01:00
|
|
|
|
|
|
|
|
|
public class SerializableMetadata : Reference {
|
|
|
|
|
|
|
|
|
|
public int PacketType;
|
|
|
|
|
public bool Reliable;
|
|
|
|
|
public int ID;
|
|
|
|
|
|
|
|
|
|
public bool Deserialized = false;
|
|
|
|
|
|
|
|
|
|
public SerializableMetadata(int packetType) {
|
|
|
|
|
PacketType = packetType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Serialize(PacketBuffer packetBuffer) {
|
|
|
|
|
packetBuffer.Write(PacketType);
|
|
|
|
|
packetBuffer.Write(Reliable);
|
|
|
|
|
packetBuffer.Write(ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Deserialize(PacketBuffer packetBuffer) {
|
|
|
|
|
Deserialized = true;
|
|
|
|
|
PacketType = packetBuffer.ReadInt();
|
|
|
|
|
Reliable = packetBuffer.ReadBool();
|
|
|
|
|
ID = packetBuffer.ReadInt();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-25 11:26:55 +01:00
|
|
|
|
}
|