diff --git a/games/oot/runtime/input_tape.h b/games/oot/runtime/input_tape.h deleted file mode 100644 index 5fc6a7fc3..000000000 --- a/games/oot/runtime/input_tape.h +++ /dev/null @@ -1,111 +0,0 @@ -/* The controller state the game reads, recorded and replayed (2026-11-18, user: "不是让你录制了吗 我的路线和操作", - "用我的 路线 运行"): a test plays the user's own route and actions again. - - OOT_INPUT_RECORD=: each change of the state pad_states hands the game (buttons and the four stick axes, after the - keyboard, the controller and the input file are merged) is a line, by frame. Frames are counted in two segments so a - load that takes longer on a later run does not shift what follows: B, game frames presented since the start - (dm_nvn_frame_count), until the world is entered; W, frames since the first world frame (mtl_world_frame, the - gate hle_nvn.c uses for its warm-up wait). The state is written again when the segment changes, and a heartbeat line - every 600 frames marks how far the recording went. - a state, from this frame on - H the recording reached this frame - OOT_INPUT_REPLAY=: the game reads the recorded state instead (the real keyboard and controller are ignored), - and 120 frames after the recording's last line the game closes normally (the input file's "close"). - - The game itself is not deterministic (its own timing, loading), so a replay follows the route closely, not exactly. */ -#pragma once -#include -#include -#include -#include -#include -#include - -__attribute__((weak)) uint64_t dm_nvn_frame_count(void); /* gfx/nvn/hle_nvn.c */ -__attribute__((weak)) uint64_t mtl_world_frame(void); /* gfx/nvn/nvn_mtl.m: the first world frame, 0 before */ -void runtime_front_request_close(void); /* frontend.m */ - -typedef struct { char seg; uint64_t frame, buttons; int32_t lx, ly, rx, ry; } TapeEvent; -static pthread_mutex_t g_tape_mu = PTHREAD_MUTEX_INITIALIZER; -static int g_tape_mode = -1; /* 0 off, 1 record, 2 replay */ -static FILE *g_tape_out; -static TapeEvent g_tape_last; static int g_tape_have_last; static char g_tape_last_seg; static uint64_t g_tape_beat; -static TapeEvent *g_tape; static size_t g_tape_n; -static char g_tape_end_seg; static uint64_t g_tape_end_frame; static int g_tape_closed; - -static void tape_where(char *seg, uint64_t *frame) { - uint64_t now = dm_nvn_frame_count ? dm_nvn_frame_count() : 0, w = mtl_world_frame ? mtl_world_frame() : 0; - if (w && now >= w) { *seg = 'W'; *frame = now - w; } else { *seg = 'B'; *frame = now; } -} -static void tape_open(void) { - const char *rec = getenv("OOT_INPUT_RECORD"), *rep = getenv("OOT_INPUT_REPLAY"); - g_tape_mode = 0; - if (rep && *rep) { - FILE *f = fopen(rep, "r"); - if (!f) { fprintf(stderr, "[input-tape] 【红】cannot read %s: no replay\n", rep); return; } - size_t cap = 1024; g_tape = (TapeEvent *)malloc(cap * sizeof *g_tape); - char line[256]; - while (g_tape && fgets(line, sizeof line, f)) { - TapeEvent e; char seg; unsigned long long fr, b; - /* the file is in the order it was played: its last line, a state or a heartbeat, is where the recording ended */ - if (line[0] == 'H' && sscanf(line, "H %c %llu", &seg, &fr) == 2) { g_tape_end_seg = seg; g_tape_end_frame = fr; continue; } - if (sscanf(line, "%c %llu %llx %d %d %d %d", &seg, &fr, &b, &e.lx, &e.ly, &e.rx, &e.ry) != 7 || (seg != 'B' && seg != 'W')) continue; - e.seg = seg; e.frame = fr; e.buttons = b; - if (g_tape_n == cap) { TapeEvent *g = (TapeEvent *)realloc(g_tape, (cap *= 2) * sizeof *g_tape); if (!g) break; g_tape = g; } - g_tape[g_tape_n++] = e; - g_tape_end_seg = seg; g_tape_end_frame = fr; - } - fclose(f); - if (!g_tape_n) { fprintf(stderr, "[input-tape] 【红】%s holds no state: no replay\n", rep); return; } - size_t w = 0; for (size_t i = 0; i < g_tape_n; i++) w += g_tape[i].seg == 'W'; - fprintf(stderr, "[input-tape] replaying %s: %zu states before the world, %zu in it, to %c %" PRIu64 "\n", rep, g_tape_n - w, w, g_tape_end_seg, g_tape_end_frame); - g_tape_mode = 2; - } else if (rec && *rec) { - g_tape_out = fopen(rec, "w"); - if (!g_tape_out) { fprintf(stderr, "[input-tape] 【红】cannot write %s: no recording\n", rec); return; } - fprintf(g_tape_out, "# OOT controller states (games/oot/runtime/input_tape.h): \n"); - fflush(g_tape_out); - fprintf(stderr, "[input-tape] recording the controller to %s\n", rec); - g_tape_mode = 1; - } -} -/* pad_states, with the state it is about to hand the game */ -static void input_tape(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { - pthread_mutex_lock(&g_tape_mu); - if (g_tape_mode < 0) tape_open(); - if (!g_tape_mode) { pthread_mutex_unlock(&g_tape_mu); return; } - char seg; uint64_t frame; tape_where(&seg, &frame); - if (g_tape_mode == 1) { - TapeEvent e = { seg, frame, *buttons, *lx, *ly, *rx, *ry }; - int changed = !g_tape_have_last || seg != g_tape_last_seg || e.buttons != g_tape_last.buttons || e.lx != g_tape_last.lx || - e.ly != g_tape_last.ly || e.rx != g_tape_last.rx || e.ry != g_tape_last.ry; - if (changed) { - fprintf(g_tape_out, "%c %" PRIu64 " %" PRIx64 " %d %d %d %d\n", seg, frame, e.buttons, e.lx, e.ly, e.rx, e.ry); - g_tape_last = e; g_tape_have_last = 1; g_tape_last_seg = seg; - } - if (frame / 600 != g_tape_beat || changed) { - if (frame / 600 != g_tape_beat) { g_tape_beat = frame / 600; fprintf(g_tape_out, "H %c %" PRIu64 "\n", seg, frame); } - fflush(g_tape_out); /* a close or a crash keeps what was played */ - } - } else { - /* the latest state at or before this point: the tape is in time order and B comes before W, so a cursor moves on */ - static size_t at; static int started; - while (at + (size_t)started < g_tape_n) { - const TapeEvent *next = &g_tape[at + (size_t)started]; - if (!(next->seg < seg || (next->seg == seg && next->frame <= frame))) break; - at += (size_t)started; started = 1; - } - const TapeEvent *use = started && g_tape[at].seg == seg ? &g_tape[at] : NULL; /* in the world, only the world's */ - /* OOT_INPUT_REPLAY_LIVE=1 (tests that drive the controllers themselves): in the world the live state stays, the tape only adds to it */ - static int live = -1; if (live < 0) { const char *e = getenv("OOT_INPUT_REPLAY_LIVE"); live = e && *e == '1'; } - if (live && seg == 'W') { if (use) { *buttons |= use->buttons; if (use->lx) *lx = use->lx; if (use->ly) *ly = use->ly; if (use->rx) *rx = use->rx; if (use->ry) *ry = use->ry; } } - else if (use) { *buttons = use->buttons; *lx = use->lx; *ly = use->ly; *rx = use->rx; *ry = use->ry; } - else { *buttons = 0; *lx = *ly = *rx = *ry = 0; } - if (!g_tape_closed && seg == g_tape_end_seg && frame > g_tape_end_frame + 120) { - g_tape_closed = 1; - fprintf(stderr, "[input-tape] the recording ended (%c %" PRIu64 "): closing the game\n", seg, g_tape_end_frame); - runtime_front_request_close(); - } - } - pthread_mutex_unlock(&g_tape_mu); -} diff --git a/games/oot/runtime/input_tape.h b/games/oot/runtime/input_tape.h new file mode 120000 index 000000000..4fd67a849 --- /dev/null +++ b/games/oot/runtime/input_tape.h @@ -0,0 +1 @@ +../../../shared/runtime/input_tape.h \ No newline at end of file diff --git a/games/totk/runtime/input.c b/games/totk/runtime/input.c index ef5e05fbf..554447ecc 100644 --- a/games/totk/runtime/input.c +++ b/games/totk/runtime/input.c @@ -6,6 +6,7 @@ #include "world_survey.h" #include "keyboard_map.h" #include "input_prompts.h" +#include "input_tape.h" /* the user's route recorded and replayed (TOTK_INPUT_RECORD / TOTK_INPUT_REPLAY, shared with Ocarina of Time) */ /* Input-only probes do not link the platform renderer. */ extern void runtime_prompt_keyboard(const KeyMap *) __attribute__((weak_import)); extern void runtime_prompt_note_input(PromptDevice) __attribute__((weak_import)); @@ -174,6 +175,7 @@ static int pad_states(NpadState *out,int max,const uint32_t *id,uint32_t want_st if(abs(pad.lx)>abs(state.lx))state.lx=pad.lx;if(abs(pad.ly)>abs(state.ly))state.ly=pad.ly; if(abs(pad.rx)>abs(state.rx))state.rx=pad.rx;if(abs(pad.ry)>abs(state.ry))state.ry=pad.ry; if(runtime_vr_stick_pitch_locked())state.ry=0; /* zeldaVR: up / down is the headset's (game_patches_v143.c) */ + input_tape(&state.buttons,&state.lx,&state.ly,&state.rx,&state.ry); /* TOTK_INPUT_RECORD / TOTK_INPUT_REPLAY (input_tape.h): the pad in use only, so a read of another id in the same frame neither adds a line of zeros to the recording nor takes the route in a replay */ }*out=state;pthread_mutex_unlock(&inputLock);return 1;} static int disconnected_states(NpadState *out,int max,const uint32_t *id){if(id)input_log("joy-con state",*id);if(max<=0)return 0;if(!out)runtime_fail("controller output pointer");pthread_mutex_lock(&inputLock);*out=(NpadState){.sampling=++sample};pthread_mutex_unlock(&inputLock);return 1;} static void hold_type(int type){if(type<0||type>1)runtime_fail("invalid joy hold type");holdType=type;(void)holdType;} diff --git a/games/totk/runtime/input_tape.h b/games/totk/runtime/input_tape.h new file mode 120000 index 000000000..4fd67a849 --- /dev/null +++ b/games/totk/runtime/input_tape.h @@ -0,0 +1 @@ +../../../shared/runtime/input_tape.h \ No newline at end of file diff --git a/shared/README.md b/shared/README.md index c81ab5422..c2bfd6a5e 100644 --- a/shared/README.md +++ b/shared/README.md @@ -54,6 +54,7 @@ a shared file is resolved from the link's folder, so it finds that game's own he | `runtime/mouse_look.h` | Mouse movement as the right stick and the gyro (mouse mode), and pointing | totk, oot, odyssey | | `runtime/controller_map.h` | The controller layout file (`controls.txt`): the physical buttons with the mouse's, modes, the screenshot hold. Odyssey's keeps its `reach` lines, and Animal Crossing's and Echoes of Wisdom's have no mouse lines (their mouse belongs to the keyboard layout), so those stay their own | totk, oot | | `runtime/input_file.h` | Scripted input: `TOTK_INPUT_SCRIPT`, and the `TOTK_INPUT_FILE` command stream with the commands every game has (keys, screenshots, capture, text entry, flags, traces, close), read on when the file is replaced. Each game's `runtime/input.c` keeps its key table and HID answers and gives its own commands and flags (`input_game_command`, `input_game_flag`) | totk, oot, odyssey, acnh, eow | +| `runtime/input_tape.h` | The user's route recorded and replayed: `TOTK_INPUT_RECORD` writes each change of the controller state the game reads, by frame (counted from the start, then from the world's first frame), and `TOTK_INPUT_REPLAY` hands the game that state instead and, on the Mac, closes it when the route ends; for A/B runs on a route the user played. The `OOT_` names still work. In Breath of the Wild, pin the frame step (`framestep 8.333` in the input file) both when recording and when replaying, or the route drifts | totk, oot | | `runtime/gate.S`, `runtime/thread_enter.S`, `runtime/thread_compat.h`, `runtime/hle_stack.c` | The host-call gate the rewritten code's `svc` and timer reads enter (every register kept), a guest thread's first entry, the thread code's log and clock names, and host functions wrapped onto the guest thread's host stack (with `hletrace`). Each game's `hle_stack.S` stays its own: TotK's and OoT's pass stack arguments beyond the eighth (48ea32ba, for DKB), which would add instructions to every host call of the other games | totk, oot, odyssey, acnh, eow | | `runtime/audio_effects.{c,h}`, `runtime/audio_reverb.{c,h}` | The audio renderer's effects (delay, I3DL2 reverb, biquad, limiter and the rest) and the reverb, as Ryujinx.Audio computes them | totk, oot, odyssey, acnh, eow | | `runtime/audio_ramp.h` | The volume ramp and depop arithmetic (`tests/test_audio_ramp.c`). Whether a game ramps is its own `audio_mixer.c`'s: Breath of the Wild under `GAME_BOTW`, Odyssey always | totk, oot, odyssey | diff --git a/shared/runtime/input_tape.h b/shared/runtime/input_tape.h new file mode 100644 index 000000000..00dc8e4c0 --- /dev/null +++ b/shared/runtime/input_tape.h @@ -0,0 +1,121 @@ +/* The controller state the game reads, recorded and replayed (2026-11-18, user: "不是让你录制了吗 我的路线和操作", + "用我的 路线 运行"): a test plays the user's own route and actions again. + + OOT_INPUT_RECORD=: each change of the state pad_states hands the game (buttons and the four stick axes, after the + keyboard, the controller and the input file are merged) is a line, by frame. Frames are counted in two segments so a + load that takes longer on a later run does not shift what follows: B, game frames presented since the start + (dm_nvn_frame_count), until the world is entered; W, frames since the first world frame (mtl_world_frame, the + gate hle_nvn.c uses for its warm-up wait). The state is written again when the segment changes, and a heartbeat line + every 600 frames marks how far the recording went. + a state, from this frame on + H the recording reached this frame + OOT_INPUT_REPLAY=: the game reads the recorded state instead (the real keyboard and controller are ignored), + and 120 frames after the recording's last line the game closes normally (the input file's "close"; on the Mac only: + the iOS frontend's close only logs, so there the last state stays until the user closes the game). + + The game itself is not deterministic (its own timing, loading), so a replay follows the route closely, not exactly. + + Shared (2026-11-26): Tears of the Kingdom and Breath of the Wild record and replay the same way, for A/B runs on a route + the user played. TOTK_INPUT_RECORD / TOTK_INPUT_REPLAY are the names in every game; the OOT_ names still work. + Breath of the Wild 1.9.0 above 30 fps steps its game time by each frame's measured length, so the same frames hold a + key for a different time in every run and the route drifts (2026-11-26: replays left the recorded path). Pin the step + while recording and while replaying: the input file's "framestep 8.333" (1/120 s a frame), sent again once the world + is entered. With it six replays of one route each reached its end on the same frames. */ +#pragma once +#include +#include +#include +#include +#include +#include + +__attribute__((weak)) uint64_t dm_nvn_frame_count(void); /* gfx/nvn/hle_nvn.c */ +__attribute__((weak)) uint64_t mtl_world_frame(void); /* gfx/nvn/nvn_mtl.m: the first world frame, 0 before */ +void runtime_front_request_close(void); /* frontend.m */ + +typedef struct { char seg; uint64_t frame, buttons; int32_t lx, ly, rx, ry; } TapeEvent; +static pthread_mutex_t g_tape_mu = PTHREAD_MUTEX_INITIALIZER; +static int g_tape_mode = -1; /* 0 off, 1 record, 2 replay */ +static FILE *g_tape_out; +static TapeEvent g_tape_last; static int g_tape_have_last; static char g_tape_last_seg; static uint64_t g_tape_beat; +static TapeEvent *g_tape; static size_t g_tape_n; +static char g_tape_end_seg; static uint64_t g_tape_end_frame; static int g_tape_closed; + +static void tape_where(char *seg, uint64_t *frame) { + uint64_t now = dm_nvn_frame_count ? dm_nvn_frame_count() : 0, w = mtl_world_frame ? mtl_world_frame() : 0; + if (w && now >= w) { *seg = 'W'; *frame = now - w; } else { *seg = 'B'; *frame = now; } +} +static void tape_open(void) { + const char *rec = getenv("TOTK_INPUT_RECORD"), *rep = getenv("TOTK_INPUT_REPLAY"); + if (!rec || !*rec) rec = getenv("OOT_INPUT_RECORD"); + if (!rep || !*rep) rep = getenv("OOT_INPUT_REPLAY"); + g_tape_mode = 0; + if (rep && *rep) { + FILE *f = fopen(rep, "r"); + if (!f) { fprintf(stderr, "[input-tape] 【红】cannot read %s: no replay\n", rep); return; } + size_t cap = 1024; g_tape = (TapeEvent *)malloc(cap * sizeof *g_tape); + char line[256]; + while (g_tape && fgets(line, sizeof line, f)) { + TapeEvent e; char seg; unsigned long long fr, b; + /* the file is in the order it was played: its last line, a state or a heartbeat, is where the recording ended */ + if (line[0] == 'H' && sscanf(line, "H %c %llu", &seg, &fr) == 2) { g_tape_end_seg = seg; g_tape_end_frame = fr; continue; } + if (sscanf(line, "%c %llu %llx %d %d %d %d", &seg, &fr, &b, &e.lx, &e.ly, &e.rx, &e.ry) != 7 || (seg != 'B' && seg != 'W')) continue; + e.seg = seg; e.frame = fr; e.buttons = b; + if (g_tape_n == cap) { TapeEvent *g = (TapeEvent *)realloc(g_tape, (cap *= 2) * sizeof *g_tape); if (!g) break; g_tape = g; } + g_tape[g_tape_n++] = e; + g_tape_end_seg = seg; g_tape_end_frame = fr; + } + fclose(f); + if (!g_tape_n) { fprintf(stderr, "[input-tape] 【红】%s holds no state: no replay\n", rep); return; } + size_t w = 0; for (size_t i = 0; i < g_tape_n; i++) w += g_tape[i].seg == 'W'; + fprintf(stderr, "[input-tape] replaying %s: %zu states before the world, %zu in it, to %c %" PRIu64 "\n", rep, g_tape_n - w, w, g_tape_end_seg, g_tape_end_frame); + g_tape_mode = 2; + } else if (rec && *rec) { + g_tape_out = fopen(rec, "w"); + if (!g_tape_out) { fprintf(stderr, "[input-tape] 【红】cannot write %s: no recording\n", rec); return; } + fprintf(g_tape_out, "# controller states (shared/runtime/input_tape.h): \n"); + fflush(g_tape_out); + fprintf(stderr, "[input-tape] recording the controller to %s\n", rec); + g_tape_mode = 1; + } +} +/* pad_states, with the state it is about to hand the game */ +static void input_tape(uint64_t *buttons, int32_t *lx, int32_t *ly, int32_t *rx, int32_t *ry) { + pthread_mutex_lock(&g_tape_mu); + if (g_tape_mode < 0) tape_open(); + if (!g_tape_mode) { pthread_mutex_unlock(&g_tape_mu); return; } + char seg; uint64_t frame; tape_where(&seg, &frame); + if (g_tape_mode == 1) { + TapeEvent e = { seg, frame, *buttons, *lx, *ly, *rx, *ry }; + int changed = !g_tape_have_last || seg != g_tape_last_seg || e.buttons != g_tape_last.buttons || e.lx != g_tape_last.lx || + e.ly != g_tape_last.ly || e.rx != g_tape_last.rx || e.ry != g_tape_last.ry; + if (changed) { + fprintf(g_tape_out, "%c %" PRIu64 " %" PRIx64 " %d %d %d %d\n", seg, frame, e.buttons, e.lx, e.ly, e.rx, e.ry); + g_tape_last = e; g_tape_have_last = 1; g_tape_last_seg = seg; + } + if (frame / 600 != g_tape_beat || changed) { + if (frame / 600 != g_tape_beat) { g_tape_beat = frame / 600; fprintf(g_tape_out, "H %c %" PRIu64 "\n", seg, frame); } + fflush(g_tape_out); /* a close or a crash keeps what was played */ + } + } else { + /* the latest state at or before this point: the tape is in time order and B comes before W, so a cursor moves on */ + static size_t at; static int started; + while (at + (size_t)started < g_tape_n) { + const TapeEvent *next = &g_tape[at + (size_t)started]; + if (!(next->seg < seg || (next->seg == seg && next->frame <= frame))) break; + at += (size_t)started; started = 1; + } + const TapeEvent *use = started && g_tape[at].seg == seg ? &g_tape[at] : NULL; /* in the world, only the world's */ + /* TOTK_INPUT_REPLAY_LIVE=1 (or OOT_INPUT_REPLAY_LIVE=1) (tests that drive the controllers themselves): in the world the live state stays, the tape only adds to it */ + static int live = -1; if (live < 0) { const char *e = getenv("TOTK_INPUT_REPLAY_LIVE"); if (!e || !*e) e = getenv("OOT_INPUT_REPLAY_LIVE"); live = e && *e == '1'; } + if (live && seg == 'W') { if (use) { *buttons |= use->buttons; if (use->lx) *lx = use->lx; if (use->ly) *ly = use->ly; if (use->rx) *rx = use->rx; if (use->ry) *ry = use->ry; } } + else if (use) { *buttons = use->buttons; *lx = use->lx; *ly = use->ly; *rx = use->rx; *ry = use->ry; } + else { *buttons = 0; *lx = *ly = *rx = *ry = 0; } + if (!g_tape_closed && seg == g_tape_end_seg && frame > g_tape_end_frame + 120) { + g_tape_closed = 1; + fprintf(stderr, "[input-tape] the recording ended (%c %" PRIu64 "): closing the game\n", seg, g_tape_end_frame); + runtime_front_request_close(); + } + } + pthread_mutex_unlock(&g_tape_mu); +} diff --git a/tests/test_oot_input_tape.py b/tests/test_oot_input_tape.py index 6778f7aff..abaa9db32 100644 --- a/tests/test_oot_input_tape.py +++ b/tests/test_oot_input_tape.py @@ -21,14 +21,16 @@ def test_record_then_replay_with_a_slower_load(self): exe, tape = Path(d) / 'probe', Path(d) / 'route.tape' subprocess.run(['xcrun', 'clang', '-std=c11', '-Wall', '-Wextra', '-Werror', '-I', str(RUNTIME), str(ROOT / 'tests/oot_input_tape_probe.c'), '-o', str(exe)], check=True) - env = dict(os.environ, OOT_INPUT_RECORD=str(tape)) + # the shared header reads the TOTK_ names first: one exported in the shell must not take the probe's route + clean = {k: v for k, v in os.environ.items() if not k.startswith(('OOT_INPUT_', 'TOTK_INPUT_'))} + env = dict(clean, OOT_INPUT_RECORD=str(tape)) subprocess.run([str(exe), 'record'], env=env, check=True, capture_output=True) lines = [l for l in tape.read_text().splitlines() if l and not l.startswith('#')] self.assertIn('B 10 1 0 0 0 0', lines) # the title's button, by frames since the start self.assertIn('W 0 0 0 0 0 0', lines) # the state again where the world begins self.assertIn('W 5 0 20000 0 0 0', lines) # walking, by frames since the world began self.assertIn('H W 600', lines) # a heartbeat every 600 frames - env = dict(os.environ, OOT_INPUT_REPLAY=str(tape)) + env = dict(clean, OOT_INPUT_REPLAY=str(tape)) run = subprocess.run([str(exe), 'replay'], env=env, capture_output=True, text=True) self.assertEqual(run.returncode, 0, run.stdout + run.stderr) self.assertIn('closed at frame', run.stdout) diff --git a/tests/test_totk_input_tape.py b/tests/test_totk_input_tape.py new file mode 100644 index 000000000..3c9363283 --- /dev/null +++ b/tests/test_totk_input_tape.py @@ -0,0 +1,56 @@ +"""Tears of the Kingdom and Breath of the Wild record the user's route and play it again (shared/runtime/input_tape.h, from +Ocarina of Time; 2026-11-26, user: "vc consegue gravar o que eu ando? assim vou longe andando"): an A/B compares two +builds on the route the user played, not on a scripted walk that ran Link into a rock in Kakariko. + +The probe (tests/oot_input_tape_probe.c) records with TOTK_INPUT_RECORD and replays with TOTK_INPUT_REPLAY: the names the +shared runtime reads in every game. The game must call the tape where it hands the controller state over, after the +keyboard, the controller and the input file are merged, or a replay would not drive what the game reads.""" +import os +import subprocess +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] + + +class InputTapeInTotk(unittest.TestCase): + def test_record_then_replay_under_the_totk_names(self): + with tempfile.TemporaryDirectory() as d: + exe, tape = Path(d) / 'probe', Path(d) / 'route.tape' + subprocess.run(['xcrun', 'clang', '-std=c11', '-Wall', '-Wextra', '-Werror', '-I', str(ROOT / 'shared/runtime'), + str(ROOT / 'tests/oot_input_tape_probe.c'), '-o', str(exe)], check=True) + clean = {k: v for k, v in os.environ.items() if not k.startswith(('OOT_INPUT_', 'TOTK_INPUT_'))} + subprocess.run([str(exe), 'record'], env=dict(clean, TOTK_INPUT_RECORD=str(tape)), check=True, capture_output=True) + lines = [l for l in tape.read_text().splitlines() if l and not l.startswith('#')] + self.assertIn('W 5 0 20000 0 0 0', lines) + run = subprocess.run([str(exe), 'replay'], env=dict(clean, TOTK_INPUT_REPLAY=str(tape)), capture_output=True, text=True) + self.assertEqual(run.returncode, 0, run.stdout + run.stderr) + self.assertIn('closed at frame', run.stdout) + + def test_the_game_hands_the_tape_the_merged_state(self): + source = (ROOT / 'games/totk/runtime/input.c').read_text() + self.assertIn('#include "input_tape.h"', source) + pad = source[source.index('static int pad_states(NpadState *out,int max,const uint32_t *id,uint32_t want_style){'):] + pad = pad[:pad.index('return 1;}')] + tape = pad.index('input_tape(&state.buttons,&state.lx,&state.ly,&state.rx,&state.ry);') + # after every merge (keyboard, controller buttons and sticks, the VR pitch lock): a recording holds what the + # game reads, and a replay is not overridden by a later merge + for merge in ('state.buttons|=pad.buttons', 'if(abs(pad.ry)>abs(state.ry))state.ry=pad.ry;', + 'if(runtime_vr_stick_pitch_locked())state.ry=0;'): + self.assertLess(pad.index(merge), tape, merge) + # inside the pad-in-use block: a read of another controller id in the same frame would otherwise record a line + # of zeros after the pad's state, and a replay (the frame's last line wins) would hand the pad zeros + block = pad.index('if(enabled&&allowed&&*id==pad_id()') + self.assertLess(block, tape) + self.assertLess(tape, pad.index('}*out=state;'), 'the tape runs before the pad-in-use block closes') + + def test_one_file_for_every_game(self): + for game in ('totk', 'oot'): + link = ROOT / f'games/{game}/runtime/input_tape.h' + self.assertTrue(link.is_symlink(), game) + self.assertEqual(link.resolve(), (ROOT / 'shared/runtime/input_tape.h').resolve(), game) + + +if __name__ == '__main__': + unittest.main()