build(ffi): game.h and host-sys ffi.rs generated (cbindgen, ffi-gen), checked in CI #2896

Closed
HumanBean wants to merge 2 commits from cleanup/area8-generated-decls into mod-api
Owner

Summary

A call across the line between the game side's Rust and the host's C was declared by hand twice: in game/game.h (159
exports of longshot-game) and in host-sys's ffi.rs (182 host functions and globals), against host/game_host.h. If a
signature differed between the two languages, it still compiled and was undefined behaviour. Each side is now generated
from the other by a new dev tool, game/tools/ffi-gen (mod-api cleanup survey, area 8; the user's decision Q4):

  • game/game.h comes from cbindgen run over longshot-game's source. That is pure Rust, with no libclang. It
    holds:

    • every exported function, with its Rust doc;
    • the #[repr(C)] records those functions name;
    • the crate's pub const GAME_*.

    The values the C named in enums are now GAME_* constants beside the code that uses them (menu.rs, post.rs,
    programs.rs, world.rs, builder_link/, and others). Values owned by another crate are checked against their
    owners at compile time or in a test (pad routes, prompt binds, the texture packs' side, the curve's operations). The
    lifted code's entry points (tables.rs, and any export that takes Ctx) are left out, since no C calls them.

  • game/crates/host-sys/src/ffi.rs is read from game_host.h, below its new /* ==== ffi-gen: line. Every host
    function and global the game's Rust calls is declared there once, as C, with the doc that ffi.rs used to carry.
    ffi-gen has its own small C reader, which refuses anything it does not know. The hand-laid C records move to
    host-sys/src/ffi_types.rs, and ffi.rs re-exports them.

  • Both files are checked in, so neither a player's build nor the game build runs the tool. They are compared with
    ffi-gen --check in CI's rust job and in mise run rust, next to schema-gen --check, which nothing ran before.
    ffi-gen's own test compares them too.

The generator forced a few changes at the line:

  • game_live_action, a Rust global the host stored into, is replaced by the function game_live_cheat(action).
  • game_mod_tool and game_start_args take main's char ** as it is.
  • GameTextureReplacement.mod is now mod_id (vk_texture.cpp).
  • hos_main_* and hos_ab_side_now are read as atomics.
  • GAME_* enum constants are now #defines, and the unused type names GamePassKind and GamePassPoint are gone.
  • Three host declarations that nothing defined or used are deleted: hos_camera_scale, hos_oc_wait,
    hos_oc_last_kind.

The tool is described in a new page, docs/dev/reference/ffi.md ("The C–Rust line"), with updates to
game/README.md, game/DESIGN.md, architecture.md and ci.md.

Read first: game/tools/ffi-gen/src/host_rs.rs, game/tools/ffi-gen/src/game_h.rs, the restructured
host/game_host.h.

Testing

Tested: Linux, RTX 3090, on this PC's job queue (build mode):

  • Workspace cargo clippy -D warnings and cargo test, with modtool run separately, as CI runs them: pass.
  • cargo fmt --check, schema-gen --check and ffi-gen --check: pass.
  • tests/run_all.py: pass. The first run's 5 new failures were header text and fixture conflicts (a Ctx typedef in
    game.h, an array parameter in a fixture, the switch table reading the generator's include guard). All are fixed and
    each re-ran green.
  • Windows renderer with llvm-mingw: pass. Windows host C with llvm-mingw: all units but host/nn_video.c, which fails the same way on mod-api@origin (this PC's tool store has no FFmpeg headers for Windows); unchanged here.
  • Game build (dev.py pack + rt): built. Smoke run (shared queue, headless): kokiri-start into play, 1437 frames, Insert opened the menu (system/menu, Graphics).

Against the old hand-written headers, checked with clang:

  • Every old game.h prototype was compiled against the new header. The only conflicts are the intended argv type
    changes.
  • Every record's layout (-fdump-record-layouts) is identical.
  • Every old GAME_* value is identical (static asserts).
  • game.h compiles alone in C11 and C++20.
  • game_host.h compiles alone.

Still to test:

  • Windows, any GPU: start the game and open the Insert menu and the Mods tab. Expect them to work as on mod-api. This
    checks the Windows-only #[cfg(windows)] bindings and game_builder_file (Test Farm: "@Beanstalk start the game on
    Windows, open Insert and the Mods tab").

Measurements

Nothing to measure: ffi-gen is a dev tool the game build never runs (its output is checked in, so game build time is unchanged), and the generated declarations keep the ABI identical (record layouts and GAME_* values compared with clang above). No switch is added or changed: the HOS_CHAIN, HOS_LOG_*, HOS_NET_*, HOS_MOD_TESTS, HOS_MOUSE_NATIVE, HOS_SHADOW_RES, HOS_WIDE_MAX_PX and LONGSHOT_SET names the check sees are existing names in moved game_host.h lines and generated doc comments, and LONGSHOT_GAME_GAME_H is game.h's include guard.

Risks

  • A wrong generated declaration: the clang comparisons above cover game.h. For ffi.rs, the C reader maps types
    the way the old hand-written file did; the diff of the old and new signatures shows only parameter names and the
    intended atomics and *mut u32 changes.
  • GAME_HOOK_ANY and GAME_CURVE_INPUTS lost their u suffix: cbindgen writes decimals. They are now a signed
    long/long long; every current use is still correct. A future use in %u or ~ arithmetic would need a cast.
  • Work in flight must regenerate rather than edit by hand: the builder-is-the-game stack's new game.h exports
    appear by themselves from its Rust; area 1's deletion of game/options.h must drop options.h from game_h.rs's
    includes; area 2's game_host.h edits go below or above the ffi-gen line.
  • Reverting: a revert is enough. Nothing changes for players (saves, longshot_options.ini and the caches are
    untouched).

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit. (onto mod-api)
  • Tested on my own PC, in the game for a change the game runs, as Testing says.
  • mise run test passes (any failure that also fails on main is named).
  • Measured against main, or Measurements says why there is nothing to measure.
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.
    (cbindgen is MPL-2.0, used only by a dev tool)

When it applies

  • A check under tests/ that reads changed source is updated, not weakened.
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. (Windows host: every unit compiles at this head with the pinned FFmpeg 9.0.2 headers from tools.lock, queue job ffm-hostwin2)
  • Docs updated.
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

🤖 Generated with Claude Code

https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc

## Summary A call across the line between the game side's Rust and the host's C was declared by hand twice: in `game/game.h` (159 exports of `longshot-game`) and in host-sys's `ffi.rs` (182 host functions and globals), against `host/game_host.h`. If a signature differed between the two languages, it still compiled and was undefined behaviour. Each side is now generated from the other by a new dev tool, `game/tools/ffi-gen` (mod-api cleanup survey, area 8; the user's decision Q4): - **`game/game.h`** comes from cbindgen run over `longshot-game`'s source. That is pure Rust, with no libclang. It holds: - every exported function, with its Rust doc; - the `#[repr(C)]` records those functions name; - the crate's `pub const GAME_*`. The values the C named in `enum`s are now `GAME_*` constants beside the code that uses them (`menu.rs`, `post.rs`, `programs.rs`, `world.rs`, `builder_link/`, and others). Values owned by another crate are checked against their owners at compile time or in a test (pad routes, prompt binds, the texture packs' side, the curve's operations). The lifted code's entry points (`tables.rs`, and any export that takes `Ctx`) are left out, since no C calls them. - **`game/crates/host-sys/src/ffi.rs`** is read from `game_host.h`, below its new `/* ==== ffi-gen:` line. Every host function and global the game's Rust calls is declared there once, as C, with the doc that `ffi.rs` used to carry. `ffi-gen` has its own small C reader, which refuses anything it does not know. The hand-laid C records move to `host-sys/src/ffi_types.rs`, and `ffi.rs` re-exports them. - **Both files are checked in**, so neither a player's build nor the game build runs the tool. They are compared with `ffi-gen --check` in CI's `rust` job and in `mise run rust`, next to `schema-gen --check`, which nothing ran before. `ffi-gen`'s own test compares them too. The generator forced a few changes at the line: - `game_live_action`, a Rust global the host stored into, is replaced by the function `game_live_cheat(action)`. - `game_mod_tool` and `game_start_args` take `main`'s `char **` as it is. - `GameTextureReplacement.mod` is now `mod_id` (`vk_texture.cpp`). - `hos_main_*` and `hos_ab_side_now` are read as atomics. - `GAME_*` enum constants are now `#define`s, and the unused type names `GamePassKind` and `GamePassPoint` are gone. - Three host declarations that nothing defined or used are deleted: `hos_camera_scale`, `hos_oc_wait`, `hos_oc_last_kind`. The tool is described in a new page, `docs/dev/reference/ffi.md` ("The C–Rust line"), with updates to `game/README.md`, `game/DESIGN.md`, `architecture.md` and `ci.md`. Read first: `game/tools/ffi-gen/src/host_rs.rs`, `game/tools/ffi-gen/src/game_h.rs`, the restructured `host/game_host.h`. ## Testing **Tested:** Linux, RTX 3090, on this PC's job queue (build mode): - Workspace `cargo clippy -D warnings` and `cargo test`, with modtool run separately, as CI runs them: pass. - `cargo fmt --check`, `schema-gen --check` and `ffi-gen --check`: pass. - `tests/run_all.py`: pass. The first run's 5 new failures were header text and fixture conflicts (a `Ctx` typedef in game.h, an array parameter in a fixture, the switch table reading the generator's include guard). All are fixed and each re-ran green. - Windows renderer with llvm-mingw: pass. Windows host C with llvm-mingw: all units but `host/nn_video.c`, which fails the same way on mod-api@origin (this PC's tool store has no FFmpeg headers for Windows); unchanged here. - Game build (`dev.py pack` + `rt`): built. Smoke run (shared queue, headless): kokiri-start into play, 1437 frames, Insert opened the menu (system/menu, Graphics). Against the old hand-written headers, checked with clang: - Every old game.h prototype was compiled against the new header. The only conflicts are the intended `argv` type changes. - Every record's layout (`-fdump-record-layouts`) is identical. - Every old `GAME_*` value is identical (static asserts). - game.h compiles alone in C11 and C++20. - `game_host.h` compiles alone. **Still to test:** - Windows, any GPU: start the game and open the Insert menu and the Mods tab. Expect them to work as on mod-api. This checks the Windows-only `#[cfg(windows)]` bindings and `game_builder_file` (Test Farm: "@Beanstalk start the game on Windows, open Insert and the Mods tab"). ## Measurements Nothing to measure: `ffi-gen` is a dev tool the game build never runs (its output is checked in, so game build time is unchanged), and the generated declarations keep the ABI identical (record layouts and `GAME_*` values compared with clang above). No switch is added or changed: the `HOS_CHAIN`, `HOS_LOG_*`, `HOS_NET_*`, `HOS_MOD_TESTS`, `HOS_MOUSE_NATIVE`, `HOS_SHADOW_RES`, `HOS_WIDE_MAX_PX` and `LONGSHOT_SET` names the check sees are existing names in moved `game_host.h` lines and generated doc comments, and `LONGSHOT_GAME_GAME_H` is game.h's include guard. ## Risks - **A wrong generated declaration:** the clang comparisons above cover game.h. For `ffi.rs`, the C reader maps types the way the old hand-written file did; the diff of the old and new signatures shows only parameter names and the intended atomics and `*mut u32` changes. - **`GAME_HOOK_ANY` and `GAME_CURVE_INPUTS` lost their `u` suffix:** cbindgen writes decimals. They are now a signed `long`/`long long`; every current use is still correct. A future use in `%u` or `~` arithmetic would need a cast. - **Work in flight must regenerate rather than edit by hand:** the builder-is-the-game stack's new game.h exports appear by themselves from its Rust; area 1's deletion of `game/options.h` must drop `options.h` from `game_h.rs`'s includes; area 2's `game_host.h` edits go below or above the ffi-gen line. - **Reverting:** a revert is enough. Nothing changes for players (saves, `longshot_options.ini` and the caches are untouched). ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. (onto `mod-api`) - [x] Tested on my own PC, in the game for a change the game runs, as Testing says. - [x] `mise run test` passes (any failure that also fails on `main` is named). - [x] Measured against `main`, or Measurements says why there is nothing to measure. - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. (cbindgen is MPL-2.0, used only by a dev tool) ### When it applies - [x] A check under `tests/` that reads changed source is updated, not weakened. - [x] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. (Windows host: every unit compiles at this head with the pinned FFmpeg 9.0.2 headers from tools.lock, queue job ffm-hostwin2) - [x] Docs updated. - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_016o1k1SQVyAWsujjFVd8Wuc
A call across the C–Rust line was declared by hand on both sides, and a signature that differed between the two
compiled and was undefined behaviour. Each side is now made from the other, by a dev tool, game/tools/ffi-gen:

- game/game.h: cbindgen over longshot-game's source (pure Rust, no libclang): every no_mangle export, the repr(C)
  records they name and the crate's pub const GAME_*, each with its Rust doc. The values the C named in enums are
  GAME_* constants beside the code that uses them (menu, post, programs, world, builder_link, ...), checked against
  their owners' (pad routes, prompt binds, the texture packs' side, the curve's operations). The lifted code's entry
  points (tables.rs, and any export taking Ctx) are left out: no C calls them.
- game/crates/host-sys/src/ffi.rs: read from game_host.h below its "ffi-gen" line, where every host function and
  global the game's Rust calls is declared once with the doc ffi.rs had; the records they take move to ffi_types.rs.
- Both checked in (no player's or game build runs the tool) and compared in CI's rust job and mise's rust task
  (ffi-gen --check, with schema-gen --check, which nothing ran), and by ffi-gen's own test.

Changes the line needed: game_live_action, a global the host stored into, is game_live_cheat(action); game_mod_tool
and game_start_args take main's char ** as is; GameTextureReplacement's mod is mod_id (vk_texture.cpp); hos_main_*
and hos_ab_side_now are read as the atomics they are; three declarations nothing used (hos_camera_scale, hos_oc_wait,
hos_oc_last_kind) are gone.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
docs(ffi): the C–Rust line: game.h and ffi.rs, each made from the other side, and ffi-gen
Some checks failed
PR description / Title and description (pull_request_target) Successful in 8s
beanstalk/approval can't be tested as asked: see the farm's comment
d433f6d3f6
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Member

Possibly related, for context:

  • PR #2822 feat(game)!: the game side and the whole mod API in Rust, with system mods and the sandbox on by default — Underlying Rust game and host FFI this PR generates bindings for
Possibly related, for context: - PR #2822 feat(game)!: the game side and the whole mod API in Rust, with system mods and the sandbox on by default — Underlying Rust game and host FFI this PR generates bindings for
Author
Owner

Windows host compile (llvm-mingw, with tools.lock's FFmpeg 9.0.2 headers): every host unit compiles at this head; box ticked.

Windows host compile (llvm-mingw, with tools.lock's FFmpeg 9.0.2 headers): every host unit compiles at this head; box ticked.
Author
Owner
@review
BeanBot left a comment

Recommendation: Request changes. The generated boundary has passed a Linux smoke run and a Windows host compile, but its Windows game path has not been exercised. The review is also incomplete: not every changed code file and its callers were read in full. (It stands on L2: The check PR description / Title and description (pull_request_target) fails on this head and passes on the base branch: Failing after 13s.)

The change generates the C and Rust declarations at the game–host boundary and checks the generated files in CI. This reduces manual signature drift at the cost of maintaining a generator and its supported C syntax.

Blocking (1): must change before merging.

  1. L2: The check PR description / Title and description (pull_request_target) fails on this head and passes on the base branch: Failing after 13s.

Testing before merging (1) (runs on the systems it can change; it doesn't hold the recommendation)

  • L1: The Windows runtime path is still untested after changing its generated bindings. When: A Windows player starts the game and opens the Mods tab, which reaches Windows-only bindings and the builder file interface; compilation alone does not exercise those calls. Do: Run the stated Windows smoke test: start the game, open Insert and the Mods tab, and report the result.

@fix can do L2.

More: does it help, other systems, switches, what was read

Does it help: Unproven as a complete cross-platform change. The author reports a 1,437-frame Linux smoke run and successful declaration and layout comparisons; no Windows in-game result is reported. This is not a performance change.

Other systems: The declarations reach host and renderer calls throughout the game on Windows and Linux, including Windows-only builder file bindings. The reported Linux smoke run and Windows compilation cover part of that surface; Windows runtime behavior remains untested.

Switches: No switches added, changed, or removed.

Reviewed: d433f6d3f6 against 12f42db5c1: Read the repository instructions, contribution rules, standards, PR template, commit log, generator sources, host-sys type definitions, and selected changed diffs. Skimmed portions of the generated declarations, host header, CI, tests, and longshot-game changes. The output for several large reads was truncated; remaining changed files and their surrounding callers were not read in full. The adjacent Tooling checkout and docs/review-checklist.md were unavailable.; nothing run (a code review).

Checked and fine:

  • Both commits record AI assistance in their trailers.
  • The reported Windows host compile used the pinned FFmpeg headers.
  • The CI description check failure is already reported in the supplied CI results.

Testing: No commands from the project, builds, or tests were run during this read-only review.

Overlaps and merge order: Not checked; network access is closed.


Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The generated boundary has passed a Linux smoke run and a Windows host compile, but its Windows game path has not been exercised. The review is also incomplete: not every changed code file and its callers were read in full. (It stands on L2: The check `PR description / Title and description (pull_request_target)` fails on this head and passes on the base branch: Failing after 13s.) The change generates the C and Rust declarations at the game–host boundary and checks the generated files in CI. This reduces manual signature drift at the cost of maintaining a generator and its supported C syntax. **Blocking (1)**: must change before merging. 1. **L2**: The check `PR description / Title and description (pull_request_target)` fails on this head and passes on the base branch: Failing after 13s. **Testing before merging (1)** (runs on the systems it can change; it doesn't hold the recommendation) - **L1**: The Windows runtime path is still untested after changing its generated bindings. *When:* A Windows player starts the game and opens the Mods tab, which reaches Windows-only bindings and the builder file interface; compilation alone does not exercise those calls. **Do:** Run the stated Windows smoke test: start the game, open Insert and the Mods tab, and report the result. `@fix` can do L2. <details> <summary>More: does it help, other systems, switches, what was read</summary> **Does it help:** Unproven as a complete cross-platform change. The author reports a 1,437-frame Linux smoke run and successful declaration and layout comparisons; no Windows in-game result is reported. This is not a performance change. **Other systems:** The declarations reach host and renderer calls throughout the game on Windows and Linux, including Windows-only builder file bindings. The reported Linux smoke run and Windows compilation cover part of that surface; Windows runtime behavior remains untested. **Switches:** No switches added, changed, or removed. **Reviewed:** d433f6d3f67b against 12f42db5c145: Read the repository instructions, contribution rules, standards, PR template, commit log, generator sources, host-sys type definitions, and selected changed diffs. Skimmed portions of the generated declarations, host header, CI, tests, and longshot-game changes. The output for several large reads was truncated; remaining changed files and their surrounding callers were not read in full. The adjacent Tooling checkout and docs/review-checklist.md were unavailable.; nothing run (a code review). **Checked and fine:** - Both commits record AI assistance in their trailers. - The reported Windows host compile used the pinned FFmpeg headers. - The CI description check failure is already reported in the supplied CI results. **Testing:** No commands from the project, builds, or tests were run during this read-only review. **Overlaps and merge order:** Not checked; network access is closed. </details> --- <sub>Automated code review by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.</sub>
Author
Owner

L2: the description's Measurements is now the one line the check wants, and it says the HOS_*/LONGSHOT_SET names it flagged are moved game_host.h lines and doc comments, not new switches (LONGSHOT_GAME_GAME_H is the include guard). No code change.
L1: agreed, Windows runtime is still untested here (no Windows PC); it stays under Still to test for the Test Farm: @Beanstalk start the game on Windows, open Insert and the Mods tab (a BigBean approves it).

L2: the description's Measurements is now the one line the check wants, and it says the HOS_*/LONGSHOT_SET names it flagged are moved `game_host.h` lines and doc comments, not new switches (LONGSHOT_GAME_GAME_H is the include guard). No code change. L1: agreed, Windows runtime is still untested here (no Windows PC); it stays under Still to test for the Test Farm: `@Beanstalk start the game on Windows, open Insert and the Mods tab` (a BigBean approves it).
Member

Beanstalk test farm

Commit 854713e · approved by nobody yet · dashboard

A member of BigBeans can review this commit and comment @Beanstalk to test it.

<!-- beanstalk:sticky --> ### Beanstalk test farm Commit `854713e` · approved by nobody yet · [dashboard](https://beanstalk.magicbean.dev/prs/2896) A member of `BigBeans` can review this commit and comment `@Beanstalk` to test it.
HumanBean force-pushed cleanup/area8-generated-decls from d433f6d3f6
Some checks failed
PR description / Title and description (pull_request_target) Successful in 8s
beanstalk/approval can't be tested as asked: see the farm's comment
to 854713ee67
Some checks are pending
PR description / Title and description (pull_request_target) Successful in 6s
beanstalk/approval not approved yet
2026-11-30 17:26:21 +00:00
Compare
Author
Owner

Rebased onto mod-api 75bcd270 (#2881): was d433f6d3, now 854713ee. game_host.h keeps #2881's hos_game_language_id in the ffi-gen section; game.h regenerated (picks up hos_mod_language). schema-gen --check, ffi-gen --check, fmt, clippy and the Linux host compile pass; run_all passes apart from two load flakes in code this PR doesn't touch (build-core's control socket test, modtool's TS system-mod tests), each passing on its own.

Rebased onto mod-api 75bcd270 (#2881): was d433f6d3, now 854713ee. game_host.h keeps #2881's hos_game_language_id in the ffi-gen section; game.h regenerated (picks up hos_mod_language). schema-gen --check, ffi-gen --check, fmt, clippy and the Linux host compile pass; run_all passes apart from two load flakes in code this PR doesn't touch (build-core's control socket test, modtool's TS system-mod tests), each passing on its own.
Author
Owner

Landed on mod-api as 8f00b801 (one commit), after its review fixes and one batched build + game run of all six cleanup areas together. mod-api work now goes straight onto the branch once self-reviewed.

Landed on mod-api as 8f00b801 (one commit), after its review fixes and one batched build + game run of all six cleanup areas together. mod-api work now goes straight onto the branch once self-reviewed.
HumanBean closed this pull request 2026-11-30 22:34:18 +00:00
Some checks are pending
PR description / Title and description (pull_request_target) Successful in 6s
beanstalk/approval not approved yet

Pull request closed

Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!2896
No description provided.