diff --git a/scripts/windows-boot/README.md b/scripts/windows-boot/README.md index 3a875af4b..cd31cb3ca 100644 --- a/scripts/windows-boot/README.md +++ b/scripts/windows-boot/README.md @@ -76,6 +76,10 @@ How to read the states: game state `0x7446510` goes 1, 2 (init), 3 (running); se screen waiting for A), `d` after A. `docs/linux_proton.md` (the same stages apply natively) section 2 has the healthy sequence with times and section 3 "which blank do you have". +To pair scripted presses with state changes after a run, use `python -I tools/stage_timeline.py LOG` with the same +two environment variables. For a saved log, `--game-state-rva` and `--session-state-rva` override them. If the log +also records the loading-screen state, pass its hex RVA with `--load-state-rva` to see when the screen ended. + ## One game at a time The script holds the named mutex `Local\LeonidaGameBoot` for the whole boot and also waits while any `gta6rt.exe` diff --git a/tests/test_stage_timeline_rvas.py b/tests/test_stage_timeline_rvas.py new file mode 100644 index 000000000..c733c3eff --- /dev/null +++ b/tests/test_stage_timeline_rvas.py @@ -0,0 +1,75 @@ +"""Exercise the boot timeline CLI with synthetic peek fields.""" +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + + +TOOL = Path(__file__).resolve().parents[1] / "tools" / "stage_timeline.py" +GAME = "LEONIDA_GAME_STATE_RVA" +SESSION = "LEONIDA_SESSION_STATE_RVA" + + +class TimelineRvas(unittest.TestCase): + def run_tool(self, log, *args, **settings): + env = os.environ.copy() + for key in (GAME, SESSION): + env.pop(key, None) + env.update(settings) + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) / "boot.log" + path.write_text(log, encoding="utf-8") + return subprocess.run([sys.executable, "-I", str(TOOL), str(path), *args], + capture_output=True, text=True, env=env) + + def test_settings_pair_press_and_ignore_unrelated_samples(self): + log = ("[rt] peek t=1s: 0x001a=2 0x2b=1\n" + "[rt] gameinput: scripted press of button 4 at 1.5 s\n" + "[rt] peek t=2s: 0x99=5\n" + "[rt] peek t=3s: 0x2B=100000003,dead 0x1A=3\n") + run = self.run_tool(log, "--json", **{GAME: "0X1A", SESSION: "002b"}) + self.assertEqual(run.returncode, 0, run.stderr) + data = json.loads(run.stdout) + self.assertEqual(data["stages"], [{"t": 1, "stage": "init"}, {"t": 3, "stage": "landing"}]) + self.assertEqual(data["presses"], [{"t": 1.5, "button": 4, "next_change": data["stages"][1]}]) + + def test_overrides_and_optional_loading_byte(self): + log = ("[rt] peek t=1s: 0x10=3 0x20=0 0x30=102\n" + "[rt] peek t=2s: 0x10=3 0x20=0 0x30=100\n") + settings = {GAME: "aa", SESSION: "bb"} + args = ("--game-state-rva", "10", "--session-state-rva", "0x20") + for extra, ended in [((), False), (("--load-state-rva", "30"), True)]: + with self.subTest(extra=extra): + run = self.run_tool(log, *args, *extra, **settings) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertIn("story load", run.stdout) + self.assertEqual("loading screen ended" in run.stdout, ended) + + def test_unmatched_or_absent_peeks_report_an_error(self): + for log in ("[rt] peek t=1s: 0xa=3 0xb=0\n", "no samples\n"): + with self.subTest(log=log): + run = self.run_tool(log, "--json", **{GAME: "10", SESSION: "20"}) + self.assertEqual(run.returncode, 1) + self.assertEqual(run.stdout, "") + self.assertIn("LEONIDA_PEEK", run.stderr) + + def test_unreadable_sample_is_still_a_match(self): + run = self.run_tool("[rt] peek t=1s: 0x10=? 0x20=?\n", "--json", **{GAME: "10", SESSION: "20"}) + self.assertEqual(run.returncode, 0, run.stderr) + self.assertEqual(json.loads(run.stdout)["stages"], [{"t": 1, "stage": "no title state yet"}]) + + def test_missing_or_invalid_configuration(self): + log = "[rt] peek t=1s: 0x10=2\n" + missing = self.run_tool(log) + self.assertEqual(missing.returncode, 2) + self.assertIn(GAME, missing.stderr) + invalid = self.run_tool(log, "--game-state-rva", "oops", **{SESSION: "20"}) + self.assertEqual(invalid.returncode, 2) + self.assertIn("hex", invalid.stderr) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/stage_timeline.py b/tools/stage_timeline.py index 9b37373f1..4427e5dcc 100644 --- a/tools/stage_timeline.py +++ b/tools/stage_timeline.py @@ -2,23 +2,18 @@ """Stage timeline of a boot from the runtime's stderr log: the title's own state (LEONIDA_PEEK lines) joined with the scripted presses (LEONIDA_INPUT_SCRIPT), so a Confirm can be paired with the state change it caused. -Reads only the runtime's own lines: - [rt] peek t=s: 0x7446510= 0x98f828c= [0x7453ed0=] ... - [rt] gameinput: scripted press of button at s - -State meanings for this build: game state 0x7446510 (2 init, 3 running); session = low dword of 0x98f828c -(1 boot screens, 3 landing, 0xd first-run pages, 0 story load); low byte of 0x7453ed0 goes 2 -> 0 when the loading -screen ends. - -usage: stage_timeline.py LOG [--json] +Use the same hexadecimal game and session state RVAs as the boot that produced the log. +For an older log, pass its RVAs on the command line. If LEONIDA_PEEK also recorded the +loading-screen state, --load-state-rva lets the timeline mark its low byte changing from 2 to 0. """ import argparse import json +import os import re import sys PEEK = re.compile(r"\[rt\] peek t=(\d+)s:(.*)") -FIELD = re.compile(r"0x([0-9a-fA-F]+)=([0-9a-fA-F?,]+)") +FIELD = re.compile(r"0[xX]([0-9a-fA-F]+)=([0-9a-fA-F?,]+)") PRESS = re.compile(r"\[rt\] gameinput: scripted press of button (\d+) at ([0-9.]+) s") SESSION = {1: "boot screens", 3: "landing", 0xD: "first-run pages", 0: "story load"} @@ -31,17 +26,25 @@ def first_value(text): return int(text.split(",")[0], 16) -def read(path): +def hex_rva(text): + if not re.fullmatch(r"(?:0[xX])?[0-9a-fA-F]+", text): + raise argparse.ArgumentTypeError("expected a hexadecimal RVA, with or without 0x") + return int(text, 16) + + +def read(path, game_rva, session_rva, load_rva): peeks, presses = [], [] with open(path, encoding="utf-8", errors="replace") as f: for line in f: m = PEEK.search(line) if m: - fields = {k.lower(): v for k, v in FIELD.findall(m.group(2))} - game = first_value(fields.get("7446510")) - raw = first_value(fields.get("98f828c")) + fields = {int(k, 16): v for k, v in FIELD.findall(m.group(2))} + if fields.keys().isdisjoint((game_rva, session_rva, load_rva)): + continue + game = first_value(fields.get(game_rva)) + raw = first_value(fields.get(session_rva)) session = None if raw is None else raw & 0xFFFFFFFF - load = first_value(fields.get("7453ed0")) + load = first_value(fields.get(load_rva)) peeks.append({"t": int(m.group(1)), "game": game, "session": session, "load": load}) continue m = PRESS.search(line) @@ -76,10 +79,19 @@ def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument("log") ap.add_argument("--json", action="store_true", help="print the stages and presses as JSON") + ap.add_argument("--game-state-rva", type=hex_rva, default=os.environ.get("LEONIDA_GAME_STATE_RVA"), + help="game state RVA (defaults to LEONIDA_GAME_STATE_RVA)") + ap.add_argument("--session-state-rva", type=hex_rva, default=os.environ.get("LEONIDA_SESSION_STATE_RVA"), + help="session state RVA (defaults to LEONIDA_SESSION_STATE_RVA)") + ap.add_argument("--load-state-rva", type=hex_rva, help="optional loading-screen state RVA") a = ap.parse_args() - peeks, presses = read(a.log) + if a.game_state_rva is None or a.session_state_rva is None: + ap.error("set LEONIDA_GAME_STATE_RVA and LEONIDA_SESSION_STATE_RVA or pass " + "--game-state-rva and --session-state-rva") + peeks, presses = read(a.log, a.game_state_rva, a.session_state_rva, a.load_state_rva) if not peeks: - print("no peek lines: run with LEONIDA_PEEK=0x7446510,0x98f828c,0x7453ed0", file=sys.stderr) + print("no peek fields matched the configured state RVAs; check the RVAs and the boot's LEONIDA_PEEK setting", + file=sys.stderr) return 1 events = timeline(peeks, presses) if a.json: