Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
# Incari's CPS (features/cps) for the Rust build (ADR 0068, 0069). Hooks by name only
|
||||
# (native-hooks.json). Its settings and HUD element are the client's (rust/mods/incari): the module
|
||||
# menu shows them and the HUD editor places it.
|
||||
[mod]
|
||||
id = "incari_cps"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_cps"
|
||||
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,16 @@
|
||||
// The count on the glass, where the player placed it.
|
||||
fn incari_cps_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_cps as m};
|
||||
let _ = corners;
|
||||
let (left, right) = game.native_clicks();
|
||||
c::element(game, batch, m::ID, "cps", |canvas| {
|
||||
let buttons = canvas.module.choice("buttons");
|
||||
m::with_text(buttons, left, right, |text| {
|
||||
Some(canvas.text_element(text, Some(m::widest(buttons)), incari_core::theme::Tone::Text))
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
// Incari's CPS for the Rust build (ADR 0068, 0069), as features/cps does it: clicks in the last
|
||||
// second, left, right or both, "12 | 4 CPS". 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.
|
||||
// This file is both the module the bundle adds to the game and, through rust/lib.rs, the code its
|
||||
// tests run: it uses no game types.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "cps";
|
||||
|
||||
/// "12 | 4 CPS" for `buttons` (the `buttons` setting's key).
|
||||
pub fn text(buttons: &str, left: u32, right: u32) -> String {
|
||||
match buttons {
|
||||
"left" => format!("{left} CPS"),
|
||||
"right" => format!("{right} CPS"),
|
||||
_ => format!("{left} | {right} CPS"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The widest it says, so its width holds still as the counts change.
|
||||
pub fn widest(buttons: &str) -> &'static str {
|
||||
if buttons == "both" {
|
||||
"00 | 00 CPS"
|
||||
} else {
|
||||
"00 CPS"
|
||||
}
|
||||
}
|
||||
|
||||
/// The last text made, and what it was made for.
|
||||
type CachedText = (Option<(&'static str, u32, u32)>, String);
|
||||
|
||||
// Hot path, don't allocate: the text is made again only when a count changes.
|
||||
thread_local! {
|
||||
static TEXT: std::cell::RefCell<CachedText> = const { std::cell::RefCell::new((None, String::new())) };
|
||||
}
|
||||
|
||||
/// Calls `f` with `text(buttons, left, right)`, made again only when one of them changed.
|
||||
pub fn with_text<R>(buttons: &'static str, left: u32, right: u32, f: impl FnOnce(&str) -> R) -> R {
|
||||
TEXT.with(|cell| {
|
||||
let mut cached = cell.borrow_mut();
|
||||
if cached.0 != Some((buttons, left, right)) {
|
||||
*cached = (Some((buttons, left, right)), text(buttons, left, right));
|
||||
}
|
||||
f(&cached.1)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn says_what_the_java_module_says() {
|
||||
assert_eq!(text("both", 12, 4), "12 | 4 CPS");
|
||||
assert_eq!(text("left", 12, 4), "12 CPS");
|
||||
assert_eq!(text("right", 12, 4), "4 CPS");
|
||||
assert_eq!(widest("left"), "00 CPS");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn makes_the_text_again_only_when_a_count_changes() {
|
||||
let first = with_text("both", 3, 1, |t| (t.to_owned(), t.as_ptr()));
|
||||
assert_eq!(first.0, "3 | 1 CPS");
|
||||
assert_eq!(with_text("both", 3, 1, |t| t.as_ptr()), first.1);
|
||||
assert_eq!(with_text("left", 3, 1, str::to_owned), "3 CPS");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user