Triforce v0.5.1

Hotfix for v0.5.0: crashes (NX2navi game folder refused by Setup, heap-full retries, GTX 10 series noise pass and whole VRAM mirrors, code-patch mods kept with their code, no page file warning), graphics (DLSS with frame generation, DLSS coming back, AMD storage buffer alignment, mesh index map, shared memory barrier, shader conversions, integrated GPUs' uniform snapshot, a Mesh shaders row), controls (microphone after restart, button prompts with a mod's size table, sprint vs Hide text, native mouse camera, controller after Restart now), Setup's Preparing shaders step, and the Linux link fix.
This commit is contained in:
ruckus7382sfgsgdf
2026-11-18 12:06:48 -06:00
parent 72793f2231
commit a750a21025
54 changed files with 1806 additions and 114 deletions

View File

@@ -488,8 +488,17 @@ static DWORD WINAPI display_main(void *p) {
/* (no activate: shown at the bottom of the z-order, so a game in front keeps its focus) */
if (noactivate)
SetWindowPos(hwnd, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_SHOWWINDOW);
else
else {
ShowWindow(hwnd, SW_SHOW);
/* ((Triforce, 11-18) brought to the front: after the Insert menu's Restart now the new window opened behind
another one (the run that started it had exited meanwhile, and the window it handed the front to kept it), and
the controllers, which count only while the game is in front (nn_hid.c), did nothing until the game was quit
and started by hand (#bug-tracker "Controller fails to work upon restart"). The run before allows it
(hos_relaunch.c AllowSetForegroundWindow); a start from Setup or an icon is in front already) */
SetForegroundWindow(hwnd);
if (GetForegroundWindow() != hwnd)
hos_log("display: the window did not come to the front at its start (controllers count once it is in front)");
}
if (getenv("HOS_FULLSCREEN") && atoi(getenv("HOS_FULLSCREEN"))) set_fullscreen(hwnd, 1);
RAWINPUTDEVICE rid = {1, 2, 0, hwnd}; /* generic desktop mouse: WM_INPUT while the window is in front */
RegisterRawInputDevices(&rid, 1, sizeof rid);

View File

@@ -1644,6 +1644,19 @@ static void occlusion_switch(void) {
a message says it once at boot; the game starts anyway. The text, or 0 when there is enough */
#define HOS_MEMORY_NEEDED_MB 10240ull
static int memory_notice_text(unsigned long long ram_mb, unsigned long long swap_mb, char *out, size_t cap) {
/* ((Triforce, 11-18) and no swap at all below 40 GB of memory: a player's PC of 32 GB with Windows' page file off
crashed as every save loaded, 17 "commit ... failed (error 1455)" before it: the game's own memory and the
renderer's reach about 21 GB there, and without a page file the other programs' share counts against the same
limit. With the page file back on it played (#bug-tracker 11-18) */
if (swap_mb == 0 && ram_mb < 40960ull) {
snprintf(out, cap,
"This PC has %.1f GB of memory and no swap (the page file on Windows is turned off). Loading a save the game "
"uses about 21 GB, and without a page file Windows can run out and the game closes.\n\nOn Windows: search "
"\"Adjust the appearance and performance of Windows\", Advanced, Virtual memory: Change, tick \"Automatically "
"manage paging file size for all drives\", then restart the PC. It starts now anyway.",
ram_mb / 1024.0);
return 1;
}
if (ram_mb + swap_mb >= HOS_MEMORY_NEEDED_MB) return 0;
snprintf(out, cap,
"This PC has %.1f GB of memory and %.1f GB of swap (the page file on Windows). The game needs about %llu GB of the "
@@ -1672,7 +1685,10 @@ static void memory_notice(void) {
hos_log("boot: %llu MB of memory, %llu MB of swap", ram, swap);
return;
}
hos_log("boot: LOW MEMORY: %llu MB of memory, %llu MB of swap, below the ~%llu MB the game needs", ram, swap, HOS_MEMORY_NEEDED_MB);
if (swap == 0 && ram + swap >= HOS_MEMORY_NEEDED_MB) /* ((Triforce, 11-18) memory_notice_text's no-swap case) */
hos_log("boot: NO PAGE FILE: %llu MB of memory, 0 MB of swap: loading a save can run out of memory (error 1455)", ram);
else
hos_log("boot: LOW MEMORY: %llu MB of memory, %llu MB of swap, below the ~%llu MB the game needs", ram, swap, HOS_MEMORY_NEEDED_MB);
if (getenv("HOS_HEADLESS")) return;
#ifndef _WIN32
int hos_sdl_load(void);

View File

@@ -43,6 +43,9 @@ static void relaunch(void) {
swprintf(pid, 16, L"%lu", (unsigned long)GetCurrentProcessId());
SetEnvironmentVariableW(L"HOS_RESTART_OF", pid);
if (CreateProcessW(exe, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) {
/* ((Triforce, 11-18) this run is in front: the new one may take the front for its window (display.c), which it
otherwise opened behind another window once this one had gone, and the controllers did nothing there) */
AllowSetForegroundWindow(pi.dwProcessId);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
} else {

View File

@@ -600,6 +600,7 @@ void hos_thread_destroy(HosThread *t) {
if (threads[i] == t) { threads[i] = threads[--nthreads]; break; }
ReleaseSRWLockExclusive(&threads_lock);
if (t->handle) CloseHandle(t->handle);
if (t->wake) CloseHandle(t->wake);
free(t);
}

View File

@@ -204,12 +204,14 @@ static void code_record_load(void) {
if (built) nbuilt = mod_code_parse(text, built, BUILT_MOST);
free(text);
}
static void code_patches(const Mod *mod) {
/* (returns how many of this mod's patches wait for a build: new since it, or changed) */
static int code_patches(const Mod *mod) {
char pat[MAX_PATH];
snprintf(pat, sizeof pat, "%s\\exefs\\*", mod->path);
WIN32_FIND_DATAA fd;
HANDLE f = FindFirstFileA(pat, &fd);
if (f == INVALID_HANDLE_VALUE) return;
if (f == INVALID_HANDLE_VALUE) return 0;
int waits = 0;
do {
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || !mod_code_is_patch(fd.cFileName)) continue;
char path[MAX_PATH], rel[MOD_CODE_PATH];
@@ -224,9 +226,11 @@ static void code_patches(const Mod *mod) {
else if (state == MOD_CODE_REFUSED) hos_log("mods: %s was not applied when the game was built: %s", rel, why);
else if (state == MOD_CODE_CHANGED) hos_log("mods: %s changed since the game was built: the game has it as it was then", rel);
else hos_log("mods: %s is not built into the game", rel);
code_waits += state == MOD_CODE_CHANGED || state == MOD_CODE_WAITS;
waits += state == MOD_CODE_CHANGED || state == MOD_CODE_WAITS;
} while (FindNextFileA(f, &fd));
FindClose(f);
code_waits += waits;
return waits;
}
/* (and the patches the game has that the mods folder no longer does, with or without a mods folder) */
static void code_patches_gone(void) {
@@ -281,10 +285,20 @@ static void mods_scan(void) {
for (int i = 0; i < nmods; i++) {
char dir[MAX_PATH];
snprintf(dir, sizeof dir, "%s\\romfs", mods[i].path);
const int files = index_romfs(dir, "", 0);
code_patches(&mods[i]); /* (Triforce: its exefs patches, built in or waiting) */
/* (Triforce: its exefs patches, built in or waiting, first. (11-18) A mod whose code waits for a build keeps its RomFS
files waiting too: the game crashed at its start with a mod's new files over its old code (Classic Item Slots
put in by the Mods menu, #bug-tracker "Crashing on startup"; a build put them in step). They come with the code,
at the next build. HOS_MODS_SPLIT=1: the files now anyway, as before) */
const int waits = code_patches(&mods[i]);
static int split = -1;
if (split < 0) split = getenv("HOS_MODS_SPLIT") && atoi(getenv("HOS_MODS_SPLIT")) == 1;
const int hold = waits > 0 && !split;
const int files = hold ? 0 : index_romfs(dir, "", 0);
cheat_files(&mods[i]); /* (and its cheat file) */
hos_log("mods: %s: %d RomFS files", mods[i].name, files);
if (hold)
hos_log("mods: %s: its RomFS files wait for the build too, with its code (%d patch(es) not built in yet)", mods[i].name, waits);
else
hos_log("mods: %s: %d RomFS files", mods[i].name, files);
}
hos_log("mods: %d mods from %s: %zu RomFS entries", nmods, root, table_n);
}
@@ -340,7 +354,10 @@ static uint64_t zs_content_size(const char *host_path) {
address 0). So a mod's file gets at least its unpacked size and room beside it: an eighth of it, 128 KB at least, and
64 KB more (a model needs about 8 KB, and 3 to 4 KB a material and a shape: 99 of the game's own fitted, 11-17). The
game's own files keep the table's figure; a figure is only ever raised. size: the table's, path: the guest's path */
volatile LONG hos_mods_size_hook_seen; /* ((Triforce, 11-18) the lifted code calls the hook below: prompts.c asks */
int hos_prompts_font_file(const char *key, char *out, size_t cap); /* prompts.c */
uint64_t hos_mods_table_size(uint64_t size, uint64_t path) {
if (!hos_mods_size_hook_seen) InterlockedExchange(&hos_mods_size_hook_seen, 1);
if (!table_n || !path) return size;
const char *p = (const char *)GP(path);
char rel[MAX_PATH], key[MAX_PATH];
@@ -352,6 +369,20 @@ uint64_t hos_mods_table_size(uint64_t size, uint64_t path) {
Entry *e = find(key);
int packed = 1;
if (!e) {
/* ((Triforce, 11-18) the button prompts' font, larger than the game's: with a mod's own size table served, ours
(prompts/rsizetable.zs, which has room for it) is not, so its room comes from here instead. Before, prompts.c
gave the prompts up whenever a mod had a size table (#bug-tracker 11-18: "Button Prompt UI Broken", with the
Young Link Rides Pony Epona mod) */
char built[MAX_PATH];
if (hos_prompts_font_file(key, built, sizeof built)) {
const uint64_t real = zs_content_size(built);
const uint64_t need = real ? (real + (64u << 10) + 0xfff) & ~(uint64_t)0xfff : 0;
static volatile LONG told_font;
if (need > (uint32_t)size && !InterlockedExchange(&told_font, 1))
hos_log("mods: the button prompts' font gets %llu bytes to load in (the size table's %llu; the font %llu unpacked)",
(unsigned long long)need, (unsigned long long)(uint32_t)size, (unsigned long long)real);
return need > (uint32_t)size ? need : size;
}
snprintf(key, sizeof key, "%s", rel);
lower_slash(key);
e = find(key), packed = 0;

View File

@@ -9,15 +9,21 @@
which f_80644c80 calls only while the stick is out of its dead zone (found with hardware write watchpoints on the two
angles in Kokiri Forest). Yaw falls as the view turns right and pitch rises as it tilts down.
The camera's state at +0x204 is 3 while it follows Link by itself, and then it eases the pitch back to its rest over a
couple of seconds; the right stick makes it 4 (the player's), where a pitch stays where it was put. The mouse leaves the
state as it is: written there from here the game puts it back and the angles with it the next frame. So a pitch the
mouse chose eases back as one the game chose does, while yaw stays.
The camera's state at +0x204 is 3 while it follows Link by itself, and then it eases the pitch back toward its rest and,
as Link walks, turns the yaw back behind him; the right stick makes it 4 (the player's), where the angles stay. The
mouse does not move the stick, so the game's follow kept easing the view back toward Link's: after a while of playing,
the camera drifted to where Link faced. So the angles the mouse chose are the host's own (target_yaw and target_pitch
below), put back into the camera every frame: the camera's update still runs inside the frame -- its easing, its
follow, its collision -- but what it does to the angles never adds up over the frames. The game's own right stick is
let through: while the camera's keys or a pad turn it (nn_hid.c, hos_mouse_native_stick), its angles become the mouse's
new start.
The motion is added before the camera reads its input, so the game's own collision, lock-on and scripted cameras still
follow. While the hook has run in the last 150 ms the mouse's motion is the camera's (hos_mouse_native gives 1: the
right stick is left alone); otherwise (the pause menu, cutscenes with another camera, a build the patch did not find
its place in) it is the right stick as before.
follow. The camera is the mouse's only while the game gives it free control: the hook ran within the last 150 ms, the
camera's state at +0x204 is 3 or 4, and the game holds none of the buttons that take the right stick for its own use
(ZR, the bow's aim; L, the item wheel; R — nn_hid.c says which with hos_mouse_native_free). Otherwise (a menu, an item
wheel, a lock-on, the bow's aim, the pause menu, cutscenes with another camera, a build the patch did not find its
place in) the motion is the right stick as before, so the game's own menus, selections and aiming still take it.
The sensitivity is the mouse's (the menu's Mouse sensitivity, - and =): 0.1 degrees a count at 1. HOS_MOUSE_INVERT_Y=1
or the menu's Invert mouse vertical flips up and down. */
@@ -37,13 +43,23 @@ extern double hos_mouse_sens;
enum { LIMITS_AT = 0x178, PITCH_AT = 0x18c, YAW_AT = 0x1a4, STATE_AT = 0x204 };
#define LIVE_NS 150000000LL /* (the hook ran this recently: the camera takes the mouse) */
#define DEG_PER_COUNT 0.1
/* (the camera's state at +0x204: 3 while it follows Link by itself, where the right stick takes it over, 4 the player's) */
#define STATE_FOLLOWS 3
#define STATE_PLAYER 4
static volatile LONG64 last_hook_ns;
static volatile LONG64 last_camera;
static volatile LONG last_state = -1; /* (the last camera's state at +0x204) */
static volatile LONG game_free = 1; /* (nn_hid.c: the game leaves the camera to the player this frame) */
static volatile LONG stick_held; /* (nn_hid.c: the camera's keys or a pad turn the camera this frame) */
static uint64_t own_camera; /* (the camera target_yaw and target_pitch belong to) */
static float target_yaw, target_pitch; /* (the mouse's angles: the game's own follow is undone every frame) */
static int target_ok;
static long long applied, cameras_seen;
static float getf(uint64_t a) { float f; memcpy(&f, GP(a), 4); return f; }
static void putf(uint64_t a, float f) { memcpy(GP(a), &f, 4); }
static int32_t geti(uint64_t a) { int32_t v; memcpy(&v, GP(a), 4); return v; }
/* the menu's Mouse camera (option 6), or HOS_MOUSE_NATIVE */
static int native_on(void) {
@@ -66,6 +82,24 @@ static int looks_like_camera(uint64_t cam) {
fabsf(y) <= 360.0f;
}
/* Whether the game gives the camera to the player now: its update ran within the last 150 ms, its state is one the
right stick turns (3 or 4), and none of the buttons the game takes the stick for is held. Otherwise the mouse is the
right stick, as in As the stick: in a menu, an item wheel, a lock-on, a scripted camera or the bow's aim. */
static int free_look(void) {
if (hos_now_ns() - InterlockedCompareExchange64(&last_hook_ns, 0, 0) >= LIVE_NS) return 0;
const int state = last_state;
return (state == STATE_FOLLOWS || state == STATE_PLAYER) && game_free != 0;
}
/* nn_hid.c, with each read of the controller: 1 while the game leaves the camera to the player, 0 while it takes the
right stick for its own use (ZR, the bow's aim; L, the item wheel; R) */
void hos_mouse_native_free(int free) { InterlockedExchange(&game_free, free ? 1 : 0); }
/* nn_hid.c, with each read of the controller: 1 while the player's own right stick (the camera's keys, a pad) turns the
camera this frame. The game has then moved the angles itself, so they are the mouse's new start: the stick and the
mouse both turn the camera instead of undoing each other. */
void hos_mouse_native_stick(int held) { InterlockedExchange(&stick_held, held ? 1 : 0); }
/* f_80644c80's first line (nx2/oot_patches.py): cam is the player camera, the game's thread */
void hos_mouse_camera(uint64_t cam) {
if (!cam || !native_on() || !looks_like_camera(cam)) return;
@@ -78,6 +112,7 @@ void hos_mouse_camera(uint64_t cam) {
if (state != 3 && state != 4) {
/* (at once: the stick takes the next motion, not one burst once the 150 ms run out) */
InterlockedExchange64(&last_hook_ns, 0);
target_ok = 0; /* the game's angles may change while another camera owns the view */
/* (each state seen leaving the mouse to the stick, once: what another view, Z-targeting, climbing or swimming,
is, to add here if one of them should turn with the mouse) */
static uint32_t said[8];
@@ -91,28 +126,45 @@ void hos_mouse_camera(uint64_t cam) {
return;
}
InterlockedExchange64(&last_hook_ns, hos_now_ns());
InterlockedExchange(&last_state, geti(cam + STATE_AT));
if (!free_look()) { /* (a menu, an item wheel, a lock-on, the bow: the game's camera, and the motion is the right stick's, nn_hid.c) */
target_ok = 0;
return;
}
if (InterlockedExchange64(&last_camera, (LONG64)cam) != (LONG64)cam && cameras_seen++ < 8)
hos_log("mouse native: the player camera at %#llx turns with the mouse", (unsigned long long)cam);
/* (a new camera, or one the game's own stick turned: its angles now are where the mouse starts from) */
if (!target_ok || own_camera != cam || stick_held) {
own_camera = cam;
target_yaw = getf(cam + YAW_AT);
target_pitch = getf(cam + PITCH_AT);
target_ok = 1;
}
int32_t dx, dy;
if (!hos_mouse_take(&dx, &dy) || (!dx && !dy)) return;
const double k = DEG_PER_COUNT * hos_mouse_sens;
float y = getf(cam + YAW_AT) - (float)(dx * k), p = getf(cam + PITCH_AT) + (float)((invert_y() ? -dy : dy) * k);
const float lo = getf(cam + LIMITS_AT), hi = getf(cam + LIMITS_AT + 4);
while (y > 180.0f) y -= 360.0f;
while (y < -180.0f) y += 360.0f;
if (p < lo) p = lo;
if (p > hi) p = hi;
putf(cam + YAW_AT, y), putf(cam + PITCH_AT, p);
if ((++applied & 4095) == 1)
hos_log("mouse native: %lld moves applied; pitch %.2f yaw %.2f", applied, p, y);
if (hos_mouse_take(&dx, &dy) && (dx || dy)) {
const double k = DEG_PER_COUNT * hos_mouse_sens;
target_yaw -= (float)(dx * k);
target_pitch += (float)((invert_y() ? -dy : dy) * k);
const float lo = getf(cam + LIMITS_AT), hi = getf(cam + LIMITS_AT + 4);
while (target_yaw > 180.0f) target_yaw -= 360.0f;
while (target_yaw < -180.0f) target_yaw += 360.0f;
if (target_pitch < lo) target_pitch = lo;
if (target_pitch > hi) target_pitch = hi;
if ((++applied & 4095) == 1)
hos_log("mouse native: %lld moves applied; pitch %.2f yaw %.2f", applied, target_pitch, target_yaw);
}
/* (put back every frame: the camera eases and follows by itself inside one, but none of that adds up to a view that
drifts back toward Link while the mouse is the camera) */
putf(cam + YAW_AT, target_yaw), putf(cam + PITCH_AT, target_pitch);
}
/* With each read of the controller (nn_hid.c, the game's threads): 0 if the mouse is not captured, 1 if the camera takes
its motion (hos_mouse_camera), -1 if no player camera runs now (the right stick takes it) */
its motion (hos_mouse_camera), -1 if the game does not give the camera free control now (a menu, an item wheel, a
lock-on, the bow, the wheels, no player camera: the right stick takes it) */
int hos_mouse_native(void) {
static int said;
if (!said) said = 1, hos_log("mouse native: on (the menu's Mouse camera, or HOS_MOUSE_NATIVE)");
if (hos_now_ns() - InterlockedCompareExchange64(&last_hook_ns, 0, 0) >= LIVE_NS) return -1;
if (!free_look()) return -1;
int32_t dx, dy;
const int captured = hos_mouse_take(&dx, &dy); /* (only to know the capture: the motion is the camera's) */
if (dx || dy) hos_mouse_give_back(dx, dy);

View File

@@ -951,6 +951,7 @@ static int flight_jump_shortcut(uint64_t *buttons, int mode, int menu_open, int
static void read_input(uint64_t *buttons, int32_t stick[4]) {
*buttons = 0;
memset(stick, 0, 4 * sizeof *stick);
void hos_mouse_native_stick(int held);
scripted_input(buttons, stick);
/* A scripted run (a test) takes no input from the player's devices until 5 s after its last press (a variable
left set in a shell no longer leaves the game without controls), and otherwise the pads count only
@@ -1014,16 +1015,21 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
if (bound_down(i, 0)) *buttons |= 1ULL << i, player_used();
int32_t mouse[4] = {0};
/* the mouse's motion: the camera itself (mouse_native.c) with the menu's Mouse camera on Native, else the right
stick; also the stick while the player camera does not run (the pause menu, other cameras: mouse_native.c) */
stick; also the stick while the game does not give the camera free control (a menu, a lock-on, the pause menu,
other cameras: mouse_native.c) and while a button it takes the stick for is held: ZR (the bow's aim), L (the
item wheel) or R, where the mouse points as it does in As the stick */
static const char *native_env;
static int native_looked;
if (!native_looked) native_looked = 1, native_env = getenv("HOS_MOUSE_NATIVE");
const int native = native_env ? atoi(native_env) != 0 : rx_option(6, 0);
int hos_mouse_native(void);
void hos_mouse_native_free(int free);
const uint64_t game_stick = (1ULL << B_ZR) | (1ULL << B_L) | (1ULL << B_R);
int captured = 0, as_stick = !native;
if (native) {
mouse_setup();
mouse_sens_keys();
hos_mouse_native_free(!(*buttons & game_stick));
captured = hos_mouse_native();
if (captured < 0) captured = 0, as_stick = 1;
}
@@ -1045,7 +1051,9 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
controller's stick and the scripted presses are as they are) */
if (walk_vk && key(walk_vk)) walk_keys(keys, rx_option(HOS_OPT_WALK_SCALE, 51));
stick_max(stick, keys);
if (keys[0] || keys[1] || keys[2] || keys[3]) player_used();
/* (hookshot, mouse_native.c) the keys and the pads are the right stick too: while they turn the camera, its angles are
the native mouse's new start, so the two do not undo each other */
hos_mouse_native_stick(stick[2] != 0 || stick[3] != 0);
}
static void vibration_send(uint64_t handle, uint64_t value);

210
host/oot_heap_retry.c Normal file
View File

@@ -0,0 +1,210 @@
/* (hookshot) Allocations of the game's "OptimizeChangeBank" thread tried again when their heap has no room, instead of the
game going on with a null pointer and crashing a few frames later.
The OptimizeChangeBank thread rebuilds a model's material block (f_804566e0, "gsys::ModelResource(MatBlock)") and takes
memory for it from heaps its caller names (its second argument's +8 for the CPU's parts, +16 for the GPU's): small heaps
of their own per model resource ("Model/....bfres", tens of KB), children of a large one. When such a heap is full none
of these allocations is checked:
- two CPU buffers from the heap's tryAlloc (vtable +0x30), at 0x8045690c (stored at the MatBlock's +200, its bump
allocator's memory) and 0x80456da0 (stored at +168, the same base/used/size layout), each sized for the model. A
null one makes its bump allocator hand out guest address 0: the next store, in f_8044cb30, writes guest 0x8 (issue
1697: "CRASH: exception 0xc0000005 (write ..., guest 0x8)", thread OptimizeChangeBanc, lr 0x80456d48, every time a few
frames after loading that player's save, since 2.0.12);
- the GPU memory, f_80522050 called at 0x80456f18 and 0x80457094, leaves the material block with no memory pool
(0x80457248), and the block is written to guest address 0 two calls later (TomShafford/Hookshot's crash at
backflip + attack).
Each is tried again in the heap's parents up to the root, the first with room (the GPU memory then in the game's
default heap). Only these call sites are changed: f_80522050's other callers (some 30) are as shipped.
Freeing: the MatBlock's destructor (f_8093f940) frees +168 and +200 through f_8021dea0, the game's free by address,
which walks the heap tree for the heap whose range holds the block (read from the lifted code, not traced in a run), so
a block from a parent goes back to that parent. The model's own heap does not count it (MatBlock +680's accounting
neither). Every block moved to a parent is counted per call site, and the totals are logged every 16 moves after the
first 60 log lines (a growing total over a session would be a leak).
The GPU part is ported from TomShafford/Hookshot 55d68e8cd6 and 65c1998222 (GPL-3.0, a Hookshot fork), by TomShafford;
its waits before the parents are left out (its own log showed they did not help).
A sead heap here (vtable 0x84c172c8, the heap class of these): +0x20 its name, +0x28 its memory, +0x30 its size, +0x38 its
parent, vtable +0x30 tryAlloc(size, alignment), vtable +0x80 its free size (ExpHeap: the free list summed, main+0x834916d0).
HOS_GPU_ALLOC_RETRY=0: none is tried again (as shipped).
HOS_GPU_ALLOC_TEST_FAIL=<n> or <gpu>,<cpu> (a test): the first n allocations of each kind at these sites (or that many of
the GPU memory and of the CPU buffers) are refused by the heap they name, as a full heap would, so the retry runs (with
HOS_GPU_ALLOC_RETRY=0: the game's crash, as shipped). */
#include "hos.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { HEAP_NAME = 0x20, HEAP_START = 0x28, HEAP_SIZE = 0x30, HEAP_PARENT = 0x38, VT_TRY_ALLOC = 0x30, VT_FREE_SIZE = 0x80 };
static int retry_on(void) {
static int on = -1;
if (on < 0) on = !getenv("HOS_GPU_ALLOC_RETRY") || atoi(getenv("HOS_GPU_ALLOC_RETRY")) != 0;
return on;
}
/* HOS_GPU_ALLOC_TEST_FAIL: 1 while this kind's first n allocations are refused (kind 0 the GPU memory, 1 the CPU buffer) */
static int test_fail(int kind) {
static long left[2];
static volatile int read; /* (set once both counts are in: no thread decrements a count another is still setting) */
if (!__atomic_load_n(&read, __ATOMIC_ACQUIRE)) { /* ("<n>": both kinds; "<gpu>,<cpu>": each its own) */
static volatile long lock;
while (__sync_lock_test_and_set(&lock, 1)) {}
if (!read) {
const char *e = getenv("HOS_GPU_ALLOC_TEST_FAIL"), *comma = e ? strchr(e, ',') : NULL;
left[0] = e ? atol(e) : 0;
left[1] = !e ? 0 : comma ? atol(comma + 1) : atol(e);
__atomic_store_n(&read, 1, __ATOMIC_RELEASE);
}
__sync_lock_release(&lock);
}
return __atomic_sub_fetch(&left[kind], 1, __ATOMIC_RELAXED) >= 0;
}
static int readable(uint64_t a) { return a >= 0x80000000ULL && a < (1ULL << 39) - 0x100; }
static int guest_string(Ctx *c, uint64_t at, char *out, int n) { /* (a guest C string: 1 if it reads as one) */
int i = 0, ok = readable(at);
for (; ok && i < n - 1; i++) {
const char ch = (char)recomp_load8(c, at + i);
if (!ch) break;
if (ch < 32 || ch >= 127) ok = 0;
out[i] = ch;
}
if (!ok || i < 2) i = 0, out[i++] = '?', ok = 0;
out[i] = 0;
return ok;
}
/* A guest function called as the game would, every register and flag as before but x0: its result */
static uint64_t guest_call(Ctx *c, uint64_t fn, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t lr) {
uint64_t x[31];
const uint8_t n = c->n, z = c->z, cf = c->c, v = c->v;
memcpy(x, c->x, sizeof x);
c->x[0] = a0, c->x[1] = a1, c->x[2] = a2, c->x[30] = lr;
a64_icall(c, fn);
const uint64_t r = c->x[0];
memcpy(c->x, x, sizeof x);
c->n = n, c->z = z, c->c = cf, c->v = v;
return r;
}
static uint64_t heap_method(Ctx *c, uint64_t heap, unsigned slot) { /* (0: none) */
if (!readable(heap)) return 0;
const uint64_t vt = recomp_load64(c, heap);
return readable(vt) ? recomp_load64(c, vt + slot) : 0;
}
static uint64_t free_size(Ctx *c, uint64_t heap) { /* (the heap's own getFreeSize; ~0: unknown) */
const uint64_t fn = heap_method(c, heap, VT_FREE_SIZE);
return fn && a64_lookup(fn) ? guest_call(c, fn, heap, 0, 0, c->x[30]) : ~0ULL;
}
static void describe(Ctx *c, uint64_t heap, char *out, int n) {
char name[48], kb[32];
if (!readable(heap)) { snprintf(out, n, "heap %#llx", (unsigned long long)heap); return; }
if (!guest_string(c, recomp_load64(c, heap + HEAP_NAME), name, sizeof name))
guest_string(c, recomp_load64(c, heap + HEAP_START), name, sizeof name);
const uint64_t f = free_size(c, heap);
if (f == ~0ULL) snprintf(kb, sizeof kb, "?");
else snprintf(kb, sizeof kb, "%llu", (unsigned long long)(f >> 10));
snprintf(out, n, "heap %#llx \"%s\" (%llu KB, %s KB free)", (unsigned long long)heap, name,
(unsigned long long)(recomp_load64(c, heap + HEAP_SIZE) >> 10), kb);
}
static int tell(void) { /* (the first 60 failures logged) */
static volatile long told;
return __atomic_fetch_add(&told, 1, __ATOMIC_RELAXED) < 60;
}
/* Blocks moved to a parent heap, per call site (0, 1: the CPU buffers at 0x8045690c and 0x80456da0; 2: the GPU memory),
and their bytes: the totals logged every 16th move after the log budget, and with each move while it lasts */
static volatile long moved_n[3], moved_bytes[3];
static void count_moved(int site, uint64_t bytes, int say) {
static const char *const names[] = {"model buffer (0x8045690c)", "model buffer (0x80456da0)", "GPU memory"};
const long n = __atomic_add_fetch(&moved_n[site], 1, __ATOMIC_RELAXED);
const long b = __atomic_add_fetch(&moved_bytes[site], (long)bytes, __ATOMIC_RELAXED);
if (say || n % 16 == 0)
hos_log("oot: heap-full retries so far: %s %ld blocks, %ld KB in parent heaps", names[site], n, b >> 10);
}
/* ---- the GPU memory: f_80522050(x0 the memory block, x1 the request: +0 the count, +8 the heap, +0x20 what it is for; x2
the size of one). nx2/oot_patches.py makes f_804566e0's two calls of it call this, with the game's function ---- */
static int gpu_alloc_in(Ctx *c, A64Fn game, uint64_t a0, uint64_t a1, uint64_t a2, uint64_t lr, uint64_t heap,
uint64_t in) {
recomp_store64(c, a1 + 8, in);
c->x[0] = a0, c->x[1] = a1, c->x[2] = a2, c->x[30] = lr;
game(c);
recomp_store64(c, a1 + 8, heap);
return (int)(c->x[0] & 1);
}
void hos_oot_gpu_alloc(Ctx *c, A64Fn game) {
const uint64_t a0 = c->x[0], a1 = c->x[1], a2 = c->x[2], lr = c->x[30];
const int on = retry_on() && readable(a1);
const int refused = readable(a1) && recomp_load64(c, a1 + 8) && test_fail(0);
if (refused && !on) { /* (the failure alone, as a full heap gives it) */
hos_log("oot: GPU memory refused by HOS_GPU_ALLOC_TEST_FAIL, not tried again (HOS_GPU_ALLOC_RETRY=0) (lr %#llx)",
(unsigned long long)lr);
c->x[0] = 0;
return;
}
if (!refused) {
game(c);
if (!on || (c->x[0] & 1)) return;
}
const uint64_t count = recomp_load64(c, a1), heap = recomp_load64(c, a1 + 8), what = recomp_load64(c, a1 + 0x20);
char name[64], where[160];
guest_string(c, what, name, sizeof name);
describe(c, heap, where, sizeof where);
const int say = tell();
if (say)
hos_log("oot: GPU memory for %s, %#llx bytes from %s, %s (lr %#llx)", name, (unsigned long long)(count * a2), where,
refused ? "refused by HOS_GPU_ALLOC_TEST_FAIL" : "failed", (unsigned long long)lr);
int ok = 0;
/* (the heaps it was made in: their parents up to the root, the free left in them after their children) */
uint64_t h = heap;
for (int up = 0; up < 8 && readable(h) && !ok; up++) {
const uint64_t parent = recomp_load64(c, h + HEAP_PARENT);
if (!readable(parent) || parent == h) break;
h = parent;
const uint64_t f = free_size(c, h);
if (f != ~0ULL && f < count * a2 + 0x2000) continue;
ok = gpu_alloc_in(c, game, a0, a1, a2, lr, heap, h);
if (say) {
describe(c, h, where, sizeof where);
hos_log("oot: GPU memory: %s in its parent %s", ok ? "made" : "not made either", where);
}
}
if (!ok && heap) { /* (the game's own default when no heap is named: the thread's heap, or the root) */
ok = gpu_alloc_in(c, game, a0, a1, a2, lr, heap, 0);
if (say) hos_log("oot: GPU memory: %s in the game's default heap", ok ? "made" : "not made either");
}
if (ok) count_moved(2, count * a2, say);
if (!ok && say) hos_log("oot: GPU memory: no heap had room: the game goes on without it (and crashes writing to it)");
}
/* ---- the CPU buffers: the heap's tryAlloc at 0x8045690c or 0x80456da0 (lr 0x80456910, 0x80456da4) gave x0 = 0 (or
HOS_GPU_ALLOC_TEST_FAIL refused it: nx2/oot_patches.py then made no call). Tried in each parent with room; x0 the block,
or 0 still ---- */
int hos_oot_heap_refuse(void) { return test_fail(1); }
void hos_oot_heap_retry(Ctx *c, uint64_t heap, uint64_t size, uint64_t align, uint64_t lr) {
if (!retry_on()) {
hos_log("oot: model buffer, %#llx bytes from heap %#llx, failed: not tried again (HOS_GPU_ALLOC_RETRY=0) (lr %#llx)",
(unsigned long long)size, (unsigned long long)heap, (unsigned long long)lr);
return;
}
char where[160];
const int say = tell();
describe(c, heap, where, sizeof where);
if (say) hos_log("oot: model buffer, %#llx bytes from %s, failed (lr %#llx)", (unsigned long long)size, where,
(unsigned long long)lr);
uint64_t h = heap, got = 0;
for (int up = 0; up < 8 && readable(h) && !got; up++) {
const uint64_t parent = recomp_load64(c, h + HEAP_PARENT);
if (!readable(parent) || parent == h) break;
h = parent;
const uint64_t f = free_size(c, h), fn = heap_method(c, h, VT_TRY_ALLOC);
if ((f != ~0ULL && f < size + 0x2000) || !fn || !a64_lookup(fn)) continue;
got = guest_call(c, fn, h, size, align, lr);
if (say) {
describe(c, h, where, sizeof where);
hos_log("oot: model buffer: %s in its parent %s", got ? "made" : "not made either", where);
}
}
if (got) count_moved(lr == 0x80456da4ULL, size, say);
if (!got && say) hos_log("oot: model buffer: no heap had room: the game goes on without it (and crashes writing to it)");
c->x[0] = got;
}

View File

@@ -1093,4 +1093,8 @@ int hos_oot_options_front(void) {
const int64_t t = __atomic_load_n(&options_seen_ns, __ATOMIC_RELAXED);
return t && hos_now_ns() - t < 200000000LL;
}
/* ((Triforce, 11-18) the Mods entry under the game's lists is the Windows half's (mods_list_tick): here no list's last button
has the cursor, so nn_hid.c never keeps a Down from the game. Without it every Linux build of v0.5.0 stopped at the link,
"undefined reference to hos_oot_mods_down" (#bug-tracker, 11-18). tests/test_oot_menu_linux_half.py) */
int hos_oot_mods_down(void) { return 0; }
#endif

View File

@@ -4,11 +4,13 @@
right after it loads those flags (nx2/oot_patches.py patch_sprint), and they put the bit in while the button's sprint
is on: the game's own sprint then runs, with its speed, animation, camera and every rule of its own that ends it.
The button: the left stick's click (which the game does not use) or B, the bottom face button of the Nintendo layout.
The button: the left stick's click or B, the bottom face button of the Nintendo layout.
Toggle: a press starts the sprint, the next press or Link's state ends it. Hold: sprint while it is held. During
gameplay the button is kept from the game, so it does not also do its own action; B only for a press that begins
while Link moves, and then until it is let go, so B puts the weapon away as usual otherwise and the game never sees
half a press. A press made in a menu, a cutscene or a load (no update of Link within OOT_SPRINT_POLLS polls of the
half a press. ((Triforce, 11-18) The stick's click only for a press that begins while Link is in a state a sprint can
be in (OOT_SPRINT_STOP is not): the game does use it, to hide the text box in a conversation, where Link's update goes
on, and with the click kept from it there that did nothing (#bug-tracker 11-18).) A press made in a menu, a cutscene or a load (no update of Link within OOT_SPRINT_POLLS polls of the
controller) starts nothing. With the game's Swap Jump Controls B is the jump: the stick's click is the one to use.
Here: the logic alone, for tests/test_sprint.py. host/oot_sprint.c reads Link and the settings. */
@@ -67,8 +69,9 @@ typedef struct {
int moved; /* step only: the sprint has moved since it started */
double wait_frames; /* step only: 60 Hz frames stood still */
int last_button; /* step only: the setting at the last update */
int sprintable; /* Link's state at his last update was one a sprint can be in (not OOT_SPRINT_STOP) */
} OotSprint;
#define OOT_SPRINT_INIT {0, 0, 0, OOT_SPRINT_POLLS, 0, 0, 0, 0, 0, 0.0, -1}
#define OOT_SPRINT_INIT {0, 0, 0, OOT_SPRINT_POLLS, 0, 0, 0, 0, 0, 0.0, -1, 0}
#define OOT_SPRINT_GET(f) __atomic_load_n(&(f), __ATOMIC_SEQ_CST)
#define OOT_SPRINT_SET(f, v) __atomic_store_n(&(f), (v), __ATOMIC_SEQ_CST)
@@ -91,7 +94,7 @@ static inline void oot_sprint_pad(OotSprint *s, int button, uint64_t *buttons) {
/* A press is decided at its start, for its whole length: kept from the game, and the sprint's, when it begins in
gameplay (B: while Link moves); else the game's alone, and no sprint's */
if (now & ~s->last & bit) {
const int mine = gameplay && (button != OOT_SPRINT_B || OOT_SPRINT_GET(s->moving));
const int mine = gameplay && (button == OOT_SPRINT_B ? OOT_SPRINT_GET(s->moving) : OOT_SPRINT_GET(s->sprintable));
OOT_SPRINT_SET(s->latched, mine);
if (mine) __atomic_fetch_or(&s->pressed, bit, __ATOMIC_SEQ_CST);
} else if (!(now & bit)) {
@@ -112,6 +115,7 @@ static inline int oot_sprint_step(OotSprint *s, int kind, int button, int hold,
int on = OOT_SPRINT_GET(s->active);
if (OOT_SPRINT_GET(s->zl_held)) kind = OOT_SPRINT_STOP; /* targeting: the game's own stance */
OOT_SPRINT_SET(s->moving, kind == OOT_SPRINT_MOVE);
OOT_SPRINT_SET(s->sprintable, kind != OOT_SPRINT_STOP);
if (button == OOT_SPRINT_OFF) {
on = 0;
} else if (hold) {

View File

@@ -563,10 +563,16 @@ static int build(char *out, size_t cap) {
char dir[MAX_PATH], path[MAX_PATH];
int face;
if (!setup(dir, sizeof dir, &face)) return 0;
/* (a mod's size table is served, not ours: the faces' fonts, larger than the game's, do not fit it) */
/* (a mod's size table is served, not ours: the faces' fonts, larger than the game's, do not fit it. ((Triforce, 11-18)
unless the lifted code asks host/mods.c's hos_mods_table_size for each file's room, which gives the font its own: so
only when that hook has not run in this game, an older build of it) */
extern volatile LONG hos_mods_size_hook_seen;
if (hos_mods_has(SIZES_PATH, ".rsizetable.zs")) {
hos_log("prompts: a mod has the resource size table: the game's own icons in texts");
return 0;
if (!hos_mods_size_hook_seen) {
hos_log("prompts: a mod has the resource size table: the game's own icons in texts");
return 0;
}
hos_log("prompts: a mod has the resource size table: the font's room comes from the size hook (host/mods.c)");
}
snprintf(path, sizeof path, "%sprompts/%s/External.bfotf", dir, faces[face]);
long n;
@@ -655,6 +661,15 @@ static int build_camera(char *out, size_t cap) {
return ok;
}
int hos_prompts_romfs(const char *key, char *out, size_t cap);
/* ((Triforce, 11-18) mods.c's size hook: for the font's key (lower case, '/', packed name), the font built for this run
(built now if it was not), 1 with one; 0 for any other key or with no prompts. A mod's own font is the mod's: the hook
finds that one first) */
int hos_prompts_font_file(const char *key, char *out, size_t cap) {
if (strcmp(key, FONT_PATH)) return 0;
return hos_prompts_romfs(FONT_PATH, out, cap);
}
/* mods.c: the file for a RomFS path the prompts replace (key: lower case, '/'), 1 with one. Asked only when no mod has
the file: a mod's own wins */
int hos_prompts_romfs(const char *key, char *out, size_t cap) {