binding/YGOLegacy.Engine (netstandard2.1, LangVersion 9, unsafe, no package, no Unity
reference) holds the Unity repo's Assets/YGOLegacy/Scripts/Protocol/ as
Protocol/{Contracts,Codecs,Framing,Routing}: 12 files copied byte-identical (LF), namespaces
YGOLegacy.Protocol.* unchanged. EngineVersion (default local) is stamped as the informational
version, so the DLL's ProductVersion is the tag it was built for.
binding/YGOLegacy.Engine.Tests (net8.0, NUnit 3.14) holds the six fixtures. 114 tests pass,
the Unity Edit Mode count. Two deviations from "as-is", lead-approved 2026-10-15:
LegacyReaders.cs is a frozen test-only copy of the legacy MDPro3 / YGOSharp readers and enums
the parity tests compare against (they live only in the Unity repo), and FrameCodecTests.cs
line 35 casts (short) instead of (byte): BitConverter.GetBytes((byte)) is ambiguous on
net8.0 (the Half overload), same bytes written.
The windows job builds and tests the binding after the native gates (both with
-p:EngineVersion so the test's project reference does not restamp the DLL as local), checks
the ProductVersion and stages ygol-engine-binding.zip (YGOLegacy.Engine.dll + .pdb) beside
ygol-engine-win64.zip; attach-windows attaches both. engine.json gains "binding": "<version>"
on every platform (windows.ps1 and the Dockerfile's export-gate, the only writers) and the
release job's agreement check includes it. README gains the Binding section and the asset row;
.gitignore ignores binding/**/bin and obj.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DJUFwdiGCHipsxLkgxssSR
157 lines
4.7 KiB
C#
157 lines
4.7 KiB
C#
// 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
|
|
}
|
|
}
|