From 63bb49c0d93f4677bb7f9b3980232bf1cccb5fb6 Mon Sep 17 00:00:00 2001 From: donnyjang Date: Wed, 14 Oct 2026 22:43:58 -0700 Subject: [PATCH] build: contract/ocgapi.h is checked against its two sources; engine.json is written next to the library TICKET-388. contract/ocgapi.h is core/ocgapi.h verbatim plus the server's YGOSERVER_API lines in a marked block. windows.ps1 refuses to build when either part differs (tested: a changed server line and a changed core line each fail; the real file passes). After the export gate it writes out/win64/engine.json { version, exports, protocol, capabilities }: protocol from FEATURE_PROTOCOL in gframe/network.h, capabilities as the OR of the FEATURE_* bits on the cap.flags line of single_duel.cpp and tag_duel.cpp (which must agree), each bit resolved from network.h. Nothing typed in. Verified: protocol 8, capabilities 0x7FF (2047), exports 334. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DJUFwdiGCHipsxLkgxssSR --- build/windows.ps1 | 50 +++++++++++++++++++ contract/ocgapi.h | 124 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 contract/ocgapi.h diff --git a/build/windows.ps1 b/build/windows.ps1 index 688f9eb..08d88a6 100644 --- a/build/windows.ps1 +++ b/build/windows.ps1 @@ -12,6 +12,13 @@ -Version is what ygol_engine_version() returns ("local" when not given). + Before building, the script checks contract\ocgapi.h: it must be core\ocgapi.h + verbatim plus the server's YGOSERVER_API lines from gframe\serverapi.h in its + marked block; a difference fails the build (the contract is a copy kept honest). + After the gate it writes out\win64\engine.json: { version, exports, protocol, + capabilities }, protocol and capabilities READ from gframe\network.h and the + SendServerCapability line of single_duel.cpp / tag_duel.cpp, never typed in. + Output: out\win64\ygol-engine.dll (+ .pdb). The export gate is the truth: the script FAILS unless the DLL exports exactly 334 symbols (start_server, stop_server, ygol_engine_version, and the core C API re-exported, ygol_sb_* @@ -47,6 +54,45 @@ if (-not (Test-Path $msbuild)) { throw "MSBuild not found at $msbuild" } $dumpbin = Get-ChildItem "$vs\VC\Tools\MSVC\*\bin\Hostx64\x64\dumpbin.exe" -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $dumpbin) { throw "dumpbin not found under $vs (install the C++ workload)" } +# The contract header is a copy of two sources; refuse to build when it drifted. +$contractPath = Join-Path $repo "contract\ocgapi.h" +$contract = [IO.File]::ReadAllText($contractPath) -replace "`r`n", "`n" +$coreHeader = [IO.File]::ReadAllText((Join-Path $repo "core\ocgapi.h")) -replace "`r`n", "`n" +$blockStart = $contract.IndexOf("// --- server exports") +$blockEnd = $contract.IndexOf("// --- end server exports ---`n") +if ($blockStart -lt 0 -or $blockEnd -lt 0) { throw "contract\ocgapi.h has no server exports block" } +$blockEnd += "// --- end server exports ---`n".Length +$block = $contract.Substring($blockStart, $blockEnd - $blockStart) +$withoutBlock = $contract.Substring(0, $blockStart) + $contract.Substring($blockEnd) +$withoutBlock = $withoutBlock -replace "(?s)\A// contract/ocgapi.h:.*?`n(?=/\*)", "" +$withoutBlock = $withoutBlock -replace "`n`n#endif /\* OCGAPI_H_ \*/`n\z", "`n#endif /* OCGAPI_H_ */`n" +if ($withoutBlock -ne $coreHeader) { throw "contract\ocgapi.h differs from core\ocgapi.h outside the server exports block; update the contract copy" } +$serverLines = @(Get-Content (Join-Path $server "gframe\serverapi.h") | Where-Object { $_ -match "^\s*YGOSERVER_API\s+\S.*;" } | ForEach-Object { $_.Trim() }) +$blockLines = @($block -split "`n" | Where-Object { $_ -match "^\s*YGOSERVER_API\s+\S.*;" } | ForEach-Object { $_.Trim() }) +if ($serverLines.Count -eq 0) { throw "no YGOSERVER_API declarations found in gframe\serverapi.h" } +if (($serverLines -join "`n") -ne ($blockLines -join "`n")) { throw "contract\ocgapi.h server exports block differs from gframe\serverapi.h:`n serverapi.h: $($serverLines -join ' | ')`n contract: $($blockLines -join ' | ')" } +Write-Host "contract\ocgapi.h matches core\ocgapi.h + serverapi.h ($($serverLines.Count) server exports)" + +# engine.json facts, read from the server source. +$networkH = Get-Content (Join-Path $server "gframe\network.h") +$protoLine = $networkH | Where-Object { $_ -match "^constexpr uint16_t FEATURE_PROTOCOL = (\d+);" } | Select-Object -First 1 +if (-not $protoLine) { throw "FEATURE_PROTOCOL not found in gframe\network.h" } +$protocol = [int]$Matches[1] +$bits = @{} +foreach ($l in $networkH) { if ($l -match "^constexpr uint32_t (FEATURE_[A-Z_]+) = (0x[0-9A-Fa-f]+);") { $bits[$Matches[1]] = [Convert]::ToUInt32($Matches[2], 16) } } +$capExprs = @() +foreach ($f in @("single_duel.cpp", "tag_duel.cpp")) { + $l = Get-Content (Join-Path $server "gframe\$f") | Where-Object { $_ -match "^\s*cap\.flags = (.+?);" } | Select-Object -First 1 + if (-not $l) { throw "cap.flags line not found in gframe\$f" } + $capExprs += ($Matches[1] -replace "\s", "") +} +if ($capExprs[0] -ne $capExprs[1]) { throw "single_duel.cpp and tag_duel.cpp advertise different capability flags" } +[uint32]$capabilities = 0 +foreach ($name in ($capExprs[0] -split "\|")) { + if (-not $bits.ContainsKey($name)) { throw "capability $name has no constexpr uint32_t in gframe\network.h" } + $capabilities = $capabilities -bor $bits[$name] +} + $buildStart = Get-Date Push-Location $server try { @@ -91,3 +137,7 @@ Write-Host "" Write-Host "Built: $out" Write-Host "exports: $n (expected $ExpectedExports)" if ($n -ne $ExpectedExports) { throw "export count $n != $ExpectedExports; do NOT ship this build" } + +$json = "{`n `"version`": `"$Version`",`n `"exports`": $n,`n `"protocol`": $protocol,`n `"capabilities`": $capabilities`n}`n" +[IO.File]::WriteAllText((Join-Path $outDir "engine.json"), $json, (New-Object Text.UTF8Encoding $false)) +Write-Host "engine.json: version $Version, exports $n, protocol $protocol, capabilities 0x$($capabilities.ToString('X'))" diff --git a/contract/ocgapi.h b/contract/ocgapi.h new file mode 100644 index 0000000..8696819 --- /dev/null +++ b/contract/ocgapi.h @@ -0,0 +1,124 @@ +// contract/ocgapi.h: the public C API of ygol-engine. This file is core/ocgapi.h verbatim plus the +// server's exports in the block before the final #endif. build/windows.ps1 refuses to build when it +// differs from those two sources; edit the sources, then this copy, never this copy alone. +/* + * interface.h + * + * Created on: 2010-4-28 + * Author: Argon + */ + +#ifndef OCGAPI_H_ +#define OCGAPI_H_ + +#include "common.h" + +#ifdef __cplusplus +#define EXTERN_C extern "C" +#else +#define EXTERN_C +#endif + +#ifndef OCGCORE_API +#if defined(__EMSCRIPTEN__) +#include +#define OCGCORE_API EXTERN_C EMSCRIPTEN_KEEPALIVE +#elif defined(_WIN32) +#define OCGCORE_API EXTERN_C __declspec(dllexport) +#else +#define OCGCORE_API EXTERN_C __attribute__ ((visibility ("default"))) +#endif +#endif + +#define SEED_COUNT 8 + +#define LEN_FAIL 0 +#define LEN_EMPTY 4 +#define LEN_HEADER 8 +#define TEMP_CARD_ID 0 + +struct card_data; + +typedef byte* (*script_reader)(const char* script_name, int* len); +typedef uint32_t (*card_reader)(uint32_t code, card_data* data); +typedef uint32_t (*message_handler)(intptr_t pduel, uint32_t msg_type); + +OCGCORE_API void set_script_reader(script_reader f); +OCGCORE_API void set_card_reader(card_reader f); +OCGCORE_API void set_message_handler(message_handler f); + +byte* read_script(const char* script_name, int* len); +uint32_t read_card(uint32_t code, card_data* data); +uint32_t handle_message(void* pduel, uint32_t message_type); + +OCGCORE_API intptr_t create_duel(uint_fast32_t seed); +OCGCORE_API intptr_t create_duel_v2(uint32_t seed_sequence[]); +OCGCORE_API intptr_t create_duel_ex(uint_fast32_t seed, uint32_t create_flags); +OCGCORE_API intptr_t create_duel_v2_ex(uint32_t seed_sequence[], uint32_t create_flags); +OCGCORE_API void start_duel(intptr_t pduel, uint32_t options); +OCGCORE_API void end_duel(intptr_t pduel); +OCGCORE_API void set_player_info(intptr_t pduel, int32_t playerid, int32_t lp, int32_t startcount, int32_t drawcount); +OCGCORE_API void get_log_message(intptr_t pduel, char* buf); +OCGCORE_API int32_t get_message(intptr_t pduel, byte* buf); +OCGCORE_API uint32_t process(intptr_t pduel); +OCGCORE_API void new_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position); +OCGCORE_API void new_tag_card(intptr_t pduel, uint32_t code, uint8_t owner, uint8_t location); +OCGCORE_API 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); +OCGCORE_API int32_t query_field_count(intptr_t pduel, uint8_t playerid, uint8_t location); +OCGCORE_API int32_t query_field_card(intptr_t pduel, uint8_t playerid, uint8_t location, uint32_t query_flag, byte* buf, int32_t use_cache); +OCGCORE_API int32_t query_field_info(intptr_t pduel, byte* buf); +OCGCORE_API void set_responsei(intptr_t pduel, int32_t value); +OCGCORE_API void set_responseb(intptr_t pduel, byte* buf); +OCGCORE_API int32_t preload_script(intptr_t pduel, const char* script_name); +OCGCORE_API byte* default_script_reader(const char* script_name, int* len); + +// [YGOL] patch 0022 (attack preview): a read-only dry run of the damage step for one attacker and one +// target (target_location 0xFF = direct). 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. 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 field::get_attack_target would not offer answers +// illegal alone with every number 0 (patch 0024). Call it only while the engine is parked on a prompt; +// the duel is byte-identical afterwards (proposal 43). +OCGCORE_API 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); + +// koishi specific +OCGCORE_API int32_t get_registry_value(intptr_t pduel, const char* key, byte* out_buf); +OCGCORE_API void set_registry_value(intptr_t pduel, const char* key, const char* value); +OCGCORE_API int32_t get_registry_keys(intptr_t pduel, byte* out_buf); +OCGCORE_API void clear_registry(intptr_t pduel); +OCGCORE_API int32_t dump_registry(intptr_t pduel, byte* out_buf); +OCGCORE_API void load_registry(intptr_t pduel, const byte* in_buf, int32_t in_len); +OCGCORE_API int32_t get_lua_coverage_dump_size(intptr_t pduel, const char* name); +OCGCORE_API int32_t dump_lua_coverage(intptr_t pduel, const char* name, byte* out_buf, int32_t out_len); +OCGCORE_API int32_t get_all_lua_coverages_dump_size(intptr_t pduel); +OCGCORE_API int32_t dump_all_lua_coverages(intptr_t pduel, byte* out_buf, int32_t out_len); +OCGCORE_API void clear_lua_coverage(intptr_t pduel, const char* name); +OCGCORE_API void clear_all_lua_coverages(intptr_t pduel); + + +// [YGOL] patch 0008 (sandbox god mode): additive, Sandbox-only exports. Nothing a normal +// duel, replay, BotArena or YGOSharp calls. Each returns 1 when it did the work, 0 when it +// refused and changed nothing. +OCGCORE_API 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); +OCGCORE_API 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); +OCGCORE_API int32_t ygol_sb_remove(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence); +OCGCORE_API int32_t ygol_sb_set_position(intptr_t pduel, uint8_t playerid, uint8_t location, uint8_t sequence, uint8_t position); +OCGCORE_API int32_t ygol_sb_set_lp(intptr_t pduel, uint8_t playerid, int32_t lp); +OCGCORE_API int32_t ygol_sb_reask(intptr_t pduel); + +// --- server exports (server/gframe/serverapi.h); the build fails if these lines differ from it --- +#define YGOSERVER_API OCGCORE_API +#ifdef __cplusplus +namespace ygo { +#endif + YGOSERVER_API int start_server(const char* args); + YGOSERVER_API void stop_server(); + YGOSERVER_API const char* ygol_engine_version(); +#ifdef __cplusplus +} +#endif +// --- end server exports --- + +#endif /* OCGAPI_H_ */