Files
donnyandClaude Fable 5.1 ffd50fa147
Release / windows (push) Successful in 32s
Release / android (push) Successful in 36s
Release / linux (push) Successful in 1m4s
Release / release (push) Successful in 5s
Release / attach-windows (push) Successful in 1s
binding: EngineApi, MessageScan, GameMessage, EngineBinding; contract section (e); v1.1.0 changelog (TICKET-398)
Namespace YGOLegacy.Engine in binding/YGOLegacy.Engine:
- EngineApi: one DllImport("ygol-engine", Cdecl) extern per export of contract/ocgapi.h (45),
  C names kept, types by the contract's type rule; delegates ScriptReader, CardReader,
  MessageHandler beside it.
- MessageScan: MsgInfo, Scan, Blank, BlankPileCodes, PromptName moved from the Unity bot host
  (LocalDuelHost), public, bodies unchanged (+ the private FieldOffset helper and the five
  constants they read).
- GameMessage: enum : byte, one member per #define MSG_* in core/common.h (84), PascalCase names.
- EngineBinding: const Version generated at build from the EngineVersion property into
  obj/EngineBindingVersion.cs (local by default); CheckNative() compares it with
  ygol_engine_version() and throws InvalidOperationException naming both.

Tests: EngineApiParityTests (header names vs extern names, both ways; public, Cdecl, no
CharSet.Unicode, no overloads) and GameMessageParityTests (missing, extra, renumbered).
114 -> 122 tests, green.

contract/engine-contract.md: five parts; "(e) C# binding" with the 45-row extern table, the type
rule and the version rule; engine.json's binding key documented. CHANGELOG.md: Unreleased ->
v1.1.0 with the TICKET-397 and TICKET-398 rows. README.md: the Binding section names the four
types and CheckNative().

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DJUFwdiGCHipsxLkgxssSR
2026-10-15 16:30:24 -07:00

66 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
using YGOLegacy.Engine;
namespace YGOLegacy.Engine.Tests
{
public class GameMessageParityTests
{
private static readonly Regex Define = new Regex(@"^#define\s+MSG_([A-Z0-9_]+)\s+(\d+)\b", RegexOptions.Compiled);
private static string Pascal(string name)
{
return string.Concat(name.Split('_').Select(p => p.Substring(0, 1).ToUpperInvariant() + p.Substring(1).ToLowerInvariant()));
}
private static Dictionary<string, int> HeaderMessages()
{
return File.ReadLines(ContractFiles.CommonHeader)
.Select(line => Define.Match(line))
.Where(m => m.Success)
.ToDictionary(m => Pascal(m.Groups[1].Value), m => int.Parse(m.Groups[2].Value));
}
private static Dictionary<string, int> EnumMessages()
{
return Enum.GetNames(typeof(GameMessage))
.ToDictionary(name => name, name => (int)(byte)Enum.Parse(typeof(GameMessage), name));
}
[Test]
public void HeaderHas84Messages()
{
Assert.That(HeaderMessages().Count, Is.EqualTo(84));
}
[Test]
public void EveryHeaderMessageIsAMember()
{
var missing = HeaderMessages().Keys.Except(EnumMessages().Keys).ToList();
Assert.That(missing, Is.Empty, "MSG_ defines without a GameMessage member");
}
[Test]
public void EveryMemberIsAHeaderMessage()
{
var extra = EnumMessages().Keys.Except(HeaderMessages().Keys).ToList();
Assert.That(extra, Is.Empty, "GameMessage members that are not a MSG_ define");
}
[Test]
public void EveryMemberHasTheHeaderValue()
{
var header = HeaderMessages();
var renumbered = EnumMessages()
.Where(kv => header.TryGetValue(kv.Key, out var value) && value != kv.Value)
.Select(kv => kv.Key + " = " + kv.Value + ", header says " + header[kv.Key])
.ToList();
Assert.That(renumbered, Is.Empty, "GameMessage members whose value differs from the header");
}
}
}