using System.Buffers.Binary; using System.Text; using Wonderking.Packets.Outgoing.Data; namespace Wonderking.Packets.Outgoing; [PacketId(OperationCode.ChannelSelectionResponse)] public class ChannelSelectionResponsePacket : IPacket { public required byte ChannelIsFullFlag { 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 + 1 + 132 * this.Characters.Length]; data.Clear(); data[0] = this.ChannelIsFullFlag; Encoding.ASCII.GetBytes(this.Endpoint, data.Slice(1, 16)); BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(17, 2), this.Port); data[19] = (byte)this.Characters.Length; // Character Data for (var i = 0; i < Characters.Length; i++) { var character = Characters[i]; BinaryPrimitives.WriteInt32LittleEndian(data.Slice(20 + (i * 132), 4), i); Encoding.ASCII.GetBytes(character.Name, data.Slice(24 + (i * 132), 20)); // Job Data data[44 + (i * 132)] = character.Job.FirstJob; data[45 + (i * 132)] = character.Job.SecondJob; data[46 + (i * 132)] = character.Job.ThirdJob; data[47 + (i * 132)] = character.Job.FourthJob; data[48 + (i * 132)] = (byte)character.Gender; BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(49 + (i * 132), 2), character.Level); data[51 + (i * 132)] = (byte)character.Experience; // Stats BinaryPrimitives.WriteInt16LittleEndian(data.Slice(52 + (i * 132), 2), character.Stats.Strength); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(54 + (i * 132), 2), character.Stats.Dexterity); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(56 + (i * 132), 2), character.Stats.Intelligence); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(58 + (i * 132), 2), character.Stats.Vitality); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(60 + (i * 132), 2), character.Stats.Luck); BinaryPrimitives.WriteInt16LittleEndian(data.Slice(62 + (i * 132), 2), character.Stats.Wisdom); BinaryPrimitives.WriteInt32LittleEndian(data.Slice(64 + (i * 132), 4), character.Health); BinaryPrimitives.WriteInt32LittleEndian(data.Slice(68 + (i * 132), 4), character.Mana); for (var j = 0; j < 20; j++) { // Equipped Items BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(72 + (i * 132) + (j * 2), 2), character.EquippedItems.Length > j ? character.EquippedItems[j] : (ushort)0); // Equipped Cash Items BinaryPrimitives.WriteUInt16LittleEndian(data.Slice(112 + (i * 132) + (j * 2), 2), character.EquippedCashItems.Length > j ? character.EquippedCashItems[j] : (ushort)0); } } return data.ToArray(); } }