Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 0 additions & 111 deletions games/oot/runtime/input_tape.h

This file was deleted.

1 change: 1 addition & 0 deletions games/oot/runtime/input_tape.h
2 changes: 2 additions & 0 deletions games/totk/runtime/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;}
Expand Down
1 change: 1 addition & 0 deletions games/totk/runtime/input_tape.h
1 change: 1 addition & 0 deletions shared/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
121 changes: 121 additions & 0 deletions shared/runtime/input_tape.h
Original file line number Diff line number Diff line change
@@ -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=<file>: 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.
<B|W> <frame> <buttons hex> <lx> <ly> <rx> <ry> a state, from this frame on
H <B|W> <frame> the recording reached this frame
OOT_INPUT_REPLAY=<file>: 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 <inttypes.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

__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): <B|W> <frame> <buttons> <lx> <ly> <rx> <ry>\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);
}
Loading