Initial Commit.

This commit is contained in:
Filip Maj
2015-09-25 18:52:25 -04:00
commit 46c4c26d01
82 changed files with 70188 additions and 0 deletions

View File

@@ -0,0 +1,289 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Diagnostics;
using FFXIVClassic_Lobby_Server.common;
using System.IO;
namespace FFXIVClassic_Lobby_Server.packets
{
[StructLayout(LayoutKind.Sequential)]
public struct BasePacketHeader
{
public byte isAuthenticated;
public byte isEncrypted;
public ushort reserved;
public ushort packetSize;
public ushort numSubpackets;
public uint unknown1; //Id?
public uint unknown2; //Usually 0x13B
}
public class BasePacket{
public const int BASEPACKET_SIZE = 0x10;
public BasePacketHeader header;
public byte[] data;
public unsafe BasePacket(String path)
{
byte[] bytes = File.ReadAllBytes(path);
if (bytes.Length < BASEPACKET_SIZE)
throw new OverflowException("Packet Error: Packet was too small");
fixed (byte* pdata = &bytes[0])
{
header = (BasePacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(BasePacketHeader));
}
if (bytes.Length < header.packetSize)
throw new OverflowException("Packet Error: Packet size didn't equal given size");
int packetSize = header.packetSize;
data = new byte[packetSize - BASEPACKET_SIZE];
Array.Copy(bytes, BASEPACKET_SIZE, data, 0, packetSize - BASEPACKET_SIZE);
}
public unsafe BasePacket(byte[] bytes)
{
if (bytes.Length < BASEPACKET_SIZE)
throw new OverflowException("Packet Error: Packet was too small");
fixed (byte* pdata = &bytes[0])
{
header = (BasePacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(BasePacketHeader));
}
if (bytes.Length < header.packetSize)
throw new OverflowException("Packet Error: Packet size didn't equal given size");
int packetSize = header.packetSize;
data = new byte[packetSize - BASEPACKET_SIZE];
Array.Copy(bytes, BASEPACKET_SIZE, data, 0, packetSize - BASEPACKET_SIZE);
}
public unsafe BasePacket(byte[] bytes, ref int offset)
{
if (bytes.Length < offset + BASEPACKET_SIZE)
throw new OverflowException("Packet Error: Packet was too small");
fixed (byte* pdata = &bytes[offset])
{
header = (BasePacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(BasePacketHeader));
}
int packetSize = header.packetSize;
if (bytes.Length < offset + header.packetSize)
throw new OverflowException("Packet Error: Packet size didn't equal given size");
data = new byte[packetSize - BASEPACKET_SIZE];
Array.Copy(bytes, offset + BASEPACKET_SIZE, data, 0, packetSize - BASEPACKET_SIZE);
offset += packetSize;
}
public BasePacket(BasePacketHeader header, byte[] data)
{
this.header = header;
this.data = data;
}
public List<SubPacket> getSubpackets()
{
List<SubPacket> subpackets = new List<SubPacket>(header.numSubpackets);
int offset = 0;
while (offset < data.Length)
subpackets.Add(new SubPacket(data, ref offset));
return subpackets;
}
public unsafe static BasePacketHeader getHeader(byte[] bytes)
{
BasePacketHeader header;
if (bytes.Length < BASEPACKET_SIZE)
throw new OverflowException("Packet Error: Packet was too small");
fixed (byte* pdata = &bytes[0])
{
header = (BasePacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(BasePacketHeader));
}
return header;
}
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;
}
public byte[] getPacketBytes()
{
byte[] outBytes = new byte[header.packetSize];
Array.Copy(getHeaderBytes(), 0, outBytes, 0, BASEPACKET_SIZE);
Array.Copy(data, 0, outBytes, BASEPACKET_SIZE, data.Length);
return outBytes;
}
#region Utility Functions
public static BasePacket createPacket(List<SubPacket> subpackets, bool isAuthed, bool isEncrypted)
{
//Create Header
BasePacketHeader header = new BasePacketHeader();
byte[] data = null;
header.isAuthenticated = isAuthed?(byte)1:(byte)0;
header.isEncrypted = isEncrypted?(byte)1:(byte)0;
header.numSubpackets = (ushort)subpackets.Count;
header.packetSize = BASEPACKET_SIZE;
//Get packet size
foreach (SubPacket subpacket in subpackets)
header.packetSize += subpacket.header.subpacketSize;
data = new byte[header.packetSize-0x10];
//Add Subpackets
int offset = 0;
foreach (SubPacket subpacket in subpackets)
{
byte[] subpacketData = subpacket.getBytes();
Array.Copy(subpacketData, 0, data, offset, subpacketData.Length);
offset += (ushort)subpacketData.Length;
}
Debug.Assert(data != null && offset == data.Length && header.packetSize == 0x10 + offset);
BasePacket packet = new BasePacket(header, data);
return packet;
}
public static BasePacket createPacket(SubPacket subpacket, bool isAuthed, bool isEncrypted)
{
//Create Header
BasePacketHeader header = new BasePacketHeader();
byte[] data = null;
header.isAuthenticated = isAuthed ? (byte)1 : (byte)0;
header.isEncrypted = isEncrypted ? (byte)1 : (byte)0;
header.numSubpackets = (ushort)1;
header.packetSize = BASEPACKET_SIZE;
//Get packet size
header.packetSize += subpacket.header.subpacketSize;
data = new byte[header.packetSize - 0x10];
//Add Subpackets
byte[] subpacketData = subpacket.getBytes();
Array.Copy(subpacketData, 0, data, 0, subpacketData.Length);
Debug.Assert(data != null);
BasePacket packet = new BasePacket(header, data);
return packet;
}
public static BasePacket createPacket(byte[] data, bool isAuthed, bool isEncrypted)
{
Debug.Assert(data != null);
//Create Header
BasePacketHeader header = new BasePacketHeader();
header.isAuthenticated = isAuthed ? (byte)1 : (byte)0;
header.isEncrypted = isEncrypted ? (byte)1 : (byte)0;
header.numSubpackets = (ushort)1;
header.packetSize = BASEPACKET_SIZE;
//Get packet size
header.packetSize += (ushort)data.Length;
BasePacket packet = new BasePacket(header, data);
return packet;
}
public static unsafe void encryptPacket(Blowfish blowfish, BasePacket packet)
{
byte[] data = packet.data;
int size = packet.header.packetSize;
int offset = 0;
while (offset < data.Length) {
if (data.Length < offset + SubPacket.SUBPACKET_SIZE)
throw new OverflowException("Packet Error: Subpacket was too small");
SubPacketHeader header;
fixed (byte* pdata = &data[offset])
{
header = (SubPacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(SubPacketHeader));
}
if (data.Length < offset + header.subpacketSize)
throw new OverflowException("Packet Error: Subpacket size didn't equal subpacket data");
blowfish.Encipher(data, offset + 0x10, header.subpacketSize-0x10);
offset += header.subpacketSize;
}
}
public static unsafe void decryptPacket(Blowfish blowfish, ref BasePacket packet)
{
byte[] data = packet.data;
int size = packet.header.packetSize;
int offset = 0;
while (offset < data.Length)
{
if (data.Length < offset + SubPacket.SUBPACKET_SIZE)
throw new OverflowException("Packet Error: Subpacket was too small");
SubPacketHeader header;
fixed (byte* pdata = &data[offset])
{
header = (SubPacketHeader)Marshal.PtrToStructure(new IntPtr(pdata), typeof(SubPacketHeader));
}
if (data.Length < offset + header.subpacketSize)
throw new OverflowException("Packet Error: Subpacket size didn't equal subpacket data");
blowfish.Decipher(data, offset + 0x10, header.subpacketSize-0x10);
offset += header.subpacketSize;
}
}
#endregion
public void debugPrintPacket()
{
#if DEBUG
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("IsAuthed: {0}, IsEncrypted: {1}, Size: 0x{2:X}, Num Subpackets: {3}", header.isAuthenticated, header.isEncrypted, header.packetSize, header.numSubpackets);
Console.WriteLine("{0}", Utils.ByteArrayToHex(getHeaderBytes()));
foreach (SubPacket sub in getSubpackets())
sub.debugPrintSubPacket();
Console.BackgroundColor = ConsoleColor.Black;
#endif
}
}
}

