using System.Buffers.Binary; using System.Text; namespace Wonderking.Packets.Outgoing; [PacketId(OperationCode.ChannelSelectionResponse)] public class ChannelSelectionResponsePacket : IPacket { public required byte UnknownFlag { get; set; } public required string Endpoint { get; set; } public required ushort Port { get; set; } public required CharacterData[] Characters { get; set; } public void Deserialize(byte[] data) { throw new NotSupportedException(); } public byte[] Serialize() { Span data = stackalloc byte[1 + 16 + 2 + 2 + 132 * this.Characters.Length]; data.Clear(); data[0] = this.UnknownFlag; Encoding.ASCII.GetBytes(this.Endpoint, data.Slice(1, 16)); BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(17, 2), this.Port); BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(19, 2), (ushort)this.Characters.Length); // Character Data for (var i = 0; i < Characters.Length; i++) { var character = Characters[i]; BinaryPrimitives.WriteInt32LittleEndian(data.Slice(21, 4), i); Encoding.ASCII.GetBytes(character.Name, data.Slice(25 + (i * 132), 16)); // Job Data data[41 + (i * 132)] = character.Job.FirstJob; data[42 + (i * 132)] = character.Job.SecondJob; data[43 + (i * 132)] = character.Job.ThirdJob; data[44 + (i * 132)] = character.Job.FourthJob; data[45 + (i * 132)] = character.Gender; data[46 + (i * 132)] = character.Level; data[47 + (i * 132)] = character.Unknown; data[48 + (i * 132)] = (byte)character.Experience; // Stats BinaryPrimitives.WriteInt16LittleEndian(data.Slice(49 + (i * 132), 2), character.Stats.Strength); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(51 + (i * 132), 2), character.Stats.Dexterity); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(53 + (i * 132), 2), character.Stats.Intelligence); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(55 + (i * 132), 2), character.Stats.Vitality); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(57 + (i * 132), 2), character.Stats.Luck); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(59 + (i * 132), 2), character.Stats.Wisdom); BinaryPrimitives.WriteInt32LittleEndian(data.Slice(61 + (i * 132), 4), character.Health); BinaryPrimitives.WriteInt32LittleEndian(data.Slice(65 + (i * 132), 4), character.Mana); for (var j = 0; j < 20; j++) { // Equipped Items BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(69 + (i * 132) + (j * 2), 2), character.EquippedItems[j] ?? 0); // Equipped Cash Items BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(109 + (i * 132) + (j * 2), 2), character.EquippedCashItems[j] ?? 0); } } return data.ToArray(); } }