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

356 lines
16 KiB
C#

using System;
namespace YGOLegacy.Engine
{
public static class MessageScan
{
private const int LOCATION_DECK = 0x01;
private const int LOCATION_HAND = 0x02;
private const int LOCATION_GRAVE = 0x10;
private const int LOCATION_OVERLAY = 0x80;
private const int POS_FACEDOWN = 0xA;
private const int QUERY_CODE = 0x1;
private const int QUERY_IS_PUBLIC = 0x100000;
public struct MsgInfo
{
public int Player;
public bool Prompt;
public bool ToPlayerOnly;
public bool NewTurn;
}
public static string PromptName(int id)
{
switch (id)
{
case 10: return "SELECT_BATTLECMD";
case 11: return "SELECT_IDLECMD";
case 12: return "SELECT_EFFECTYN";
case 13: return "SELECT_YESNO";
case 14: return "SELECT_OPTION";
case 15: return "SELECT_CARD";
case 16: return "SELECT_CHAIN";
case 18: return "SELECT_PLACE";
case 19: return "SELECT_POSITION";
case 20: return "SELECT_TRIBUTE";
case 22: return "SELECT_COUNTER";
case 23: return "SELECT_SUM";
case 24: return "SELECT_DISFIELD";
case 25: return "SORT_CARD";
default: return "?";
}
}
public static MsgInfo Scan(byte[] b, ref int pos, int id, int len)
{
var info = new MsgInfo();
int count;
switch (id)
{
case 1: /* RETRY */ break;
case 2: /* HINT */
{
var type = b[pos];
info.Player = b[pos + 1];
pos += 6;
// ygopro's SingleDuel: HINT_EVENT/MESSAGE/SELECTMSG/EFFECT go
// to the hinted player only, the rest is public.
info.ToPlayerOnly = type == 1 || type == 2 || type == 3 || type == 5;
break;
}
case 3: /* WAITING */ break;
case 4: /* START */ pos += 18; break;
case 5: /* WIN */ pos += 2; break;
case 8: /* REQUEST_DECK */ break;
case 10: /* SELECT_BATTLECMD */
info.Player = b[pos]; pos += 1;
count = b[pos++]; pos += count * 11;
count = b[pos++]; pos += count * 8;
pos += 2;
info.Prompt = true; break;
case 11: /* SELECT_IDLECMD */
info.Player = b[pos]; pos += 1;
for (var k = 0; k < 5; k++) { count = b[pos++]; pos += count * 7; }
count = b[pos++]; pos += count * 11;
pos += 3;
info.Prompt = true; break;
case 12: /* SELECT_EFFECTYN */ info.Player = b[pos]; pos += 13; info.Prompt = true; break;
case 13: /* SELECT_YESNO */ info.Player = b[pos]; pos += 5; info.Prompt = true; break;
case 14: /* SELECT_OPTION */
info.Player = b[pos]; pos += 1; count = b[pos++]; pos += count * 4;
info.Prompt = true; break;
case 15: /* SELECT_CARD */
case 20: /* SELECT_TRIBUTE */
info.Player = b[pos]; pos += 4; count = b[pos++]; pos += count * 8;
info.Prompt = true; break;
case 26: /* SELECT_UNSELECT_CARD */
info.Player = b[pos]; pos += 5;
count = b[pos++]; pos += count * 8;
count = b[pos++]; pos += count * 8;
info.Prompt = true; break;
case 16: /* SELECT_CHAIN */
info.Player = b[pos]; count = b[pos + 1]; pos += 11 + count * 14;
info.Prompt = true; break;
case 18: /* SELECT_PLACE */
case 19: /* SELECT_POSITION */
case 24: /* SELECT_DISFIELD */
info.Player = b[pos]; pos += 6; info.Prompt = true; break;
case 22: /* SELECT_COUNTER */
info.Player = b[pos]; pos += 5; count = b[pos++]; pos += count * 9;
info.Prompt = true; break;
case 23: /* SELECT_SUM */
info.Player = b[pos + 1]; pos += 8;
count = b[pos++]; pos += count * 11;
count = b[pos++]; pos += count * 11;
info.Prompt = true; break;
case 21: /* SORT_CHAIN */
case 25: /* SORT_CARD */
info.Player = b[pos]; pos += 1; count = b[pos++]; pos += count * 7;
info.Prompt = true; break;
case 30: /* CONFIRM_DECKTOP */
case 42: /* CONFIRM_EXTRATOP */
info.Player = b[pos]; pos += 1; count = b[pos++]; pos += count * 7; break;
case 31: /* CONFIRM_CARDS */
info.Player = b[pos]; pos += 2; count = b[pos++]; pos += count * 7; break;
case 32: /* SHUFFLE_DECK */
case 34: /* REFRESH_DECK */
case 35: /* SWAP_GRAVE_DECK*/
info.Player = b[pos]; pos += 1; break;
case 33: /* SHUFFLE_HAND */
case 39: /* SHUFFLE_EXTRA */
case 90: /* DRAW */
info.Player = b[pos]; pos += 1; count = b[pos++]; pos += count * 4; break;
case 36: /* SHUFFLE_SET_CARD */
pos += 1; count = b[pos++]; pos += count * 8; break;
case 37: /* REVERSE_DECK */ break;
case 38: /* DECK_TOP */ info.Player = b[pos]; pos += 6; break;
case 40: /* NEW_TURN */ info.NewTurn = true; pos += 1; break;
case 41: /* NEW_PHASE */ pos += 2; break;
case 50: /* MOVE */ pos += 16; break;
case 53: /* POS_CHANGE */ pos += 9; break;
case 54: /* SET */ pos += 8; break;
case 55: /* SWAP */ pos += 16; break;
case 56: /* FIELD_DISABLED */ pos += 4; break;
case 60: /* SUMMONING */
case 62: /* SPSUMMONING */
case 64: /* FLIPSUMMONING */ pos += 8; break;
case 61: /* SUMMONED */
case 63: /* SPSUMMONED */
case 65: /* FLIPSUMMONED */ break;
case 70: /* CHAINING */ pos += 16; break;
case 71: /* CHAINED */
case 72: /* CHAIN_SOLVING */
case 73: /* CHAIN_SOLVED */
case 75: /* CHAIN_NEGATED */
case 76: /* CHAIN_DISABLED*/ pos += 1; break;
case 74: /* CHAIN_END */ break;
case 80: /* CARD_SELECTED */
case 81: /* RANDOM_SELECTED */
pos += 1; count = b[pos++]; pos += count * 4; break;
case 83: /* BECOME_TARGET */
count = b[pos++]; pos += count * 4; break;
case 91: /* DAMAGE */
case 92: /* RECOVER */
case 94: /* LP_UPDATE */
case 100: /* PAY_LPCOST */ pos += 5; break;
case 93: /* EQUIP */ pos += 8; break;
case 95: /* UNEQUIP */ pos += 4; break;
case 96: /* CARD_TARGET */
case 97: /* CANCEL_TARGET */ pos += 8; break;
case 101: /* ADD_COUNTER */
case 102: /* REMOVE_COUNTER */ pos += 7; break;
case 110: /* ATTACK */ pos += 8; break;
case 111: /* BATTLE */ pos += 26; break;
case 112: /* ATTACK_DISABLED */
case 113: /* DAMAGE_STEP_START */
case 114: /* DAMAGE_STEP_END */ break;
case 120: /* MISSED_EFFECT */ pos += 8; break;
case 121: /* BE_CHAIN_TARGET */
case 122: /* CREATE_RELATION */
case 123: /* RELEASE_RELATION */ break;
case 130: /* TOSS_COIN */
case 131: /* TOSS_DICE */
info.Player = b[pos]; pos += 1; count = b[pos++]; pos += count; break;
case 132: /* ROCK_PAPER_SCISSORS */ info.Player = b[pos]; pos += 1; info.Prompt = true; break;
case 133: /* HAND_RES */ pos += 2; break;
case 140: /* ANNOUNCE_RACE */
case 141: /* ANNOUNCE_ATTRIB */
info.Player = b[pos]; pos += 6; info.Prompt = true; break;
case 142: /* ANNOUNCE_CARD */
case 143: /* ANNOUNCE_NUMBER */
info.Player = b[pos]; pos += 1; count = b[pos++]; pos += count * 4;
info.Prompt = true; break;
case 160: /* CARD_HINT */ pos += 9; break;
case 163: /* AI_NAME */
case 164: /* SHOW_HINT */
{
var slen = b[pos] | (b[pos + 1] << 8);
pos += 2 + slen + 1; break;
}
case 165: /* PLAYER_HINT */ pos += 6; break;
case 170: /* MATCH_KILL */ pos += 4; break;
case 180: /* CUSTOM_MSG */ break;
case 162: /* RELOAD_FIELD — Debug.ReloadFieldEnd(), so a scripted
board (Tools/CardTests) emits it where a duel never does. */
{
pos += 1; // duel rule
for (var side = 0; side < 2; side++)
{
pos += 4; // life points
for (var i = 0; i < 7; i++) if (b[pos++] > 0) pos += 2; // monster zones
for (var i = 0; i < 8; i++) if (b[pos++] > 0) pos += 1; // spell/trap zones
pos += 6; // deck, hand, grave, banish, extra, extra pendulum counts
}
// The chain block is written ONCE, after both players. Reading it per
// player takes player 1's low LP byte as a chain count and swallows
// every message that follows — see the [YGOL] note in CoreWrapper.Analyse.
count = b[pos++]; pos += count * 15;
break;
}
case 221: /* RESET_TIME */ pos += 3; break;
default:
throw new Exception("unknown core message " + id + " at offset " + (pos - 1) + " of " + len
+ " — the host's layout table needs a case for it");
}
if (pos > len) throw new Exception("core message " + id + " overran its buffer (" + pos + " > " + len + ")");
return info;
}
public static byte[] Blank(int id, byte[] msg, MsgInfo info, int recipient)
{
switch (id)
{
case 39: // MSG_SHUFFLE_EXTRA: the opponent may not read an extra
// deck (RefreshPile hides its codes the same way), so
// the new order goes out blank to them.
case 33: // MSG_SHUFFLE_HAND: only the owner sees the new order.
{
if (recipient == info.Player) return msg;
var copy = (byte[])msg.Clone();
for (var i = 2; i + 4 <= copy.Length; i += 4)
copy[i] = copy[i + 1] = copy[i + 2] = copy[i + 3] = 0;
return copy;
}
case 90: // MSG_DRAW: only the drawing player sees what was drawn.
{
var copy = (byte[])msg.Clone();
for (var i = 2; i + 4 <= copy.Length; i += 4)
{
if (recipient == info.Player)
copy[i + 3] &= 0x7f; // strip the "public" bit like Analyse
else
copy[i] = copy[i + 1] = copy[i + 2] = copy[i + 3] = 0;
}
return copy;
}
case 50: // MSG_MOVE: a card that lands in a deck/hand (but not a
{ // grave or an xyz pile) or lands face-down is hidden —
// from the OTHER player only. Its new controller always
// sees it: you know the monster you just set. (This is
// the one place where ygoserver is per-recipient and
// Percy.Ygopro, having a single client, is not.)
if (msg.Length < 16) return msg;
int cc = msg[8], cl = msg[9], cp = msg[11];
if (recipient == cc) return msg;
var hidden = ((cl & (LOCATION_GRAVE | LOCATION_OVERLAY)) == 0 && (cl & (LOCATION_DECK | LOCATION_HAND)) != 0)
|| (cp & POS_FACEDOWN) != 0;
if (!hidden) return msg;
var copy = (byte[])msg.Clone();
copy[0] = copy[1] = copy[2] = copy[3] = 0;
return copy;
}
case 54: // MSG_SET: the code is never revealed.
{
if (msg.Length < 4) return msg;
var copy = (byte[])msg.Clone();
copy[0] = copy[1] = copy[2] = copy[3] = 0;
return copy;
}
default:
return msg;
}
}
public static void BlankPileCodes(byte[] buf, bool all)
{
var o = 0;
while (o + 8 <= buf.Length)
{
var recLen = BitConverter.ToInt32(buf, o);
if (recLen <= 4 || o + recLen > buf.Length) { o += Math.Max(recLen, 4); continue; }
var flag = BitConverter.ToInt32(buf, o + 4);
if ((flag & QUERY_CODE) != 0)
{
var hide = all;
if (!hide)
{
var publicOffset = FieldOffset(buf, o, recLen, flag, QUERY_IS_PUBLIC);
hide = publicOffset < 0 || BitConverter.ToInt32(buf, publicOffset) == 0;
}
if (hide)
buf[o + 8] = buf[o + 9] = buf[o + 10] = buf[o + 11] = 0;
}
o += recLen;
}
}
private static int FieldOffset(byte[] buf, int recordStart, int recordLen, int flag, int wanted)
{
var o = recordStart + 8; // past len + flag
var end = recordStart + recordLen;
for (var bit = 0x1; bit != 0 && bit <= 0x800000; bit <<= 1)
{
if ((flag & bit) == 0) continue;
if (bit == wanted) return o <= end - 4 ? o : -1;
if (o + 4 > end) return -1;
switch (bit)
{
case 0x8000: // TARGET_CARD
case 0x10000: // OVERLAY_CARD
case 0x20000: // COUNTERS
o += 4 + BitConverter.ToInt32(buf, o) * 4;
break;
case 0x800000: // LINK (count + marker)
o += 8;
break;
default:
o += 4;
break;
}
}
return -1;
}
}
}