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
117 lines
3.7 KiB
C#
117 lines
3.7 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace YGOLegacy.Protocol.Framing
|
|
{
|
|
public static class FrameCodec
|
|
{
|
|
public const int HeaderSize = 2;
|
|
public const int MaxFrame = 0xFFFE;
|
|
|
|
public static void Write(BinaryWriter writer, byte id, byte[] payload)
|
|
{
|
|
var payloadLength = payload == null ? 0 : payload.Length;
|
|
WriteHeader(writer, payloadLength + 1);
|
|
writer.Write(id);
|
|
if (payloadLength > 0)
|
|
writer.Write(payload, 0, payloadLength);
|
|
}
|
|
|
|
public static void Write(BinaryWriter writer, byte[] packet)
|
|
{
|
|
if (packet == null || packet.Length == 0)
|
|
throw new ArgumentException("A frame needs at least the id byte", nameof(packet));
|
|
WriteHeader(writer, packet.Length);
|
|
writer.Write(packet, 0, packet.Length);
|
|
}
|
|
|
|
public static int ReadLength(byte[] header)
|
|
{
|
|
if (header == null || header.Length < HeaderSize)
|
|
throw new ArgumentException("A frame header is two bytes", nameof(header));
|
|
return header[0] | (header[1] << 8);
|
|
}
|
|
|
|
private static void WriteHeader(BinaryWriter writer, int length)
|
|
{
|
|
if (length > MaxFrame)
|
|
throw new ArgumentException("Frame of " + length + " bytes exceeds the " + MaxFrame + "-byte ceiling");
|
|
writer.Write((byte)length);
|
|
writer.Write((byte)(length >> 8));
|
|
}
|
|
}
|
|
|
|
public sealed class FrameExtractor
|
|
{
|
|
private byte[] _buffer = new byte[4096];
|
|
private int _start;
|
|
private int _count;
|
|
|
|
public int DroppedEmpty { get; private set; }
|
|
public bool Faulted { get; private set; }
|
|
public int Buffered => _count;
|
|
|
|
public void Feed(byte[] chunk, int count)
|
|
{
|
|
if (Faulted || chunk == null || count <= 0)
|
|
return;
|
|
if (count > chunk.Length)
|
|
count = chunk.Length;
|
|
Reserve(count);
|
|
Array.Copy(chunk, 0, _buffer, _start + _count, count);
|
|
_count += count;
|
|
}
|
|
|
|
public bool TryTake(out byte[] packet)
|
|
{
|
|
packet = null;
|
|
while (!Faulted && _count >= FrameCodec.HeaderSize)
|
|
{
|
|
var length = _buffer[_start] | (_buffer[_start + 1] << 8);
|
|
if (length > FrameCodec.MaxFrame)
|
|
{
|
|
Faulted = true;
|
|
return false;
|
|
}
|
|
if (length == 0)
|
|
{
|
|
Consume(FrameCodec.HeaderSize);
|
|
DroppedEmpty++;
|
|
continue;
|
|
}
|
|
if (_count < FrameCodec.HeaderSize + length)
|
|
return false;
|
|
packet = new byte[length];
|
|
Array.Copy(_buffer, _start + FrameCodec.HeaderSize, packet, 0, length);
|
|
Consume(FrameCodec.HeaderSize + length);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private void Consume(int bytes)
|
|
{
|
|
_start += bytes;
|
|
_count -= bytes;
|
|
if (_count == 0)
|
|
_start = 0;
|
|
}
|
|
|
|
private void Reserve(int extra)
|
|
{
|
|
if (_start + _count + extra <= _buffer.Length)
|
|
return;
|
|
if (_count + extra <= _buffer.Length)
|
|
{
|
|
Array.Copy(_buffer, _start, _buffer, 0, _count);
|
|
_start = 0;
|
|
return;
|
|
}
|
|
var grown = new byte[Math.Max(_buffer.Length * 2, _count + extra)];
|
|
Array.Copy(_buffer, _start, grown, 0, _count);
|
|
_buffer = grown;
|
|
_start = 0;
|
|
}
|
|
}
|
|
}
|