From 39b900ad7781bc1bbdeb5696f8d8a1f5a6ac5af5 Mon Sep 17 00:00:00 2001 From: OpenAI Codex Date: Thu, 26 Nov 2026 13:19:28 +1000 Subject: [PATCH] tools: read boot timeline states from the configured RVAs Use the state addresses supplied to the boot script, with command-line overrides for saved logs. Retain #1789's input validation and file-error behavior. Adapt both parser test suites to synthetic addresses. Original implementation AI-assisted by OpenAI Codex (gpt-6.2-astra). Upstream reconciliation AI-assisted by OpenAI Codex (GPT-6). --- scripts/windows-boot/README.md | 4 ++ tests/test_stage_timeline.py | 37 +++++++-------- tests/test_stage_timeline_rvas.py | 75 +++++++++++++++++++++++++++++++ tools/stage_timeline.py | 46 ++++++++++++------- 4 files changed, 127 insertions(+), 35 deletions(-) create mode 100644 tests/test_stage_timeline_rvas.py diff --git a/scripts/windows-boot/README.md b/scripts/windows-boot/README.md index 3a875af4bb..cd31cb3ca2 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.py b/tests/test_stage_timeline.py index a1a1412f79..01d905d589 100644 --- a/tests/test_stage_timeline.py +++ b/tests/test_stage_timeline.py @@ -11,7 +11,6 @@ import sys import tempfile import unittest -from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOLS = os.path.join(ROOT, "tools") @@ -22,25 +21,27 @@ import stage_timeline # noqa: E402 +RVA_ARGS = ("--game-state-rva", "1a", "--session-state-rva", "2b", "--load-state-rva", "3c") + + class StageTimelineTests(unittest.TestCase): def test_parses_boot_timeline_and_pairs_presses_json(self): log = "\n".join([ - "[rt] peek t=1s: 0x7446510=2,0 0x98f828c=1,0 0x7453ed0=0,0", - "[rt] peek t=3s: 0x7446510=3,0 0x98f828c=1,0 0x7453ed0=0,0", + "[rt] peek t=1s: 0x1a=2,0 0x2b=1,0 0x3c=0,0", + "[rt] peek t=3s: 0x1a=3,0 0x2b=1,0 0x3c=0,0", "[rt] gameinput: scripted press of button 1 at 4.0 s", - "[rt] peek t=5s: 0x7446510=3,0 0x98f828c=3,0 0x7453ed0=0,0", + "[rt] peek t=5s: 0x1a=3,0 0x2b=3,0 0x3c=0,0", "[rt] gameinput: scripted press of button 1 at 6.5 s", - "[rt] peek t=8s: 0x7446510=3,0 0x98f828c=0,0 0x7453ed0=2,0", - "[rt] peek t=10s: 0x7446510=3,0 0x98f828c=0,0 0x7453ed0=0,0", + "[rt] peek t=8s: 0x1a=3,0 0x2b=0,0 0x3c=2,0", + "[rt] peek t=10s: 0x1a=3,0 0x2b=0,0 0x3c=0,0", ]) + "\n" with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "boot.log") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(log) out = io.StringIO() - with mock.patch.object(sys, "argv", ["stage_timeline.py", path, "--json"]), \ - contextlib.redirect_stdout(out): - rc = stage_timeline.main() + with contextlib.redirect_stdout(out): + rc = stage_timeline.main([path, "--json", *RVA_ARGS]) self.assertEqual(rc, 0) doc = json.loads(out.getvalue()) self.assertEqual(doc["stages"], [ @@ -63,7 +64,7 @@ def test_first_value_handles_empty_leading_comma_and_partial_unknowns(self): def test_malformed_press_timestamp_does_not_crash_read(self): log = "\n".join([ - "[rt] peek t=2s: 0x7446510=3,0 0x98f828c=1,0", + "[rt] peek t=2s: 0x1a=3,0 0x2b=1,0", "[rt] gameinput: scripted press of button 1 at . s", "[rt] gameinput: scripted press of button 1 at 1.2.3 s", "[rt] gameinput: scripted press of button 2 at 3 s", @@ -72,7 +73,7 @@ def test_malformed_press_timestamp_does_not_crash_read(self): path = os.path.join(tmp, "boot.log") with open(path, "w", encoding="utf-8", newline="\n") as f: f.write(log) - peeks, presses = stage_timeline.read(path) + peeks, presses = stage_timeline.read(path, 0x1a, 0x2b, 0x3c) self.assertEqual(len(peeks), 1) self.assertEqual(presses, [{"t": 3.0, "button": 2}]) @@ -93,8 +94,8 @@ def test_utf8_bom_log_is_read(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "bom.log") with open(path, "w", encoding="utf-8-sig", newline="\n") as f: - f.write("[rt] peek t=1s: 0x7446510=2,0 0x98f828c=1,0\n") - peeks, _ = stage_timeline.read(path) + f.write("[rt] peek t=1s: 0x1a=2,0 0x2b=1,0\n") + peeks, _ = stage_timeline.read(path, 0x1a, 0x2b, 0x3c) self.assertEqual(len(peeks), 1) self.assertEqual(peeks[0]["game"], 2) @@ -104,17 +105,17 @@ def test_missing_or_directory_log_returns_exit_2_without_traceback(self): for target in (missing, tmp): with self.subTest(target=target): p = subprocess.run( - [sys.executable, "-I", TOOL, target], + [sys.executable, "-I", TOOL, target, *RVA_ARGS], capture_output=True, encoding="utf-8", errors="replace", ) self.assertEqual(p.returncode, 2, p.stdout + p.stderr) self.assertNotIn("Traceback", p.stderr) - self.assertTrue(p.stderr.strip()) + self.assertIn(target, p.stderr) err = io.StringIO() - with mock.patch.object(sys, "argv", ["stage_timeline.py", target]), \ - contextlib.redirect_stderr(err): - rc = stage_timeline.main() + with contextlib.redirect_stderr(err): + rc = stage_timeline.main([target, *RVA_ARGS]) self.assertEqual(rc, 2) + self.assertIn(target, err.getvalue()) def test_main_block_guards_stdout_reconfigure(self): with open(TOOL, encoding="utf-8") as f: diff --git a/tests/test_stage_timeline_rvas.py b/tests/test_stage_timeline_rvas.py new file mode 100644 index 0000000000..c733c3eff7 --- /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 e0c6c1b506..ebe4f9e039 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]+(?:\.[0-9]+)?) s") SESSION = {1: "boot screens", 3: "landing", 0xD: "first-run pages", 0: "story load"} @@ -37,17 +32,25 @@ def first_value(text): return None -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-sig", 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) @@ -82,14 +85,23 @@ def main(argv=None): 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(argv) + 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") try: - peeks, presses = read(a.log) + peeks, presses = read(a.log, a.game_state_rva, a.session_state_rva, a.load_state_rva) except OSError as e: print(f"stage_timeline: {a.log}: {e.strerror or e}", file=sys.stderr) return 2 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: