[PR #7073] [MERGED] Mod API: a mod can implement the key hook #7315

Closed
opened 2026-11-03 06:37:36 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/7073
Author: @arismoko
Created: 11/2/2026
Status: ✅ Merged
Merged: 11/2/2026
Merged by: @ludovic111

Base: main ← Head: v2/ses_01M6BECNQ499TYMMBQBDF00VJ5-mod-api-key


📝 Commits (1)

  • 9d7ea67 Mod API: a mod can implement the key hook

📊 Changes

3 files changed (+26 additions, -1 deletions)

View changed files

📝 crates/horizon-client/Cargo.toml (+4 -0)
📝 crates/horizon-client/src/mod_api.rs (+2 -1)
➕ crates/horizon-client/tests/mod_api_hooks.rs (+20 -0)

📄 Description

🤖 Opened by Aria's agent swarm, Dyna, not by Aria.

A mod crate couldn't implement Hooks::key. #7044's in-game test found this: its mod wouldn't build without a local patch.

The bug

#6216 added pub use winit::event::KeyEvent; to mod_api.rs. An explicit pub use beats the glob pub use generated::*, so outside the crate mod_api::KeyEvent meant winit's key event, not the generated KeyEvent<'_> that fn key(&self, host, event: &mut KeyEvent<'_>) takes.

Inside the crate nothing noticed, because generated.rs sees its own KeyEvent. A mod sees winit's:

error[E0107]: struct takes 0 lifetime arguments but 1 lifetime argument was supplied
error[E0053]: method `key` has an incompatible type for trait
error[E0609]: no field `key` on type `&mut KeyEvent`
error[E0609]: no field `pressed` on type `&mut KeyEvent`

The fix

The re-export is gone. Nothing in the repository used mod_api::KeyEvent as winit's type: generated.rs already writes winit::event::KeyEvent for InputKeyEvent::event.

In its place mod_api re-exports winit itself, as it already does serde_json. A mod that handles input_key can then name the event as mod_api::winit::event::KeyEvent, and match its keys with mod_api::winit::keyboard::{KeyCode, PhysicalKey}, without a winit dependency of its own. A renamed re-export would have kept only the event type, not the key types.

Test

crates/horizon-client/tests/mod_api_hooks.rs builds a mod-side impl Hooks from outside the crate, as a mod does:

  • key reads event.key and event.pressed;
  • input_key names winit::event::KeyEvent through mod_api.

It's registered in crates/horizon-client/Cargo.toml (the crate sets autotests = false).

  • On main it fails to build with the errors above, plus unresolved import horizon_client::mod_api::winit.
  • At this head it builds and passes.

Tested in game

The test route is horizon-machine-sample, used through its trait example (examples/trait-api, the driver that links a Hooks mod). The game was built at this head with no local patch, on the server thread (single player's default).

The sample's mod (commit 25d5b06) got a throwaway key hook:

fn key(&self, _host: &mut dyn Host, event: &mut KeyEvent<'_>) {
    eprintln!("KEY_PROOF key={} pressed={}", event.key, event.pressed);
}

It built, and tapping Q, Space and 3 through horizon-ctl key … tap logged:

KEY_PROOF key=key.keyboard.q pressed=true
KEY_PROOF key=key.keyboard.q pressed=false
KEY_PROOF key=key.keyboard.space pressed=true
KEY_PROOF key=key.keyboard.space pressed=false
KEY_PROOF key=key.keyboard.3 pressed=true
KEY_PROOF key=key.keyboard.3 pressed=false

Checks

All at head 9d7ea67f81:

  • cargo test -p horizon-client --test mod_api_hooks: 1 passed. On main the file doesn't build (the errors above).
  • pr_ready, below: rustfmt, clippy, 2073 horizon-client and horizon-client-lib tests, and the mod API up to date.

pr_ready

pr_ready (tools/pr_ready.py --quick --base d928d1cd6161e95f5704d05e14193f5714730d9f on f2196cc12001, against d928d1cd61 at d928d1cd6, 11 min 56 s)

Check Result
Scope ✅ 3 files, +26 −1
Cargo.lock ✅ unchanged
Public tree ✅
Python tests ✅ 611 passed (32 s)
rustfmt ✅ (7 s)
clippy ✅ (1 min 19 s)
tests: horizon-client, horizon-client-lib ✅ 2073 passed (9 min 42 s)
doc tests ✅ 0 passed (4 s)
content root tests ➖ content root untouched
mod API up to date ✅ (11 s)
tokens.css up to date ➖ theme untouched
launcher rustfmt ➖ launcher untouched
launcher clippy ➖ launcher untouched
launcher tests ➖ launcher untouched
Parity ➖ not run (--quick): CI's parity job picks the affected harnesses
Label player-facing: feature if players get something new, no label for a fix (release-now if urgent)

Merge with a merge commit, please.

🤖 Generated with Dyna.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/7073 **Author:** [@arismoko](https://github.com/arismoko) **Created:** 11/2/2026 **Status:** ✅ Merged **Merged:** 11/2/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `v2/ses_01M6BECNQ499TYMMBQBDF00VJ5-mod-api-key` --- ### 📝 Commits (1) - [`9d7ea67`](https://github.com/horizon-rust/horizon/commit/9d7ea67f81d208ccdd1e5881a1e871a68c23dc2f) Mod API: a mod can implement the key hook ### 📊 Changes **3 files changed** (+26 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/Cargo.toml` (+4 -0) 📝 `crates/horizon-client/src/mod_api.rs` (+2 -1) ➕ `crates/horizon-client/tests/mod_api_hooks.rs` (+20 -0) </details> ### 📄 Description > 🤖 Opened by Aria's agent swarm, Dyna, not by Aria. A mod crate couldn't implement `Hooks::key`. #7044's in-game test found this: its mod wouldn't build without a local patch. ## The bug #6216 added `pub use winit::event::KeyEvent;` to `mod_api.rs`. An explicit `pub use` beats the glob `pub use generated::*`, so outside the crate `mod_api::KeyEvent` meant winit's key event, not the generated `KeyEvent<'_>` that `fn key(&self, host, event: &mut KeyEvent<'_>)` takes. Inside the crate nothing noticed, because `generated.rs` sees its own `KeyEvent`. A mod sees winit's: ``` error[E0107]: struct takes 0 lifetime arguments but 1 lifetime argument was supplied error[E0053]: method `key` has an incompatible type for trait error[E0609]: no field `key` on type `&mut KeyEvent` error[E0609]: no field `pressed` on type `&mut KeyEvent` ``` ## The fix The re-export is gone. Nothing in the repository used `mod_api::KeyEvent` as winit's type: `generated.rs` already writes `winit::event::KeyEvent` for `InputKeyEvent::event`. In its place `mod_api` re-exports `winit` itself, as it already does `serde_json`. A mod that handles `input_key` can then name the event as `mod_api::winit::event::KeyEvent`, and match its keys with `mod_api::winit::keyboard::{KeyCode, PhysicalKey}`, without a winit dependency of its own. A renamed re-export would have kept only the event type, not the key types. ## Test `crates/horizon-client/tests/mod_api_hooks.rs` builds a mod-side `impl Hooks` from outside the crate, as a mod does: - `key` reads `event.key` and `event.pressed`; - `input_key` names `winit::event::KeyEvent` through `mod_api`. It's registered in `crates/horizon-client/Cargo.toml` (the crate sets `autotests = false`). - **On main** it fails to build with the errors above, plus `unresolved import horizon_client::mod_api::winit`. - **At this head** it builds and passes. ## Tested in game The test route is [horizon-machine-sample](https://github.com/arismoko/horizon-machine-sample), used through its trait example (`examples/trait-api`, the driver that links a `Hooks` mod). The game was built at this head with no local patch, on the server thread (single player's default). The sample's mod (commit 25d5b06) got a throwaway `key` hook: ```rust fn key(&self, _host: &mut dyn Host, event: &mut KeyEvent<'_>) { eprintln!("KEY_PROOF key={} pressed={}", event.key, event.pressed); } ``` It built, and tapping Q, Space and 3 through `horizon-ctl key … tap` logged: ``` KEY_PROOF key=key.keyboard.q pressed=true KEY_PROOF key=key.keyboard.q pressed=false KEY_PROOF key=key.keyboard.space pressed=true KEY_PROOF key=key.keyboard.space pressed=false KEY_PROOF key=key.keyboard.3 pressed=true KEY_PROOF key=key.keyboard.3 pressed=false ``` ## Checks All at head 9d7ea67f81: - `cargo test -p horizon-client --test mod_api_hooks`: 1 passed. On main the file doesn't build (the errors above). - pr_ready, below: rustfmt, clippy, 2073 horizon-client and horizon-client-lib tests, and the mod API up to date. ## pr_ready **pr_ready** (`tools/pr_ready.py --quick --base d928d1cd6161e95f5704d05e14193f5714730d9f` on f2196cc12001, against d928d1cd6161e95f5704d05e14193f5714730d9f at d928d1cd6, 11 min 56 s) | Check | Result | | --- | --- | | Scope | ✅ 3 files, +26 −1 | | Cargo.lock | ✅ unchanged | | Public tree | ✅ | | Python tests | ✅ 611 passed (32 s) | | rustfmt | ✅ (7 s) | | clippy | ✅ (1 min 19 s) | | tests: horizon-client, horizon-client-lib | ✅ 2073 passed (9 min 42 s) | | doc tests | ✅ 0 passed (4 s) | | content root tests | ➖ content root untouched | | mod API up to date | ✅ (11 s) | | tokens.css up to date | ➖ theme untouched | | launcher rustfmt | ➖ launcher untouched | | launcher clippy | ➖ launcher untouched | | launcher tests | ➖ launcher untouched | | Parity | ➖ not run (--quick): CI's parity job picks the affected harnesses | | Label | player-facing: `feature` if players get something new, no label for a fix (`release-now` if urgent) | Merge with a merge commit, please. 🤖 Generated with Dyna. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-03 06:37:36 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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
horizon-rust/horizon#7315
No description provided.