nasty packet deserialization

This commit is contained in:
rafi1212122
2023-05-26 14:04:44 +07:00
parent 7a12c9ec55
commit f0dd9f1a73
2 changed files with 53 additions and 1 deletions

50
Gameserver/Packet.cs Normal file
View File

@@ -0,0 +1,50 @@
using System.Buffers.Binary;
using Common.Resources.Proto;
using Newtonsoft.Json;
using Common.Utils;
using ProtoBuf;
namespace PemukulPaku.Gameserver
{
public class Packet
{
public readonly byte[] Raw;
public readonly byte[] HeadMagic;
public readonly uint UserId;
public readonly uint CmdId;
public readonly uint BodyLen;
public readonly byte[] Body;
public readonly byte[] TailMagic;
public static readonly Logger c = new("Packet", ConsoleColor.Magenta);
public Packet(byte[] buf)
{
Raw = buf;
CmdId = BinaryPrimitives.ReadUInt32BigEndian(buf.AsSpan(24));
string? PacketName = Enum.GetName(typeof(CmdId), CmdId);
if (PacketName == null)
{
c.Error($"CMD ID {CmdId} NOT RECOGNIZED!");
}
HeadMagic = buf.Take(4).ToArray();
UserId = BinaryPrimitives.ReadUInt32BigEndian(buf.AsSpan(12));
ushort headerLen = BinaryPrimitives.ReadUInt16BigEndian(buf.AsSpan(28));
BodyLen = BinaryPrimitives.ReadUInt32BigEndian(buf.AsSpan(30));
Body = buf.Skip(34 + headerLen).Take((int)BodyLen).ToArray();
TailMagic = buf.Skip(buf.Length - 4).ToArray();
try
{
MemoryStream ms = new(Body);
object SerializedBody = Serializer.NonGeneric.Deserialize(typeof(Common.Global).Assembly.GetType($"Common.Resources.Proto.{PacketName}"), ms)!;
c.Debug(JsonConvert.SerializeObject(SerializedBody));
}
catch
{
c.Error($"Failed to deserialized packet with Common.Resources.Proto.{PacketName}");
}
}
}
}

View File

@@ -1,5 +1,6 @@
using System.Net.Sockets;
using Common.Utils;
using Newtonsoft.Json;
namespace PemukulPaku.Gameserver
{
@@ -84,6 +85,7 @@ namespace PemukulPaku.Gameserver
{
c.Warn($"{Id} disconnected");
Server.GetInstance().Sessions.Remove(Id);
c.Debug("TCP client disconnect reason: " + ex.Message);
}
else
{
@@ -95,7 +97,7 @@ namespace PemukulPaku.Gameserver
public void ProcessPacket(byte[] packet)
{
c.Debug("Received packet: " + BitConverter.ToString(packet).Replace("-", ""));
_ = new Packet(packet);
}
}
}