Files
moonwell-client-core/crates/benilla-ui/src/script/clock.rs
T
sindoring 779460d87e Sync upstream through d9c64230
Upstream 0.3.0. The ruRU pack now takes upstream's locale path
(Locale::from_language_country picks the column and the auth tag); our
own part is the Data/ruRU mount, the name GetLocale answers and the UI's
Russian name charsets. A language pack folder mounts without the base
set that upstream now requires. moonwell-gfx shadows and per-pixel
points move onto the per-vertex lit colour of upstream #2899; the
retained static pass passes its own normal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 10:25:03 +04:00

89 lines
3.8 KiB
Rust

//! The two clocks Lua reads: `GetTime()`, the session seconds [`UiScript::tick`] advances, in the
//! [`Model`], out of an addon's reach, and `GetGameTime()`, the game clock the host answers at the
//! call ([`GameTimeQuery`]), as the reference's getter reads the clock object.
//!
//! [`UiScript::tick`]: super::UiScript::tick
use mlua::Lua;
use super::live::{ask, LiveQuery};
use super::Model;
/// The question `GetGameTime` (`0x515ee0`) asks at the call: the game clock's hour `[0xce853c]` and
/// minute `[0xce8538]`, as the clock holds them, -1 for a field it has not set.
pub struct GameTimeQuery;
impl LiveQuery for GameTimeQuery {
type Answer = (i32, i32);
}
impl super::UiScript {
/// The current `GetTime()` value in seconds; the app stamps absolute expiries with it, such as
/// an aura's `expirationTime`.
pub fn now(&self) -> f64 {
self.model_ref().now
}
/// Start this VM's `GetTime()` clock at `secs`, so a rebuilt VM (a relog, a `ReloadUI`) keeps
/// the process's clock. The reference's `GetTime` (`0x515ea0`, through `0x42c010` and
/// `0x42b790`) is `GetTickCount` scaled by 0.001, an OS clock that never restarts, which stock
/// `Cooldown.lua` relies on (`start > 0`). Set once, at construction; after that only
/// [`Self::tick`] moves it.
pub fn set_now(&mut self, secs: f64) {
self.model_mut().now = secs;
}
/// Seat the game clock `GetGameTime()` answers with no host to ask, `(hour, minute)`, as a new
/// VM's seed; -1, -1 until the first. The clock holds either as the server sent it, so a field
/// is -1 when unset and may run past a day's end (an hour to 30, a minute to 62).
pub fn set_game_time(&mut self, hour: i32, minute: i32) {
self.model_mut().game_time = Some((hour, minute));
}
}
/// The start a cooldown binding answers: a held one (`enabled` false) reports the call's own now,
/// as the reference's held legs write the query time (`0x6e1517`, `0x6e15a1`), so the pushed
/// triple carries no moving start.
pub(crate) fn held_start(start: f64, enabled: bool, now: f64) -> f64 {
if enabled {
start
} else {
now
}
}
/// `GetTime()`'s clock, for a caller holding no model borrow; one that holds it reads `model.now`.
pub(crate) fn now(lua: &Lua) -> f64 {
lua.app_data_ref::<Model>().expect("model app_data").now
}
/// Register `GetTime` and `GetGameTime`.
pub(super) fn install(lua: &Lua) -> mlua::Result<()> {
let g = lua.globals();
// GetTime() (`0x515ea0`): one number, `GetTickCount` times the f64 0.001 at `0x801608`.
// Deviation: the reference reads the clock live at each call (`0x42c010`); ours is the session
// clock the tick advances by the paced frame step, one value per frame, because that way
// `GetTime()` deltas and the `elapsed` handed to OnUpdate agree, as FrameXML (CastingBarFrame
// and kin) assumes.
g.set("GetTime", lua.create_function(|lua, ()| Ok(now(lua)))?)?;
// GetGameTime() (`0x515ee0`): two numbers, the hour `[0xce853c]` then the minute `[0xce8538]`,
// the clock the server's time messages set, read at the call through the host; minute
// resolution, no seconds. Read unguarded, so before the server sets the clock it answers the -1
// its constructor stores in both fields (`0x642300`, called at `0x643308`). With no host to
// ask, the copy a new VM was seeded with.
g.set(
"GetGameTime",
lua.create_function(|lua, ()| {
let clock = ask(lua, &GameTimeQuery).or_else(|| {
lua.app_data_ref::<Model>()
.expect("model app_data")
.game_time
});
Ok(clock.map_or((-1.0, -1.0), |(hour, minute)| {
(f64::from(hour), f64::from(minute))
}))
})?,
)?;
Ok(())
}