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
407 lines
17 KiB
C#
407 lines
17 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using NUnit.Framework;
|
|
using YGOLegacy.Protocol.Codecs;
|
|
using YGOLegacy.Protocol.Contracts;
|
|
|
|
namespace YGOLegacy.Protocol.Tests
|
|
{
|
|
public class StocCodecTests
|
|
{
|
|
private static byte[] Bytes(Action<BinaryWriter> write)
|
|
{
|
|
using (var stream = new MemoryStream())
|
|
using (var writer = new BinaryWriter(stream))
|
|
{
|
|
write(writer);
|
|
writer.Flush();
|
|
return stream.ToArray();
|
|
}
|
|
}
|
|
|
|
private static BinaryReader Reader(byte[] bytes)
|
|
{
|
|
return new BinaryReader(new MemoryStream(bytes));
|
|
}
|
|
|
|
private static byte[] Utf16(string text)
|
|
{
|
|
return Encoding.Unicode.GetBytes(text);
|
|
}
|
|
|
|
private static byte[] Concat(params byte[][] parts)
|
|
{
|
|
return parts.SelectMany(p => p).ToArray();
|
|
}
|
|
|
|
private static byte[] Le16(int value)
|
|
{
|
|
return new[] { (byte)value, (byte)(value >> 8) };
|
|
}
|
|
|
|
private static byte[] Le32(int value)
|
|
{
|
|
return new[] { (byte)value, (byte)(value >> 8), (byte)(value >> 16), (byte)(value >> 24) };
|
|
}
|
|
|
|
private static byte[] Zeros(int count)
|
|
{
|
|
return new byte[count];
|
|
}
|
|
|
|
[Test]
|
|
public void Ids_MatchTheStocCatalog()
|
|
{
|
|
Assert.That(StocCodecs.ErrorMsg.Id, Is.EqualTo(StocMessage.ErrorMsg));
|
|
Assert.That(StocCodecs.HandResult.Id, Is.EqualTo(StocMessage.HandResult));
|
|
Assert.That(StocCodecs.JoinGame.Id, Is.EqualTo(StocMessage.JoinGame));
|
|
Assert.That(StocCodecs.TypeChange.Id, Is.EqualTo(StocMessage.TypeChange));
|
|
Assert.That(StocCodecs.TimeLimit.Id, Is.EqualTo(StocMessage.TimeLimit));
|
|
Assert.That(StocCodecs.Chat.Id, Is.EqualTo(StocMessage.Chat));
|
|
Assert.That(StocCodecs.HsPlayerEnter.Id, Is.EqualTo(StocMessage.HsPlayerEnter));
|
|
Assert.That(StocCodecs.HsPlayerChange.Id, Is.EqualTo(StocMessage.HsPlayerChange));
|
|
Assert.That(StocCodecs.HsWatchChange.Id, Is.EqualTo(StocMessage.HsWatchChange));
|
|
Assert.That(StocCodecs.DeckCount.Id, Is.EqualTo(StocMessage.DeckCount));
|
|
}
|
|
|
|
[Test]
|
|
public void ErrorMsg_GoldenBytes_MatchTheNativeStruct()
|
|
{
|
|
var code = (4 << 28) | 12345;
|
|
var bytes = Bytes(w => StocCodecs.ErrorMsg.Write(w, new ErrorMsg(2, code)));
|
|
|
|
Assert.That(bytes.Length, Is.EqualTo(8));
|
|
Assert.That(bytes, Is.EqualTo(Concat(new byte[] { 2 }, Zeros(3), Le32(code))));
|
|
|
|
var read = StocCodecs.ErrorMsg.Read(Reader(bytes));
|
|
Assert.That(read.Msg, Is.EqualTo(2));
|
|
Assert.That(read.Code, Is.EqualTo(code));
|
|
Assert.That(read.Code >> 28, Is.EqualTo(4));
|
|
Assert.That(read.Code & 0xFFFFFFF, Is.EqualTo(12345));
|
|
}
|
|
|
|
[Test]
|
|
public void ErrorMsg_ReadsWhatBothLegacyHandlersRead()
|
|
{
|
|
var bytes = Concat(new byte[] { 1 }, new byte[] { 0xAA, 0xBB, 0xCC }, Le32(2));
|
|
var legacy = Reader(bytes);
|
|
int legacyMsg = legacy.ReadByte();
|
|
legacy.ReadByte();
|
|
legacy.ReadByte();
|
|
legacy.ReadByte();
|
|
var legacyCode = legacy.ReadInt32();
|
|
|
|
var read = StocCodecs.ErrorMsg.Read(Reader(bytes));
|
|
Assert.That(read.Msg, Is.EqualTo(legacyMsg));
|
|
Assert.That(read.Code, Is.EqualTo(legacyCode));
|
|
}
|
|
|
|
[Test]
|
|
public void HandResult_GoldenBytes_AndShortPacket()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.HandResult.Write(w, new HandResultAnnounce(1, 3)));
|
|
Assert.That(bytes, Is.EqualTo(new byte[] { 1, 3 }));
|
|
|
|
var read = StocCodecs.HandResult.Read(Reader(bytes));
|
|
Assert.That(read.First, Is.EqualTo(1));
|
|
Assert.That(read.Second, Is.EqualTo(3));
|
|
|
|
var shortRead = StocCodecs.HandResult.Read(Reader(new byte[] { 2 }));
|
|
Assert.That(shortRead.First, Is.EqualTo(2));
|
|
Assert.That(shortRead.Second, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public void HostInfo_GoldenBytes_MatchTheNative20ByteStruct()
|
|
{
|
|
var info = new HostInfo(0xDEADBEEF, 5, 2, 5, true, false, 8000, 5, 1, 180);
|
|
var bytes = Bytes(w => StocCodecs.JoinGame.Write(w, info));
|
|
|
|
Assert.That(bytes.Length, Is.EqualTo(20));
|
|
Assert.That(bytes, Is.EqualTo(Concat(
|
|
Le32(unchecked((int)0xDEADBEEF)),
|
|
new byte[] { 5, 2, 5, 1, 0 },
|
|
Zeros(3),
|
|
Le32(8000),
|
|
new byte[] { 5, 1 },
|
|
Le16(180))));
|
|
Assert.That(bytes.Skip(9).Take(3), Is.All.EqualTo(0));
|
|
|
|
var read = StocCodecs.JoinGame.Read(Reader(bytes));
|
|
Assert.That(read.Lflist, Is.EqualTo(0xDEADBEEF));
|
|
Assert.That(read.Rule, Is.EqualTo(5));
|
|
Assert.That(read.Mode, Is.EqualTo(2));
|
|
Assert.That(read.DuelRule, Is.EqualTo(5));
|
|
Assert.That(read.NoCheckDeck, Is.True);
|
|
Assert.That(read.NoShuffleDeck, Is.False);
|
|
Assert.That(read.StartLp, Is.EqualTo(8000));
|
|
Assert.That(read.StartHand, Is.EqualTo(5));
|
|
Assert.That(read.DrawCount, Is.EqualTo(1));
|
|
Assert.That(read.TimeLimit, Is.EqualTo(180));
|
|
}
|
|
|
|
[Test]
|
|
public void HostInfo_ReadsWhatUnitysLegacyFieldSequenceRead()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.JoinGame.Write(w, new HostInfo(7, 0, 1, 4, false, true, 16000, 4, 2, 240)));
|
|
|
|
var legacy = Reader(bytes);
|
|
var lflist = legacy.ReadUInt32();
|
|
var rule = legacy.ReadByte();
|
|
var mode = legacy.ReadByte();
|
|
int masterRule = legacy.ReadChar();
|
|
var noCheck = legacy.ReadBoolean();
|
|
var noShuffle = legacy.ReadBoolean();
|
|
legacy.ReadByte();
|
|
legacy.ReadByte();
|
|
legacy.ReadByte();
|
|
var startLp = legacy.ReadInt32();
|
|
var startHand = legacy.ReadByte();
|
|
var drawCount = legacy.ReadByte();
|
|
var timeLimit = legacy.ReadInt16();
|
|
|
|
var read = StocCodecs.JoinGame.Read(Reader(bytes));
|
|
Assert.That(read.Lflist, Is.EqualTo(lflist));
|
|
Assert.That(read.Rule, Is.EqualTo(rule));
|
|
Assert.That(read.Mode, Is.EqualTo(mode));
|
|
Assert.That((int)read.DuelRule, Is.EqualTo(masterRule));
|
|
Assert.That(read.NoCheckDeck, Is.EqualTo(noCheck));
|
|
Assert.That(read.NoShuffleDeck, Is.EqualTo(noShuffle));
|
|
Assert.That(read.StartLp, Is.EqualTo(startLp));
|
|
Assert.That(read.StartHand, Is.EqualTo(startHand));
|
|
Assert.That(read.DrawCount, Is.EqualTo(drawCount));
|
|
Assert.That((short)read.TimeLimit, Is.EqualTo(timeLimit));
|
|
}
|
|
|
|
[Test]
|
|
public void TypeChange_NibblesRoundTrip()
|
|
{
|
|
for (byte position = 0; position < 4; position++)
|
|
{
|
|
foreach (var isHost in new[] { false, true })
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.TypeChange.Write(w, new TypeChange(position, isHost)));
|
|
Assert.That(bytes, Is.EqualTo(new[] { (byte)(position | (isHost ? 0x10 : 0)) }));
|
|
|
|
var read = StocCodecs.TypeChange.Read(Reader(bytes));
|
|
Assert.That(read.Position, Is.EqualTo(position));
|
|
Assert.That(read.IsHost, Is.EqualTo(isHost));
|
|
}
|
|
}
|
|
|
|
var observer = StocCodecs.TypeChange.Read(Reader(new byte[] { 0x07 }));
|
|
Assert.That(observer.Position, Is.EqualTo(7));
|
|
Assert.That(observer.IsHost, Is.False);
|
|
|
|
var hostByOtherBit = StocCodecs.TypeChange.Read(Reader(new byte[] { 0x21 }));
|
|
Assert.That(hostByOtherBit.Position, Is.EqualTo(1));
|
|
Assert.That(hostByOtherBit.IsHost, Is.True);
|
|
}
|
|
|
|
[Test]
|
|
public void TimeLimit_GoldenBytes_AndShortPackets()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.TimeLimit.Write(w, new TimeLimit(1, 300)));
|
|
Assert.That(bytes, Is.EqualTo(Concat(new byte[] { 1, 0 }, Le16(300))));
|
|
|
|
var read = StocCodecs.TimeLimit.Read(Reader(bytes));
|
|
Assert.That(read.Player, Is.EqualTo(1));
|
|
Assert.That(read.LeftTime, Is.EqualTo(300));
|
|
|
|
var twoBytes = StocCodecs.TimeLimit.Read(Reader(new byte[] { 0, 0 }));
|
|
Assert.That(twoBytes.Player, Is.EqualTo(0));
|
|
Assert.That(twoBytes.LeftTime, Is.EqualTo(0));
|
|
|
|
var oneByte = StocCodecs.TimeLimit.Read(Reader(new byte[] { 1 }));
|
|
Assert.That(oneByte.Player, Is.EqualTo(1));
|
|
Assert.That(oneByte.LeftTime, Is.EqualTo(0));
|
|
}
|
|
|
|
[Test]
|
|
public void ServerChat_GoldenBytes_PlayerThenTextThenTerminator()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.Chat.Write(w, new ServerChat(1, "hi")));
|
|
Assert.That(bytes, Is.EqualTo(Concat(Le16(1), Utf16("hi"), Zeros(2))));
|
|
|
|
var read = StocCodecs.Chat.Read(Reader(bytes));
|
|
Assert.That(read.Player, Is.EqualTo(1));
|
|
Assert.That(read.Text, Is.EqualTo("hi"));
|
|
|
|
var system = StocCodecs.Chat.Read(Reader(Concat(Le16(8), Utf16("watch"), Zeros(2))));
|
|
Assert.That(system.Player, Is.EqualTo(8));
|
|
Assert.That(system.Text, Is.EqualTo("watch"));
|
|
}
|
|
|
|
[Test]
|
|
public void ServerChat_ReadsWhatBothLegacyHandlersRead()
|
|
{
|
|
var bytes = Concat(Le16(2), Utf16("twenty byte chat msg"), Zeros(2));
|
|
|
|
var unity = Reader(bytes);
|
|
int unityPlayer = unity.ReadInt16();
|
|
var unityText = MDPro3.BinaryExtensions.ReadUnicode(unity, (int)((unity.BaseStream.Length - unity.BaseStream.Position) / 2));
|
|
|
|
var windbot = Reader(bytes);
|
|
int windbotPlayer = windbot.ReadInt16();
|
|
var windbotText = YGOSharp.Network.Utils.BinaryExtensions.ReadUnicode(windbot, 256);
|
|
|
|
var read = StocCodecs.Chat.Read(Reader(bytes));
|
|
Assert.That(read.Player, Is.EqualTo(unityPlayer));
|
|
Assert.That(read.Player, Is.EqualTo(windbotPlayer));
|
|
Assert.That(read.Text, Is.EqualTo(unityText));
|
|
Assert.That(read.Text, Is.EqualTo(windbotText));
|
|
}
|
|
|
|
[Test]
|
|
public void ServerChat_WithoutTerminator_ReadsTheWholeTail()
|
|
{
|
|
var bytes = Concat(Le16(0), Utf16("no terminator"));
|
|
|
|
var read = StocCodecs.Chat.Read(Reader(bytes));
|
|
Assert.That(read.Text, Is.EqualTo("no terminator"));
|
|
|
|
var unity = Reader(bytes);
|
|
unity.ReadInt16();
|
|
Assert.Throws<ArgumentOutOfRangeException>(() =>
|
|
MDPro3.BinaryExtensions.ReadUnicode(unity, (int)((unity.BaseStream.Length - unity.BaseStream.Position) / 2)));
|
|
}
|
|
|
|
[Test]
|
|
public void ServerChat_EmptyText_ReadsEmpty()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.Chat.Write(w, new ServerChat(0, "")));
|
|
Assert.That(bytes, Is.EqualTo(Concat(Le16(0), Zeros(2))));
|
|
Assert.That(StocCodecs.Chat.Read(Reader(bytes)).Text, Is.EqualTo(""));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerEnter_GoldenBytes_MatchThePacked41ByteStruct()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.HsPlayerEnter.Write(w, new PlayerEnter("Joey", 1)));
|
|
|
|
Assert.That(bytes.Length, Is.EqualTo(41));
|
|
Assert.That(bytes, Is.EqualTo(Concat(Utf16("Joey"), Zeros(32), new byte[] { 1 })));
|
|
|
|
var read = StocCodecs.HsPlayerEnter.Read(Reader(bytes));
|
|
Assert.That(read.Name, Is.EqualTo("Joey"));
|
|
Assert.That(read.Position, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerEnter_ReadsWhatBothLegacyHandlersRead()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.HsPlayerEnter.Write(w, new PlayerEnter("Mai Valentine B", 0)));
|
|
|
|
var unity = Reader(bytes);
|
|
var unityName = MDPro3.BinaryExtensions.ReadUnicode(unity, 20);
|
|
var unityPos = unity.ReadByte() & 3;
|
|
|
|
var windbot = Reader(bytes);
|
|
var windbotName = YGOSharp.Network.Utils.BinaryExtensions.ReadUnicode(windbot, 20);
|
|
int windbotPos = windbot.ReadByte();
|
|
|
|
var read = StocCodecs.HsPlayerEnter.Read(Reader(bytes));
|
|
Assert.That(read.Name, Is.EqualTo(unityName));
|
|
Assert.That(read.Name, Is.EqualTo(windbotName));
|
|
Assert.That(read.Position & 3, Is.EqualTo(unityPos));
|
|
Assert.That((int)read.Position, Is.EqualTo(windbotPos));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerEnter_EmptyName_ReadsEmpty_WhereYgoSharpReadTwentyNuls()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.HsPlayerEnter.Write(w, new PlayerEnter("", 2)));
|
|
Assert.That(bytes, Is.EqualTo(Concat(Zeros(40), new byte[] { 2 })));
|
|
|
|
var read = StocCodecs.HsPlayerEnter.Read(Reader(bytes));
|
|
Assert.That(read.Name, Is.EqualTo(""));
|
|
Assert.That(read.Position, Is.EqualTo(2));
|
|
|
|
var legacy = YGOSharp.Network.Utils.BinaryExtensions.ReadUnicode(Reader(bytes), 20);
|
|
Assert.That(legacy.Length, Is.EqualTo(20));
|
|
Assert.That(legacy, Is.All.EqualTo('\0'));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerEnter_NameClipsToNineteenAndAFullNameDoesNotThrow()
|
|
{
|
|
var longName = new string('x', 25);
|
|
var bytes = Bytes(w => StocCodecs.HsPlayerEnter.Write(w, new PlayerEnter(longName, 3)));
|
|
Assert.That(bytes.Length, Is.EqualTo(41));
|
|
Assert.That(bytes.Skip(38).Take(2), Is.All.EqualTo(0));
|
|
Assert.That(StocCodecs.HsPlayerEnter.Read(Reader(bytes)).Name, Is.EqualTo(new string('x', 19)));
|
|
|
|
var unterminated = Concat(Utf16(new string('y', 20)), new byte[] { 0 });
|
|
var read = StocCodecs.HsPlayerEnter.Read(Reader(unterminated));
|
|
Assert.That(read.Name, Is.EqualTo(new string('y', 20)));
|
|
Assert.That(read.Position, Is.EqualTo(0));
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => MDPro3.BinaryExtensions.ReadUnicode(Reader(unterminated), 20));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerChange_NibblesRoundTrip()
|
|
{
|
|
var ready = Bytes(w => StocCodecs.HsPlayerChange.Write(w, new PlayerChange(2, 0x9)));
|
|
Assert.That(ready, Is.EqualTo(new byte[] { 0x29 }));
|
|
|
|
var read = StocCodecs.HsPlayerChange.Read(Reader(ready));
|
|
Assert.That(read.Position, Is.EqualTo(2));
|
|
Assert.That(read.State, Is.EqualTo(0x9));
|
|
|
|
var observe = StocCodecs.HsPlayerChange.Read(Reader(new byte[] { 0x38 }));
|
|
Assert.That(observe.Position, Is.EqualTo(3));
|
|
Assert.That(observe.State, Is.EqualTo(0x8));
|
|
|
|
var move = StocCodecs.HsPlayerChange.Read(Reader(new byte[] { 0x01 }));
|
|
Assert.That(move.Position, Is.EqualTo(0));
|
|
Assert.That(move.State, Is.EqualTo(1));
|
|
|
|
var leave = Bytes(w => StocCodecs.HsPlayerChange.Write(w, new PlayerChange(1, 0xB)));
|
|
Assert.That(leave, Is.EqualTo(new byte[] { 0x1B }));
|
|
}
|
|
|
|
[TestCase(0x29, 2, PlayerChangeKind.Ready, 0)]
|
|
[TestCase(0x38, 3, PlayerChangeKind.Observe, 0)]
|
|
[TestCase(0x1B, 1, PlayerChangeKind.Leave, 0)]
|
|
[TestCase(0x1A, 1, PlayerChangeKind.NotReady, 0)]
|
|
[TestCase(0x10, 1, PlayerChangeKind.MoveTo, 0)]
|
|
[TestCase(0x03, 0, PlayerChangeKind.MoveTo, 3)]
|
|
[TestCase(0x2F, 2, PlayerChangeKind.Unknown, 0)]
|
|
public void PlayerChange_Kind_IsDerivedFromTheStateNibble_AsTheLobbyHandlerDid(int status, int seat, PlayerChangeKind kind, int target)
|
|
{
|
|
var read = StocCodecs.HsPlayerChange.Read(Reader(new byte[] { (byte)status }));
|
|
|
|
Assert.That(read.Position, Is.EqualTo(seat));
|
|
Assert.That(read.Kind, Is.EqualTo(kind));
|
|
Assert.That(read.TargetSeat, Is.EqualTo(target));
|
|
}
|
|
|
|
[Test]
|
|
public void WatchChange_GoldenBytes_LittleEndianUInt16()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.HsWatchChange.Write(w, new WatchChange(258)));
|
|
Assert.That(bytes, Is.EqualTo(new byte[] { 0x02, 0x01 }));
|
|
Assert.That(StocCodecs.HsWatchChange.Read(Reader(bytes)).Count, Is.EqualTo(258));
|
|
}
|
|
|
|
[Test]
|
|
public void DeckCount_GoldenBytes_InTheServersOrder()
|
|
{
|
|
var bytes = Bytes(w => StocCodecs.DeckCount.Write(w, new DeckCount(40, 15, 10, 41, 14, 9)));
|
|
|
|
Assert.That(bytes.Length, Is.EqualTo(12));
|
|
Assert.That(bytes, Is.EqualTo(Concat(Le16(40), Le16(15), Le16(10), Le16(41), Le16(14), Le16(9))));
|
|
|
|
var read = StocCodecs.DeckCount.Read(Reader(bytes));
|
|
Assert.That(read.Main0, Is.EqualTo(40));
|
|
Assert.That(read.Extra0, Is.EqualTo(15));
|
|
Assert.That(read.Side0, Is.EqualTo(10));
|
|
Assert.That(read.Main1, Is.EqualTo(41));
|
|
Assert.That(read.Extra1, Is.EqualTo(14));
|
|
Assert.That(read.Side1, Is.EqualTo(9));
|
|
}
|
|
}
|
|
}
|