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
{
///
/// Parameterless constructor to be used through inheritance
///
public Packet() { }
///
/// Incoming message based construtor that deserielizes the item for the message
///
public Packet(NetIncomingMessage im) { Deserialize(im); }
///
/// Searilizes the given packet item
///
/// Outgoing Message that the packet should be added to
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);
//
om.WriteAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
}
///
/// Deserializes the given packet item
///
/// Incoming Message that the packet should be deserialized from
public virtual void Deserialize(NetIncomingMessage im)
{
im.ReadAllFields(this, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
}
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;
}
}
}
}