Files
yugioh-legacy-engine/README.md
T
donnyandClaude Opus 5.5 ca2004112b
Release / windows (push) Successful in 40s
Release / android (push) Successful in 53s
Release / linux (push) Successful in 1m34s
Release / release (push) Skipped
Release / attach-windows (push) Skipped
contract: prompt context rows, changelog v1.7.0, probe suite (TICKET-451)
Contract: the export row (22 bytes, the stable index, the hidden rule), the
0x44 message row (23 bytes), bit 0x8000, protocol 12, binding rows. README
export counts. Probe: Single, a second Single duel with another deck order
and an opponent without the bit, Tag: a card asks in cost, target and
operation, a Fusion's material procedure asks inside that operation and the
outer context comes back, then the card hides in the deck and asks both
players.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 16:05:09 -07:00

200 lines
13 KiB
Markdown

# yugioh-legacy-engine
The duel engine of Yu-Gi-Oh! Legacy: the rules core and the duel server, built as ONE native
library per platform, `ygol-engine`, behind a written contract. Proposal 47 in the Unity repo
(`Docs/Proposals/GameArchitecture/47-engine-repo.md`) is the decision; this repo carries it out.
## Layout
| Path | What |
|---|---|
| `core/` | The rules core (C++, Lua 5.4 inside). Plain directory, no submodule. |
| `server/` | The duel server (C++, libevent) with its third-party sources vendored inside. Its premake builds the core from `../core`. |
| `build/windows.ps1` | Builds `out/win64/ygol-engine.dll` (+ `engine.json`), checks the contract header, runs the export gate. |
| `build/Dockerfile` | The Linux x64 and Android arm64 builds, targets `linux` and `android`, same checks and gates. |
| `build/linux.sh`, `build/android.sh` | Run the Docker targets into `out/linux-x64/` and `out/android-arm64/`. Need Docker and nothing else. |
| `binding/` | The C# binding `YGOLegacy.Engine` (the wire protocol: contracts, codecs, framing, routing; `EngineApi`, `MessageScan`, `GameMessage`, `EngineBinding`) and its NUnit tests. See "Binding". |
| `contract/` | The contract: `engine-contract.md` (the one spec), `ocgapi.h` (the public header), `probe-feature-messages.py` (the wire gate), `lua-reference/` (the generated Lua API). |
| `CHANGELOG.md` | One row per contract change; the replayed patches 0001-0024 first. |
There is exactly one copy of the core in this repo. The server's old `ocgcore/` submodule is gone;
`server/premake5.lua` includes `../core`, and the six `gframe` sources that named `../ocgcore/`
now name `../../core/`.
## Sources (upstream is cut)
`core/` and `server/` start from two pinned upstream commits and carry our commits on top. There is
no upstream remote, subtree or submodule; a wanted upstream fix is copied in by hand as a normal commit.
| Directory | Upstream | Commit |
|---|---|---|
| `core/` | `nanahira/ygopro-core` (as vendored in the Unity repo, `Tools/YGOLegacy/ocgcore`) | `973672dc9ce7cb49f47173277ab1ad0e318b7593` |
| `server/` | `https://code.moenext.com/nanahira/ygopro.git`, branch `server` | `52c868ce071ea0f2fcb0cf39bf2fb4151d12b90c` |
The server import is the build-relevant part of that tree only: `gframe/`, `premake5.lua`, `.ci/`,
`LICENSE`, and `gframe/ygopro.rc` + `.ico` (upstream keeps them in `resource/` and copies them in
before building). No game assets, no card scripts.
Vendored third-party sources (the ones upstream's Windows CI downloads), at the positions its
premake expects, each with its premake fragment from upstream's `premake/` folder inside it:
| Dir | Source | Trimmed |
|---|---|---|
| `server/lua/` | lua 5.4.8 | no |
| `server/sqlite3/` | sqlite-autoconf 3.53.3 | no |
| `server/event/` | libevent 2.1.13-stable | tests, samples, autotools helpers; the files its Windows prebuild copies into `include/` (gitignored) |
| `server/zlib/` | zlib 1.3.2 | contrib, examples, platform folders |
| `server/lzma/` | xz 5.8.3 | `src/liblzma` + `src/common` only |
| `server/irrlicht/` | mycard irrlicht-1.9 `20f86d251624334d53585a1809ea4c7178f72a34` | `include/` plus the `source/Irrlicht` headers and the 11 `.cpp` files the server's zip reader project compiles (`premake5-only-zipreader.lua`). The pro3 server build turns zip support on, so this is source, not headers alone. |
## Fork changes as commits
The Unity repo's 24 patch files (`Tools/YGOLegacy/patches/`) are replayed here once as ordinary
commits and retire. Six core patches (0001, 0004, 0008, 0015, 0022, 0024) were already in the
vendored core when it was imported, so they are part of the `core:` import commit. The 16 server
patches are one commit each, in the order the old build script applied them:
0002, 0003, 0005, 0006, 0009, 0011, 0012, 0013, 0014, 0016, 0017, 0018, 0019, 0020, 0021, 0023.
Their `[YGOL]` markers are kept. `git log --oneline -- server/gframe` lists them.
## Build (Windows)
```powershell
git clone https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-engine.git C:\ygol-engine
pwsh C:\ygol-engine\build\windows.ps1 -Version v0.0.1
```
- The repo path must have NO spaces: libevent's premake emits an unquoted `xcopy` prebuild step. The
script refuses otherwise.
- Needs premake 5.0.0-beta8 (`-Premake`, default `C:\premake\premake5.exe`), VS 2022 Build Tools with
the C++ workload and a Windows 10 SDK.
- The script wipes `server/obj`, `server/bin` and `server/build` first (an incremental MSBuild once
reused a stale object and the DLL still built), runs `premake5 vs2022` in `server/` with
`SERVER_PRO3_SUPPORT=1 LOG_IN_CHAT=1 USE_SIMD=best`, then MSBuild on `build\YGOPro.sln`, target
`ygoserver`, Release x64.
- `-Version <tag>` is what `ygol_engine_version()` returns (`local` when not given).
- Before building, the script checks `contract/ocgapi.h` against `core/ocgapi.h` + the server's
`YGOSERVER_API` lines in `gframe/serverapi.h`; a drift fails the build.
- Output: `out/win64/ygol-engine.dll` (+ `.pdb`) and `out/win64/engine.json`
(`{ version, exports, protocol, capabilities, binding }`; protocol and capabilities are read from
`gframe/network.h` and the `cap.flags` line of `single_duel.cpp` / `tag_duel.cpp`, never typed in;
`binding` is the version of the C# binding released with this engine, the same string as `version`).
### The export gate is the truth
The script FAILS unless the DLL exports exactly **336** symbols: `start_server`, `stop_server`,
`ygol_engine_version`, and the core's whole C API re-exported, including the six `ygol_sb_*` Sandbox
exports and `ygol_preview_attack` (47 contract exports, `ygol_query_tag_cards` and `ygol_query_prompt_context` included, + 289 `sqlite3_*`; see the contract). A different count means the wrong tree, not "a small difference". For the
record: the Unity repo's old `ygoserver.dll` exported 327 because its build applied patch 0008
(the `ygol_sb_*` six) only to `ocgcore.dll`, never to the server's own core copy; with one core they
are in the one library.
The wire probe (`python contract/probe-feature-messages.py --dll out/win64/ygol-engine.dll`, run from
the Unity repo root because the library reads `./expansions`) checks the fork's messages and
capability bits against a running server.
## Build (Linux and Android)
Both run in Docker from `build/Dockerfile`; the host needs Docker and nothing else (not this
Windows VM: it has no nested virtualisation). From a clean clone on any Linux box:
```bash
./build/linux.sh v0.0.1 # out/linux-x64/libygol-engine.so, ygoserver, engine.json
./build/android.sh v0.0.1 # out/android-arm64/libygol-engine.so, engine.json
```
- The argument is the version `ygol_engine_version()` returns (`local` when not given).
- Base image: Ubuntu 22.04 pinned by digest. Its glibc (2.35) is the floor of the outputs: they
run on Ubuntu 22.04, Debian bookworm and anything newer, not on Ubuntu 20.04 (a bookworm build
would need 2.36; bullseye, 2.31, is end of life and its package pool is gone); premake
5.0.0-beta8 bootstrapped from its tag; every dependency built from the vendored
sources (`BUILD_ALL`), libevent configured by its own generated `configure`
(`.ci/configure-libevent.sh`; `server/event/build-aux/` is the tarball's, put back for it).
`obj/`, `bin/`, `build/` are wiped before every compile.
- Linux builds the tree twice: the library (`SERVER_PRO3_SUPPORT`, `libygol-engine.so`) and the
plain server executable (project `ygopro`, with the zip support and tag surrender confirm PRO3
turns on, so it carries the same rules), renamed `ygoserver`; it is what the server repo's
`duel-server` runs. `-static-libstdc++ -static-libgcc`, AVX2 like the Windows build.
- Android: NDK r23b (`23.1.7779620`, the build id in the shipped `.so`, pinned by sha1), API
level 26, arm64 only, upstream's `exec_android_pro3` job. One library; `libocgcore.so` is not
built any more.
- The same contract-header check runs first, and the same export gate runs last, with the plain C
export count (`nm --dynamic --defined-only --extern-only`, names without a leading underscore; C++
symbols are `_Z`-mangled) and every name in `contract/ocgapi.h` present. Expected: **Linux 857**,
**Android 851** (the shipped Android server had 842: 841 + `ygol_preview_attack`; one core adds
the six `ygol_sb_*` and `ygol_engine_version`; `lua_*`, `event_*`, `z*`, ... count on these
platforms, unlike on Windows, so the numbers differ from 336; Linux has one more than Android,
`evutil_secure_rng_add_bytes`, which libevent only builds where glibc has no `arc4random`, so the
number belongs to the pinned base image). `engine.json` is written the same way as on Windows. A
`docker build` that fails the gate leaves nothing in `out/`.
## Binding
`binding/YGOLegacy.Engine/` is the managed side of the engine: a `netstandard2.1` class library
(`YGOLegacy.Engine.dll`, LangVersion 9, unsafe allowed, no package and no Unity reference) that
holds the wire protocol under `Protocol/` in the namespaces `YGOLegacy.Protocol.{Contracts,
Codecs, Framing, Routing}`: the message ids and payload records, the CTOS / STOC codecs, the
frame codec and the STOC router, and the namespace `YGOLegacy.Engine`: `EngineApi` (one typed
extern per export of `contract/ocgapi.h`, plus the three callback delegates), `MessageScan` (the
engine message-layout scanner), `GameMessage` (one enum from the `MSG_*` defines) and
`EngineBinding` (`Version` and `CheckNative()`, which throws when the native library's
`ygol_engine_version()` is not the binding's version). Unity, the tools and the server repo's
card-tester consume this DLL instead of their own copies of the sources and call `CheckNative()`
once at start. `binding/YGOLegacy.Engine.Tests/` (`net8.0`, NUnit) holds the fixtures, including the
two parity tests that parse the headers; nothing in the tests touches the native library. Contract
section (e) documents the DLL.
```powershell
dotnet build binding\YGOLegacy.Engine -c Release -p:EngineVersion=v0.0.1
dotnet test binding\YGOLegacy.Engine.Tests -c Release
(Get-Item binding\YGOLegacy.Engine\bin\Release\netstandard2.1\YGOLegacy.Engine.dll).VersionInfo.ProductVersion
```
`EngineVersion` is stamped into the assembly's informational version (`local` when not given), so
`ProductVersion` prints the tag the DLL was built for. The release workflow's `windows` job builds
and tests the binding after the native gates and ships `ygol-engine-binding.zip`
(`YGOLegacy.Engine.dll`, `YGOLegacy.Engine.pdb`); `engine.json`'s `binding` field names the same tag.
The binding is built on Windows only (the Linux and Android jobs run in Docker without dotnet); it
needs the dotnet 8 SDK, and the test project restores NUnit from nuget.org.
## Release
`.gitea/workflows/release.yml`. Every push and pull request runs the three builds and their gates
(Windows on the self-hosted `unity-windows` runner, Linux and Android in Docker on `ubuntu-latest`);
nothing is published. A tag releases:
```bash
# CHANGELOG.md: rename "## Unreleased" to "## vX.Y.Z — <date>" (the section is the release body), commit
git tag vX.Y.Z && git push gitea main vX.Y.Z
```
Then wait for the run (`Actions` tab). The release named after the tag appears only when every
build passed its gate, the binding built and its tests passed, every platform's `engine.json` names
the tag (as `version` and `binding`) and agrees on protocol and capabilities, and
`ygol_engine_version()` on the Windows DLL returns the tag. A tag whose release
already exists is refused: a bad release gets a new patch tag, never a re-upload.
The Windows runner needs premake 5.0.0-beta8 at `C:\premake\premake5.exe`, VS 2022 Build Tools with
the C++ workload, Python 3, the dotnet 8 SDK (the binding) and `tar.exe` (Windows 10+); its checkout is
`C:\gitea-runner\checkout\yugioh-legacy-engine` (no spaces) and the Windows bundle is staged under
`C:\gitea-runner\release-out\ygol-engine\<tag>\` for the last job to attach. The release token is
the run's own `GITHUB_TOKEN`.
### Fetching a release
`https://gitea.dev.blueoceanswim.com/donny/yugioh-legacy-engine/releases/download/<tag>/<asset>`:
| Asset | Contents |
|---|---|
| `ygol-engine-win64.zip` | `ygol-engine.dll`, `ygol-engine.pdb`, `engine.json` |
| `ygol-engine-binding.zip` | `YGOLegacy.Engine.dll`, `YGOLegacy.Engine.pdb` (the C# binding; its `ProductVersion` is the tag) |
| `ygol-engine-linux-x64.tar.gz` | `libygol-engine.so`, `engine.json` |
| `ygoserver-linux-x64.tar.gz` | `ygoserver` (the plain server executable), `engine.json` |
| `ygol-engine-android-arm64.tar.gz` | `libygol-engine.so`, `engine.json` |
| `engine.json` | `{ version, exports, protocol, capabilities, binding }`; a copy of the Linux bundle's. `version`, `protocol`, `capabilities` and `binding` are identical in every bundle (the release job checks); `exports` is each platform's own count (336 / 857 / 851) |
## Line endings
The repo stores LF (`core.autocrlf=false` when importing; `git archive` on a machine with
`autocrlf=true` converts, so pass `-c core.autocrlf=false`). Irrlicht is CRLF because its upstream
stores CRLF.