Files
yugioh-legacy-engine/binding/YGOLegacy.Engine/Protocol/Codecs/LobbyCodecs.cs
T
donnyandClaude Fable 5.1 7ec61d18e6
Release / windows (push) Successful in 33s
Release / android (push) Successful in 41s
Release / linux (push) Successful in 1m8s
Release / release (push) Skipped
Release / attach-windows (push) Skipped
binding: YGOLegacy.Engine carries the wire protocol; CI builds, tests and ships ygol-engine-binding.zip (TICKET-397)
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
2026-10-15 16:14:17 -07:00

206 lines
7.4 KiB
C#

using System.Collections.Generic;
using System.IO;
using YGOLegacy.Protocol.Contracts;
namespace YGOLegacy.Protocol.Codecs
{
public interface ICtosCodec<T>
{
CtosMessage Id { get; }
void Write(BinaryWriter writer, T payload);
T Read(BinaryReader reader);
}
public static class CtosPacket
{
public static BinaryWriter Begin(CtosMessage id)
{
var writer = new BinaryWriter(new MemoryStream());
writer.Write((byte)id);
return writer;
}
}
public sealed class EmptyPayloadCodec : ICtosCodec<NoPayload>
{
public CtosMessage Id { get; }
public EmptyPayloadCodec(CtosMessage id)
{
Id = id;
}
public void Write(BinaryWriter writer)
{
}
public void Write(BinaryWriter writer, NoPayload payload)
{
}
public NoPayload Read(BinaryReader reader)
{
return default(NoPayload);
}
}
public static class LobbyCodecs
{
public const int NameChars = 20;
public const int PasswordChars = 20;
public const int HostChars = 256;
public const int ChatChars = 256;
public static readonly ExternalAddressCodec ExternalAddress = new ExternalAddressCodec();
public static readonly PlayerInfoCodec PlayerInfo = new PlayerInfoCodec();
public static readonly JoinGameCodec JoinGame = new JoinGameCodec();
public static readonly UpdateDeckCodec UpdateDeck = new UpdateDeckCodec();
public static readonly ChatCodec Chat = new ChatCodec();
public static readonly KickCodec HsKick = new KickCodec();
public static readonly EmptyPayloadCodec LeaveGame = new EmptyPayloadCodec(CtosMessage.LeaveGame);
public static readonly EmptyPayloadCodec HsToDuelist = new EmptyPayloadCodec(CtosMessage.HsToDuelist);
public static readonly EmptyPayloadCodec HsToObserver = new EmptyPayloadCodec(CtosMessage.HsToObserver);
public static readonly EmptyPayloadCodec HsReady = new EmptyPayloadCodec(CtosMessage.HsReady);
public static readonly EmptyPayloadCodec HsNotReady = new EmptyPayloadCodec(CtosMessage.HsNotReady);
public static readonly EmptyPayloadCodec HsStart = new EmptyPayloadCodec(CtosMessage.HsStart);
public static readonly CapabilityCodec Capability = new CapabilityCodec();
public static readonly AppearanceCodec Appearance = new AppearanceCodec();
public static readonly DialogueCodec Dialogue = new DialogueCodec();
public static readonly SeatPlanCodec SeatPlan = new SeatPlanCodec();
public static readonly SeatRequestCodec SeatRequest = new SeatRequestCodec();
public static readonly SeatSwapCodec SeatSwap = new SeatSwapCodec();
public static readonly SeatMoveCodec SeatMove = new SeatMoveCodec();
public static readonly RestartPrepareCodec RestartPrepare = new RestartPrepareCodec();
public static readonly RestartAckCodec RestartAck = new RestartAckCodec();
public static readonly RestartControlCodec RestartControl = new RestartControlCodec();
public static readonly SeatResumeCodec SeatResume = new SeatResumeCodec();
public static readonly RestartOpenCodec RestartOpen = new RestartOpenCodec();
public static readonly SeatStateCodec SeatState = new SeatStateCodec();
public static readonly ProfileCodec Profile = new ProfileCodec();
public static readonly AttackPreviewQueryCodec AttackPreview = new AttackPreviewQueryCodec();
}
public sealed class ChatCodec : ICtosCodec<Chat>
{
public CtosMessage Id => CtosMessage.Chat;
public void Write(BinaryWriter writer, Chat payload)
{
UnicodeCodec.WriteTerminated(writer, payload.Text, LobbyCodecs.ChatChars);
}
public Chat Read(BinaryReader reader)
{
return new Chat(UnicodeCodec.ReadTerminated(reader, LobbyCodecs.ChatChars));
}
}
public sealed class KickCodec : ICtosCodec<Kick>
{
public CtosMessage Id => CtosMessage.HsKick;
public void Write(BinaryWriter writer, Kick payload)
{
writer.Write(payload.Position);
}
public Kick Read(BinaryReader reader)
{
return new Kick(reader.ReadByte());
}
}
public sealed class ExternalAddressCodec : ICtosCodec<ExternalAddress>
{
public CtosMessage Id => CtosMessage.ExternalAddress;
public void Write(BinaryWriter writer, ExternalAddress payload)
{
writer.Write(0u);
UnicodeCodec.WriteTerminated(writer, payload.Host, LobbyCodecs.HostChars);
}
public ExternalAddress Read(BinaryReader reader)
{
reader.ReadUInt32();
return new ExternalAddress(UnicodeCodec.ReadTerminated(reader, LobbyCodecs.HostChars));
}
}
public sealed class PlayerInfoCodec : ICtosCodec<PlayerInfo>
{
public CtosMessage Id => CtosMessage.PlayerInfo;
public void Write(BinaryWriter writer, PlayerInfo payload)
{
UnicodeCodec.WriteFixed(writer, payload.Name, LobbyCodecs.NameChars);
}
public PlayerInfo Read(BinaryReader reader)
{
return new PlayerInfo(UnicodeCodec.ReadFixed(reader, LobbyCodecs.NameChars));
}
}
public sealed class JoinGameCodec : ICtosCodec<JoinGame>
{
public CtosMessage Id => CtosMessage.JoinGame;
public void Write(BinaryWriter writer, JoinGame payload)
{
writer.Write(payload.Version);
// Struct padding after the uint16 version: both clients have always sent CC CC here.
writer.Write((byte)0xCC);
writer.Write((byte)0xCC);
writer.Write(0u);
UnicodeCodec.WriteFixed(writer, payload.Password, LobbyCodecs.PasswordChars);
}
public JoinGame Read(BinaryReader reader)
{
var version = reader.ReadUInt16();
reader.ReadUInt16();
reader.ReadUInt32();
return new JoinGame(version, UnicodeCodec.ReadFixed(reader, LobbyCodecs.PasswordChars));
}
}
public sealed class UpdateDeckCodec : ICtosCodec<UpdateDeck>
{
public CtosMessage Id => CtosMessage.UpdateDeck;
public void Write(BinaryWriter writer, UpdateDeck payload)
{
writer.Write(payload.Main.Count + payload.Extra.Count);
writer.Write(payload.Side.Count);
WriteIds(writer, payload.Main);
WriteIds(writer, payload.Extra);
WriteIds(writer, payload.Side);
}
// The wire carries one main+extra count, so a read cannot split them again.
public UpdateDeck Read(BinaryReader reader)
{
var mainCount = reader.ReadInt32();
var sideCount = reader.ReadInt32();
var main = ReadIds(reader, mainCount);
var side = ReadIds(reader, sideCount);
return new UpdateDeck(main, null, side);
}
private static void WriteIds(BinaryWriter writer, IReadOnlyList<int> ids)
{
for (var i = 0; i < ids.Count; i++)
writer.Write(ids[i]);
}
private static int[] ReadIds(BinaryReader reader, int count)
{
var ids = new int[count];
for (var i = 0; i < count; i++)
ids[i] = reader.ReadInt32();
return ids;
}
}
}