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
72 lines
2.2 KiB
C#
72 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using YGOLegacy.Protocol.Contracts;
|
|
|
|
namespace YGOLegacy.Protocol.Routing
|
|
{
|
|
public enum RouteOutcome
|
|
{
|
|
Handled,
|
|
Ignored,
|
|
Unknown,
|
|
Empty
|
|
}
|
|
|
|
public sealed class StocRouter
|
|
{
|
|
private readonly Dictionary<StocMessage, Action<BinaryReader>> _handlers = new Dictionary<StocMessage, Action<BinaryReader>>();
|
|
private readonly HashSet<StocMessage> _ignored = new HashSet<StocMessage>();
|
|
private readonly HashSet<StocMessage> _reported = new HashSet<StocMessage>();
|
|
|
|
public Action<StocMessage, int> OnUnknown { get; set; }
|
|
|
|
public void Register(StocMessage id, Action<BinaryReader> handler)
|
|
{
|
|
if (handler == null)
|
|
throw new ArgumentNullException(nameof(handler));
|
|
RequireFree(id);
|
|
_handlers.Add(id, handler);
|
|
}
|
|
|
|
public void Ignore(StocMessage id)
|
|
{
|
|
RequireFree(id);
|
|
_ignored.Add(id);
|
|
}
|
|
|
|
public bool IsRegistered(StocMessage id)
|
|
{
|
|
return _handlers.ContainsKey(id) || _ignored.Contains(id);
|
|
}
|
|
|
|
public RouteOutcome Dispatch(BinaryReader reader, out StocMessage id)
|
|
{
|
|
if (reader == null)
|
|
throw new ArgumentNullException(nameof(reader));
|
|
id = default(StocMessage);
|
|
var stream = reader.BaseStream;
|
|
if (stream.Position >= stream.Length)
|
|
return RouteOutcome.Empty;
|
|
id = (StocMessage)reader.ReadByte();
|
|
Action<BinaryReader> handler;
|
|
if (_handlers.TryGetValue(id, out handler))
|
|
{
|
|
handler(reader);
|
|
return RouteOutcome.Handled;
|
|
}
|
|
if (_ignored.Contains(id))
|
|
return RouteOutcome.Ignored;
|
|
if (_reported.Add(id))
|
|
OnUnknown?.Invoke(id, (int)(stream.Length - stream.Position));
|
|
return RouteOutcome.Unknown;
|
|
}
|
|
|
|
private void RequireFree(StocMessage id)
|
|
{
|
|
if (IsRegistered(id))
|
|
throw new ArgumentException("STOC " + id + " already has an owner", nameof(id));
|
|
}
|
|
}
|
|
}
|