Files
yugioh-legacy-engine/contract/engine-contract.md
T
donnyandClaude Opus 5.5 4cd908d7af
Release / linux (push) Successful in 1m19s
Release / windows (push) Successful in 41s
Release / android (push) Successful in 52s
Release / release (push) Successful in 4s
Release / attach-windows (push) Successful in 1s
contract: Tag shared Extra Deck rows, changelog v1.6.0, probe suite (TICKET-433)
The suite plays a pair-present and a pair-absent Tag room: a Summon of the
waiting partner's monster, the partner value across a move, and the replay
played back through the library. The contract states that bit 0x100000 stays
unused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 00:54:35 -07:00

537 lines
48 KiB
Markdown

# The ygol-engine contract
This is the one spec every consumer of `ygol-engine` reads instead of the C++. It has five parts:
(a) the C API, (b) the wire protocol the duel server speaks, (c) the engine message stream a duel
produces, (d) the setup pairs and the Lua API, (e) the C# binding `YGOLegacy.Engine`. A change to any
part is a row in `CHANGELOG.md`
(proposal 47, Rule 2). The facts here are read from the source: `core/ocgapi.h`,
`server/gframe/serverapi.h`, `server/gframe/network.h`, `server/gframe/netserver.cpp`,
`server/gframe/single_duel.cpp` / `tag_duel.cpp`, `core/common.h`. When this file and the source
disagree, the source is right and this file has a bug.
Companion files in `contract/`:
| File | What |
|---|---|
| `ocgapi.h` | The public header: `core/ocgapi.h` verbatim plus the server's exports. The build refuses to run when it drifts from those two sources. |
| `probe-feature-messages.py` | The wire gate: hosts lobbies from a built library and checks every fork message against this contract. `python contract/probe-feature-messages.py --dll out/win64/ygol-engine.dll`, run from the Unity repo root (the library reads `./expansions`). |
| `lua-reference/` | The Lua API the core registers for card scripts, generated from `core/` by `generate.py`. |
## Versions and `engine.json`
A release is a git tag `vMAJOR.MINOR.PATCH` (proposal 47, Rule 3). The build writes `engine.json`
next to the library (`out/win64/engine.json` on Windows):
```json
{
"version": "v0.0.1",
"exports": 335,
"protocol": 11,
"capabilities": 30719,
"binding": "v0.0.1"
}
```
| Key | Type | Source |
|---|---|---|
| `version` | string | The `-Version` the build was given; `local` when none. The same string `ygol_engine_version()` returns. |
| `exports` | integer | The library's export count after the gate passed (Windows: 335). |
| `protocol` | integer | `FEATURE_PROTOCOL` in `server/gframe/network.h` (11 as of 2026-11-02). |
| `capabilities` | integer | The OR of every `FEATURE_*` bit `SendServerCapability` advertises (`single_duel.cpp` and `tag_duel.cpp` must agree), as a decimal integer. 30719 = `0x77FF` = bits `0x1` to `0x400` plus `0x1000`, `0x2000` and `0x4000` (`0x800` is reserved by proposal 44). |
| `binding` | string | The version of the C# binding released with this engine (section (e)); the same string as `version`. |
The build reads `protocol` and `capabilities` from the source every time; nothing types them in.
## (a) C API
One native library, `ygol-engine` (`ygol-engine.dll` on Windows), exports the core's C API and the
server's three functions, all `extern "C"`. Every signature is in `contract/ocgapi.h`. Types: `intptr_t`
is the duel handle `create_duel*` returns; `byte` is `unsigned char`; buffers the caller passes are
the caller's.
The library also exports 289 `sqlite3_*` symbols because the server links SQLite with
`SQLITE_API=__declspec(dllexport)`. They are NOT part of this contract: nothing of ours calls them
through the library, and they are counted by the export gate only so that the number stays exact
(Windows: 45 contract exports + 289 SQLite + 1 = 335; the extra 1 is `ygol_engine_version`, see below).
### Server exports (`server/gframe/serverapi.h`)
| Export | Signature | Added by |
|---|---|---|
| `start_server` | `int start_server(const char* args)` | Upstream. Runs the duel server on the calling thread until it stops; `args` is the host string (below). Returns `main`'s result. |
| `stop_server` | `void stop_server()` | Upstream. Stops the running server from any thread. |
| `ygol_engine_version` | `const char* ygol_engine_version()` | TICKET-388, 2026-10-14. The version the build was given, `local` when none. Static storage, never freed, no allocation, no state. |
The host string is space-separated tokens, in this order (`server/gframe/gframe.cpp`, `main`):
| Slot | Token |
|---|---|
| 1 | port |
| 2 | lflist index (`-1` = none) |
| 3 | rule (OCG / TCG / ...) |
| 4 | mode: 0 Single, 1 Match, 2 Tag |
| 5 | duel rule: `T` (previous master rule), `F` (default), or a numeric `duel_flags` |
| 6 | `T` = no deck check |
| 7 | `T` = no deck shuffle |
| 8 | start LP (seat side A; side B too unless the setup pair `ygol.side.b.lp` names its own) |
| 9 | start hand |
| 10 | draw count |
| 11 | time limit (seconds; 0 = none) |
| 12 | replay mode |
| 13+ | setup pairs `ygol.<key>=<value>` (patch 0009, section (d)), the bootstrap secret of a rules restart (patch 0012), and base64 seeds: the n-th token that is neither is seed n |
`argv` is sized from the token count (patch 0009); the stock 12-token buffer is gone.
### Core exports (`core/ocgapi.h`)
| Export | Signature | Added by |
|---|---|---|
| `set_script_reader` | `void set_script_reader(script_reader f)` | Upstream |
| `set_card_reader` | `void set_card_reader(card_reader f)` | Upstream |
| `set_message_handler` | `void set_message_handler(message_handler f)` | Upstream |
| `create_duel` | `intptr_t create_duel(uint_fast32_t seed)` | Upstream |
| `create_duel_v2` | `intptr_t create_duel_v2(uint32_t seed_sequence[])` | Upstream (`SEED_COUNT` = 8 words) |
| `create_duel_ex` | `intptr_t create_duel_ex(uint_fast32_t seed, uint32_t create_flags)` | Upstream |
| `create_duel_v2_ex` | `intptr_t create_duel_v2_ex(uint32_t seed_sequence[], uint32_t create_flags)` | Upstream |
| `start_duel` | `void start_duel(intptr_t pduel, uint32_t options)` | Upstream. v1.6.0: option `DUEL_TAG_SHARED_EXTRA` `0x200` with `DUEL_TAG_MODE` merges each team's Extra Decks (section (b), behaviour v1.6.0). |
| `end_duel` | `void end_duel(intptr_t pduel)` | Upstream |
| `set_player_info` | `void set_player_info(intptr_t pduel, int32_t playerid, int32_t lp, int32_t startcount, int32_t drawcount)` | Upstream |
| `get_log_message` | `void get_log_message(intptr_t pduel, char* buf)` | Upstream |
| `get_message` | `int32_t get_message(intptr_t pduel, byte* buf)` | Upstream. Returns the length; the buffer holds the messages of section (c). |
| `process` | `uint32_t process(intptr_t pduel)` | Upstream. Low 16 bits = message length, high 16 bits = status. |
| `new_card` | `void new_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position)` | Upstream |
| `new_tag_card` | `void new_tag_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t location)` | Upstream |
| `query_card` | `int32_t query_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint32_t query_flag, byte* buf, int32_t use_cache)` | Upstream |
| `query_field_count` | `int32_t query_field_count(intptr_t pduel, uint8_t playerid, uint8_t location)` | Upstream |
| `query_field_card` | `int32_t query_field_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint32_t query_flag, byte* buf, int32_t use_cache)` | Upstream |
| `query_field_info` | `int32_t query_field_info(intptr_t pduel, byte* buf)` | Upstream. Writes a `MSG_RELOAD_FIELD` (162). |
| `set_responsei` | `void set_responsei(intptr_t pduel, int32_t value)` | Upstream |
| `set_responseb` | `void set_responseb(intptr_t pduel, byte* buf)` | Upstream |
| `preload_script` | `int32_t preload_script(intptr_t pduel, const char* script_name)` | Upstream |
| `default_script_reader` | `byte* default_script_reader(const char* script_name, int* len)` | Upstream |
| `ygol_preview_attack` | `int32_t ygol_preview_attack(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t target_location, uint8_t target_sequence, uint8_t* out)` | Patch 0022 (TICKET-374) + 0024 (TICKET-379), 2026-10-13. A read-only dry run of the damage step. Returns 1 answered, 0 refused (bad pointer, no such card, attacker not a face-up monster on `playerid`'s field, target not a monster on the other field); a refusal writes nothing. `target_location` `0xFF` = direct. `out` is 24 bytes: version (1), flags (bit 0 direct, bit 1 unknown, bit 2 attacker destroyed, bit 3 target destroyed, bit 4 pierce, bit 5 illegal), 2 pad bytes, then five `int32`: attacker value, target value, damage to player 0, damage to player 1, reserved 0. A face-down target answers unknown with every number 0. A pair the battle menu (`field::get_attack_target`) would not offer answers illegal alone with every number 0. Call it only while the engine is parked on a prompt; the duel is byte-identical afterwards. |
| `ygol_query_tag_cards` | `int32_t ygol_query_tag_cards(intptr_t pduel, uint8_t playerid, uint8_t* out)` | v1.2.0 (TICKET-405), 2026-10-15. The waiting tag partner's cards of `playerid`'s side, read only. Returns the byte length written, 0 refused (bad pointer, bad player, not a tag duel); a refusal writes nothing. `out`: `u8 version` (1), `u8 hand_count`, `u8 extra_count`, `u8 pad` (0), `u16 deck_count`, then `u32 hand[hand_count]`, `u32 extra[extra_count]` (bit 31 = face-up, as in `MSG_TAG_SWAP`), `u32 deck[deck_count]` sorted ascending (the order stays secret). At most 7 + 4 x (255 + 255 + 250) bytes in practice; size the buffer for your deck limits. The duel is byte-identical afterwards. |
| `get_registry_value` | `int32_t get_registry_value(intptr_t pduel, const char* key, byte* out_buf)` | Upstream (koishi) |
| `set_registry_value` | `void set_registry_value(intptr_t pduel, const char* key, const char* value)` | Upstream (koishi). The server writes every setup pair here before `special.lua` (patch 0009). |
| `get_registry_keys` | `int32_t get_registry_keys(intptr_t pduel, byte* out_buf)` | Upstream (koishi) |
| `clear_registry` | `void clear_registry(intptr_t pduel)` | Upstream (koishi) |
| `dump_registry` | `int32_t dump_registry(intptr_t pduel, byte* out_buf)` | Upstream (koishi) |
| `load_registry` | `void load_registry(intptr_t pduel, const byte* in_buf, int32_t in_len)` | Upstream (koishi) |
| `get_lua_coverage_dump_size` | `int32_t get_lua_coverage_dump_size(intptr_t pduel, const char* name)` | Upstream (koishi) |
| `dump_lua_coverage` | `int32_t dump_lua_coverage(intptr_t pduel, const char* name, byte* out_buf, int32_t out_len)` | Upstream (koishi) |
| `get_all_lua_coverages_dump_size` | `int32_t get_all_lua_coverages_dump_size(intptr_t pduel)` | Upstream (koishi) |
| `dump_all_lua_coverages` | `int32_t dump_all_lua_coverages(intptr_t pduel, byte* out_buf, int32_t out_len)` | Upstream (koishi) |
| `clear_lua_coverage` | `void clear_lua_coverage(intptr_t pduel, const char* name)` | Upstream (koishi) |
| `clear_all_lua_coverages` | `void clear_all_lua_coverages(intptr_t pduel)` | Upstream (koishi) |
| `ygol_sb_add` | `int32_t ygol_sb_add(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position)` | Patch 0008 (Sandbox god mode), 2026-09-26. Sandbox only. Returns 1 did it, 0 refused and changed nothing. |
| `ygol_sb_move` | `int32_t ygol_sb_move(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t dst_playerid, uint8_t dst_location, uint8_t dst_sequence, uint8_t position)` | Patch 0008. Same rule. |
| `ygol_sb_remove` | `int32_t ygol_sb_remove(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence)` | Patch 0008. Same rule. |
| `ygol_sb_set_position` | `int32_t ygol_sb_set_position(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position)` | Patch 0008. Same rule. |
| `ygol_sb_set_lp` | `int32_t ygol_sb_set_lp(intptr_t pduel, uint8_t playerid, int32_t lp)` | Patch 0008. Same rule. |
| `ygol_sb_reask` | `int32_t ygol_sb_reask(intptr_t pduel)` | Patch 0008. Re-issues the open prompt after a god-mode edit. Same rule. |
Locations the sandbox exports accept: DECK, HAND, MZONE, SZONE, GRAVE, REMOVED, EXTRA.
Export count on Windows: 3 server + 43 core = 46 contract exports, + 289 `sqlite3_*` = 335. The
Unity repo's old `ygoserver.dll` had 327: its own core copy never carried patch 0008, and it had no
`ygol_engine_version`.
### Declarations in the Unity client that are NOT in this library
`Assets/Scripts/MDPro3/Duel/CoreWrapper.cs` declares three names with `DllImport` that no core or
server source defines, nothing exports and nothing calls. They are dead declarations; a call would
throw `EntryPointNotFoundException`. Phase 2 drops them.
| Declared name | Status |
|---|---|
| `get_ai_going_first_second` | Not in the library; dead declaration, no callers |
| `set_player_going_first_second` | Not in the library; dead declaration, no callers |
| `set_ai_id` | Not in the library; dead declaration, no callers |
Every other `DllImport` in `CoreWrapper.cs`, `YgoServer.cs` and
`Assets/YGOLegacy/Scripts/Core/Bot/LocalDuelHost.cs` names a row above.
## (b) Wire protocol: the fork's messages
The server speaks the stock ygopro wire: each packet is `uint16_t length` + `uint8_t id` + payload.
Stock ids (`CTOS_*` `0x1`-`0x30`, `STOC_*` `0x1`-`0x31`) are unchanged. The fork added the ids
below. Every fork CTOS message is dropped (connection kept, no reply) when its length is not exactly
the struct size (`netserver.cpp`); every fork message but `CTOS_SEAT_REQUEST` needs the connection
to have joined a game first.
**Sender stamp.** A stamped STOC message is `uint16_t sender` + the CTOS payload, where `sender` is
the seat (`player_type`) of the connection that sent it, or `0xFF` (`FEATURE_SENDER_SERVER`) when
the server itself speaks. The STOC form of a relayed message is always the CTOS bytes with that
stamp in front. "Stamp" in the tables below says whether the STOC message carries one.
**Capability.** The server sends `STOC_CAPABILITY` (stamp `0xFF`) to every connection between
`STOC_JOIN_GAME` and `STOC_TYPE_CHANGE`: `protocol` = `FEATURE_PROTOCOL` (11) and `flags` = the OR of
the bits below (`0x77FF`). A client sends its own `CTOS_CAPABILITY` after joining. A client refuses a
host without bit `0x4` as out of date, and without bit `0x1000` too (v1.5.0). A Tag room refuses a
client whose `CTOS_CAPABILITY` lacks `0x1000` or `0x4000` (v1.6.0): `STOC_ERROR_MSG` `ERRMSG_VERERROR`
(code = `FEATURE_PROTOCOL`), then the connection is closed; a Single room keeps it.
**Join snapshot.** A joiner gets, per occupied seat, right after that seat's `STOC_HS_PLAYER_ENTER`:
the seat's stored capability, appearance (0006), seat state (0013) and profile (0020) when present,
then the setup pairs (0009), the seat plan (0011) if one was ever sent, and, during a duel, the field
resync (0005), the deck lists the audience may see (0019) and the Tag partner cards: a seated
duelist's own side (v1.2.0), both sides for an observer when spectators see all (v1.3.0).
### Client to server
| Id | Name | Payload (bytes) | Who may send | Else | Bit | Protocol | Patch |
|---|---|---|---|---|---|---|---|
| `0x40` | `CTOS_CAPABILITY` | `CTOS_Capability` (6): `u16 protocol`, `u32 flags` | Any joined connection | - | - | 1 | 0006 |
| `0x41` | `CTOS_APPEARANCE` | `CTOS_Appearance` (352): `i32 items[8]`, `u16 deck_name[48]`, `u16 character_id[64]`, `u16 deck_group[48]` | A seated duelist (`type` 0-3) | Dropped | `0x1` | 1 | 0006 |
| `0x42` | `CTOS_DIALOGUE` | `CTOS_Dialogue` (204): `u8 version`, `u8 flags`, `i16 index`, `u32 card_code`, `u32 target_code`, `u16 corpus_key[48]`, `u16 event_key[48]` | A seated duelist | Dropped | `0x2` | 1 | 0006 |
| `0x50` | `CTOS_SEAT_PLAN` | `CTOS_SeatPlan` (91): `u8 restart_id[16]`, `u32 deadline_ms`, `u16 revision`, `u8 seat_count`, `u8 kind[4]` (0 Open, 1 Cpu, 2 Reserved), `u8 token[4][16]` | The host, in the lobby | Dropped; a plan that would make a human's seat Cpu is refused to the host alone with the refused revision | `0x8` | 2 | 0011 |
| `0x51` | `CTOS_SEAT_REQUEST` | `CTOS_SeatRequest` (35): `u16 protocol`, `u8 restart_id[16]`, `u8 seat`, `u8 token[16]` | A connection BEFORE `CTOS_JOIN_GAME` (the only fork message accepted there) | A wrong token = `ERRMSG_JOINERROR` + disconnect at JoinGame, never the next free seat | `0x8` | 2 | 0011 |
| `0x52` | `CTOS_SEAT_SWAP` | `CTOS_SeatSwap` (2): `u8 first`, `u8 second` | The host, in the lobby, no restart in progress | Dropped | `0x8` | 2 | 0011 |
| `0x53` | `CTOS_SEAT_MOVE` | `CTOS_SeatMove` (2): `u8 from`, `u8 to` | A duelist moving its own seat, or an observer into an Open seat | Dropped | `0x8` | 2 | 0011 |
| `0x54` | `CTOS_RESTART_PREPARE` | `CTOS_RestartPrepare` (101): `u8 restart_id[16]`, `u32 timeout_ms`, `u8 seat_count`, `u8 seat_token[4][16]`, `u8 observer_token[16]` | The host, in the lobby | Refused with a reason in `STOC_RESTART_STATUS` (a participant lacks bit `0x10`, not lobby, busy, bad request) | `0x10` | 2 | 0012 |
| `0x55` | `CTOS_RESTART_ACK` | `CTOS_RestartAck` (17): `u8 restart_id[16]`, `u8 phase` | Every human and watcher that got `STOC_ROOM_RESTARTING` | Dropped | `0x10` | 2 | 0012 |
| `0x56` | `CTOS_RESTART_CONTROL` | `CTOS_RestartControl` (17): `u8 restart_id[16]`, `u8 action` (2 Commit, 3 Abort) | The host; Commit only after every ack | Dropped | `0x10` | 2 | 0012 |
| `0x57` | `CTOS_SEAT_RESUME` | `CTOS_SeatResume` (18): `u8 restart_id[16]`, `u8 action` (1 Complete, 2 Release), `u8 reason` | A seat restoring after a restart, or a held CPU seat after a match (zero id) | `STOC_SEAT_RESUME_RESULT` with the reason | `0x10` | 2 | 0012 |
| `0x58` | `CTOS_RESTART_OPEN` | `CTOS_RestartOpen` (32): `u8 restart_id[16]`, `u8 observer_token[16]` | The bootstrap host of a replacement server (the holder of the secret) | Dropped | `0x10` | 2 | 0012 |
| `0x59` | `CTOS_SEAT_STATE` | `CTOS_SeatState` (4): `u8 version` (1), `u8 state` (0 none, 1 picking a deck), `u16 value` | A seated duelist, own seat, in the lobby | Dropped (observer, wrong version or state, during the duel) | `0x20` | 3 | 0013 |
| `0x5D` | `CTOS_PROFILE` | `CTOS_Profile` (98): `u8 version` (1), `u8 reserved`, `u16 avatar_id[32]`, `u16 frame_id[16]` | A seated HUMAN, own seat (a CPU seat's is dropped) | Dropped | `0x200` | 7 | 0020 |
| `0x5E` | `CTOS_ATTACK_PREVIEW` | `u8 version` (1), `u8 count` (1-64), then `count` x `CTOS_AttackPreviewPair` (4): `u8 attacker_location`, `u8 attacker_sequence`, `u8 target_location` (`0xFF` direct), `u8 target_sequence` | The seat whose `MSG_SELECT_BATTLECMD` is open (Tag: the team's current hand) | No reply at all (wrong version, count 0 or > 64, wrong length, out-of-turn seat, spectator) | `0x400` | 8 | 0023 |
### Server to client
| Id | Name | Payload (bytes) | Stamp | To whom | Bit | Protocol | Patch |
|---|---|---|---|---|---|---|---|
| `0x40` | `STOC_CAPABILITY` | `u16 sender` + `CTOS_Capability` (8) | Yes (`0xFF` = the server's own) | Everyone, on join; each seat's relayed to everyone | - | 1 | 0006 |
| `0x41` | `STOC_APPEARANCE` | `u16 sender` + `CTOS_Appearance` (354) | Yes | Everyone; stored per seat, replayed to joiners, dropped when the seat empties or moves to the watchers | `0x1` | 1 | 0006 |
| `0x42` | `STOC_DIALOGUE` | `u16 sender` + `CTOS_Dialogue` (206) | Yes | Everyone | `0x2` | 1 | 0006 |
| `0x43` | `STOC_SETUP_PAIRS` | `u16 0xFF`, `u16 count`, then per pair `u16 length` + `key=value` bytes (no terminator) | Yes (`0xFF`) | Every joiner, in the snapshot | `0x4` | 1 | 0009 |
| `0x50` | `STOC_SEAT_PLAN` | `u16 0xFF` + `STOC_SeatPlan` (23): `u8 restart_id[16]`, `u16 revision`, `u8 seat_count`, `u8 kind[4]` (25 total; no token on the wire) | Yes (`0xFF`) | Everyone after a plan, swap or move; joiners in the snapshot | `0x8` | 2 | 0011 |
| `0x54` | `STOC_ROOM_RESTARTING` | `u16 0xFF` + `STOC_RoomRestarting` (38): `u8 phase` (1 Prepare, 2 Commit, 3 Abort), `u8 restart_id[16]`, `u8 seat`, `u8 token[16]`, `u32 timeout_ms` | Yes (`0xFF`) | Each participant, with ITS OWN seat and token only | `0x10` | 2 | 0012 |
| `0x56` | `STOC_RESTART_STATUS` | `u16 0xFF` + `STOC_RestartStatus` (21): `u8 phase`, `u8 restart_id[16]`, `u8 acked`, `u8 expected`, `u8 complete`, `u8 reason` (0 none, 1 incompatible, 2 ack timeout, 3 not lobby, 4 not host, 5 busy, 6 bad request) | Yes (`0xFF`) | The host only | `0x10` | 2 | 0012 |
| `0x57` | `STOC_SEAT_RESUME_RESULT` | `u16 0xFF` + `STOC_SeatResumeResult` (19): `u8 restart_id[16]`, `u8 accepted`, `u8 seat`, `u8 reason` (1 no restart, 2 not restoring) | Yes (`0xFF`) | The claimant | `0x10` | 2 | 0012 |
| `0x58` | `STOC_RESTART_OPEN` | `u16 0xFF` + `STOC_RestartOpen` (17): `u8 restart_id[16]`, `u8 opened` | Yes (`0xFF`) | The bootstrap host | `0x10` | 2 | 0012 |
| `0x59` | `STOC_SEAT_STATE` | `u16 sender` + `CTOS_SeatState` (6) | Yes | Everyone; stored per seat, in the snapshot after that seat's APPEARANCE, re-sent on a swap with the new seat, cleared on leave / move / duel start | `0x20` | 3 | 0013 |
| `0x5A` | `STOC_MATCH_OVER` | `u16 0xFF` + `STOC_MatchOver` (6): `u8 version` (1), `u8 seat_count`, `u8 occupied[4]` | Yes (`0xFF`) | Everyone, after the stock `STOC_DUEL_END`; the room is a lobby again (nobody READY, the game-2 seat swap undone, CPU seats held until the host's START) | `0x40` | 4 | 0014 |
| `0x5B` | `STOC_COIN_TOSS` | `STOC_CoinToss` (4): `u8 version` (1), `u8 chooser_seat`, `u8 face` (0 heads, 1 tails, `0xFF` none: game 2+, the loser chooses), `u8 choose_seconds` (10) | No | Every seat and watcher on START; then the stock `STOC_SELECT_TP` to the chooser alone, answered FIRST by the server after 10 s if the chooser does not. `STOC_SELECT_HAND` is never sent; `CTOS_HAND_RESULT` is ignored | `0x80` | 5 | 0017 |
| `0x5C` | `STOC_DECK_LIST` | `STOC_DeckList` (5): `u8 version` (1), `u8 side`, `u8 kind`, `u16 count`, then `u32 codes[count]` | No | Kind 0 (the deck's remaining cards, SORTED) to its owner (both teammates in Tag); kind 1 (real order, top first) and kind 2 (the submitted Main Deck of the duelist whose deck is in the zone, sorted) to observers only when spectators see all. Sent when a list differs from the last one that audience got; in the late-watcher snapshot | `0x100` | 6 | 0019 |
| `0x5D` | `STOC_PROFILE` | `u16 sender` + `CTOS_Profile` (100) | Yes | Everyone; stored per seat, in the snapshot after APPEARANCE, swapped with the seat, kept across the 0014 lobby reset, cleared when the seat empties | `0x200` | 7 | 0020 |
| `0x5E` | `STOC_ATTACK_PREVIEW` | `u8 version` (1), `u8 count`, then per pair the 4 ask bytes echoed + the 24-byte `ygol_preview_attack` answer | No | The asker only. Only attackers the open prompt listed; a pair the engine refuses is left out (the count shrinks); a valid ask whose every pair is left out still gets count 0 | `0x400` | 8 | 0023 |
| `0x5F` | `STOC_TAG_PARTNER_CARDS` | `u8 side` + the `ygol_query_tag_cards` bytes (7 + 4 x the three counts) | No | The two seats of `side`, never the other team. Sent after `start_duel` (the waiting members' opening hands), after every `MSG_TAG_SWAP` for the side that swapped (the member that just left is the waiting one), and to a seated duelist in its field resync (`CTOS_REQUEST_FIELD`, after the `MSG_RELOAD_FIELD`). **v1.3.0 (TICKET-412): when spectators see all (no `ygol.room.spectatorview=hidden`), every observer gets the same bytes for BOTH sides at the start and after every swap, and an observer's field resync and a late watcher's snapshot carry both sides (after the deck lists); with the pair, an observer never gets one.** **v1.4.2 (TICKET-424): after every engine batch (beside the deck lists, not when the duel ended), a side whose bytes differ from the last ones broadcast for it is sent again to the same receivers (an effect that moves a waiting member's card mid-turn); every broadcast records its bytes, so a swap is never sent twice; a resync to one player does not record.** Tag rooms only; a Single / Match room never sends it | `0x2000` | 9 | v1.2.0, v1.3.0, v1.4.2 |
### Capability bits and protocol numbers
| Bit | Name | Protocol | Patch | Feature |
|---|---|---|---|---|
| `0x1` | `FEATURE_APPEARANCE` | 1 | 0006 | Appearance messages |
| `0x2` | `FEATURE_DIALOGUE` | 1 | 0006 | Dialogue messages |
| `0x4` | `FEATURE_SETUP_PAIRS` | 1 | 0009 | Setup pairs; a client refuses a host without it |
| `0x8` | `FEATURE_SEAT_PLAN` | 2 | 0011 | Host seats |
| `0x10` | `FEATURE_RULES_RESTART` | 2 | 0012 | Rules restart |
| `0x20` | `FEATURE_SEAT_STATE` | 3 | 0013 | Seat state |
| `0x40` | `FEATURE_LOBBY_AFTER_MATCH` | 4 | 0014 | The room survives the match |
| `0x80` | `FEATURE_COIN_TOSS` | 5 | 0017 | Server-rolled coin toss |
| `0x100` | `FEATURE_DECK_LIST` | 6 | 0019 | Deck lists |
| `0x200` | `FEATURE_PROFILE` | 7 | 0020 | Profile |
| `0x400` | `FEATURE_ATTACK_PREVIEW` | 8 | 0023 | Attack preview over the wire |
| `0x800` | reserved | - | - | Proposal 44 (agreed, not shipped) |
| `0x1000` | `FEATURE_TAG_TWO_FIELDS` | 10 | v1.5.0 | Tag two Field slots: Spell/Trap sequence 6 is a team's second Field Zone (no message); a Tag room refuses a joiner without it |
| `0x2000` | `FEATURE_TAG_PARTNER_CARDS` | 9 | v1.2.0 | Tag partner cards (`STOC_TAG_PARTNER_CARDS`, `ygol_query_tag_cards`) |
| `0x4000` | `FEATURE_TAG_SHARED_EXTRA` | 11 | v1.6.0 | Tag shared Extra Deck: the setup pair `ygol.tag.extra=shared`, `QUERY_TAG_MEMBER` on Extra Deck refreshes; a Tag room refuses a joiner without it |
`FEATURE_PROTOCOL` is bumped by every patch that adds a message a client must understand; the join
list hides hosts older than the client's number. Rule 4 (proposal 47): one feature, one door. Never
carry a new feature inside another feature's message.
### Behaviour changes without a new message
| Patch | Change |
|---|---|
| 0002 | Tag duel: teammates share one visible hand. |
| 0003 | `YGOPRO_MAX_EXTRA` = 99: `CTOS_UPDATE_DECK` with up to 99 extra deck cards is accepted (the stock server dropped the surplus silently while receiving). Rush rulesets cap it lower in the client. |
| 0005 | The server keeps listening after the duel starts. A late observer gets `STOC_DUEL_START` and a field resync (`STOC_GAME_MSG` with `MSG_RELOAD_FIELD`), unmasked when spectators see all. |
| 0016 | Tag: team B is loaded as seat 2 main deck + seat 3 tag deck, the prompt owner starts at seat 2; with core patch 0015 the turn order is seat 0, 2, 1, 3. |
| 0018 | Spectators see all: every observer gets the UNMASKED stream (both hands, draws, set cards, extra, banished, the 0005 snapshot) unless the setup pair `ygol.room.spectatorview=hidden` is present, which restores the stock masked stream byte for byte. Duelists' streams never change. |
| 0021 | START ends a rules-restart reservation window and stops the lobby clock, so an untimed duel no longer ends at once on "time limit up". |
| v1.1.1 | Tag: no Battle Phase on turns 1, 2 and 3 (1A, 2A, 1B); the fourth duelist (2B) is the first who can battle. `MSG_SELECT_IDLECMD` carries `to_bp` = 0 on those turns and `Duel.IsAbleToEnterBP` answers false. Single duels: turn 1 only, as stock. |
| v1.3.0 | Tag partner cards reach observers: when spectators see all, `STOC_TAG_PARTNER_CARDS` goes to every observer for both sides at every send point, plus both sides in an observer's resync and the late-watcher snapshot. Same id, same payload, no new capability bit (TICKET-412). |
| v1.4.0 | Life points per side: with the setup pair `ygol.side.b.lp=<n>` (section (d)) seat side B (seat 1; Tag seats 2 and 3) starts at `n` LP and side A at slot 8; without it both sides start at slot 8, as before. `set_player_info` gets each duel player its own LP, and `MSG_START` (the start and the late-watcher snapshot) carries `lp[0]`, `lp[1]` in duel player order: the side that goes first is player 0, so the two values swap when side B won the toss. `STOC_JOIN_GAME`'s `start_lp` stays side A's. Single, Match (every duel) and Tag; no new message, no capability bit. |
| v1.5.0 | Tag two Field slots (proposal 45, TICKET-384; core `field::has_two_fields` = `DUEL_TAG_MODE` on Master Rule 4 or 5): a team has a Field slot per partner, Spell/Trap sequence 5 for the partner who plays the team's first turn and 6 for the other (`player_info::tag_partner` flips in `tag_swap`). A Field Spell that lands (activated, Set, `Duel.MoveToField` to `LOCATION_FZONE`) goes to the playing partner's slot and replaces only the card there; one that changes control while that slot is taken and the other is free takes the free one. A Set opens only the landing slot's bit in `MSG_SELECT_PLACE`. `LOCATION_FZONE` covers both slots (`Card.IsLocation`, `Duel.GetMatchingGroup` and the other filters, `Duel.IsEnvironment`, `Duel.CheckLocation(p, LOCATION_FZONE, 0/1)`), `Duel.GetFieldCard(p, LOCATION_FZONE, 0)` is the playing partner's field, else the other one, and sequence 1 the remaining one; `Duel.GetFieldCard(p, LOCATION_SZONE, 6)` works. The second field reaches a client as `MSG_MOVE` to Spell/Trap sequence 6 and in `MSG_RELOAD_FIELD`'s sixth Spell/Trap entry: no new message. Singles, Match and Master Rule 3 (where slot 6 is a Pendulum Zone) are unchanged. Scripts that name Spell/Trap sequence 5 by number see one field (the TICKET-384 closing note lists them). Capability `0x1000`, protocol 9 -> 10. |
| v1.6.0 | Tag shared Extra Deck (proposal 53, TICKET-433): with the setup pair `ygol.tag.extra=shared` (section (d)) `TagDuel` sets the duel option `DUEL_TAG_SHARED_EXTRA` `0x200`. `start_duel` moves each team's `tag_list_extra` into `list_extra` (the first partner's cards first) and renumbers the sequences, so either partner can Summon any of the cards; `tag_swap` leaves the Extra Deck alone, `MSG_TAG_SWAP` keeps its layout and sends the one list, and `ygol_query_tag_cards` / `0x5F` report an Extra count of 0. The opening `MSG_START` carries the sum of both partners' Extra Decks, and the opening Extra Deck refreshes run after `start_duel`. The core has one `owner` per team, so each card has `card::tag_member`, set once when it is loaded (0 `new_card`, 1 `new_tag_card`), and `RefreshExtra` adds `QUERY_TAG_MEMBER` to its flag in such a duel (section (c), Query records). Replays need no format change: `opt` holds the option and every reader passes `opt` to `start_duel`; a reader that writes its own `MSG_START` before `start_duel` must add the partner's Extra count. An older engine ignores the option and plays such a replay wrongly. Without the pair every stream is unchanged. Capability `0x4000`, protocol 10 -> 11. |
### Replay
Patch 0009 adds the replay flag `REPLAY_YGOL_SETUP_PAIRS` = `0x40000000`. When set, the replay
carries, before the player names, `u32 length` + the setup pairs block (`u16 count`, then per pair
`u16 length` + `key=value`). Limits: `SETUP_PAIRS_MAX` 64 pairs, `SETUP_PAIR_MAX_LEN` 512 bytes per
pair.
v1.4.0 adds the replay flag `REPLAY_YGOL_SIDE_LP` = `0x20000000`, set only when the two duel players
start with different LP (`ygol.side.b.lp`). When set, `i32 lp[0]` + `i32 lp[1]` (duel player order)
follow the stock `opt` field, before the decks; a reader without the flag keeps the stock layout and
gives both players the header's `start_lp`.
## (c) Engine message stream
`process` + `get_message` hand the consumer a buffer of messages; each is `u8 id` + payload. The
server relays them to clients inside `STOC_GAME_MSG` (`0x1`), masking the other side's hidden cards.
The table is the one `MessageScan.Scan` in the C# binding reads (section (e)); the ids are `MSG_*` in
`core/common.h`, the enum `GameMessage`.
`n` = a count byte read at that point; "prompt" = the engine waits for `set_responsei` /
`set_responseb` from `player`. **The fork adds NO message id and changes NO layout**; the only
fork-related rows are marked.
| Id | Name | Payload length rule | Prompt |
|---|---|---|---|
| 1 | `MSG_RETRY` | 0 | - |
| 2 | `MSG_HINT` | 6: `u8 type`, `u8 player`, `u32 data`. Types 1 EVENT, 2 MESSAGE, 3 SELECTMSG, 5 EFFECT go to that player only; the rest is public | - |
| 3 | `MSG_WAITING` | 0 | - |
| 4 | `MSG_START` | 18 | - |
| 5 | `MSG_WIN` | 2: `u8 player`, `u8 reason` | - |
| 6 | `MSG_UPDATE_DATA` | server only: `u8 player`, `u8 location`, then the query buffer | - |
| 7 | `MSG_UPDATE_CARD` | server only: `u8 player`, `u8 location`, `u8 sequence`, then the query buffer | - |
| 8 | `MSG_REQUEST_DECK` | 0 | - |
| 10 | `MSG_SELECT_BATTLECMD` | `u8 player`; `n` x 11 (activatable); `n` x 8 (attackable); 2 | yes |
| 11 | `MSG_SELECT_IDLECMD` | `u8 player`; 5 lists of `n` x 7; `n` x 11; 3 | yes |
| 12 | `MSG_SELECT_EFFECTYN` | `u8 player` + 12 | yes |
| 13 | `MSG_SELECT_YESNO` | `u8 player` + 4 | yes |
| 14 | `MSG_SELECT_OPTION` | `u8 player`; `n` x 4 | yes |
| 15 | `MSG_SELECT_CARD` | `u8 player` + 3; `n` x 8 | yes |
| 16 | `MSG_SELECT_CHAIN` | `u8 player`, `u8 n`, 9 more header bytes; `n` x 14 | yes |
| 18 | `MSG_SELECT_PLACE` | `u8 player` + 5 | yes |
| 19 | `MSG_SELECT_POSITION` | `u8 player` + 5 | yes |
| 20 | `MSG_SELECT_TRIBUTE` | `u8 player` + 3; `n` x 8 | yes |
| 21 | `MSG_SORT_CHAIN` | `u8 player`; `n` x 7 | yes |
| 22 | `MSG_SELECT_COUNTER` | `u8 player` + 4; `n` x 9 | yes |
| 23 | `MSG_SELECT_SUM` | 1, `u8 player`, 6 more; `n` x 11; `n` x 11 | yes |
| 24 | `MSG_SELECT_DISFIELD` | `u8 player` + 5 | yes |
| 25 | `MSG_SORT_CARD` | `u8 player`; `n` x 7 | yes |
| 26 | `MSG_SELECT_UNSELECT_CARD` | `u8 player` + 4; `n` x 8; `n` x 8 | yes |
| 30 | `MSG_CONFIRM_DECKTOP` | `u8 player`; `n` x 7 | - |
| 31 | `MSG_CONFIRM_CARDS` | `u8 player` + 1; `n` x 7 | - |
| 32 | `MSG_SHUFFLE_DECK` | `u8 player` | - |
| 33 | `MSG_SHUFFLE_HAND` | `u8 player`; `n` x 4 | - |
| 34 | `MSG_REFRESH_DECK` | `u8 player` | - |
| 35 | `MSG_SWAP_GRAVE_DECK` | `u8 player` | - |
| 36 | `MSG_SHUFFLE_SET_CARD` | 1; `n` x 8 | - |
| 37 | `MSG_REVERSE_DECK` | 0 | - |
| 38 | `MSG_DECK_TOP` | `u8 player` + 5 | - |
| 39 | `MSG_SHUFFLE_EXTRA` | `u8 player`; `n` x 4 | - |
| 40 | `MSG_NEW_TURN` | 1: `u8 player` | - |
| 41 | `MSG_NEW_PHASE` | 2 | - |
| 42 | `MSG_CONFIRM_EXTRATOP` | `u8 player`; `n` x 7 | - |
| 50 | `MSG_MOVE` | 16 | - |
| 53 | `MSG_POS_CHANGE` | 9 | - |
| 54 | `MSG_SET` | 8 | - |
| 55 | `MSG_SWAP` | 16 | - |
| 56 | `MSG_FIELD_DISABLED` | 4 | - |
| 60 | `MSG_SUMMONING` | 8 | - |
| 61 | `MSG_SUMMONED` | 0 | - |
| 62 | `MSG_SPSUMMONING` | 8 | - |
| 63 | `MSG_SPSUMMONED` | 0 | - |
| 64 | `MSG_FLIPSUMMONING` | 8 | - |
| 65 | `MSG_FLIPSUMMONED` | 0 | - |
| 70 | `MSG_CHAINING` | 16 | - |
| 71 | `MSG_CHAINED` | 1 | - |
| 72 | `MSG_CHAIN_SOLVING` | 1 | - |
| 73 | `MSG_CHAIN_SOLVED` | 1 | - |
| 74 | `MSG_CHAIN_END` | 0 | - |
| 75 | `MSG_CHAIN_NEGATED` | 1 | - |
| 76 | `MSG_CHAIN_DISABLED` | 1 | - |
| 80 | `MSG_CARD_SELECTED` | 1; `n` x 4 | - |
| 81 | `MSG_RANDOM_SELECTED` | 1; `n` x 4 | - |
| 83 | `MSG_BECOME_TARGET` | `n` x 4 | - |
| 90 | `MSG_DRAW` | `u8 player`; `n` x 4 | - |
| 91 | `MSG_DAMAGE` | 5 | - |
| 92 | `MSG_RECOVER` | 5 | - |
| 93 | `MSG_EQUIP` | 8 | - |
| 94 | `MSG_LPUPDATE` | 5 | - |
| 95 | `MSG_UNEQUIP` | 4 | - |
| 96 | `MSG_CARD_TARGET` | 8 | - |
| 97 | `MSG_CANCEL_TARGET` | 8 | - |
| 100 | `MSG_PAY_LPCOST` | 5 | - |
| 101 | `MSG_ADD_COUNTER` | 7 | - |
| 102 | `MSG_REMOVE_COUNTER` | 7 | - |
| 110 | `MSG_ATTACK` | 8 | - |
| 111 | `MSG_BATTLE` | 26 | - |
| 112 | `MSG_ATTACK_DISABLED` | 0 | - |
| 113 | `MSG_DAMAGE_STEP_START` | 0 | - |
| 114 | `MSG_DAMAGE_STEP_END` | 0 | - |
| 120 | `MSG_MISSED_EFFECT` | 8 | - |
| 121 | `MSG_BE_CHAIN_TARGET` | 0 | - |
| 122 | `MSG_CREATE_RELATION` | 0 | - |
| 123 | `MSG_RELEASE_RELATION` | 0 | - |
| 130 | `MSG_TOSS_COIN` | `u8 player`; `n` x 1 | - |
| 131 | `MSG_TOSS_DICE` | `u8 player`; `n` x 1 | - |
| 132 | `MSG_ROCK_PAPER_SCISSORS` | `u8 player` | yes |
| 133 | `MSG_HAND_RES` | 2 | - |
| 140 | `MSG_ANNOUNCE_RACE` | `u8 player` + 5 | yes |
| 141 | `MSG_ANNOUNCE_ATTRIB` | `u8 player` + 5 | yes |
| 142 | `MSG_ANNOUNCE_CARD` | `u8 player`; `n` x 4 | yes |
| 143 | `MSG_ANNOUNCE_NUMBER` | `u8 player`; `n` x 4 | yes |
| 160 | `MSG_CARD_HINT` | 9 | - |
| 161 | `MSG_TAG_SWAP` | `u8 player`, `u8 main count`, `u8 extra count`, `u8 extra pendulum count`, `u8 hand count`, `u32 top code`, then `u32` x hand, `u32` x extra. Not in `Scan` (the bot host never runs Tag). **Fork (core patch 0015): never emitted on a team's first turn; the first swap comes on turn 3 or later. v1.2.0: the server follows every swap with `STOC_TAG_PARTNER_CARDS` to the swapping side's two seats; v1.3.0: and to every observer when spectators see all. v1.4.1 (TICKET-415): a card an effect moves out of the waiting member's hand, deck or Extra Deck (one it kept across the swap, e.g. Time Fusion's Fusion) leaves that list, so the next swap lists one card fewer; its `MSG_MOVE` `from` sequence counts into the waiting list, not the active one.** | - |
| 162 | `MSG_RELOAD_FIELD` | `u8 duel rule`; per player: `u32 lp`, 7 monster zones (`u8 present`, +2 when present), 8 spell zones (`u8 present`, +1 when present), 6 count bytes; then ONE chain block `n` x 15 after both players. Produced by `query_field_info` and `Debug.ReloadFieldEnd()`; the server sends it as the 0005 field resync | - |
| 163 | `MSG_AI_NAME` | `u16 len` + bytes + 1 | - |
| 164 | `MSG_SHOW_HINT` | `u16 len` + bytes + 1 | - |
| 165 | `MSG_PLAYER_HINT` | 6 | - |
| 170 | `MSG_MATCH_KILL` | 4 | - |
| 180 | `MSG_CUSTOM_MSG` | 0 | - |
| 221 | `MSG_RESET_TIME` | 3 | - |
Ids 6, 7 and 34 are server-side (`network.h`); the core's `common.h` lists the rest. Any other id is
an error for the consumer.
### Query records
`query_card`, `query_field_card` and the query buffer of `MSG_UPDATE_DATA` / `MSG_UPDATE_CARD` hold one
record per card: `i32 length`, `u32 flag`, then one value per set `QUERY_*` bit in bit order, the bits
in `core/common.h`. The fork adds one bit:
| Bit | Name | Value | Since |
|---|---|---|---|
| `0x1000000` | `QUERY_TAG_MEMBER` | `u32`, written LAST, after `QUERY_LINK`: the partner who brought the card, 0 from `new_card`, 1 from `new_tag_card`. It never changes when the card moves. The core writes it for any caller that sets the bit; the server sets it only on `RefreshExtra` in a duel with `ygol.tag.extra=shared` | v1.6.0 |
**Bit `0x100000` stays unused.** Old flags set it with no value after it: `RefreshSingle`'s default
`0xf81fff`, the hand refresh `0x181fff` and YGOSharp's `0xFFFFFF` mask, and saved `.yrp3d` replays are
recorded streams of them. A value for that bit would shift every later field of those records, with no
error.
## (d) Setup pairs and the Lua API
### Setup pairs (patch 0009, TICKET-258; ADR 0004 in the Unity repo)
A setup pair is a host-string token `ygol.<key>=<value>` after slot 12 (section (a)). The server
never interprets a pair: it stores the ordered list (max 64 pairs, 512 bytes each; a token without
`=` or with an empty key is dropped with a stderr line), writes each one into the duel registry
(`set_registry_value(key, value)`) before `special.lua` runs, writes them into the replay (flag
`0x40000000`) and sends them to every joiner as `STOC_SETUP_PAIRS` (`0x43`). Values that need `,`,
`=`, `%` or a space are percent-encoded by the client (`MatchSetup.cs`).
Four pairs ARE read by the server itself:
| Key | Value | Read by |
|---|---|---|
| `ygol.room.watchers` | `0` = no observers: every observer join and HsToObserver is refused | 0012 |
| `ygol.room.spectatorview` | `hidden` = the stock masked observer stream; absent = spectators see all | 0018, 0019 |
| `ygol.side.b.lp` | a positive integer: seat side B's start LP (seat 1; Tag seats 2 and 3); side A keeps slot 8. Absent, `0`, negative or not a number = both sides at slot 8 | v1.4.0 |
| `ygol.tag.extra` | `shared` = a Tag team has one Extra Deck (option `0x200`, section (b) v1.6.0); anything else or absent = separate Extra Decks. A Single or Match room ignores it | v1.6.0 |
Keys the Unity client sends today (`Assets/YGOLegacy/Scripts/Core/Duel/MatchSetup.cs`,
`SetupPairKeys`); the rules Lua reads them from the registry:
| Key | Value |
|---|---|
| `ygol.ruleset` | Ruleset id (`master`, `rush`, ...) |
| `ygol.scripts` | Comma list of Lua scripts to run (`rulesets/rush.lua`, `mutators/<id>.lua`), each name encoded |
| `ygol.room.host` | The host's identity |
| `ygol.map` | Map name |
| `ygol.mutator.<id>.<key>` | A mutator parameter (`<id>` and `<key>` encoded) |
### Lua API
The card-script API the core registers is generated from `core/lib*.cpp` and `core/interpreter.cpp`
into `contract/lua-reference/`: `api-card.md`, `api-duel.md`, `api-effect.md`, `api-group.md`,
`api-debug.md` (the function tables; 670 functions in 5 tables) and `api-globals.md` (the 37 globals
the core sets). A name that is not there is not in the core; the card scripts' `library/` defines the
rest. Regenerate with `python contract/lua-reference/generate.py` after any core change to those
files and commit the result with it. The fork's core patches add no Lua function (0001 adds the
setcode `0xFDF`, `SETCODE_FACEDOWN_TOKEN`, as a cdb convention, not an API). v1.5.0 gives two
existing calls a second answer in Tag on Master Rule 4/5: `Duel.GetFieldCard(p, LOCATION_FZONE, 1)`
is the other partner's field and `Duel.CheckLocation(p, LOCATION_FZONE, 1)` asks about its slot
(both say nothing elsewhere, as before).
## (e) C# binding
The managed side of the engine ships in the same release as the native libraries: the asset
`ygol-engine-binding.zip` holds `YGOLegacy.Engine.dll` and `YGOLegacy.Engine.pdb`, built from
`binding/YGOLegacy.Engine/` (`netstandard2.1`, LangVersion 9, unsafe allowed, no package and no Unity
reference) and tested by `binding/YGOLegacy.Engine.Tests/` (`net8.0`, NUnit) in the release workflow's
`windows` job. Unity, the Tools and the server repo's card-tester consume this DLL instead of their own
copies of the sources.
Namespaces:
| Namespace | What |
|---|---|
| `YGOLegacy.Protocol.{Contracts, Codecs, Framing, Routing}` | Section (b) in C#: the message ids and payload records, the CTOS / STOC codecs, the frame codec and the STOC router. Unchanged from the Unity repo (TICKET-397); v1.2.0 adds `StocMessage.TagPartnerCards`, `FeatureFlags.TagPartnerCards`, the `TagPartnerCards` record and `StocCodecs.TagPartnerCards`, and `Capability.ProtocolVersion` is 9; v1.4.0 adds `ReplayFlags` (`SetupPairs` = `0x40000000`, `SideLp` = `0x20000000`); v1.5.0 adds `FeatureFlags.TagTwoFields` (`0x1000`, in `All`) and `Capability.ProtocolVersion` is 10. |
| `YGOLegacy.Engine` | The four types below (TICKET-398). |
| Type | What |
|---|---|
| `EngineApi` | `public static unsafe class`: one `DllImport("ygol-engine", Cdecl)` extern per export of section (a), C names kept, no overloads; the delegates `ScriptReader`, `CardReader`, `MessageHandler` (`UnmanagedFunctionPointer(Cdecl)`) beside it. `[MonoPInvokeCallback]` belongs on the consumer's callback methods (IL2CPP), not here. |
| `MessageScan` | `public static class`: the section (c) layout scanner, moved from the Unity bot host (`LocalDuelHost`) with its bodies unchanged. `MsgInfo` (player, prompt, to-player-only, new-turn), `Scan(byte[] b, ref int pos, int id, int len)` (advances `pos` past one message and says who may see it; throws on an unknown id or an overrun), `Blank(id, msg, info, recipient)` (hidden-information blanking per recipient for `MSG_SHUFFLE_HAND`, `MSG_SHUFFLE_EXTRA`, `MSG_DRAW`, `MSG_MOVE`, `MSG_SET`), `BlankPileCodes(buf, all)` (zeroes the `QUERY_CODE` field of query records) and `PromptName(id)`. |
| `GameMessage` | `public enum GameMessage : byte`: one member per `#define MSG_<NAME> <n>` in `core/common.h` (84), member = PascalCase of `<NAME>` (`MSG_SELECT_BATTLECMD` -> `SelectBattlecmd`), value = `<n>`. `GameMessageParityTests` parses the header and fails on a missing, extra or renumbered id. The commented-out defines (`MSG_WAITING`, `MSG_START`, `MSG_UPDATE_DATA`, ...) are not members. |
| `EngineBinding` | `public const string Version` (the tag the DLL was built for, `local` when none; generated at build from the `EngineVersion` property) and `public static string CheckNative()`: returns `ygol_engine_version()` when it equals `Version`, else throws `InvalidOperationException` naming both strings. |
### `EngineApi` externs
Every row is one export of section (a) with its managed signature. A `string` parameter is
`[MarshalAs(UnmanagedType.LPUTF8Str)]`.
| Export | Managed signature |
|---|---|
| `set_script_reader` | `void set_script_reader(ScriptReader f)` |
| `set_card_reader` | `void set_card_reader(CardReader f)` |
| `set_message_handler` | `void set_message_handler(MessageHandler f)` |
| `create_duel` | `IntPtr create_duel(uint seed)` |
| `create_duel_v2` | `IntPtr create_duel_v2(uint[] seed_sequence)` |
| `create_duel_ex` | `IntPtr create_duel_ex(uint seed, uint create_flags)` |
| `create_duel_v2_ex` | `IntPtr create_duel_v2_ex(uint[] seed_sequence, uint create_flags)` |
| `start_duel` | `void start_duel(IntPtr pduel, uint options)` |
| `end_duel` | `void end_duel(IntPtr pduel)` |
| `set_player_info` | `void set_player_info(IntPtr pduel, int playerid, int lp, int startcount, int drawcount)` |
| `get_log_message` | `void get_log_message(IntPtr pduel, byte[] buf)` |
| `get_message` | `int get_message(IntPtr pduel, byte[] buf)` |
| `process` | `uint process(IntPtr pduel)` |
| `new_card` | `void new_card(IntPtr pduel, uint code, byte owner, byte playerid, byte location, byte sequence, byte position)` |
| `new_tag_card` | `void new_tag_card(IntPtr pduel, uint code, byte owner, byte location)` |
| `query_card` | `int query_card(IntPtr pduel, byte playerid, byte location, byte sequence, uint query_flag, byte[] buf, int use_cache)` |
| `query_field_count` | `int query_field_count(IntPtr pduel, byte playerid, byte location)` |
| `query_field_card` | `int query_field_card(IntPtr pduel, byte playerid, byte location, uint query_flag, byte[] buf, int use_cache)` |
| `query_field_info` | `int query_field_info(IntPtr pduel, byte[] buf)` |
| `set_responsei` | `void set_responsei(IntPtr pduel, int value)` |
| `set_responseb` | `void set_responseb(IntPtr pduel, byte[] buf)` |
| `preload_script` | `int preload_script(IntPtr pduel, string script_name)` |
| `default_script_reader` | `IntPtr default_script_reader(string script_name, int* len)` |
| `ygol_preview_attack` | `int ygol_preview_attack(IntPtr pduel, byte playerid, byte location, byte sequence, byte target_location, byte target_sequence, byte[] @out)` |
| `ygol_query_tag_cards` | `int ygol_query_tag_cards(IntPtr pduel, byte playerid, byte[] @out)` |
| `get_registry_value` | `int get_registry_value(IntPtr pduel, string key, byte[] out_buf)` |
| `set_registry_value` | `void set_registry_value(IntPtr pduel, string key, string value)` |
| `get_registry_keys` | `int get_registry_keys(IntPtr pduel, byte[] out_buf)` |
| `clear_registry` | `void clear_registry(IntPtr pduel)` |
| `dump_registry` | `int dump_registry(IntPtr pduel, byte[] out_buf)` |
| `load_registry` | `void load_registry(IntPtr pduel, byte[] in_buf, int in_len)` |
| `get_lua_coverage_dump_size` | `int get_lua_coverage_dump_size(IntPtr pduel, string name)` |
| `dump_lua_coverage` | `int dump_lua_coverage(IntPtr pduel, string name, byte[] out_buf, int out_len)` |
| `get_all_lua_coverages_dump_size` | `int get_all_lua_coverages_dump_size(IntPtr pduel)` |
| `dump_all_lua_coverages` | `int dump_all_lua_coverages(IntPtr pduel, byte[] out_buf, int out_len)` |
| `clear_lua_coverage` | `void clear_lua_coverage(IntPtr pduel, string name)` |
| `clear_all_lua_coverages` | `void clear_all_lua_coverages(IntPtr pduel)` |
| `ygol_sb_add` | `int ygol_sb_add(IntPtr pduel, uint code, byte owner, byte playerid, byte location, byte sequence, byte position)` |
| `ygol_sb_move` | `int ygol_sb_move(IntPtr pduel, byte playerid, byte location, byte sequence, byte dst_playerid, byte dst_location, byte dst_sequence, byte position)` |
| `ygol_sb_remove` | `int ygol_sb_remove(IntPtr pduel, byte playerid, byte location, byte sequence)` |
| `ygol_sb_set_position` | `int ygol_sb_set_position(IntPtr pduel, byte playerid, byte location, byte sequence, byte position)` |
| `ygol_sb_set_lp` | `int ygol_sb_set_lp(IntPtr pduel, byte playerid, int lp)` |
| `ygol_sb_reask` | `int ygol_sb_reask(IntPtr pduel)` |
| `start_server` | `int start_server(string args)` |
| `stop_server` | `void stop_server()` |
| `ygol_engine_version` | `IntPtr ygol_engine_version()` |
Delegates: `IntPtr ScriptReader(string script_name, int* len)`, `uint CardReader(uint code, IntPtr data)`
(`data` is the `card_data*` the core wants filled), `uint MessageHandler(IntPtr pduel, uint msg_type)`.
**Type rule** (the header has no wide string, so `CharSet.Unicode` appears nowhere): `intptr_t` ->
`IntPtr`; `int32_t` / `int` -> `int`; `uint32_t` -> `uint`; `uint8_t` -> `byte`; `uint_fast32_t` ->
`uint` (32-bit on MSVC x64, 64-bit on glibc x64; the register is zero-extended); `byte*`, `char* buf`,
`uint8_t* out`, `const byte*` -> `byte[]`; `uint32_t seed_sequence[]` -> `uint[]`; `const char*` in ->
`string` with `LPUTF8Str`; `const char*` out (`ygol_engine_version`) and `byte*` out
(`default_script_reader`) -> `IntPtr`; `int*` -> `int*`; `card_data*` -> `IntPtr`; function pointers ->
the delegates. `EngineApiParityTests` parses `contract/ocgapi.h` and fails when a header export has no
extern of that name or an extern has no header line.
**Version rule.** The binding's version is the engine tag: `EngineBinding.Version` = `engine.json`'s
`binding` = `engine.json`'s `version` = `ygol_engine_version()`, all the same string for one release. A
consumer calls `EngineBinding.CheckNative()` once at start and treats the exception as "fetch the engine
release whose tag equals the binding version". The native libraries and the binding are released
together even when only one of them changed.