mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-21 00:59:59 -04:00
Faster frames (the replay's memory work, held submits, helper threads, collection and capture cuts), the game letting old areas go (QueryMemoryInfo), the 0.5.2 stutter fixed, AMD RX 5000/6000 and integrated-GPU fixes, the GPU device chosen by name, the in-game menus and launcher reworked, controller, Linux and benchmark fixes.
893 lines
48 KiB
C
893 lines
48 KiB
C
/* (hookshot) The button prompts of the controller in use: the game's button-icon font (Font2/Scalable/External.*.bfotf,
|
|
every icon in a text: dialogue, the control hints, menus) served from prompts/<face>/External.bfotf in the game folder,
|
|
which nx2/prompt_assets.py draws per controller face at build time, with each Switch button's icon pointed at the icon of
|
|
the pad input bound to it (host/nn_hid.c's controls, the menu's Layout preset and Controls page). So the icons say which
|
|
button to press on this pad, custom controls too.
|
|
|
|
The face: triforce_options.ini's button_prompts (the menu's Button prompts row): 0 Auto, from the first controller's name
|
|
(SDL), else 1 Switch, 2 Xbox, 3 PlayStation 4, 4 PlayStation 5, 5 Steam Deck, 6 Keyboard & mouse. The game reads the font
|
|
once, at its start: a new face or new controls show at the next start.
|
|
|
|
(Triforce, 11-19) Auto follows the device used last over runs: with no controller there at the start it takes the face of
|
|
the device the player used last in the run before (<user>/prompts_auto.txt, follow_main). And while the game has the keys'
|
|
face, the overlay's own prompts (the Insert menu, Mods, the Quit dialog, the options' tabs) show the icons of the
|
|
controller used last, from a font of that controller's face built during play (prompts/External.pad.<n>.bfotf.zs, n
|
|
its HOS_PROMPT_*): the game's own texts keep the face they were given at the start.
|
|
|
|
(Triforce) The keyboard and mouse face (NX2navi's prompts = pc; nx2/prompt_keys.py draws it): its font has an icon per
|
|
key that can be bound, at U+F700 + the key's code, and each button's icon is that of the first key bound to it
|
|
(bound_input). The left stick's and the d-pad's icons there are those keys side by side (bind_keys); the right stick's
|
|
are the mouse. Auto chooses it when the game starts with no controller (auto_face).
|
|
|
|
The font as built: an OpenType font behind an 8-byte header, XORed per little-endian word with the key the header's magic
|
|
(0x18029a7f) gives. Its cmap has one icon per pad input at U+F700 + HOS_PAD_*, and each Switch button's code point in a
|
|
format 4 segment of its own, by idDelta: pointing a button at another icon is that segment's idDelta. The result goes out
|
|
as a zstd frame of raw blocks (the file name says .zs; the game's decoder takes a frame made without its dictionary). */
|
|
#include "hos.h"
|
|
#include "hos_paths.h"
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <windows.h>
|
|
#include "../gpu/vk/vk_api.h" /* (HOS_SW_*, HOS_PAD_*, HOS_BIND_PAD) */
|
|
|
|
int hos_binding(int set, int button, int slot); /* nn_hid.c */
|
|
int hos_sdl_pads(uint64_t *buttons, int32_t stick[4]); /* pad_sdl.c */
|
|
void hos_sdl_pad_first_name(char *out, size_t cap);
|
|
int hos_xinput_connected(void); /* nn_hid.c */
|
|
int hos_input_last(void); /* nn_hid.c: 0 none yet, 1 the keys or the mouse, 2 a controller */
|
|
unsigned hos_input_uses(int device); /* nn_hid.c: that device's uses so far */
|
|
extern const char *hos_user_dir; /* nn_fs.c: the saves' folder */
|
|
void hos_sdl_pad_last_name(char *out, size_t cap); /* pad_sdl.c */
|
|
int hos_sdl_pad_scans(void);
|
|
void hos_host_thread_register_self(const char *name); /* hos_prof.c */
|
|
int hos_mods_has(const char *prefix, const char *suffix); /* mods.c */
|
|
|
|
#define FONT_PATH "font2/scalable/external.nin_ounce_nvn2.bfotf.zs" /* (as hos_mods_romfs's keys: lower case) */
|
|
#define TEXTURES_PATH "ui/layout2archive/common.product.100.nin_ounce_nvn2.bntx.zs"
|
|
#define LAYOUTS_PATH "ui/layout2archive/common.product.100.nin_ounce_nvn2.blarc.zs"
|
|
#define SIZES_PATH "system/resource/resourcesizetable." /* (then the version, ".rsizetable.zs") */
|
|
#define CAMERA_PATH "pack/actor/playercamera.pack.zs" /* (Triforce: the Camera speed row, build_camera) */
|
|
#define MAGIC 0x18029a7fu
|
|
#define PAD_BASE 0xF700
|
|
static const char *const faces[HOS_PROMPT_N] = {"switch", "xbox", "ps4", "ps5", "steamdeck", "keyboard"};
|
|
/* the Switch buttons' code points in the font, in HOS_SW_* order (nx2/prompt_assets.py SWITCH) */
|
|
static const uint16_t switch_code[HOS_SW_N] = {0xE040, 0xE041, 0xE042, 0xE043, 0xE056, 0xE057, 0xE044, 0xE045,
|
|
0xE046, 0xE047, 0xE04B, 0xE04C, 0xE062, 0xE060, 0xE063, 0xE061};
|
|
|
|
static int game_dir(char *out, size_t cap) { /* the game folder, with its separator */
|
|
char path[MAX_PATH];
|
|
const DWORD len = hos_game_path_a(path, MAX_PATH);
|
|
char *slash = len ? (strrchr(path, 92) ? strrchr(path, 92) : strrchr(path, 47)) : NULL; /* (linux: '/') */
|
|
if (!slash) return 0;
|
|
slash[1] = 0;
|
|
snprintf(out, cap, "%s", path);
|
|
return 1;
|
|
}
|
|
|
|
static int contains(const char *text, const char *word) { /* (any case) */
|
|
for (; *text; text++) {
|
|
size_t i = 0;
|
|
while (word[i] && tolower((unsigned char)text[i]) == word[i]) i++;
|
|
if (!word[i]) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* (Triforce) Auto's face: the first controller's, from its name (SDL's; "": it has none). xinput: XInput has a controller
|
|
that SDL does not name (HOS_NO_SDL=1, no SDL beside the game, or one SDL had not opened yet): the Xbox's, as XInput
|
|
reads only pads of the Xbox's layout ((11-19) it was the Switch's: an Xbox pad SDL had not opened in time showed the
|
|
Switch's letters). With none, remembered: the face of the device used last in the run before (remembered_face,
|
|
follow_main; -1 none), so a controller switched on after the game started is the face at the next start; without one
|
|
the keyboard and mouse are what the player has: their keys' icons.
|
|
The game reads its icons once, at its start, so Auto is chosen once, then: its texts cannot follow the device used last
|
|
during play, as NX2navi's prompts setting could not. A controller that is there at the start is the face even while the
|
|
keys are used with it, so the two never take turns */
|
|
static int auto_face(const char *name, int xinput, int remembered) {
|
|
if (!name[0]) return xinput ? HOS_PROMPT_XBOX : remembered >= 0 && remembered < HOS_PROMPT_N ? remembered : HOS_PROMPT_KEYBOARD;
|
|
if (contains(name, "dualsense") || contains(name, "ps5")) return HOS_PROMPT_PS5;
|
|
if (contains(name, "dualshock") || contains(name, "ps4") || contains(name, "playstation")) return HOS_PROMPT_PS4;
|
|
if (contains(name, "steam deck")) return HOS_PROMPT_STEAMDECK;
|
|
if (contains(name, "nintendo") || contains(name, "switch") || contains(name, "joy-con")) return HOS_PROMPT_SWITCH;
|
|
return HOS_PROMPT_XBOX; /* (Xbox pads, and the many that copy their layout) */
|
|
}
|
|
|
|
/* ((Triforce, 11-19) where Auto keeps the device used last: <user>/prompts_auto.txt, beside the saves and mouse_sens.txt.
|
|
Not in prompts/, which Setup makes again from nothing when its tools or the game's files change (nx2/make_game.py
|
|
Prompts.finish), as a release's update does: the face remembered went with it, and the first start after each update
|
|
without a controller ready had the keys' face again */
|
|
static void auto_path(char *out, size_t cap) {
|
|
snprintf(out, cap, "%s/prompts_auto.txt", hos_user_dir ? hos_user_dir : "user");
|
|
}
|
|
/* ((Triforce) the face auto_path has (faces[]), the device used last in the run before (follow_main), or prompts/auto.txt's,
|
|
where an older build kept it; -1 none */
|
|
static int remembered_face(const char *dir) {
|
|
char path[MAX_PATH], line[32] = "";
|
|
auto_path(path, sizeof path);
|
|
FILE *f = fopen(path, "r");
|
|
if (!f) {
|
|
snprintf(path, sizeof path, "%sprompts/auto.txt", dir);
|
|
f = fopen(path, "r");
|
|
}
|
|
if (!f) return -1;
|
|
if (!fgets(line, sizeof line, f)) line[0] = 0;
|
|
fclose(f);
|
|
line[strcspn(line, "\r\n")] = 0;
|
|
for (int i = 0; i < HOS_PROMPT_N; i++)
|
|
if (!strcmp(line, faces[i])) return i;
|
|
return -1;
|
|
}
|
|
|
|
/* (Triforce) Auto's wait at the start: SDL's first scan for controllers and four more, a second of them (pad_sdl.c scans
|
|
each quarter second at first), 3 s at most; none without SDL */
|
|
#define AUTO_SCANS 5
|
|
static int chosen_auto; /* (choose_face's: the row is on Auto) */
|
|
|
|
/* HOS_PROMPT_* (faces[]) */
|
|
static int choose_face(const char *dir) {
|
|
char path[MAX_PATH], line[80];
|
|
int choice = 0;
|
|
snprintf(path, sizeof path, "%striforce_options.ini", dir);
|
|
FILE *f = fopen(path, "r");
|
|
if (f) {
|
|
while (fgets(line, sizeof line, f))
|
|
if (!strncmp(line, "button_prompts=", 15)) choice = atoi(line + 15);
|
|
fclose(f);
|
|
}
|
|
if (choice >= 1 && choice <= HOS_PROMPT_N) return choice - 1;
|
|
chosen_auto = 1;
|
|
/* Auto: the first controller SDL opens, as long as AUTO_SCANS's wait */
|
|
char name[128] = "";
|
|
uint64_t b = 0;
|
|
int32_t s[4] = {0};
|
|
for (int i = 0; i < 60; i++) {
|
|
hos_sdl_pads(&b, s);
|
|
hos_sdl_pad_first_name(name, sizeof name);
|
|
const int scans = hos_sdl_pad_scans();
|
|
if (name[0] || scans < 0 || scans >= AUTO_SCANS) break;
|
|
Sleep(50);
|
|
}
|
|
const int xinput = !name[0] && hos_xinput_connected();
|
|
const int remembered = name[0] || xinput ? -1 : remembered_face(dir);
|
|
const int face = auto_face(name, xinput, remembered);
|
|
hos_log("prompts: auto: controller \"%s\": %s", name[0] ? name : xinput ? "(XInput's, no name)" :
|
|
remembered >= 0 ? "(none: the device used last in the run before, prompts_auto.txt)" : "(none)", faces[face]);
|
|
return face;
|
|
}
|
|
|
|
static uint32_t rd32le(const uint8_t *p) { return p[0] | p[1] << 8 | p[2] << 16 | (uint32_t)p[3] << 24; }
|
|
static void wr32le(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v, p[1] = (uint8_t)(v >> 8), p[2] = (uint8_t)(v >> 16), p[3] = (uint8_t)(v >> 24); }
|
|
static unsigned rd16(const uint8_t *p) { return p[0] << 8 | p[1]; }
|
|
static uint32_t rd32(const uint8_t *p) { return (uint32_t)p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; }
|
|
static void wr16(uint8_t *p, unsigned v) { p[0] = (uint8_t)(v >> 8), p[1] = (uint8_t)v; }
|
|
|
|
/* the glyph of a code point in a format 4 subtable (st: its start, n its bytes), 0 none */
|
|
static unsigned cmap4_glyph(const uint8_t *st, size_t n, unsigned code) {
|
|
const unsigned segs = rd16(st + 6) / 2;
|
|
if (14 + 8 * (size_t)segs + 2 > n) return 0;
|
|
const uint8_t *ends = st + 14, *starts = ends + 2 * segs + 2, *deltas = starts + 2 * segs, *ranges = deltas + 2 * segs;
|
|
for (unsigned i = 0; i < segs; i++) {
|
|
if (rd16(ends + 2 * i) < code) continue;
|
|
if (rd16(starts + 2 * i) > code) return 0;
|
|
const unsigned ro = rd16(ranges + 2 * i);
|
|
if (!ro) return (code + rd16(deltas + 2 * i)) & 0xFFFF;
|
|
const uint8_t *g = ranges + 2 * i + ro + 2 * (code - rd16(starts + 2 * i));
|
|
if (g + 2 > st + n) return 0;
|
|
const unsigned gid = rd16(g);
|
|
return gid ? (gid + rd16(deltas + 2 * i)) & 0xFFFF : 0;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* (Triforce) What a control is on (control: a Switch button, HOS_SW_*, or a movement key's place, HOS_KEY_*), as the
|
|
face's icons are numbered: a controller's input (HOS_PAD_*; -1 none, and for what is no button), or on the keyboard and
|
|
mouse face the first of the control's keys (its key code; 0 none: the blank cap), as NX2navi's showed the first key */
|
|
static int bound_input(int face, int control) {
|
|
if (face != HOS_PROMPT_KEYBOARD) return hos_binding(HOS_BIND_PAD, control, 0);
|
|
const int first = hos_binding(HOS_BIND_KEY, control, 0);
|
|
return first > 0 ? first : hos_binding(HOS_BIND_KEY, control, 1);
|
|
}
|
|
|
|
/* each Switch button's segment pointed at its input's icon (bound_input); how many. (Triforce) On the keyboard and mouse
|
|
face the left stick's four directions too, at their movement keys' icons (nx2/prompt_keys.py MOVES), and a key that has
|
|
no icon (one a hand put into the options file) shows the blank cap */
|
|
static int cmap4_bind(uint8_t *st, size_t n, int face) {
|
|
const unsigned segs = rd16(st + 6) / 2;
|
|
if (14 + 8 * (size_t)segs + 2 > n) return 0; /* (as cmap4_glyph: the ranges' last entry too) */
|
|
uint8_t *ends = st + 14, *starts = ends + 2 * segs + 2, *deltas = starts + 2 * segs, *ranges = deltas + 2 * segs;
|
|
static const struct { uint16_t code; uint8_t control; } moves[4] = {
|
|
{0xE052, HOS_KEY_MOVE_UP}, {0xE050, HOS_KEY_MOVE_DOWN}, {0xE05A, HOS_KEY_MOVE_LEFT}, {0xE058, HOS_KEY_MOVE_RIGHT}};
|
|
const int keys = face == HOS_PROMPT_KEYBOARD, inputs = keys ? 256 : HOS_PAD_N;
|
|
int done = 0;
|
|
for (int b = 0; b < HOS_SW_N + (keys ? 4 : 0); b++) {
|
|
const unsigned code = b < HOS_SW_N ? switch_code[b] : moves[b - HOS_SW_N].code;
|
|
const int pad = bound_input(face, b < HOS_SW_N ? b : moves[b - HOS_SW_N].control);
|
|
if (pad < 0 || pad >= inputs) continue; /* (unbound: the default icon stays) */
|
|
unsigned gid = cmap4_glyph(st, n, PAD_BASE + (unsigned)pad);
|
|
if (!gid && keys) gid = cmap4_glyph(st, n, PAD_BASE);
|
|
if (!gid) continue;
|
|
for (unsigned i = 0; i < segs; i++)
|
|
if (rd16(starts + 2 * i) == code && rd16(ends + 2 * i) == code && !rd16(ranges + 2 * i)) {
|
|
wr16(deltas + 2 * i, (gid - code) & 0xFFFF);
|
|
done++;
|
|
break;
|
|
}
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/* (Triforce) The keyboard and mouse face's icons of several keys (the left stick: the movement keys; the d-pad: its four
|
|
keys). Each such glyph calls a subroutine per key, that key's small cap, and prompts/keyboard/keys.bin (t; nx2/
|
|
prompt_keys.py: "HPKB", u32 count; each: u32 offset in the font, u8 control (HOS_KEY_*), 3 bytes; then u32 keys, and for
|
|
each key code the two bytes of its cap's call, 0 0 none) says where those calls are: each gets the call of the first key
|
|
bound to its control (none bound, or a key with no cap: the blank cap's, key code 0's). A call is two bytes whatever the
|
|
key (a charstring's number from 108 to 1131: its first byte 247 to 250), so nothing else in the font moves; a place or a
|
|
call that is not such a number is left alone. How many were written */
|
|
static int bind_keys(uint8_t *otf, size_t n, const uint8_t *t, size_t tn) {
|
|
if (tn < 12) return 0;
|
|
const uint32_t count = rd32le(t + 4);
|
|
if (count > (tn - 12) / 8) return 0;
|
|
const uint8_t *calls = t + 12 + 8 * (size_t)count;
|
|
const uint32_t keys = rd32le(calls - 4);
|
|
if (!keys || keys > (tn - (size_t)(calls - t)) / 2) return 0;
|
|
int done = 0;
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
const uint8_t *e = t + 8 + 8 * (size_t)i;
|
|
const uint32_t off = rd32le(e);
|
|
const int key = bound_input(HOS_PROMPT_KEYBOARD, e[4]);
|
|
const uint8_t *call = calls + (key > 0 && (uint32_t)key < keys && calls[2 * (size_t)key] ? 2 * (size_t)key : 0);
|
|
if (off > n || n - off < 2 || otf[off] < 247 || otf[off] > 250 || call[0] < 247 || call[0] > 250) continue;
|
|
otf[off] = call[0], otf[off + 1] = call[1];
|
|
done++;
|
|
}
|
|
return done;
|
|
}
|
|
|
|
/* the controls into the decrypted font's format 4 subtables; how many buttons */
|
|
static int bind_font(uint8_t *otf, size_t n, int face) {
|
|
if (n < 12) return 0;
|
|
const unsigned tables = rd16(otf + 4);
|
|
for (unsigned t = 0; t < tables && 12 + 16 * (size_t)(t + 1) <= n; t++) {
|
|
const uint8_t *rec = otf + 12 + 16 * t;
|
|
if (memcmp(rec, "cmap", 4)) continue;
|
|
const uint32_t off = rd32(rec + 8), len = rd32(rec + 12);
|
|
if (off > n || len > n - off || len < 4) return 0;
|
|
uint8_t *cmap = otf + off;
|
|
const unsigned subs = rd16(cmap + 2);
|
|
uint32_t seen[8];
|
|
int nseen = 0, done = 0;
|
|
for (unsigned s = 0; s < subs && 4 + 8 * (size_t)(s + 1) <= len; s++) {
|
|
const uint32_t so = rd32(cmap + 4 + 8 * s + 4);
|
|
if (so > len || len - so < 14 || rd16(cmap + so) != 4) continue; /* (so + 14 wrapped past 4 GB) */
|
|
int dup = 0; /* (platforms 0 and 3 share one subtable) */
|
|
for (int k = 0; k < nseen; k++) dup |= seen[k] == so;
|
|
if (dup) continue;
|
|
if (nseen < 8) seen[nseen++] = so;
|
|
const unsigned sl = rd16(cmap + so + 2);
|
|
done = cmap4_bind(cmap + so, sl <= len - so ? sl : len - so, face);
|
|
}
|
|
return done;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* a zstd frame's header for raw blocks of 128 KB: the 4-byte content size, and a window of 128 KB (raw blocks never look back:
|
|
a single-segment frame's window, the whole content, is more than decoders take: zstd's own refuses 244 MB) */
|
|
static void zstd_head(uint8_t head[10], size_t n) {
|
|
wr32le(head, 0xFD2FB528u);
|
|
head[4] = 0x80; /* (FCS 4 bytes, not single segment, no checksum, no dictionary) */
|
|
head[5] = 7 << 3; /* (window 2^(10+7)) */
|
|
wr32le(head + 6, (uint32_t)n);
|
|
}
|
|
|
|
/* one raw block of a frame (zstd_head): k bytes (at most 128 KB), last for the frame's last */
|
|
static int zstd_block(FILE *o, const uint8_t *data, size_t k, int last) {
|
|
const uint32_t bh = (uint32_t)(k << 3) | (last ? 1u : 0u);
|
|
const uint8_t b3[3] = {(uint8_t)bh, (uint8_t)(bh >> 8), (uint8_t)(bh >> 16)};
|
|
return fwrite(b3, 1, 3, o) == 3 && fwrite(data, 1, k, o) == k;
|
|
}
|
|
|
|
/* data as a zstd frame of raw blocks (zstd_head) */
|
|
static int write_zstd_raw(const char *path, const uint8_t *data, size_t n) {
|
|
FILE *f = fopen(path, "wb");
|
|
if (!f) return 0;
|
|
uint8_t head[10];
|
|
zstd_head(head, n);
|
|
int ok = fwrite(head, 1, 10, f) == 10;
|
|
size_t at = 0;
|
|
do {
|
|
const size_t k = n - at < 131072 ? n - at : 131072;
|
|
ok &= zstd_block(f, data + at, k, at + k == n);
|
|
at += k;
|
|
} while (at < n);
|
|
ok &= fclose(f) == 0;
|
|
return ok;
|
|
}
|
|
|
|
static uint8_t *read_all(const char *path, const char *magic, long *n) { /* (the whole file, if it starts with magic: NULL any) */
|
|
FILE *f = fopen(path, "rb");
|
|
if (!f) return NULL;
|
|
fseek(f, 0, SEEK_END);
|
|
*n = ftell(f);
|
|
fseek(f, 0, SEEK_SET);
|
|
uint8_t *t = *n > 8 ? (uint8_t *)malloc((size_t)*n) : NULL;
|
|
const int got = t && fread(t, 1, (size_t)*n, f) == (size_t)*n;
|
|
fclose(f);
|
|
if (!got || (magic && memcmp(t, magic, 4))) {
|
|
free(t);
|
|
return NULL;
|
|
}
|
|
return t;
|
|
}
|
|
|
|
static volatile LONG setup_state; /* setup's: 0 not looked, 1 looking, 2 prompts, 3 none */
|
|
static DWORD WINAPI follow_main(void *arg);
|
|
|
|
/* the game folder and the face, found once; 0 when the folder has no prompts/ (built before them: the game's own icons) */
|
|
static int setup(char *dir, size_t cap, int *face) {
|
|
static char d[MAX_PATH];
|
|
static int f;
|
|
if (InterlockedCompareExchange(&setup_state, 1, 0) == 0) {
|
|
char path[MAX_PATH];
|
|
int ok = game_dir(d, sizeof d);
|
|
if (ok) {
|
|
snprintf(path, sizeof path, "%sprompts", d);
|
|
ok = GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
|
|
}
|
|
if (ok) f = choose_face(d);
|
|
/* (v0.3.5 shipped tools whose Python lacked nx2/prompt_requirements.txt: no folder was made, the Button prompts
|
|
setting did nothing, and the log said nothing of it) */
|
|
else hos_log("prompts: no prompts folder beside the game: the game's own icons, whatever Button prompts is set to "
|
|
"(Setup's Update makes them)");
|
|
/* ((Triforce) the device used last followed, on Auto or with the keys' face: follow_main, which waits for this) */
|
|
if (ok && (chosen_auto || f == HOS_PROMPT_KEYBOARD)) {
|
|
HANDLE t = CreateThread(NULL, 0, follow_main, NULL, 0, NULL);
|
|
if (t) CloseHandle(t);
|
|
}
|
|
InterlockedExchange(&setup_state, ok ? 2 : 3);
|
|
}
|
|
while (setup_state == 1) Sleep(1);
|
|
snprintf(dir, cap, "%s", d);
|
|
*face = f;
|
|
return setup_state == 2;
|
|
}
|
|
|
|
static uint64_t fnv(uint64_t h, const void *p, size_t n) {
|
|
for (size_t i = 0; i < n; i++) h = (h ^ ((const uint8_t *)p)[i]) * 0x100000001b3ull;
|
|
return h;
|
|
}
|
|
|
|
typedef struct { uint32_t off, len, idx; const uint8_t *data; } Run; /* (idx: its place in textures.bin) */
|
|
static int run_cmp(const void *a, const void *b) {
|
|
const Run *x = (const Run *)a, *y = (const Run *)b;
|
|
return x->off != y->off ? (x->off < y->off ? -1 : 1) : (x->idx > y->idx) - (x->idx < y->idx);
|
|
}
|
|
static int run_order(const void *a, const void *b) {
|
|
const uint32_t x = (*(const Run *const *)a)->idx, y = (*(const Run *const *)b)->idx;
|
|
return (x > y) - (x < y);
|
|
}
|
|
|
|
static void add_run(Run **runs, size_t *nruns, size_t *cap, uint32_t off, uint32_t len, const uint8_t *data) {
|
|
if (*nruns == *cap) *runs = (Run *)realloc(*runs, (*cap *= 2) * sizeof **runs);
|
|
(*runs)[*nruns] = (Run){off, len, (uint32_t)*nruns, data};
|
|
(*nruns)++;
|
|
}
|
|
|
|
/* prompts/Common.bntx.zs: the game's textures (prompts/Common.bntx) with the blocks of prompts/<face>/textures.bin that this
|
|
pad's controls choose (nx2/prompt_assets.py: "HPTX", u32 count; each: u8 Switch button (0xff: always), u8 pad input, u16,
|
|
u32 runs; each run: u32 offset, u32 length, the bytes; a later entry over an earlier). Written again only when the face, the
|
|
controls or the blocks changed (prompts/Common.key) */
|
|
/* out: the game's file (game) with the runs written over it (a later one over an earlier where they meet), as raw zstd blocks;
|
|
written again only when its key (keypath: the hash h of what chose the runs) changed */
|
|
static int write_patched(const char *game, const char *out, const char *keypath, uint64_t h, Run *runs, size_t nruns) {
|
|
qsort(runs, nruns, sizeof *runs, run_cmp);
|
|
char keyline[32] = "", want[32];
|
|
FILE *f;
|
|
if ((f = fopen(keypath, "r"))) {
|
|
if (!fgets(keyline, sizeof keyline, f)) keyline[0] = 0;
|
|
fclose(f);
|
|
}
|
|
snprintf(want, sizeof want, "%016llx", (unsigned long long)h);
|
|
int ok = !strcmp(keyline, want) && GetFileAttributesA(out) != INVALID_FILE_ATTRIBUTES;
|
|
if (!ok) { /* the game's file through, the chosen runs written over it, out as raw zstd blocks */
|
|
FILE *in = fopen(game, "rb"), *o = fopen(out, "wb");
|
|
uint8_t *chunk = (uint8_t *)malloc(131072);
|
|
ok = in && o && chunk;
|
|
if (ok) {
|
|
fseek(in, 0, SEEK_END);
|
|
const uint64_t n = (uint64_t)ftell(in);
|
|
fseek(in, 0, SEEK_SET);
|
|
uint8_t head[10];
|
|
zstd_head(head, n);
|
|
ok = n < 0xFFFFFFFFull && fwrite(head, 1, 10, o) == 10;
|
|
size_t first = 0; /* (the first run that may reach this chunk) */
|
|
const Run **here = (const Run **)malloc((nruns ? nruns : 1) * sizeof *here);
|
|
ok = ok && here;
|
|
for (uint64_t pos = 0; ok && pos < n;) {
|
|
const size_t k = n - pos < 131072 ? (size_t)(n - pos) : 131072;
|
|
ok = fread(chunk, 1, k, in) == k;
|
|
while (first < nruns && runs[first].off + runs[first].len <= pos) first++;
|
|
size_t nh = 0; /* (the runs in this chunk, written in their order: a later one over an earlier) */
|
|
for (size_t r = first; r < nruns && runs[r].off < pos + k; r++)
|
|
if (runs[r].off + runs[r].len > pos) here[nh++] = &runs[r];
|
|
qsort(here, nh, sizeof *here, run_order);
|
|
for (size_t i = 0; i < nh; i++) {
|
|
const Run *r = here[i];
|
|
const uint64_t a = r->off > pos ? r->off : pos;
|
|
const uint64_t z = r->off + r->len < pos + k ? r->off + r->len : pos + k;
|
|
memcpy(chunk + (a - pos), r->data + (a - r->off), (size_t)(z - a));
|
|
}
|
|
ok = ok && zstd_block(o, chunk, k, pos + k == n);
|
|
pos += k;
|
|
}
|
|
free(here);
|
|
}
|
|
if (in) fclose(in);
|
|
if (o) ok &= fclose(o) == 0;
|
|
free(chunk);
|
|
if (ok && (f = fopen(keypath, "w"))) {
|
|
fputs(want, f);
|
|
fclose(f);
|
|
}
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
static int build_textures(char *out, size_t cap) {
|
|
char dir[MAX_PATH], path[MAX_PATH];
|
|
int face;
|
|
if (!setup(dir, sizeof dir, &face)) return 0;
|
|
snprintf(path, sizeof path, "%sprompts/%s/textures.bin", dir, faces[face]);
|
|
long tn;
|
|
uint8_t *t = read_all(path, "HPTX", &tn);
|
|
if (!t) return 0; /* (built without the textures: the game's) */
|
|
int bind[HOS_SW_N]; /* ((Triforce) on the keyboard and mouse face the entries' inputs are key codes: bound_input) */
|
|
for (int b = 0; b < HOS_SW_N; b++) bind[b] = bound_input(face, b);
|
|
uint64_t h = fnv(fnv(fnv(0xcbf29ce484222325ull, &face, sizeof face), bind, sizeof bind), t, (size_t)tn);
|
|
/* the runs these controls choose */
|
|
const uint32_t count = rd32le(t + 4);
|
|
size_t at = 8, nruns = 0, cap_runs = 256;
|
|
Run *runs = (Run *)malloc(cap_runs * sizeof *runs);
|
|
for (uint32_t e = 0; e < count && at + 8 <= (size_t)tn; e++) {
|
|
const int sw = t[at], pad = t[at + 1];
|
|
const uint32_t nr = rd32le(t + at + 4);
|
|
const int on = sw == 0xff || (sw < HOS_SW_N && bind[sw] == pad);
|
|
at += 8;
|
|
for (uint32_t r = 0; r < nr && at + 8 <= (size_t)tn; r++) {
|
|
const uint32_t off = rd32le(t + at), len = rd32le(t + at + 4);
|
|
if (len > (size_t)tn - at - 8) break;
|
|
if (on) add_run(&runs, &nruns, &cap_runs, off, len, t + at + 8);
|
|
at += 8 + len;
|
|
}
|
|
}
|
|
snprintf(out, cap, "%sprompts/Common.bntx.zs", dir);
|
|
snprintf(path, sizeof path, "%sprompts/Common.bntx", dir);
|
|
char keypath[MAX_PATH];
|
|
snprintf(keypath, sizeof keypath, "%sprompts/Common.key", dir);
|
|
const int ok = write_patched(path, out, keypath, h, runs, nruns);
|
|
hos_log("prompts: %s textures, %zu runs for these controls%s", faces[face], nruns,
|
|
ok ? "" : ": could not be written");
|
|
free(runs);
|
|
free(t);
|
|
return ok;
|
|
}
|
|
|
|
/* the face the game was given (faces[]), -1 none: the Insert menu names the pad inputs as that controller does. -1 too
|
|
until the game has asked for a prompt file: the menu asks from the present thread, and choose_face can wait for SDL */
|
|
int hos_prompts_face(void) {
|
|
char dir[MAX_PATH];
|
|
int face;
|
|
return setup_state >= 2 && setup(dir, sizeof dir, &face) ? face : -1;
|
|
}
|
|
|
|
/* the Talk ring's place for each of A B X Y (0 east, 1 south, 2 north, 3 west: nx2/prompt_assets.py RING_PLACES): where its pad
|
|
input is among the face buttons, else its own place on the Switch when free, else the first one free */
|
|
static void ring_places(const int *bind, int place[4]) {
|
|
static const int of_pad[4] = {1, 0, 3, 2}; /* (HOS_PAD_SOUTH, EAST, WEST, NORTH) */
|
|
int taken[4] = {0};
|
|
for (int s = 0; s < 4; s++) {
|
|
const int p = bind[s] >= 0 && bind[s] < 4 ? of_pad[bind[s]] : -1;
|
|
place[s] = p >= 0 && !taken[p] ? p : -1;
|
|
if (place[s] >= 0) taken[place[s]] = 1;
|
|
}
|
|
for (int s = 0; s < 4; s++) {
|
|
if (place[s] >= 0) continue;
|
|
int p = s; /* (A B X Y's own places are 0 1 2 3) */
|
|
if (taken[p])
|
|
for (p = 0; taken[p]; p++) {}
|
|
place[s] = p;
|
|
taken[p] = 1;
|
|
}
|
|
}
|
|
|
|
/* prompts/Common.blarc.zs: the game's layouts (prompts/Common.blarc) with
|
|
- the Talk ring's A B X Y where their pad inputs are (prompts/ring.bin, nx2/prompt_assets.py: "HPRG", u32 count; each: u8
|
|
group, u8 Switch button placed, u8 place, u8, u32 runs, the runs);
|
|
- the ocarina hint's icons moved apart for the wide ones bound (prompts/<face>/hint.bin, nx2/prompt_assets.py HINTS: "HPHN",
|
|
u32 count of pad inputs, each one's icon's extra width (f32), u32 panes; each: u32 offset, u8 slot, 3 bytes, f32 x): each
|
|
icon the one before's extra further on, the last one where it is;
|
|
- the hearts, the item slot and the small map out at a wide picture's sides (prompts/wide.bin, nx2/prompt_assets.py HUD_SIDE_PANES: "HPWD", u32
|
|
count; each: u32 offset, i8 side, 3 bytes, f32 x), with the Insert menu's Ultrawide HUD row on Screen edges
|
|
(host/oot_wide_hud.h);
|
|
none when all are as the game has them (the game's own file) */
|
|
static int build_layouts(char *out, size_t cap) {
|
|
char dir[MAX_PATH], path[MAX_PATH];
|
|
int face;
|
|
if (!setup(dir, sizeof dir, &face)) return 0;
|
|
int bind[HOS_SW_N], place[4] = {0, 1, 2, 3};
|
|
for (int b = 0; b < HOS_SW_N; b++) bind[b] = bound_input(face, b);
|
|
/* ((Triforce) keys have no places among four face buttons: the keyboard and mouse face keeps the Switch's) */
|
|
if (face != HOS_PROMPT_KEYBOARD) ring_places(bind, place);
|
|
const int ring_moved = !(place[0] == 0 && place[1] == 1 && place[2] == 2 && place[3] == 3);
|
|
size_t nruns = 0, cap_runs = 64;
|
|
Run *runs = (Run *)malloc(cap_runs * sizeof *runs);
|
|
uint64_t h = fnv(0xcbf29ce484222325ull, place, sizeof place);
|
|
long tn = 0, hn = 0;
|
|
snprintf(path, sizeof path, "%sprompts/ring.bin", dir);
|
|
uint8_t *t = ring_moved ? read_all(path, "HPRG", &tn) : NULL;
|
|
if (t) {
|
|
const uint32_t count = rd32le(t + 4);
|
|
size_t at = 8;
|
|
for (uint32_t e = 0; e < count && at + 8 <= (size_t)tn; e++) {
|
|
const int placed = t[at + 1], where = t[at + 2];
|
|
const uint32_t nr = rd32le(t + at + 4);
|
|
const int on = placed < 4 && place[placed] == where;
|
|
at += 8;
|
|
for (uint32_t r = 0; r < nr && at + 8 <= (size_t)tn; r++) {
|
|
const uint32_t off = rd32le(t + at), len = rd32le(t + at + 4);
|
|
if (len > (size_t)tn - at - 8) break;
|
|
if (on) add_run(&runs, &nruns, &cap_runs, off, len, t + at + 8);
|
|
at += 8 + len;
|
|
}
|
|
}
|
|
h = fnv(h, t, (size_t)tn);
|
|
}
|
|
/* the hint's notes, in its order (Switch L R Y X A: up, down, left, right, A) */
|
|
static const int notes[5] = {HOS_SW_L, HOS_SW_R, HOS_SW_Y, HOS_SW_X, HOS_SW_A};
|
|
snprintf(path, sizeof path, "%sprompts/%s/hint.bin", dir, faces[face]);
|
|
uint8_t *hint = read_all(path, "HPHN", &hn);
|
|
float *xs = NULL;
|
|
int spread = 0;
|
|
if (hint) {
|
|
const uint32_t npads = rd32le(hint + 4);
|
|
const size_t panes_at = 8 + 4 * (size_t)npads;
|
|
float extra[5], shift[6] = {0};
|
|
for (int i = 0; i < 5; i++) {
|
|
const int p = bind[notes[i]];
|
|
extra[i] = 0;
|
|
if (p >= 0 && (uint32_t)p < npads && panes_at <= (size_t)hn) memcpy(&extra[i], hint + 8 + 4 * p, 4);
|
|
}
|
|
for (int i = 3; i >= 0; i--) shift[i] = shift[i + 1] - extra[i]; /* (an icon is drawn from its pane's left) */
|
|
shift[5] = shift[0]; /* (the text with the first icon) */
|
|
spread = shift[0] != 0;
|
|
const uint32_t count = panes_at + 4 <= (size_t)hn ? rd32le(hint + panes_at) : 0;
|
|
if (spread && count <= ((size_t)hn - panes_at - 4) / 12) {
|
|
xs = (float *)malloc(count * sizeof *xs);
|
|
for (uint32_t k = 0; k < count; k++) {
|
|
const uint8_t *e = hint + panes_at + 4 + 12 * k;
|
|
memcpy(&xs[k], e + 8, 4);
|
|
xs[k] += shift[e[4] < 6 ? e[4] : 5];
|
|
add_run(&runs, &nruns, &cap_runs, rd32le(e), 4, (const uint8_t *)&xs[k]);
|
|
}
|
|
h = fnv(fnv(h, shift, sizeof shift), hint, (size_t)hn);
|
|
}
|
|
}
|
|
float hos_wide_hud_out(void);
|
|
const float wide_out = hos_wide_hud_out();
|
|
long wn = 0;
|
|
snprintf(path, sizeof path, "%sprompts/wide.bin", dir);
|
|
uint8_t *wide = wide_out > 0.0f ? read_all(path, "HPWD", &wn) : NULL;
|
|
float *wxs = NULL;
|
|
if (wide) {
|
|
const uint32_t count = rd32le(wide + 4);
|
|
if (count && count <= ((size_t)wn - 8) / 12) {
|
|
wxs = (float *)malloc(count * sizeof *wxs);
|
|
for (uint32_t k = 0; k < count; k++) {
|
|
const uint8_t *e = wide + 8 + 12 * k;
|
|
memcpy(&wxs[k], e + 8, 4);
|
|
wxs[k] += (float)(int8_t)e[4] * wide_out;
|
|
add_run(&runs, &nruns, &cap_runs, rd32le(e), 4, (const uint8_t *)&wxs[k]);
|
|
}
|
|
h = fnv(fnv(h, &wide_out, sizeof wide_out), wide, (size_t)wn);
|
|
}
|
|
} else if (wide_out > 0.0f) {
|
|
hos_log("prompts: no prompts/wide.bin: the Ultrawide HUD stays in the middle (Setup's Update makes it)");
|
|
}
|
|
int ok = 0;
|
|
if (nruns) {
|
|
char keypath[MAX_PATH];
|
|
snprintf(out, cap, "%sprompts/Common.blarc.zs", dir);
|
|
snprintf(path, sizeof path, "%sprompts/Common.blarc", dir);
|
|
snprintf(keypath, sizeof keypath, "%sprompts/Common.blarc.key", dir);
|
|
ok = write_patched(path, out, keypath, h, runs, nruns);
|
|
hos_log("prompts: the Talk ring's A B X Y at places %d %d %d %d, the ocarina hint %s, the hearts, item slot and map %s%s",
|
|
place[0], place[1], place[2], place[3], xs ? "spread" : "as is", wxs ? "at the picture's sides" : "as is",
|
|
ok ? "" : ": could not be written");
|
|
}
|
|
free(wxs);
|
|
free(wide);
|
|
free(xs);
|
|
free(hint);
|
|
free(runs);
|
|
free(t);
|
|
return ok;
|
|
}
|
|
|
|
/* face's font (prompts/<face>/External.bfotf) with these controls, written to prompts/<name> (out its path); 1 when written,
|
|
-1 when the folder has no font of that face. pad: (Triforce) the overlay's font of the controller used last
|
|
(follow_main), written whole and then put in place: (11-19) once a run for each face, before the overlay is told of it,
|
|
so never while it reads it */
|
|
static int build_font(const char *dir, int face, const char *name, int pad, char *out, size_t cap) {
|
|
char path[MAX_PATH];
|
|
snprintf(path, sizeof path, "%sprompts/%s/External.bfotf", dir, faces[face]);
|
|
long n;
|
|
uint8_t *b = read_all(path, NULL, &n);
|
|
if (!b || n < 16 || n % 4) {
|
|
hos_log("prompts: no %s: the game's own icons", path);
|
|
free(b);
|
|
return -1;
|
|
}
|
|
const uint32_t key = rd32le(b) ^ MAGIC;
|
|
for (long i = 8; i < n; i += 4) wr32le(b + i, rd32le(b + i) ^ key);
|
|
const int bound = bind_font(b + 8, (size_t)n - 8, face);
|
|
int several = 0;
|
|
if (face == HOS_PROMPT_KEYBOARD) { /* ((Triforce) the icons of several keys: bind_keys) */
|
|
long kn = 0;
|
|
snprintf(path, sizeof path, "%sprompts/%s/keys.bin", dir, faces[face]);
|
|
uint8_t *k = read_all(path, "HPKB", &kn);
|
|
if (k) several = bind_keys(b + 8, (size_t)n - 8, k, (size_t)kn);
|
|
free(k);
|
|
}
|
|
for (long i = 8; i < n; i += 4) wr32le(b + i, rd32le(b + i) ^ key);
|
|
snprintf(out, cap, "%sprompts/%s", dir, name);
|
|
snprintf(path, sizeof path, "%s.new", out);
|
|
const int ok = pad ? write_zstd_raw(path, b, (size_t)n) && MoveFileExA(path, out, MOVEFILE_REPLACE_EXISTING)
|
|
: write_zstd_raw(out, b, (size_t)n);
|
|
free(b);
|
|
if (pad)
|
|
hos_log("prompts: the overlay's icons of the controller used last: %s, %d buttons on this pad's controls%s", faces[face],
|
|
bound, ok ? "" : ": could not be written");
|
|
else if (face == HOS_PROMPT_KEYBOARD)
|
|
hos_log("prompts: %s icons, %d buttons and directions on these keys, %d keys in the icons of several%s", faces[face],
|
|
bound, several, ok ? "" : ": could not be written");
|
|
else
|
|
hos_log("prompts: %s icons, %d buttons on this pad's controls%s", faces[face], bound, ok ? "" : ": could not be written");
|
|
return ok;
|
|
}
|
|
|
|
/* builds prompts/External.bfotf.zs for this run; 1 when there is one */
|
|
static int build(char *out, size_t cap) {
|
|
char dir[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. ((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")) {
|
|
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)");
|
|
}
|
|
return build_font(dir, face, "External.bfotf.zs", 0, out, cap) > 0;
|
|
}
|
|
|
|
/* the face of the device used last (device: hos_input_last's; name: the controller used last's, SDL's, "" none: XInput's,
|
|
an Xbox pad), -1 none yet */
|
|
static int device_face(int device, const char *name) {
|
|
return device == 2 ? auto_face(name, 1, -1) : device == 1 ? HOS_PROMPT_KEYBOARD : -1;
|
|
}
|
|
|
|
/* follow_main's: the device's face (device_face) held since held_ns, the looks it was used in since (active), and the face
|
|
auto_path has (-1 none) */
|
|
typedef struct { int held, recorded; int64_t held_ns; int active; } Follow;
|
|
/* ((Triforce, 11-19) the keys' face is recorded once they have been used in this many of the looks (a quarter second
|
|
each) while they were the device used last: a key pressed once, or the mouse bumped, and the game then left alone is
|
|
not a player of the keys */
|
|
#define KEYS_ACTIVE 20
|
|
/* face seen at now, its device used since the last look or not (active): whether auto_path is to have it now (then
|
|
recorded is it) */
|
|
static int follow_record(Follow *f, int face, int64_t now, int active) {
|
|
if (face < 0) return 0;
|
|
if (face != f->held) f->held = face, f->held_ns = now, f->active = 0;
|
|
if (active) f->active++;
|
|
const int keys = face == HOS_PROMPT_KEYBOARD;
|
|
if (face == f->recorded || now - f->held_ns < (keys ? 30 : 3) * 1000000000LL || (keys && f->active < KEYS_ACTIVE)) return 0;
|
|
f->recorded = face;
|
|
return 1;
|
|
}
|
|
|
|
/* ((Triforce, 11-19) the overlay's icons follow a controller once it has been the one used last this long: two controllers
|
|
of two faces given the same presses (a DualSense and the virtual Xbox pad DS4Windows makes of it, a Steam Input pad)
|
|
take turns as the one used last, and the icons would change at each press */
|
|
#define PAD_HOLD_NS 1000000000LL
|
|
/* whether the overlay is to show face's icons now: the game has the keys' face, face is a controller's, not the one shown
|
|
(shown: -1 none), the one used last for PAD_HOLD_NS (held_ns) unless none is shown yet, and not one whose font cannot be
|
|
made (failed: a bit a face) */
|
|
static int pad_font_due(int served, int face, int shown, int64_t held_ns, unsigned failed) {
|
|
return served == HOS_PROMPT_KEYBOARD && face >= 0 && face != HOS_PROMPT_KEYBOARD && face != shown && !(failed >> face & 1) &&
|
|
(shown < 0 || held_ns >= PAD_HOLD_NS);
|
|
}
|
|
|
|
/* ((Triforce, 11-19) The device the player uses (host/nn_hid.c hos_input_last), looked at four times a second on a thread
|
|
of its own, from the start (setup) on Auto or with the keys' face:
|
|
- while the game has the keys' face, a controller used builds the overlay's font of its face (build_font's pad), once a
|
|
run for each face (prompts/External.pad.<n>.bfotf.zs), and hos_prompts_pad_face says which to show once written;
|
|
another face's after it has been the last for a second (pad_font_due). One not written is tried again 2 s on;
|
|
- on Auto, the face of the device used last (a controller: its name's, auto_face; the keys or the mouse: theirs) goes
|
|
into auto_path for the next start (remembered_face) once it has been the last for 3 s, the keys' for 30 s and used in
|
|
5 s of them (a key pressed between a controller's presses, or to close the game, keeps the controller's); written
|
|
only when it changes. Never on the input's thread: read_input only stores the device */
|
|
static volatile LONG pad_face_shown = -1; /* the face of the overlay's font to show this run (built), -1 none */
|
|
static DWORD WINAPI follow_main(void *arg) {
|
|
(void)arg;
|
|
hos_host_thread_register_self("button prompts");
|
|
char dir[MAX_PATH];
|
|
int served;
|
|
setup(dir, sizeof dir, &served);
|
|
Follow follow = {-1, remembered_face(dir), 0, 0};
|
|
unsigned built = 0, failed = 0, uses[3] = {0};
|
|
int seen = -1; /* (the face used last, since seen_ns) */
|
|
int64_t seen_ns = 0, retry_ns[HOS_PROMPT_N] = {0};
|
|
for (;;) {
|
|
Sleep(250);
|
|
char name[128] = "";
|
|
const int device = hos_input_last();
|
|
if (device == 2) hos_sdl_pad_last_name(name, sizeof name);
|
|
const int face = device_face(device, name);
|
|
const int64_t now = hos_now_ns();
|
|
const unsigned used = hos_input_uses(device);
|
|
const int active = device >= 1 && device <= 2 && used != uses[device];
|
|
if (device >= 1 && device <= 2) uses[device] = used;
|
|
if (face != seen) seen = face, seen_ns = now;
|
|
if (pad_font_due(served, face, (int)pad_face_shown, now - seen_ns, failed) && now >= retry_ns[face]) {
|
|
char out[MAX_PATH], file[48];
|
|
snprintf(file, sizeof file, "External.pad.%d.bfotf.zs", face);
|
|
const int got = built >> face & 1 ? 1 : build_font(dir, face, file, 1, out, sizeof out);
|
|
if (got > 0) built |= 1u << face, InterlockedExchange(&pad_face_shown, face);
|
|
else if (got < 0) failed |= 1u << face; /* (no font of that face: not tried again) */
|
|
else retry_ns[face] = now + 2000000000LL; /* (not written: again in 2 s) */
|
|
}
|
|
if (chosen_auto && follow_record(&follow, face, now, active)) { /* (written or not: again only when it changes) */
|
|
char path[MAX_PATH];
|
|
auto_path(path, sizeof path);
|
|
FILE *f = fopen(path, "w");
|
|
int ok = f && fputs(faces[face], f) >= 0;
|
|
if (f) ok &= fclose(f) == 0;
|
|
hos_log("prompts: auto: the device used last has the %s face: the next start's without a controller%s",
|
|
faces[face], ok ? "" : " (prompts_auto.txt could not be written)");
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ((Triforce) the face of the controller used last whose icons the overlay is to show, from prompts/External.pad.<n>.bfotf.zs
|
|
(n the face's HOS_PROMPT_*), built while the game has the keys' face (follow_main); -1 none (no controller used yet, or the
|
|
game has a controller's face) */
|
|
int hos_prompts_pad_face(void) {
|
|
return (int)pad_face_shown;
|
|
}
|
|
|
|
/* (Triforce) The Insert menu's Camera speed row (camera_speed= in the options file, percent; HOS_CAMERA_SPEED=<percent>
|
|
over it): how fast the right stick turns the camera, times the game's own speed at any of the five Camera sensitivity
|
|
settings of its Options. The rates are five floats in the game's camera pack (nx2/prompt_assets.py CAMERA has where and
|
|
why): prompts/PlayerCamera.pack.zs is that pack (prompts/PlayerCamera.pack) with each rate of prompts/camera.bin ("HPCM",
|
|
u32 count; each: u32 offset, f32 the game's rate) times the factor. Read once: the game loads the pack at its start, so
|
|
the row applies after a restart. At 100 the game's own file is served. NX2navi's camera speed setting, without its
|
|
search of the game's memory for a change during play. */
|
|
int hos_ini_option(const char *key, int otherwise);
|
|
static volatile double camera_applied = 1.0; /* the factor in the pack the game was given */
|
|
static double camera_wanted(void) {
|
|
static int v = -1;
|
|
if (v < 0) {
|
|
const char *e = getenv("HOS_CAMERA_SPEED");
|
|
v = e && atoi(e) > 0 ? atoi(e) : hos_ini_option("camera_speed", 100);
|
|
v = v < 25 ? 25 : v > 300 ? 300 : v;
|
|
}
|
|
return v / 100.0;
|
|
}
|
|
/* host/nn_hid.c: the mouse's panning reaches the game as the right stick; divided by this it keeps its own sensitivity */
|
|
double hos_camera_speed(void) {
|
|
return camera_applied;
|
|
}
|
|
static int build_camera(char *out, size_t cap) {
|
|
char dir[MAX_PATH], path[MAX_PATH], keypath[MAX_PATH];
|
|
int face;
|
|
const double k = camera_wanted();
|
|
if (k == 1.0 || !setup(dir, sizeof dir, &face)) return 0;
|
|
long tn = 0;
|
|
snprintf(path, sizeof path, "%sprompts/camera.bin", dir);
|
|
uint8_t *t = read_all(path, "HPCM", &tn);
|
|
const uint32_t count = t ? rd32le(t + 4) : 0;
|
|
if (!count || count > 8 || (size_t)tn < 8 + 8 * (size_t)count) {
|
|
hos_log("prompts: no prompts/camera.bin: the camera keeps the game's speed (Setup's Update makes it)");
|
|
free(t);
|
|
return 0;
|
|
}
|
|
float rates[8];
|
|
size_t nruns = 0, cap_runs = 8;
|
|
Run *runs = (Run *)malloc(cap_runs * sizeof *runs);
|
|
for (uint32_t i = 0; i < count; i++) {
|
|
memcpy(&rates[i], t + 8 + 8 * i + 4, 4);
|
|
rates[i] = (float)(rates[i] * k);
|
|
add_run(&runs, &nruns, &cap_runs, rd32le(t + 8 + 8 * i), 4, (const uint8_t *)&rates[i]);
|
|
}
|
|
const uint64_t h = fnv(fnv(0xcbf29ce484222325ull, &k, sizeof k), t, (size_t)tn);
|
|
snprintf(out, cap, "%sprompts/PlayerCamera.pack.zs", dir);
|
|
snprintf(path, sizeof path, "%sprompts/PlayerCamera.pack", dir);
|
|
snprintf(keypath, sizeof keypath, "%sprompts/PlayerCamera.key", dir);
|
|
const int ok = write_patched(path, out, keypath, h, runs, nruns);
|
|
if (ok) camera_applied = k;
|
|
hos_log("prompts: camera speed x%.2f, the first rate %.2f%s", k, (double)rates[0], ok ? "" : ": could not be written");
|
|
free(runs);
|
|
free(t);
|
|
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);
|
|
}
|
|
|
|
/* (Triforce) The title screen's third entry, "Options / ZELDA NOTES Setting" in US English, reads "Options / Triforce":
|
|
prompts/mals/<language>.product.110.sarc.zs is that language's messages (Mals/, its name in lower case) with the entry's
|
|
rest after the game's word for Options made Triforce's (nx2/prompt_assets.py title_messages; the wording is its
|
|
TITLE_TEXT), built at Setup's Update from the player's own files. The one of the language the game shows is served
|
|
(desired_language: language.txt, HOS_LANGUAGE over it), each language's messages are a file of their own. No larger
|
|
than the game's file (title_messages writes no other), so the size table's entry holds it with its allowance. A mod's file of the same name wins (mods.c asks here only without one) */
|
|
const char *hos_desired_language(void); /* nn_applet.c */
|
|
static const struct { const char *code, *mals; } title_languages[] = { /* (nn_applet.c languages[] -> romfs/Mals) */
|
|
{"en-US", "usen"}, {"en-GB", "euen"}, {"fr", "eufr"}, {"fr-CA", "usfr"}, {"de", "eude"}, {"it", "euit"},
|
|
{"es", "eues"}, {"es-419", "uses"}, {"pt-BR", "uspt"}, {"nl", "eunl"}, {"pl", "eupl"}, {"ru", "euru"},
|
|
{"ja", "jpja"}, {"ko", "krko"}, {"zh-Hans", "cnzh"}, {"zh-Hant", "twzh"},
|
|
};
|
|
static int title_messages(const char *key, char *out, size_t cap) {
|
|
char dir[MAX_PATH], want[16];
|
|
int face;
|
|
const char *lang = hos_desired_language(), *mals = "usen";
|
|
for (size_t i = 0; i < sizeof title_languages / sizeof *title_languages; i++)
|
|
if (!strcmp(lang, title_languages[i].code)) mals = title_languages[i].mals;
|
|
snprintf(want, sizeof want, "mals/%s.", mals);
|
|
if (strncmp(key, want, strlen(want)) || !setup(dir, sizeof dir, &face)) return 0; /* (another language's: the game's) */
|
|
snprintf(out, cap, "%sprompts/%s", dir, key);
|
|
const int ok = GetFileAttributesA(out) != INVALID_FILE_ATTRIBUTES;
|
|
static volatile LONG logged;
|
|
if (InterlockedExchange(&logged, 1) == 0)
|
|
hos_log(ok ? "prompts: the title's Options entry from prompts/%s (%s)"
|
|
: "prompts: no prompts/%s (%s): the title's Options entry is the game's (Setup's Update makes it)", key, lang);
|
|
return ok;
|
|
}
|
|
|
|
/* 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) {
|
|
static volatile LONG state[4]; /* the font, the textures, the layouts, the camera: 0 not built, 1 being built, 2 built, 3 none */
|
|
static char path[4][MAX_PATH];
|
|
const size_t kn = strlen(key);
|
|
if (!strncmp(key, "mals/", 5) && kn > 8 && !strcmp(key + kn - 8, ".sarc.zs")) return title_messages(key, out, cap);
|
|
if (!strncmp(key, SIZES_PATH, sizeof SIZES_PATH - 1) && kn > 14 && !strcmp(key + kn - 14, ".rsizetable.zs")) {
|
|
char dir[MAX_PATH]; /* (the game's size table with room for the faces' fonts, larger than its: nx2/prompt_assets.py) */
|
|
int face;
|
|
if (!setup(dir, sizeof dir, &face)) return 0;
|
|
snprintf(out, cap, "%sprompts/rsizetable.zs", dir);
|
|
return GetFileAttributesA(out) != INVALID_FILE_ATTRIBUTES;
|
|
}
|
|
const int which = !strcmp(key, FONT_PATH) ? 0 : !strcmp(key, TEXTURES_PATH) ? 1 : !strcmp(key, LAYOUTS_PATH) ? 2 :
|
|
!strcmp(key, CAMERA_PATH) ? 3 : -1;
|
|
if (which < 0) return 0;
|
|
static int (*const builders[4])(char *, size_t) = {build, build_textures, build_layouts, build_camera};
|
|
if (InterlockedCompareExchange(&state[which], 1, 0) == 0)
|
|
InterlockedExchange(&state[which], builders[which](path[which], MAX_PATH) ? 2 : 3);
|
|
while (state[which] == 1) Sleep(1);
|
|
if (state[which] != 2) return 0;
|
|
snprintf(out, cap, "%s", path[which]);
|
|
return 1;
|
|
}
|