View File

@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using FFXIVClassic_Lobby_Server;
using FFXIVClassic_Lobby_Server.common;
namespace FFXIVClassic_Lobby_Server.packets
{
[StructLayout(LayoutKind.Sequential)]
public struct SubPacketHeader
{
public ushort subpacketSize;
public ushort unknown0; //Always 0x03
public uint sourceId;
public uint targetId;
public uint unknown1;
public ushort unknown4; //Always 0x14
public ushort opcode;
public uint unknown5;
public uint timestamp;
public uint unknown6;
}
public class SubPacket
{
public const int SUBPACKET_SIZE = 0x20;
public SubPacketHeader header;
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));
}
if (bytes.Length < offset + header.subpacketSize)
throw new OverflowException("Packet Error: Subpacket size didn't equal subpacket data");
data = new byte[header.subpacketSize - SUBPACKET_SIZE];
Array.Copy(bytes, offset + SUBPACKET_SIZE, data, 0, data.Length);
offset += header.subpacketSize;
}
public SubPacket(ushort opcode, uint sourceId, uint targetId, byte[] data)
{
this.header = new SubPacketHeader();
header.opcode = opcode;
header.sourceId = sourceId;
header.targetId = targetId;
header.timestamp = Utils.UnixTimeStampUTC();
header.unknown0 = 0x03;
header.unknown1 = 0x00;
header.unknown4 = 0x14;
header.unknown5 = 0x00;
header.unknown6 = 0x00;
this.data = data;
header.subpacketSize = (ushort)(0x20 + data.Length);
}
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;
}
public byte[] getBytes()
{
byte[] outBytes = new byte[header.subpacketSize];
Array.Copy(getHeaderBytes(), 0, outBytes, 0, SUBPACKET_SIZE);
Array.Copy(data, 0, outBytes, SUBPACKET_SIZE, data.Length);
return outBytes;
}
public void debugPrintSubPacket()
{
#if DEBUG
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.WriteLine("Size: 0x{0:X}, Opcode: 0x{1:X}", header.subpacketSize, header.opcode);
Console.WriteLine("{0}", Utils.ByteArrayToHex(getHeaderBytes()));
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine("{0}", Utils.ByteArrayToHex(data));
Console.BackgroundColor = ConsoleColor.Black;
#endif
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.receive
{
class HandshakePacket
{
bool invalidPacket = false;
public uint actorID;
public HandshakePacket(byte[] data)
{
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryReader binReader = new BinaryReader(mem))
{
try{
binReader.BaseStream.Seek(4, SeekOrigin.Begin);
actorID = UInt32.Parse(Encoding.ASCII.GetString(binReader.ReadBytes(10)));
}
catch (Exception){
invalidPacket = true;
}
}
}
}
}
}

