Archive incari-client (client, 26.3, clean)

This commit is contained in:
2026-11-28 00:54:38 -06:00
parent 7a6174687f
commit 12838e838c
2383 changed files with 280266 additions and 0 deletions
@@ -0,0 +1,25 @@
# Incari's FPS Display (features/fpsdisplay) for the Rust build (ADR 0068, 0069). Hooks by name only
# (native-hooks.json). Its settings and HUD elements are the client's (rust/mods/incari): the module
# menu shows them and the HUD editor places them.
[mod]
id = "incari_fpsdisplay"
version = "0.1.0"
priority = 100
[[module]]
id = "runtime"
file = "crates/horizon-client/src/game.rs"
name = "incari_fpsdisplay"
source = "runtime.rs"
[[injection]]
id = "hud"
hook = "hud"
hook_version = 1
kind = "HEAD"
helper = "hud.rs"
bindings = [
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
{ name = "batch", ty = "&mut horizon_client::hud::HudBatch", value = "&mut *batch" },
{ name = "corners", ty = "&mut horizon_client::extras_hud::Corners", value = "&mut *corners" },
]
@@ -0,0 +1,23 @@
// The frame rate on the glass, where the player placed it.
fn incari_fpsdisplay_hud(
game: &mut crate::game::Game<'_>,
batch: &mut horizon_client::hud::HudBatch,
corners: &mut horizon_client::extras_hud::Corners,
) {
use crate::game::{incari_client as c, incari_fpsdisplay as m};
use incari_core::theme::Tone;
let _ = corners;
let (fps, _, _) = game.native_frame_rate();
c::element(game, batch, m::ID, m::ELEMENT, |canvas| {
let format = canvas.module.choice("format");
let tone = match m::tone(canvas.module.bool("speed-colors"), fps) {
m::Tone::Text => Tone::Text,
m::Tone::Positive => Tone::Positive,
m::Tone::Warning => Tone::Warning,
m::Tone::Negative => Tone::Negative,
};
m::with_text(format, fps, |text| {
Some(canvas.text_element(text, Some(m::widest(format)), tone))
})
});
}
@@ -0,0 +1,92 @@
// Incari's FPS Display for the Rust build (ADR 0068, 0069), as features/fpsdisplay does it: frames
// drawn in the last second, "120 FPS", green from 60, amber from 30, red below. Its settings and HUD
// element are the module's in `incari_core::modules` (the Java ids), shown in the module menu and
// placed in the HUD editor. No game types: rust/lib.rs tests it.
/// The module's id, as the Java client's profiles name it.
pub const ID: &str = "fpsdisplay";
/// Its HUD element's id.
pub const ELEMENT: &str = "fps";
/// "120 FPS" in `format` (the `format` setting's key).
pub fn text(format: &str, fps: u32) -> String {
match format {
"before" => format!("FPS: {fps}"),
"number" => fps.to_string(),
_ => format!("{fps} FPS"),
}
}
/// The widest it says, so its width holds still as the count changes.
pub fn widest(format: &str) -> &'static str {
match format {
"before" => "FPS: 000",
"number" => "000",
_ => "000 FPS",
}
}
/// The text's tone (`incari_core::theme::Tone`'s of the same names).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Tone {
Text,
Positive,
Warning,
Negative,
}
/// The theme's text, or by speed when `by_speed`.
pub fn tone(by_speed: bool, fps: u32) -> Tone {
match fps {
_ if !by_speed => Tone::Text,
60.. => Tone::Positive,
30.. => Tone::Warning,
_ => Tone::Negative,
}
}
type CachedText = (Option<(&'static str, u32)>, String);
// Hot path, don't allocate: made again only when the count changes, about once a second.
thread_local! {
static TEXT: std::cell::RefCell<CachedText> = const { std::cell::RefCell::new((None, String::new())) };
}
/// Calls `f` with `text(format, fps)`, made again only when one of them changed.
pub fn with_text<R>(format: &'static str, fps: u32, f: impl FnOnce(&str) -> R) -> R {
TEXT.with(|cell| {
let mut cached = cell.borrow_mut();
if cached.0 != Some((format, fps)) {
*cached = (Some((format, fps)), text(format, fps));
}
f(&cached.1)
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn says_what_the_java_module_says() {
assert_eq!(text("after", 120), "120 FPS");
assert_eq!(text("before", 120), "FPS: 120");
assert_eq!(text("number", 120), "120");
assert_eq!(widest("before"), "FPS: 000");
assert_eq!(with_text("after", 7, str::to_owned), "7 FPS");
}
#[test]
fn colours_by_speed_only_when_asked() {
assert_eq!(tone(false, 5), Tone::Text);
assert_eq!(
[
tone(true, 60),
tone(true, 59),
tone(true, 30),
tone(true, 29)
],
[Tone::Positive, Tone::Warning, Tone::Warning, Tone::Negative]
);
}
}