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
305 lines
12 KiB
C#
305 lines
12 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 ProtocolCodecTests
|
|
{
|
|
private const ushort Version = 0x1362;
|
|
|
|
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 void AssertEqualExceptPadding(byte[] ours, byte[] legacy, int paddingFrom, byte legacyPad)
|
|
{
|
|
Assert.That(ours.Length, Is.EqualTo(legacy.Length));
|
|
Assert.That(ours.Take(paddingFrom), Is.EqualTo(legacy.Take(paddingFrom)));
|
|
Assert.That(ours.Skip(paddingFrom), Is.All.EqualTo(0));
|
|
Assert.That(legacy.Skip(paddingFrom), Is.All.EqualTo(legacyPad));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerInfo_WritesFortyZeroPaddedBytes()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.PlayerInfo.Write(w, new PlayerInfo("Alice")));
|
|
|
|
Assert.That(ours, Is.EqualTo(Concat(Utf16("Alice\0"), new byte[28])));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerInfo_MatchesWindBotExactly_AndUnityExceptPadding()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.PlayerInfo.Write(w, new PlayerInfo("Alice")));
|
|
var windbot = Bytes(w => YGOSharp.Network.Utils.BinaryExtensions.WriteUnicode(w, "Alice", 20));
|
|
var unity = Bytes(w => MDPro3.BinaryExtensions.WriteUnicode(w, "Alice", 20));
|
|
|
|
Assert.That(ours, Is.EqualTo(windbot));
|
|
AssertEqualExceptPadding(ours, unity, 12, 0xCC);
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerInfo_OverlongName_KeepsNineteenUnitsAndTerminator()
|
|
{
|
|
var name = new string('n', 25);
|
|
byte[] ours = null;
|
|
|
|
Assert.DoesNotThrow(() => ours = Bytes(w => LobbyCodecs.PlayerInfo.Write(w, new PlayerInfo(name))));
|
|
Assert.That(ours.Length, Is.EqualTo(40));
|
|
Assert.That(ours.Skip(38), Is.All.EqualTo(0));
|
|
Assert.That(LobbyCodecs.PlayerInfo.Read(Reader(ours)).Name, Is.EqualTo(name.Substring(0, 19)));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerInfo_EmojiAtTheBoundary_IsNotSplit()
|
|
{
|
|
var name = new string('a', 18) + "\U0001F600";
|
|
var ours = Bytes(w => LobbyCodecs.PlayerInfo.Write(w, new PlayerInfo(name)));
|
|
|
|
Assert.That(ours.Length, Is.EqualTo(40));
|
|
Assert.That(ours.Skip(36), Is.All.EqualTo(0));
|
|
Assert.That(LobbyCodecs.PlayerInfo.Read(Reader(ours)).Name, Is.EqualTo(new string('a', 18)));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerInfo_EmojiThatFits_RoundTrips()
|
|
{
|
|
var name = new string('a', 17) + "\U0001F600";
|
|
var ours = Bytes(w => LobbyCodecs.PlayerInfo.Write(w, new PlayerInfo(name)));
|
|
|
|
Assert.That(ours.Length, Is.EqualTo(40));
|
|
Assert.That(LobbyCodecs.PlayerInfo.Read(Reader(ours)).Name, Is.EqualTo(name));
|
|
}
|
|
|
|
[Test]
|
|
public void PlayerInfo_NullName_WritesZeros()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.PlayerInfo.Write(w, default(PlayerInfo)));
|
|
|
|
Assert.That(ours, Is.EqualTo(new byte[40]));
|
|
}
|
|
|
|
[Test]
|
|
public void ReadFixed_TerminatorAtZero_ReadsEmpty()
|
|
{
|
|
Assert.That(UnicodeCodec.ReadFixed(Reader(new byte[40]), 20), Is.EqualTo(string.Empty));
|
|
}
|
|
|
|
[Test]
|
|
public void ReadFixed_NoTerminator_ReadsWholeBuffer()
|
|
{
|
|
var full = new string('x', 20);
|
|
|
|
Assert.That(UnicodeCodec.ReadFixed(Reader(Utf16(full)), 20), Is.EqualTo(full));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteTerminated_CapsAtMaxCharsIncludingTerminator()
|
|
{
|
|
var ours = Bytes(w => UnicodeCodec.WriteTerminated(w, "abcdef", 4));
|
|
|
|
Assert.That(ours, Is.EqualTo(Utf16("abc\0")));
|
|
}
|
|
|
|
[Test]
|
|
public void WriteTerminated_EmojiAtTheCap_IsNotSplit()
|
|
{
|
|
var ours = Bytes(w => UnicodeCodec.WriteTerminated(w, "a\U0001F600", 3));
|
|
|
|
Assert.That(ours, Is.EqualTo(Utf16("a\0")));
|
|
}
|
|
|
|
[Test]
|
|
public void JoinGame_WritesFortyEightBytes()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.JoinGame.Write(w, new JoinGame(Version, "abc")));
|
|
var head = new byte[] { 0x62, 0x13, 0xCC, 0xCC, 0x00, 0x00, 0x00, 0x00 };
|
|
|
|
Assert.That(ours, Is.EqualTo(Concat(head, Utf16("abc\0"), new byte[32])));
|
|
}
|
|
|
|
[Test]
|
|
public void JoinGame_MatchesWindBotExactly_AndUnityExceptPadding()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.JoinGame.Write(w, new JoinGame(Version, "abc")));
|
|
var windbot = Bytes(w =>
|
|
{
|
|
w.Write((short)Version);
|
|
w.Write(new byte[] { 0xCC, 0xCC, 0x00, 0x00, 0x00, 0x00 });
|
|
YGOSharp.Network.Utils.BinaryExtensions.WriteUnicode(w, "abc", 20);
|
|
});
|
|
var unity = Bytes(w =>
|
|
{
|
|
w.Write((short)Version);
|
|
w.Write((byte)204);
|
|
w.Write((byte)204);
|
|
w.Write(0);
|
|
MDPro3.BinaryExtensions.WriteUnicode(w, "abc", 20);
|
|
});
|
|
|
|
Assert.That(ours, Is.EqualTo(windbot));
|
|
AssertEqualExceptPadding(ours, unity, 16, 0xCC);
|
|
}
|
|
|
|
[Test]
|
|
public void JoinGame_RoundTrips()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.JoinGame.Write(w, new JoinGame(Version, "secret")));
|
|
var read = LobbyCodecs.JoinGame.Read(Reader(ours));
|
|
|
|
Assert.That(read.Version, Is.EqualTo(Version));
|
|
Assert.That(read.Password, Is.EqualTo("secret"));
|
|
}
|
|
|
|
[Test]
|
|
public void ExternalAddress_WritesZeroIpThenTerminatedHost()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.ExternalAddress.Write(w, new ExternalAddress("127.0.0.1")));
|
|
|
|
Assert.That(ours, Is.EqualTo(Concat(new byte[4], Utf16("127.0.0.1\0"))));
|
|
}
|
|
|
|
[Test]
|
|
public void ExternalAddress_MatchesBothClientsExactly()
|
|
{
|
|
const string host = "127.0.0.1";
|
|
var ours = Bytes(w => LobbyCodecs.ExternalAddress.Write(w, new ExternalAddress(host)));
|
|
var windbot = Bytes(w =>
|
|
{
|
|
w.Write((uint)0);
|
|
YGOSharp.Network.Utils.BinaryExtensions.WriteUnicodeAutoLength(w, host, 255);
|
|
});
|
|
var unity = Bytes(w =>
|
|
{
|
|
w.Write((uint)0);
|
|
MDPro3.BinaryExtensions.WriteUnicode(w, host, host.Length + 1);
|
|
});
|
|
|
|
Assert.That(ours, Is.EqualTo(windbot));
|
|
Assert.That(ours, Is.EqualTo(unity));
|
|
}
|
|
|
|
[Test]
|
|
public void ExternalAddress_RoundTrips()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.ExternalAddress.Write(w, new ExternalAddress("gitea.dev.blueoceanswim.com")));
|
|
|
|
Assert.That(LobbyCodecs.ExternalAddress.Read(Reader(ours)).Host, Is.EqualTo("gitea.dev.blueoceanswim.com"));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateDeck_WritesCountsThenIds()
|
|
{
|
|
var deck = new UpdateDeck(new[] { 1, 2 }, new[] { 3 }, new[] { 4 });
|
|
var ours = Bytes(w => LobbyCodecs.UpdateDeck.Write(w, deck));
|
|
var legacy = Bytes(w =>
|
|
{
|
|
w.Write(2 + 1);
|
|
w.Write(1);
|
|
w.Write(1);
|
|
w.Write(2);
|
|
w.Write(3);
|
|
w.Write(4);
|
|
});
|
|
|
|
Assert.That(ours, Is.EqualTo(legacy));
|
|
Assert.That(ours.Length, Is.EqualTo(24));
|
|
}
|
|
|
|
[Test]
|
|
public void UpdateDeck_ReadsMainAndExtraAsOneList()
|
|
{
|
|
var deck = new UpdateDeck(new[] { 1, 2 }, new[] { 3 }, new[] { 4 });
|
|
var read = LobbyCodecs.UpdateDeck.Read(Reader(Bytes(w => LobbyCodecs.UpdateDeck.Write(w, deck))));
|
|
|
|
Assert.That(read.Main, Is.EqualTo(new[] { 1, 2, 3 }));
|
|
Assert.That(read.Extra, Is.Empty);
|
|
Assert.That(read.Side, Is.EqualTo(new[] { 4 }));
|
|
}
|
|
|
|
[Test]
|
|
public void CtosPacket_PutsTheIdByteBeforeThePayload()
|
|
{
|
|
var packet = CtosPacket.Begin(LobbyCodecs.PlayerInfo.Id);
|
|
LobbyCodecs.PlayerInfo.Write(packet, new PlayerInfo("Alice"));
|
|
var bytes = ((MemoryStream)packet.BaseStream).ToArray();
|
|
|
|
Assert.That(bytes.Length, Is.EqualTo(41));
|
|
Assert.That(bytes[0], Is.EqualTo(0x10));
|
|
Assert.That(bytes.Skip(1), Is.EqualTo(Bytes(w => LobbyCodecs.PlayerInfo.Write(w, new PlayerInfo("Alice")))));
|
|
}
|
|
|
|
[Test]
|
|
public void CodecIds_MatchTheCatalog()
|
|
{
|
|
Assert.That((int)LobbyCodecs.ExternalAddress.Id, Is.EqualTo(0x17));
|
|
Assert.That((int)LobbyCodecs.PlayerInfo.Id, Is.EqualTo(0x10));
|
|
Assert.That((int)LobbyCodecs.JoinGame.Id, Is.EqualTo(0x12));
|
|
Assert.That((int)LobbyCodecs.UpdateDeck.Id, Is.EqualTo(0x02));
|
|
Assert.That((int)LobbyCodecs.Profile.Id, Is.EqualTo(0x5D));
|
|
Assert.That((int)StocCodecs.Profile.Id, Is.EqualTo(0x5D));
|
|
}
|
|
|
|
[Test]
|
|
public void CtosCatalog_AgreesWithWindBotEnum()
|
|
{
|
|
AssertCatalogParity(typeof(CtosMessage),
|
|
new[] { typeof(YGOSharp.Network.Enums.CtosMessage) },
|
|
new[] { "RequestField", "Capability", "Appearance", "Dialogue", "SeatPlan", "SeatRequest", "SeatSwap", "SeatMove",
|
|
"RestartPrepare", "RestartAck", "RestartControl", "SeatResume", "RestartOpen", "SeatState", "Profile", "AttackPreview" });
|
|
}
|
|
|
|
[Test]
|
|
public void StocCatalog_AgreesWithWindBotEnum()
|
|
{
|
|
Assert.That((int)StocMessage.DeckCount, Is.EqualTo(0x09));
|
|
AssertCatalogParity(typeof(StocMessage),
|
|
new[] { typeof(YGOSharp.Network.Enums.StocMessage) },
|
|
new[] { "DeckCount", "FieldFinish", "SrvproRoomList", "Capability", "Appearance", "Dialogue", "SetupPairs", "SeatPlan",
|
|
"RoomRestarting", "RestartStatus", "SeatResumeResult", "RestartOpen", "SeatState", "MatchOver", "CoinToss", "DeckList", "Profile", "AttackPreview" });
|
|
}
|
|
|
|
private static void AssertCatalogParity(Type catalog, Type[] legacyEnums, string[] expectedExtras)
|
|
{
|
|
var legacyNames = legacyEnums.SelectMany(Enum.GetNames).Distinct().ToArray();
|
|
foreach (var legacy in legacyEnums)
|
|
{
|
|
foreach (var name in Enum.GetNames(legacy))
|
|
{
|
|
Assert.That(Enum.IsDefined(catalog, name), Is.True, legacy.FullName + "." + name + " is missing from the catalog");
|
|
Assert.That(Convert.ToInt32(Enum.Parse(catalog, name)), Is.EqualTo(Convert.ToInt32(Enum.Parse(legacy, name))), name);
|
|
}
|
|
}
|
|
var extras = Enum.GetNames(catalog).Except(legacyNames).OrderBy(n => n).ToArray();
|
|
Assert.That(extras, Is.EqualTo(expectedExtras.OrderBy(n => n).ToArray()));
|
|
}
|
|
}
|
|
}
|