View File

@@ -0,0 +1,33 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.receive
{
class PingPacket
{
bool invalidPacket = false;
public uint time;
public PingPacket(byte[] data)
{
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryReader binReader = new BinaryReader(mem))
{
try{
time = binReader.ReadUInt32();
}
catch (Exception){
invalidPacket = true;
}
}
}
}
}
}

View File

@@ -0,0 +1,41 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.receive
{
class UpdatePlayerPositionPacket
{
bool invalidPacket = false;
public ulong time;
public float x, y, z, rot;
public ushort moveState; //0: Standing, 1: Walking, 2: Running
public UpdatePlayerPositionPacket(byte[] data)
{
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryReader binReader = new BinaryReader(mem))
{
try{
time = binReader.ReadUInt64();
x = binReader.ReadSingle();
y = binReader.ReadSingle();
z = binReader.ReadSingle();
rot = binReader.ReadSingle();
moveState = binReader.ReadUInt16();
}
catch (Exception){
invalidPacket = true;
}
}
}
}
}
}

View File

@@ -0,0 +1,21 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.actor
{
class AddActorPacket
{
public const ushort OPCODE = 0x00CA;
public const uint PACKET_SIZE = 0x28;
public static SubPacket buildPacket(uint playerActorID, uint actorID)
{
return new SubPacket(OPCODE, playerActorID, actorID, new byte[8]);
}
}
}

View File

@@ -0,0 +1,34 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.actor
{
class MoveActorToPositionPacket
{
public const ushort OPCODE = 0x00CF;
public const uint PACKET_SIZE = 0x48;
public static SubPacket buildPacket(uint playerActorID, uint actorID)
{
byte[] data = new byte[PACKET_SIZE - 0x20];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
}
data = mem.GetBuffer();
}
SubPacket packet = new SubPacket(OPCODE, playerActorID, actorID, data);
return packet;
}
}
}

View File

@@ -0,0 +1,21 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.actor
{
class RemoveActorPacket
{
public const ushort OPCODE = 0x00CB;
public const uint PACKET_SIZE = 0x28;
public static SubPacket buildPacket(uint playerActorID, uint actorID)
{
return new SubPacket(OPCODE, playerActorID, actorID, new byte[8]);
}
}
}

View File

@@ -0,0 +1,79 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.actor
{
class SetActorAppearancePacket
{
public const ushort OPCODE = 0x00D6;
public const uint PACKET_SIZE = 0x128;
public const int SIZE = 0;
public const int COLORINFO = 1;
public const int FACEINFO = 2;
public const int HIGHLIGHT_HAIR = 3;
public const int VOICE = 4;
public const int WEAPON1 = 5;
public const int WEAPON2 = 6;
public const int WEAPON3 = 7;
public const int HEADGEAR = 8;
public const int BODYGEAR = 9;
public const int LEGSGEAR = 10;
public const int HANDSGEAR = 11;
public const int FEETGEAR = 12;
public const int WAISTGEAR = 13;
public const int UNKNOWN1 = 14;
public const int R_EAR = 15;
public const int L_EAR = 16;
public const int UNKNOWN2 = 17;
public const int UNKNOWN3 = 18;
public const int R_FINGER = 19;
public const int L_FINGER = 20;
public uint modelID;
public uint[] appearanceIDs;
public SetActorAppearancePacket(uint monsterModelID)
{
modelID = monsterModelID;
appearanceIDs = new uint[22];
}
public SetActorAppearancePacket(uint[] appearanceTable)
{
appearanceIDs = appearanceTable;
}
public SubPacket buildPacket(uint playerActorID, uint actorID)
{
byte[] data = new byte[PACKET_SIZE - 0x20];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
binWriter.Write((uint)modelID);
for (int i = 0; i < 0x1A; i++)
{
binWriter.Write((uint)i);
binWriter.Write((uint)appearanceIDs[i]);
}
binWriter.Write((uint) 0x1B);
binWriter.Seek(0x20, SeekOrigin.Current);
binWriter.Write((uint) 0x1C);
binWriter.Write((uint) 0x00);
}
data = mem.GetBuffer();
}
SubPacket packet = new SubPacket(OPCODE, playerActorID, actorID, data);
return packet;
}
}
}

