diff --git a/Cargo.lock b/Cargo.lock index 6945e09cf..ecea2e04e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,6 +993,13 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fps-overlay" +version = "1.0.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "futures-core" version = "0.3.34" @@ -1812,6 +1819,13 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "keystrokes" +version = "1.0.0" +dependencies = [ + "horizon-mod-sdk", +] + [[package]] name = "lazy_static" version = "1.5.1" diff --git a/Cargo.toml b/Cargo.toml index 4076b9079..57991c185 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] exclude = ["launcher", "crates/horizon-telemetry-server", "crates/horizon-mod-registry", "third_party/wgpu-hal"] -members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "xtask"] +members = ["crates/horizon-core", "crates/horizon-dualsense", "crates/horizon-threads", "crates/horizon-world", "crates/horizon-client", "crates/horizon-server-files", "crates/horizon-server", "crates/horizon-bots", "crates/horizon-assets", "crates/horizon-protocol", "crates/horizon-theme", "crates/horizon-prepare", "crates/horizon-telemetry", "crates/horizon-bench", "crates/horizon-friends", "crates/horizon-relay", "crates/horizon-mod-sdk", "crates/horizon-mods", "crates/horizon-mods/tests/guest", "crates/horizon-mods/tests/ui_guest", "crates/cargo-horizon-mod", "crates/horizon-port-pack", "crates/horizon-shaders", "crates/horizon-shader-api", "crates/horizon-release-sign", "crates/horizon-mod-sdk/examples/coords-hud", "mods/official/fps-overlay", "mods/official/keystrokes", "xtask"] resolver = "2" [workspace.package] diff --git a/docs/mods.md b/docs/mods.md index d92c88fa6..44cd24122 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -15,6 +15,7 @@ folder, loads no runtime and plays exactly like vanilla 26.3. | The contract (WIT) | `crates/horizon-mod-sdk/wit/horizon.wit` | 0.1: log, game-read, chat, hud, storage, ui; tick, frame, chat and command hooks | | SDK for mod authors | `crates/horizon-mod-sdk` | 0.1 | | Example mod | `crates/horizon-mod-sdk/examples/coords-hud` | coordinates, facing and time HUD, `/where`, `/coords` | +| Horizon's official mods | `mods/official/` | the extras moving to the registry: FPS Counter (`fps-overlay`), Keystrokes | | Runtime | `crates/horizon-mods` | wasmtime 36 LTS, component model | | Authoring tool | `crates/cargo-horizon-mod` | `cargo horizon-mod new / build / run / check`, and the registry's CI (`registry build / review / list`) | | In the game | `crates/horizon-client/src/game/mods.rs`, Options > Mods | client mods | @@ -406,6 +407,21 @@ An update is the same form with a new version; only the project's `owners` can p has one. A published file never changes. Without the form, open the pull request by hand (the repository's README says how). The formats: [api/mod-index.md](api/mod-index.md). +### Horizon's official mods + +The Horizon extras that are mods now live in `mods/official//`, one crate each in the game's workspace, so +`cargo clippy --workspace` and pr_ready cover them. They use only the public API, like any other mod, with the same +options on their settings page as the extra had and the same look. Each is published by a maintainer: build it on +`main` with the toolchain CI uses (`cd mods/official/ && cargo horizon-mod build`), then open a pull request in +horizon-mods with its `mods/.toml` (owners `ludovic111`, `source` pointing at the folder) and the file's +SHA-256. The reviewer builds it the same way and merges only when the hashes match: the `.wasm` holds no local paths, +so the same rustc and `Cargo.lock` give the same file. + +| Mod | Id | Replaces | Settings (storage key: values) | +|---|---|---|---| +| FPS Counter | `fps_overlay` | Extras > FPS counter | `corner`: `top_left` (default), `top_right`, `bottom_left`, `bottom_right`; `lows`: `false` (default), `true` | +| Keystrokes | `keystrokes` | Extras > Keystrokes | `corner`: as above, default `top_left`; `cps`: `true` (default), `false` | + ## Versions and compatibility - The API is the WIT package `horizon:mods@0.1.0`. New functions and diff --git a/mods/official/fps-overlay/Cargo.toml b/mods/official/fps-overlay/Cargo.toml new file mode 100644 index 000000000..7af17ea0d --- /dev/null +++ b/mods/official/fps-overlay/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "fps-overlay" +version = "1.0.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Horizon's official FPS counter: frames per second in a corner, with the minimum and the 1% low." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../../../crates/horizon-mod-sdk" } + +[lints] +workspace = true diff --git a/mods/official/fps-overlay/horizon-mod.toml b/mods/official/fps-overlay/horizon-mod.toml new file mode 100644 index 000000000..b18e79f12 --- /dev/null +++ b/mods/official/fps-overlay/horizon-mod.toml @@ -0,0 +1,9 @@ +id = "fps_overlay" +name = "FPS Counter" +version = "1.0.0" +description = "Frames per second in a corner of the screen, with the slowest frame and the 1% low if you like. Horizon's own, formerly an extra." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["render", "ui", "storage"] +experimental = ["render", "ui"] diff --git a/mods/official/fps-overlay/src/lib.rs b/mods/official/fps-overlay/src/lib.rs new file mode 100644 index 000000000..897402d02 --- /dev/null +++ b/mods/official/fps-overlay/src/lib.rs @@ -0,0 +1,131 @@ +//! Horizon's official FPS counter, formerly the built-in extra: the game's +//! frame counter (frames in the last second, the slowest frame and the 1% +//! low of the last 5 seconds) in a box in a corner, drawn as the extra drew +//! it. Its settings page has the extra's two options; they are kept in the +//! mod's storage (`corner`, `lows`), where the game writes a player's old +//! extra settings when it installs the mod for them. +use horizon_mod_sdk::{ + Mod, Registration, + render::{self, Corner, FillShape, Point, Rect, Shape, TextShape}, + storage, + ui::{self, ButtonWidget, Widget}, +}; + +/// The extra's box: translucent black, white text with a shadow. +const BACKGROUND: u32 = 0x7800_0000; +const CORNERS: [(Corner, &str, &str); 4] = [ + (Corner::TopLeft, "top_left", "Top left"), + (Corner::TopRight, "top_right", "Top right"), + (Corner::BottomLeft, "bottom_left", "Bottom left"), + (Corner::BottomRight, "bottom_right", "Bottom right"), +]; +/// Settings page buttons. +const CORNER_BUTTON: u32 = 1; +const LOWS_BUTTON: u32 = 2; + +struct Fps { + /// Index in `CORNERS`. + corner: usize, + lows: bool, + /// What the box shows now, to redraw only on a change. + shown: String, +} + +impl Fps { + fn text(&self, f: render::FrameRate) -> String { + if self.lows { + format!("{} fps min {} 1% {}", f.fps, f.min, f.low) + } else { + format!("{} fps", f.fps) + } + } + fn draw(&mut self) { + let text = self.text(render::frame_stats()); + if text == self.shown { + return; + } + let w = render::text_width(&text) + 4.0; + render::set_corner( + CORNERS[self.corner].0, + w, + 12.0, + &[ + Shape::Fill(FillShape { + rect: Rect { + x: 0.0, + y: 0.0, + w, + h: 12.0, + }, + color: BACKGROUND, + }), + Shape::Text(TextShape { + text: text.clone(), + at: Point { x: 2.0, y: 2.0 }, + color: 0xFF_FFFF, + centered: false, + shadow: true, + }), + ], + ); + self.shown = text; + } + /// The settings page: the screen's title is the mod's name already. + fn page(&self) { + let on = |b: bool| if b { "ON" } else { "OFF" }; + ui::set_settings(&[ + Widget::Button(ButtonWidget { + id: CORNER_BUTTON, + label: format!("Corner: {}", CORNERS[self.corner].2), + }), + Widget::Button(ButtonWidget { + id: LOWS_BUTTON, + label: format!("Minimum and 1% low: {}", on(self.lows)), + }), + ]); + } + fn save(&self) { + let _ = storage::set("corner", CORNERS[self.corner].1.as_bytes()); + let _ = storage::set("lows", if self.lows { b"true" } else { b"false" }); + } +} + +fn stored(key: &str) -> Option { + storage::get(key).and_then(|v| String::from_utf8(v).ok()) +} + +impl Mod for Fps { + fn init() -> Result<(Self, Registration), String> { + // The extra's defaults: top left, no lows. + let corner = stored("corner") + .and_then(|c| CORNERS.iter().position(|k| k.1 == c.trim())) + .unwrap_or(0); + let lows = stored("lows").is_some_and(|v| v.trim() == "true"); + let fps = Fps { + corner, + lows, + shown: String::new(), + }; + fps.page(); + Ok((fps, Registration::new().on_tick())) + } + fn tick(&mut self, _tick: u64) { + self.draw(); + } + fn ui_select(&mut self, id: u32, _row: u32) { + match id { + CORNER_BUTTON => self.corner = (self.corner + 1) % CORNERS.len(), + LOWS_BUTTON => self.lows = !self.lows, + _ => return, + } + self.save(); + self.page(); + self.shown.clear(); + self.draw(); + } + fn ui_settings_opened(&mut self) { + self.page(); + } +} + +horizon_mod_sdk::export_mod!(Fps); diff --git a/mods/official/keystrokes/Cargo.toml b/mods/official/keystrokes/Cargo.toml new file mode 100644 index 000000000..36982a12f --- /dev/null +++ b/mods/official/keystrokes/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "keystrokes" +version = "1.0.0" +edition.workspace = true +license.workspace = true +publish = false +description = "Horizon's official keystrokes overlay: movement keys, mouse buttons and clicks per second." + +[lib] +crate-type = ["cdylib"] + +[dependencies] +horizon-mod-sdk = { path = "../../../crates/horizon-mod-sdk" } + +[lints] +workspace = true diff --git a/mods/official/keystrokes/horizon-mod.toml b/mods/official/keystrokes/horizon-mod.toml new file mode 100644 index 000000000..ffda6cd3b --- /dev/null +++ b/mods/official/keystrokes/horizon-mod.toml @@ -0,0 +1,9 @@ +id = "keystrokes" +name = "Keystrokes" +version = "1.0.0" +description = "Your movement keys, mouse buttons and space bar in a corner, lit while held, with clicks per second. Horizon's own, formerly an extra." +authors = ["Horizon"] +api = "0.1" +side = "client" +capabilities = ["render", "input", "ui", "storage"] +experimental = ["render", "input", "ui"] diff --git a/mods/official/keystrokes/src/lib.rs b/mods/official/keystrokes/src/lib.rs new file mode 100644 index 000000000..29866ffbf --- /dev/null +++ b/mods/official/keystrokes/src/lib.rs @@ -0,0 +1,192 @@ +//! Horizon's official keystrokes overlay, formerly the built-in extra: +//! forward, left, back and right as the player bound them, the two mouse +//! buttons (with clicks per second) and the space bar, lit while held, +//! drawn as the extra drew them. It redraws in the frame hook only when +//! something changed. Settings (`corner`, `cps`) live in the mod's storage. +use horizon_mod_sdk::{ + Mod, Registration, input, + render::{self, Corner, FillShape, Rect, Shape, TextShape}, + storage, + ui::{self, ButtonWidget, Widget}, +}; + +const BACKGROUND: u32 = 0x7800_0000; +/// A held key: light fill, dark text without a shadow. +const HELD: u32 = 0xB0FF_FFFF; +const INK_HELD: u32 = 0x14_1414; +const INK: u32 = 0xFF_FFFF; +const KEY: f32 = 22.0; +const GAP: f32 = 2.0; +const CORNERS: [(Corner, &str, &str); 4] = [ + (Corner::TopLeft, "top_left", "Top left"), + (Corner::TopRight, "top_right", "Top right"), + (Corner::BottomLeft, "bottom_left", "Bottom left"), + (Corner::BottomRight, "bottom_right", "Bottom right"), +]; +const CORNER_BUTTON: u32 = 1; +const CPS_BUTTON: u32 = 2; +const MOVES: [&str; 4] = ["key.forward", "key.left", "key.back", "key.right"]; + +/// What the overlay shows; a change redraws it. +#[derive(Clone, PartialEq, Default)] +struct View { + moves: [(bool, String); 4], + jump: bool, + attack: bool, + use_: bool, + cps: Option<(u32, u32)>, +} + +struct Keystrokes { + corner: usize, + cps: bool, + shown: Option, +} + +fn down(name: &str) -> (bool, String) { + input::mapping(name).map_or((false, String::new()), |m| (m.down, m.label)) +} + +/// The extra's key box: a fill and its lines centred, 9 pixels apart. +fn key_box(out: &mut Vec, [x, y, w, h]: [f32; 4], held: bool, lines: &[&str]) { + let (fill, ink) = if held { + (HELD, INK_HELD) + } else { + (BACKGROUND, INK) + }; + out.push(Shape::Fill(FillShape { + rect: Rect { x, y, w, h }, + color: fill, + })); + let total = lines.len() as f32 * 9.0 - 1.0; + for (i, line) in lines.iter().enumerate() { + let tw = render::text_width(line); + out.push(Shape::Text(TextShape { + text: (*line).into(), + at: render::Point { + x: (x + (w - tw) / 2.0).round(), + y: (y + (h - total) / 2.0).round() + i as f32 * 9.0, + }, + color: ink, + centered: false, + shadow: !held, + })); + } +} + +impl Keystrokes { + fn view(&self) -> View { + let (l, r) = input::clicks_per_second(); + View { + moves: MOVES.map(down), + jump: down("key.jump").0, + attack: down("key.attack").0, + use_: down("key.use").0, + cps: self.cps.then_some((l, r)), + } + } + fn draw(&mut self) { + let v = self.view(); + if self.shown.as_ref() == Some(&v) { + return; + } + let (w, h) = (3.0 * KEY + 2.0 * GAP, 3.0 * KEY + 3.0 * GAP + 10.0); + let step = KEY + GAP; + let mut out = Vec::with_capacity(24); + let [forward, left, back, right] = &v.moves; + key_box(&mut out, [step, 0.0, KEY, KEY], forward.0, &[&forward.1]); + for (i, key) in [left, back, right].into_iter().enumerate() { + key_box( + &mut out, + [i as f32 * step, step, KEY, KEY], + key.0, + &[&key.1], + ); + } + let mw = (w - GAP) / 2.0; + let my = 2.0 * step; + for (i, (held, label, cps)) in [ + (v.attack, "LMB", v.cps.map(|c| c.0)), + (v.use_, "RMB", v.cps.map(|c| c.1)), + ] + .into_iter() + .enumerate() + { + let rate = cps.map(|n| format!("{n} CPS")); + let lines: Vec<&str> = std::iter::once(label).chain(rate.as_deref()).collect(); + key_box(&mut out, [i as f32 * (mw + GAP), my, mw, KEY], held, &lines); + } + // The space bar: a line in a short key. + let sy = my + step; + key_box(&mut out, [0.0, sy, w, 10.0], v.jump, &[]); + out.push(Shape::Fill(FillShape { + rect: Rect { + x: w / 2.0 - 12.0, + y: sy + 4.0, + w: 24.0, + h: 2.0, + }, + color: 0xFF00_0000 | if v.jump { INK_HELD } else { INK }, + })); + render::set_corner(CORNERS[self.corner].0, w, h, &out); + self.shown = Some(v); + } + /// The settings page: the screen's title is the mod's name already. + fn page(&self) { + let on = |b: bool| if b { "ON" } else { "OFF" }; + ui::set_settings(&[ + Widget::Button(ButtonWidget { + id: CORNER_BUTTON, + label: format!("Corner: {}", CORNERS[self.corner].2), + }), + Widget::Button(ButtonWidget { + id: CPS_BUTTON, + label: format!("Clicks per second: {}", on(self.cps)), + }), + ]); + } + fn save(&self) { + let _ = storage::set("corner", CORNERS[self.corner].1.as_bytes()); + let _ = storage::set("cps", if self.cps { b"true" } else { b"false" }); + } +} + +fn stored(key: &str) -> Option { + storage::get(key).and_then(|v| String::from_utf8(v).ok()) +} + +impl Mod for Keystrokes { + fn init() -> Result<(Self, Registration), String> { + // The extra's defaults: top left, with clicks per second. + let corner = stored("corner") + .and_then(|c| CORNERS.iter().position(|k| k.1 == c.trim())) + .unwrap_or(0); + let cps = stored("cps").is_none_or(|v| v.trim() != "false"); + let k = Keystrokes { + corner, + cps, + shown: None, + }; + k.page(); + Ok((k, Registration::new().on_frame())) + } + fn frame(&mut self, _seconds: f32) { + self.draw(); + } + fn ui_select(&mut self, id: u32, _row: u32) { + match id { + CORNER_BUTTON => self.corner = (self.corner + 1) % CORNERS.len(), + CPS_BUTTON => self.cps = !self.cps, + _ => return, + } + self.save(); + self.page(); + self.shown = None; + self.draw(); + } + fn ui_settings_opened(&mut self) { + self.page(); + } +} + +horizon_mod_sdk::export_mod!(Keystrokes);