diff --git a/crates/xcl/src/hle/gameinput.rs b/crates/xcl/src/hle/gameinput.rs index 58aeb5c106..5af5bf12d2 100644 --- a/crates/xcl/src/hle/gameinput.rs +++ b/crates/xcl/src/hle/gameinput.rs @@ -12,7 +12,8 @@ //! State changes enter a half-second history and reach the reading callbacks. //! Methods not listed log their slot once and fail. //! -//! `XCL_PRESS_A_AT=s1,s2…`: press A for 200 ms at those seconds after start (tests). +//! `XCL_PRESS_A_AT=s1,s2…`: press A for 200 ms at those seconds after the title's entry point is +//! called (the startup shader preparation and loading are not counted) (tests). #![allow(non_snake_case)] @@ -242,7 +243,9 @@ fn button_named(name: &str) -> Option { /// `XCL_PRESS_AT=s:BUTTON,…` ("14:A,18.5:DOWN,20:A"; buttons A B X Y UP DOWN LEFT RIGHT MENU /// VIEW LB RB) and `XCL_PRESS_A_AT=s,…`: buttons held for 200 ms at those seconds after the -/// host started (the log's times) – a title's menus walked without a person at the controls. +/// title's entry point was called (the startup shader preparation and loading are not counted; +/// the log's times run from the host's start) – a title's menus walked without a person at the +/// controls. fn parse_presses(script: &str, a_only: &str) -> Vec<(f64, u32)> { let mut v: Vec<(f64, u32)> = a_only.split(',').filter_map(|s| s.trim().parse().ok()).map(|s| (s, A)).collect(); for item in script.split(',').map(str::trim).filter(|s| !s.is_empty()) { @@ -254,15 +257,22 @@ fn parse_presses(script: &str, a_only: &str) -> Vec<(f64, u32)> { v } +/// The scripted buttons held `t` seconds after the title's entry point was called (`None`: not +/// called yet, e.g. the startup shader preparation is still running, so nothing is pressed). +fn press_held(at: &[(f64, u32)], t: Option) -> u32 { + let Some(t) = t else { return 0 }; + at.iter().filter(|&&(s, _)| t >= s && t < s + 0.2).fold(0, |b, &(_, g)| b | g) +} + fn scripted() -> u32 { static AT: OnceLock> = OnceLock::new(); let at = AT.get_or_init(|| { let var = |n: &str| std::env::var(n).unwrap_or_default(); parse_presses(&var("XCL_PRESS_AT"), &var("XCL_PRESS_A_AT")) }); - // seconds since the host started, as the log's times (not since the title first polled) - let t = crate::log::uptime(); - at.iter().filter(|&&(s, _)| t >= s && t < s + 0.2).fold(0, |b, &(_, g)| b | g) + // seconds since the title's entry point was called (not since the host started: shader + // preparation runs before the guest and can take longer than the whole script) + press_held(at, crate::log::guest_uptime()) } #[link(name = "user32")] @@ -1350,6 +1360,20 @@ mod tests { assert!(parse_presses("", "").is_empty()); } + #[test] + fn scripted_presses_count_from_guest_entry() { + let at = parse_presses("", "40,60"); + // preparation (guest not entered): nothing fires, however long it takes + assert_eq!(press_held(&at, None), 0); + // at entry + 40 s the first press is held for 200 ms + assert_eq!(press_held(&at, Some(39.9)), 0); + assert_eq!(press_held(&at, Some(40.0)), A); + assert_eq!(press_held(&at, Some(40.19)), A); + assert_eq!(press_held(&at, Some(40.2)), 0); + assert_eq!(press_held(&at, Some(60.1)), A); + assert_eq!(press_held(&at, Some(61.0)), 0); + } + #[test] fn keyboard_mouse_config_gates_keyboard_and_kbm_but_keeps_scripted() { let mut s = GamepadState { buttons: B, left_x: 0.25, ..Default::default() }; diff --git a/crates/xcl/src/loader.rs b/crates/xcl/src/loader.rs index 52c49afe78..4b46859e04 100644 --- a/crates/xcl/src/loader.rs +++ b/crates/xcl/src/loader.rs @@ -328,6 +328,7 @@ struct EntryArgs { unsafe extern "system" fn main_thread(param: *mut c_void) -> u32 { let args = Box::from_raw(param as *mut EntryArgs); let f: ExeEntry = std::mem::transmute(args.entry); + crate::log::mark_guest_entry(); f(peb() as *mut c_void) } diff --git a/crates/xcl/src/log.rs b/crates/xcl/src/log.rs index 7f5d1f180a..2aef7afabc 100644 --- a/crates/xcl/src/log.rs +++ b/crates/xcl/src/log.rs @@ -49,6 +49,19 @@ pub fn uptime() -> f64 { START.get_or_init(Instant::now).elapsed().as_secs_f64() } +static GUEST: OnceLock = OnceLock::new(); + +/// Records the moment the title's entry point is called (after the startup shader preparation +/// and the loading). +pub fn mark_guest_entry() { + GUEST.get_or_init(Instant::now); +} + +/// Seconds since the title's entry point was called, `None` before that. +pub fn guest_uptime() -> Option { + GUEST.get().map(|t| t.elapsed().as_secs_f64()) +} + pub fn init(path: Option<&Path>, level: Level, echo_stderr: bool) { START.get_or_init(Instant::now); LEVEL.store(level as u8, Ordering::Relaxed);