using System.Buffers.Binary; using System.Text; namespace NetMessageTests; /// Little-endian writer for message bodies, in the AC wire types the parsers read. internal sealed class Body { private readonly List _b = new(); public byte[] ToArray() => _b.ToArray(); public int Length => _b.Count; public Body U16(ushort v) { Span t = stackalloc byte[2]; BinaryPrimitives.WriteUInt16LittleEndian(t, v); _b.AddRange(t.ToArray()); return this; } public Body U32(uint v) { Span t = stackalloc byte[4]; BinaryPrimitives.WriteUInt32LittleEndian(t, v); _b.AddRange(t.ToArray()); return this; } public Body I32(int v) => U32(unchecked((uint)v)); public Body F32(float v) => U32(BitConverter.SingleToUInt32Bits(v)); public Body Bool(bool v) => U32(v ? 1u : 0u); public Body Bytes(params byte[] v) { _b.AddRange(v); return this; } /// u16 length, Latin-1 bytes, zero padding so 2 + length is a multiple of 4. public Body Str(string s) { byte[] raw = Encoding.Latin1.GetBytes(s); U16((ushort)raw.Length); _b.AddRange(raw); for (int pad = (4 - ((2 + raw.Length) & 3)) & 3; pad > 0; pad--) _b.Add(0); return this; } public Body U32List(params uint[] v) { U32((uint)v.Length); foreach (uint x in v) U32(x); return this; } public Body TableHeader(int count, int buckets = 32) => U16((ushort)count).U16((ushort)buckets); /// A whole top-level message: u32 opcode then this body. public byte[] Message(uint opcode) => new Body().U32(opcode).Bytes(ToArray()).ToArray(); /// A whole game event message: 0xF7B0, object id, sequence, event type, this body. public byte[] GameEvent(uint eventType, uint objectId = 0x50000001, uint sequence = 7) => new Body().U32(0xF7B0).U32(objectId).U32(sequence).U32(eventType).Bytes(ToArray()).ToArray(); } /// One fragment for . internal readonly record struct Frag(uint Sequence, ushort Count, ushort Index, byte[] Data, uint Id = 0x80000001, ushort Queue = 10, int? SizeOverride = null); /// Builds server-to-client datagrams: 20-byte header, optional headers, fragments. internal static class Datagram { public const uint BlobFragments = 0x4, AckSequence = 0x4000, TimeSync = 0x01000000, EchoResponse = 0x04000000, Flow = 0x08000000, RejectRetransmit = 0x2000, Referral = 0x800, ConnectRequest = 0x40000, Retransmission = 0x1; private static uint _packetSeq = 100; /// /// Optional headers are written for the flags given (fixed sizes, zero contents; a reject /// list gets entries). BlobFragments is added when frags are given. /// public static byte[] Build(uint flags, int rejectCount = 0, int? sizeOverride = null, params Frag[] frags) { if (frags.Length > 0) flags |= BlobFragments; var after = new Body(); if ((flags & RejectRetransmit) != 0) { after.U32((uint)rejectCount); for (int i = 0; i < rejectCount; i++) after.U32((uint)(i + 1)); } if ((flags & AckSequence) != 0) after.U32(99); if ((flags & ConnectRequest) != 0) after.Bytes(new byte[32]); if ((flags & TimeSync) != 0) after.Bytes(new byte[8]); if ((flags & EchoResponse) != 0) after.Bytes(new byte[8]); if ((flags & Flow) != 0) after.Bytes(new byte[6]); foreach (Frag f in frags) { after.U32(f.Sequence).U32(f.Id).U16(f.Count).U16((ushort)(f.SizeOverride ?? (16 + f.Data.Length))).U16(f.Index).U16(f.Queue); after.Bytes(f.Data); } byte[] tail = after.ToArray(); var d = new Body().U32(_packetSeq++).U32(flags).U32(0).U16(1).U16(0).U16((ushort)(sizeOverride ?? tail.Length)).U16(0); return d.Bytes(tail).ToArray(); } public static byte[] Frags(params Frag[] frags) => Build(0, 0, null, frags); /// Splits a message into fragments of at most bytes (448 like the server). public static Frag[] Split(uint sequence, byte[] message, int chunk = 448) { int count = Math.Max(1, (message.Length + chunk - 1) / chunk); var list = new Frag[count]; for (int i = 0; i < count; i++) { int off = i * chunk; list[i] = new Frag(sequence, (ushort)count, (ushort)i, message.AsSpan(off, Math.Min(chunk, message.Length - off)).ToArray()); } return list; } }