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
253 lines
10 KiB
C#
253 lines
10 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using NUnit.Framework;
|
|
using YGOLegacy.Protocol.Codecs;
|
|
using YGOLegacy.Protocol.Contracts;
|
|
|
|
namespace YGOLegacy.Protocol.Tests
|
|
{
|
|
public class CtosCodecTests
|
|
{
|
|
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[] Packet(CtosMessage id, Action<BinaryWriter> write)
|
|
{
|
|
var writer = CtosPacket.Begin(id);
|
|
write(writer);
|
|
writer.Flush();
|
|
return ((MemoryStream)writer.BaseStream).ToArray();
|
|
}
|
|
|
|
private static byte[] LegacyWindBotIntPacket(CtosMessage id, int value)
|
|
{
|
|
return Bytes(w =>
|
|
{
|
|
w.Write((byte)id);
|
|
w.Write(value);
|
|
});
|
|
}
|
|
|
|
private static byte[] LegacyUnityChat(string text)
|
|
{
|
|
return Bytes(w => MDPro3.BinaryExtensions.WriteUnicode(w, text, text.Length + 1));
|
|
}
|
|
|
|
private static byte[] LegacyWindBotChat(string text)
|
|
{
|
|
return Bytes(w => YGOSharp.Network.Utils.BinaryExtensions.WriteUnicodeAutoLength(w, text, 255));
|
|
}
|
|
|
|
[Test]
|
|
public void Response_WritesTheEngineBytesUnchanged()
|
|
{
|
|
var answer = new byte[] { 0x02, 0x00, 0x00, 0x00 };
|
|
var ours = Bytes(w => DuelCodecs.Response.Write(w, new Response(answer)));
|
|
var unity = Bytes(w => w.Write(answer));
|
|
|
|
Assert.That(ours, Is.EqualTo(answer));
|
|
Assert.That(ours, Is.EqualTo(unity));
|
|
Assert.That(DuelCodecs.Response.Read(Reader(ours)).Bytes, Is.EqualTo(answer));
|
|
}
|
|
|
|
[Test]
|
|
public void Response_WriteInt_EqualsBinaryWriterInt32()
|
|
{
|
|
var ours = Bytes(w => DuelCodecs.Response.Write(w, -1));
|
|
var legacy = Bytes(w => w.Write(-1));
|
|
|
|
Assert.That(ours, Is.EqualTo(legacy));
|
|
Assert.That(ours, Is.EqualTo(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }));
|
|
Assert.That(Packet(DuelCodecs.Response.Id, w => DuelCodecs.Response.Write(w, 7)), Is.EqualTo(LegacyWindBotIntPacket(CtosMessage.Response, 7)));
|
|
}
|
|
|
|
[Test]
|
|
public void Response_Packet_MatchesTheOldFactoryForm()
|
|
{
|
|
var answer = new byte[] { 0x01, 0x03, 0x05 };
|
|
var ours = Packet(DuelCodecs.Response.Id, w => DuelCodecs.Response.Write(w, new Response(answer)));
|
|
|
|
Assert.That(ours, Is.EqualTo(Concat(new byte[] { 0x01 }, answer)));
|
|
}
|
|
|
|
[Test]
|
|
public void Response_Null_WritesNothing()
|
|
{
|
|
Assert.That(Bytes(w => DuelCodecs.Response.Write(w, default(Response))), Is.Empty);
|
|
Assert.That(Bytes(w => DuelCodecs.Response.Write(w, new Response(null))), Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void HandResult_IsOneByte_OnBothClients()
|
|
{
|
|
var ours = Bytes(w => DuelCodecs.HandResult.Write(w, new HandResult(2)));
|
|
var unity = Bytes(w => w.Write((byte)2));
|
|
var packet = Packet(DuelCodecs.HandResult.Id, w => DuelCodecs.HandResult.Write(w, new HandResult(2)));
|
|
|
|
Assert.That(ours, Is.EqualTo(unity));
|
|
Assert.That(packet, Is.EqualTo(new byte[] { 0x03, 0x02 }));
|
|
Assert.That(packet, Is.Not.EqualTo(LegacyWindBotIntPacket(CtosMessage.HandResult, 2)));
|
|
Assert.That(LegacyWindBotIntPacket(CtosMessage.HandResult, 2).Length, Is.EqualTo(5));
|
|
Assert.That(DuelCodecs.HandResult.Read(Reader(ours)).Result, Is.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
public void TpResult_IsOneByte_OnBothClients()
|
|
{
|
|
var first = Packet(DuelCodecs.TpResult.Id, w => DuelCodecs.TpResult.Write(w, new TpResult(true)));
|
|
var second = Packet(DuelCodecs.TpResult.Id, w => DuelCodecs.TpResult.Write(w, new TpResult(false)));
|
|
|
|
Assert.That(first, Is.EqualTo(new byte[] { 0x04, 0x01 }));
|
|
Assert.That(second, Is.EqualTo(new byte[] { 0x04, 0x00 }));
|
|
Assert.That(first.Skip(1), Is.EqualTo(Bytes(w => w.Write((byte)1))));
|
|
Assert.That(first, Is.Not.EqualTo(LegacyWindBotIntPacket(CtosMessage.TpResult, 1)));
|
|
Assert.That(DuelCodecs.TpResult.Read(Reader(first.Skip(1).ToArray())).GoFirst, Is.True);
|
|
Assert.That(DuelCodecs.TpResult.Read(Reader(second.Skip(1).ToArray())).GoFirst, Is.False);
|
|
}
|
|
|
|
[Test]
|
|
public void NoDataIds_WriteZeroBytes()
|
|
{
|
|
var codecs = new[]
|
|
{
|
|
LobbyCodecs.LeaveGame, DuelCodecs.Surrender, DuelCodecs.TimeConfirm,
|
|
LobbyCodecs.HsToDuelist, LobbyCodecs.HsToObserver, LobbyCodecs.HsReady,
|
|
LobbyCodecs.HsNotReady, LobbyCodecs.HsStart,
|
|
};
|
|
var expected = new[]
|
|
{
|
|
CtosMessage.LeaveGame, CtosMessage.Surrender, CtosMessage.TimeConfirm,
|
|
CtosMessage.HsToDuelist, CtosMessage.HsToObserver, CtosMessage.HsReady,
|
|
CtosMessage.HsNotReady, CtosMessage.HsStart,
|
|
};
|
|
|
|
for (var i = 0; i < codecs.Length; i++)
|
|
{
|
|
Assert.That(codecs[i].Id, Is.EqualTo(expected[i]));
|
|
Assert.That(Bytes(w => codecs[i].Write(w)), Is.Empty, expected[i].ToString());
|
|
Assert.That(Bytes(w => codecs[i].Write(w, default(NoPayload))), Is.Empty, expected[i].ToString());
|
|
Assert.That(Packet(codecs[i].Id, w => codecs[i].Write(w)), Is.EqualTo(new[] { (byte)expected[i] }));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void Kick_IsOneByte()
|
|
{
|
|
var ours = Bytes(w => LobbyCodecs.HsKick.Write(w, new Kick(1)));
|
|
var packet = Packet(LobbyCodecs.HsKick.Id, w => LobbyCodecs.HsKick.Write(w, new Kick(1)));
|
|
|
|
Assert.That(ours, Is.EqualTo(Bytes(w => w.Write((byte)1))));
|
|
Assert.That(packet, Is.EqualTo(new byte[] { 0x24, 0x01 }));
|
|
Assert.That(LobbyCodecs.HsKick.Read(Reader(ours)).Position, Is.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public void Chat_ShortText_MatchesBothClientsExactly()
|
|
{
|
|
const string text = "gg wp";
|
|
var ours = Bytes(w => LobbyCodecs.Chat.Write(w, new Chat(text)));
|
|
|
|
Assert.That(ours, Is.EqualTo(Utf16(text + "\0")));
|
|
Assert.That(ours, Is.EqualTo(LegacyUnityChat(text)));
|
|
Assert.That(ours, Is.EqualTo(LegacyWindBotChat(text)));
|
|
Assert.That(LobbyCodecs.Chat.Read(Reader(ours)).Text, Is.EqualTo(text));
|
|
}
|
|
|
|
[Test]
|
|
public void Chat_Empty_WritesTheTerminatorOnly()
|
|
{
|
|
Assert.That(Bytes(w => LobbyCodecs.Chat.Write(w, new Chat(""))), Is.EqualTo(new byte[] { 0, 0 }));
|
|
Assert.That(Bytes(w => LobbyCodecs.Chat.Write(w, default(Chat))), Is.EqualTo(new byte[] { 0, 0 }));
|
|
}
|
|
|
|
[Test]
|
|
public void Chat_300Chars_ClipsTo255UnitsPlusTerminator()
|
|
{
|
|
var text = new string('x', 300);
|
|
var ours = Bytes(w => LobbyCodecs.Chat.Write(w, new Chat(text)));
|
|
|
|
Assert.That(ours.Length, Is.EqualTo(512));
|
|
Assert.That(ours.Skip(510), Is.All.EqualTo(0));
|
|
Assert.That(ours.Take(510), Is.EqualTo(Utf16(text.Substring(0, 255))));
|
|
Assert.That(LobbyCodecs.Chat.Read(Reader(ours)).Text, Is.EqualTo(text.Substring(0, 255)));
|
|
Assert.That(LegacyUnityChat(text).Length, Is.EqualTo(602));
|
|
}
|
|
|
|
[Test]
|
|
public void Chat_255Chars_KeepsAll_WhereWindBotKept254()
|
|
{
|
|
var text = new string('y', 255);
|
|
var ours = Bytes(w => LobbyCodecs.Chat.Write(w, new Chat(text)));
|
|
|
|
Assert.That(ours.Length, Is.EqualTo(512));
|
|
Assert.That(LobbyCodecs.Chat.Read(Reader(ours)).Text, Is.EqualTo(text));
|
|
Assert.That(ours, Is.EqualTo(LegacyUnityChat(text)));
|
|
}
|
|
|
|
[Test]
|
|
public void Chat_SurrogatePairAtTheCap_IsNotSplit()
|
|
{
|
|
var text = new string('a', 254) + "\U0001F600";
|
|
var ours = Bytes(w => LobbyCodecs.Chat.Write(w, new Chat(text)));
|
|
|
|
Assert.That(text.Length, Is.EqualTo(256));
|
|
Assert.That(ours.Length, Is.EqualTo(510));
|
|
Assert.That(LobbyCodecs.Chat.Read(Reader(ours)).Text, Is.EqualTo(new string('a', 254)));
|
|
}
|
|
|
|
[Test]
|
|
public void Chat_SurrogatePairThatFits_RoundTrips()
|
|
{
|
|
var text = new string('a', 253) + "\U0001F600";
|
|
var ours = Bytes(w => LobbyCodecs.Chat.Write(w, new Chat(text)));
|
|
|
|
Assert.That(ours.Length, Is.EqualTo(512));
|
|
Assert.That(LobbyCodecs.Chat.Read(Reader(ours)).Text, Is.EqualTo(text));
|
|
}
|
|
|
|
[Test]
|
|
public void CodecIds_MatchTheCatalog()
|
|
{
|
|
Assert.That((int)DuelCodecs.Response.Id, Is.EqualTo(0x01));
|
|
Assert.That((int)DuelCodecs.HandResult.Id, Is.EqualTo(0x03));
|
|
Assert.That((int)DuelCodecs.TpResult.Id, Is.EqualTo(0x04));
|
|
Assert.That((int)LobbyCodecs.LeaveGame.Id, Is.EqualTo(0x13));
|
|
Assert.That((int)DuelCodecs.Surrender.Id, Is.EqualTo(0x14));
|
|
Assert.That((int)DuelCodecs.TimeConfirm.Id, Is.EqualTo(0x15));
|
|
Assert.That((int)LobbyCodecs.Chat.Id, Is.EqualTo(0x16));
|
|
Assert.That((int)LobbyCodecs.HsToDuelist.Id, Is.EqualTo(0x20));
|
|
Assert.That((int)LobbyCodecs.HsToObserver.Id, Is.EqualTo(0x21));
|
|
Assert.That((int)LobbyCodecs.HsReady.Id, Is.EqualTo(0x22));
|
|
Assert.That((int)LobbyCodecs.HsNotReady.Id, Is.EqualTo(0x23));
|
|
Assert.That((int)LobbyCodecs.HsKick.Id, Is.EqualTo(0x24));
|
|
Assert.That((int)LobbyCodecs.HsStart.Id, Is.EqualTo(0x25));
|
|
}
|
|
}
|
|
}
|