// Frozen copies of the legacy Unity (MDPro3) and WindBot (YGOSharp) readers the parity tests // compare the shared codec against. Copied verbatim from the Unity repo on 2026-10-15 // (Assets/Scripts/MDPro3/Duel/TcpHelper.cs, Assets/Scripts/YGOSharp/YGOSharp.Network/); // only MDPro3's Debug.Log in its catch is dropped. Test-only; never shipped. using System; using System.IO; using System.Text; namespace MDPro3 { public static class BinaryExtensions { public static void WriteUnicode(this BinaryWriter writer, string text, int len) { try { var unicode = Encoding.Unicode.GetBytes(text); var result = new byte[len * 2]; for (var i = 0; i < result.Length; i++) result[i] = 204; var max = len * 2 - 2; Array.Copy(unicode, result, unicode.Length > max ? max : unicode.Length); result[unicode.Length] = 0; result[unicode.Length + 1] = 0; writer.Write(result); } catch (Exception) { } } public static string ReadUnicode(this BinaryReader reader, int len) { var unicode = reader.ReadBytes(len * 2); var text = Encoding.Unicode.GetString(unicode); text = text[..text.IndexOf('\0')]; return text; } public static string ReadALLUnicode(this BinaryReader reader) { var unicode = reader.ReadToEnd(); var text = Encoding.Unicode.GetString(unicode); text = text.Substring(0, text.IndexOf('\0')); return text; } public static byte[] ReadToEnd(this BinaryReader reader) { return reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); } } } namespace YGOSharp.Network.Utils { public static class BinaryExtensions { // fixed length strings public static void WriteUnicode(this BinaryWriter writer, string text, int len) { byte[] unicode = Encoding.Unicode.GetBytes(text); byte[] result = new byte[len * 2]; int copy = unicode.Length; if (unicode.Length > len * 2 - 2) { copy = len * 2 - 2; #if DEBUG throw new ArgumentException("String '" + text + "' is too long for fixed length " + len + "."); #endif } Array.Copy(unicode, result, copy); writer.Write(result); } // variable length strings public static void WriteUnicodeAutoLength(this BinaryWriter writer, string text, int maxlen) { byte[] result = Encoding.Unicode.GetBytes(text + "\0"); int len = result.Length / 2; if (len > maxlen) { len = maxlen; result[len * 2 - 2] = 0; result[len * 2 - 1] = 0; #if DEBUG throw new ArgumentException("String '" + text + "' is too long for max length " + maxlen + "."); #endif } writer.Write(result, 0, len * 2); } public static string ReadUnicode(this BinaryReader reader, int len) { byte[] unicode = reader.ReadBytes(len * 2); string text = Encoding.Unicode.GetString(unicode); int index = text.IndexOf('\0'); if (index > 0) text = text.Substring(0, index); return text; } public static byte[] ReadToEnd(this BinaryReader reader) { return reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position)); } } } namespace YGOSharp.Network.Enums { public enum CtosMessage { Response = 0x1, UpdateDeck = 0x2, HandResult = 0x3, TpResult = 0x4, PlayerInfo = 0x10, CreateGame = 0x11, JoinGame = 0x12, LeaveGame = 0x13, Surrender = 0x14, TimeConfirm = 0x15, Chat = 0x16, ExternalAddress = 0x17, HsToDuelist = 0x20, HsToObserver = 0x21, HsReady = 0x22, HsNotReady = 0x23, HsKick = 0x24, HsStart = 0x25 } public enum StocMessage { GameMsg = 0x1, ErrorMsg = 0x2, SelectHand = 0x3, SelectTp = 0x4, HandResult = 0x5, TpResult = 0x6, ChangeSide = 0x7, WaitingSide = 0x8, CreateGame = 0x11, JoinGame = 0x12, TypeChange = 0x13, LeaveGame = 0x14, DuelStart = 0x15, DuelEnd = 0x16, Replay = 0x17, TimeLimit = 0x18, Chat = 0x19, HsPlayerEnter = 0x20, HsPlayerChange = 0x21, HsWatchChange = 0x22, TeammateSurrender = 0x23 } }