View File

@@ -0,0 +1,34 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.actor
{
class SetActorPositionPacket
{
public const ushort OPCODE = 0x00CE;
public const uint PACKET_SIZE = 0x48;
public static SubPacket buildPacket(uint playerActorID, uint actorID)
{
byte[] data = new byte[PACKET_SIZE-0x20];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
}
data = mem.GetBuffer();
}
SubPacket packet = new SubPacket(OPCODE, playerActorID, actorID, data);
return packet;
}
}
}

View File

@@ -0,0 +1,35 @@
using FFXIVClassic_Lobby_Server.common;
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.receive
{
class PongPacket
{
public const ushort OPCODE = 0x0001;
public const uint PACKET_SIZE = 0x40;
public static SubPacket buildPacket(uint playerActorID, ulong pingTicks)
{
byte[] data = new byte[PACKET_SIZE-0x20];
using (MemoryStream mem = new MemoryStream(data))
{
using(BinaryWriter binWriter = new BinaryWriter(mem))
{
ulong time = pingTicks;
binWriter.Write(time);
}
}
SubPacket subpacket = new SubPacket(OPCODE, playerActorID, playerActorID, data);
return subpacket;
}
}
}

View File

@@ -0,0 +1,33 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send
{
class SetMapPacket
{
public const ushort OPCODE = 0x0005;
public const uint PACKET_SIZE = 0x30;
public static SubPacket buildPacket(uint playerActorID, uint mapID)
{
byte[] data = new byte[PACKET_SIZE - 0x20];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
binWriter.Write((uint)mapID);
binWriter.Write((uint)0x9B);
binWriter.Write((uint)0x28);
}
}
return new SubPacket(OPCODE, playerActorID, playerActorID, data);
}
}
}

View File

@@ -0,0 +1,21 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send
{
class SetMusicPacket
{
public const ushort OPCODE = 0x000C;
public const uint PACKET_SIZE = 0x28;
public static SubPacket buildPacket(uint playerActorID, uint musicID, uint musicTrackMode)
{
ulong combined = musicID | (musicTrackMode << 32);
return new SubPacket(OPCODE, 0, playerActorID, BitConverter.GetBytes(combined));
}
}
}

View File

@@ -0,0 +1,20 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send
{
class SetWeatherPacket
{
public const ushort OPCODE = 0x000D;
public const uint PACKET_SIZE = 0x28;
public static SubPacket buildPacket(uint playerActorID, long weatherID)
{
return new SubPacket(OPCODE, 0, playerActorID, BitConverter.GetBytes(weatherID));
}
}
}

View File

@@ -0,0 +1,32 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.login
{
class _0x2Packet
{
public const ushort OPCODE = 0x0002;
public const uint PACKET_SIZE = 0x30;
public static SubPacket buildPacket(uint playerActorID)
{
byte[] data = new byte[PACKET_SIZE-0x20];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
binWriter.BaseStream.Seek(0x8, SeekOrigin.Begin);
binWriter.Write((uint)playerActorID);
}
}
return new SubPacket(OPCODE, playerActorID, playerActorID, data);
}
}
}

View File

@@ -0,0 +1,41 @@
using FFXIVClassic_Lobby_Server.packets;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FFXIVClassic_Map_Server.packets.send.login
{
class InitPacket
{
public static BasePacket buildPacket(uint unknown, uint time)
{
byte[] data = new byte[18];
using (MemoryStream mem = new MemoryStream(data))
{
using (BinaryWriter binWriter = new BinaryWriter(mem))
{
try
{
binWriter.Write((short)0x18);
binWriter.Write((short)0x7);
binWriter.Write((uint)0);
binWriter.Write((uint)0);
binWriter.Write((uint)0xFFFFFD7F);
binWriter.Write((uint)unknown);
binWriter.Write((uint)time);
}
catch (Exception)
{
}
}
}
return BasePacket.createPacket(data, false, false);
}
}
}