mirror of
https://bitbucket.org/Ioncannon/project-meteor-server.git
synced 2025-04-02 19:42:05 -04:00
Reimplemented ConsoleColor for packets in NLog.
This commit is contained in:
parent
0aac675b30
commit
590ad3e002
@ -1,4 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
|
||||||
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
|
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
|
||||||
@ -30,10 +31,19 @@
|
|||||||
layout="${longdate} ${uppercase:${level}} ${message}" />
|
layout="${longdate} ${uppercase:${level}} ${message}" />
|
||||||
-->
|
-->
|
||||||
<!--<target xsi:type="ColoredConsole" name="console" layout="[${longdate}] [${uppercase:${level}}] ${message}" />-->
|
<!--<target xsi:type="ColoredConsole" name="console" layout="[${longdate}] [${uppercase:${level}}] ${message}" />-->
|
||||||
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}/map.log" layout="[${longdate}] [${uppercase:${level}}] ${message}"/>
|
<target xsi:type="File" name="file" fileName="${basedir}/logs/${shortdate}/map.log"
|
||||||
<target xsi:type="ColoredConsole" name="console" layout="[${longdate}] [${uppercase:${level}}] ${message}">
|
layout="[${date:format=MM/dd/yyyy HH\:mm\:ss}][${uppercase:${level}}]${message}" />
|
||||||
<highlight-row condition="equals('${logger}','FFXIVClassic_Map_Server.packets.BasePacket') and equals('${event-context:item=color}','5')" foregroundColor="White" backgroundColor="Yellow" />
|
<target xsi:type="ColoredConsole" name="console"
|
||||||
<highlight-row condition="equals('${logger}','FFXIVClassic_Map_Server.packets.SubPacket') and equals('${event-context:item=color}','5')" foregroundColor="White" backgroundColor="DarkMagenta" />
|
layout="[${date:format=MM/dd/yyyy HH\:mm\:ss}][${uppercase:${level}}]${message}">
|
||||||
|
<highlight-row
|
||||||
|
condition="equals('${logger}', 'FFXIVClassic_Map_Server.packets.BasePacket') and equals('${event-context:item=color}', '6')"
|
||||||
|
backgroundColor="DarkYellow" foregroundColor="NoChange" />
|
||||||
|
<highlight-row
|
||||||
|
condition="equals('${logger}', 'FFXIVClassic_Map_Server.packets.SubPacket') and equals('${event-context:item=color}', '4')"
|
||||||
|
backgroundColor="DarkRed" foregroundColor="NoChange" />
|
||||||
|
<highlight-row
|
||||||
|
condition="equals('${logger}', 'FFXIVClassic_Map_Server.packets.SubPacket') and equals('${event-context:item=color}', '5')"
|
||||||
|
backgroundColor="DarkMagenta" foregroundColor="NoChange" />
|
||||||
</target>
|
</target>
|
||||||
</targets>
|
</targets>
|
||||||
|
|
||||||
|
@ -1,14 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using FFXIVClassic.Common;
|
using System.Runtime.InteropServices;
|
||||||
using NLog;
|
|
||||||
|
|
||||||
namespace FFXIVClassic_Map_Server.packets
|
namespace FFXIVClassic_Map_Server.packets
|
||||||
{
|
{
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
public struct BasePacketHeader
|
public struct BasePacketHeader
|
||||||
{
|
{
|
||||||
@ -22,19 +19,18 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public class BasePacket
|
public class BasePacket
|
||||||
{
|
{
|
||||||
public static Logger Log = LogManager.GetCurrentClassLogger();
|
|
||||||
|
|
||||||
public const int TYPE_ZONE = 1;
|
public const int TYPE_ZONE = 1;
|
||||||
public const int TYPE_CHAT = 2;
|
public const int TYPE_CHAT = 2;
|
||||||
public const int BASEPACKET_SIZE = 0x10;
|
public const int BASEPACKET_SIZE = 0x10;
|
||||||
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||||||
public BasePacketHeader header;
|
|
||||||
public byte[] data;
|
public byte[] data;
|
||||||
|
|
||||||
|
public BasePacketHeader header;
|
||||||
|
|
||||||
//Loads a sniffed packet from a file
|
//Loads a sniffed packet from a file
|
||||||
public unsafe BasePacket(String path)
|
public unsafe BasePacket(string path)
|
||||||
{
|
{
|
||||||
byte[] bytes = File.ReadAllBytes(path);
|
var bytes = File.ReadAllBytes(path);
|
||||||
|
|
||||||
if (bytes.Length < BASEPACKET_SIZE)
|
if (bytes.Length < BASEPACKET_SIZE)
|
||||||
throw new OverflowException("Packet Error: Packet was too small");
|
throw new OverflowException("Packet Error: Packet was too small");
|
||||||
@ -107,9 +103,9 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public List<SubPacket> GetSubpackets()
|
public List<SubPacket> GetSubpackets()
|
||||||
{
|
{
|
||||||
List<SubPacket> subpackets = new List<SubPacket>(header.numSubpackets);
|
var subpackets = new List<SubPacket>(header.numSubpackets);
|
||||||
|
|
||||||
int offset = 0;
|
var offset = 0;
|
||||||
|
|
||||||
while (offset < data.Length)
|
while (offset < data.Length)
|
||||||
subpackets.Add(new SubPacket(data, ref offset));
|
subpackets.Add(new SubPacket(data, ref offset));
|
||||||
@ -117,7 +113,7 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
return subpackets;
|
return subpackets;
|
||||||
}
|
}
|
||||||
|
|
||||||
public unsafe static BasePacketHeader GetHeader(byte[] bytes)
|
public static unsafe BasePacketHeader GetHeader(byte[] bytes)
|
||||||
{
|
{
|
||||||
BasePacketHeader header;
|
BasePacketHeader header;
|
||||||
if (bytes.Length < BASEPACKET_SIZE)
|
if (bytes.Length < BASEPACKET_SIZE)
|
||||||
@ -133,10 +129,10 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public byte[] GetHeaderBytes()
|
public byte[] GetHeaderBytes()
|
||||||
{
|
{
|
||||||
int size = Marshal.SizeOf(header);
|
var size = Marshal.SizeOf(header);
|
||||||
byte[] arr = new byte[size];
|
var arr = new byte[size];
|
||||||
|
|
||||||
IntPtr ptr = Marshal.AllocHGlobal(size);
|
var ptr = Marshal.AllocHGlobal(size);
|
||||||
Marshal.StructureToPtr(header, ptr, true);
|
Marshal.StructureToPtr(header, ptr, true);
|
||||||
Marshal.Copy(ptr, arr, 0, size);
|
Marshal.Copy(ptr, arr, 0, size);
|
||||||
Marshal.FreeHGlobal(ptr);
|
Marshal.FreeHGlobal(ptr);
|
||||||
@ -145,7 +141,7 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public byte[] GetPacketBytes()
|
public byte[] GetPacketBytes()
|
||||||
{
|
{
|
||||||
byte[] outBytes = new byte[header.packetSize];
|
var outBytes = new byte[header.packetSize];
|
||||||
Array.Copy(GetHeaderBytes(), 0, outBytes, 0, BASEPACKET_SIZE);
|
Array.Copy(GetHeaderBytes(), 0, outBytes, 0, BASEPACKET_SIZE);
|
||||||
Array.Copy(data, 0, outBytes, BASEPACKET_SIZE, data.Length);
|
Array.Copy(data, 0, outBytes, BASEPACKET_SIZE, data.Length);
|
||||||
return outBytes;
|
return outBytes;
|
||||||
@ -154,16 +150,18 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
//Replaces all instances of the sniffed actorID with the given one
|
//Replaces all instances of the sniffed actorID with the given one
|
||||||
public void ReplaceActorID(uint actorID)
|
public void ReplaceActorID(uint actorID)
|
||||||
{
|
{
|
||||||
using (MemoryStream mem = new MemoryStream(data))
|
using (var mem = new MemoryStream(data))
|
||||||
{
|
{
|
||||||
using (BinaryWriter binWriter = new BinaryWriter(mem))
|
using (var binWriter = new BinaryWriter(mem))
|
||||||
{
|
{
|
||||||
using (BinaryReader binreader = new BinaryReader(mem))
|
using (var binreader = new BinaryReader(mem))
|
||||||
{
|
{
|
||||||
while (binreader.BaseStream.Position + 4 < data.Length)
|
while (binreader.BaseStream.Position + 4 < data.Length)
|
||||||
{
|
{
|
||||||
uint read = binreader.ReadUInt32();
|
var read = binreader.ReadUInt32();
|
||||||
if (read == 0x029B2941 || read == 0x02977DC7 || read == 0x0297D2C8 || read == 0x0230d573 || read == 0x23317df || read == 0x23344a3 || read == 0x1730bdb || read == 0x6c) //Original ID
|
if (read == 0x029B2941 || read == 0x02977DC7 || read == 0x0297D2C8 || read == 0x0230d573 ||
|
||||||
|
read == 0x23317df || read == 0x23344a3 || read == 0x1730bdb || read == 0x6c)
|
||||||
|
//Original ID
|
||||||
{
|
{
|
||||||
binWriter.BaseStream.Seek(binreader.BaseStream.Position - 0x4, SeekOrigin.Begin);
|
binWriter.BaseStream.Seek(binreader.BaseStream.Position - 0x4, SeekOrigin.Begin);
|
||||||
binWriter.Write(actorID);
|
binWriter.Write(actorID);
|
||||||
@ -177,15 +175,15 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
//Replaces all instances of the sniffed actorID with the given one
|
//Replaces all instances of the sniffed actorID with the given one
|
||||||
public void ReplaceActorID(uint fromActorID, uint actorID)
|
public void ReplaceActorID(uint fromActorID, uint actorID)
|
||||||
{
|
{
|
||||||
using (MemoryStream mem = new MemoryStream(data))
|
using (var mem = new MemoryStream(data))
|
||||||
{
|
{
|
||||||
using (BinaryWriter binWriter = new BinaryWriter(mem))
|
using (var binWriter = new BinaryWriter(mem))
|
||||||
{
|
{
|
||||||
using (BinaryReader binreader = new BinaryReader(mem))
|
using (var binreader = new BinaryReader(mem))
|
||||||
{
|
{
|
||||||
while (binreader.BaseStream.Position + 4 < data.Length)
|
while (binreader.BaseStream.Position + 4 < data.Length)
|
||||||
{
|
{
|
||||||
uint read = binreader.ReadUInt32();
|
var read = binreader.ReadUInt32();
|
||||||
if (read == fromActorID) //Original ID
|
if (read == fromActorID) //Original ID
|
||||||
{
|
{
|
||||||
binWriter.BaseStream.Seek(binreader.BaseStream.Position - 0x4, SeekOrigin.Begin);
|
binWriter.BaseStream.Seek(binreader.BaseStream.Position - 0x4, SeekOrigin.Begin);
|
||||||
@ -197,11 +195,27 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void DebugPrintPacket()
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
logger.ColorDebug(
|
||||||
|
string.Format("IsAuth:{0} IsEncrypted:{1}, Size:0x{2:X}, NumSubpackets:{3}{4}{5}",
|
||||||
|
header.isAuthenticated, header.isCompressed, header.packetSize, header.numSubpackets,
|
||||||
|
Environment.NewLine, Utils.ByteArrayToHex(GetHeaderBytes())), ConsoleOutputColor.DarkYellow);
|
||||||
|
|
||||||
|
foreach (var sub in GetSubpackets())
|
||||||
|
{
|
||||||
|
sub.DebugPrintSubPacket();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
#region Utility Functions
|
#region Utility Functions
|
||||||
|
|
||||||
public static BasePacket CreatePacket(List<SubPacket> subpackets, bool isAuthed, bool isCompressed)
|
public static BasePacket CreatePacket(List<SubPacket> subpackets, bool isAuthed, bool isCompressed)
|
||||||
{
|
{
|
||||||
//Create Header
|
//Create Header
|
||||||
BasePacketHeader header = new BasePacketHeader();
|
var header = new BasePacketHeader();
|
||||||
byte[] data = null;
|
byte[] data = null;
|
||||||
|
|
||||||
header.isAuthenticated = isAuthed ? (byte) 1 : (byte) 0;
|
header.isAuthenticated = isAuthed ? (byte) 1 : (byte) 0;
|
||||||
@ -211,35 +225,35 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
header.timestamp = Utils.MilisUnixTimeStampUTC();
|
header.timestamp = Utils.MilisUnixTimeStampUTC();
|
||||||
|
|
||||||
//Get packet size
|
//Get packet size
|
||||||
foreach (SubPacket subpacket in subpackets)
|
foreach (var subpacket in subpackets)
|
||||||
header.packetSize += subpacket.header.subpacketSize;
|
header.packetSize += subpacket.header.subpacketSize;
|
||||||
|
|
||||||
data = new byte[header.packetSize - 0x10];
|
data = new byte[header.packetSize - 0x10];
|
||||||
|
|
||||||
//Add Subpackets
|
//Add Subpackets
|
||||||
int offset = 0;
|
var offset = 0;
|
||||||
foreach (SubPacket subpacket in subpackets)
|
foreach (var subpacket in subpackets)
|
||||||
{
|
{
|
||||||
byte[] subpacketData = subpacket.GetBytes();
|
var subpacketData = subpacket.GetBytes();
|
||||||
Array.Copy(subpacketData, 0, data, offset, subpacketData.Length);
|
Array.Copy(subpacketData, 0, data, offset, subpacketData.Length);
|
||||||
offset += (ushort) subpacketData.Length;
|
offset += (ushort) subpacketData.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.Assert(data != null && offset == data.Length && header.packetSize == 0x10 + offset);
|
Debug.Assert(data != null && offset == data.Length && header.packetSize == 0x10 + offset);
|
||||||
|
|
||||||
BasePacket packet = new BasePacket(header, data);
|
var packet = new BasePacket(header, data);
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BasePacket CreatePacket(SubPacket subpacket, bool isAuthed, bool isCompressed)
|
public static BasePacket CreatePacket(SubPacket subpacket, bool isAuthed, bool isCompressed)
|
||||||
{
|
{
|
||||||
//Create Header
|
//Create Header
|
||||||
BasePacketHeader header = new BasePacketHeader();
|
var header = new BasePacketHeader();
|
||||||
byte[] data = null;
|
byte[] data = null;
|
||||||
|
|
||||||
header.isAuthenticated = isAuthed ? (byte) 1 : (byte) 0;
|
header.isAuthenticated = isAuthed ? (byte) 1 : (byte) 0;
|
||||||
header.isCompressed = isCompressed ? (byte) 1 : (byte) 0;
|
header.isCompressed = isCompressed ? (byte) 1 : (byte) 0;
|
||||||
header.numSubpackets = (ushort)1;
|
header.numSubpackets = 1;
|
||||||
header.packetSize = BASEPACKET_SIZE;
|
header.packetSize = BASEPACKET_SIZE;
|
||||||
header.timestamp = Utils.MilisUnixTimeStampUTC();
|
header.timestamp = Utils.MilisUnixTimeStampUTC();
|
||||||
|
|
||||||
@ -249,43 +263,43 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
data = new byte[header.packetSize - 0x10];
|
data = new byte[header.packetSize - 0x10];
|
||||||
|
|
||||||
//Add Subpackets
|
//Add Subpackets
|
||||||
byte[] subpacketData = subpacket.GetBytes();
|
var subpacketData = subpacket.GetBytes();
|
||||||
Array.Copy(subpacketData, 0, data, 0, subpacketData.Length);
|
Array.Copy(subpacketData, 0, data, 0, subpacketData.Length);
|
||||||
|
|
||||||
Debug.Assert(data != null);
|
Debug.Assert(data != null);
|
||||||
|
|
||||||
BasePacket packet = new BasePacket(header, data);
|
var packet = new BasePacket(header, data);
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BasePacket CreatePacket(byte[] data, bool isAuthed, bool isCompressed)
|
public static BasePacket CreatePacket(byte[] data, bool isAuthed, bool isCompressed)
|
||||||
{
|
{
|
||||||
|
|
||||||
Debug.Assert(data != null);
|
Debug.Assert(data != null);
|
||||||
|
|
||||||
//Create Header
|
//Create Header
|
||||||
BasePacketHeader header = new BasePacketHeader();
|
var header = new BasePacketHeader();
|
||||||
|
|
||||||
header.isAuthenticated = isAuthed ? (byte) 1 : (byte) 0;
|
header.isAuthenticated = isAuthed ? (byte) 1 : (byte) 0;
|
||||||
header.isCompressed = isCompressed ? (byte) 1 : (byte) 0;
|
header.isCompressed = isCompressed ? (byte) 1 : (byte) 0;
|
||||||
header.numSubpackets = (ushort)1;
|
header.numSubpackets = 1;
|
||||||
header.packetSize = BASEPACKET_SIZE;
|
header.packetSize = BASEPACKET_SIZE;
|
||||||
header.timestamp = Utils.MilisUnixTimeStampUTC();
|
header.timestamp = Utils.MilisUnixTimeStampUTC();
|
||||||
|
|
||||||
//Get packet size
|
//Get packet size
|
||||||
header.packetSize += (ushort) data.Length;
|
header.packetSize += (ushort) data.Length;
|
||||||
|
|
||||||
BasePacket packet = new BasePacket(header, data);
|
var packet = new BasePacket(header, data);
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static unsafe void EncryptPacket(Blowfish blowfish, BasePacket packet)
|
public static unsafe void EncryptPacket(Blowfish blowfish, BasePacket packet)
|
||||||
{
|
{
|
||||||
byte[] data = packet.data;
|
var data = packet.data;
|
||||||
int size = packet.header.packetSize;
|
int size = packet.header.packetSize;
|
||||||
|
|
||||||
int offset = 0;
|
var offset = 0;
|
||||||
while (offset < data.Length) {
|
while (offset < data.Length)
|
||||||
|
{
|
||||||
if (data.Length < offset + SubPacket.SUBPACKET_SIZE)
|
if (data.Length < offset + SubPacket.SUBPACKET_SIZE)
|
||||||
throw new OverflowException("Packet Error: Subpacket was too small");
|
throw new OverflowException("Packet Error: Subpacket was too small");
|
||||||
|
|
||||||
@ -302,15 +316,14 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
offset += header.subpacketSize;
|
offset += header.subpacketSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static unsafe void DecryptPacket(Blowfish blowfish, ref BasePacket packet)
|
public static unsafe void DecryptPacket(Blowfish blowfish, ref BasePacket packet)
|
||||||
{
|
{
|
||||||
byte[] data = packet.data;
|
var data = packet.data;
|
||||||
int size = packet.header.packetSize;
|
int size = packet.header.packetSize;
|
||||||
|
|
||||||
int offset = 0;
|
var offset = 0;
|
||||||
while (offset < data.Length)
|
while (offset < data.Length)
|
||||||
{
|
{
|
||||||
if (data.Length < offset + SubPacket.SUBPACKET_SIZE)
|
if (data.Length < offset + SubPacket.SUBPACKET_SIZE)
|
||||||
@ -330,24 +343,17 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
offset += header.subpacketSize;
|
offset += header.subpacketSize;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
public void DebugPrintPacket()
|
public static class LoggerExtensions
|
||||||
{
|
{
|
||||||
#if DEBUG
|
public static void ColorDebug(this Logger logger, string message, ConsoleOutputColor color)
|
||||||
// todo: create new target for colourful packet logging
|
|
||||||
//Console.BackgroundColor = ConsoleColor.DarkYellow;
|
|
||||||
|
|
||||||
Log.Debug("IsAuth: {0} IsEncrypted: {1}, Size: 0x{2:X}, NumSubpackets: {3}{4}{5}", header.isAuthenticated, header.isCompressed, header.packetSize, header.numSubpackets, Environment.NewLine, Utils.ByteArrayToHex(GetHeaderBytes()));
|
|
||||||
|
|
||||||
foreach (SubPacket sub in GetSubpackets())
|
|
||||||
{
|
{
|
||||||
sub.DebugPrintSubPacket();
|
var logEvent = new LogEventInfo(LogLevel.Debug, logger.Name, message);
|
||||||
}
|
logEvent.Properties["color"] = (int) color;
|
||||||
|
logger.Log(logEvent);
|
||||||
//Console.BackgroundColor = ConsoleColor.Black;
|
}
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using FFXIVClassic.Common;
|
|
||||||
using NLog;
|
|
||||||
|
|
||||||
namespace FFXIVClassic_Map_Server.packets
|
namespace FFXIVClassic_Map_Server.packets
|
||||||
{
|
{
|
||||||
@ -27,13 +25,13 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public class SubPacket
|
public class SubPacket
|
||||||
{
|
{
|
||||||
public static Logger Log = LogManager.GetCurrentClassLogger();
|
|
||||||
public const int SUBPACKET_SIZE = 0x10;
|
public const int SUBPACKET_SIZE = 0x10;
|
||||||
public const int GAMEMESSAGE_SIZE = 0x10;
|
public const int GAMEMESSAGE_SIZE = 0x10;
|
||||||
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
||||||
|
public byte[] data;
|
||||||
|
public GameMessageHeader gameMessage;
|
||||||
|
|
||||||
public SubPacketHeader header;
|
public SubPacketHeader header;
|
||||||
public GameMessageHeader gameMessage;
|
|
||||||
public byte[] data;
|
|
||||||
|
|
||||||
public unsafe SubPacket(byte[] bytes, ref int offset)
|
public unsafe SubPacket(byte[] bytes, ref int offset)
|
||||||
{
|
{
|
||||||
@ -49,7 +47,8 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
{
|
{
|
||||||
fixed (byte* pdata = &bytes[offset + SUBPACKET_SIZE])
|
fixed (byte* pdata = &bytes[offset + SUBPACKET_SIZE])
|
||||||
{
|
{
|
||||||
gameMessage = (GameMessageHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(GameMessageHeader));
|
gameMessage =
|
||||||
|
(GameMessageHeader) Marshal.PtrToStructure(new IntPtr(pdata), typeof(GameMessageHeader));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,8 +71,8 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public SubPacket(ushort opcode, uint sourceId, uint targetId, byte[] data)
|
public SubPacket(ushort opcode, uint sourceId, uint targetId, byte[] data)
|
||||||
{
|
{
|
||||||
this.header = new SubPacketHeader();
|
header = new SubPacketHeader();
|
||||||
this.gameMessage = new GameMessageHeader();
|
gameMessage = new GameMessageHeader();
|
||||||
|
|
||||||
gameMessage.opcode = opcode;
|
gameMessage.opcode = opcode;
|
||||||
header.sourceId = sourceId;
|
header.sourceId = sourceId;
|
||||||
@ -94,8 +93,8 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public SubPacket(SubPacket original, uint newTargetId)
|
public SubPacket(SubPacket original, uint newTargetId)
|
||||||
{
|
{
|
||||||
this.header = new SubPacketHeader();
|
header = new SubPacketHeader();
|
||||||
this.gameMessage = original.gameMessage;
|
gameMessage = original.gameMessage;
|
||||||
header.subpacketSize = original.header.subpacketSize;
|
header.subpacketSize = original.header.subpacketSize;
|
||||||
header.type = original.header.type;
|
header.type = original.header.type;
|
||||||
header.sourceId = original.header.sourceId;
|
header.sourceId = original.header.sourceId;
|
||||||
@ -105,10 +104,10 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public byte[] GetHeaderBytes()
|
public byte[] GetHeaderBytes()
|
||||||
{
|
{
|
||||||
int size = Marshal.SizeOf(header);
|
var size = Marshal.SizeOf(header);
|
||||||
byte[] arr = new byte[size];
|
var arr = new byte[size];
|
||||||
|
|
||||||
IntPtr ptr = Marshal.AllocHGlobal(size);
|
var ptr = Marshal.AllocHGlobal(size);
|
||||||
Marshal.StructureToPtr(header, ptr, true);
|
Marshal.StructureToPtr(header, ptr, true);
|
||||||
Marshal.Copy(ptr, arr, 0, size);
|
Marshal.Copy(ptr, arr, 0, size);
|
||||||
Marshal.FreeHGlobal(ptr);
|
Marshal.FreeHGlobal(ptr);
|
||||||
@ -117,10 +116,10 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public byte[] GetGameMessageBytes()
|
public byte[] GetGameMessageBytes()
|
||||||
{
|
{
|
||||||
int size = Marshal.SizeOf(gameMessage);
|
var size = Marshal.SizeOf(gameMessage);
|
||||||
byte[] arr = new byte[size];
|
var arr = new byte[size];
|
||||||
|
|
||||||
IntPtr ptr = Marshal.AllocHGlobal(size);
|
var ptr = Marshal.AllocHGlobal(size);
|
||||||
Marshal.StructureToPtr(gameMessage, ptr, true);
|
Marshal.StructureToPtr(gameMessage, ptr, true);
|
||||||
Marshal.Copy(ptr, arr, 0, size);
|
Marshal.Copy(ptr, arr, 0, size);
|
||||||
Marshal.FreeHGlobal(ptr);
|
Marshal.FreeHGlobal(ptr);
|
||||||
@ -129,7 +128,7 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
|
|
||||||
public byte[] GetBytes()
|
public byte[] GetBytes()
|
||||||
{
|
{
|
||||||
byte[] outBytes = new byte[header.subpacketSize];
|
var outBytes = new byte[header.subpacketSize];
|
||||||
Array.Copy(GetHeaderBytes(), 0, outBytes, 0, SUBPACKET_SIZE);
|
Array.Copy(GetHeaderBytes(), 0, outBytes, 0, SUBPACKET_SIZE);
|
||||||
|
|
||||||
if (header.type == 0x3)
|
if (header.type == 0x3)
|
||||||
@ -142,23 +141,30 @@ namespace FFXIVClassic_Map_Server.packets
|
|||||||
public void DebugPrintSubPacket()
|
public void DebugPrintSubPacket()
|
||||||
{
|
{
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
// todo: create new target for colourful packet logging
|
logger.ColorDebug(
|
||||||
//Console.BackgroundColor = ConsoleColor.DarkRed;
|
string.Format("Size:0x{0:X} Opcode:0x{1:X}{2}{3}", header.subpacketSize, header.type,
|
||||||
|
Environment.NewLine,
|
||||||
Log.Debug("Size: 0x{0:X}{1}{2}", header.subpacketSize, Environment.NewLine, Utils.ByteArrayToHex(GetHeaderBytes()));
|
Utils.ByteArrayToHex(GetHeaderBytes())), ConsoleOutputColor.DarkRed);
|
||||||
|
|
||||||
if (header.type == 0x03)
|
if (header.type == 0x03)
|
||||||
{
|
{
|
||||||
Log.Debug("Opcode: 0x{0:X}{1}{2}", gameMessage.opcode, Environment.NewLine, Utils.ByteArrayToHex(GetGameMessageBytes(), SUBPACKET_SIZE));
|
logger.ColorDebug(Utils.ByteArrayToHex(GetGameMessageBytes(), SUBPACKET_SIZE),
|
||||||
|
ConsoleOutputColor.DarkRed);
|
||||||
|
|
||||||
//Console.BackgroundColor = ConsoleColor.DarkMagenta;
|
logger.ColorDebug(Utils.ByteArrayToHex(data, SUBPACKET_SIZE + GAMEMESSAGE_SIZE),
|
||||||
|
ConsoleOutputColor.DarkMagenta);
|
||||||
Log.Debug("Data: {0}{1}", Environment.NewLine, Utils.ByteArrayToHex(data, SUBPACKET_SIZE + GAMEMESSAGE_SIZE));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Console.BackgroundColor = ConsoleColor.Black;
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LoggerExtensions
|
||||||
|
{
|
||||||
|
public static void ColorDebug(this Logger logger, string message, ConsoleOutputColor color)
|
||||||
|
{
|
||||||
|
var logEvent = new LogEventInfo(LogLevel.Debug, logger.Name, message);
|
||||||
|
logEvent.Properties["color"] = (int) color;
|
||||||
|
logger.Log(logEvent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user