Code mods are the client's mods feature: tests and probes skip wasmtime

horizon-mods' runtime is no longer a default feature, and its sandbox test
requires runtime and pack instead of a dev-dependency on itself (that
dev-dependency turned the runtime on in every workspace build with examples,
the parity probe build included). The client gets a mods feature that turns
the runtime on; without it game/mods.rs keeps content mods and logs why code
mods don't start, and every hook does nothing.

Release builds (client and server) pass the feature, CI's clippy and tests
pass it (and pack, for the sandbox test), and pr_ready does where horizon-mods
is checked. horizon_mods::find moves to manifest.rs: content mods use it
without the runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-23 14:58:39 +02:00
parent 2439475716
commit b15e5e893e
14 changed files with 138 additions and 52 deletions
+5 -1
View File
@@ -18,7 +18,8 @@ horizon-protocol = { path = "../horizon-protocol" }
horizon-server-files = { path = "../horizon-server-files" }
horizon-friends = { path = "../horizon-friends" }
# Horizon mods: sandboxed WebAssembly components, off unless installed and turned on (docs/mods.md).
horizon-mods = { path = "../horizon-mods" }
# Their runtime is the `mods` feature.
horizon-mods = { path = "../horizon-mods", default-features = false }
wgpu = { version = "30.0.1", default-features = false, features = ["std", "parking_lot", "vulkan", "metal", "wgsl"] }
winit = "0.30.13"
pollster = "0.4"
@@ -78,6 +79,9 @@ default = ["audio", "controllers"]
audio = ["dep:cpal"]
# Controllers: gilrs, and the DualSense's own output. Without it only `horizon-ctl pad`'s virtual pad.
controllers = ["dep:gilrs", "dep:horizon-dualsense"]
# Runs code mods (wasmtime and cranelift: a quarter of a clean build). On in every release build
# (release.yml); the tests and probes don't need it. Content mods load without it.
mods = ["horizon-mods/runtime"]
# Records the call sites of mob random draws (`mob_ai_probe` traces).
rng-trace = ["horizon-core/rng-trace"]
+2 -2
View File
@@ -454,7 +454,7 @@ impl Game<'_> {
return;
}
let focused = self.screen == Screen::Chat;
let heard = self.mods.mods.is_running().then(|| text.plain());
let heard = self.mods.running().then(|| text.plain());
self.ui.chat.add(
&self.gui.atlas,
text,
@@ -583,7 +583,7 @@ impl Game<'_> {
&[Text::literal(PLAYER_NAME), Text::literal(message)],
);
let focused = self.screen == Screen::Chat;
let heard = self.mods.mods.is_running().then(|| line.plain());
let heard = self.mods.running().then(|| line.plain());
self.ui
.chat
.add(&self.gui.atlas, line, None, self.ticks, focused);
+1 -1
View File
@@ -237,7 +237,7 @@ impl Game<'_> {
/// the caller). The top left corner gives way to the debug screen.
pub(super) fn draw_extras(&mut self, batch: &mut HudBatch) {
let e = &self.settings.extras;
let mod_panels = self.mods.mods.panels().next().is_some();
let mod_panels = self.mods.has_panels();
if !e.fps && !e.keystrokes && !e.minimap && !mod_panels {
return;
}
+70 -13
View File
@@ -2,43 +2,76 @@
//! their tick, frame, chat and command hooks, their HUD panels and what
//! they print. With no mod on, `horizon_mods::Mods` holds no engine and
//! every hook here returns after one check.
use super::{Game, Screen};
//!
//! Code mods run only in builds with the `mods` feature (release builds):
//! without it the game links no wasmtime, and every hook does nothing.
//! Content mods (`content_root`) load in every build.
use super::Game;
use horizon_client::{
command::{Dispatcher, vanilla},
extras::Corner,
extras_hud::{self, Corners},
extras_hud::Corners,
hud::HudBatch,
mod_list::{self, Installed},
text::{self, Text},
mod_list,
};
use horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status};
use std::{
path::PathBuf,
time::{Duration, Instant},
use std::{path::PathBuf, time::Duration};
#[cfg(feature = "mods")]
use {
super::Screen,
horizon_client::{
extras::Corner,
extras_hud,
mod_list::Installed,
text::{self, Text},
},
horizon_mods::{Clock, Mods, Output, Player, Snapshot, Status},
std::time::Instant,
};
#[derive(Default)]
pub(super) struct ModsState {
pub(super) mods: Mods,
#[cfg(feature = "mods")]
mods: Mods,
/// `--dev-mod PATH`: always on, reloaded when rebuilt.
pub(super) dev: Vec<PathBuf>,
/// When dev mods were last checked for a rebuild.
#[cfg(feature = "mods")]
checked: Option<Instant>,
/// The chat's command tree while a running mod owns commands.
#[cfg(feature = "mods")]
tree: Option<Dispatcher>,
}
impl ModsState {
/// The chat's command tree: vanilla's, plus the running mods' commands.
pub(super) fn dispatcher(&self) -> &Dispatcher {
self.tree
.as_ref()
.unwrap_or_else(|| vanilla::game_dispatcher())
#[cfg(feature = "mods")]
if let Some(tree) = &self.tree {
return tree;
}
vanilla::game_dispatcher()
}
/// Whether a mod runs: the chat keeps its lines for them.
pub(super) fn running(&self) -> bool {
#[cfg(feature = "mods")]
if self.mods.is_running() {
return true;
}
false
}
/// Whether a mod draws a HUD panel.
pub(super) fn has_panels(&self) -> bool {
#[cfg(feature = "mods")]
if self.mods.panels().next().is_some() {
return true;
}
false
}
}
/// How often dev mods are checked for a rebuild.
#[cfg(feature = "mods")]
const RELOAD_CHECK: Duration = Duration::from_secs(1);
/// A vanilla command name: mods may not take it.
#[cfg(feature = "mods")]
fn vanilla_command(name: &str) -> bool {
let d = vanilla::game_dispatcher();
d.nodes[0]
@@ -112,6 +145,30 @@ pub(super) fn content_root(
}
}
/// A build without the `mods` feature: no mod runs.
#[cfg(not(feature = "mods"))]
impl Game<'_> {
/// Says why the installed and dev mods don't run.
pub(super) fn start_mods(&mut self) {
let dir = Self::options_path().with_file_name(mod_list::DIR);
let found = horizon_mods::find(&dir).len() + self.mods.dev.len();
if found > 0 {
log::warn!(
"Horizon: {found} mod(s) not started: this build has no mod runtime (build with --features mods)"
);
}
}
pub(super) fn sync_mods(&mut self) {}
pub(super) fn tick_mods(&mut self) {}
pub(super) fn mods_frame(&mut self, _elapsed: Duration) {}
pub(super) fn mod_command(&mut self, _command: &str) -> bool {
false
}
pub(super) fn mods_chat(&mut self, _line: &str) {}
pub(super) fn draw_mod_panels(&self, _batch: &mut HudBatch, _corners: &mut Corners) {}
}
#[cfg(feature = "mods")]
impl Game<'_> {
/// Reads `horizon-mods/` and the dev mods, and starts the ones turned on.
pub(super) fn start_mods(&mut self) {
+7 -4
View File
@@ -19,13 +19,16 @@ wit-component = { version = "0.239", optional = true }
wit-parser = { version = "0.239", optional = true }
[features]
default = ["runtime"]
# Loads and runs mods; without it only manifests and packaging (cargo horizon-mod).
# Loads and runs mods (the game's `mods` feature); without it only manifests,
# content and packaging. Off by default so builds that don't run mods (the
# game's tests and probes) skip wasmtime and cranelift.
runtime = ["dep:wasmtime"]
pack = ["dep:wit-component", "dep:wit-parser"]
[dev-dependencies]
horizon-mods = { path = ".", features = ["pack"] }
# Runs real mods: `cargo test -p horizon-mods --features runtime,pack`.
[[test]]
name = "sandbox"
required-features = ["runtime", "pack"]
[lints]
workspace = true
+1 -1
View File
@@ -11,6 +11,6 @@ pub mod manifest;
mod runtime;
pub use content::Content;
pub use manifest::{Capability, Manifest, Package, Side};
pub use manifest::{Capability, Manifest, Package, Side, find};
#[cfg(feature = "runtime")]
pub use runtime::*;
+19 -1
View File
@@ -1,6 +1,10 @@
//! `horizon-mod.toml`, a mod's manifest, and the `.hmod` file around it.
use serde::Deserialize;
use std::{fmt, io::Read, path::Path};
use std::{
fmt,
io::Read,
path::{Path, PathBuf},
};
/// The manifest's file name, beside `mod.wasm`.
pub const MANIFEST: &str = "horizon-mod.toml";
@@ -282,6 +286,20 @@ impl Package {
}
}
/// The `.hmod` files and mod folders in `dir`, by name.
pub fn find(dir: &Path) -> Vec<PathBuf> {
let Ok(read) = std::fs::read_dir(dir) else {
return Vec::new();
};
let mut found: Vec<PathBuf> = read
.flatten()
.map(|e| e.path())
.filter(|p| p.extension().is_some_and(|e| e == "hmod") || p.join(MANIFEST).is_file())
.collect();
found.sort();
found
}
/// Turns a `wasm32-unknown-unknown` cdylib built with the SDK into the
/// component the game loads.
#[cfg(feature = "pack")]
-17
View File
@@ -131,23 +131,6 @@ pub struct Mods {
out: Vec<Output>,
}
/// The `.hmod` files and mod folders in `dir`, by name.
pub fn find(dir: &Path) -> Vec<PathBuf> {
let Ok(read) = std::fs::read_dir(dir) else {
return Vec::new();
};
let mut found: Vec<PathBuf> = read
.flatten()
.map(|e| e.path())
.filter(|p| {
p.extension().is_some_and(|e| e == "hmod")
|| p.join(crate::manifest::MANIFEST).is_file()
})
.collect();
found.sort();
found
}
impl Mods {
/// Reads every mod at `paths` and starts those `enabled` says are on.
/// A command that `reserved` (vanilla's commands) or an earlier mod