mirror of
https://github.com/rafi1212122/PemukulPaku
synced 2025-12-12 20:54:34 +01:00
nasty packet deserialization
This commit is contained in:
50
Gameserver/Packet.cs
Normal file
50
Gameserver/Packet.cs
Normal 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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user