From 48e8c5216c6527f90f2f1cae02f5b3ab011fe8db Mon Sep 17 00:00:00 2001 From: Joseph Weiner Date: Mon, 30 Nov 2026 10:24:03 -0500 Subject: [PATCH] gameinput: support state-relative scripted presses --- docs/gameinput.md | 2 +- runtime/README.md | 4 +- runtime/shims/gameinput.cpp | 24 +++-- runtime/shims/gameinput.h | 3 + runtime/shims/gameinput_no_sdl.cpp | 48 +++++++++- runtime/shims/gameinput_script.h | 119 ++++++++++++++++++++++-- runtime/src/main.cpp | 121 ++++++++++++++++--------- runtime/test/gameinput_no_sdl_test.cpp | 49 +++++++++- scripts/windows-boot/README.md | 2 +- scripts/windows-boot/boot.ps1 | 7 +- 10 files changed, 306 insertions(+), 73 deletions(-) diff --git a/docs/gameinput.md b/docs/gameinput.md index 1819f0543e..b34bf15f90 100644 --- a/docs/gameinput.md +++ b/docs/gameinput.md @@ -534,7 +534,7 @@ line is logged when it takes effect); tracking goes on underneath, so clearing i | `LEONIDA_PROMPTS_SWITCH_MS` | `300` | shortest time between two changes of the active input family, 0..2000 ms | | `LEONIDA_GAMEINPUT_THREAD` | `1` | `0` stops the background poll thread; only `rt::gameinput::poll()` then reads the pads (tests) | | `LEONIDA_KEYBOARD_PAD` | `1` | the [keyboard pad](#keyboard-pad), read while this process has focus; `always` reads keys whatever has focus; `off` removes it | -| `LEONIDA_INPUT_SCRIPT` | unset | Comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. With SDL3 they drive the keyboard pad (on unless `LEONIDA_KEYBOARD_PAD=off`); without SDL3 they drive [one scripted pad](#scripted-input-without-sdl3), the only device. The first pad update (without SDL3, the first `GameInputCreate`) reads the script once at startup and starts the clock. `reload_config()` does not reread it. Each press lasts 300 ms, or `hold` seconds when the entry is `button@seconds:hold` (more than 0, at most 600). With SDL3 a press then stays down until the title has read the pads six times since it began, but never past 5 s from its start (or its hold, if longer). A title reads its pad once a frame, so at a few seconds a frame a 300 ms press can fall between two reads and never arrive: on the retail title's "Couldn't autosave" prompt, three 300 ms `b` presses did nothing, while 120 presses 0.5 s apart got through; one `b@1100:3` replaces them. Buttons are `a b x y start back lb rb up down left right`. Spaces around entries are allowed. Empty entries, unknown buttons, invalid times (including negative or non-finite values) and invalid holds (0 or less, more than 600, not a number) are counted and ignored. | +| `LEONIDA_INPUT_SCRIPT` | unset | Comma-separated `button@seconds` entries, e.g. `a@2,a@4,start@6`. A state-relative entry can use `button@game=+` or `button@session=+` (for example `a@session=d+5:3`); it starts its hold after the named state has been observed for that many seconds. State values are the low dword in hexadecimal. This form needs both locally supplied `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA`; when configured, the runtime samples them once a second. Without those values, relative entries stay inactive. With SDL3 scripts drive the keyboard pad (on unless `LEONIDA_KEYBOARD_PAD=off`); without SDL3 they drive [one scripted pad](#scripted-input-without-sdl3), the only device. The first pad update (without SDL3, the first `GameInputCreate`) reads the script once at startup and starts the clock. `reload_config()` does not reread it. Each press lasts 300 ms, or `hold` seconds when the entry has `:hold` (more than 0, at most 600). With SDL3 a press then stays down until the title has read the pads six times since it began, but never past 5 s from its start (or its hold, if longer). A title reads its pad once a frame, so at a few seconds a frame a 300 ms press can fall between two reads and never arrive: on the retail title's "Couldn't autosave" prompt, three 300 ms `b` presses did nothing, while 120 presses 0.5 s apart got through; one `b@1100:3` replaces them. Buttons are `a b x y start back lb rb up down left right`. Spaces around entries are allowed. Empty entries, unknown buttons, invalid times (including negative or non-finite values) and invalid holds (0 or less, more than 600, not a number) are counted and ignored. | | `LEONIDA_MOUSE_SENSITIVITY` | `1` | scales how far the mouse moves the keyboard pad's right stick, 0.1..10 | SDL's own hints (`SDL_JOYSTICK_*`, see SDL's `SDL_hints.h`) also apply, for example `SDL_JOYSTICK_HIDAPI_PS5=0` to diff --git a/runtime/README.md b/runtime/README.md index 62b7db7bcc..fb94dbef4c 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -403,8 +403,8 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_TITLE_WATCHDOG_KICK` | unset | `,` (hex, measured locally and not kept in the repository): where the watchdog's trap instruction is and the byte the game's main loop sets. Used only with `LEONIDA_TITLE_WATCHDOG=0`, and only when the watchdog's trap instruction is at the first RVA; set without `LEONIDA_TITLE_WATCHDOG=0`, the watchdog stays armed and the loader says so once. | | `LEONIDA_POOL_DIAG_AT` | unset | *Diagnostic.* `+0x` of the title's transient-pool assert, as the trace prints it (measured locally): the trace dumps the pool's resource list when that int3 is hit (`shims/startup_trace.h`). | | `LEONIDA_PEEK` | unset | *Diagnostic.* `[:n],,...` (hex): a thread prints the 8-byte values at these RVAs of the title's image (`:n` = n consecutive values) every `LEONIDA_PEEK_SECONDS` (`src/main.cpp`). The RVAs are measured locally and not kept in the repository. `scripts/windows-boot/boot.ps1` no longer sets the game-state and session-state RVAs by default: it sets `LEONIDA_PEEK` only from `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA`, and without them its RESULT states read `?` ([scripts/windows-boot/README.md](../scripts/windows-boot/README.md)). | -| `LEONIDA_PEEK_SECONDS` | `10` | *Diagnostic.* The period of the `LEONIDA_PEEK` thread (`src/main.cpp`), which prints 8-byte values of the title's image. | -| `LEONIDA_INPUT_SCRIPT` | unset | Timed pad presses, `button@seconds` separated by commas (`a@70,a@85,start@120`), each held 300 ms or its own `button@seconds:hold`; the clock starts with the shim. Works with and without SDL3: with SDL3 it drives the keyboard pad (`LEONIDA_KEYBOARD_PAD=always`), without SDL3 one scripted pad that is the only device. Controllers and the keyboard pad still need SDL3. Format and buttons in [docs/gameinput.md](../docs/gameinput.md#environment-variables). | +| `LEONIDA_PEEK_SECONDS` | `10` | *Diagnostic.* The period of the `LEONIDA_PEEK` thread (`src/main.cpp`), which prints 8-byte values of the title's image; with both state RVAs configured, state-relative input sampling uses a 1 s period. | +| `LEONIDA_INPUT_SCRIPT` | unset | Timed pad presses, `button@seconds` or state-relative `button@game=+seconds` / `button@session=+seconds`, comma-separated; each held 300 ms or its own `:hold`. State-relative entries need both local state RVAs and sample once a second. Works with and without SDL3: with SDL3 it drives the keyboard pad (`LEONIDA_KEYBOARD_PAD=always`), without SDL3 one scripted pad that is the only device. Controllers and the keyboard pad still need SDL3. Format and buttons in [docs/gameinput.md](../docs/gameinput.md#environment-variables). | | `LEONIDA_INPUT_LOG` | on | The GameInput shim logs every press in `LEONIDA_INPUT_SCRIPT`'s form (`name@seconds`), so a session played by hand can be replayed. `0` turns the lines off. | | `LEONIDA_INPUT_TRACE` | unset | `1` (diagnostic, the GameInput shim built without SDL3) logs the title's reading calls on the scripted pad: the first 128, then each one with a button down and every 1000th. Details in [docs/gameinput.md](../docs/gameinput.md#scripted-input-without-sdl3). | | `LEONIDA_LOG_ALLOCINFO` | unset | *Diagnostic.* `1` logs every device slot 25 call (`GetResourceAllocationInfo`) with its thread, description and sizes (`shims/d3d12_xs_device.cpp`). | diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp index eeb5fdf6a2..b4ce57dca8 100644 --- a/runtime/shims/gameinput.cpp +++ b/runtime/shims/gameinput.cpp @@ -1990,11 +1990,15 @@ void update_keyboard_pad() { }(); if (!scripted.empty() || !input_file.empty()) { static const ULONGLONG origin = GetTickCount64(); - const double now = (GetTickCount64() - origin) / 1000.0; + const ULONGLONG tick = GetTickCount64(); + const uint64_t origin_us = origin * 1000; + const double now = (tick - origin) / 1000.0; + const script::TitleState title = script::current_title_state(); script::Held held{}; static constexpr uint64_t kReads = 6; static constexpr double kMaxHold = 5.0; static std::vector began(scripted.presses.size(), 0); + static std::vector scheduled(scripted.presses.size(), 0); static std::vector started(scripted.presses.size(), 0); const uint64_t calls = g_reading_calls.load(std::memory_order_relaxed); if (!input_file.empty()) { @@ -2035,14 +2039,14 @@ void update_keyboard_pad() { } for (std::size_t i = 0; i < scripted.presses.size(); ++i) { const auto& press = scripted.presses[i]; - bool active = false; - if (now >= press.at && now < press.at + std::max(press.hold, kMaxHold)) { - if (!started[i]) { - started[i] = 1; - began[i] = calls; - } - active = now < press.at + press.hold || calls - began[i] < kReads; + if (!started[i]) { + const std::optional due = script::start_at(press, origin_us, title); + if (!due || now < *due || now >= *due + std::max(press.hold, kMaxHold)) continue; + started[i] = 1; + scheduled[i] = *due; + began[i] = calls; } + const bool active = now < scheduled[i] + press.hold || calls - began[i] < kReads; if (!active) continue; if (press.stick) held.stick = press.stick; else held.buttons |= press.button; @@ -2979,6 +2983,10 @@ void poll() { poll_and_deliver(); } +void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us) { + script::update_title_state(game, session, observed_us); +} + int connected_count() { std::lock_guard lock(g().mu); return static_cast(g().connected.size()); diff --git a/runtime/shims/gameinput.h b/runtime/shims/gameinput.h index fdb0b07042..f5f537ed18 100644 --- a/runtime/shims/gameinput.h +++ b/runtime/shims/gameinput.h @@ -48,6 +48,9 @@ const Config& config(); // Re-reads the environment (tests). Takes effect for the next rumble update. void reload_config(); +// Publishes the locally peeked title states for state-relative LEONIDA_INPUT_SCRIPT entries. Safe from any thread. +void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us); + // One step of the backend, on the calling thread: initialise SDL if needed, open new gamepads, close removed ones, // take a reading from each, apply pending rumble/trigger output, and deliver queued callbacks unless a dispatcher // owns them. With the background thread on (the default) this runs every 2 ms by itself; with it off diff --git a/runtime/shims/gameinput_no_sdl.cpp b/runtime/shims/gameinput_no_sdl.cpp index 8f553a4b79..0c39b2710e 100644 --- a/runtime/shims/gameinput_no_sdl.cpp +++ b/runtime/shims/gameinput_no_sdl.cpp @@ -36,6 +36,7 @@ #include #include +#include "gameinput.h" #include "gameinput_abi.h" #include "gameinput_no_sdl.h" #include "gameinput_script.h" @@ -393,6 +394,9 @@ struct State { script::Script script; uint64_t origin_us = 0; // the script's clock starts at the first GameInputCreate double updated_s = -1; // the script time of the last update; its changes up to here are in the history + std::vector scheduled; + std::vector started; + bool script_loaded = false; bool log_presses = true; // LEONIDA_INPUT_LOG bool trace_reads = false; // LEONIDA_INPUT_TRACE uint32_t logged_held = 0; // what log_scripted saw last @@ -415,7 +419,14 @@ State& g() { // Takes a reading when the script's state `seconds` into it differs from the current one. Under g().mu. void take_reading(double seconds, uint64_t timestamp) { State& s = g(); - const script::Held held = script::held_at(s.script, seconds); + script::Held held; + for (size_t i = 0; i < s.script.presses.size(); ++i) { + if (!s.started[i]) continue; + const script::Press& press = s.script.presses[i]; + if (seconds < s.scheduled[i] || seconds >= s.scheduled[i] + press.hold) continue; + if (press.stick) held.stick = press.stick; + else held.buttons |= press.button; + } GameInputGamepadState state{}; state.buttons = held.buttons; state.rightThumbstickY = static_cast(held.stick); @@ -441,11 +452,29 @@ void take_reading(double seconds, uint64_t timestamp) { void update(uint64_t now) { State& s = g(); const double seconds = now > s.origin_us ? (now - s.origin_us) / 1e6 : 0.0; + if (s.scheduled.size() != s.script.presses.size()) { + s.scheduled.assign(s.script.presses.size(), 0.0); + s.started.assign(s.script.presses.size(), false); + } + const script::TitleState title = script::current_title_state(); + for (size_t i = 0; i < s.script.presses.size(); ++i) { + if (s.started[i]) continue; + const script::Press& press = s.script.presses[i]; + const std::optional due = script::start_at(press, s.origin_us, title); + if (due && seconds >= *due && seconds < *due + std::max(press.hold, script::kMaxHoldSeconds)) { + s.scheduled[i] = *due; + s.started[i] = true; + } + } if (seconds > s.updated_s) { std::vector edges; - for (const script::Press& p : s.script.presses) - for (const double edge : {p.at, p.at + p.hold}) + for (size_t i = 0; i < s.script.presses.size(); ++i) { + if (!s.started[i]) continue; + const double start = s.scheduled[i]; + const double hold = s.script.presses[i].hold; + for (const double edge : {start, start + hold}) if (edge > s.updated_s && edge < seconds) edges.push_back(edge); + } std::sort(edges.begin(), edges.end()); edges.erase(std::unique(edges.begin(), edges.end()), edges.end()); for (const double edge : edges) take_reading(edge, s.origin_us + static_cast(edge * 1e6 + 0.5)); @@ -767,6 +796,9 @@ ScriptedInput* scripted() { State& s = g(); std::lock_guard lock(s.mu); s.script = script::parse(text); + s.script_loaded = true; + s.scheduled.assign(s.script.presses.size(), 0.0); + s.started.assign(s.script.presses.size(), false); s.log_presses = script::env_lower("LEONIDA_INPUT_LOG") != "0"; s.trace_reads = script::env_lower("LEONIDA_INPUT_TRACE") == "1"; g_trace_readings = s.trace_reads; @@ -799,6 +831,16 @@ HRESULT WINAPI GameInputCreate(void** out) { } // namespace +namespace rt::gameinput { +void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us) { + (void)observed_us; // the scripted pad's clock is QPC-based (and replaceable in tests) + script::update_title_state(game, session, now_us()); + State& s = g(); + std::lock_guard lock(s.mu); + if (s.script_loaded) update(now_us()); +} +} // namespace rt::gameinput + namespace rt::gameinput_no_sdl { void set_clock(ClockFn now) { g_clock.store(now ? now : &qpc_now_us); } } // namespace rt::gameinput_no_sdl diff --git a/runtime/shims/gameinput_script.h b/runtime/shims/gameinput_script.h index 5be5544240..d0e363ba8d 100644 --- a/runtime/shims/gameinput_script.h +++ b/runtime/shims/gameinput_script.h @@ -4,8 +4,10 @@ // needs SDL. docs/gameinput.md, "Scripted input", has the format. // // LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" +// LEONIDA_INPUT_SCRIPT="a@session=d+5:3" (requires the local game/session state RVAs) // -// Each entry is button@seconds[:hold], the seconds counted from the shim's first update. A press is held for `hold` +// Each entry is button@seconds[:hold], the seconds counted from the shim's first update, or button@game=+seconds +// / button@session=+seconds[:hold], counted from when that state was observed. A press is held for `hold` // seconds (kHoldSeconds when the entry has none), then released: a longer hold reaches a title that reads its pad // only every few seconds, as the retail title does at world-start frame rates. Buttons: a b x y start back lb rb // up down left right; rsup and rsdown throw the right stick fully up or down for the same time (they replay the @@ -14,11 +16,15 @@ // kMaxHoldSeconds) are counted as rejected. #pragma once #include +#include +#include #include #include #include +#include #include #include +#include #include #include @@ -46,11 +52,66 @@ inline constexpr Button kButtons[] = { {"left", gi::GameInputGamepadDPadLeft, 13}, {"right", gi::GameInputGamepadDPadRight, 14}, }; +enum class Anchor : uint8_t { Absolute, GameState, SessionState }; + +struct TitleState { + uint64_t game = UINT64_MAX; + uint64_t session = UINT64_MAX; + uint64_t game_since_us = 0; + uint64_t session_since_us = 0; + bool initialized = false; +}; + +struct TitleStateStore { + std::atomic sequence{0}; + std::atomic game{UINT64_MAX}; + std::atomic session{UINT64_MAX}; + std::atomic game_since_us{0}; + std::atomic session_since_us{0}; + std::atomic initialized{false}; +}; + +inline TitleStateStore& title_state_store() { + static TitleStateStore state; + return state; +} + +// Called by the runtime's local state-peek thread. RVAs and state values stay on the user's machine. +inline void update_title_state(uint64_t game, uint64_t session, uint64_t observed_us) { + TitleStateStore& state = title_state_store(); + state.sequence.fetch_add(1, std::memory_order_acq_rel); + if (!state.initialized.load(std::memory_order_relaxed) || state.game.load(std::memory_order_relaxed) != game) + state.game_since_us.store(observed_us, std::memory_order_relaxed); + if (!state.initialized.load(std::memory_order_relaxed) || state.session.load(std::memory_order_relaxed) != session) + state.session_since_us.store(observed_us, std::memory_order_relaxed); + state.game.store(game, std::memory_order_relaxed); + state.session.store(session, std::memory_order_relaxed); + state.initialized.store(true, std::memory_order_relaxed); + state.sequence.fetch_add(1, std::memory_order_release); +} + +inline TitleState current_title_state() { + TitleStateStore& state = title_state_store(); + TitleState result; + for (;;) { + const uint64_t before = state.sequence.load(std::memory_order_acquire); + if (before & 1) continue; + result.game = state.game.load(std::memory_order_relaxed); + result.session = state.session.load(std::memory_order_relaxed); + result.game_since_us = state.game_since_us.load(std::memory_order_relaxed); + result.session_since_us = state.session_since_us.load(std::memory_order_relaxed); + result.initialized = state.initialized.load(std::memory_order_relaxed); + if (before == state.sequence.load(std::memory_order_acquire)) return result; + } +} + struct Press { uint32_t button = 0; // a GameInput gamepad bit, or 0 for a stick throw int stick = 0; // +1 rsup, -1 rsdown (GameInput's +Y is up) - double at = 0; // seconds + double at = 0; // seconds, absolute or relative offset double hold = kHoldSeconds; + Anchor anchor = Anchor::Absolute; + uint64_t state = 0; // hexadecimal game/session state for a relative press }; struct Script { @@ -60,7 +121,8 @@ struct Script { bool empty() const { return presses.empty(); } }; -// The script in `text`, matched without regard to case. +// The script in `text`, matched without regard to case. Absolute entries keep their existing form; relative entries +// use button@game=+ or button@session=+ (for example a@session=d+5). inline Script parse(std::string text) { for (auto& c : text) c = static_cast(std::tolower(static_cast(c))); Script out; @@ -74,8 +136,21 @@ inline Script parse(std::string text) { const size_t first = item.find_first_not_of(whitespace); const size_t sep = item.find('@'); if (sep == std::string::npos || first >= sep) continue; + + Anchor anchor = Anchor::Absolute; + uint64_t state = 0; const char* time = item.c_str() + sep + 1; - char* tail; + if (std::strncmp(time, "game=", 5) == 0 || std::strncmp(time, "session=", 8) == 0) { + anchor = time[0] == 'g' ? Anchor::GameState : Anchor::SessionState; + time += anchor == Anchor::GameState ? 5 : 8; + if (*time == '-' || *time == '+') continue; + char* state_tail = nullptr; + errno = 0; + state = std::strtoull(time, &state_tail, 16); + if (state_tail == time || errno == ERANGE || *state_tail != '+') continue; + time = state_tail + 1; + } + char* tail = nullptr; const double seconds = std::strtod(time, &tail); if (tail == time || !std::isfinite(seconds) || seconds < 0) continue; double hold = kHoldSeconds; @@ -87,12 +162,18 @@ inline Script parse(std::string text) { if (item.find_first_not_of(whitespace, static_cast(tail - item.c_str())) != std::string::npos) continue; const auto is = [&](const char* name) { return item.compare(first, sep - first, name) == 0; }; if (is("rsup") || is("rsdown")) { - out.presses.push_back({0, is("rsup") ? 1 : -1, seconds, hold}); + Press press{0, is("rsup") ? 1 : -1, seconds, hold}; + press.anchor = anchor; + press.state = state; + out.presses.push_back(press); continue; } for (const Button& b : kButtons) { if (is(b.name)) { - out.presses.push_back({b.bit, 0, seconds, hold}); + Press press{b.bit, 0, seconds, hold}; + press.anchor = anchor; + press.state = state; + out.presses.push_back(press); out.named |= b.bit; break; } @@ -102,6 +183,24 @@ inline Script parse(std::string text) { return out; } +// The start of a press, relative to the script's clock. A state-relative entry is unavailable until the runtime's +// local state-peek thread has observed its anchor state. +inline std::optional start_at(const Press& press, uint64_t origin_us, const TitleState& title) { + if (press.anchor == Anchor::Absolute) return press.at; + if (!title.initialized) return std::nullopt; + uint64_t since_us = 0; + if (press.anchor == Anchor::GameState) { + if (title.game != press.state) return std::nullopt; + since_us = title.game_since_us; + } else { + if (title.session != press.state) return std::nullopt; + since_us = title.session_since_us; + } + const double since_script_start = since_us >= origin_us ? (since_us - origin_us) / 1e6 + : -static_cast(origin_us - since_us) / 1e6; + return std::max(0.0, since_script_start + press.at); +} + // What the script holds `seconds` after its clock started: the buttons (GameInput bits) and the right stick throw. // One decision per button: a later entry for the same button must not release what an earlier entry holds (it did: // with several entries only the last one ever reached the title). @@ -109,15 +208,19 @@ struct Held { uint32_t buttons = 0; int stick = 0; // +1 up, -1 down, 0 at rest }; -inline Held held_at(const Script& script, double seconds) { +inline Held held_at(const Script& script, double seconds, uint64_t origin_us, const TitleState& title) { Held h; for (const Press& p : script.presses) { - if (seconds < p.at || seconds >= p.at + p.hold) continue; + const std::optional start = start_at(p, origin_us, title); + if (!start || seconds < *start || seconds >= *start + p.hold) continue; if (p.stick) h.stick = p.stick; else h.buttons |= p.button; } return h; } +inline Held held_at(const Script& script, double seconds) { + return held_at(script, seconds, 0, {}); +} inline const Button* button_of(uint32_t bit) { for (const Button& b : kButtons) diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index ad17dd3b0d..e74ddb2dc2 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -38,6 +38,8 @@ #include +#include "../shims/gameinput.h" + namespace { // The retail title runs a hang watchdog thread: when the game's main loop has not set its "kick" byte for 30 @@ -108,13 +110,28 @@ void keep_title_watchdog_kicked(std::uint8_t* base) { // LEONIDA_PEEK=":3,," (diagnostic): a thread prints the 8-byte values at these RVAs of the // title's image (":n" = n consecutive values, at most 32; "RVA>OFF" reads through the pointer stored at the RVA, at -// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). For answering "what does this -// global hold at run time" without a debugger: the reverse-engineering notes (docs/re/requests.md) ask for exactly -// that. Reads are checked; nothing is written. +// pointer + OFF, both hex) every LEONIDA_PEEK_SECONDS (default 10). When both local state RVAs are set, it also sends +// the sampled game/session states to GameInput for state-relative scripted presses; nothing is written to the title. void start_title_peek(std::uint8_t* base) { - static char spec[1024]; + static char spec[1024]{}; const DWORD n = GetEnvironmentVariableA("LEONIDA_PEEK", spec, sizeof(spec)); - if (!n || n >= sizeof(spec)) return; + static bool have_peek = false; + have_peek = n && n < sizeof(spec); + auto state_rva = [](const char* name, std::uint64_t* out) { + char text[32]{}; + const DWORD size = GetEnvironmentVariableA(name, text, sizeof(text)); + if (!size || size >= sizeof(text)) return false; + char* end = nullptr; + const uint64_t value = std::strtoull(text, &end, 16); + if (end == text || *end) return false; + *out = value; + return true; + }; + static std::uint64_t game_state_rva = 0, session_state_rva = 0; + static bool have_title_state = false; + have_title_state = state_rva("LEONIDA_GAME_STATE_RVA", &game_state_rva) && + state_rva("LEONIDA_SESSION_STATE_RVA", &session_state_rva); + if (!have_peek && !have_title_state) return; static std::uint8_t* image = base; const HANDLE thread = CreateThread( nullptr, 0, @@ -123,50 +140,66 @@ void start_title_peek(std::uint8_t* base) { DWORD period = 10; if (GetEnvironmentVariableA("LEONIDA_PEEK_SECONDS", seconds, sizeof(seconds)) > 0) period = std::max(1, static_cast(std::strtoul(seconds, nullptr, 10))); + if (have_title_state) period = 1; // state-relative presses use a bounded, one-second observation interval for (unsigned tick = 0;; ++tick) { std::string line = "[rt] peek t=" + std::to_string(tick * period) + "s:"; - for (const char* at = spec; *at;) { - char* end = nullptr; - const std::uint64_t rva = std::strtoull(at, &end, 16); - if (end == at) break; - // "RVA>OFF": the 8-byte pointer at the RVA, then the values at pointer + OFF (both hex). - bool deref = false; - std::uint64_t offset = 0; - if (*end == '>') { - deref = true; - offset = std::strtoull(end + 1, &end, 16); - } - unsigned count = 1; - if (*end == ':') count = static_cast(std::strtoul(end + 1, &end, 10)); - char text[48]; - if (deref) - std::snprintf(text, sizeof(text), " 0x%llx>%llx=", static_cast(rva), - static_cast(offset)); - else - std::snprintf(text, sizeof(text), " 0x%llx=", static_cast(rva)); - line += text; - const std::uint8_t* from = image + rva; - if (deref) { - std::uint64_t pointer = 0; - SIZE_T got = 0; - if (!ReadProcessMemory(GetCurrentProcess(), from, &pointer, sizeof(pointer), &got) || - got != sizeof(pointer) || !pointer) - count = 0, line += "?"; - from = reinterpret_cast(pointer + offset); - } - for (unsigned i = 0; i < count && i < 32; ++i) { - std::uint64_t value = 0; - SIZE_T got = 0; - const bool ok = ReadProcessMemory(GetCurrentProcess(), from + 8ull * i, &value, - sizeof(value), &got) && got == sizeof(value); - std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "", - static_cast(value)); + if (have_peek) { + for (const char* at = spec; *at;) { + char* end = nullptr; + const std::uint64_t rva = std::strtoull(at, &end, 16); + if (end == at) break; + // "RVA>OFF": the 8-byte pointer at the RVA, then the values at pointer + OFF (both hex). + bool deref = false; + std::uint64_t offset = 0; + if (*end == '>') { + deref = true; + offset = std::strtoull(end + 1, &end, 16); + } + unsigned count = 1; + if (*end == ':') count = static_cast(std::strtoul(end + 1, &end, 10)); + char text[48]; + if (deref) + std::snprintf(text, sizeof(text), " 0x%llx>%llx=", static_cast(rva), + static_cast(offset)); + else + std::snprintf(text, sizeof(text), " 0x%llx=", static_cast(rva)); line += text; + const std::uint8_t* from = image + rva; + if (deref) { + std::uint64_t pointer = 0; + SIZE_T got = 0; + if (!ReadProcessMemory(GetCurrentProcess(), from, &pointer, sizeof(pointer), &got) || + got != sizeof(pointer) || !pointer) + count = 0, line += "?"; + from = reinterpret_cast(pointer + offset); + } + for (unsigned i = 0; i < count && i < 32; ++i) { + std::uint64_t value = 0; + SIZE_T got = 0; + const bool ok = ReadProcessMemory(GetCurrentProcess(), from + 8ull * i, &value, + sizeof(value), &got) && got == sizeof(value); + std::snprintf(text, sizeof(text), ok ? "%s%llx" : "%s?", i ? "," : "", + static_cast(value)); + line += text; + } + at = *end == ',' ? end + 1 : end; + if (at == end && *end) break; } - at = *end == ',' ? end + 1 : end; - if (at == end && *end) break; + std::fprintf(stderr, "%s\n", line.c_str()); + } + if (have_title_state) { + std::uint64_t game = 0, session = 0; + SIZE_T game_bytes = 0, session_bytes = 0; + const bool game_ok = ReadProcessMemory(GetCurrentProcess(), image + game_state_rva, &game, + sizeof(game), &game_bytes) && game_bytes == sizeof(game); + const bool session_ok = ReadProcessMemory(GetCurrentProcess(), image + session_state_rva, &session, + sizeof(session), &session_bytes) && + session_bytes == sizeof(session); + if (game_ok && session_ok) + rt::gameinput::update_title_state(static_cast(game), + static_cast(session), + GetTickCount64() * 1000); } - std::fprintf(stderr, "%s\n", line.c_str()); Sleep(period * 1000); } }, diff --git a/runtime/test/gameinput_no_sdl_test.cpp b/runtime/test/gameinput_no_sdl_test.cpp index e292c61297..ea51a18dbb 100644 --- a/runtime/test/gameinput_no_sdl_test.cpp +++ b/runtime/test/gameinput_no_sdl_test.cpp @@ -13,6 +13,7 @@ #include #include +#include "gameinput.h" #include "gameinput_abi.h" #include "gameinput_no_sdl.h" #include "shims.h" @@ -98,10 +99,10 @@ Pad read_at(IGameInput* input, IGameInputDevice* device, double seconds) { } int scripted_pad(CreateFn create) { - // Six presses (two overlapping A presses, Menu, B held 0.8 s, a right-stick throw, a Y press the title never reads - // while it is down) and seven rejected entries (four of them holds that are zero, negative, missing or too long). + // Eight presses (the six absolute cases plus game- and session-relative presses) and eight rejected entries, + // including a malformed state-relative entry. Both relative presses are checked against synthetic state changes. SetEnvironmentVariableA("LEONIDA_INPUT_SCRIPT", - "a@1, START@2.5 ,a@1.2,b@3:0.8 ,rsup@4,y@10,bogus@1,a@-1,,x@3:0,x@3:-1,x@3:,x@3:601"); + "a@1, START@2.5 ,a@1.2,b@3:0.8 ,rsup@4,y@10,x@session=d+1:0.6,a@game=4+2:0.6,bogus@1,a@-1,,x@3:0,x@3:-1,x@3:,x@3:601,b@session=z+1"); rt::gameinput_no_sdl::set_clock(fake_clock); at(0); @@ -257,6 +258,48 @@ int scripted_pad(CreateFn create) { now->Release(); } + // A session state seen at 61 s schedules X one second later. The next title read is at 63 s, after the 0.6 s + // press has ended; its press and release must still be in the history in order. + at(61.0); + rt::gameinput::update_title_state(3, 0xd, g_now_us); + const Pad after_relative = read_at(input, device, 63.0); + check(after_relative.buttons == 0, "script: a state-relative press is released by the later sparse read"); + IGameInputReading* relative_release = nullptr; + IGameInputReading* relative_press = nullptr; + GameInputGamepadState relative_state{}; + check(input->GetCurrentReading(GameInputKindGamepad, device, &relative_release) == S_OK && relative_release && + relative_release->GetGamepadState(&relative_state) && relative_state.buttons == 0 && + relative_release->GetTimestamp() == 7'000'000 + 62'600'000, + "script: the state-relative release is recorded at its scheduled time"); + if (relative_release) { + check(input->GetPreviousReading(relative_release, GameInputKindGamepad, device, &relative_press) == S_OK && + relative_press && relative_press->GetGamepadState(&relative_state) && + relative_state.buttons == GameInputGamepadX && + relative_press->GetTimestamp() == 7'000'000 + 62'000'000, + "script: the state-relative press survives reads that miss its whole hold"); + } + if (relative_press) relative_press->Release(); + if (relative_release) relative_release->Release(); + + at(65.0); + rt::gameinput::update_title_state(4, 0xd, g_now_us); + check(read_at(input, device, 68.0).buttons == 0, "script: a game-state-relative press is released by a sparse read"); + relative_release = nullptr; + relative_press = nullptr; + check(input->GetCurrentReading(GameInputKindGamepad, device, &relative_release) == S_OK && relative_release && + relative_release->GetGamepadState(&relative_state) && relative_state.buttons == 0 && + relative_release->GetTimestamp() == 7'000'000 + 67'600'000, + "script: the game-state-relative release is recorded at its scheduled time"); + if (relative_release) { + check(input->GetPreviousReading(relative_release, GameInputKindGamepad, device, &relative_press) == S_OK && + relative_press && relative_press->GetGamepadState(&relative_state) && + relative_state.buttons == GameInputGamepadA && + relative_press->GetTimestamp() == 7'000'000 + 67'000'000, + "script: the game-state-relative press is scheduled from the observed transition"); + } + if (relative_press) relative_press->Release(); + if (relative_release) relative_release->Release(); + IGameInputReading* motion = nullptr; check(input->GetCurrentReading(GameInputKindMotion, nullptr, &motion) == GAMEINPUT_E_READING_NOT_FOUND && !motion, "script: the pad has no motion readings"); diff --git a/scripts/windows-boot/README.md b/scripts/windows-boot/README.md index 1d3328ec8e..e16f2159e9 100644 --- a/scripts/windows-boot/README.md +++ b/scripts/windows-boot/README.md @@ -54,7 +54,7 @@ Only when your shell has not set them already (`-DryRun` prints the final list): | `LEONIDA_STARTUP_CACHE_CHECKPOINTS` | `1` | save the pipeline cache during startup | | `LEONIDA_PIPELINE_CACHE_DIR` | `\pipeline-cache` | one warmed cache shared by every build you boot | | `LEONIDA_PIPELINE_JOURNAL_SEED_DIR` | `pipeline_cache.d` beside `gta6rt.exe`, when it exists | the journal `gta6rt --precompile-pipelines` fills there when it runs without `LEONIDA_PIPELINE_CACHE_DIR`, read-only, so its pipelines are not compiled again. Not set when the boot's own journal (`\pipeline_cache.d`, or beside `gta6rt.exe` when the cache folder is empty) is that folder or one holds the other, compared through Windows' final paths (so a junction, symbolic link, substituted or mapped drive, 8.3 name or `\\?\` / `\\.\` prefix counts as the same folder), or when the cache folder is a relative path. `LEONIDA_PIPELINE_JOURNAL_SEED_DIR=` (empty) turns the seed off. If the runtime still logs `[rt] d3d12: invalid or overlapping compute journal directories; journal disabled`, set it so. On NVIDIA, `LEONIDA_DEVICE_FAULT=1` puts the diagnostics config into the journal's file names, so the pre-compile and the boot have to agree on it; this script sets `2`, which names them as a pre-compile without the variable does, except with `LEONIDA_AFTERMATH=1` (see `LEONIDA_DEVICE_FAULT`) | -| `LEONIDA_PEEK` / `_SECONDS` | the game-state and session-state RVAs from `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA` (measured locally, not kept here; unset: no peek) / `5` | game state and session state (low dword), the two numbers every RESULT reports | +| `LEONIDA_PEEK` / `_SECONDS` | the game-state and session-state RVAs from `LEONIDA_GAME_STATE_RVA` and `LEONIDA_SESSION_STATE_RVA` (measured locally, not kept here; unset: no peek) / `5` | game state and session state (low dword), the two numbers every RESULT reports. When an input script uses `@game=+seconds` or `@session=+seconds`, both RVAs are required and the runtime samples them once a second to schedule those presses. | | `LEONIDA_DEVICE_FAULT` | `2` | on device loss: `VK_EXT_device_fault` info, NVIDIA checkpoints, last replay steps, without NVIDIA's diagnostics config, so a pre-compiled compute journal still matches (that config is part of every compute journal file name). `1` adds the config (shader debug info in a crash dump); `LEONIDA_DEVICE_FAULT=1` turns it on for a boot, and the script uses `1` by itself when `LEONIDA_AFTERMATH=1` and you did not set this variable. Under `2`, "NVIDIA diagnostics config unavailable" in the runtime's `[rt] device-fault: enabled` line means it was not requested, not a driver problem | | `LEONIDA_KEYBOARD_PAD` | `always` (with an input script) | keyboard as a pad even unfocused, so the script's presses arrive | | `LEONIDA_CAPTURE_EVERY` / `_LIMIT` | `60` / `40` | a swapchain read-back every 60 presents, at most 40 | diff --git a/scripts/windows-boot/boot.ps1 b/scripts/windows-boot/boot.ps1 index 98be96e929..50f805745b 100644 --- a/scripts/windows-boot/boot.ps1 +++ b/scripts/windows-boot/boot.ps1 @@ -49,9 +49,10 @@ Writable folder for everything the title writes (saves, caches). Default \overlay. .PARAMETER InputScript - LEONIDA_INPUT_SCRIPT: timed pad presses, seconds since the pad's first poll (button@seconds, comma-separated; - buttons a b x y start back lb rb up down left right). Each press is held 300 ms; button@seconds:hold holds it - longer, e.g. b@1100:3 for a title that reads its pad only every few seconds. Sets LEONIDA_KEYBOARD_PAD=always. Builds older than + LEONIDA_INPUT_SCRIPT: timed pad presses (button@seconds) or state-relative presses + (button@game=+seconds, button@session=+seconds), comma-separated; buttons a b x y start back lb rb up + down left right. State-relative presses need LEONIDA_GAME_STATE_RVA and LEONIDA_SESSION_STATE_RVA. Each press is + held 300 ms; :hold extends it, e.g. b@1100:3. Sets LEONIDA_KEYBOARD_PAD=always. Builds older than main @ 8146ef690 ignore a script longer than 63 characters. Default 'a@45,a@60,a@75,a@90'. .PARAMETER NoInput