SRMP-Public/SRMP/Packets/_Classes/Packet.cs

64 lines
2 KiB
C#
Raw Normal View History

2023-05-29 22:23:11 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using UnityEngine;
namespace SRMultiplayer.Packets
{
public abstract class Packet : IPacket
{
/// <summary>
/// Parameterless constructor to be used through inheritance
/// </summary>
public Packet() { }
2023-05-29 22:23:11 +02:00
/// <summary>
/// Incoming message based construtor that deserielizes the item for the message
/// </summary>
public Packet(NetIncomingMessage im) { Deserialize(im); }
/// <summary>
/// Searilizes the given packet item
/// </summary>
/// <param name="om">Outgoing Message that the packet should be added to</param>
2023-05-29 22:23:11 +02:00
public virtual void Serialize(NetOutgoingMessage om)
{
om.Write((ushort)GetPacketType());
//writes the object type using the gettype name
//this allows the object to assume its object packet type
//om.Write(this.GetType().AssemblyQualifiedName);
//
2023-05-29 22:23:11 +02:00
om.WriteAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
}
/// <summary>
/// Deserializes the given packet item
/// </summary>
/// <param name="om">Incoming Message that the packet should be deserialized from</param>
public virtual void Deserialize(NetIncomingMessage im)
{
im.ReadAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
}
2023-05-29 22:23:11 +02:00
public PacketType GetPacketType()
{
PacketAttribute packet = (PacketAttribute)Attribute.GetCustomAttribute(GetType(), typeof(PacketAttribute));
if (packet != null)
{
return packet.Type;
}
else
{
SRMP.Log($"Packet {GetType()} has no attribute");
return PacketType.Unknown;
}
}
}
}