Files
yugioh-legacy-engine/binding/YGOLegacy.Engine/Protocol/Codecs/UnicodeCodec.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

82 lines
2.5 KiB
C#

using System.IO;
using System.Text;
namespace YGOLegacy.Protocol.Codecs
{
public static class UnicodeCodec
{
public static void WriteFixed(BinaryWriter writer, string text, int chars)
{
if (chars <= 0)
return;
var buffer = new byte[chars * 2];
Encode(Clip(text, chars - 1), buffer);
writer.Write(buffer);
}
public static void WriteTerminated(BinaryWriter writer, string text, int maxChars)
{
if (maxChars <= 0)
return;
var kept = Clip(text, maxChars - 1);
var buffer = new byte[(kept.Length + 1) * 2];
Encode(kept, buffer);
writer.Write(buffer);
}
public static string ReadFixed(BinaryReader reader, int chars)
{
if (chars <= 0)
return string.Empty;
var buffer = reader.ReadBytes(chars * 2);
var units = buffer.Length / 2;
var text = new char[units];
for (var i = 0; i < units; i++)
{
var c = (char)(buffer[2 * i] | (buffer[2 * i + 1] << 8));
if (c == '\0')
return new string(text, 0, i);
text[i] = c;
}
return new string(text);
}
public static string ReadTerminated(BinaryReader reader, int maxChars)
{
var text = new StringBuilder();
while (text.Length < maxChars)
{
var pair = reader.ReadBytes(2);
if (pair.Length < 2)
break;
var c = (char)(pair[0] | (pair[1] << 8));
if (c == '\0')
break;
text.Append(c);
}
return text.ToString();
}
private static string Clip(string text, int maxUnits)
{
if (string.IsNullOrEmpty(text) || maxUnits <= 0)
return string.Empty;
if (text.Length <= maxUnits)
return text;
var end = maxUnits;
if (char.IsHighSurrogate(text[end - 1]))
end--;
return text.Substring(0, end);
}
private static void Encode(string text, byte[] buffer)
{
for (var i = 0; i < text.Length; i++)
{
buffer[2 * i] = (byte)text[i];
buffer[2 * i + 1] = (byte)(text[i] >> 8);
}
}
}
}