2015-09-25 18:52:25 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2016-06-08 22:29:04 +01:00
|
|
|
|
using FFXIVClassic_Map_Server.common;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
|
2016-06-08 22:29:04 +01:00
|
|
|
|
namespace FFXIVClassic_Map_Server.packets
|
2015-09-25 18:52:25 -04:00
|
|
|
|
{
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
public struct SubPacketHeader
|
|
|
|
|
{
|
|
|
|
|
public ushort subpacketSize;
|
2015-10-14 23:48:49 -04:00
|
|
|
|
public ushort type;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
public uint sourceId;
|
|
|
|
|
public uint targetId;
|
|
|
|
|
public uint unknown1;
|
2015-10-14 23:48:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
|
public struct GameMessageHeader
|
|
|
|
|
{
|
2015-09-25 18:52:25 -04:00
|
|
|
|
public ushort unknown4; //Always 0x14
|
|
|
|
|
public ushort opcode;
|
|
|
|
|
public uint unknown5;
|
|
|
|
|
public uint timestamp;
|
|
|
|
|
public uint unknown6;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class SubPacket
|
|
|
|
|
{
|
2015-10-14 23:48:49 -04:00
|
|
|
|
public const int SUBPACKET_SIZE = 0x10;
|
|
|
|
|
public const int GAMEMESSAGE_SIZE = 0x10;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
|
|
|
|
|
public SubPacketHeader header;
|
2015-10-14 23:48:49 -04:00
|
|
|
|
public GameMessageHeader gameMessage;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
public byte[] data;
|
|
|
|
|
|
|
|
|
|
public unsafe SubPacket(byte[] bytes, ref int offset)
|
|
|
|
|
{
|
|
|
|
|
if (bytes.Length < offset + SUBPACKET_SIZE)
|
|
|
|
|
throw new OverflowException("Packet Error: Subpacket was too small");
|
|
|
|
|
|
|
|
|
|
fixed (byte* pdata = &bytes[offset])
|
|
|
|
|
{
|
|
|
|
|
header = (SubPacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(SubPacketHeader));
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
if (header.type == 0x3)
|
|
|
|
|
{
|
|
|
|
|
fixed (byte* pdata = &bytes[offset + SUBPACKET_SIZE])
|
|
|
|
|
{
|
|
|
|
|
gameMessage = (GameMessageHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(GameMessageHeader));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:52:25 -04:00
|
|
|
|
if (bytes.Length < offset + header.subpacketSize)
|
|
|
|
|
throw new OverflowException("Packet Error: Subpacket size didn't equal subpacket data");
|
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
if (header.type == 0x3)
|
|
|
|
|
{
|
|
|
|
|
data = new byte[header.subpacketSize - SUBPACKET_SIZE - GAMEMESSAGE_SIZE];
|
|
|
|
|
Array.Copy(bytes, offset + SUBPACKET_SIZE + GAMEMESSAGE_SIZE, data, 0, data.Length);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
data = new byte[header.subpacketSize - SUBPACKET_SIZE];
|
|
|
|
|
Array.Copy(bytes, offset + SUBPACKET_SIZE, data, 0, data.Length);
|
|
|
|
|
}
|
2015-09-25 18:52:25 -04:00
|
|
|
|
|
|
|
|
|
offset += header.subpacketSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SubPacket(ushort opcode, uint sourceId, uint targetId, byte[] data)
|
|
|
|
|
{
|
|
|
|
|
this.header = new SubPacketHeader();
|
2015-10-14 23:48:49 -04:00
|
|
|
|
this.gameMessage = new GameMessageHeader();
|
|
|
|
|
|
|
|
|
|
gameMessage.opcode = opcode;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
header.sourceId = sourceId;
|
|
|
|
|
header.targetId = targetId;
|
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
gameMessage.timestamp = Utils.UnixTimeStampUTC();
|
2015-09-25 18:52:25 -04:00
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
header.type = 0x03;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
header.unknown1 = 0x00;
|
2015-10-14 23:48:49 -04:00
|
|
|
|
gameMessage.unknown4 = 0x14;
|
|
|
|
|
gameMessage.unknown5 = 0x00;
|
|
|
|
|
gameMessage.unknown6 = 0x00;
|
2015-09-25 18:52:25 -04:00
|
|
|
|
|
|
|
|
|
this.data = data;
|
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
header.subpacketSize = (ushort)(SUBPACKET_SIZE + GAMEMESSAGE_SIZE + data.Length);
|
2015-09-25 18:52:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-02 00:02:06 -05:00
|
|
|
|
public SubPacket(SubPacket original, uint newTargetId)
|
|
|
|
|
{
|
|
|
|
|
this.header = new SubPacketHeader();
|
|
|
|
|
this.gameMessage = original.gameMessage;
|
|
|
|
|
header.subpacketSize = original.header.subpacketSize;
|
|
|
|
|
header.type = original.header.type;
|
|
|
|
|
header.sourceId = original.header.sourceId;
|
|
|
|
|
header.targetId = newTargetId;
|
|
|
|
|
data = original.data;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:52:25 -04:00
|
|
|
|
public byte[] getHeaderBytes()
|
|
|
|
|
{
|
|
|
|
|
int size = Marshal.SizeOf(header);
|
|
|
|
|
byte[] arr = new byte[size];
|
|
|
|
|
|
|
|
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
|
|
|
Marshal.StructureToPtr(header, ptr, true);
|
|
|
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
|
|
|
Marshal.FreeHGlobal(ptr);
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
public byte[] getGameMessageBytes()
|
|
|
|
|
{
|
|
|
|
|
int size = Marshal.SizeOf(gameMessage);
|
|
|
|
|
byte[] arr = new byte[size];
|
|
|
|
|
|
|
|
|
|
IntPtr ptr = Marshal.AllocHGlobal(size);
|
|
|
|
|
Marshal.StructureToPtr(gameMessage, ptr, true);
|
|
|
|
|
Marshal.Copy(ptr, arr, 0, size);
|
|
|
|
|
Marshal.FreeHGlobal(ptr);
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:52:25 -04:00
|
|
|
|
public byte[] getBytes()
|
|
|
|
|
{
|
|
|
|
|
byte[] outBytes = new byte[header.subpacketSize];
|
|
|
|
|
Array.Copy(getHeaderBytes(), 0, outBytes, 0, SUBPACKET_SIZE);
|
2015-10-14 23:48:49 -04:00
|
|
|
|
|
|
|
|
|
if (header.type == 0x3)
|
|
|
|
|
Array.Copy(getGameMessageBytes(), 0, outBytes, SUBPACKET_SIZE, GAMEMESSAGE_SIZE);
|
|
|
|
|
|
2015-10-15 22:17:21 -04:00
|
|
|
|
Array.Copy(data, 0, outBytes, SUBPACKET_SIZE + (header.type == 0x3 ? GAMEMESSAGE_SIZE : 0), data.Length);
|
2015-09-25 18:52:25 -04:00
|
|
|
|
return outBytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void debugPrintSubPacket()
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
Console.BackgroundColor = ConsoleColor.DarkRed;
|
2016-06-10 21:14:28 -04:00
|
|
|
|
|
|
|
|
|
Log.Debug(String.Format("Size: 0x{0:X}{1}{2}", header.subpacketSize, Environment.NewLine, Utils.ByteArrayToHex(getHeaderBytes())));
|
|
|
|
|
|
2015-10-14 23:48:49 -04:00
|
|
|
|
if (header.type == 0x03)
|
2016-06-10 21:14:28 -04:00
|
|
|
|
{
|
|
|
|
|
Log.Debug(String.Format("Opcode: 0x{0:X}{1}{2}", gameMessage.opcode, Environment.NewLine, Utils.ByteArrayToHex(getGameMessageBytes(), SUBPACKET_SIZE)));
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:52:25 -04:00
|
|
|
|
Console.BackgroundColor = ConsoleColor.DarkMagenta;
|
2016-06-10 21:14:28 -04:00
|
|
|
|
|
|
|
|
|
Log.Debug(String.Format("Data: {0}{1}", Environment.NewLine, Utils.ByteArrayToHex(data, SUBPACKET_SIZE + GAMEMESSAGE_SIZE)));
|
|
|
|
|
|
2015-09-25 18:52:25 -04:00
|
|
|
|
Console.BackgroundColor = ConsoleColor.Black;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|