mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-19 16:20:01 -04:00
Triforce v0.5.0
In-game mods (GameBanana browser, search and installs, with the Mask Salesman), code-patch and cheat mods, the devs' test18 to test45 work (full performance overlay, frame pacing on, steady frame rate, save export and import, bug capture, new options rows, Setup's Mods page and Add to Steam), occlusion queries and new defaults, Auto resolution that scales, replay helper threads and robust buffers off by default, the options in sections.
This commit is contained in:
214
host/amiibo.h
Normal file
214
host/amiibo.h
Normal file
@@ -0,0 +1,214 @@
|
||||
/* (Triforce) An amiibo from a file, and the NFC reader the game is shown (host/nn_nfp.c has the SDK's functions; no SDK,
|
||||
no game and no file here: tests/test_amiibo.py compiles this header and runs it on bytes of its own making).
|
||||
|
||||
What the game asks of a figure (read in its own code, build e0f3ccfa: the NfpMgr thread's task at 0x83b84230 and the
|
||||
amiibo sequence's callback at 0x8328b480): it lists the readers, starts detection on one, waits for a tag by asking for
|
||||
the reader's state, reads the tag's TagInfo, mounts it with the mount target 1 (the tag's read-only part), reads its
|
||||
ModelInfo, hands both to the game and then waits for the figure to be lifted off. Of the TagInfo it keeps the serial
|
||||
number; of the ModelInfo the figure's character (bytes 0 to 2), its series (byte 3) and its number (bytes 4 and 5, which
|
||||
it looks up in its own list). The game has no other request: the owner, the nickname and a game's own data on a
|
||||
figure are never opened.
|
||||
|
||||
Both are in the clear in a dump of a figure's NTAG215 tag, so no key is needed and none is read: the serial number in
|
||||
pages 0 and 1 (bytes 0 to 2 and 4 to 7; bytes 3 and 8 are its check bytes) and the eight bytes that say which figure it
|
||||
is in pages 21 and 22 (0x54 to 0x5b: the character, its variant, the kind of figure, the number high byte first, the
|
||||
series, and 2). A dump that a tool has already decrypted keeps the same bytes in other places (amiitool's layout: the
|
||||
tag's bytes 8 to 15 first, the serial number's page at 0x1d4, the figure's at 0x1dc). Everything a tag keeps encrypted
|
||||
is left unread. */
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
enum { AMIIBO_FILE_MAX = 1024 }; /* (the most of a file that is read: a dump is 532, 540 or 572 bytes) */
|
||||
enum { AMIIBO_OK = 0, AMIIBO_BAD_SIZE, AMIIBO_NOT_A_FIGURE };
|
||||
|
||||
typedef struct AmiiboTag {
|
||||
uint8_t uid[7]; /* the tag's serial number */
|
||||
uint8_t model[8]; /* the tag's bytes 0x54 to 0x5b */
|
||||
int decrypted; /* 1: read from a decrypted dump's layout */
|
||||
} AmiiboTag;
|
||||
|
||||
/* a dump's size: an NTAG215's 135 pages (540), the same without its last two (532), or with the tag's signature (572) */
|
||||
static inline int amiibo_size_ok(size_t n) { return n == 532 || n == 540 || n == 572; }
|
||||
|
||||
/* the serial number's two check bytes, the tag's capability bytes (an amiibo's are f1 10 ff ee) and the 2 that ends the
|
||||
figure's eight bytes: uid8 the tag's bytes 0 to 7, rest8 its bytes 8 to 15, model its bytes 0x54 to 0x5b */
|
||||
static inline int amiibo_marks_ok(const uint8_t *uid8, const uint8_t *rest8, const uint8_t *model) {
|
||||
static const uint8_t cc[4] = {0xf1, 0x10, 0xff, 0xee};
|
||||
if ((uint8_t)(0x88 ^ uid8[0] ^ uid8[1] ^ uid8[2]) != uid8[3]) return 0;
|
||||
if ((uint8_t)(uid8[4] ^ uid8[5] ^ uid8[6] ^ uid8[7]) != rest8[0]) return 0;
|
||||
return !memcmp(rest8 + 4, cc, 4) && model[7] == 2;
|
||||
}
|
||||
|
||||
/* A dump's serial number and figure: AMIIBO_OK, or why not. Nothing past data[n - 1] is read, and nothing is written
|
||||
to *out unless the dump is taken */
|
||||
static inline int amiibo_parse(const uint8_t *data, size_t n, AmiiboTag *out) {
|
||||
if (!data || !amiibo_size_ok(n)) return AMIIBO_BAD_SIZE;
|
||||
const uint8_t *uid8 = NULL, *model = NULL;
|
||||
int decrypted = 0;
|
||||
if (amiibo_marks_ok(data, data + 8, data + 0x54)) uid8 = data, model = data + 0x54; /* (as the tag has them) */
|
||||
else if (amiibo_marks_ok(data + 0x1d4, data, data + 0x1dc)) uid8 = data + 0x1d4, model = data + 0x1dc, decrypted = 1;
|
||||
if (!uid8) return AMIIBO_NOT_A_FIGURE;
|
||||
memcpy(out->uid, uid8, 3);
|
||||
memcpy(out->uid + 3, uid8 + 4, 4);
|
||||
memcpy(out->model, model, 8);
|
||||
out->decrypted = decrypted;
|
||||
return AMIIBO_OK;
|
||||
}
|
||||
|
||||
/* nn::nfp::TagInfo (0x58 bytes): the serial number in its first ten bytes, its length at 10, the protocol at 0x20 (1,
|
||||
NFC-A) and the tag's type at 0x24 (2, a type 2 tag) */
|
||||
enum { NFP_TAG_INFO_SIZE = 0x58, NFP_MODEL_INFO_SIZE = 0x40 };
|
||||
static inline void amiibo_tag_info(const AmiiboTag *t, uint8_t out[NFP_TAG_INFO_SIZE]) {
|
||||
memset(out, 0, NFP_TAG_INFO_SIZE);
|
||||
memcpy(out, t->uid, 7);
|
||||
out[10] = 7;
|
||||
out[0x20] = 1;
|
||||
out[0x24] = 2;
|
||||
}
|
||||
/* nn::nfp::ModelInfo (0x40 bytes), as the game reads it: the character's three bytes as the tag has them, the series,
|
||||
the figure's number as a 16-bit number of this processor (the tag has its high byte first), the kind of figure */
|
||||
static inline void amiibo_model_info(const AmiiboTag *t, uint8_t out[NFP_MODEL_INFO_SIZE]) {
|
||||
memset(out, 0, NFP_MODEL_INFO_SIZE);
|
||||
memcpy(out, t->model, 3);
|
||||
out[3] = t->model[6];
|
||||
out[4] = t->model[5];
|
||||
out[5] = t->model[4];
|
||||
out[6] = t->model[3];
|
||||
}
|
||||
|
||||
/* ---- the readers ----
|
||||
Two are listed once a figure was chosen (never before: the game is then told what it was always told, no reader): the
|
||||
first controller's and the console's own, since the game asks for one or the other by how it is played. A reader's
|
||||
state is nn::nfp::DeviceState's: 0 just initialized, 1 looking for a tag, 2 a tag found, 3 the tag taken away,
|
||||
4 the tag mounted. The figure chosen is put on the reader that looks for one first, stays there while the game reads it,
|
||||
and is lifted off NFP_HOLD_NS after the game read its ModelInfo (the game waits for that before it goes on), or when
|
||||
the game stops looking. One choice is one touch. */
|
||||
enum { NFP_INIT = 0, NFP_SEARCH, NFP_ACTIVE, NFP_DEACTIVE, NFP_MOUNT };
|
||||
enum { NFP_READERS = 2 };
|
||||
#define NFP_HANDLE_BASE 0x7466000000000000ULL /* (a DeviceHandle of ours: this, and the controller's number) */
|
||||
#define NFP_HOLD_NS 1000000000LL
|
||||
/* nn::Result 2115-0073, "the reader is not in the state for that": the game's task ends as for a reader that went away */
|
||||
#define NFP_WRONG_STATE 0x9273u
|
||||
/* what the Scan amiibo row is told (HOS_OPT_AMIIBO_SAID) */
|
||||
enum { NFP_SAID_NONE = 0, NFP_SAID_WAITING, NFP_SAID_ON_READER, NFP_SAID_READ, NFP_SAID_REFUSED, NFP_SAID_NO_FILE };
|
||||
|
||||
typedef struct Nfp {
|
||||
int listed; /* the readers are shown to the game (from the first figure chosen, for the rest of the run) */
|
||||
int waiting; /* a figure waits for a reader that looks for one */
|
||||
int holder; /* the reader the figure is on, -1 none */
|
||||
int64_t read_at; /* when the game read the ModelInfo of the figure on the reader, 0 not yet */
|
||||
int said; /* NFP_SAID_* */
|
||||
int state[NFP_READERS];
|
||||
AmiiboTag tag;
|
||||
} Nfp;
|
||||
|
||||
static inline void nfp_reset(Nfp *n) {
|
||||
memset(n, 0, sizeof *n);
|
||||
n->holder = -1;
|
||||
}
|
||||
static inline uint32_t nfp_reader_npad(int reader) { return reader ? 0x20u : 0u; } /* (No. 1, and handheld) */
|
||||
static inline uint64_t nfp_handle(int reader) { return NFP_HANDLE_BASE | nfp_reader_npad(reader); }
|
||||
/* the reader a DeviceHandle names, -1 when it is not one of ours (or none is listed) */
|
||||
static inline int nfp_reader(const Nfp *n, uint64_t handle) {
|
||||
if (!n->listed) return -1;
|
||||
for (int i = 0; i < NFP_READERS; i++)
|
||||
if (handle == nfp_handle(i)) return i;
|
||||
return -1;
|
||||
}
|
||||
/* the figure on a reader is lifted off: that reader says so until the game stops or starts detection */
|
||||
static inline void nfp_lift(Nfp *n, int said) {
|
||||
if (n->holder >= 0) n->state[n->holder] = NFP_DEACTIVE;
|
||||
n->holder = -1;
|
||||
n->read_at = 0;
|
||||
n->said = said;
|
||||
}
|
||||
/* A figure chosen: 1 when the readers were not listed before (the game is then told the readers changed). A figure
|
||||
still on a reader is lifted off first */
|
||||
static inline int nfp_choose(Nfp *n, const AmiiboTag *tag) {
|
||||
const int was = n->listed;
|
||||
if (n->holder >= 0) nfp_lift(n, NFP_SAID_NONE);
|
||||
n->tag = *tag;
|
||||
n->listed = 1, n->waiting = 1;
|
||||
n->said = NFP_SAID_WAITING;
|
||||
return !was;
|
||||
}
|
||||
/* No figure any more: the one waiting is forgotten, the one on a reader lifted off. The readers stay listed */
|
||||
static inline void nfp_take_away(Nfp *n) {
|
||||
n->waiting = 0;
|
||||
if (n->holder >= 0) nfp_lift(n, NFP_SAID_NONE);
|
||||
n->said = NFP_SAID_NONE;
|
||||
}
|
||||
/* nn::nfp::Initialize and Finalize: every reader as new, a figure on one lifted off; what was chosen still waits */
|
||||
static inline void nfp_restart(Nfp *n) {
|
||||
if (n->holder >= 0) nfp_lift(n, n->read_at ? NFP_SAID_READ : NFP_SAID_NONE);
|
||||
for (int i = 0; i < NFP_READERS; i++) n->state[i] = NFP_INIT;
|
||||
}
|
||||
/* ListDevices: the handles (at most max of them), their number; 0 while none is listed */
|
||||
static inline int nfp_list(const Nfp *n, uint64_t *handles, int max) {
|
||||
int count = 0;
|
||||
for (int i = 0; n->listed && i < NFP_READERS && count < max; i++) handles[count++] = nfp_handle(i);
|
||||
return count;
|
||||
}
|
||||
/* time passing on a reader: the figure that waits goes onto a reader that looks for one; the one the game has read is
|
||||
lifted off NFP_HOLD_NS later */
|
||||
static inline void nfp_step(Nfp *n, int reader, int64_t now) {
|
||||
if (n->state[reader] == NFP_SEARCH && n->waiting && n->holder < 0) {
|
||||
n->waiting = 0, n->holder = reader, n->read_at = 0;
|
||||
n->state[reader] = NFP_ACTIVE;
|
||||
n->said = NFP_SAID_ON_READER;
|
||||
}
|
||||
if (n->holder == reader && n->read_at && now - n->read_at >= NFP_HOLD_NS) nfp_lift(n, NFP_SAID_READ);
|
||||
}
|
||||
static inline int nfp_state(Nfp *n, int reader, int64_t now) {
|
||||
nfp_step(n, reader, now);
|
||||
return n->state[reader];
|
||||
}
|
||||
/* StartDetection: a reader that is not looking yet looks for a tag */
|
||||
static inline uint32_t nfp_start(Nfp *n, int reader, int64_t now) {
|
||||
if (n->state[reader] != NFP_INIT && n->state[reader] != NFP_DEACTIVE) return NFP_WRONG_STATE;
|
||||
n->state[reader] = NFP_SEARCH;
|
||||
nfp_step(n, reader, now);
|
||||
return 0;
|
||||
}
|
||||
/* StopDetection: the reader as new again. A figure on it is lifted off: that was its touch, read or not */
|
||||
static inline uint32_t nfp_stop(Nfp *n, int reader) {
|
||||
if (n->holder == reader) nfp_lift(n, n->read_at ? NFP_SAID_READ : NFP_SAID_NONE);
|
||||
n->state[reader] = NFP_INIT;
|
||||
return 0;
|
||||
}
|
||||
/* GetTagInfo: of the figure on the reader */
|
||||
static inline uint32_t nfp_tag_info(Nfp *n, int reader, int64_t now, uint8_t out[NFP_TAG_INFO_SIZE]) {
|
||||
nfp_step(n, reader, now);
|
||||
if (n->holder != reader || (n->state[reader] != NFP_ACTIVE && n->state[reader] != NFP_MOUNT)) return NFP_WRONG_STATE;
|
||||
amiibo_tag_info(&n->tag, out);
|
||||
return 0;
|
||||
}
|
||||
/* Mount: the tag found is mounted (whatever part of it was asked for: only the part in the clear is ever answered) */
|
||||
static inline uint32_t nfp_mount(Nfp *n, int reader, int64_t now) {
|
||||
nfp_step(n, reader, now);
|
||||
if (n->holder != reader || (n->state[reader] != NFP_ACTIVE && n->state[reader] != NFP_MOUNT)) return NFP_WRONG_STATE;
|
||||
n->state[reader] = NFP_MOUNT;
|
||||
return 0;
|
||||
}
|
||||
/* GetModelInfo: of the mounted figure. From then on it is lifted off after NFP_HOLD_NS */
|
||||
static inline uint32_t nfp_model_info(Nfp *n, int reader, int64_t now, uint8_t out[NFP_MODEL_INFO_SIZE]) {
|
||||
nfp_step(n, reader, now);
|
||||
if (n->holder != reader || n->state[reader] != NFP_MOUNT) return NFP_WRONG_STATE;
|
||||
amiibo_model_info(&n->tag, out);
|
||||
if (!n->read_at) n->read_at = now ? now : 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* A dump's file name as the Scan amiibo page sends it: a name of the amiibo folder itself, never a path */
|
||||
static inline int amiibo_name_ok(const char *name) {
|
||||
const size_t len = strlen(name);
|
||||
if (!len || len > 200 || name[0] == '.' || name[0] == ' ') return 0;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
const unsigned char ch = (unsigned char)name[i];
|
||||
if (ch < 0x20 || ch == '/' || ch == '\\' || ch == ':' || ch == '*' || ch == '?' || ch == '"' || ch == '<' || ch == '>' || ch == '|')
|
||||
return 0;
|
||||
}
|
||||
return !strstr(name, "..");
|
||||
}
|
||||
335
host/bug_capture.c
Normal file
335
host/bug_capture.c
Normal file
@@ -0,0 +1,335 @@
|
||||
/* (triforce) A bug capture: F8 in the game's window (host/display.c), or the System tab's Bug capture row, writes one folder a
|
||||
player can send with a problem report, captures/<date>-<time> in the game folder:
|
||||
|
||||
frame.png the frame the window was shown next, without the overlays (gpu/vk/vk_bugshot.cpp)
|
||||
triforce_options.ini the settings
|
||||
Triforce-save-....zip the save, as the System tab's Export save writes it (host/save_share.c)
|
||||
log-tail.txt the last 400 lines of triforce_log.txt
|
||||
capture.txt written last, so a folder that has it is whole: the build, the GPU and its driver as the log's
|
||||
first lines name them, the game's frames shown so far, the frame rate before the press, and
|
||||
the map and Link's place when he is in play
|
||||
|
||||
The behaviour is NX2navi's F8 (its docs/bug-capture.md). Its code is another author's and none of it is here; this is
|
||||
written for Triforce from what that page says a capture holds.
|
||||
|
||||
Nothing of the file work is done where the key is pressed: the press only notes the time and the frame count, asks the
|
||||
renderer for the next frame's picture (rx_option_set HOS_OPT_BUG_SHOT) and wakes the save rows' thread, which writes
|
||||
the folder (hos_bug_capture_work) while the game goes on. One capture at a time: a press while one is being written is
|
||||
skipped, and said in the log.
|
||||
|
||||
The map and Link's place come from what the game's own thread says when asked "where" (host/hos_main.c
|
||||
hos_oot_live_crawl, the lab's command: Sequence_CurrentBanc and the player's position, read there on the thread that
|
||||
updates him). It answers in the log within a frame while Link is in play, and the capture reads that line back: the
|
||||
first one after the line the press itself logged, which is in the log before the question is asked. At the title
|
||||
screen or in a loading screen there is no answer and capture.txt says so. The camera is not in a capture: nothing of
|
||||
the host reads it outside the debug tools' free camera.
|
||||
|
||||
The frame rate is counted from the game's own presents (hos_present_count), which the save rows' thread notes ten times a
|
||||
second (hos_bug_capture_tick). */
|
||||
#include "hos_paths.h" /* (first: hos_game_path_a, and windows.h on Windows) */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define SEP "\\"
|
||||
#else
|
||||
#define SEP "/"
|
||||
#endif
|
||||
#define CAPTURE_PATH 1024
|
||||
#define TAIL_LINES 400
|
||||
#define TAIL_BYTES (512u << 10) /* (400 of the log's lines are well under this) */
|
||||
#define HEAD_BYTES (64u << 10) /* (the build, the GPUs and the driver are in the log's first hundred lines) */
|
||||
#define SHOT_WAIT_MS 3000 /* (the picture is there three presents after the press; a game that shows no frame has none) */
|
||||
#define TICKS 128 /* (a tenth of a second each: the twelve seconds before a press) */
|
||||
|
||||
void hos_log(const char *fmt, ...);
|
||||
void hos_log_flush(void);
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
const char *hos_version(void); /* hos_main.c */
|
||||
void hos_oot_live_crawl(const char *line); /* hos_main.c: "!where" */
|
||||
int hos_binding(int set, int button, int slot); /* nn_hid.c */
|
||||
extern volatile LONG64 hos_present_count; /* display.c, posix/display_sdl.c: the game's presents */
|
||||
/* save_share.c */
|
||||
int hos_share_game_dir(char *out, size_t cap);
|
||||
int hos_save_export(const char *dir, int bug_report, char *path, size_t cap, int *files);
|
||||
void hos_save_share_start(void);
|
||||
void hos_save_share_wake(void);
|
||||
void hos_save_share_say(int row, const char *text);
|
||||
|
||||
/* ---- the press ---- */
|
||||
static volatile LONG capture_state; /* 0 none, 1 asked: the thread has it to write, 2 being written, 3 a press is noting itself */
|
||||
static struct {
|
||||
int number; /* (the run's first capture is 1: what its lines in the log are told apart by) */
|
||||
char trigger[40];
|
||||
LONG64 presents;
|
||||
time_t when;
|
||||
ULONGLONG tick;
|
||||
} asked;
|
||||
|
||||
/* A capture is asked for (any thread; trigger: what asked, for the log). 1: it will be written; 0: one is under way */
|
||||
int hos_bug_capture_ask(const char *trigger) {
|
||||
if (InterlockedCompareExchange(&capture_state, 3, 0) != 0) {
|
||||
hos_log("capture: %s: skipped, the capture before it is still being written", trigger);
|
||||
return 0;
|
||||
}
|
||||
static int count; /* (one press at a time gets here: capture_state) */
|
||||
asked.number = ++count;
|
||||
snprintf(asked.trigger, sizeof asked.trigger, "%s", trigger);
|
||||
asked.presents = InterlockedCompareExchange64(&hos_present_count, 0, 0);
|
||||
asked.when = time(NULL);
|
||||
asked.tick = GetTickCount64();
|
||||
/* (in the log before the game's thread is asked where Link is: its answer is the first one after this line) */
|
||||
hos_log("capture: bug capture %d asked for (%s) at present %lld", asked.number, trigger, (long long)asked.presents);
|
||||
rx_option_set(HOS_OPT_BUG_SHOT, 1); /* (the frame shown next) */
|
||||
#if defined(_WIN32) || defined(__linux__) /* (the debug tools' side of hos_main.c: as there) */
|
||||
hos_oot_live_crawl("!where"); /* (the game's thread says the map and Link's place in the log, while he is in play) */
|
||||
#endif
|
||||
InterlockedExchange(&capture_state, 1);
|
||||
hos_save_share_start();
|
||||
hos_save_share_wake();
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* F8 in the game's window. Not when the player bound F8 to one of the controls (none is by default): the key is the
|
||||
game's then, and the System tab's row still takes a capture */
|
||||
void hos_bug_capture_key(void) {
|
||||
for (int button = 0; button < HOS_KEY_N; button++)
|
||||
for (int slot = 0; slot < 2; slot++)
|
||||
if (hos_binding(HOS_BIND_KEY, button, slot) == VK_F8) {
|
||||
static volatile LONG said;
|
||||
if (!InterlockedExchange(&said, 1))
|
||||
hos_log("capture: F8 is bound to one of the controls: no capture from the key (the System tab's Bug capture row takes one)");
|
||||
return;
|
||||
}
|
||||
hos_bug_capture_ask("F8");
|
||||
}
|
||||
|
||||
/* ---- the frames shown, a tenth of a second at a time (the save rows' thread, each time round) ---- */
|
||||
static struct {
|
||||
ULONGLONG ms;
|
||||
LONG64 presents;
|
||||
} ticks[TICKS];
|
||||
static unsigned tick_n;
|
||||
|
||||
void hos_bug_capture_tick(void) {
|
||||
const ULONGLONG now = GetTickCount64();
|
||||
if (tick_n && now - ticks[(tick_n - 1) % TICKS].ms < 50) return; /* (woken early: not a tenth of a second yet) */
|
||||
ticks[tick_n % TICKS].ms = now;
|
||||
ticks[tick_n % TICKS].presents = InterlockedCompareExchange64(&hos_present_count, 0, 0);
|
||||
tick_n++;
|
||||
}
|
||||
|
||||
/* The frame rate over the span_ms before the press (span_ms > 0): from the newest tick that far back to the press itself,
|
||||
whose own count and time the press noted; -1 when the ticks do not reach that far back */
|
||||
static double rate_before(unsigned span_ms) {
|
||||
const unsigned kept = tick_n < TICKS ? tick_n : TICKS;
|
||||
for (unsigned k = 0; k < kept; k++) { /* (newest first) */
|
||||
const unsigned i = (tick_n - 1 - k) % TICKS;
|
||||
if (ticks[i].ms + span_ms > asked.tick || ticks[i].presents > asked.presents) continue;
|
||||
return (double)(asked.presents - ticks[i].presents) * 1000.0 / (double)(asked.tick - ticks[i].ms);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ---- files ---- */
|
||||
/* Up to max bytes of a file, from its start or its end: malloc'd with a NUL after them, NULL when it is not there */
|
||||
static char *read_part(const char *path, size_t max, int from_end, size_t *n) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
_fseeki64(f, 0, SEEK_END);
|
||||
const long long size = _ftelli64(f);
|
||||
const size_t want = size < 0 ? 0 : (unsigned long long)size > max ? max : (size_t)size;
|
||||
char *data = (char *)malloc(want + 1);
|
||||
_fseeki64(f, from_end ? size - (long long)want : 0, SEEK_SET);
|
||||
*n = data ? fread(data, 1, want, f) : 0;
|
||||
fclose(f);
|
||||
if (data) data[*n] = 0;
|
||||
return data;
|
||||
}
|
||||
|
||||
static int write_file(const char *path, const void *data, size_t n) {
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return 0;
|
||||
const size_t put = fwrite(data, 1, n, f);
|
||||
return !fclose(f) && put == n;
|
||||
}
|
||||
|
||||
/* Where the last `lines` lines of text[0..n) begin */
|
||||
static const char *last_lines(const char *text, size_t n, int lines) {
|
||||
const char *p = text + n;
|
||||
if (p > text && p[-1] == '\n') p--; /* (the last line's own end) */
|
||||
while (p > text) {
|
||||
if (p[-1] == '\n' && --lines == 0) break;
|
||||
p--;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
/* A line of the log without its "[hos <thread>] " and its end, into out. Returns the line after it */
|
||||
static const char *log_line(const char *line, char *out, size_t cap) {
|
||||
const char *end = strchr(line, '\n');
|
||||
if (!end) end = line + strlen(line);
|
||||
const char *from = line;
|
||||
if (!strncmp(line, "[hos ", 5)) {
|
||||
const char *close = strstr(line, "] ");
|
||||
if (close && close < end) from = close + 2;
|
||||
}
|
||||
size_t len = (size_t)(end - from);
|
||||
while (len && (from[len - 1] == '\r' || from[len - 1] == ' ')) len--;
|
||||
snprintf(out, cap, "%.*s", (int)len, from);
|
||||
return *end ? end + 1 : end;
|
||||
}
|
||||
|
||||
/* ---- the capture, on the save rows' thread ---- */
|
||||
void hos_bug_capture_work(void) {
|
||||
if (InterlockedCompareExchange(&capture_state, 2, 1) != 1) return;
|
||||
const ULONGLONG began = GetTickCount64();
|
||||
char game[MAX_PATH], name[48], dir[CAPTURE_PATH], path[CAPTURE_PATH + 64], other[CAPTURE_PATH + 64], line[CAPTURE_PATH + 200];
|
||||
struct tm local, utc;
|
||||
localtime_s(&local, &asked.when);
|
||||
gmtime_s(&utc, &asked.when);
|
||||
int made = 0;
|
||||
if (hos_share_game_dir(game, sizeof game)) {
|
||||
snprintf(dir, sizeof dir, "%s" SEP "captures", game);
|
||||
CreateDirectoryA(dir, NULL);
|
||||
for (int k = 1; k < 100 && !made; k++) { /* (two in one second: "-2") */
|
||||
const int len = snprintf(name, sizeof name, k == 1 ? "%04d%02d%02d-%02d%02d%02d" : "%04d%02d%02d-%02d%02d%02d-%d", local.tm_year + 1900,
|
||||
local.tm_mon + 1, local.tm_mday, local.tm_hour, local.tm_min, local.tm_sec, k);
|
||||
if (len < 0 || snprintf(dir, sizeof dir, "%s" SEP "captures" SEP "%s", game, name) >= (int)sizeof dir) break;
|
||||
made = CreateDirectoryA(dir, NULL) != 0;
|
||||
}
|
||||
}
|
||||
if (!made) {
|
||||
rx_option_set(HOS_OPT_BUG_SHOT, 0);
|
||||
hos_log("capture: bug capture %d: no folder could be made under the game folder's captures: nothing was written", asked.number);
|
||||
hos_save_share_say(HOS_SHARE_CAPTURE, "The capture's folder could not be made in the game folder: nothing was written.");
|
||||
InterlockedExchange(&capture_state, 0);
|
||||
return;
|
||||
}
|
||||
hos_log("capture: bug capture %d goes to %s", asked.number, dir); /* (the one line that says where) */
|
||||
|
||||
/* the settings, and the save as Export save writes it */
|
||||
size_t n = 0;
|
||||
snprintf(path, sizeof path, "%s" SEP "triforce_options.ini", game);
|
||||
char *ini = read_part(path, 1u << 20, 0, &n);
|
||||
snprintf(path, sizeof path, "%s" SEP "triforce_options.ini", dir);
|
||||
const int have_ini = ini && write_file(path, ini, n);
|
||||
free(ini);
|
||||
int files = 0;
|
||||
char zip[CAPTURE_PATH + 64];
|
||||
const int have_save = hos_save_export(dir, 0, zip, sizeof zip, &files);
|
||||
|
||||
/* the picture: the renderer writes it once the frame it copied is done */
|
||||
int shot = 1;
|
||||
for (unsigned waited = 0; waited < SHOT_WAIT_MS && (shot = rx_option(HOS_OPT_BUG_SHOT, 3)) == 1; waited += 20) Sleep(20);
|
||||
if (shot == 1) rx_option_set(HOS_OPT_BUG_SHOT, 0); /* (no frame was shown in that time: asked no longer) */
|
||||
snprintf(other, sizeof other, "%s" SEP "captures" SEP "frame-pending.png", game);
|
||||
snprintf(path, sizeof path, "%s" SEP "frame.png", dir);
|
||||
const int have_picture = shot == 2 && MoveFileExA(other, path, MOVEFILE_REPLACE_EXISTING) != 0;
|
||||
|
||||
/* the log: its end as log-tail.txt, and in it what the game's thread said of Link's place since the press */
|
||||
while (GetTickCount64() - asked.tick < 300) Sleep(20); /* (a few frames, for that answer) */
|
||||
hos_log_flush();
|
||||
char place[400] = "";
|
||||
snprintf(path, sizeof path, "%s" SEP "triforce_log.txt", game);
|
||||
char *tail = read_part(path, TAIL_BYTES, 1, &n);
|
||||
int have_log = 0;
|
||||
if (tail) {
|
||||
const char *from = last_lines(tail, n, TAIL_LINES);
|
||||
snprintf(other, sizeof other, "%s" SEP "log-tail.txt", dir);
|
||||
have_log = write_file(other, from, (size_t)(tail + n - from));
|
||||
char mark[64]; /* (the press's own line: a capture's number is the run's, and the log is the run's) */
|
||||
snprintf(mark, sizeof mark, "capture: bug capture %d asked for (", asked.number);
|
||||
const char *mine = NULL;
|
||||
for (const char *p = tail; (p = strstr(p, mark)) != NULL; p++) mine = p;
|
||||
const char *where = mine ? strstr(mine, "cheats: crawl: where: ") : NULL;
|
||||
if (where) log_line(where + strlen("cheats: crawl: where: "), place, sizeof place);
|
||||
}
|
||||
|
||||
/* capture.txt, last */
|
||||
snprintf(path, sizeof path, "%s" SEP "capture.txt.part", dir);
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (f) {
|
||||
fprintf(f, "Triforce bug capture %s\n\n", name);
|
||||
fprintf(f, "time = %04d-%02d-%02d %02d:%02d:%02d (%04d-%02d-%02dT%02d:%02d:%02dZ)\n", local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
|
||||
local.tm_hour, local.tm_min, local.tm_sec, utc.tm_year + 1900, utc.tm_mon + 1, utc.tm_mday, utc.tm_hour, utc.tm_min, utc.tm_sec);
|
||||
fprintf(f, "trigger = %s\n", asked.trigger);
|
||||
fprintf(f, "version = %s\n", hos_version());
|
||||
/* (the build, the GPUs and the driver, as the log's first lines give them) */
|
||||
snprintf(other, sizeof other, "%s" SEP "triforce_log.txt", game);
|
||||
size_t hn = 0;
|
||||
char *head = read_part(other, HEAD_BYTES, 0, &hn);
|
||||
int head_lines = 0;
|
||||
for (const char *p = head; p && *p && head_lines < 24;) {
|
||||
static const char *const says[] = {"] triforce ", "vk: device ", "; mesh shaders ", "vk: vendor fixes", "rx: renderer:",
|
||||
"oe: output resolution", "vk: swapchain ", "vk: HDR:"};
|
||||
const char *end = strchr(p, '\n');
|
||||
const size_t len = end ? (size_t)(end - p) : strlen(p);
|
||||
for (size_t i = 0; i < sizeof says / sizeof *says; i++) {
|
||||
const char *hit = strstr(p, says[i]);
|
||||
if (!hit || hit >= p + len) continue;
|
||||
log_line(p, line, sizeof line);
|
||||
fprintf(f, "log = %s\n", line);
|
||||
head_lines++;
|
||||
break;
|
||||
}
|
||||
p = end ? end + 1 : p + len;
|
||||
}
|
||||
if (!head) fprintf(f, "log = none (triforce_log.txt is not in the game folder: the game was started with its output sent elsewhere)\n");
|
||||
free(head);
|
||||
fprintf(f, "\npresents = %lld (the game's frames shown up to the press)\n", (long long)asked.presents);
|
||||
const double second = rate_before(1000), ten = rate_before(10000);
|
||||
if (second >= 0) fprintf(f, "frame rate = %.1f a second over the second before the press\n", second);
|
||||
else fprintf(f, "frame rate = not known (the press came in the game's first second)\n");
|
||||
if (ten >= 0) fprintf(f, "frame rate, ten seconds = %.1f a second\n", ten);
|
||||
{ /* (the last three seconds a tenth at a time, oldest first: a stutter shows as a low count. A span that was not a
|
||||
tenth, the thread busy with an export or woken early, is written with its length) */
|
||||
const unsigned kept = tick_n < TICKS ? tick_n : TICKS;
|
||||
int shown = 0;
|
||||
fprintf(f, "frames each tenth of a second before the press =");
|
||||
for (unsigned k = kept < 31 ? kept : 31; k >= 2; k--) {
|
||||
const unsigned a = (tick_n - k) % TICKS, b = (tick_n - k + 1) % TICKS;
|
||||
if (ticks[b].ms >= asked.tick || ticks[b].presents > asked.presents) break; /* (at the press, or after it) */
|
||||
const ULONGLONG span = ticks[b].ms - ticks[a].ms;
|
||||
if (span >= 80 && span <= 125) fprintf(f, " %lld", (long long)(ticks[b].presents - ticks[a].presents));
|
||||
else fprintf(f, " %lld/%llums", (long long)(ticks[b].presents - ticks[a].presents), (unsigned long long)span);
|
||||
shown++;
|
||||
}
|
||||
fprintf(f, shown ? "\n" : " not known\n");
|
||||
}
|
||||
if (place[0]) fprintf(f, "\nplace = %s\n", place);
|
||||
else fprintf(f, "\nplace = not known (Link is not in play: the title screen, a menu before a save is loaded, or a loading screen)\n");
|
||||
fprintf(f, "camera = not in a capture\n\n");
|
||||
if (have_picture) fprintf(f, "picture = frame.png (the frame shown after the press, without the overlays)\n");
|
||||
else
|
||||
fprintf(f, "picture = none (%s)\n", shot == 1 ? "no frame was shown in three seconds"
|
||||
: "the renderer could not read the window's picture: the log says why");
|
||||
fprintf(f, "settings = %s\n", have_ini ? "triforce_options.ini" : "none (the game folder has no triforce_options.ini yet)");
|
||||
if (have_save) {
|
||||
const char *base = strrchr(zip, SEP[0]);
|
||||
fprintf(f, "save = %s (%d files, the save the next start would load)\n", base ? base + 1 : zip, files);
|
||||
} else {
|
||||
fprintf(f, "save = none (%s)\n", files == 0 ? "the game has not saved yet" : files == -1 ? "a file of it could not be read"
|
||||
: files == -2 ? "it is written in place in this run" : "its zip could not be written");
|
||||
}
|
||||
fprintf(f, "log tail = %s\n", have_log ? "log-tail.txt (the last 400 lines)" : "none (triforce_log.txt is not in the game folder)");
|
||||
const int whole = !fclose(f);
|
||||
snprintf(other, sizeof other, "%s" SEP "capture.txt", dir);
|
||||
if (!whole || !MoveFileExA(path, other, MOVEFILE_REPLACE_EXISTING)) f = NULL;
|
||||
}
|
||||
free(tail);
|
||||
hos_log("capture: bug capture %d written in %llu ms: picture %s, save %s, settings %s%s", asked.number,
|
||||
(unsigned long long)(GetTickCount64() - began), have_picture ? "yes" : "no", have_save ? "yes" : "no", have_ini ? "yes" : "no",
|
||||
f ? "" : "; capture.txt could not be written");
|
||||
snprintf(line, sizeof line, "Saved in %s%s", dir, have_picture ? "" : " (without a picture: the log says why)");
|
||||
hos_save_share_say(HOS_SHARE_CAPTURE, line);
|
||||
snprintf(line, sizeof line, "Bug capture saved: captures" SEP "%s", name);
|
||||
hos_save_share_say(HOS_SHARE_NOTICE, line);
|
||||
InterlockedExchange(&capture_state, 0);
|
||||
}
|
||||
@@ -21,6 +21,7 @@ void rx_close_asked(void); /* host/rx_backend.c */
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
|
||||
void hos_relaunch_ask(void); /* (host/hos_relaunch.c) */
|
||||
void hos_bug_capture_key(void); /* (host/bug_capture.c) */
|
||||
void hos_window_closed_exit(void);
|
||||
#ifndef WM_DPICHANGED
|
||||
#define WM_DPICHANGED 0x02E0
|
||||
@@ -211,10 +212,14 @@ int hos_mouse_game_pointer(int *x, int *y, int *w, int *h, uint32_t *pressed, in
|
||||
if (!(ptr_in && GetForegroundWindow() == hwnd && *w > 0 && *h > 0)) return 0;
|
||||
return shown ? 1 : 2;
|
||||
}
|
||||
int hos_oot_options_front(void); /* (host/oot_menu.c) */
|
||||
static void game_menu_press(LPARAM lp, LONG bit) {
|
||||
ptr_x = (short)LOWORD(lp), ptr_y = (short)HIWORD(lp), ptr_in = 1;
|
||||
gm_mouse_used();
|
||||
InterlockedOr(&gm_pressed, bit);
|
||||
/* ((Triforce) the game's Options screen: the options' tabs over its header (gpu/vk/vk_menu.cpp draw_game_tab) take their
|
||||
clicks through hos_mouse_pointer as the open menu does; host/oot_menu.c leaves the header to them, the rest is the game's) */
|
||||
if (bit == HOS_MOUSE_LEFT && hos_oot_options_front()) InterlockedOr(&ptr_pressed, bit);
|
||||
}
|
||||
static void menu_press(HWND w, LPARAM lp, LONG bit) { /* (a click while the menu is open: the menu's) */
|
||||
ptr_x = (short)LOWORD(lp), ptr_y = (short)HIWORD(lp), ptr_in = 1;
|
||||
@@ -222,6 +227,41 @@ static void menu_press(HWND w, LPARAM lp, LONG bit) { /* (a click while the me
|
||||
SetCapture(w); /* (a drag past the window's edge still reaches it) */
|
||||
}
|
||||
|
||||
/* ((Triforce) 11-17) Text typed while the menu wants it (the Mods screen's search: rx_option HOS_OPT_TEXT_WANTED), to the menu
|
||||
as code points (HOS_OPT_TEXT_CHAR): the characters, 8 Backspace, 127 Ctrl+Backspace; Ctrl+V the clipboard's text. Enter, Esc
|
||||
and Tab stay the menu's keys. The window is an ANSI one: a character's byte is in the code page of the keyboard's layout */
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
static void typed_text_paste(HWND w) {
|
||||
if (!IsClipboardFormatAvailable(CF_UNICODETEXT) || !OpenClipboard(w)) return;
|
||||
HANDLE h = GetClipboardData(CF_UNICODETEXT);
|
||||
const wchar_t *t = h ? (const wchar_t *)GlobalLock(h) : NULL;
|
||||
for (int i = 0, n = 0; t && t[i] && n < 100; i++, n++) {
|
||||
unsigned cp = t[i];
|
||||
if (cp >= 0xD800 && cp < 0xDC00 && t[i + 1] >= 0xDC00 && t[i + 1] < 0xE000) cp = 0x10000 + ((cp - 0xD800) << 10) + (t[++i] - 0xDC00);
|
||||
else if (cp >= 0xD800 && cp < 0xE000) continue; /* (half of a pair alone: no character) */
|
||||
if (cp < 0x20 || cp == 0x7f) cp = ' '; /* (a line's end, a tab, a DEL: a space, never the menu's own codes) */
|
||||
rx_option_set(HOS_OPT_TEXT_CHAR, (int)cp);
|
||||
}
|
||||
if (h) GlobalUnlock(h);
|
||||
CloseClipboard();
|
||||
}
|
||||
static void typed_text(HWND w, WPARAM wp) {
|
||||
if (!menu_open || !rx_option(HOS_OPT_TEXT_WANTED, 0)) return;
|
||||
if (wp == 0x16) { typed_text_paste(w); return; } /* (Ctrl+V) */
|
||||
if (wp == 8 || wp == 127) { rx_option_set(HOS_OPT_TEXT_CHAR, (int)wp); return; }
|
||||
if (wp < 0x20) return; /* (Enter, Esc, Tab and the other control keys) */
|
||||
wchar_t u = (wchar_t)wp;
|
||||
if (wp >= 0x80) {
|
||||
char cp[8] = "", b = (char)wp;
|
||||
const LCID layout = (LCID)(LOWORD((uintptr_t)GetKeyboardLayout(0)));
|
||||
/* (no layout's code page: the byte left out, never read in the system's own: tests/test_utf8_paths.py) */
|
||||
const UINT page = GetLocaleInfoA(layout, LOCALE_IDEFAULTANSICODEPAGE, cp, sizeof cp) > 0 ? (UINT)atoi(cp) : 0;
|
||||
if (!page || MultiByteToWideChar(page, 0, &b, 1, &u, 1) != 1) return;
|
||||
}
|
||||
rx_option_set(HOS_OPT_TEXT_CHAR, (int)u);
|
||||
}
|
||||
|
||||
static LRESULT CALLBACK wndproc(HWND w, UINT msg, WPARAM wp, LPARAM lp) {
|
||||
switch (msg) {
|
||||
case WM_MOUSEMOVE:
|
||||
@@ -314,6 +354,8 @@ static LRESULT CALLBACK wndproc(HWND w, UINT msg, WPARAM wp, LPARAM lp) {
|
||||
extern void hos_vk_note_key_press(unsigned key);
|
||||
if (!(lp & (1 << 30))) hos_vk_note_key_press((unsigned)wp);
|
||||
if (wp == VK_ESCAPE) mouse_capture(w, 0);
|
||||
/* (Triforce, after NX2navi's F8) a bug capture: only noted here, written on a thread of its own (host/bug_capture.c) */
|
||||
if (wp == VK_F8 && !(lp & (1 << 30))) hos_bug_capture_key();
|
||||
/* (Triforce: F11 does nothing; F10 and Alt+Enter switch fullscreen, below: F10 comes as a system key) */
|
||||
return 0;
|
||||
}
|
||||
@@ -329,6 +371,9 @@ static LRESULT CALLBACK wndproc(HWND w, UINT msg, WPARAM wp, LPARAM lp) {
|
||||
case WM_SYSCHAR: /* (its character: without this Windows beeps, as for an Alt key with no menu) */
|
||||
if (wp == '\r') return 0;
|
||||
break;
|
||||
case WM_CHAR: /* ((Triforce) a character typed: the menu's when it takes text) */
|
||||
typed_text(w, wp);
|
||||
return 0;
|
||||
case WM_APP + 7: /* (the Insert menu's row) */
|
||||
set_fullscreen(w, (int)wp);
|
||||
return 0;
|
||||
@@ -422,7 +467,10 @@ static DWORD WINAPI display_main(void *p) {
|
||||
int win_w = hos_aspect_ratio > 0.0f ? (int)(720 * hos_aspect_ratio + 0.5f) : 1280, win_h = 720;
|
||||
if (getenv("HOS_WINDOW_SIZE")) {
|
||||
int w = 0, h = 0;
|
||||
if (sscanf(getenv("HOS_WINDOW_SIZE"), "%dx%d", &w, &h) == 2 && w >= 64 && h >= 64) win_w = w, win_h = h;
|
||||
/* ((Triforce) at most 16384 a side: the window's rectangle with its frame, and the size times the screen's dpi
|
||||
below, are ints, and wrapped for a huge one) */
|
||||
if (sscanf(getenv("HOS_WINDOW_SIZE"), "%dx%d", &w, &h) == 2 && w >= 64 && h >= 64 && w <= 16384 && h <= 16384)
|
||||
win_w = w, win_h = h;
|
||||
}
|
||||
RECT r = {0, 0, win_w, win_h};
|
||||
AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE);
|
||||
@@ -433,7 +481,7 @@ static DWORD WINAPI display_main(void *p) {
|
||||
theme_caption(hwnd);
|
||||
if (window_dpi(hwnd) != 96) {
|
||||
const UINT dpi = window_dpi(hwnd);
|
||||
RECT s = {0, 0, (LONG)(win_w * dpi / 96), (LONG)(win_h * dpi / 96)};
|
||||
RECT s = {0, 0, (LONG)((int64_t)win_w * dpi / 96), (LONG)((int64_t)win_h * dpi / 96)};
|
||||
AdjustWindowRect(&s, WS_OVERLAPPEDWINDOW, FALSE);
|
||||
SetWindowPos(hwnd, NULL, 0, 0, s.right - s.left, s.bottom - s.top, SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
|
||||
}
|
||||
|
||||
21
host/guest_bounds.h
Normal file
21
host/guest_bounds.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/* (Triforce) Bounds checks on what the game passes in that cannot overflow. A check written as `first + n > cap` wraps
|
||||
around when the game's offset or count is huge (in 32 bits for ints, in 64 for addresses) and lets the range through;
|
||||
these compare by subtraction, in 64 bits, so a range that does not fit is refused whatever its size.
|
||||
Plain functions: tests/test_guest_bounds.py compiles and runs them against the old expressions. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
/* [first, first + n) lies in [0, cap): a start and a count as the game gave them (ints widened, a negative one refused) */
|
||||
static inline int hos_span_ok(int64_t first, int64_t n, int64_t cap) {
|
||||
return first >= 0 && n >= 0 && first <= cap && n <= cap - first;
|
||||
}
|
||||
|
||||
/* [at, at + n) lies in [0, end): addresses and sizes, unsigned */
|
||||
static inline int hos_range_ok(uint64_t at, uint64_t n, uint64_t end) {
|
||||
return at <= end && n <= end - at;
|
||||
}
|
||||
|
||||
/* how many of the n units from x lie in a cap-wide row: n, or what is left after x (0 from cap on) */
|
||||
static inline uint32_t hos_span_clip(uint32_t x, uint32_t n, uint32_t cap) {
|
||||
return x >= cap ? 0 : (uint64_t)x + n <= cap ? n : cap - x;
|
||||
}
|
||||
279
host/guest_census.h
Normal file
279
host/guest_census.h
Normal file
@@ -0,0 +1,279 @@
|
||||
/* (Triforce) The guest memory census (hos_mem.c, mkw_glue.c): which pages of the guest window the runtime has handed out,
|
||||
which of them the game holds as pages or physical maps, and which the game let go that stay committed, and the line that
|
||||
says so every few seconds. Apart from Windows and the runtime so that tests/test_guest_census.py runs it alone.
|
||||
|
||||
Why: NX2navi's guest region above the heap grew 6.6 -> 13.3 -> 16.1 GB over 6,000 frames while the heap stayed flat,
|
||||
until the PC's commit ran out (MapPhysicalMemory ... LimitReached, then a lost device). Triforce's log had only the
|
||||
window's committed total (the renderer's "vk: memory, the parts" line, the guest window walked every eighth line): 8.7
|
||||
-> 9.2 -> 12.4 -> 14.6 GB over an owner's session of 134,000 presents, the heap 6.9 GB throughout, and nothing said
|
||||
which part held it. The game is told 9809 MB in all.
|
||||
|
||||
What is kept, a bit a 4 KB page, in 1 GB chunks made as the window's gigabytes are first used (96 KB a chunk, 512 at
|
||||
most):
|
||||
- GC_HELD: the runtime handed the page out and committed it (hos_commit: the program, the system region, the heap, the
|
||||
address regions whole, AllocateMemoryPages, MapPhysicalMemory; and a first touch of a page nothing handed out)
|
||||
- GC_GAME: of those, the pages the game holds in Horizon's own terms, as pages (AllocateMemoryPages) or physical maps
|
||||
(MapPhysicalMemory): what its 9809 MB pays for besides the heap and the program
|
||||
- GC_KEPT: pages the game let go that stay committed: zeroed instead of decommitted because a Vulkan import held them
|
||||
(FreeMemoryPages, UnmapPhysicalMemory: "decommitted pages under an import faulted the device"), and the pages of a
|
||||
freed address region (discarded: out of RAM, but still the process's commit). Nothing gave them back before: an
|
||||
import given back later left its zeroed pages committed for the rest of the session. The give-back (mkw_glue.c,
|
||||
HOS_MEM_GIVE_BACK) decommits them once no import holds them, under the renderer's import locks.
|
||||
|
||||
The bits are changed with atomic operations (the first touch's handler takes no lock); a chunk is made with GC_ALLOC,
|
||||
which the runtime points at VirtualAlloc (no heap lock in an exception handler). */
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef GC_ALLOC
|
||||
#include <stdlib.h>
|
||||
#define GC_ALLOC(n) calloc(1, (n))
|
||||
#define GC_FREE(p) free(p)
|
||||
#endif
|
||||
|
||||
enum { GC_HELD = 0, GC_GAME = 1, GC_KEPT = 2, GC_MAPS = 3 };
|
||||
#define GC_PAGE_SHIFT 12
|
||||
#define GC_CHUNK_SHIFT 30
|
||||
#define GC_CHUNK_WORDS ((1u << (GC_CHUNK_SHIFT - GC_PAGE_SHIFT)) / 64)
|
||||
#define GC_CHUNKS 512u /* (the 512 GB window, HOS_GUEST_SPACE) */
|
||||
#define GC_SPACE ((uint64_t)GC_CHUNKS << GC_CHUNK_SHIFT)
|
||||
|
||||
typedef struct {
|
||||
uint64_t bits[GC_MAPS][GC_CHUNK_WORDS];
|
||||
} GcChunk;
|
||||
typedef struct {
|
||||
GcChunk *chunk[GC_CHUNKS];
|
||||
} GcMap;
|
||||
|
||||
/* The chunk that holds page bit c's gigabyte (NULL when none was made and make is 0, or none could be made) */
|
||||
static inline GcChunk *gc_chunk(GcMap *m, uint64_t c, int make) {
|
||||
if (c >= GC_CHUNKS) return NULL;
|
||||
GcChunk *k = __atomic_load_n(&m->chunk[c], __ATOMIC_ACQUIRE);
|
||||
if (k || !make) return k;
|
||||
GcChunk *fresh = (GcChunk *)GC_ALLOC(sizeof(GcChunk));
|
||||
if (!fresh) return NULL;
|
||||
GcChunk *expected = NULL;
|
||||
if (__atomic_compare_exchange_n(&m->chunk[c], &expected, fresh, 0, __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE)) return fresh;
|
||||
GC_FREE(fresh); /* (another thread made it first) */
|
||||
return expected;
|
||||
}
|
||||
|
||||
/* Walks the whole pages of [lo, hi) (lo rounded up, hi down: a part of a page is not the page) a word at a time:
|
||||
body for each word's bits (chunk, word_at, mask). A chunk not made (make 0), or that could not be made, is stepped over
|
||||
whole (Triforce: the census's walks of the 512 GB window had gone through its 2 million words a map, chunk or none, some
|
||||
10 million steps a census; now they cost the gigabytes in use) */
|
||||
#define GC_WALK(m, lo, hi, make, body) \
|
||||
do { \
|
||||
uint64_t gc_p = ((lo) + 0xFFFULL) >> GC_PAGE_SHIFT, gc_e = (hi) >> GC_PAGE_SHIFT; \
|
||||
if (gc_e > (GC_SPACE >> GC_PAGE_SHIFT)) gc_e = GC_SPACE >> GC_PAGE_SHIFT; \
|
||||
while (gc_p < gc_e) { \
|
||||
const uint64_t gc_c = gc_p >> (GC_CHUNK_SHIFT - GC_PAGE_SHIFT); \
|
||||
const uint64_t gc_cend = (gc_c + 1) << (GC_CHUNK_SHIFT - GC_PAGE_SHIFT); \
|
||||
const uint64_t gc_stop = gc_e < gc_cend ? gc_e : gc_cend; \
|
||||
GcChunk *gc_k = gc_chunk((m), gc_c, (make)); \
|
||||
if (!gc_k) gc_p = gc_stop; /* (no bits there: on to the chunk's end, or to hi's page in it) */ \
|
||||
while (gc_p < gc_stop) { \
|
||||
const uint64_t gc_w = (gc_p & ((1u << (GC_CHUNK_SHIFT - GC_PAGE_SHIFT)) - 1)) >> 6, gc_b = gc_p & 63; \
|
||||
const uint64_t gc_n = gc_stop - gc_p < 64 - gc_b ? gc_stop - gc_p : 64 - gc_b; \
|
||||
const uint64_t mask = (gc_n == 64 ? ~0ULL : ((1ULL << gc_n) - 1)) << gc_b; \
|
||||
const uint64_t word_at = gc_w; \
|
||||
GcChunk *chunk = gc_k; \
|
||||
(void)word_at, (void)chunk; \
|
||||
body; \
|
||||
gc_p += gc_n; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
/* Sets (set 1) or clears (set 0) one map's bits for the whole pages of [lo, hi); the bytes whose bit changed */
|
||||
static inline uint64_t gc_change(GcMap *m, int map, uint64_t lo, uint64_t hi, int set) {
|
||||
uint64_t changed = 0;
|
||||
if (hi <= lo) return 0;
|
||||
GC_WALK(m, lo, hi, set, {
|
||||
if (chunk) {
|
||||
uint64_t *w = &chunk->bits[map][word_at];
|
||||
if (set) changed += (uint64_t)__builtin_popcountll(mask & ~__atomic_fetch_or(w, mask, __ATOMIC_ACQ_REL));
|
||||
else changed += (uint64_t)__builtin_popcountll(mask & __atomic_fetch_and(w, ~mask, __ATOMIC_ACQ_REL));
|
||||
}
|
||||
});
|
||||
return changed << GC_PAGE_SHIFT;
|
||||
}
|
||||
|
||||
/* Moves one map's set bits over [lo, hi) into another (cleared in from, set in to); the bytes moved */
|
||||
static inline uint64_t gc_move(GcMap *m, int from, int to, uint64_t lo, uint64_t hi) {
|
||||
uint64_t moved = 0;
|
||||
if (hi <= lo) return 0;
|
||||
GC_WALK(m, lo, hi, 0, {
|
||||
if (chunk) {
|
||||
const uint64_t had = mask & __atomic_fetch_and(&chunk->bits[from][word_at], ~mask, __ATOMIC_ACQ_REL);
|
||||
if (had) __atomic_fetch_or(&chunk->bits[to][word_at], had, __ATOMIC_ACQ_REL);
|
||||
moved += (uint64_t)__builtin_popcountll(had);
|
||||
}
|
||||
});
|
||||
return moved << GC_PAGE_SHIFT;
|
||||
}
|
||||
|
||||
/* (Triforce) hos_commit's held pages (hos_mem.c hos_mem_commit_begin): gc_words(lo, hi) words, one a word of the range's
|
||||
pages from lo's on; gc_set_keeping sets one map's bits over [lo, hi) and says in had which were set already (had starts
|
||||
all ones: a word it never reaches, its chunk not made, set nothing), and gc_unset_had clears the bits it set, no other */
|
||||
static inline uint64_t gc_words(uint64_t lo, uint64_t hi) {
|
||||
const uint64_t p = (lo + 0xFFFULL) >> GC_PAGE_SHIFT, e = hi >> GC_PAGE_SHIFT;
|
||||
return e > p ? ((e - 1) >> 6) - (p >> 6) + 1 : 0;
|
||||
}
|
||||
static inline void gc_set_keeping(GcMap *m, int map, uint64_t lo, uint64_t hi, uint64_t *had) {
|
||||
const uint64_t first = ((lo + 0xFFFULL) >> GC_PAGE_SHIFT) >> 6;
|
||||
if (hi <= lo) return;
|
||||
GC_WALK(m, lo, hi, 1, { /* (gc_p: the walk's page) */
|
||||
if (chunk) had[(gc_p >> 6) - first] = mask & __atomic_fetch_or(&chunk->bits[map][word_at], mask, __ATOMIC_ACQ_REL);
|
||||
});
|
||||
}
|
||||
static inline void gc_unset_had(GcMap *m, int map, uint64_t lo, uint64_t hi, const uint64_t *had) {
|
||||
const uint64_t first = ((lo + 0xFFFULL) >> GC_PAGE_SHIFT) >> 6;
|
||||
if (hi <= lo) return;
|
||||
GC_WALK(m, lo, hi, 0, {
|
||||
const uint64_t set = mask & ~had[(gc_p >> 6) - first];
|
||||
if (chunk && set) __atomic_fetch_and(&chunk->bits[map][word_at], ~set, __ATOMIC_ACQ_REL);
|
||||
});
|
||||
}
|
||||
|
||||
/* The bytes of [lo, hi) whose bit is set in one map */
|
||||
static inline uint64_t gc_count(GcMap *m, int map, uint64_t lo, uint64_t hi) {
|
||||
uint64_t n = 0;
|
||||
if (hi <= lo) return 0;
|
||||
GC_WALK(m, lo, hi, 0, {
|
||||
if (chunk) n += (uint64_t)__builtin_popcountll(mask & __atomic_load_n(&chunk->bits[map][word_at], __ATOMIC_ACQUIRE));
|
||||
});
|
||||
return n << GC_PAGE_SHIFT;
|
||||
}
|
||||
|
||||
/* The next run of set bits of one map at or after from (and below hi), at most max bytes long: 1 and [*rlo, *rhi), or 0
|
||||
when there is none. Chunks never made and empty words are stepped over whole */
|
||||
static inline int gc_next_run(GcMap *m, int map, uint64_t from, uint64_t hi, uint64_t max, uint64_t *rlo, uint64_t *rhi) {
|
||||
uint64_t p = (from + 0xFFFULL) >> GC_PAGE_SHIFT, e = hi >> GC_PAGE_SHIFT;
|
||||
const uint64_t per = 1u << (GC_CHUNK_SHIFT - GC_PAGE_SHIFT);
|
||||
if (e > (GC_SPACE >> GC_PAGE_SHIFT)) e = GC_SPACE >> GC_PAGE_SHIFT;
|
||||
uint64_t start = 0;
|
||||
int in = 0;
|
||||
while (p < e) {
|
||||
GcChunk *k = gc_chunk(m, p / per, 0);
|
||||
if (!k) {
|
||||
if (in) break;
|
||||
p = (p / per + 1) * per;
|
||||
continue;
|
||||
}
|
||||
const uint64_t w = __atomic_load_n(&k->bits[map][(p % per) >> 6], __ATOMIC_ACQUIRE) >> (p & 63);
|
||||
if (!in) {
|
||||
if (!w) { /* (the rest of this word is clear) */
|
||||
p = (p | 63) + 1;
|
||||
continue;
|
||||
}
|
||||
p += (uint64_t)__builtin_ctzll(w);
|
||||
if (p >= e) break;
|
||||
start = p, in = 1;
|
||||
continue;
|
||||
}
|
||||
if (!(w & 1)) break;
|
||||
const uint64_t ones = ~w ? (uint64_t)__builtin_ctzll(~w) : 64 - (p & 63); /* (the set bits from p in this word) */
|
||||
p += ones;
|
||||
if (max && (p - start) << GC_PAGE_SHIFT >= max) {
|
||||
p = start + (max >> GC_PAGE_SHIFT);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!in) return 0;
|
||||
if (p > e) p = e;
|
||||
*rlo = start << GC_PAGE_SHIFT, *rhi = p << GC_PAGE_SHIFT;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ---- the census: the counts by kind, and its log line */
|
||||
typedef struct {
|
||||
uint64_t held, low, heap, heap_size, regions, region_span, region_game, outside, outside_game, game, kept;
|
||||
int64_t nregions;
|
||||
uint64_t touched; /* (first touches so far, 64 KB each) */
|
||||
uint64_t kept_import, kept_discard, given_back, given_runs, refused; /* (so far) */
|
||||
uint64_t budget, program; /* (what the game is told it has, hos_total_memory; the program) */
|
||||
int64_t proc_commit, proc_peak, working_set, sys_commit, sys_limit, ram_avail, ram_total; /* (-1: not known) */
|
||||
} GcCensus;
|
||||
|
||||
/* The counts from the maps: regions[i] = {address, size} of the live address regions, the heap at heap_lo, heap_size long.
|
||||
Whatever is handed out below the heap and not in a region is the program's and the system region's; above it, not in
|
||||
the heap or a region, the physical maps', the pages the game took outside its regions', and first touches' */
|
||||
static inline void gc_census_count(GcMap *m, const uint64_t (*regions)[2], int n, uint64_t heap_lo, uint64_t heap_size,
|
||||
GcCensus *c) {
|
||||
c->held = gc_count(m, GC_HELD, 0, GC_SPACE);
|
||||
c->game = gc_count(m, GC_GAME, 0, GC_SPACE);
|
||||
c->kept = gc_count(m, GC_KEPT, 0, GC_SPACE);
|
||||
const uint64_t low_all = gc_count(m, GC_HELD, 0, heap_lo);
|
||||
c->heap = gc_count(m, GC_HELD, heap_lo, heap_lo + heap_size), c->heap_size = heap_size;
|
||||
uint64_t regions_low = 0;
|
||||
c->regions = c->region_span = c->region_game = 0;
|
||||
c->nregions = n;
|
||||
for (int i = 0; i < n; i++) {
|
||||
const uint64_t lo = regions[i][0], hi = regions[i][0] + regions[i][1];
|
||||
const uint64_t h = gc_count(m, GC_HELD, lo, hi);
|
||||
c->regions += h, c->region_span += regions[i][1];
|
||||
c->region_game += gc_count(m, GC_GAME, lo, hi);
|
||||
if (lo < heap_lo) regions_low += h;
|
||||
}
|
||||
c->low = low_all > regions_low ? low_all - regions_low : 0;
|
||||
const uint64_t accounted = low_all + c->heap + (c->regions - regions_low);
|
||||
c->outside = c->held > accounted ? c->held - accounted : 0;
|
||||
c->outside_game = c->game > c->region_game ? c->game - c->region_game : 0;
|
||||
}
|
||||
|
||||
static inline long long gc_mb(int64_t b) { return b < 0 ? -1 : (long long)(b >> 20); }
|
||||
|
||||
/* The line, without its "mem: " head (under a thousand characters: hos_log's line is 1024). The game's own count is what
|
||||
it counts against what it is told it has: the heap, the program, and its pages and physical maps */
|
||||
static inline int gc_line(char *out, size_t size, const GcCensus *c) {
|
||||
const uint64_t own = c->heap_size + c->program + c->game;
|
||||
char sys[64] = "?";
|
||||
if (c->sys_commit >= 0 && c->sys_limit > 0) snprintf(sys, sizeof sys, "%lld of %lld", gc_mb(c->sys_commit), gc_mb(c->sys_limit));
|
||||
return snprintf(out, size,
|
||||
"census (MB): handed out %lld: the program and system %lld, heap %lld of %lld, address regions %lld in %lld "
|
||||
"(spanning %lld; the game's pages in them %lld), outside them %lld (the game's pages and physical maps %lld; "
|
||||
"%llu first touches); the game's own count %lld of its %lld; kept committed after the game let go %lld (so far "
|
||||
"%lld zeroed under imports, %lld discarded; given back %lld in %llu runs, %llu runs found under an import); "
|
||||
"process commit %lld (peak %lld), working set %lld; the PC's commit %s, RAM %lld of %lld available",
|
||||
gc_mb((int64_t)c->held), gc_mb((int64_t)c->low), gc_mb((int64_t)c->heap), gc_mb((int64_t)c->heap_size),
|
||||
gc_mb((int64_t)c->regions), (long long)c->nregions, gc_mb((int64_t)c->region_span),
|
||||
gc_mb((int64_t)c->region_game), gc_mb((int64_t)c->outside), gc_mb((int64_t)c->outside_game),
|
||||
(unsigned long long)c->touched, gc_mb((int64_t)own), gc_mb((int64_t)c->budget), gc_mb((int64_t)c->kept),
|
||||
gc_mb((int64_t)c->kept_import), gc_mb((int64_t)c->kept_discard), gc_mb((int64_t)c->given_back),
|
||||
(unsigned long long)c->given_runs, (unsigned long long)c->refused, gc_mb(c->proc_commit), gc_mb(c->proc_peak),
|
||||
gc_mb(c->working_set), sys, gc_mb(c->ram_avail), gc_mb(c->ram_total));
|
||||
}
|
||||
|
||||
/* ---- the give-back: the kept pages, decommitted once no import holds them */
|
||||
/* release(ctx, lo, hi): 1 when [lo, hi) was given back, 0 when not (an import holds it, or the game took a page back) */
|
||||
typedef int (*GcRelease)(void *ctx, uint64_t lo, uint64_t hi);
|
||||
|
||||
/* One pass over the kept runs at or above floor: from *cursor on (and from floor once the window's end is reached), each at
|
||||
most max_run bytes, until max_bytes are given back or max_calls runs were offered; *cursor is where the next pass starts.
|
||||
The bytes given back; *runs and *refused count the runs given back and refused. (The runtime's floor is 4 GB: below it
|
||||
AllocateMemoryPages hands out pages without committing them, hos_mem.c, so a page decommitted there could reach a host
|
||||
call uncommitted) */
|
||||
static inline uint64_t gc_give_back(GcMap *m, uint64_t floor, uint64_t *cursor, uint64_t max_run, uint64_t max_bytes,
|
||||
int max_calls, GcRelease release, void *ctx, uint64_t *runs, uint64_t *refused) {
|
||||
uint64_t given = 0, at = *cursor < floor ? floor : *cursor, lo = 0, hi = 0;
|
||||
int calls = 0, wrapped = 0;
|
||||
const uint64_t start = at;
|
||||
while (given < max_bytes && calls < max_calls) {
|
||||
const uint64_t end = wrapped ? start : GC_SPACE;
|
||||
if (!gc_next_run(m, GC_KEPT, at, end, max_run, &lo, &hi)) {
|
||||
if (wrapped || start == floor) break;
|
||||
wrapped = 1, at = floor;
|
||||
continue;
|
||||
}
|
||||
calls++;
|
||||
if (release(ctx, lo, hi)) given += hi - lo, (*runs)++;
|
||||
else (*refused)++;
|
||||
at = hi;
|
||||
}
|
||||
*cursor = at >= GC_SPACE ? floor : at;
|
||||
return given;
|
||||
}
|
||||
19
host/hos.h
19
host/hos.h
@@ -51,7 +51,22 @@ extern const char hos_main_build_id[]; /* (gen/hos_image.c: main
|
||||
hos_thread.c) run only when the lifted main is Mario Kart World's: any other game has other code there */
|
||||
int hos_game_is_mkw(void);
|
||||
#define HOS_GAME_TITLE "Triforce"
|
||||
void hos_commit(uint64_t addr, uint64_t size); /* (mkw_glue.c) guest pages made usable by host APIs */
|
||||
/* (mkw_glue.c) guest pages made usable by host APIs. (Triforce) 1: committed, 0: the commit failed (logged), -1: not in
|
||||
the guest window (logged) */
|
||||
int hos_commit(uint64_t addr, uint64_t size);
|
||||
/* (Triforce) the guest memory census (hos_mem.c, host/guest_census.h): each place that commits, maps or lets go of guest
|
||||
pages says what it did. COMMITTED: handed out and committed (before the commit itself); TOUCHED: committed on a first
|
||||
touch; PAGES, MAPPED: the game's own pages and physical maps; DECOMMITTED: let go and decommitted; ZEROED: let go and
|
||||
zeroed in place (an import holds them); DISCARDED: let go, out of RAM but still committed */
|
||||
enum { HOS_MEM_COMMITTED, HOS_MEM_TOUCHED, HOS_MEM_PAGES, HOS_MEM_MAPPED, HOS_MEM_DECOMMITTED, HOS_MEM_ZEROED, HOS_MEM_DISCARDED };
|
||||
void hos_mem_note(int what, uint64_t addr, uint64_t size);
|
||||
int hos_mem_census_on(void); /* (the notes are kept: HOS_MEM_CENSUS or HOS_MEM_GIVE_BACK) */
|
||||
int64_t hos_mem_give_back(uint64_t max_bytes);
|
||||
void hos_mem_census_start(void); /* (mkw_glue.c: the census's thread) */
|
||||
/* (Triforce, rx_backend.c) before a call that hands the guest memory [p, p + n) to the kernel or changes its pages' state,
|
||||
and after it with what the first returned: the lazy write-back's pages there are paid and left alone meanwhile */
|
||||
int hos_guest_io_begin(const void *p, uint64_t n);
|
||||
void hos_guest_io_end(int held);
|
||||
#if defined(A64_WRITE_TRACK) && !defined(__EMSCRIPTEN__)
|
||||
void hos_mark_written(const void *p, size_t n); /* (hos_mem.c: guest memory written by host code, native soft watch) */
|
||||
#define HOS_WRITTEN(p, n) hos_mark_written((p), (n))
|
||||
@@ -150,5 +165,7 @@ uint32_t movie_reader_codec(MovieReader *r); /* the stream's FourCC: 'VP90', '
|
||||
/* ---- time */
|
||||
uint64_t hos_ticks(void);
|
||||
void hos_sleep_ns(int64_t ns);
|
||||
/* (Triforce) until hos_now_ns reads `until`: the timer to margin_ns before it, the rest yielded (pace_wait.h) */
|
||||
void hos_sleep_until_ns(int64_t until, int64_t margin_ns);
|
||||
int64_t hos_now_ns(void);
|
||||
#endif
|
||||
|
||||
118
host/hos_instance.h
Normal file
118
host/hos_instance.h
Normal file
@@ -0,0 +1,118 @@
|
||||
/* (Triforce) One game at a time for a game folder (handoff §5). Two copies of the game started on one folder (a second click on
|
||||
the icon while the first was still loading, the launcher and a shortcut) wrote the same saves, triforce_options.ini, logs and
|
||||
shader caches over each other, and the second's log rotation took the first's log away. The first holds a named mutex for as
|
||||
long as it runs (Windows lets go of it when the process ends, however it ends); a second one for the same folder says which
|
||||
process holds it, in a message box, and ends. The name comes from the folder's path, so the lab's copy and the owner's game,
|
||||
other folders, never meet, and a builder (Triforce Setup's own lock is named after Setup's folder) never meets the game.
|
||||
Its process id and executable are in a small named shared section beside the mutex, for the second one's message.
|
||||
HOS_SINGLE_INSTANCE=0: no lock (as before). Plain logic, with no part of the game: tests/test_game_instance.py runs it */
|
||||
#pragma once
|
||||
#ifdef _WIN32
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
|
||||
typedef struct { DWORD pid; wchar_t exe[1024]; } HosInstanceWho;
|
||||
|
||||
/* The lock's name for a folder: one for each spelling of it (case, '/' or '\', a separator at its end, "\\?\" in front), a
|
||||
64-bit FNV-1a of that spelling. Local\: this user's session (a game folder is one user's). (Triforce: "\\?\UNC\server\share"
|
||||
is \\server\share, and a drive alone, "C:", is its root "C:\". With only "\\?\" taken off the first was "UNC\server\share"
|
||||
and the second the working directory on that drive: GetFullPathNameW put both under the working directory, and the name
|
||||
changed with the directory a copy was started from) */
|
||||
static inline void hos_instance_name(const wchar_t *folder, wchar_t *out, size_t cap) {
|
||||
wchar_t full[1100], spelled[1100];
|
||||
const wchar_t *p = folder;
|
||||
if (p[0] == L'\\' && p[1] == L'\\' && p[2] == L'?' && p[3] == L'\\') {
|
||||
p += 4;
|
||||
if (_wcsnicmp(p, L"UNC\\", 4) == 0) {
|
||||
_snwprintf(spelled, 1100, L"\\\\%ls", p + 4);
|
||||
spelled[1099] = 0;
|
||||
p = spelled;
|
||||
}
|
||||
}
|
||||
if ((p[0] | 32) >= L'a' && (p[0] | 32) <= L'z' && p[1] == L':' && !p[2]) {
|
||||
_snwprintf(spelled, 1100, L"%lc:\\", p[0]);
|
||||
spelled[1099] = 0;
|
||||
p = spelled;
|
||||
}
|
||||
DWORD n = GetFullPathNameW(p, 1100, full, NULL);
|
||||
if (!n || n >= 1100) {
|
||||
wcsncpy(full, p, 1099);
|
||||
full[1099] = 0;
|
||||
n = (DWORD)wcslen(full);
|
||||
}
|
||||
for (DWORD i = 0; i < n; i++)
|
||||
if (full[i] == L'/') full[i] = L'\\';
|
||||
while (n > 3 && full[n - 1] == L'\\') full[--n] = 0; /* (C:\ keeps its own) */
|
||||
CharUpperBuffW(full, n);
|
||||
uint64_t h = 14695981039346656037ull;
|
||||
for (DWORD i = 0; i < n; i++) h = (h ^ full[i]) * 1099511628211ull;
|
||||
_snwprintf(out, cap, L"Local\\Triforce-Game-%016llx", (unsigned long long)h);
|
||||
out[cap - 1] = 0;
|
||||
}
|
||||
|
||||
/* The game folder of the game's own path (hos_game_path_w's), in place: the path up to its last separator. (Triforce: a folder
|
||||
at the root of a drive keeps its separator, "C:\" and not "C:", which is the working directory on drive C) */
|
||||
static inline void hos_instance_folder_of(wchar_t *path) {
|
||||
size_t n = wcslen(path);
|
||||
while (n && path[n - 1] != L'\\' && path[n - 1] != L'/') n--;
|
||||
if (n >= 2 && path[n - 2] == L':') path[n] = 0;
|
||||
else path[n ? n - 1 : 0] = 0;
|
||||
}
|
||||
|
||||
/* 1: this process holds the folder now (it waited up to wait_ms for one that was ending: a restart, a close just before);
|
||||
0: another process does, *who what it said of itself (pid 0: it said nothing yet). Where no lock can be made at all nobody
|
||||
is refused. The handles are kept until the process ends */
|
||||
static inline int hos_instance_take(const wchar_t *folder, DWORD wait_ms, HosInstanceWho *who) {
|
||||
wchar_t name[96], shared[112];
|
||||
hos_instance_name(folder, name, 96);
|
||||
_snwprintf(shared, 112, L"%ls-who", name);
|
||||
shared[111] = 0;
|
||||
memset(who, 0, sizeof *who);
|
||||
HANDLE m = CreateMutexW(NULL, FALSE, name);
|
||||
if (!m) return GetLastError() != ERROR_ACCESS_DENIED; /* (denied: it is there, held by a copy run as administrator) */
|
||||
const DWORD got = WaitForSingleObject(m, wait_ms);
|
||||
if (got != WAIT_OBJECT_0 && got != WAIT_ABANDONED) { /* (abandoned: its holder ended without letting go: ours now) */
|
||||
if (got == WAIT_TIMEOUT) {
|
||||
const HANDLE s = OpenFileMappingW(FILE_MAP_READ, FALSE, shared);
|
||||
const HosInstanceWho *v = s ? (const HosInstanceWho *)MapViewOfFile(s, FILE_MAP_READ, 0, 0, sizeof *v) : NULL;
|
||||
if (v) {
|
||||
who->pid = v->pid;
|
||||
wcsncpy(who->exe, v->exe, 1023);
|
||||
UnmapViewOfFile(v);
|
||||
}
|
||||
if (s) CloseHandle(s);
|
||||
}
|
||||
CloseHandle(m);
|
||||
return got != WAIT_TIMEOUT; /* (WAIT_FAILED: no lock to be had, nobody refused) */
|
||||
}
|
||||
const HANDLE s = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(HosInstanceWho), shared);
|
||||
HosInstanceWho *v = s ? (HosInstanceWho *)MapViewOfFile(s, FILE_MAP_WRITE, 0, 0, sizeof *v) : NULL;
|
||||
if (v) {
|
||||
GetModuleFileNameW(NULL, v->exe, 1023);
|
||||
v->exe[1023] = 0;
|
||||
v->pid = GetCurrentProcessId(); /* (last: a reader that finds a process id finds its exe too) */
|
||||
UnmapViewOfFile(v); /* (the section stays while s is open) */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The second copy's message: what holds the folder, and what to do */
|
||||
static inline void hos_instance_message(const wchar_t *folder, const HosInstanceWho *who, wchar_t *out, size_t cap) {
|
||||
wchar_t holder[1200];
|
||||
if (who->pid) _snwprintf(holder, 1200, L"process %lu, %ls", (unsigned long)who->pid, who->exe);
|
||||
else _snwprintf(holder, 1200, L"another copy that is starting");
|
||||
holder[1199] = 0;
|
||||
_snwprintf(out, cap,
|
||||
L"Triforce is already running from this game folder:\n\n%ls\n\nIt is held by %ls.\n\nUse the window that is open, or "
|
||||
L"close it and start the game again. (Two copies of one game folder would write the same saves, settings and "
|
||||
L"shader caches over each other.)",
|
||||
folder, holder);
|
||||
out[cap - 1] = 0;
|
||||
}
|
||||
#endif
|
||||
233
host/hos_main.c
233
host/hos_main.c
@@ -10,8 +10,11 @@
|
||||
HOS_NVN_CONTINUE=1 (go on past nvnBootstrapLoader). */
|
||||
#include "hos.h"
|
||||
#include "hos_paths.h"
|
||||
#include "hos_instance.h" /* ((Triforce) one game at a time for a game folder) */
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
#include "../gpu/vk/oot_cheat_items.h"
|
||||
#include "../gpu/vk/oot_debug_link.h" /* ((Triforce) the Game flags and Cutscenes pages' lines) */
|
||||
#include "../gpu/vk/ultrawide_rows.h" /* ((Triforce) the Ultrawide row's shapes: ultrawide_ratio) */
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -56,17 +59,24 @@ uint64_t hos_space_end;
|
||||
#define IMAGE_BASE hos_image_base /* (the lift's: Eden's layout) */
|
||||
|
||||
#ifdef _WIN32
|
||||
static void no_power_throttling(void) {
|
||||
/* Windows 11 runs a process whose window is not in front (another window focused, a second monitor's overlay) at
|
||||
efficiency speed (EcoQoS, the E-cores of a hybrid CPU) and may ignore its timer resolution request, which turns the
|
||||
1 ms waits the pacing and short sleeps rely on into 15.6 ms ones. Opting out keeps full speed and the 1 ms timer
|
||||
(ProcessPowerThrottling, 4: PROCESS_POWER_THROTTLING_EXECUTION_SPEED 0x1 and _IGNORE_TIMER_RESOLUTION 0x4 in the control
|
||||
mask, off in the state). The ignore-timer-resolution control needs Windows 11; on Windows 10 the call fails and execution
|
||||
speed alone is asked. HOS_POWER_THROTTLING=1 leaves Windows' throttling as it is (A/B runs).
|
||||
((Triforce) the comment and the outcome after NX2navi's plat_power_full, chrg6's.) Returns 2: full speed and the timer
|
||||
kept, 1: full speed only, 0: neither (no such call here, or it failed), -1: left as it is by the switch */
|
||||
static int no_power_throttling(void) {
|
||||
const char *e = getenv("HOS_POWER_THROTTLING");
|
||||
if (e && atoi(e)) return;
|
||||
if (e && atoi(e)) return -1;
|
||||
typedef BOOL(WINAPI * SetInfo)(HANDLE, int, LPVOID, DWORD);
|
||||
SetInfo set = (SetInfo)(void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "SetProcessInformation");
|
||||
if (!set) return;
|
||||
if (!set) return 0;
|
||||
struct { ULONG version, control, state; } s = {1, 0x1 | 0x4, 0};
|
||||
if (!set(GetCurrentProcess(), 4, &s, sizeof s)) {
|
||||
s.control = 0x1;
|
||||
set(GetCurrentProcess(), 4, &s, sizeof s);
|
||||
}
|
||||
if (set(GetCurrentProcess(), 4, &s, sizeof s)) return 2;
|
||||
s.control = 0x1;
|
||||
return set(GetCurrentProcess(), 4, &s, sizeof s) ? 1 : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1076,6 +1086,137 @@ static void oot_teleport_followup(Ctx *c) {
|
||||
events && recomp_load64(c, events + 104) ? 1 : 0);
|
||||
}
|
||||
|
||||
/* ---- (Triforce) Game flags by name and events by name: the Debug tab's two pages ----
|
||||
After NX2navi's F9 panel (its flags by name and its cutscene starter; that code is not the owner's and none of it
|
||||
is here: this is written on what the tools above already had of build e0f3ccfa). A flag is a Bool of GameData, which
|
||||
the game knows by the MurmurHash3 of its name alone; an event is started by the name of its file. The pages read both
|
||||
lists of names from the player's own game files (gpu/vk/debug_names.h) and send what they want as lines
|
||||
(gpu/vk/oot_debug_link.h), read here at Link's update, on the game's thread, as the other tools' actions are.
|
||||
|
||||
A flag is read where oot_gd_bool finds it and changed through the game's own SetBool (oot_gd_set), so that what follows
|
||||
a change of the game's own follows this one too; never while no save is loaded, and never one the save data does not
|
||||
have. An event is asked of the event manager as oot_teleport asks for its map change, with no arguments and Link as
|
||||
its starter; not while another plays, a map loads or no save is loaded. */
|
||||
static void oot_link_say(int kind, const char *text) {
|
||||
void rx_option_set(int id, int value);
|
||||
for (const char *t = text; *t; t++) rx_option_set(HOS_OPT_DEBUG_TEXT, (unsigned char)*t);
|
||||
rx_option_set(HOS_OPT_DEBUG_SAID, kind);
|
||||
}
|
||||
|
||||
/* a flag as the page shows it: '1' set, '0' not, '-' the save data has no such flag, '?' no save is loaded */
|
||||
static char oot_flag_state(Ctx *c, int loaded, uint32_t hash) {
|
||||
const uint64_t mgr = loaded ? oot_singleton(c, OOT_GAMEDATA_SLOT) : 0;
|
||||
if (!mgr) return '?';
|
||||
const uint64_t entry = oot_gd_bool(c, mgr, hash);
|
||||
return !entry ? '-' : recomp_load8(c, entry + 22) & 1 ? '1' : '0';
|
||||
}
|
||||
|
||||
/* A flag set or cleared through the game's own setter: 1 asked of the game, 0 not (no save loaded, or the save data
|
||||
has no such flag: nothing is called and nothing written then) */
|
||||
static int oot_flag_set(Ctx *c, int loaded, uint32_t hash, int value) {
|
||||
const uint64_t mgr = loaded ? oot_singleton(c, OOT_GAMEDATA_SLOT) : 0;
|
||||
if (!mgr || !hash || !oot_gd_bool(c, mgr, hash)) return 0;
|
||||
return oot_gd_set(c, mgr, hash, value ? 1u : 0u, 1);
|
||||
}
|
||||
|
||||
/* An event asked of the event manager by its name, Link its starter: what became of it in words (says), 1 when the
|
||||
request was made. The request as oot_teleport builds it, without arguments */
|
||||
static int oot_start_event(Ctx *c, uint64_t actor, int loaded, const char *name, char *says, size_t cap) {
|
||||
const uint64_t events = oot_singleton(c, OOT_EVENTMGR_SLOT);
|
||||
A64Fn make_arg = a64_lookup(0x80d99f10ULL), request = a64_lookup(0x80d9a820ULL);
|
||||
A64Fn set_link = a64_lookup(0x802dc440ULL), release_link = a64_lookup(0x8022b6f0ULL);
|
||||
int loading = 0;
|
||||
if (!loaded) return snprintf(says, cap, "Load a save first."), 0;
|
||||
if (!oot_name_ok(name)) return snprintf(says, cap, "That is not an event's name."), 0;
|
||||
if (!events || !make_arg || !request || !set_link || !release_link) return snprintf(says, cap, "The event manager is not there."), 0;
|
||||
if (recomp_load64(c, events + 104)) return snprintf(says, cap, "Another event is playing."), 0;
|
||||
if (oot_gamedata_bool(c, 0xef2b60c2u, &loading) && loading) return snprintf(says, cap, "A map is loading."), 0; /* Sequence_IsLoadingBanc */
|
||||
if (teleport.requested_at && hos_now_ns() - teleport.requested_at < 8000000000LL) return snprintf(says, cap, "A teleport is under way."), 0;
|
||||
const Ctx saved = *c;
|
||||
const uint64_t arg = (c->x[31] - OOT_EVENT_ARG_SIZE - 0x100) & ~0xfULL;
|
||||
c->x[31] = arg;
|
||||
c->x[0] = arg;
|
||||
make_arg(c);
|
||||
int ok = 1;
|
||||
{ /* the event's name: arg+6800 points at its buffer, arg+6808 the capacity */
|
||||
const uint64_t buf = recomp_load64(c, arg + 6800);
|
||||
const uint32_t room = recomp_load32(c, arg + 6808);
|
||||
if (!buf || strlen(name) + 1 > room) ok = 0;
|
||||
else memcpy(A64_PTR(buf), name, strlen(name) + 1);
|
||||
}
|
||||
uint64_t result = 0;
|
||||
if (ok) {
|
||||
c->x[31] = arg;
|
||||
c->x[0] = arg + 0x1950, c->x[1] = actor, c->x[2] = 0;
|
||||
set_link(c);
|
||||
c->x[31] = arg;
|
||||
c->x[0] = events, c->x[1] = arg;
|
||||
request(c);
|
||||
result = c->x[0];
|
||||
}
|
||||
for (unsigned i = 0; i < 2; i++) { /* (the arg's two actor links let go, as SeqOneShotRequestEvent does) */
|
||||
const uint64_t link = arg + (i ? 0x1950 : 0x1968);
|
||||
recomp_store64(c, link, 0x84994b20ULL);
|
||||
c->x[31] = arg;
|
||||
c->x[0] = link;
|
||||
release_link(c);
|
||||
}
|
||||
*c = saved;
|
||||
hos_log("cheats: event %s: request %s, result %#llx", name, ok ? "sent" : "not built", (unsigned long long)result);
|
||||
if (!ok) return snprintf(says, cap, "The request could not be built."), 0;
|
||||
snprintf(says, cap, result ? "%s asked of the game." : "The game did not take %s.", name);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The pages' lines taken and answered, at Link's update (now: hos_now_ns): a line read whole before it is acted on; the
|
||||
watched flags' states twice a second, and at once after a line. Nothing is read of the game while no line came and no
|
||||
flag is watched */
|
||||
static void oot_link_tick(Ctx *c, uint64_t actor, int64_t now) {
|
||||
int rx_option(int id, int otherwise);
|
||||
static uint32_t watch[OOT_WATCH_MAX];
|
||||
static int watching;
|
||||
static int64_t next;
|
||||
static char line[OOT_LINE_MAX + 1];
|
||||
static size_t used;
|
||||
static int long_line;
|
||||
int loaded = -1, byte; /* (a save is loaded once the pouch answers, as the Inventory page has it: asked once a tick at most) */
|
||||
while ((byte = rx_option(HOS_OPT_DEBUG_ASK, 0)) > 0) {
|
||||
if (byte != '\n') {
|
||||
if (used < OOT_LINE_MAX) line[used++] = (char)byte;
|
||||
else long_line = 1;
|
||||
continue;
|
||||
}
|
||||
line[used] = 0;
|
||||
const int whole = !long_line;
|
||||
used = 0, long_line = 0;
|
||||
OotAsk ask;
|
||||
if (!whole || !oot_ask_parse(line, &ask)) continue; /* (not a line of the pages': dropped) */
|
||||
if (loaded < 0) loaded = oot_pouch_count(c, oot_item_names[0]) >= 0;
|
||||
if (ask.kind == 'W') {
|
||||
memcpy(watch, ask.watch, sizeof watch);
|
||||
watching = ask.count;
|
||||
} else if (ask.kind == 'F') {
|
||||
const int done = oot_flag_set(c, loaded, ask.hash, ask.value);
|
||||
hos_log("cheats: flag %08x %s: %s", (unsigned)ask.hash, ask.value ? "set" : "cleared",
|
||||
done ? "asked of the game" : loaded ? "the save data has no such flag" : "no save is loaded");
|
||||
oot_link_say(OOT_SAID_STATUS, done ? (ask.value ? "Flag set." : "Flag cleared.") : loaded ? "The save data has no such flag." : "Load a save first.");
|
||||
} else {
|
||||
char says[160];
|
||||
oot_start_event(c, actor, loaded, ask.name, says, sizeof says);
|
||||
oot_link_say(OOT_SAID_STATUS, says);
|
||||
}
|
||||
next = now + 100000000LL; /* (the states soon: a change is made at the game's next frame) */
|
||||
}
|
||||
if (!watching || now < next) return;
|
||||
next = now + 500000000LL;
|
||||
if (loaded < 0) loaded = oot_pouch_count(c, oot_item_names[0]) >= 0;
|
||||
char states[16 + OOT_WATCH_MAX];
|
||||
int at = snprintf(states, sizeof states, "%08x ", (unsigned)oot_watch_sum(watch, watching));
|
||||
for (int i = 0; i < watching; i++) states[at++] = oot_flag_state(c, loaded, watch[i]);
|
||||
states[at] = 0;
|
||||
oot_link_say(OOT_SAID_FLAGS, states);
|
||||
}
|
||||
|
||||
static uint64_t oot_gameplay_view(Ctx *c) {
|
||||
const uint64_t singleton = recomp_load64(c, 0x84ca6bc0ULL);
|
||||
const uint64_t manager = singleton ? recomp_load64(c, singleton) : 0;
|
||||
@@ -1111,6 +1252,7 @@ static void oot_update_player_cheats(Ctx *c, uint64_t actor, uint64_t components
|
||||
if (!action) action = __atomic_exchange_n(&hos_oot_live_action, 0, __ATOMIC_RELAXED);
|
||||
oot_teleport_followup(c);
|
||||
oot_inventory_refresh(c, now);
|
||||
oot_link_tick(c, actor, now); /* ((Triforce) the Game flags and Cutscenes pages) */
|
||||
if (action >= OOT_CHEAT_ITEM_ACTION && action < OOT_CHEAT_ITEM_ACTION + 256 * (int)OOT_ITEM_COUNT) {
|
||||
oot_inventory_edit(c, action);
|
||||
} else if (action == OOT_AGE_ACTION) {
|
||||
@@ -1473,6 +1615,15 @@ void hos_oot_free_view(Ctx *c, uint64_t view) {
|
||||
}
|
||||
|
||||
float hos_aspect_ratio = 0; /* (the window's default shape: host/display.c) */
|
||||
/* (Triforce, after NX2navi's game_aspect) The Insert menu's Ultrawide row (ultrawide= in the options file, of options_version
|
||||
version) as the picture's shape, width over height, 0 for the game's own 16:9: 0 the game's own, 1 a fixed 16:10, 2 a fixed
|
||||
21:9 (64:27), 3 a fixed 32:9 (gpu/vk/ultrawide_rows.h). The fixed ones are for a window, or for a screen of that shape; a
|
||||
screen of another shape shows the picture between black bars. A file from before options_version 6 whose row is 1 had the
|
||||
main monitor's shape (screen_w x screen_h), no longer a choice: the shape nearest it. A value past the row is its nearest
|
||||
end, as the menu reads it (gpu/vk/vk_menu.cpp) */
|
||||
static float ultrawide_ratio(int row, int version, int screen_w, int screen_h) {
|
||||
return ultrawide_row_ratio(ultrawide_row_read(row, version, screen_w, screen_h));
|
||||
}
|
||||
/* (nx2/oot_wide.py) read by the widescreen patches in the game's code; 0: the game's own 16:9 */
|
||||
volatile uint64_t hos_wide_out, hos_wide_scene;
|
||||
/* (nx2/oot_native_resolution.py) mode 1's scene base scale 1.0 instead of 0.8: Resolution 100% (host/nn_vi.c keeps it current),
|
||||
@@ -1565,6 +1716,35 @@ static int narrow_paths_ok(int argc, char **argv) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ((Triforce) handoff §5) One game at a time for a game folder: host/hos_instance.h. Taken before the log is opened (a second
|
||||
copy's rotation moved the first's triforce_log.txt away and wrote over it) and after a restart's wait for the run before it.
|
||||
A copy that finds the folder held waits up to 5 s for one that is ending (a close and a new start at once; 20 s after the
|
||||
menu's Restart), then says which process holds it and ends: a message box, or for the builder's --prepare-shaders run and
|
||||
HOS_HEADLESS a line on stdout and stderr. 0: refused. HOS_SINGLE_INSTANCE=0: no lock */
|
||||
static int instance_held = -1; /* (for the log's line: 1 held, 0 not asked) */
|
||||
static int one_game_per_folder(int argc, char **argv, int restart) {
|
||||
const char *e = getenv("HOS_SINGLE_INSTANCE");
|
||||
if (e && !strcmp(e, "0")) return instance_held = 0, 1;
|
||||
wchar_t folder[1100];
|
||||
DWORD n = hos_game_path_w(folder, 1100);
|
||||
if (!n || n >= 1100) return instance_held = 0, 1;
|
||||
hos_instance_folder_of(folder); /* ((Triforce) "C:\" for a game at a drive's root, not drive C's working directory) */
|
||||
HosInstanceWho who;
|
||||
if (hos_instance_take(folder, restart ? 20000 : 5000, &who)) return instance_held = 1, 1;
|
||||
wchar_t text[2600];
|
||||
hos_instance_message(folder, &who, text, sizeof text / sizeof text[0]);
|
||||
int quiet = getenv("HOS_HEADLESS") != NULL;
|
||||
for (int i = 1; i < argc; i++) quiet |= !strcmp(argv[i], "--prepare-shaders");
|
||||
if (quiet) {
|
||||
printf("@@error Triforce is running from this game folder (process %lu): it prepares its shaders itself\n", (unsigned long)who.pid);
|
||||
fprintf(stderr, "triforce: this game folder is held by process %lu: not started\n", (unsigned long)who.pid);
|
||||
fflush(stdout);
|
||||
} else {
|
||||
MessageBoxW(NULL, text, L"Triforce", MB_OK | MB_ICONINFORMATION | MB_SETFOREGROUND);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
#else
|
||||
static int game_main(int argc, char **argv) {
|
||||
@@ -1591,12 +1771,18 @@ static int game_main(int argc, char **argv) {
|
||||
{ /* (started again by the Insert menu's Restart now (host/hos_relaunch.c): the run before still has its log open until
|
||||
it exits, so wait for it (up to 10 s) before the log moves to .1, or the move fails and its log is overwritten) */
|
||||
const char *of = getenv("HOS_RESTART_OF");
|
||||
if (of && *of) {
|
||||
const int restart = of && *of;
|
||||
if (restart) {
|
||||
HANDLE before = OpenProcess(SYNCHRONIZE, FALSE, (DWORD)strtoul(of, NULL, 10));
|
||||
if (before) WaitForSingleObject(before, 10000), CloseHandle(before);
|
||||
SetEnvironmentVariableA("HOS_RESTART_OF", NULL); /* (not passed on: a later restart sets its own) */
|
||||
_putenv("HOS_RESTART_OF=");
|
||||
}
|
||||
if (!one_game_per_folder(argc, argv, restart)) { /* ((Triforce) before the log: host/hos_instance.h) */
|
||||
for (int i = 1; i < argc; i++)
|
||||
if (!strcmp(argv[i], "--prepare-shaders")) return 3; /* (as with no Vulkan: the game prepares at its start) */
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
HANDLE err = GetStdHandle(STD_ERROR_HANDLE);
|
||||
@@ -1618,6 +1804,21 @@ static int game_main(int argc, char **argv) {
|
||||
}
|
||||
}
|
||||
hos_log("triforce %s", HOOKSHOT_VERSION); /* (the log's first line: which build a report comes from) */
|
||||
#ifdef _WIN32
|
||||
if (instance_held >= 0)
|
||||
hos_log("instance: %s", instance_held ? "this game folder is this run's (a second copy started on it says so and ends)"
|
||||
: "no lock on this game folder (HOS_SINGLE_INSTANCE=0, or its path was not found)");
|
||||
#endif
|
||||
#ifdef _WIN32
|
||||
{ /* ((Triforce) every run, the shader preparation's too, as NX2navi's, and said in the log: it was the game's run alone,
|
||||
after its image was read, and the log never said whether Windows took it) */
|
||||
const int got = no_power_throttling();
|
||||
hos_log("power: %s", got == 2 ? "full speed and the 1 ms timer kept when the window is not in front"
|
||||
: got == 1 ? "full speed kept when the window is not in front (no timer control here: Windows 10)"
|
||||
: got == 0 ? "Windows' throttling of a window not in front stays as it is (refused, or no such call)"
|
||||
: "Windows' throttling of a window not in front left as it is (HOS_POWER_THROTTLING=1)");
|
||||
}
|
||||
#endif
|
||||
const char *image = NULL;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "--romfs") && i + 1 < argc) hos_romfs_dir = argv[++i];
|
||||
@@ -1702,8 +1903,11 @@ static int game_main(int argc, char **argv) {
|
||||
const long long file_size = _ftelli64(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (file_size > 0) hos_commit(IMAGE_BASE, (uint64_t)file_size);
|
||||
/* (Triforce: the kernel writes guest memory here, before any renderer runs: the lazy write-back's rule all the same) */
|
||||
const int io = file_size > 0 ? hos_guest_io_begin(A64_MEM + IMAGE_BASE, (uint64_t)file_size) : 0;
|
||||
size_t n = file_size > 0 && (uint64_t)file_size <= GUEST_SPACE - IMAGE_BASE
|
||||
? fread(A64_MEM + IMAGE_BASE, 1, (size_t)file_size, f) : 0;
|
||||
hos_guest_io_end(io);
|
||||
fclose(f);
|
||||
#endif
|
||||
if (IMAGE_BASE + n != hos_image_end) {
|
||||
@@ -1713,14 +1917,14 @@ static int game_main(int argc, char **argv) {
|
||||
}
|
||||
hos_mem_init(hos_image_end);
|
||||
{ /* (hookshot) Widescreen and ultrawide (nx2/oot_wide.py): HOS_ASPECT=<ratio> (2.3889 for 3440x1440), else the Insert menu's
|
||||
Ultrawide (off unless it says 1) takes the main monitor's shape. The game's picture takes that shape at its own height (the
|
||||
Ultrawide (off unless set: a fixed 16:10, 21:9 or 32:9, ultrawide_ratio). The game's picture takes that shape at its own height (the
|
||||
world wider at the sides on an ultrawide screen, narrower on a 16:10 or 4:3 one), its 16:9 cameras the picture's ratio. Wider
|
||||
than 16:9 the HUD, menus and movies keep the middle 16:9; narrower they fill the picture's width. Until now the one 16:9
|
||||
constant in the game's data was changed and the 16:9 picture stretched to the window. */
|
||||
const char *e = getenv("HOS_ASPECT");
|
||||
float want = e ? (float)atof(e) : 0.0f;
|
||||
if (!e) {
|
||||
int on = 0;
|
||||
int row = 0, version = 0;
|
||||
char path[MAX_PATH];
|
||||
const DWORD len = hos_game_path_a(path, MAX_PATH);
|
||||
char *slash = len ? strrchr(path, SEP) : NULL;
|
||||
@@ -1730,15 +1934,15 @@ static int game_main(int argc, char **argv) {
|
||||
if (o) {
|
||||
char line[80];
|
||||
while (fgets(line, sizeof line, o)) {
|
||||
if (!strncmp(line, "ultrawide=", 10)) on = atoi(line + 10) != 0;
|
||||
if (!strncmp(line, "ultrawide=", 10)) row = atoi(line + 10);
|
||||
if (!strncmp(line, "options_version=", 16)) version = atoi(line + 16); /* ((Triforce) 1's old meaning before 6) */
|
||||
/* (the game sizes its scene before the menu is up: Resolution 100% read here too) */
|
||||
/* (resolution=1 no longer raises the scene: #849 makes a fixed Resolution reach the 3D; HOS_SCENE_NATIVE=1 below) */
|
||||
}
|
||||
fclose(o);
|
||||
}
|
||||
}
|
||||
const int sw = GetSystemMetrics(SM_CXSCREEN), sh = GetSystemMetrics(SM_CYSCREEN);
|
||||
if (on && sh > 0) want = (float)sw / (float)sh;
|
||||
want = ultrawide_ratio(row, version, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
|
||||
}
|
||||
if (getenv("HOS_SCENE_NATIVE")) hos_native_scene = atoi(getenv("HOS_SCENE_NATIVE")) != 0;
|
||||
if (getenv("HOS_FIX_RES")) hos_fix_res = atoi(getenv("HOS_FIX_RES")) != 0;
|
||||
@@ -1788,7 +1992,6 @@ static int game_main(int argc, char **argv) {
|
||||
hos_prof_start(); /* HOS_PROFILE=<seconds> */
|
||||
#ifdef _WIN32
|
||||
timeBeginPeriod(1); /* 1 ms timer resolution: the 60 Hz vsync and the game's frame pacing sleep in short steps */
|
||||
no_power_throttling();
|
||||
#endif
|
||||
void hos_display_start(void);
|
||||
hos_display_start(); /* the game's window; HOS_HEADLESS=1 for none */
|
||||
@@ -1822,6 +2025,8 @@ static int game_argc;
|
||||
static char **game_argv;
|
||||
static DWORD WINAPI game_thread(void *p) {
|
||||
(void)p;
|
||||
void hos_stack_guarantee(void); /* (hos_thread.c: room for a stack overflow's crash report) */
|
||||
hos_stack_guarantee();
|
||||
ExitProcess((UINT)game_main(game_argc, game_argv));
|
||||
}
|
||||
int main(int argc, char **argv) {
|
||||
|
||||
223
host/hos_mem.c
223
host/hos_mem.c
@@ -21,8 +21,32 @@ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
uint64_t hos_heap_size, hos_heap_used, hos_total_memory, hos_program_size;
|
||||
static uint64_t sys_next = HOS_SYSTEM_BASE;
|
||||
|
||||
/* Address regions are handed out top-down from HOS_SPACE_END; the heap grows up from HOS_HEAP_BASE. */
|
||||
typedef struct { uint64_t addr, size; } Range;
|
||||
/* (Triforce) the guest memory census's map (guest_census.h; the notes, the counts and the give-back's decommit are at this
|
||||
file's end). Its chunks come from the system, not the heap: the first touch's exception handler may make one */
|
||||
static void *census_alloc(size_t n) {
|
||||
#if defined(_WIN32)
|
||||
return VirtualAlloc(NULL, n, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
#else
|
||||
void *p = mmap(NULL, n, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
||||
return p == MAP_FAILED ? NULL : p;
|
||||
#endif
|
||||
}
|
||||
static void census_free(void *p, size_t n) {
|
||||
#if defined(_WIN32)
|
||||
(void)n;
|
||||
VirtualFree(p, 0, MEM_RELEASE);
|
||||
#else
|
||||
munmap(p, n);
|
||||
#endif
|
||||
}
|
||||
#define GC_ALLOC(n) census_alloc(n)
|
||||
#define GC_FREE(p) census_free((p), sizeof(GcChunk))
|
||||
#include "guest_census.h"
|
||||
static GcMap census;
|
||||
|
||||
/* Address regions are handed out top-down from HOS_SPACE_END; the heap grows up from HOS_HEAP_BASE. (Triforce) freeing: a
|
||||
region being freed, listed until its pages are discarded (hos_region_free) */
|
||||
typedef struct { uint64_t addr, size; int freeing; } Range;
|
||||
static Range *regions;
|
||||
static int nregions, capregions;
|
||||
static uint64_t region_floor = ~0ULL; /* the lowest region (none yet: the window's end) */
|
||||
@@ -34,6 +58,7 @@ static uint64_t low_hole[2][2];
|
||||
void hos_mem_init(uint64_t image_end) {
|
||||
hos_program_size = image_end - hos_image_base;
|
||||
hos_commit(HOS_SYSTEM_BASE, HOS_SYSTEM_END - HOS_SYSTEM_BASE);
|
||||
hos_mem_census_start(); /* (Triforce: the census line and the give-back, mkw_glue.c) */
|
||||
low_hole[0][0] = 0x00100000ULL, low_hole[0][1] = HOS_SYSTEM_BASE;
|
||||
low_hole[1][0] = (image_end + 0xFFFFFULL) & ~0xFFFFFULL, low_hole[1][1] = HOS_HEAP_BASE;
|
||||
/* Retail Switch gives an application 3285 MB. HOS_MEMORY_MB overrides it: TOTK sizes its heap from this
|
||||
@@ -107,6 +132,7 @@ uint64_t hos_region_alloc(uint64_t size, uint64_t align) {
|
||||
memmove(®ions[at + 1], ®ions[at], (nregions - at) * sizeof(Range));
|
||||
regions[at].addr = found;
|
||||
regions[at].size = size;
|
||||
regions[at].freeing = 0;
|
||||
nregions++;
|
||||
if (found < region_floor && found >= HOS_HEAP_BASE) region_floor = found; /* (not the holes below) */
|
||||
}
|
||||
@@ -132,6 +158,9 @@ static void discard_pages(uint64_t addr, uint64_t size) {
|
||||
#if defined(_WIN32) && !defined(__EMSCRIPTEN__)
|
||||
extern uint64_t hos_large_lo, hos_large_hi; /* (hos_main.c: large pages cannot be discarded) */
|
||||
if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) return;
|
||||
/* (Triforce, the census: let go and still committed. Not the large pages above, left as they were, nor Linux's below,
|
||||
noted zeroed or decommitted there) */
|
||||
hos_mem_note(HOS_MEM_DISCARDED, lo, hi - lo);
|
||||
typedef DWORD(WINAPI * DiscardFn)(PVOID, SIZE_T);
|
||||
static DiscardFn discard;
|
||||
static int looked;
|
||||
@@ -139,33 +168,47 @@ static void discard_pages(uint64_t addr, uint64_t size) {
|
||||
looked = 1;
|
||||
discard = (DiscardFn)(void *)GetProcAddress(GetModuleHandleA("kernel32.dll"), "DiscardVirtualMemory");
|
||||
}
|
||||
const int io = hos_guest_io_begin(GP(lo), hi - lo); /* (Triforce: the pages' state changes under the lazy write-back's: rx_backend.c) */
|
||||
if (!discard || discard(GP(lo), (SIZE_T)(hi - lo)) != ERROR_SUCCESS)
|
||||
VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_RESET, PAGE_READWRITE);
|
||||
hos_guest_io_end(io);
|
||||
#elif !defined(__EMSCRIPTEN__)
|
||||
extern int hos_posix_guest_discard(void *p, size_t n); /* (hos_main.c: MADV_REMOVE on the memfd window) */
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
/* (pages a dma-buf import holds stay where they are, zeroed: a hole punched under one would leave the GPU on
|
||||
the old pages and the game on new ones) */
|
||||
if (rx_guest_imported(lo, hi)) memset(GP(lo), 0, (size_t)(hi - lo));
|
||||
else hos_posix_guest_discard(GP(lo), (size_t)(hi - lo));
|
||||
if (rx_guest_imported(lo, hi)) memset(GP(lo), 0, (size_t)(hi - lo)), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo);
|
||||
else hos_posix_guest_discard(GP(lo), (size_t)(hi - lo)), hos_mem_note(HOS_MEM_DECOMMITTED, lo, hi - lo);
|
||||
#endif
|
||||
}
|
||||
|
||||
void hos_region_free(uint64_t addr) {
|
||||
uint64_t freed = 0, freed_size = 0;
|
||||
uint64_t freed_size = 0;
|
||||
LOCK();
|
||||
for (int i = 0; i < nregions; i++) {
|
||||
if (regions[i].addr == addr) {
|
||||
if (regions[i].addr == addr && !regions[i].freeing) {
|
||||
pages_mark_used_locked(regions[i].addr, regions[i].size);
|
||||
freed = regions[i].addr;
|
||||
freed_size = regions[i].size;
|
||||
regions[i].freeing = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
if (!freed_size) return;
|
||||
/* (Triforce) discarded, and noted so in the census, while the region is still listed: a region asked for meanwhile is not
|
||||
given these pages, which a discard after it would take from it, and a commit of them marked kept by the late note. Not
|
||||
under the lock: the discard may wait for the GPU (the lazy write-back) with the thread's virtual core given up, and a
|
||||
thread taking its core back with the lock held could wait on one that holds a core and waits for the lock */
|
||||
discard_pages(addr, freed_size);
|
||||
LOCK();
|
||||
for (int i = 0; i < nregions; i++) {
|
||||
if (regions[i].addr == addr && regions[i].freeing) {
|
||||
memmove(®ions[i], ®ions[i + 1], (nregions - i - 1) * sizeof(Range));
|
||||
nregions--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
UNLOCK();
|
||||
if (freed_size) discard_pages(freed, freed_size);
|
||||
}
|
||||
|
||||
/* ---- nn::os memory -------------------------------------------------------------------------------------- */
|
||||
@@ -262,6 +305,7 @@ HLE(_ZN2nn2os19AllocateMemoryPagesEmm) {
|
||||
instead: only newly mapped pages read as zero. A commit leaves committed pages as they are and gives fresh
|
||||
ones zeroed; FreeMemoryPages decommits. */
|
||||
hos_commit(a, n);
|
||||
hos_mem_note(HOS_MEM_PAGES, a, n); /* (Triforce: the census, the game's own pages) */
|
||||
} else if (zero_all || end > (1ULL << 32) || end < a) {
|
||||
memset(GP(a), 0, n);
|
||||
} else {
|
||||
@@ -304,15 +348,20 @@ HLE(_ZN2nn2os15FreeMemoryPagesEmm) {
|
||||
/* (pages the Vulkan back end imported, as part of a pool's 16 MB chunk, stay committed and are zeroed: the GPU
|
||||
still maps them, and decommitted pages under an import faulted the device. Read as zero, as freed pages) */
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
if (hi > lo && rx_guest_imported(lo, hi)) memset(GP(lo), 0, (size_t)(hi - lo));
|
||||
/* (Triforce: zeroed or decommitted: the lazy write-back's pages there first, rx_backend.c) */
|
||||
const int io = hi > lo ? hos_guest_io_begin(GP(lo), hi - lo) : 0;
|
||||
/* ((Triforce) the census: zeroed pages stay committed, kept for the give-back once no import holds them) */
|
||||
if (hi > lo && rx_guest_imported(lo, hi)) memset(GP(lo), 0, (size_t)(hi - lo)), hos_mem_note(HOS_MEM_ZEROED, lo, hi - lo);
|
||||
#if defined(_WIN32)
|
||||
else if (hi > lo) VirtualFree(GP(lo), (SIZE_T)(hi - lo), MEM_DECOMMIT);
|
||||
else if (hi > lo) VirtualFree(GP(lo), (SIZE_T)(hi - lo), MEM_DECOMMIT), hos_mem_note(HOS_MEM_DECOMMITTED, lo, hi - lo);
|
||||
#else
|
||||
else if (hi > lo) { /* (linux: kept mapped, the pages read as zero; MADV_REMOVE on the memfd window) */
|
||||
extern int hos_posix_guest_discard(void *p, size_t n);
|
||||
hos_posix_guest_discard(GP(lo), (size_t)(hi - lo));
|
||||
hos_mem_note(HOS_MEM_DECOMMITTED, lo, hi - lo);
|
||||
}
|
||||
#endif
|
||||
hos_guest_io_end(io);
|
||||
RET(0);
|
||||
return;
|
||||
}
|
||||
@@ -339,3 +388,157 @@ HLE(_ZN2nn2os15QueryMemoryInfoEPNS0_10MemoryInfoE) {
|
||||
|
||||
/* nn::os::SetDyingMessageRegion(unsigned long, unsigned long) -> Result */
|
||||
HLE(_ZN2nn2os21SetDyingMessageRegionEmm) { RET(0); }
|
||||
|
||||
/* ---- (Triforce) the guest memory census (guest_census.h has why) ----------------------------------------------------------
|
||||
The notes: what each place that commits, maps or lets go of guest pages did, into the census's map. Kept while the census
|
||||
line or the give-back is on (HOS_MEM_CENSUS, on by default; HOS_MEM_GIVE_BACK: mkw_glue.c hos_mem_census_start); with both
|
||||
off nothing is noted. */
|
||||
#if defined(_WIN32)
|
||||
static SRWLOCK census_lock = SRWLOCK_INIT;
|
||||
#define CENSUS_LOCK() AcquireSRWLockExclusive(&census_lock)
|
||||
#define CENSUS_UNLOCK() ReleaseSRWLockExclusive(&census_lock)
|
||||
#else
|
||||
static pthread_mutex_t census_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
#define CENSUS_LOCK() pthread_mutex_lock(&census_lock)
|
||||
#define CENSUS_UNLOCK() pthread_mutex_unlock(&census_lock)
|
||||
#endif
|
||||
static uint64_t census_touched, census_zeroed, census_discarded, census_given, census_given_runs, census_refused;
|
||||
static int census_unsupported; /* (the renderer has no mkwvk_guest_release: no give-back) */
|
||||
|
||||
int hos_mem_census_on(void) {
|
||||
static volatile int on = -1;
|
||||
int v = on;
|
||||
if (v < 0) { /* (asked first by the program's commit, before any thread runs) */
|
||||
const char *c = getenv("HOS_MEM_CENSUS"), *g = getenv("HOS_MEM_GIVE_BACK");
|
||||
v = !(c && atoi(c) <= 0) || (g && atoi(g) > 0);
|
||||
on = v;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
void hos_mem_note(int what, uint64_t addr, uint64_t size) {
|
||||
const uint64_t lo = addr, hi = addr + size;
|
||||
if (!size || hi < lo || hi > GC_SPACE || !hos_mem_census_on()) return;
|
||||
switch (what) {
|
||||
case HOS_MEM_COMMITTED: /* (before the commit, under the lock the give-back checks and decommits under) */
|
||||
CENSUS_LOCK();
|
||||
gc_change(&census, GC_HELD, lo, hi, 1);
|
||||
gc_change(&census, GC_KEPT, lo, hi, 0);
|
||||
CENSUS_UNLOCK();
|
||||
break;
|
||||
case HOS_MEM_TOUCHED: /* (the first touch's exception handler: no lock. A page it commits was reserved, never kept) */
|
||||
gc_change(&census, GC_HELD, lo, hi, 1);
|
||||
gc_change(&census, GC_KEPT, lo, hi, 0);
|
||||
__atomic_add_fetch(&census_touched, 1, __ATOMIC_RELAXED);
|
||||
break;
|
||||
case HOS_MEM_PAGES:
|
||||
case HOS_MEM_MAPPED:
|
||||
gc_change(&census, GC_GAME, lo, hi, 1);
|
||||
break;
|
||||
case HOS_MEM_DECOMMITTED:
|
||||
gc_change(&census, GC_HELD, lo, hi, 0);
|
||||
gc_change(&census, GC_GAME, lo, hi, 0);
|
||||
gc_change(&census, GC_KEPT, lo, hi, 0);
|
||||
break;
|
||||
case HOS_MEM_ZEROED: /* (every page of it committed: the zeroing touched it) */
|
||||
gc_change(&census, GC_HELD, lo, hi, 0);
|
||||
gc_change(&census, GC_GAME, lo, hi, 0);
|
||||
__atomic_add_fetch(&census_zeroed, gc_change(&census, GC_KEPT, lo, hi, 1), __ATOMIC_RELAXED);
|
||||
break;
|
||||
case HOS_MEM_DISCARDED: /* (only what was committed stays so) */
|
||||
gc_change(&census, GC_GAME, lo, hi, 0);
|
||||
__atomic_add_fetch(&census_discarded, gc_move(&census, GC_HELD, GC_KEPT, lo, hi), __ATOMIC_RELAXED);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* hos_commit's notes (mkw_glue.c), around its commit: held before it, under the lock the give-back checks and decommits under
|
||||
(so no give-back takes a page it is committing), and no longer kept only once the commit is made. A commit that failed
|
||||
puts back the pages it set held (kept ones kept, never committed ones neither); begin's answer goes to end */
|
||||
void *hos_mem_commit_begin(uint64_t addr, uint64_t size) {
|
||||
const uint64_t lo = addr, hi = addr + size;
|
||||
if (!size || hi < lo || hi > GC_SPACE || !hos_mem_census_on()) return NULL;
|
||||
const uint64_t n = gc_words(lo, hi);
|
||||
uint64_t *had = n ? (uint64_t *)malloc((size_t)n * sizeof *had) : NULL;
|
||||
if (had) memset(had, 0xFF, (size_t)n * sizeof *had);
|
||||
CENSUS_LOCK();
|
||||
if (had) gc_set_keeping(&census, GC_HELD, lo, hi, had);
|
||||
else gc_change(&census, GC_HELD, lo, hi, 1); /* (no room to say which were held: a failed commit leaves them held) */
|
||||
CENSUS_UNLOCK();
|
||||
return had;
|
||||
}
|
||||
void hos_mem_commit_end(void *had, uint64_t addr, uint64_t size, int committed) {
|
||||
const uint64_t lo = addr, hi = addr + size;
|
||||
if (size && hi >= lo && hi <= GC_SPACE && hos_mem_census_on()) {
|
||||
CENSUS_LOCK();
|
||||
if (committed) gc_change(&census, GC_KEPT, lo, hi, 0);
|
||||
else if (had) gc_unset_had(&census, GC_HELD, lo, hi, (const uint64_t *)had);
|
||||
CENSUS_UNLOCK();
|
||||
}
|
||||
free(had);
|
||||
}
|
||||
|
||||
/* The counts (mkw_glue.c's line): the live address regions copied under the regions' lock, then counted without it */
|
||||
void hos_mem_census(GcCensus *c) {
|
||||
static uint64_t (*list)[2];
|
||||
static int cap;
|
||||
int n = 0;
|
||||
LOCK();
|
||||
if (nregions > cap) {
|
||||
uint64_t(*grown)[2] = (uint64_t(*)[2])realloc(list, (size_t)nregions * sizeof *list);
|
||||
if (grown) list = grown, cap = nregions;
|
||||
}
|
||||
for (; n < nregions && n < cap; n++) list[n][0] = regions[n].addr, list[n][1] = regions[n].size;
|
||||
UNLOCK();
|
||||
gc_census_count(&census, (const uint64_t(*)[2])list, n, HOS_HEAP_BASE, hos_heap_size, c);
|
||||
c->touched = __atomic_load_n(&census_touched, __ATOMIC_RELAXED);
|
||||
c->kept_import = __atomic_load_n(&census_zeroed, __ATOMIC_RELAXED);
|
||||
c->kept_discard = __atomic_load_n(&census_discarded, __ATOMIC_RELAXED);
|
||||
c->given_back = census_given, c->given_runs = census_given_runs, c->refused = census_refused;
|
||||
c->budget = hos_total_memory, c->program = hos_program_size;
|
||||
}
|
||||
|
||||
/* The give-back's decommit of [lo, hi), run by the renderer with its import locks held (rx_backend.c rx_guest_release): no
|
||||
import holds the pages, and none can start to meanwhile. Only when every page is still kept and none was handed out again
|
||||
since the pass found them (hos_commit notes before it commits, under this lock). 1: given back */
|
||||
static int census_release(uint64_t lo, uint64_t hi) {
|
||||
extern uint64_t hos_large_lo, hos_large_hi;
|
||||
if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) return 0; /* (large pages are not decommitted) */
|
||||
if (lo < (1ULL << 32)) return 0; /* (below 4 GB pages are handed out without a commit: hos_mem_give_back) */
|
||||
CENSUS_LOCK();
|
||||
int ok = gc_count(&census, GC_KEPT, lo, hi) == hi - lo && !gc_count(&census, GC_HELD, lo, hi);
|
||||
if (ok) {
|
||||
#if defined(_WIN32)
|
||||
ok = VirtualFree(GP(lo), (SIZE_T)(hi - lo), MEM_DECOMMIT) != 0;
|
||||
#else
|
||||
extern int hos_posix_guest_discard(void *p, size_t n);
|
||||
ok = hos_posix_guest_discard(GP(lo), (size_t)(hi - lo)) == 0;
|
||||
#endif
|
||||
if (ok) gc_change(&census, GC_KEPT, lo, hi, 0);
|
||||
}
|
||||
CENSUS_UNLOCK();
|
||||
return ok;
|
||||
}
|
||||
/* One kept run offered: the lazy write-back's pages there paid first (as FreeMemoryPages does), then the renderer asked */
|
||||
static int census_offer(void *ctx, uint64_t lo, uint64_t hi) {
|
||||
(void)ctx;
|
||||
int rx_guest_release(uint64_t lo, uint64_t hi, int (*release)(uint64_t lo, uint64_t hi)); /* (rx_backend.c) */
|
||||
const int io = hos_guest_io_begin(GP(lo), hi - lo);
|
||||
const int r = rx_guest_release(lo, hi, census_release);
|
||||
hos_guest_io_end(io);
|
||||
if (r == -2) census_unsupported = 1;
|
||||
return r == 1;
|
||||
}
|
||||
/* A pass of the give-back (the census's thread, mkw_glue.c): kept runs of 16 MB at most, 64 of them at most, until max_bytes
|
||||
are given back. The bytes given back; -1 when the renderer cannot (an older one, without mkwvk_guest_release) */
|
||||
int64_t hos_mem_give_back(uint64_t max_bytes) {
|
||||
static uint64_t cursor;
|
||||
if (census_unsupported) return -1;
|
||||
if (!hos_mem_census_on()) return 0;
|
||||
uint64_t runs = 0, refused = 0;
|
||||
/* (from 4 GB up: below it AllocateMemoryPages zeroes pages without committing them, and a host call handed one that was
|
||||
given back would fail on it without a fault) */
|
||||
const uint64_t given = gc_give_back(&census, 1ULL << 32, &cursor, 16ULL << 20, max_bytes, 64, census_offer, NULL, &runs, &refused);
|
||||
census_given += given, census_given_runs += runs, census_refused += refused;
|
||||
return census_unsupported && !given ? -1 : (int64_t)given;
|
||||
}
|
||||
|
||||
@@ -14,6 +14,44 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* (Triforce) Long paths. The probe for image.bin below is the one Win32 path these helpers make. A path of MAX_PATH and more
|
||||
goes to GetFileAttributesW with "\\?\" in front ("\\?\UNC\" for \\server\share), which takes it whatever Windows' own
|
||||
LongPathsEnabled says; GetFileAttributesA does not past MAX_PATH (only when the manifest's longPathAware, nx2/win_manifest.py,
|
||||
meets LongPathsEnabled). The probe was a MAX_PATH buffer: a folder of 250 characters and more, whose bin\Triforce.exe a
|
||||
caller with a larger buffer had (discord_rpc.c's 1024), was taken for its bin folder. The path given back has no "\\?\": its
|
||||
callers add names with '/', which "\\?\" would keep as they are, and show it in messages and the log. path: a whole wide path,
|
||||
n its length. 1: there is a file or folder of that name */
|
||||
static inline int hos_path_there_w(const wchar_t *path, size_t n) {
|
||||
const int already = path[0] == L'\\' && path[1] == L'\\' && (path[2] == L'?' || path[2] == L'.');
|
||||
const int unc = !already && path[0] == L'\\' && path[1] == L'\\';
|
||||
const int drive = path[0] && path[1] == L':' && (path[2] == L'\\' || path[2] == L'/');
|
||||
if (n < MAX_PATH - 1 || already || !(unc || drive)) return GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES;
|
||||
wchar_t *w = (wchar_t *)malloc((n + 9) * sizeof *w);
|
||||
if (!w) return 0;
|
||||
wcscpy(w, unc ? L"\\\\?\\UNC" : L"\\\\?\\");
|
||||
size_t at = wcslen(w);
|
||||
for (size_t i = unc ? 1 : 0; i < n; i++) w[at++] = path[i] == L'/' ? L'\\' : path[i];
|
||||
w[at] = 0;
|
||||
const int there = GetFileAttributesW(w) != INVALID_FILE_ATTRIBUTES;
|
||||
free(w);
|
||||
return there;
|
||||
}
|
||||
|
||||
/* (the A helper's probe: a short one as it always was, through the A function; a long one as UTF-8, the process's code page
|
||||
with the manifest's activeCodePage, made wide) */
|
||||
static inline int hos_path_there_a(const char *path) {
|
||||
const size_t n = strlen(path);
|
||||
if (n < MAX_PATH - 1) return GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES;
|
||||
const int wn = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
|
||||
if (wn <= 0) return 0;
|
||||
wchar_t *w = (wchar_t *)malloc((size_t)wn * sizeof *w);
|
||||
if (!w) return 0;
|
||||
const int there = MultiByteToWideChar(CP_UTF8, 0, path, -1, w, wn) > 0 && hos_path_there_w(w, wcslen(w));
|
||||
free(w);
|
||||
return there;
|
||||
}
|
||||
|
||||
static inline DWORD hos_game_path_a(char *buf, DWORD size) {
|
||||
DWORD n = GetModuleFileNameA(NULL, buf, size);
|
||||
@@ -25,12 +63,14 @@ static inline DWORD hos_game_path_a(char *buf, DWORD size) {
|
||||
char *bin = dir;
|
||||
while (bin > buf && bin[-1] != '\\' && bin[-1] != '/') bin--;
|
||||
if (dir - bin != 3 || _strnicmp(bin, "bin", 3) != 0) return n;
|
||||
char probe[MAX_PATH + 16];
|
||||
const size_t root = (size_t)(bin - buf); /* (the folder, with its separator) */
|
||||
if (root + 10 >= sizeof probe) return n;
|
||||
char *probe = (char *)malloc(root + 10); /* ((Triforce) as long as the folder's path is: hos_path_there_w) */
|
||||
if (!probe) return n;
|
||||
memcpy(probe, buf, root);
|
||||
strcpy(probe + root, "image.bin");
|
||||
if (GetFileAttributesA(probe) == INVALID_FILE_ATTRIBUTES) return n;
|
||||
const int there = hos_path_there_a(probe);
|
||||
free(probe);
|
||||
if (!there) return n;
|
||||
memmove(buf + root, name, strlen(name) + 1); /* (folder\Triforce.exe) */
|
||||
return (DWORD)strlen(buf);
|
||||
}
|
||||
@@ -45,12 +85,14 @@ static inline DWORD hos_game_path_w(wchar_t *buf, DWORD size) {
|
||||
wchar_t *bin = dir;
|
||||
while (bin > buf && bin[-1] != L'\\' && bin[-1] != L'/') bin--;
|
||||
if (dir - bin != 3 || _wcsnicmp(bin, L"bin", 3) != 0) return n;
|
||||
wchar_t probe[MAX_PATH + 16];
|
||||
const size_t root = (size_t)(bin - buf);
|
||||
if (root + 10 >= sizeof probe / sizeof probe[0]) return n;
|
||||
wchar_t *probe = (wchar_t *)malloc((root + 10) * sizeof *probe); /* ((Triforce) hos_path_there_w) */
|
||||
if (!probe) return n;
|
||||
wmemcpy(probe, buf, root);
|
||||
wcscpy(probe + root, L"image.bin");
|
||||
if (GetFileAttributesW(probe) == INVALID_FILE_ATTRIBUTES) return n;
|
||||
const int there = hos_path_there_w(probe, root + 9);
|
||||
free(probe);
|
||||
if (!there) return n;
|
||||
wmemmove(buf + root, name, wcslen(name) + 1);
|
||||
return (DWORD)wcslen(buf);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ static struct {
|
||||
/* controllers */
|
||||
void *PadOpen, *PadClose, *PadConnected, *PadsUpdate, *PadButton, *PadAxis, *PadName, *PadRumble;
|
||||
void *PadType, *PadEffect; /* (optional: HD rumble, host/pad_sdl.c) */
|
||||
void *PadRumbleTriggers; /* (optional: the motors in an Xbox pad's triggers, host/pad_sdl.c) */
|
||||
void *PadHasSensor, *PadSensorEnable, *PadSensorData; /* (motion: optional, NULL in an SDL without them) */
|
||||
void *GetGamepads, *GetGamepadID, *Free; /* (SDL3) */
|
||||
void *NumJoysticks, *IsGameController, *DeviceInstanceID, *GetJoystick, *JoystickInstanceID; /* (SDL2) */
|
||||
@@ -67,6 +68,8 @@ static int get_all(HMODULE m, int three) {
|
||||
/* (SDL2 2.0.12 and 2.0.16 on, SDL3 3.2 on: without them every pad keeps the motor rumble) */
|
||||
*(FARPROC *)&f.PadType = GetProcAddress(m, three ? "SDL_GetGamepadType" : "SDL_GameControllerGetType");
|
||||
*(FARPROC *)&f.PadEffect = GetProcAddress(m, three ? "SDL_SendGamepadEffect" : "SDL_GameControllerSendEffect");
|
||||
/* ((Triforce) the triggers' motors: SDL2 2.0.14 on and every SDL3; without the call they stay still) */
|
||||
*(FARPROC *)&f.PadRumbleTriggers = GetProcAddress(m, three ? "SDL_RumbleGamepadTriggers" : "SDL_GameControllerRumbleTriggers");
|
||||
/* (motion sensors: SDL 2.0.14 on and every SDL3; without them the pads still play, with no gyro) */
|
||||
*(FARPROC *)&f.PadHasSensor = GetProcAddress(m, three ? "SDL_GamepadHasSensor" : "SDL_GameControllerHasSensor");
|
||||
*(FARPROC *)&f.PadSensorEnable = GetProcAddress(m, three ? "SDL_SetGamepadSensorEnabled" : "SDL_GameControllerSetSensorEnabled");
|
||||
@@ -599,6 +602,12 @@ void hos_sdl_pad_rumble(void *pad, uint16_t low, uint16_t high, uint32_t ms) {
|
||||
if (version == 3) CALL(_Bool (*)(void *, uint16_t, uint16_t, uint32_t), PadRumble)(pad, low, high, ms);
|
||||
else CALL(int (*)(void *, uint16_t, uint16_t, uint32_t), PadRumble)(pad, low, high, ms);
|
||||
}
|
||||
int hos_sdl_pad_has_trigger_rumble(void) { return f.PadRumbleTriggers != NULL; }
|
||||
int hos_sdl_pad_rumble_triggers(void *pad, uint16_t left, uint16_t right, uint32_t ms) {
|
||||
if (!f.PadRumbleTriggers) return 0;
|
||||
if (version == 3) return CALL(_Bool (*)(void *, uint16_t, uint16_t, uint32_t), PadRumbleTriggers)(pad, left, right, ms);
|
||||
return CALL(int (*)(void *, uint16_t, uint16_t, uint32_t), PadRumbleTriggers)(pad, left, right, ms) == 0;
|
||||
}
|
||||
int hos_sdl_pad_type(void *pad) { return f.PadType ? CALL(int (*)(void *), PadType)(pad) : 0; }
|
||||
int hos_sdl_pad_has_effect(void) { return f.PadEffect != NULL; }
|
||||
int hos_sdl_pad_effect(void *pad, const void *data, int size) {
|
||||
|
||||
@@ -73,6 +73,11 @@ int16_t hos_sdl_pad_axis(void *pad, int axis);
|
||||
const char *hos_sdl_pad_name(void *pad);
|
||||
int32_t hos_sdl_pad_id(void *pad);
|
||||
void hos_sdl_pad_rumble(void *pad, uint16_t low, uint16_t high, uint32_t ms);
|
||||
/* ((Triforce) the motors in the triggers, an Xbox One or Series pad's: SDL_RumbleGamepadTriggers /
|
||||
SDL_GameControllerRumbleTriggers) whether the SDL has the call; the left and the right trigger's strength for ms: 1
|
||||
when SDL took it, 0 when the pad has none, its driver cannot reach them, or the SDL has no such call */
|
||||
int hos_sdl_pad_has_trigger_rumble(void);
|
||||
int hos_sdl_pad_rumble_triggers(void *pad, uint16_t left, uint16_t right, uint32_t ms);
|
||||
/* (HD rumble, host/pad_sdl.c) the pad's SDL_GetGamepadType / SDL_GameControllerGetType, in the loaded SDL's numbers (0
|
||||
unknown, or no such call); whether the SDL has the effect call; a raw effect sent (SDL_SendGamepadEffect /
|
||||
SDL_GameControllerSendEffect): 1 when SDL took it */
|
||||
|
||||
@@ -462,11 +462,26 @@ void a64_svc(Ctx *c, uint32_t imm) {
|
||||
c->x[0] = R_OK;
|
||||
return;
|
||||
}
|
||||
case 0x2C: /* MapPhysicalMemory(address, size) */
|
||||
hos_commit(c->x[0], c->x[1]);
|
||||
case 0x2C: { /* MapPhysicalMemory(address, size) */
|
||||
const int committed = hos_commit(c->x[0], c->x[1]);
|
||||
if (hos_trace) hos_log("MapPhysicalMemory(%#llx, %#llx)", (unsigned long long)c->x[0], (unsigned long long)c->x[1]);
|
||||
if (committed > 0) hos_mem_note(HOS_MEM_MAPPED, c->x[0], c->x[1]); /* (Triforce: the census, the game's physical maps) */
|
||||
/* ((Triforce) Windows refused the commit (the PC's commit is spent; hos_commit logged it): Horizon's LimitReached, which
|
||||
the SDK turns into its out-of-memory result, rather than success and a crash at the first touch of a page that is
|
||||
not there. HOS_MEM_MAP_RESULT=0: success, as before) */
|
||||
static int refuse = -1;
|
||||
if (refuse < 0) refuse = !(getenv("HOS_MEM_MAP_RESULT") && atoi(getenv("HOS_MEM_MAP_RESULT")) == 0);
|
||||
if (committed == 0 && refuse) {
|
||||
static volatile LONG told;
|
||||
if (InterlockedIncrement(&told) <= 16)
|
||||
hos_log("mem: MapPhysicalMemory(%#llx, %#llx): the commit failed: LimitReached (%#x) to the game", (unsigned long long)c->x[0],
|
||||
(unsigned long long)c->x[1], 0x10801u);
|
||||
c->x[0] = 0x10801; /* (svc::ResultLimitReached) */
|
||||
return;
|
||||
}
|
||||
c->x[0] = R_OK;
|
||||
return;
|
||||
}
|
||||
case 0x2D: /* UnmapPhysicalMemory(address, size): the pages read as zero if mapped again */
|
||||
if (!hos_guest_range(c->x[0], c->x[1])) { /* (hookshot: guest pages only) */
|
||||
c->x[0] = 0xCA01;
|
||||
@@ -474,8 +489,12 @@ void a64_svc(Ctx *c, uint32_t imm) {
|
||||
}
|
||||
{ /* (pages under a Vulkan import stay committed, zeroed: hos_mem.c FreeMemoryPages) */
|
||||
extern int rx_guest_imported(uint64_t lo, uint64_t hi);
|
||||
if (rx_guest_imported(c->x[0], c->x[0] + c->x[1])) memset(GP(c->x[0]), 0, (size_t)c->x[1]);
|
||||
else VirtualFree(GP(c->x[0]), (SIZE_T)c->x[1], MEM_DECOMMIT);
|
||||
const int io = hos_guest_io_begin(GP(c->x[0]), c->x[1]); /* (Triforce: the lazy write-back's pages first: rx_backend.c) */
|
||||
/* ((Triforce) the census: zeroed pages stay committed, kept for the give-back once no import holds them) */
|
||||
if (rx_guest_imported(c->x[0], c->x[0] + c->x[1]))
|
||||
memset(GP(c->x[0]), 0, (size_t)c->x[1]), hos_mem_note(HOS_MEM_ZEROED, c->x[0], c->x[1]);
|
||||
else VirtualFree(GP(c->x[0]), (SIZE_T)c->x[1], MEM_DECOMMIT), hos_mem_note(HOS_MEM_DECOMMITTED, c->x[0], c->x[1]);
|
||||
hos_guest_io_end(io);
|
||||
}
|
||||
c->x[0] = R_OK;
|
||||
return;
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
there. TPIDRRO_EL0 (thread-local region): 0x200 bytes; the SDK's nn::os keeps the current ThreadType* at
|
||||
+0x1F8, so lifted SDK code that reads it inline finds the same thread the HLE knows. */
|
||||
#include "hos.h"
|
||||
#include "hos_x3d.h"
|
||||
#include "pace_wait.h" /* ((Triforce) hos_sleep_until_ns: the frame wait to its time) */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -32,10 +34,11 @@ struct HosThread {
|
||||
/* the kernel side (hos_svc.c): this thread's Horizon handle and the event it blocks on */
|
||||
uint32_t kernel_handle;
|
||||
HANDLE wake;
|
||||
/* virtual cores (HOS_VCORES): the core held + 1 (0: none), the last one, a better waiter's request, queued */
|
||||
/* virtual cores (HOS_VCORES): the core held + 1 (0: none), the last one, a better waiter's request, queued; (Triforce)
|
||||
when a release granted it the core it waits for (vc_grant), until it runs (the vcores: line's grant to run) */
|
||||
int vc_held, vc_last, vc_queued;
|
||||
volatile int vc_kick;
|
||||
int64_t vc_since;
|
||||
int64_t vc_since, vc_grant_at;
|
||||
CONDITION_VARIABLE vc_cv;
|
||||
/* (hookshot) fibers: the context running now (NULL: ctx, the thread's own), the thread's host fiber once it
|
||||
switched to a guest fiber */
|
||||
@@ -306,12 +309,46 @@ void hos_log(const char *fmt, ...) {
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
/* (Triforce, after NX2navi) 32 KiB of the calling thread's or fiber's stack kept back for its own stack overflow. Windows
|
||||
leaves a thread that ran out of stack about a guard page's worth, and the crash report (mkw_glue.c report_crash: its
|
||||
kilobyte buffers, hos_log's, the frame walk) needs more: it faulted again inside the report and the process ended with
|
||||
nothing in the log. The game's threads and fibers ask at their start (64 MB of stack reserved each, so a guest function
|
||||
that calls itself without end is the case), and each then commits that much more of its stack. The guarantee is the
|
||||
running fiber's own, so a fiber asks too. tests/test_crash_report.py */
|
||||
void hos_stack_guarantee(void) {
|
||||
#ifdef _WIN32
|
||||
ULONG bytes = 32768;
|
||||
SetThreadStackGuarantee(&bytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* (Triforce, after NX2navi) One end for a failed run. A crash is reported by every thread that faults, without waiting
|
||||
for the others, and a fatal error can come beside a crash from the same bad state (an allocation that failed here, a
|
||||
null pointer read there): the later one ended the process and cut the first report where it was. 1 for the first
|
||||
caller, which reports and ends the process; 0 for that thread again (a failure inside its own report: no second
|
||||
one); any other thread waits here, logging nothing (it may hold what the report needs), until the process ends, and
|
||||
ends it itself after 30 s if the first report never finishes. */
|
||||
int hos_ending_enter(void) {
|
||||
static volatile LONG ending;
|
||||
const LONG me = (LONG)GetCurrentThreadId();
|
||||
const LONG was = InterlockedCompareExchange(&ending, me, 0);
|
||||
if (!was) return 1;
|
||||
if (was == me) return 0;
|
||||
Sleep(30000);
|
||||
TerminateProcess(GetCurrentProcess(), 3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hos_fatal(Ctx *c, const char *fmt, ...) {
|
||||
char buf[1024];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buf, sizeof buf, fmt, ap);
|
||||
va_end(ap);
|
||||
if (!hos_ending_enter()) { /* (a fatal error inside this thread's own report: the report so far stands) */
|
||||
TerminateProcess(GetCurrentProcess(), 3);
|
||||
ExitProcess(3);
|
||||
}
|
||||
if (!c) c = hos_ctx();
|
||||
log_flush();
|
||||
fprintf(stderr, "[hos %s] FATAL: %s", cur ? cur->name : "host", buf);
|
||||
@@ -321,7 +358,12 @@ void hos_fatal(Ctx *c, const char *fmt, ...) {
|
||||
void hos_backtrace(void), hos_hostcall_stats(void);
|
||||
hos_backtrace();
|
||||
hos_hostcall_stats();
|
||||
ExitProcess(3);
|
||||
/* (Triforce, after NX2navi) TerminateProcess, not ExitProcess: ExitProcess stops the other threads wherever they are
|
||||
and then runs every library's detach on this thread, and a detach that faults there (an upscaler's, a driver's) put
|
||||
a crash report of its own after the lines above, or held the process open. Everything said here is written out
|
||||
already (log_flush, fflush). tests/test_device_lost_exit.py */
|
||||
TerminateProcess(GetCurrentProcess(), 3);
|
||||
ExitProcess(3); /* (not reached) */
|
||||
}
|
||||
|
||||
/* Traps from the lifted code (rt/a64_rt.c): name the thread, then stop. */
|
||||
@@ -468,6 +510,7 @@ Ctx *hos_boot_main_thread(uint64_t stack_size) {
|
||||
|
||||
static DWORD WINAPI thread_main(void *p) {
|
||||
HosThread *t = (HosThread *)p;
|
||||
hos_stack_guarantee();
|
||||
cur = t;
|
||||
Ctx *c = &t->ctx;
|
||||
c->x[0] = t->arg;
|
||||
@@ -623,6 +666,79 @@ static uint64_t pick_reserved_core(const uint64_t *masks, const unsigned char *c
|
||||
if (logical < 8 || fast < 4) return 0;
|
||||
return last;
|
||||
}
|
||||
/* (Triforce, after NX2navi's cache placement) The die with the largest L3 cache, on a processor whose L3 caches differ: a
|
||||
two-die X3D part, 3D V-Cache on one die (host/hos_x3d.h has the rule and where nothing is done). The game's threads
|
||||
are kept on it (hos_thread_pin), and the core kept for the replay is its last one (last_fastest_core), not the
|
||||
processor's last, which is on the other die: the replay reads what the game's threads just wrote. Only where the rule
|
||||
finds such a die; everywhere else 0, and nothing changes. HOS_X3D=0: off there too. Other threads of the process (the
|
||||
driver's, the shader workers) stay where Windows puts them. Windows only: Linux as before.
|
||||
NX2navi keeps the whole process on the die and reserves no core there; here the replay's core stays, on the die, as
|
||||
the replay is pinned to it by default (gpu/vk/vk_replay.cpp ReplayPin). HOS_RESERVE_CORE=0 keeps none, as ever */
|
||||
#if defined(_WIN32)
|
||||
/* (hos_x3d_pick over what Windows lists for the calling thread's processor group) */
|
||||
static uint64_t x3d_detect(HosX3d *out) {
|
||||
static HosCache caches[1024];
|
||||
static uint64_t cores[256];
|
||||
int ncaches = 0, ncores = 0;
|
||||
HosX3d none = {HOS_X3D_NONE, 0, 0, 0, 0};
|
||||
*out = none;
|
||||
DWORD_PTR proc = 0, sys = 0;
|
||||
if (!GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys) || !proc) return 0;
|
||||
GROUP_AFFINITY ga;
|
||||
const WORD group = GetThreadGroupAffinity(GetCurrentThread(), &ga) ? ga.Group : 0;
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
const LOGICAL_PROCESSOR_RELATIONSHIP rel = pass ? RelationProcessorCore : RelationCache;
|
||||
DWORD len = 0;
|
||||
if (GetLogicalProcessorInformationEx(rel, NULL, &len) || GetLastError() != ERROR_INSUFFICIENT_BUFFER) return 0;
|
||||
char *buf = (char *)malloc(len);
|
||||
if (!buf || !GetLogicalProcessorInformationEx(rel, (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)buf, &len)) {
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
||||
for (DWORD at = 0; at < len;) {
|
||||
const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(buf + at);
|
||||
if (!e->Size) break;
|
||||
if (e->Relationship == RelationCache && e->Cache.GroupMask.Group == group && ncaches < 1024) {
|
||||
caches[ncaches].level = e->Cache.Level;
|
||||
caches[ncaches].mask = (uint64_t)e->Cache.GroupMask.Mask;
|
||||
caches[ncaches].bytes = e->Cache.CacheSize;
|
||||
ncaches++;
|
||||
} else if (e->Relationship == RelationProcessorCore && e->Processor.GroupCount &&
|
||||
e->Processor.GroupMask[0].Group == group && ncores < 256) {
|
||||
cores[ncores++] = (uint64_t)e->Processor.GroupMask[0].Mask;
|
||||
}
|
||||
at += e->Size;
|
||||
}
|
||||
free(buf);
|
||||
}
|
||||
return hos_x3d_pick(caches, ncaches, cores, ncores, (uint64_t)proc, (int)GetActiveProcessorGroupCount(), out);
|
||||
}
|
||||
#endif
|
||||
static uint64_t hos_x3d_cpus(void) {
|
||||
static uint64_t mask = ~0ull;
|
||||
static SRWLOCK lk = SRWLOCK_INIT;
|
||||
AcquireSRWLockExclusive(&lk);
|
||||
if (mask == ~0ull) {
|
||||
mask = 0;
|
||||
#if defined(_WIN32)
|
||||
HosX3d x;
|
||||
const char *e = getenv("HOS_X3D");
|
||||
const int off = e && *e && !atoi(e);
|
||||
const uint64_t die = x3d_detect(&x);
|
||||
if (x.why == HOS_X3D_USED && !off) mask = die;
|
||||
if (x.why == HOS_X3D_USED || x.why == HOS_X3D_FEW) /* (a processor with L3 caches of two sizes: said either way) */
|
||||
hos_log("cpu: logical processors %#llx share the largest L3 cache (%llu MB, against %llu MB), %d cores: %s",
|
||||
(unsigned long long)x.die, (unsigned long long)(x.big >> 20), (unsigned long long)(x.small >> 20), x.cores,
|
||||
x.why == HOS_X3D_FEW ? "fewer than 8, so the game's threads are not kept there"
|
||||
: off ? "not used (HOS_X3D=0)"
|
||||
: "the game's threads and the replay's core are kept there (HOS_X3D=0: as before)");
|
||||
#endif
|
||||
}
|
||||
const uint64_t m = mask;
|
||||
ReleaseSRWLockExclusive(&lk);
|
||||
return m;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
/* (pick_reserved_core over the calling thread's group's cores; *cores: how many, 0 when they could not be read) */
|
||||
static uint64_t last_fastest_core(int *cores) {
|
||||
@@ -633,6 +749,7 @@ static uint64_t last_fastest_core(int *cores) {
|
||||
*cores = 0;
|
||||
GROUP_AFFINITY ga;
|
||||
const WORD group = GetThreadGroupAffinity(GetCurrentThread(), &ga) ? ga.Group : 0;
|
||||
const uint64_t die = hos_x3d_cpus(); /* (Triforce: the cores of the die the game's threads are kept on, when there is one) */
|
||||
if (GetLogicalProcessorInformationEx(RelationProcessorCore, NULL, &len) || GetLastError() != ERROR_INSUFFICIENT_BUFFER)
|
||||
return 0;
|
||||
char *buf = (char *)malloc(len);
|
||||
@@ -640,7 +757,8 @@ static uint64_t last_fastest_core(int *cores) {
|
||||
for (DWORD at = 0; at < len && n < 256;) {
|
||||
const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *e = (const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *)(buf + at);
|
||||
if (!e->Size) break;
|
||||
if (e->Relationship == RelationProcessorCore && e->Processor.GroupCount && e->Processor.GroupMask[0].Group == group) {
|
||||
if (e->Relationship == RelationProcessorCore && e->Processor.GroupCount && e->Processor.GroupMask[0].Group == group &&
|
||||
(!die || ((uint64_t)e->Processor.GroupMask[0].Mask & die))) {
|
||||
masks[n] = (uint64_t)e->Processor.GroupMask[0].Mask;
|
||||
classes[n] = e->Processor.EfficiencyClass;
|
||||
n++;
|
||||
@@ -679,11 +797,12 @@ uint64_t hos_reserved_cpus(void) {
|
||||
}
|
||||
|
||||
void hos_thread_pin(void *h, const char *name) {
|
||||
const uint64_t reserved = hos_reserved_cpus();
|
||||
if (reserved) {
|
||||
const uint64_t reserved = hos_reserved_cpus(), die = hos_x3d_cpus();
|
||||
if (reserved || die) { /* (Triforce: on the die with the largest L3 where there is one, hos_x3d.h; else as before) */
|
||||
DWORD_PTR proc = 0, sys = 0;
|
||||
GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys);
|
||||
if ((uint64_t)proc & ~reserved) SetThreadAffinityMask(h, (DWORD_PTR)((uint64_t)proc & ~reserved));
|
||||
const uint64_t want = hos_x3d_thread_mask((uint64_t)proc, die, reserved);
|
||||
if (want) SetThreadAffinityMask(h, (DWORD_PTR)want);
|
||||
}
|
||||
static NameSpec pins[24], prios[24];
|
||||
static int npins = -1, nprios;
|
||||
@@ -790,6 +909,24 @@ static void sleep_ns(int64_t ns) {
|
||||
if (ns > 2000000) Sleep((DWORD)((ns - 1000000) / 1000000));
|
||||
while (hos_now_ns() < until) SwitchToThread();
|
||||
}
|
||||
/* (Triforce) The game's frame wait to its time (pace_wait.h, host/nn_vi.c hos_vi_native_acquire): the timer up to the
|
||||
margin before it (sleep_ns, which takes the timer for a millisecond and more), the rest yielded away as sleep_ns yields
|
||||
a short wait; the core given up once for the whole of it, as hos_sleep_ns gives it up */
|
||||
static void yield_for_wait(void) {
|
||||
if (!SwitchToThread()) YieldProcessor();
|
||||
}
|
||||
void hos_sleep_until_ns(int64_t until, int64_t margin_ns) {
|
||||
if (until <= hos_now_ns()) return;
|
||||
const int away = hos_vcore_away();
|
||||
#ifdef __EMSCRIPTEN__
|
||||
(void)yield_for_wait, (void)margin_ns; /* (the web's own sleep_ns spins the end of a wait already) */
|
||||
const int64_t left = until - hos_now_ns();
|
||||
if (left > 0) sleep_ns(left);
|
||||
#else
|
||||
pace_wait_until(until, margin_ns, hos_now_ns, sleep_ns, yield_for_wait);
|
||||
#endif
|
||||
if (away) hos_vcore_back();
|
||||
}
|
||||
|
||||
/* ---- nn::os threads -------------------------------------------------------------------------------------- */
|
||||
static HosThread *thread_arg(Ctx *c, uint64_t tt) {
|
||||
@@ -1061,6 +1198,7 @@ static void fiber_switch(Ctx *from, HosFiber *to);
|
||||
|
||||
static void WINAPI fiber_main(void *p) {
|
||||
HosFiber *f = (HosFiber *)p;
|
||||
hos_stack_guarantee(); /* (the guarantee is the fiber's own stack's) */
|
||||
Ctx *c = &f->ctx;
|
||||
c->x[0] = f->arg;
|
||||
c->x[30] = 0;
|
||||
@@ -1169,13 +1307,19 @@ void hos_thread_log_handles(void) {
|
||||
once. Guest code with no checkpoint (a spin loop) keeps its core, so a waiter that has waited too long runs anyway
|
||||
without one (an "escape", logged with the holders): HOS_VCORE_ESCAPE_US for a better waiter (default 1000),
|
||||
HOS_VCORE_ESCAPE_EQ_US otherwise (20000). Every 5 s the log has a "vcores:" line. GetCurrentCoreNumber answers the
|
||||
core a thread holds. */
|
||||
core a thread holds. (Triforce) A waiter that asked a holder for its core spins for it before it sleeps (vc_spin). */
|
||||
#if !defined(__EMSCRIPTEN__)
|
||||
static SRWLOCK vc_lock = SRWLOCK_INIT;
|
||||
static HosThread *vc_owner[8];
|
||||
static HosThread *vc_queue[1024];
|
||||
static int vc_nq;
|
||||
static struct { int64_t takes, waits, wait_ns, max_ns, escapes, kicks, yields, since; } vc_st;
|
||||
static struct {
|
||||
int64_t takes, waits, wait_ns, max_ns, escapes, kicks, yields, since;
|
||||
/* (Triforce) the waits that ended in the spin and those that slept, the time spun, the spins left out (HOS_VCORE_SPIN_MAX
|
||||
threads spinning already); grant to run: from vc_grant until the waiter holds vc_lock again, after a spin and after a
|
||||
sleep (NX2navi's "core grant to run", runtime/kernel.c grant_at) */
|
||||
int64_t spun, slept, spin_ns, spin_capped, run_spin_ns, run_spin_n, run_sleep_ns, run_sleep_n, run_max_ns;
|
||||
} vc_st;
|
||||
|
||||
int hos_vcores_on(void) {
|
||||
static int on = -1;
|
||||
@@ -1218,7 +1362,7 @@ static int vc_free_for(HosThread *t) {
|
||||
}
|
||||
static void vc_take(HosThread *t, int c) {
|
||||
vc_owner[c] = t;
|
||||
t->vc_held = c + 1;
|
||||
__atomic_store_n(&t->vc_held, c + 1, __ATOMIC_RELEASE); /* ((Triforce) a waiter in vc_spin reads it without vc_lock) */
|
||||
t->vc_last = c + 1;
|
||||
t->vc_kick = 0;
|
||||
}
|
||||
@@ -1237,16 +1381,23 @@ static void vc_grant(int c) {
|
||||
if ((vc_allowed(vc_queue[i]) >> c & 1) && (!best || vc_better(vc_queue[i], best))) best = vc_queue[i];
|
||||
if (!best) return;
|
||||
vc_dequeue(best);
|
||||
best->vc_grant_at = hos_now_ns(); /* ((Triforce) grant to run, taken where it holds vc_lock again: vc_acquire) */
|
||||
vc_take(best, c);
|
||||
WakeConditionVariable(&best->vc_cv);
|
||||
WakeConditionVariable(&best->vc_cv); /* (no one sleeps there when it spins: vc_spin reads vc_held) */
|
||||
}
|
||||
static void vc_stats_locked(int64_t now) {
|
||||
if (!vc_st.since) vc_st.since = now;
|
||||
if (now - vc_st.since < 5000000000LL) return;
|
||||
hos_log("vcores: %lld takes, %lld waits (mean %.0f us, longest %.1f ms), %lld kicks, %lld yields, %lld escapes in %.0f s",
|
||||
hos_log("vcores: %lld takes, %lld waits (mean %.0f us, longest %.1f ms), %lld kicks, %lld yields, %lld escapes in %.0f s; "
|
||||
"%lld of the waits ended in the spin (%.2f ms spun, %lld spins left out with HOS_VCORE_SPIN_MAX threads spinning), "
|
||||
"%lld slept; a granted core ran %.1f us after its grant after a spin (%lld), %.1f us after a sleep (%lld), longest "
|
||||
"%.0f us",
|
||||
(long long)vc_st.takes, (long long)vc_st.waits, vc_st.waits ? vc_st.wait_ns / 1e3 / vc_st.waits : 0.0,
|
||||
vc_st.max_ns / 1e6, (long long)vc_st.kicks, (long long)vc_st.yields, (long long)vc_st.escapes,
|
||||
(now - vc_st.since) / 1e9);
|
||||
(now - vc_st.since) / 1e9, (long long)vc_st.spun, vc_st.spin_ns / 1e6, (long long)vc_st.spin_capped,
|
||||
(long long)vc_st.slept, vc_st.run_spin_n ? vc_st.run_spin_ns / 1e3 / vc_st.run_spin_n : 0.0,
|
||||
(long long)vc_st.run_spin_n, vc_st.run_sleep_n ? vc_st.run_sleep_ns / 1e3 / vc_st.run_sleep_n : 0.0,
|
||||
(long long)vc_st.run_sleep_n, vc_st.run_max_ns / 1e3);
|
||||
memset(&vc_st, 0, sizeof vc_st);
|
||||
vc_st.since = now;
|
||||
}
|
||||
@@ -1254,10 +1405,63 @@ static int64_t vc_env_ns(const char *name, int64_t us) {
|
||||
const char *e = getenv(name);
|
||||
return (e ? atoll(e) : us) * 1000;
|
||||
}
|
||||
/* (Triforce, the third performance audit's P1: NX2navi's spin before the sleep, runtime/kernel.c core_take, which came
|
||||
from totk-pc-recomp's runtime/kernel.c, MIT, Copyright (c) 2026 the totk-pc-recomp authors) A waiter that asked a holder
|
||||
to give its core up spins for it before it sleeps: up to HOS_VCORE_SPIN_US (250; 0: it sleeps at once, as before), with
|
||||
vc_lock let go, until a release grants it the core (vc_grant sets its vc_held), then takes the lock again. The holder
|
||||
gives the core up at its next nn::os call, SVC or loop poll, mostly within microseconds; a sleep adds an OS wake to
|
||||
each such hand-off (NX2navi's log on this PC: 1,391,252 waits ended in its spin against 530 that did not; Triforce at
|
||||
the Market: ~66,000 core waits per 5 s, 67 us mean). Who gets a core does not change: the waiter stays queued, and
|
||||
vc_grant picks the best waiter as before; only how soon the one it picks runs. At most HOS_VCORE_SPIN_MAX threads spin
|
||||
at once (vc_spin_most: by default a quarter of the logical processors beyond 8, at most 4: none on 8 or fewer, 1 on 12,
|
||||
2 on 16), so that the spin takes only processors the game leaves idle (5.4-6.1 of 16 busy at the Market) and a busy PC
|
||||
does not lose. tests/test_vcore_spin.py */
|
||||
static int vc_spin_most(int logical) {
|
||||
const int n = logical > 8 ? (logical - 8) / 4 : 0;
|
||||
return n > 4 ? 4 : n;
|
||||
}
|
||||
static int vc_spinning; /* (threads in vc_spin now) */
|
||||
static int vc_spin_max(void) {
|
||||
static int most = -1;
|
||||
if (most < 0) {
|
||||
const char *e = getenv("HOS_VCORE_SPIN_MAX");
|
||||
SYSTEM_INFO si;
|
||||
GetSystemInfo(&si);
|
||||
const int n = e ? atoi(e) : vc_spin_most((int)si.dwNumberOfProcessors);
|
||||
most = n < 0 ? 0 : n;
|
||||
}
|
||||
return most;
|
||||
}
|
||||
/* holding vc_lock: lets it go and spins until t holds a core or ns pass, then takes it again; 0: it did not spin
|
||||
(HOS_VCORE_SPIN_MAX threads spin already) */
|
||||
static int vc_spin(HosThread *t, int64_t ns) {
|
||||
if (__atomic_add_fetch(&vc_spinning, 1, __ATOMIC_RELAXED) > vc_spin_max()) {
|
||||
__atomic_sub_fetch(&vc_spinning, 1, __ATOMIC_RELAXED);
|
||||
vc_st.spin_capped++;
|
||||
return 0;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&vc_lock);
|
||||
const int64_t s0 = hos_now_ns(), end = s0 + ns;
|
||||
while (!__atomic_load_n(&t->vc_held, __ATOMIC_ACQUIRE) && hos_now_ns() < end) _mm_pause();
|
||||
const int64_t spun = hos_now_ns() - s0;
|
||||
__atomic_sub_fetch(&vc_spinning, 1, __ATOMIC_RELAXED);
|
||||
AcquireSRWLockExclusive(&vc_lock);
|
||||
vc_st.spin_ns += spun;
|
||||
return 1;
|
||||
}
|
||||
static void vc_acquire(HosThread *t) {
|
||||
static int64_t esc_better = -1, esc_eq, slice;
|
||||
static int64_t esc_better = -1, esc_eq, slice, spin;
|
||||
if (esc_better < 0) {
|
||||
esc_eq = vc_env_ns("HOS_VCORE_ESCAPE_EQ_US", 250000);
|
||||
{ /* ((Triforce) the spin, vc_spin: HOS_VCORE_SPIN_US, up to a second; off where no thread may spin) */
|
||||
const int64_t s = vc_env_ns("HOS_VCORE_SPIN_US", 250);
|
||||
spin = !vc_spin_max() || s <= 0 ? 0 : s > 1000000000LL ? 1000000000LL : s;
|
||||
static int told;
|
||||
if (!told++)
|
||||
hos_log("vcores: a waiter spins up to %lld us for a core it asked a holder for before it sleeps "
|
||||
"(HOS_VCORE_SPIN_US; 0: off), %d threads at most at once (HOS_VCORE_SPIN_MAX)",
|
||||
(long long)(spin / 1000), vc_spin_max());
|
||||
}
|
||||
esc_better = vc_env_ns("HOS_VCORE_ESCAPE_US", 250000);
|
||||
slice = vc_env_ns("HOS_VCORE_SLICE_US", 2000);
|
||||
}
|
||||
@@ -1274,6 +1478,7 @@ static void vc_acquire(HosThread *t) {
|
||||
t->vc_queued = 1;
|
||||
if (vc_nq < 1024) vc_queue[vc_nq++] = t;
|
||||
vc_st.waits++;
|
||||
int spun = 0, slept = 0, ended = 0; /* ((Triforce) the spin tried, a sleep taken, the wait ended in the spin) */
|
||||
for (;;) {
|
||||
/* the worst holder worse than t on a core t may use gives it up at its next checkpoint; after
|
||||
HOS_VCORE_SLICE_US (2000) of waiting, a holder as good as t does too (a time slice: Horizon would leave
|
||||
@@ -1294,6 +1499,24 @@ static void vc_acquire(HosThread *t) {
|
||||
__atomic_store_n(rc ? &rc->poll : &worst->ctx.poll, 1, __ATOMIC_RELAXED);
|
||||
vc_st.kicks++;
|
||||
}
|
||||
/* (Triforce) a holder was asked for a core t may use: a spin for it first, once a wait (vc_spin). A release in
|
||||
the spin granted t its core (vc_held), the only one it then holds; else a core freed that no waiter took */
|
||||
if (worst && spin && !spun) {
|
||||
spun = 1;
|
||||
if (vc_spin(t, spin)) {
|
||||
if (t->vc_held) {
|
||||
ended = 1;
|
||||
break;
|
||||
}
|
||||
if ((c = vc_free_for(t)) >= 0) {
|
||||
vc_dequeue(t);
|
||||
vc_take(t, c);
|
||||
ended = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
slept = 1;
|
||||
SleepConditionVariableSRW(&t->vc_cv, &vc_lock, 1, 0);
|
||||
if (t->vc_held) break; /* granted by a release */
|
||||
if ((c = vc_free_for(t)) >= 0) {
|
||||
@@ -1320,10 +1543,19 @@ static void vc_acquire(HosThread *t) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
const int64_t waited = hos_now_ns() - t0;
|
||||
const int64_t now = hos_now_ns(), waited = now - t0;
|
||||
vc_st.wait_ns += waited;
|
||||
if (waited > vc_st.max_ns) vc_st.max_ns = waited;
|
||||
vc_stats_locked(hos_now_ns());
|
||||
if (ended) vc_st.spun++; /* ((Triforce) every wait ends in the spin or after a sleep) */
|
||||
else if (slept) vc_st.slept++;
|
||||
if (t->vc_grant_at) { /* ((Triforce) granted by a release: from the grant until it runs) */
|
||||
const int64_t run = now - t->vc_grant_at;
|
||||
if (ended) vc_st.run_spin_ns += run, vc_st.run_spin_n++;
|
||||
else vc_st.run_sleep_ns += run, vc_st.run_sleep_n++;
|
||||
if (run > vc_st.run_max_ns) vc_st.run_max_ns = run;
|
||||
t->vc_grant_at = 0;
|
||||
}
|
||||
vc_stats_locked(now);
|
||||
ReleaseSRWLockExclusive(&vc_lock);
|
||||
}
|
||||
static void vc_release(HosThread *t) {
|
||||
@@ -1339,6 +1571,12 @@ static void vc_release(HosThread *t) {
|
||||
/* gives the core to a waiter allowed on it that is better than t (strict) or as good (a yield) */
|
||||
static void vc_yield(HosThread *t, int strict) {
|
||||
if (!t->vc_held) return;
|
||||
/* (Triforce, NX2navi's yield fast path: runtime/kernel.c SleepThread's yield, chrg6 745d48bc) nobody waits for a core
|
||||
and no waiter asked t for its own: no lock taken, as most yields find (the game's yield loops pay this thousands of
|
||||
times a second). The look is racy as NX2navi's: a thread that queues right after it is seen by t's next yield, and
|
||||
asks t itself meanwhile (vc_acquire's kick: at once when better, after HOS_VCORE_SLICE_US when as good); a request
|
||||
already made (vc_kick) takes the lock as before */
|
||||
if (!__atomic_load_n(&vc_nq, __ATOMIC_RELAXED) && !t->vc_kick) return;
|
||||
AcquireSRWLockExclusive(&vc_lock);
|
||||
const int c = t->vc_held - 1;
|
||||
int any = 0;
|
||||
|
||||
99
host/hos_x3d.h
Normal file
99
host/hos_x3d.h
Normal file
@@ -0,0 +1,99 @@
|
||||
/* (Triforce, after NX2navi's cache placement) The logical processors that share the largest L3 cache, on a processor whose
|
||||
L3 caches are not all the same size: AMD's two-die X3D parts (a Ryzen 9 7950X3D or 9950X3D), where one die of eight
|
||||
cores has the stacked 3D V-Cache (96 MB) and the other has not (32 MB). The game's threads and the replay's core are
|
||||
kept on that die (host/hos_thread.c), so that what one of them wrote is in the cache the next one reads it from, and
|
||||
none of them crosses dies. Windows' own placement of games on that die (Game Mode with AMD's chipset driver) depends
|
||||
on its taking the program for a game.
|
||||
|
||||
Nothing is done:
|
||||
- with every L3 cache the same size: one die (a 7800X3D), two dies alike (a 7950X, a part with the cache on both), an
|
||||
Intel processor of two kinds of core, whose cores differ in their L2 caches and share one L3;
|
||||
- where the largest L3 serves fewer than 8 cores: a 7900X3D's six, or the four large cores of a mobile part whose
|
||||
small cores share a smaller L3. The game runs six cores' worth of threads beside the replay's: held to fewer cores
|
||||
it would lose more than the cache gives (NX2navi's rule, measured there);
|
||||
- with more than one processor group (over 64 logical processors): the masks here are one group's;
|
||||
- when the process is already held to that die (an affinity given at its start), or to so little of it that fewer
|
||||
than 8 of its cores are left.
|
||||
|
||||
Plain code over the caches and cores the system lists, with no system calls: tests/test_x3d_placement.py runs it on
|
||||
authored topologies. */
|
||||
#ifndef HOS_X3D_H
|
||||
#define HOS_X3D_H
|
||||
#include <stdint.h>
|
||||
|
||||
/* A cache as the system lists it: its level (1 to 3), the logical processors that share it, its size in bytes */
|
||||
typedef struct { int level; uint64_t mask, bytes; } HosCache;
|
||||
|
||||
enum {
|
||||
HOS_X3D_NONE, /* no L3 cache listed, or a list that contradicts itself */
|
||||
HOS_X3D_GROUPS, /* more than one processor group */
|
||||
HOS_X3D_SAME, /* every L3 the same size */
|
||||
HOS_X3D_FEW, /* the largest L3 serves fewer than HOS_X3D_MIN_CORES of the cores the process may use */
|
||||
HOS_X3D_ALL, /* the process may use no processor outside it: nothing to do */
|
||||
HOS_X3D_USED /* the die: the processors to keep the game's threads on */
|
||||
};
|
||||
#define HOS_X3D_MIN_CORES 8
|
||||
|
||||
typedef struct {
|
||||
int why; /* HOS_X3D_* */
|
||||
uint64_t die; /* the allowed logical processors under the largest L3 (FEW, ALL, USED) */
|
||||
uint64_t big, small; /* the largest and the smallest L3, in bytes */
|
||||
int cores; /* the cores in die */
|
||||
} HosX3d;
|
||||
|
||||
/* caches: every cache listed (the L3 ones are looked at); cores: each core's logical processors; allowed: the
|
||||
process's affinity; groups: the active processor groups. The die's mask when the game's threads are to be kept on it
|
||||
(HOS_X3D_USED), else 0; *out says which and why */
|
||||
static inline uint64_t hos_x3d_pick(const HosCache *caches, int ncaches, const uint64_t *cores, int ncores,
|
||||
uint64_t allowed, int groups, HosX3d *out) {
|
||||
HosX3d x = {HOS_X3D_NONE, 0, 0, 0, 0};
|
||||
uint64_t die = 0, rest = 0;
|
||||
int n = 0;
|
||||
*out = x;
|
||||
if (groups != 1) {
|
||||
out->why = HOS_X3D_GROUPS;
|
||||
return 0;
|
||||
}
|
||||
for (int i = 0; i < ncaches; i++) {
|
||||
if (caches[i].level != 3 || !caches[i].mask || !caches[i].bytes) continue;
|
||||
if (!n || caches[i].bytes > x.big) x.big = caches[i].bytes;
|
||||
if (!n || caches[i].bytes < x.small) x.small = caches[i].bytes;
|
||||
n++;
|
||||
}
|
||||
if (!n) return 0;
|
||||
for (int i = 0; i < ncaches; i++) {
|
||||
if (caches[i].level != 3 || !caches[i].mask || !caches[i].bytes) continue;
|
||||
if (caches[i].bytes == x.big) die |= caches[i].mask;
|
||||
else rest |= caches[i].mask;
|
||||
}
|
||||
out->big = x.big, out->small = x.small;
|
||||
if (x.big == x.small) {
|
||||
out->why = HOS_X3D_SAME;
|
||||
return 0;
|
||||
}
|
||||
if (die & rest) return 0; /* (a processor under two L3 caches: not a list to act on) */
|
||||
die &= allowed;
|
||||
for (int i = 0; i < ncores; i++)
|
||||
if (cores[i] & die) x.cores++;
|
||||
out->die = die, out->cores = x.cores;
|
||||
if (x.cores < HOS_X3D_MIN_CORES) {
|
||||
out->why = HOS_X3D_FEW;
|
||||
return 0;
|
||||
}
|
||||
if (!(allowed & ~die)) {
|
||||
out->why = HOS_X3D_ALL;
|
||||
return 0;
|
||||
}
|
||||
out->why = HOS_X3D_USED;
|
||||
return die;
|
||||
}
|
||||
|
||||
/* The affinity a game thread gets (hos_thread_pin), 0 for none set: the processors the process may use, on the die
|
||||
when there is one (hos_x3d_pick's, 0: none), without the core kept for the replay (hos_reserved_cpus, 0: none).
|
||||
With no die this is what it was before: the process's processors but the reserved core's, where any are left */
|
||||
static inline uint64_t hos_x3d_thread_mask(uint64_t process, uint64_t die, uint64_t reserved) {
|
||||
const uint64_t on = die && (process & die) ? process & die : process;
|
||||
if (on & ~reserved) return reserved || on != process ? on & ~reserved : 0;
|
||||
return on != process ? on : 0;
|
||||
}
|
||||
#endif
|
||||
106
host/hos_zstd.c
Normal file
106
host/hos_zstd.c
Normal file
@@ -0,0 +1,106 @@
|
||||
/* (Triforce, after NX2navi) The host's zstd decoder in place of the game's own, for the files it loads: host/host_zstd.h
|
||||
has what is replaced, the game's functions as their translated code reads, and the rules. Here: the switch and the two
|
||||
calls the lifted code makes at the head of those functions (nx2/oot_patches.py patch_host_zstd).
|
||||
|
||||
HOS_HOST_ZSTD, read once at the first call:
|
||||
unset or 0 the game's own decoder, exactly as before (the calls answer 0 and its code runs; after the first call
|
||||
the lifted code no longer makes them)
|
||||
1 the host decodes what host_zstd.h takes, the game's code the rest
|
||||
2 check: the game's code decodes everything and its results stand; the host decodes beside it and every
|
||||
difference is logged ("zstd check: ... MISMATCH"). The log's "zstd:" lines count what was compared
|
||||
Off by default: nobody has run it in Triforce's game. A wrong answer here damages every file the game loads. */
|
||||
#include "hos.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static SRWLOCK hz_lock = SRWLOCK_INIT;
|
||||
#define HZ_PTR(a) GP(a)
|
||||
#define HZ_OK(a, n) (hos_guest_range((a), (n)) != NULL)
|
||||
#define HZ_WRITTEN(a, n) HOS_WRITTEN(GP(a), (size_t)(n))
|
||||
#define HZ_LOG(...) hos_log(__VA_ARGS__)
|
||||
#define HZ_LOCK() AcquireSRWLockExclusive(&hz_lock)
|
||||
#define HZ_UNLOCK() ReleaseSRWLockExclusive(&hz_lock)
|
||||
#define HZ_AWAY() hos_vcore_away()
|
||||
#define HZ_BACK(x) do { if (x) hos_vcore_back(); } while (0)
|
||||
#define HZ_NOW_NS() hos_now_ns()
|
||||
#include "host_zstd.h"
|
||||
|
||||
#define GAME_MULTIFRAME 0x8010b380ULL /* main+0xeb380: ZSTD_decompressMultiFrame */
|
||||
#define GAME_STREAM 0x80115c80ULL /* main+0xf5c80: the stream feed */
|
||||
|
||||
/* What the lifted code tests before it calls in: not 0 until the switch is read, then 0 with the switch off */
|
||||
volatile int hos_host_zstd = -1;
|
||||
|
||||
static int mode(void) {
|
||||
static volatile LONG m = -1;
|
||||
LONG v = m;
|
||||
if (v < 0) {
|
||||
const char *e = getenv("HOS_HOST_ZSTD");
|
||||
v = e && *e ? atoi(e) : 0;
|
||||
v = v == 1 || v == 2 ? v : 0;
|
||||
/* (only the build the addresses are for: nx2/oot_patches.py puts the calls into no other, and this says so twice) */
|
||||
if (v && strncmp(hos_main_build_id, "e0f3ccfa", 8)) {
|
||||
hos_log("zstd: HOS_HOST_ZSTD is for game build e0f3ccfa, this is %.8s: the game's own decoder", hos_main_build_id);
|
||||
v = 0;
|
||||
}
|
||||
if (v && (!a64_lookup(GAME_MULTIFRAME) || !a64_lookup(GAME_STREAM))) {
|
||||
hos_log("zstd: the game's decoder is not where HOS_HOST_ZSTD expects it: the game's own decoder");
|
||||
v = 0;
|
||||
}
|
||||
if (InterlockedCompareExchange(&m, v, -1) == -1 && v)
|
||||
hos_log(v == 2 ? "zstd: check mode (HOS_HOST_ZSTD=2): the game's decoder decodes, the host's beside it, differences are logged"
|
||||
: "zstd: the game's files are decoded by the host's zstd %s (HOS_HOST_ZSTD=1; =2 checks it against the game's)",
|
||||
ZSTD_versionString());
|
||||
v = m;
|
||||
hos_host_zstd = (int)v;
|
||||
}
|
||||
return (int)v;
|
||||
}
|
||||
|
||||
/* The game's function itself, entered again from its own head: the call in below answers 0 inside it */
|
||||
typedef struct { Ctx *c; uint64_t fn; } GameCall;
|
||||
static _Thread_local int in_game;
|
||||
static uint64_t game_call(void *user) {
|
||||
const GameCall *g = (const GameCall *)user;
|
||||
in_game++;
|
||||
a64_lookup(g->fn)(g->c);
|
||||
in_game--;
|
||||
return g->c->x[0];
|
||||
}
|
||||
|
||||
/* f_8010b380's head: 1 when the call is answered (x0 the result), 0 for the game's code to run */
|
||||
int hos_zstd_multiframe(Ctx *c) {
|
||||
const int m = in_game ? 0 : mode();
|
||||
if (!m) return 0;
|
||||
hz_caller = c->x[30]; /* (the check's lines name it) */
|
||||
/* (Triforce: the output and the input, paid and left alone by the lazy write-back first, rx_backend.c. The decoder's
|
||||
accesses are the CPU's and would fault and pay one by one; this is one payment, before the work) */
|
||||
const int io_dst = HZ_OK(c->x[1], c->x[2]) ? hos_guest_io_begin(GP(c->x[1]), c->x[2]) : 0;
|
||||
const int io_src = HZ_OK(c->x[3], c->x[4]) ? hos_guest_io_begin(GP(c->x[3]), c->x[4]) : 0;
|
||||
int answered = 1;
|
||||
if (m == 2) {
|
||||
GameCall g = {c, GAME_MULTIFRAME};
|
||||
const uint64_t dst = c->x[1], cap = c->x[2], src = c->x[3], size = c->x[4], dict = c->x[5], dict_size = c->x[6], ddict = c->x[7];
|
||||
c->x[0] = hz_oneshot_check(dst, cap, src, size, dict, dict_size, ddict, game_call, &g);
|
||||
} else {
|
||||
uint64_t r = 0;
|
||||
if (hz_oneshot(c->x[1], c->x[2], c->x[3], c->x[4], c->x[5], c->x[6], c->x[7], &r)) c->x[0] = r;
|
||||
else answered = 0;
|
||||
}
|
||||
hos_guest_io_end(io_src);
|
||||
hos_guest_io_end(io_dst);
|
||||
return answered;
|
||||
}
|
||||
|
||||
/* f_80115c80's head: the same */
|
||||
int hos_zstd_stream(Ctx *c) {
|
||||
const int m = in_game ? 0 : mode();
|
||||
if (!m) return 0;
|
||||
hz_caller = c->x[30];
|
||||
GameCall g = {c, GAME_STREAM};
|
||||
uint64_t r = 0;
|
||||
if (!hz_stream(c->x[0], c->x[1], (int32_t)c->x[2], c->x[3], m == 2, game_call, &g, &r)) return 0;
|
||||
c->x[0] = r;
|
||||
return 1;
|
||||
}
|
||||
596
host/host_zstd.h
Normal file
596
host/host_zstd.h
Normal file
@@ -0,0 +1,596 @@
|
||||
/* (Triforce, after NX2navi's runtime/ov_zstd.c) The game's zstd decoder on the host's libzstd (third_party/zstd).
|
||||
|
||||
Most of the game's files are zstd frames (.zs), many with a dictionary from Pack/ZsDic.pack.zs, and the game decodes
|
||||
them with zstd linked into its main module: translated Arm code here. Two of its functions are entered through the
|
||||
host (nx2/oot_patches.py patch_host_zstd puts a call at the head of each; host/hos_zstd.c is the glue and the switch,
|
||||
HOS_HOST_ZSTD, off unless set). Game 1.1.1, build e0f3ccfa, main at 0x80020000:
|
||||
|
||||
f_8010b380 (main+0xeb380), zstd's ZSTD_decompressMultiFrame(dctx, dst, dstCapacity, src, srcSize, dict, dictSize,
|
||||
ddict): one call decodes a whole buffer. Its three callers look at the result as zstd does: an error is a value
|
||||
above (size_t)-120, of which they tell -70 (the output is too small) from the rest. hz_oneshot decodes the same
|
||||
input with libzstd and answers only with a decoded size: whatever libzstd refuses, and whatever is not plainly
|
||||
safe to take (input inside the output, a dictionary that cannot be taken, under 5 bytes of input), goes to the
|
||||
game's own function, which then gives its own answer and its own error code, as today.
|
||||
|
||||
f_80115c80 (main+0xf5c80), the game's stream feed, which decodes most of what the loading threads read. Its decode
|
||||
job (f_80115bf0) calls feed(S, src, int size, ddict) for each chunk it read and looks only at the result: 0 the
|
||||
content is out, bit 31 set an error, anything else go on. The feed drives zstd's bufferless decoder over a state S:
|
||||
S+0x00 u8 phase: 1 begin, 2 running, 3 failed, 4 done
|
||||
S+0x08 u64 where the next output goes S+0x10 i32 output room left S+0x14 u32 content left
|
||||
S+0x18 u64 the DCtx workspace S+0x20 u64, +0x28 i32, +0x2c i32 carry buffer, its size, its fill
|
||||
S+0x30 u64 the DCtx (set at begin)
|
||||
What the translated code does, and hz_stream does for the streams it takes:
|
||||
- phase 1: reads the frame header from the chunk (ZSTD_getFrameHeader); a header not whole in it, an error, or a
|
||||
frame without its content size: phase 3 and 0xffffffff. Else +0x14 = the content size, the DCtx made in the
|
||||
workspace, +0x30 = it, phase 2, and the chunk is decoded as in phase 2.
|
||||
- phase 2: decodes what the input completes, moves +0x08 on and +0x10 and +0x14 down by the bytes written, and
|
||||
returns +0x14. When the frame ends (with its checksum, when it has one) phase 4. Input after the frame's end:
|
||||
phase 3 and 0xfffffffd. A decode error: phase 3 and 0xfffffffe when it is zstd's "output too small" (70),
|
||||
0xffffffff for any other.
|
||||
- phase 3: 0xffffffff. Phase 4: 0xfffffffd. A size of 0 or less in phase 2: +0x14 as it is.
|
||||
hz_stream owns a stream from its begin or not at all: the game's DCtx never sees the blocks a host stream decoded,
|
||||
so there is no going back half way. It takes a stream whose first chunk holds the whole header of a plain frame with
|
||||
its content size, whose output has room for all of that content and does not overlap the chunk, whose workspace is
|
||||
one the game would make its DCtx in, whose dictionary (if any) the host can take, and for which a slot is free. Every other stream, and every feed of a stream it did not
|
||||
begin, is the game's. That leaves the cases whose result depends on the game's decoder itself (an output too small:
|
||||
0xfffffffe part way, with what fits written) to the game. For its own streams it decodes with ZSTD_decompressStream
|
||||
straight into the game's output, one buffer for the whole frame (ZSTD_d_stableOutBuffer: no window of its own); a
|
||||
feed that finds the output anywhere but where the last one left it fails the stream (0xffffffff).
|
||||
The job stops feeding at a result of 0, which a frame gives once its content is out even when its checksum is still
|
||||
to come: such a stream keeps its slot but is marked spent, and a new stream takes a spent slot when none is free.
|
||||
|
||||
Known differences for a damaged frame (never for a sound one): which feed reports the error, and the bytes written
|
||||
before it. The verdict is the same: phase 3 and a result with bit 31 set.
|
||||
|
||||
Check mode (HOS_HOST_ZSTD=2): the game's own code decodes everything as before and its results stand; the host
|
||||
decodes the same input into memory of its own and the two are compared, every difference logged ("zstd check").
|
||||
The way to try the host decoder on a PC before trusting it with =1.
|
||||
|
||||
The includer says how guest memory is reached (host/hos_zstd.c; tests/test_host_zstd.py with a buffer of its own):
|
||||
HZ_PTR(a) host pointer of guest address a
|
||||
HZ_OK(a, n) whether the n guest bytes at a lie in guest memory
|
||||
HZ_WRITTEN(a, n) host code wrote those guest bytes (the write tracking the GPU's uploads read)
|
||||
HZ_LOG(...) a log line
|
||||
HZ_LOCK() / HZ_UNLOCK() one lock for the tables here
|
||||
HZ_AWAY() / HZ_BACK(x) the game's virtual core given up for a long decode, and taken again
|
||||
HZ_NOW_NS() a clock, for the statistics
|
||||
The stream feed is ported from NX2navi's (the owner's lines there); the one-shot path and the dictionary table are
|
||||
written here from their description (another contributor's in NX2navi). */
|
||||
#ifndef HOST_ZSTD_H
|
||||
#define HOST_ZSTD_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define ZSTD_STATIC_LINKING_ONLY
|
||||
#include "../third_party/zstd/lib/zstd.h"
|
||||
#include "../third_party/zstd/lib/zstd_errors.h"
|
||||
|
||||
#define HZ_AWAY_BYTES (64u << 10) /* inputs from this size give the virtual core up while they decode */
|
||||
#define HZ_MAX_DICT (64u << 20)
|
||||
#define HZ_NDICT 32
|
||||
#define HZ_NSTREAM 16
|
||||
#define HZ_NDCTX 8
|
||||
/* The game's DCtx's "bytes expected next" (zstd's dctx->expected, at +28776 of the DCtx in this build): the first thing
|
||||
the game's feed reads of it in phase 2. For a host stream the game's DCtx is never made, and a feed of such a stream
|
||||
must never reach the game's own code; one could only if the stream's slot here were gone while the game still fed it
|
||||
(a spent stream's slot taken by another, and the game feeding on after a result of 0, which its job does not do).
|
||||
So that even that ends in the game's own words and not on a context that was never made, the host's begin sets this
|
||||
field to 0: the game's feed then says "input after the frame's end" (phase 3, 0xfffffffd) for any bytes, and
|
||||
answers the content left for none */
|
||||
#define HZ_DCTX_EXPECTED 28776
|
||||
|
||||
/* The game's function, run by the includer for check mode: its result (x0) */
|
||||
typedef uint64_t (*HzGame)(void *user);
|
||||
|
||||
/* (Triforce, after NX2navi's ov_zstd.c, whose check lines name the caller) The game's return address of the call being
|
||||
answered, set by the includer before it calls in (0: not known): a check's "MISMATCH" line says which caller it was */
|
||||
static _Thread_local uint64_t hz_caller;
|
||||
|
||||
static struct {
|
||||
uint64_t calls, in, out, ns, game, inplace, away, checked, differ; /* one-shot */
|
||||
uint64_t streams, feeds, s_in, s_out, s_ns, s_game, s_checked, s_differ;
|
||||
} hz_stats;
|
||||
|
||||
static inline uint32_t hz_ld32(uint64_t a) { uint32_t v; memcpy(&v, HZ_PTR(a), 4); return v; }
|
||||
static inline uint64_t hz_ld64(uint64_t a) { uint64_t v; memcpy(&v, HZ_PTR(a), 8); return v; }
|
||||
static inline void hz_st32(uint64_t a, uint32_t v) { memcpy(HZ_PTR(a), &v, 4); }
|
||||
static inline void hz_st64(uint64_t a, uint64_t v) { memcpy(HZ_PTR(a), &v, 8); }
|
||||
static inline void hz_add(uint64_t *p, uint64_t v) { __atomic_add_fetch(p, v, __ATOMIC_RELAXED); }
|
||||
/* zstd's own test of a result, for the game's results too (its error codes are the same numbers) */
|
||||
static inline int hz_is_error(uint64_t r) { return r > (uint64_t)0 - 120; }
|
||||
|
||||
/* ---- dictionaries: a guest ZSTD_DDict (zstd's struct: dictBuffer, dictContent, dictSize first; the game's own code
|
||||
reads the content and its size at +8 and +16) or raw dictionary bytes become a host ZSTD_DDict made from a copy of
|
||||
the content, kept for the run under the guest's addresses, the size and a print of the content. Entries are never
|
||||
freed (another thread may be decoding with one); with the table full further dictionaries decode in the game ---- */
|
||||
typedef struct { uint64_t guest, content, size, print; ZSTD_DDict *dd; } HzDict;
|
||||
static HzDict hz_dicts[HZ_NDICT];
|
||||
static int hz_ndicts;
|
||||
|
||||
/* (the size, and 256 bytes from the start, the middle and the end: a new dictionary at an old address differs there,
|
||||
a zstd dictionary's id is in its first 8 bytes) */
|
||||
static uint64_t hz_print(const uint8_t *p, uint64_t n) {
|
||||
uint64_t h = 0xcbf29ce484222325ull ^ n;
|
||||
const uint64_t at[3] = {0, n / 2, n > 256 ? n - 256 : 0};
|
||||
for (int k = 0; k < 3; k++)
|
||||
for (uint64_t i = at[k]; i < at[k] + 256 && i < n; i++) h = (h ^ p[i]) * 0x100000001b3ull;
|
||||
return h;
|
||||
}
|
||||
|
||||
static ZSTD_DDict *hz_dict_find(uint64_t guest, uint64_t content, uint64_t size, uint64_t print) {
|
||||
for (int i = 0; i < hz_ndicts; i++)
|
||||
if (hz_dicts[i].guest == guest && hz_dicts[i].content == content && hz_dicts[i].size == size && hz_dicts[i].print == print)
|
||||
return hz_dicts[i].dd;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The host dictionary for the content at guest address `content` (guest: the ZSTD_DDict it came from, 0 for raw
|
||||
bytes); NULL when it cannot be taken */
|
||||
static ZSTD_DDict *hz_dict_for(uint64_t guest, uint64_t content, uint64_t size) {
|
||||
if (!content || !size || size > HZ_MAX_DICT || !HZ_OK(content, size)) return NULL;
|
||||
const uint64_t print = hz_print((const uint8_t *)HZ_PTR(content), size);
|
||||
HZ_LOCK();
|
||||
ZSTD_DDict *dd = hz_dict_find(guest, content, size, print);
|
||||
const int full = !dd && hz_ndicts >= HZ_NDICT;
|
||||
HZ_UNLOCK();
|
||||
if (dd || full) return dd;
|
||||
ZSTD_DDict *made = ZSTD_createDDict(HZ_PTR(content), (size_t)size); /* (a copy: the game may free or reuse its buffer) */
|
||||
if (!made) return NULL;
|
||||
int slot = -1;
|
||||
HZ_LOCK();
|
||||
dd = hz_dict_find(guest, content, size, print); /* (another thread may have made it meanwhile) */
|
||||
if (!dd && hz_ndicts < HZ_NDICT) {
|
||||
slot = hz_ndicts;
|
||||
hz_dicts[hz_ndicts].guest = guest, hz_dicts[hz_ndicts].content = content, hz_dicts[hz_ndicts].size = size;
|
||||
hz_dicts[hz_ndicts].print = print, hz_dicts[hz_ndicts].dd = made;
|
||||
hz_ndicts++;
|
||||
dd = made;
|
||||
}
|
||||
HZ_UNLOCK();
|
||||
if (dd != made) ZSTD_freeDDict(made);
|
||||
if (!dd) HZ_LOG("zstd: more than %d dictionaries: further ones decode in the game", HZ_NDICT);
|
||||
else if (slot >= 0)
|
||||
HZ_LOG("zstd: host dictionary %d for %s %#llx: %llu bytes at %#llx, id %u", slot, guest ? "the DDict" : "raw bytes at",
|
||||
(unsigned long long)(guest ? guest : content), (unsigned long long)size, (unsigned long long)content,
|
||||
ZSTD_getDictID_fromDDict(dd));
|
||||
return dd;
|
||||
}
|
||||
|
||||
/* (the dictionary a call names: a ZSTD_DDict, else raw bytes, else none. 0 when it names one the host cannot take) */
|
||||
static int hz_dict_of(uint64_t dict, uint64_t dict_size, uint64_t ddict, ZSTD_DDict **dd) {
|
||||
*dd = NULL;
|
||||
if (ddict) {
|
||||
if (!HZ_OK(ddict, 24)) return 0;
|
||||
return (*dd = hz_dict_for(ddict, hz_ld64(ddict + 8), hz_ld64(ddict + 16))) != NULL;
|
||||
}
|
||||
if (dict && dict_size) return (*dd = hz_dict_for(0, dict, dict_size)) != NULL;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ---- one call, one buffer (ZSTD_decompressMultiFrame) ---- */
|
||||
static ZSTD_DCtx *hz_dctx[HZ_NDCTX];
|
||||
static int hz_dctx_busy[HZ_NDCTX];
|
||||
|
||||
/* (a decoder context for one call: one of a few kept, else one made for the call. *slot: which, -1 the call's own) */
|
||||
static ZSTD_DCtx *hz_dctx_take(int *slot) {
|
||||
*slot = -1;
|
||||
HZ_LOCK();
|
||||
for (int i = 0; i < HZ_NDCTX && *slot < 0; i++)
|
||||
if (!hz_dctx_busy[i]) hz_dctx_busy[i] = 1, *slot = i;
|
||||
HZ_UNLOCK();
|
||||
if (*slot < 0) return ZSTD_createDCtx();
|
||||
if (!hz_dctx[*slot]) hz_dctx[*slot] = ZSTD_createDCtx();
|
||||
if (!hz_dctx[*slot]) {
|
||||
HZ_LOCK();
|
||||
hz_dctx_busy[*slot] = 0;
|
||||
HZ_UNLOCK();
|
||||
*slot = -1;
|
||||
return NULL;
|
||||
}
|
||||
return hz_dctx[*slot];
|
||||
}
|
||||
static void hz_dctx_give(ZSTD_DCtx *d, int slot) {
|
||||
if (slot < 0) {
|
||||
ZSTD_freeDCtx(d);
|
||||
return;
|
||||
}
|
||||
HZ_LOCK();
|
||||
hz_dctx_busy[slot] = 0;
|
||||
HZ_UNLOCK();
|
||||
}
|
||||
|
||||
/* Decodes src into out (host memory: the game's output, or check mode's own). libzstd's result; *ok 0 when the inputs
|
||||
cannot be taken (then nothing was written) */
|
||||
static size_t hz_decode(void *out, uint64_t cap, uint64_t src, uint64_t size, uint64_t dict, uint64_t dict_size,
|
||||
uint64_t ddict, int *ok) {
|
||||
ZSTD_DDict *dd = NULL;
|
||||
*ok = 0;
|
||||
if (size < 5 || !src || !HZ_OK(src, size) || !hz_dict_of(dict, dict_size, ddict, &dd)) return 0;
|
||||
int slot;
|
||||
ZSTD_DCtx *d = hz_dctx_take(&slot);
|
||||
if (!d) return 0;
|
||||
const int away = size >= HZ_AWAY_BYTES ? HZ_AWAY() : 0;
|
||||
if (away) hz_add(&hz_stats.away, 1);
|
||||
*ok = 1;
|
||||
const size_t r = dd ? ZSTD_decompress_usingDDict(d, out, (size_t)cap, HZ_PTR(src), (size_t)size, dd)
|
||||
: ZSTD_decompressDCtx(d, out, (size_t)cap, HZ_PTR(src), (size_t)size);
|
||||
HZ_BACK(away);
|
||||
hz_dctx_give(d, slot);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void hz_count(uint64_t in, uint64_t out, uint64_t ns) {
|
||||
const uint64_t n = __atomic_add_fetch(&hz_stats.calls, 1, __ATOMIC_RELAXED);
|
||||
hz_add(&hz_stats.in, in), hz_add(&hz_stats.out, out), hz_add(&hz_stats.ns, ns);
|
||||
if (n == 1 || !(n & 0x3fff))
|
||||
HZ_LOG("zstd: %llu calls decoded on the host, %.1f MB in, %.1f MB out, %.0f ms; %llu left to the game's decoder (%llu of "
|
||||
"them in place), %llu with the core given up; check: %llu compared, %llu differ", (unsigned long long)n,
|
||||
hz_stats.in / 1048576.0, hz_stats.out / 1048576.0, hz_stats.ns / 1e6, (unsigned long long)hz_stats.game,
|
||||
(unsigned long long)hz_stats.inplace, (unsigned long long)hz_stats.away, (unsigned long long)hz_stats.checked,
|
||||
(unsigned long long)hz_stats.differ);
|
||||
}
|
||||
|
||||
/* (the input inside the output: the result depends on how the decoder's writes overtake its reads, which libzstd does
|
||||
not promise to do as the game's version does, and a host decode that failed half way would leave the game's decoder
|
||||
a damaged input) */
|
||||
static int hz_in_place(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size) {
|
||||
return cap && src < dst + cap && dst < src + size;
|
||||
}
|
||||
|
||||
/* A call of the game's ZSTD_decompressMultiFrame. 1: decoded here, *ret its result (the decoded size). 0: the game's
|
||||
own function is to run, with its arguments as they are */
|
||||
static int hz_oneshot(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size, uint64_t dict, uint64_t dict_size,
|
||||
uint64_t ddict, uint64_t *ret) {
|
||||
if (hz_in_place(dst, cap, src, size)) {
|
||||
hz_add(&hz_stats.inplace, 1), hz_add(&hz_stats.game, 1);
|
||||
return 0;
|
||||
}
|
||||
int ok = 0;
|
||||
const uint64_t t0 = (uint64_t)HZ_NOW_NS();
|
||||
const size_t r = (cap && !dst) || !HZ_OK(dst, cap) ? 0 : hz_decode(HZ_PTR(dst), cap, src, size, dict, dict_size, ddict, &ok);
|
||||
if (!ok || ZSTD_isError(r)) { /* (the game's decoder decides, and gives its own error code) */
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 8)
|
||||
HZ_LOG("zstd: %llu bytes at %#llx go to the game's decoder: %s", (unsigned long long)size, (unsigned long long)src,
|
||||
!ok ? "inputs not taken" : ZSTD_getErrorName(r));
|
||||
hz_add(&hz_stats.game, 1);
|
||||
return 0;
|
||||
}
|
||||
if (r) HZ_WRITTEN(dst, r);
|
||||
*ret = r;
|
||||
hz_count(size, r, (uint64_t)HZ_NOW_NS() - t0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* The same call in check mode: the game's function runs and its result stands; the host decodes into memory of its
|
||||
own first, and the two are compared */
|
||||
static uint64_t hz_oneshot_check(uint64_t dst, uint64_t cap, uint64_t src, uint64_t size, uint64_t dict, uint64_t dict_size,
|
||||
uint64_t ddict, HzGame game, void *user) {
|
||||
if (hz_in_place(dst, cap, src, size)) {
|
||||
hz_add(&hz_stats.inplace, 1), hz_add(&hz_stats.game, 1);
|
||||
return game(user);
|
||||
}
|
||||
uint8_t *mine = cap ? (uint8_t *)malloc((size_t)cap) : NULL;
|
||||
int ok = 0;
|
||||
const uint64_t t0 = (uint64_t)HZ_NOW_NS();
|
||||
const size_t r = cap && !mine ? 0 : hz_decode(mine, cap, src, size, dict, dict_size, ddict, &ok);
|
||||
const uint64_t ns = (uint64_t)HZ_NOW_NS() - t0;
|
||||
const uint64_t g = game(user);
|
||||
const int host_ok = ok && !ZSTD_isError(r);
|
||||
if (!host_ok) { /* (outside check mode this call would have gone to the game: nothing to compare) */
|
||||
hz_add(&hz_stats.game, 1);
|
||||
static int logged;
|
||||
if (!hz_is_error(g) && __atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 8)
|
||||
HZ_LOG("zstd check: %llu bytes at %#llx the game decoded (%llu bytes) and the host would have left to it: %s",
|
||||
(unsigned long long)size, (unsigned long long)src, (unsigned long long)g,
|
||||
!ok ? "inputs not taken" : ZSTD_getErrorName(r));
|
||||
} else {
|
||||
hz_add(&hz_stats.checked, 1);
|
||||
if (g != r || (r && (!HZ_OK(dst, r) || memcmp(mine, HZ_PTR(dst), r)))) {
|
||||
hz_add(&hz_stats.differ, 1);
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 12) {
|
||||
/* (Triforce) and what the input's first frame says of itself: its size in the input and its content's
|
||||
(-1: not in its header, -2: no frame there), which tell one frame from several and a short output
|
||||
from other bytes */
|
||||
const size_t first = ZSTD_findFrameCompressedSize(HZ_PTR(src), (size_t)size);
|
||||
const unsigned long long content = ZSTD_getFrameContentSize(HZ_PTR(src), (size_t)size);
|
||||
HZ_LOG("zstd check: MISMATCH: %llu bytes at %#llx (room %llu, dict %#llx/%llu, ddict %#llx; its first frame %lld "
|
||||
"bytes, content %lld; called from %#llx): the host decoded %llu bytes, the game answered %#llx%s",
|
||||
(unsigned long long)size, (unsigned long long)src, (unsigned long long)cap, (unsigned long long)dict,
|
||||
(unsigned long long)dict_size, (unsigned long long)ddict, ZSTD_isError(first) ? -1ll : (long long)first,
|
||||
(long long)content, (unsigned long long)hz_caller, (unsigned long long)r, (unsigned long long)g,
|
||||
g == r ? " (the same size, other bytes)" : "");
|
||||
}
|
||||
}
|
||||
}
|
||||
free(mine);
|
||||
hz_count(size, host_ok ? r : 0, ns);
|
||||
return g;
|
||||
}
|
||||
|
||||
/* ---- the stream feed ---- */
|
||||
typedef struct {
|
||||
uint64_t s; /* the game's stream state's address; 0: free */
|
||||
ZSTD_DCtx *dctx; /* stays with the slot from stream to stream */
|
||||
uint8_t *shadow; /* check mode: the host's output */
|
||||
uint64_t content, pos; /* the frame's size; bytes the host wrote */
|
||||
uint64_t dst0, cap0; /* the game's output at the begin: every feed writes on into this one buffer */
|
||||
uint64_t in; /* compressed bytes fed */
|
||||
uint64_t ddict; /* the game's ZSTD_DDict the stream began with (0: none), for the check's lines */
|
||||
int spent; /* all the content is out: the game feeds no more, and the slot may be taken */
|
||||
int done, bad, moved; /* check mode: the host reached the frame's end / failed; the game's output moved */
|
||||
} HzStream;
|
||||
static HzStream hz_streams[HZ_NSTREAM];
|
||||
|
||||
static HzStream *hz_stream_find(uint64_t s) {
|
||||
HzStream *st = NULL;
|
||||
HZ_LOCK();
|
||||
for (int i = 0; i < HZ_NSTREAM && !st; i++)
|
||||
if (hz_streams[i].s == s) st = &hz_streams[i];
|
||||
HZ_UNLOCK();
|
||||
return st;
|
||||
}
|
||||
|
||||
/* The slot of s, or a free one for it, or one whose stream is spent; NULL when there is none (that stream decodes in
|
||||
the game) */
|
||||
static HzStream *hz_stream_take(uint64_t s) {
|
||||
HzStream *st = NULL;
|
||||
HZ_LOCK();
|
||||
for (int i = 0; i < HZ_NSTREAM && !st; i++)
|
||||
if (hz_streams[i].s == s) st = &hz_streams[i];
|
||||
for (int i = 0; i < HZ_NSTREAM && !st; i++)
|
||||
if (!hz_streams[i].s) st = &hz_streams[i];
|
||||
for (int i = 0; i < HZ_NSTREAM && !st; i++)
|
||||
if (hz_streams[i].spent) st = &hz_streams[i];
|
||||
if (st) st->s = s, st->spent = 0;
|
||||
HZ_UNLOCK();
|
||||
return st;
|
||||
}
|
||||
|
||||
static void hz_stream_drop(HzStream *st) {
|
||||
if (!st) return;
|
||||
free(st->shadow);
|
||||
st->shadow = NULL;
|
||||
HZ_LOCK();
|
||||
st->s = 0, st->spent = 0;
|
||||
HZ_UNLOCK();
|
||||
}
|
||||
|
||||
/* Drops the slot of stream state s, if it has one. Found and cleared under one hold of the lock: between a find and a
|
||||
drop another thread can take a spent slot for its own stream (hz_stream_take), and the drop then cleared that
|
||||
thread's slot, which a third thread took as free and reset the decoder context in use */
|
||||
static void hz_stream_drop_of(uint64_t s) {
|
||||
HZ_LOCK();
|
||||
for (int i = 0; i < HZ_NSTREAM; i++)
|
||||
if (hz_streams[i].s == s) {
|
||||
free(hz_streams[i].shadow);
|
||||
hz_streams[i].shadow = NULL;
|
||||
hz_streams[i].s = 0, hz_streams[i].spent = 0;
|
||||
}
|
||||
HZ_UNLOCK();
|
||||
}
|
||||
|
||||
static void hz_stream_spent(HzStream *st) {
|
||||
HZ_LOCK();
|
||||
st->spent = 1;
|
||||
HZ_UNLOCK();
|
||||
}
|
||||
|
||||
/* A context for streams: the output is one buffer for the whole frame, so the decoder allocates no window (a window
|
||||
of its own would be as large as the frame's, and a frame may name one as large as its content), and it takes any
|
||||
window size, as the game's decoder does */
|
||||
static ZSTD_DCtx *hz_stream_dctx(void) {
|
||||
ZSTD_DCtx *d = ZSTD_createDCtx();
|
||||
if (d && (ZSTD_isError(ZSTD_DCtx_setParameter(d, ZSTD_d_stableOutBuffer, 1)) ||
|
||||
ZSTD_isError(ZSTD_DCtx_setParameter(d, ZSTD_d_windowLogMax, ZSTD_WINDOWLOG_MAX)))) {
|
||||
ZSTD_freeDCtx(d);
|
||||
d = NULL;
|
||||
}
|
||||
return d;
|
||||
}
|
||||
|
||||
/* Feeds in to the stream's context until the input is used up, the frame ends (0), an error, or nothing moves. With
|
||||
the virtual core given up for large chunks, as hz_decode */
|
||||
static size_t hz_stream_run(HzStream *st, ZSTD_outBuffer *out, ZSTD_inBuffer *in) {
|
||||
const int away = in->size >= HZ_AWAY_BYTES ? HZ_AWAY() : 0;
|
||||
if (away) hz_add(&hz_stats.away, 1);
|
||||
size_t r = 1;
|
||||
while (in->pos < in->size) {
|
||||
const size_t i0 = in->pos, o0 = out->pos;
|
||||
r = ZSTD_decompressStream(st->dctx, out, in);
|
||||
if (ZSTD_isError(r) || !r || (in->pos == i0 && out->pos == o0)) break;
|
||||
}
|
||||
HZ_BACK(away);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void hz_stream_count(const HzStream *st, uint64_t out) {
|
||||
const uint64_t n = __atomic_add_fetch(&hz_stats.streams, 1, __ATOMIC_RELAXED);
|
||||
hz_add(&hz_stats.s_in, st->in), hz_add(&hz_stats.s_out, out);
|
||||
if (n == 1 || !(n & 0x3ff))
|
||||
HZ_LOG("zstd: %llu streams decoded on the host in %llu feeds, %.1f MB in, %.1f MB out, %.0f ms; %llu streams left to the "
|
||||
"game's decoder; check: %llu compared, %llu differ", (unsigned long long)n, (unsigned long long)hz_stats.feeds,
|
||||
hz_stats.s_in / 1048576.0, hz_stats.s_out / 1048576.0, hz_stats.s_ns / 1e6, (unsigned long long)hz_stats.s_game,
|
||||
(unsigned long long)hz_stats.s_checked, (unsigned long long)hz_stats.s_differ);
|
||||
}
|
||||
|
||||
/* A stream at its begin (phase 1): its slot, the host context ready for the frame, or NULL when the game's feed is to
|
||||
decode it (see the top). check: the game's state is left alone, and the host's output is memory of its own */
|
||||
static HzStream *hz_stream_begin(uint64_t s, uint64_t src, int32_t size, uint64_t ddict, int check) {
|
||||
if (size <= 0 || !src || !HZ_OK(src, (uint64_t)size)) return NULL;
|
||||
ZSTD_FrameHeader h;
|
||||
if (ZSTD_getFrameHeader(&h, HZ_PTR(src), (size_t)size) != 0 || h.frameType != ZSTD_frame || !h.frameContentSize ||
|
||||
h.frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN || h.frameContentSize > 0x7fffffffull)
|
||||
return NULL;
|
||||
const uint64_t dst = hz_ld64(s + 8), ws = hz_ld64(s + 0x18);
|
||||
const int32_t cap = (int32_t)hz_ld32(s + 0x10);
|
||||
/* an output without room for the content fails part way in the game, with what fits written: left to the game */
|
||||
if (cap < 0 || (uint64_t)cap < h.frameContentSize || !dst || !HZ_OK(dst, (uint64_t)cap)) return NULL;
|
||||
if (src < dst + (uint64_t)cap && dst < src + (uint64_t)size) return NULL; /* in place: the game's decoder */
|
||||
/* (the game makes its DCtx there, and none in a workspace not aligned to 8) */
|
||||
if (!ws || (ws & 7) || !HZ_OK(ws, HZ_DCTX_EXPECTED + 8)) return NULL;
|
||||
ZSTD_DDict *dd = NULL;
|
||||
if (!hz_dict_of(0, 0, ddict, &dd)) return NULL;
|
||||
HzStream *st = hz_stream_take(s);
|
||||
if (!st) return NULL;
|
||||
free(st->shadow);
|
||||
st->shadow = NULL, st->content = h.frameContentSize, st->pos = 0, st->dst0 = dst, st->cap0 = (uint64_t)cap;
|
||||
st->in = 0, st->ddict = ddict, st->done = st->bad = st->moved = 0;
|
||||
if ((!st->dctx && !(st->dctx = hz_stream_dctx())) ||
|
||||
ZSTD_isError(ZSTD_DCtx_reset(st->dctx, ZSTD_reset_session_only)) ||
|
||||
ZSTD_isError(ZSTD_DCtx_refDDict(st->dctx, dd)) ||
|
||||
(check && !(st->shadow = (uint8_t *)malloc((size_t)st->content)))) {
|
||||
hz_stream_drop(st);
|
||||
return NULL;
|
||||
}
|
||||
if (!check) { /* what the game's begin leaves in its state */
|
||||
hz_st32(s + 0x14, (uint32_t)st->content);
|
||||
hz_st64(s + 0x30, ws);
|
||||
*(uint8_t *)HZ_PTR(s) = 2;
|
||||
HZ_WRITTEN(s, 0x38);
|
||||
hz_st64(ws + HZ_DCTX_EXPECTED, 0); /* (see HZ_DCTX_EXPECTED) */
|
||||
HZ_WRITTEN(ws + HZ_DCTX_EXPECTED, 8);
|
||||
}
|
||||
return st;
|
||||
}
|
||||
|
||||
/* A feed of a host stream: the game's fields and the result as the game's feed leaves them */
|
||||
static uint64_t hz_stream_feed(HzStream *st, uint64_t s, uint64_t src, int32_t size) {
|
||||
if (size <= 0) return hz_ld32(s + 0x14); /* nothing to feed: the content left, as the game */
|
||||
const uint64_t t0 = (uint64_t)HZ_NOW_NS(), dst = hz_ld64(s + 8);
|
||||
const uint32_t cap = hz_ld32(s + 0x10);
|
||||
uint32_t ret;
|
||||
if (!src || !HZ_OK(src, (uint64_t)size) || dst != st->dst0 + st->pos || cap != st->cap0 - st->pos) {
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 8)
|
||||
HZ_LOG("zstd: a host stream cannot go on after %llu bytes out: input %#llx + %d, output at %#llx with room for %u "
|
||||
"where %#llx with %llu was left", (unsigned long long)st->pos, (unsigned long long)src, size,
|
||||
(unsigned long long)dst, cap, (unsigned long long)(st->dst0 + st->pos), (unsigned long long)(st->cap0 - st->pos));
|
||||
*(uint8_t *)HZ_PTR(s) = 3;
|
||||
HZ_WRITTEN(s, 1);
|
||||
hz_stream_drop(st);
|
||||
return 0xffffffffu;
|
||||
}
|
||||
ZSTD_inBuffer in = {HZ_PTR(src), (size_t)size, 0};
|
||||
ZSTD_outBuffer out = {HZ_PTR(st->dst0), (size_t)st->cap0, (size_t)st->pos};
|
||||
const size_t r = hz_stream_run(st, &out, &in);
|
||||
const uint32_t wrote = (uint32_t)(out.pos - st->pos);
|
||||
st->pos = out.pos, st->in += in.pos;
|
||||
hz_st64(s + 8, dst + wrote);
|
||||
hz_st32(s + 0x10, cap - wrote);
|
||||
hz_st32(s + 0x14, hz_ld32(s + 0x14) - wrote);
|
||||
if (wrote) HZ_WRITTEN(dst, wrote);
|
||||
hz_add(&hz_stats.feeds, 1);
|
||||
hz_add(&hz_stats.s_ns, (uint64_t)HZ_NOW_NS() - t0);
|
||||
int end = 1;
|
||||
if (ZSTD_isError(r)) {
|
||||
*(uint8_t *)HZ_PTR(s) = 3;
|
||||
ret = ZSTD_getErrorCode(r) == ZSTD_error_dstSize_tooSmall ? 0xfffffffeu : 0xffffffffu;
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 8)
|
||||
HZ_LOG("zstd: a host stream failed after %llu bytes in: %s", (unsigned long long)st->in, ZSTD_getErrorName(r));
|
||||
} else if (!r && in.pos < in.size) { /* input after the frame's end */
|
||||
*(uint8_t *)HZ_PTR(s) = 3;
|
||||
ret = 0xfffffffdu;
|
||||
} else if (!r) {
|
||||
*(uint8_t *)HZ_PTR(s) = 4;
|
||||
ret = hz_ld32(s + 0x14);
|
||||
} else if (in.pos < in.size) { /* nothing moved with input left */
|
||||
*(uint8_t *)HZ_PTR(s) = 3;
|
||||
ret = 0xfffffffeu;
|
||||
} else {
|
||||
ret = hz_ld32(s + 0x14);
|
||||
end = 0;
|
||||
if (!ret) hz_stream_spent(st);
|
||||
}
|
||||
HZ_WRITTEN(s, 0x38);
|
||||
if (end) {
|
||||
hz_stream_count(st, st->pos);
|
||||
hz_stream_drop(st);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Check mode: the host decodes the chunk into its own buffer, the game's feed runs as always and its result stands;
|
||||
when the game ends the stream, or has all its content out, the two outputs are compared */
|
||||
static uint64_t hz_stream_check(HzStream *st, uint64_t s, uint64_t src, int32_t size, HzGame game, void *user) {
|
||||
if (st->spent) { /* compared already: the game was not expected to feed on after a result of 0 */
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 4)
|
||||
HZ_LOG("zstd check: a feed of %d bytes after the content of a stream was out (content %llu)", size,
|
||||
(unsigned long long)st->content);
|
||||
const uint64_t g = game(user);
|
||||
const uint8_t ph = *(const uint8_t *)HZ_PTR(s);
|
||||
if (ph == 3 || ph == 4) hz_stream_drop(st);
|
||||
return g;
|
||||
}
|
||||
if (!st->bad && !st->done && size > 0 && src && HZ_OK(src, (uint64_t)size)) {
|
||||
ZSTD_inBuffer in = {HZ_PTR(src), (size_t)size, 0};
|
||||
ZSTD_outBuffer out = {st->shadow, (size_t)st->content, (size_t)st->pos};
|
||||
const uint64_t t0 = (uint64_t)HZ_NOW_NS();
|
||||
const size_t r = hz_stream_run(st, &out, &in);
|
||||
hz_add(&hz_stats.feeds, 1);
|
||||
hz_add(&hz_stats.s_ns, (uint64_t)HZ_NOW_NS() - t0);
|
||||
st->pos = out.pos, st->in += in.pos;
|
||||
if (ZSTD_isError(r)) st->bad = 1;
|
||||
else if (!r) st->done = in.pos == in.size ? 1 : 2; /* 2: input after the frame's end */
|
||||
else if (in.pos < in.size) st->bad = 1;
|
||||
}
|
||||
const uint32_t g = (uint32_t)game(user);
|
||||
const uint8_t ph = *(const uint8_t *)HZ_PTR(s);
|
||||
/* a host stream writes into the buffer the stream began with: the game's output has to advance through it alone */
|
||||
if ((ph == 2 || ph == 4) && hz_ld64(s + 8) - st->dst0 != st->content - hz_ld32(s + 0x14)) st->moved = 1;
|
||||
const int out_all = ph == 2 && !g; /* the job feeds no more, with or without the frame's last bytes */
|
||||
if (ph != 3 && ph != 4 && !out_all) return g;
|
||||
hz_add(&hz_stats.s_checked, 1);
|
||||
const int same = ph == 3 ? st->bad || st->done != 1 /* the game failed: the host must not have a clean frame */
|
||||
: !st->bad && !st->moved && st->done != 2 && (ph != 4 || st->done == 1) &&
|
||||
st->pos == st->content && HZ_OK(st->dst0, st->content) &&
|
||||
!memcmp(st->shadow, HZ_PTR(st->dst0), (size_t)st->content);
|
||||
if (!same) {
|
||||
hz_add(&hz_stats.s_differ, 1);
|
||||
static int logged;
|
||||
if (__atomic_fetch_add(&logged, 1, __ATOMIC_RELAXED) < 12)
|
||||
HZ_LOG("zstd check: stream MISMATCH: the game ended in phase %d with %#x after %llu bytes in (content %llu, ddict %#llx, "
|
||||
"called from %#llx)%s; the host wrote %llu bytes, %s", (int)ph, g, (unsigned long long)st->in,
|
||||
(unsigned long long)st->content, (unsigned long long)st->ddict, (unsigned long long)hz_caller,
|
||||
st->moved ? ", its output not advancing through one buffer" : "", (unsigned long long)st->pos,
|
||||
st->bad ? "failed" : st->done == 1 ? "reached the frame's end"
|
||||
: st->done ? "found input after the frame's end" : "had not reached the frame's end");
|
||||
}
|
||||
hz_stream_count(st, st->pos);
|
||||
if (out_all) { /* keeps the slot in case the game does feed on; nothing more is compared */
|
||||
free(st->shadow);
|
||||
st->shadow = NULL;
|
||||
hz_stream_spent(st);
|
||||
} else {
|
||||
hz_stream_drop(st);
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
/* A call of the game's stream feed. 1: fed here, *ret its result. 0: the game's own feed is to run. check (with the
|
||||
game's function to run): always 1, the game's result */
|
||||
static int hz_stream(uint64_t s, uint64_t src, int32_t size, uint64_t ddict, int check, HzGame game, void *user, uint64_t *ret) {
|
||||
HzStream *st = NULL;
|
||||
if (s && HZ_OK(s, 0x38)) {
|
||||
const uint8_t ph = *(const uint8_t *)HZ_PTR(s);
|
||||
if (ph == 1) {
|
||||
hz_stream_drop_of(s); /* a stream the game gave up without ending it */
|
||||
st = hz_stream_begin(s, src, size, ddict, check);
|
||||
if (!st) hz_add(&hz_stats.s_game, 1);
|
||||
} else if (ph == 2) {
|
||||
st = hz_stream_find(s);
|
||||
}
|
||||
}
|
||||
if (check) {
|
||||
*ret = st ? hz_stream_check(st, s, src, size, game, user) : game(user);
|
||||
return 1;
|
||||
}
|
||||
if (!st) return 0;
|
||||
*ret = hz_stream_feed(st, s, src, size);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
281
host/menu_sound.c
Normal file
281
host/menu_sound.c
Normal file
@@ -0,0 +1,281 @@
|
||||
/* (Triforce) The options' sounds: the game's own interface sounds (Sound/Resource/Stream/SE_Sys_*.bwav of the player's
|
||||
game files: the cursor, decide, cancel, the Plus menu's open and close, the tab moves), read once, decoded and mixed
|
||||
over the game's output. After NX2navi's menu sounds (its runtime/menu_sound.c, the owner's): the decoder, the voices
|
||||
and the mix are that file's. Nothing is shipped: game files without the sounds stay silent, and a mod's file is
|
||||
played where a mod replaces one (hos_fs_rom_file, nn_fs.c).
|
||||
|
||||
BWAV: "BWAV", byte order mark, version, CRC32, a prefetch flag (u16) and the channel count (u16), then 0x4C bytes per
|
||||
channel: codec (u16, 0 PCM16, 1 DSP-ADPCM), pan (u16, 0 left, 1 right, 2 middle), sample rate, sample count (u32),
|
||||
the count again, 16 DSP coefficients (s16), the absolute offset of the samples (u32), ... The interface sounds are
|
||||
48 kHz DSP-ADPCM, one channel in the middle or two, left and right: 8-byte frames of a header (coefficient pair
|
||||
index, scale exponent) and 14 four-bit samples. A file of another rate (a mod's) is brought to 48 kHz when it is
|
||||
read, in a straight line between its samples: the mixer's rate is the output's (nn_audio.c RATE), always.
|
||||
|
||||
Which sound: the options say (gpu/vk/vk_menu.cpp, gpu/vk/menu_sound_events.h), and the sound thread asks them at each
|
||||
block it mixes, 5 ms (hos_menu_sound_frame: rx_option HOS_OPT_MENU_SOUND). So a sound is in the block after its
|
||||
press, and what is heard later than that is the output's own delay, the game's sounds' too. The files are read on a
|
||||
thread of their own, when the sound thread first finds the Menu sounds row above 0; a sound asked for before they
|
||||
are read is not played.
|
||||
|
||||
hos_menu_sound_play starts a voice (a free one, else the oldest), hos_menu_sound_mix adds the voices to a block. A
|
||||
voice is free while its place is negative; play sets its sound first and its place last (release), the mix reads
|
||||
them in the other order (acquire) and moves the place on only if it is still the one it read, so the two may run on
|
||||
different threads without a lock. The place is the frame with the number of the voice's start above it: NX2navi's
|
||||
was the frame alone, and a voice started again while its first block was mixed had the frame it had before, 0, so
|
||||
the mix moved the new sound on by a block it never played (or ended one shorter than a block unheard). Here both
|
||||
run on the sound thread, one after the other.
|
||||
|
||||
The level: the Audio tab's Menu sounds row (menu_sounds= in triforce_options.ini, 0 to 100, 80 unless set; 0
|
||||
silent), read at each block, and the Master volume over it as over the game's own sound (the sounds are added before
|
||||
render_frame applies it). They are added after the game's limiter, and the block is clipped as it is made 16-bit. */
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "menu_sound.h"
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_MENU_SOUND_*, HOS_OPT_MENU_SOUND, HOS_OPT_MENU_SOUND_VOLUME) */
|
||||
|
||||
#ifndef MENU_SOUND_TEST /* (tests/menu_sound_test.c has its own of these, and reads the files when it says) */
|
||||
#include "hos.h"
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
int rx_option(int id, int otherwise); /* rx_backend.c */
|
||||
uint8_t *hos_fs_rom_file(const char *rel, uint64_t most, uint64_t *size); /* nn_fs.c */
|
||||
static void load_all(void);
|
||||
static DWORD WINAPI load_main(void *arg) {
|
||||
(void)arg;
|
||||
load_all();
|
||||
return 0;
|
||||
}
|
||||
static void load_start(void) { /* (the files are read off the sound thread: a disk may take its time) */
|
||||
const HANDLE t = CreateThread(NULL, 0, load_main, NULL, 0, NULL);
|
||||
if (t) CloseHandle(t);
|
||||
else hos_log("menu sounds: no thread to read the game's interface sounds: the options stay silent");
|
||||
}
|
||||
#endif
|
||||
|
||||
#define MIX_RATE 48000 /* (nn_audio.c RATE: the output's) */
|
||||
#define SOUNDS HOS_MENU_SOUND_N /* (by HOS_MENU_SOUND_*: 0 is no sound) */
|
||||
#define FILE_MOST (4u << 20) /* (the longest of the game's seven is 138 KB) */
|
||||
|
||||
typedef struct {
|
||||
float *pcm; /* interleaved stereo at 48 kHz */
|
||||
uint32_t frames;
|
||||
} Sound;
|
||||
|
||||
static const char *const files[SOUNDS] = {
|
||||
[HOS_MENU_SOUND_OPEN] = "Sound/Resource/Stream/SE_Sys_PlusMenu_Open00.bwav",
|
||||
[HOS_MENU_SOUND_CLOSE] = "Sound/Resource/Stream/SE_Sys_PlusMenu_Close00.bwav",
|
||||
[HOS_MENU_SOUND_CURSOR] = "Sound/Resource/Stream/SE_Sys_Cursor00.bwav",
|
||||
[HOS_MENU_SOUND_DECIDE] = "Sound/Resource/Stream/SE_Sys_Do00.bwav",
|
||||
[HOS_MENU_SOUND_CANCEL] = "Sound/Resource/Stream/SE_Sys_Cancel00.bwav",
|
||||
[HOS_MENU_SOUND_TAB_LEFT] = "Sound/Resource/Stream/SE_Sys_MinusMenu_TabMove_L00.bwav",
|
||||
[HOS_MENU_SOUND_TAB_RIGHT] = "Sound/Resource/Stream/SE_Sys_MinusMenu_TabMove_R00.bwav",
|
||||
/* ((Triforce) the Mods screen's install done: the game's own item-get fanfare, its short da-na-na-naaa (4 s; _Intro is the
|
||||
whole chest's opening before it)) */
|
||||
[HOS_MENU_SOUND_FANFARE] = "Sound/Resource/Stream/BGM_ItemGet_Medium.bwav",
|
||||
/* ((Triforce) 11-17) the Mods entry under the title's list and the System page: the sounds those lists make (the lab's
|
||||
file opens: the title's cursor, its Continue and the Load list's choice) */
|
||||
[HOS_MENU_SOUND_SYS_CURSOR] = "Sound/Resource/Stream/SE_Sys_SysMenu_Cursor00.bwav",
|
||||
[HOS_MENU_SOUND_SYS_DECIDE] = "Sound/Resource/Stream/SE_Sys_SysMenu_DecideL00.bwav",
|
||||
};
|
||||
|
||||
static Sound sounds[SOUNDS];
|
||||
static atomic_int loaded; /* 0 not yet, 1 being read (sounds[] is written before the release store of 2), 2 ready */
|
||||
|
||||
#define VOICES 8
|
||||
static struct {
|
||||
_Atomic int64_t at; /* the number of its start << 32 | the frame; -1 free */
|
||||
atomic_int sound;
|
||||
uint64_t started;
|
||||
} voices[VOICES];
|
||||
static uint64_t started_n;
|
||||
#define AT_FRAME(at) ((uint32_t)(at))
|
||||
#define AT_START(at) ((at) & ~(int64_t)0xffffffff)
|
||||
|
||||
static uint16_t u16(const uint8_t *p) { return (uint16_t)(p[0] | p[1] << 8); }
|
||||
static uint32_t u32(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
|
||||
|
||||
static int16_t clamp16(int64_t v) { return (int16_t)(v < -32768 ? -32768 : v > 32767 ? 32767 : v); }
|
||||
|
||||
/* One channel into dst[2 * i] and dst[2 * i + 1] by its pan (0 the left alone, 1 the right alone, else both). 0 when
|
||||
the samples do not lie inside the file. */
|
||||
static int decode_channel(const uint8_t *b, uint64_t size, const uint8_t *info, float *dst, uint32_t frames) {
|
||||
const uint16_t codec = u16(info), pan = u16(info + 2);
|
||||
uint32_t count = u32(info + 8);
|
||||
const uint32_t off = u32(info + 0x30);
|
||||
if (count > frames) count = frames;
|
||||
int16_t coef[16];
|
||||
for (int i = 0; i < 16; i++) coef[i] = (int16_t)u16(info + 0x10 + 2 * i);
|
||||
const float scale = 1.0f / 32768.0f;
|
||||
if (codec == 0) { /* PCM16 */
|
||||
if ((uint64_t)off + (uint64_t)count * 2 > size) return 0;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
const float v = (int16_t)u16(b + off + 2 * (uint64_t)i) * scale;
|
||||
if (pan != 1) dst[2 * (size_t)i] += v;
|
||||
if (pan != 0) dst[2 * (size_t)i + 1] += v;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if (codec != 1) return 0;
|
||||
const uint64_t need = ((uint64_t)count + 13) / 14 * 8;
|
||||
if ((uint64_t)off + need > size) return 0;
|
||||
int32_t h1 = 0, h2 = 0;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
const uint8_t *fr = b + off + (uint64_t)(i / 14) * 8;
|
||||
const uint32_t k = i % 14;
|
||||
const int idx = fr[0] >> 4 & 7, sh = fr[0] & 15;
|
||||
int nib = fr[1 + k / 2] >> (k & 1 ? 0 : 4) & 15;
|
||||
if (nib >= 8) nib -= 16;
|
||||
/* (64 bits: coefficients a file made up could carry the sum past 32) */
|
||||
const int64_t s = (int64_t)nib * (1 << sh) * 2048 + (int64_t)coef[2 * idx] * h1 + (int64_t)coef[2 * idx + 1] * h2;
|
||||
const int16_t v = clamp16((s + 1024) >> 11);
|
||||
h2 = h1, h1 = v;
|
||||
if (pan != 1) dst[2 * (size_t)i] += v * scale;
|
||||
if (pan != 0) dst[2 * (size_t)i + 1] += v * scale;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* pcm (*frames stereo frames at `rate`) at 48 kHz, in a straight line between its samples; pcm is freed */
|
||||
static float *to_mix_rate(float *pcm, uint32_t *frames, uint32_t rate) {
|
||||
const uint64_t n = ((uint64_t)*frames * MIX_RATE + rate - 1) / rate;
|
||||
float *out = (float *)malloc((size_t)n * 2 * sizeof *out);
|
||||
for (uint64_t i = 0; out && i < n; i++) {
|
||||
const uint64_t at = i * rate; /* (in 48000ths of a frame of pcm) */
|
||||
const uint32_t k = (uint32_t)(at / MIX_RATE), k1 = k + 1 < *frames ? k + 1 : k;
|
||||
const float t = (float)(at % MIX_RATE) / MIX_RATE;
|
||||
out[2 * i] = pcm[2 * (size_t)k] + (pcm[2 * (size_t)k1] - pcm[2 * (size_t)k]) * t;
|
||||
out[2 * i + 1] = pcm[2 * (size_t)k + 1] + (pcm[2 * (size_t)k1 + 1] - pcm[2 * (size_t)k + 1]) * t;
|
||||
}
|
||||
free(pcm);
|
||||
if (out) *frames = (uint32_t)n;
|
||||
return out;
|
||||
}
|
||||
|
||||
float *hos_menu_sound_decode(const uint8_t *b, uint64_t size, uint32_t *frames_out) {
|
||||
if (size < 0x10 || memcmp(b, "BWAV", 4)) return NULL;
|
||||
const uint16_t nch = u16(b + 0x0e);
|
||||
if (!nch || nch > 2 || 0x10 + (uint64_t)nch * 0x4c > size) return NULL;
|
||||
const uint32_t rate = u32(b + 0x10 + 4);
|
||||
uint32_t frames = u32(b + 0x10 + 8);
|
||||
/* (the interface sounds are 48 kHz and short: at most ten seconds of a rate a sound file has) */
|
||||
if (rate < 8000 || rate > 192000 || !frames || frames > rate * 10) return NULL;
|
||||
float *pcm = (float *)calloc((size_t)frames * 2, sizeof *pcm);
|
||||
int ok = pcm != NULL;
|
||||
for (uint16_t c = 0; ok && c < nch; c++) ok = decode_channel(b, size, b + 0x10 + c * 0x4c, pcm, frames);
|
||||
if (!ok) {
|
||||
free(pcm);
|
||||
return NULL;
|
||||
}
|
||||
if (nch == 1 && u16(b + 0x10 + 2) > 1) /* a middle-panned mono sound in both */
|
||||
for (size_t i = 0; i < (size_t)frames * 2; i++) pcm[i] *= 0.7071f;
|
||||
if (rate != MIX_RATE && !(pcm = to_mix_rate(pcm, &frames, rate))) return NULL;
|
||||
*frames_out = frames;
|
||||
return pcm;
|
||||
}
|
||||
|
||||
static void load_all(void) { /* (once: hos_menu_sound_frame's store of 1 into loaded starts it) */
|
||||
int n = 0;
|
||||
for (int id = 1; id < SOUNDS; id++) {
|
||||
uint64_t size = 0;
|
||||
uint8_t *b = hos_fs_rom_file(files[id], FILE_MOST, &size);
|
||||
if (!b) continue;
|
||||
uint32_t frames = 0;
|
||||
float *pcm = hos_menu_sound_decode(b, size, &frames);
|
||||
free(b);
|
||||
if (!pcm) {
|
||||
hos_log("menu sounds: %s is not a BWAV file the options play (PCM16 or DSP-ADPCM, one or two channels, at most 10 s): "
|
||||
"left out", files[id]);
|
||||
continue;
|
||||
}
|
||||
sounds[id] = (Sound){pcm, frames};
|
||||
n++;
|
||||
}
|
||||
for (int i = 0; i < VOICES; i++) atomic_store(&voices[i].at, -1);
|
||||
hos_log("menu sounds: %d of %d interface sounds from the game's files", n, SOUNDS - 1);
|
||||
atomic_store_explicit(&loaded, 2, memory_order_release);
|
||||
}
|
||||
|
||||
float hos_menu_sound_gain(int percent) { return percent <= 0 ? 0.0f : percent >= 100 ? 1.0f : (float)percent / 100.0f; }
|
||||
|
||||
/* The least time between a cursor tick and the sound before it. Moving down a list or changing a value asks for a tick
|
||||
at every step, up to 30 a second while a direction is held, and the focus moving with a press or a tab change is
|
||||
part of that sound: at most five ticks a second are heard. */
|
||||
#define CURSOR_GAP_NS 180000000ll
|
||||
#define CURSOR_GAIN 0.6f /* the tick under the sounds of a decision */
|
||||
static _Atomic int64_t last_start_ns = -CURSOR_GAP_NS;
|
||||
|
||||
void hos_menu_sound_play(int id, int64_t now_ns) {
|
||||
if (id <= 0 || id >= SOUNDS) return;
|
||||
if (id == HOS_MENU_SOUND_CURSOR && now_ns - atomic_load(&last_start_ns) < CURSOR_GAP_NS) return;
|
||||
atomic_store(&last_start_ns, now_ns);
|
||||
if (atomic_load_explicit(&loaded, memory_order_acquire) != 2 || !sounds[id].pcm) return;
|
||||
int pick = 0;
|
||||
uint64_t oldest = UINT64_MAX;
|
||||
for (int i = 0; i < VOICES; i++) {
|
||||
if (atomic_load_explicit(&voices[i].at, memory_order_acquire) < 0) { pick = i; break; }
|
||||
if (voices[i].started < oldest) oldest = voices[i].started, pick = i;
|
||||
}
|
||||
/* A sound that replaces itself restarts instead of stacking up, and a decision replaces the tick of the same press
|
||||
(a row changes its value in the tick the press is seen). */
|
||||
for (int i = 0; i < VOICES; i++) {
|
||||
const int64_t at = atomic_load_explicit(&voices[i].at, memory_order_acquire);
|
||||
if (at >= 0 && (atomic_load(&voices[i].sound) == id || atomic_load(&voices[i].sound) == HOS_MENU_SOUND_CURSOR) &&
|
||||
AT_FRAME(at) < 2400) { /* started under 50 ms ago */
|
||||
pick = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
atomic_store_explicit(&voices[pick].at, -1, memory_order_release);
|
||||
atomic_store_explicit(&voices[pick].sound, id, memory_order_relaxed);
|
||||
voices[pick].started = __atomic_add_fetch(&started_n, 1, __ATOMIC_RELAXED);
|
||||
atomic_store_explicit(&voices[pick].at, (int64_t)(voices[pick].started & 0x7fffffff) << 32, memory_order_release);
|
||||
}
|
||||
|
||||
#ifndef MENU_SOUND_BETWEEN /* (tests/menu_sound_test.c: a play that lands between the mix's reads, 0, or its adds and its store, 1) */
|
||||
#define MENU_SOUND_BETWEEN(where) ((void)0)
|
||||
#endif
|
||||
|
||||
void hos_menu_sound_mix(float *left, float *right, uint32_t frames, float gain) {
|
||||
if (atomic_load_explicit(&loaded, memory_order_acquire) != 2) return;
|
||||
for (int v = 0; v < VOICES; v++) {
|
||||
int64_t at = atomic_load_explicit(&voices[v].at, memory_order_acquire);
|
||||
if (at < 0) continue;
|
||||
MENU_SOUND_BETWEEN(0);
|
||||
const int which = atomic_load_explicit(&voices[v].sound, memory_order_acquire);
|
||||
const Sound *s = &sounds[which];
|
||||
/* hos_menu_sound_play may restart the voice between the two loads: the frame would then belong to the old sound,
|
||||
past the end of a shorter new one; such a voice is mixed from the next call on */
|
||||
if (atomic_load_explicit(&voices[v].at, memory_order_acquire) != at) continue;
|
||||
const uint32_t pos = AT_FRAME(at);
|
||||
if (pos >= s->frames) { /* (past its sound's end, should that ever be: free again, not held for good) */
|
||||
atomic_compare_exchange_strong(&voices[v].at, &at, -1);
|
||||
continue;
|
||||
}
|
||||
const float vg = which == HOS_MENU_SOUND_CURSOR ? gain * CURSOR_GAIN : gain;
|
||||
const uint32_t n = s->frames - pos < frames ? s->frames - pos : frames;
|
||||
const float *src = s->pcm + (size_t)pos * 2;
|
||||
for (uint32_t i = 0; vg != 0.0f && i < n; i++) {
|
||||
left[i] += src[2 * (size_t)i] * vg;
|
||||
right[i] += src[2 * (size_t)i + 1] * vg;
|
||||
}
|
||||
MENU_SOUND_BETWEEN(1);
|
||||
/* Only advance if the voice was not restarted meanwhile. */
|
||||
atomic_compare_exchange_strong(&voices[v].at, &at, pos + n >= s->frames ? -1 : AT_START(at) | (int64_t)(pos + n));
|
||||
}
|
||||
}
|
||||
|
||||
void hos_menu_sound_frame(float *left, float *right, uint32_t frames) {
|
||||
const int volume = rx_option(HOS_OPT_MENU_SOUND_VOLUME, -1);
|
||||
if (volume < 0) return; /* (no renderer with the options: nothing asks for a sound) */
|
||||
int none = 0;
|
||||
if (volume > 0 && atomic_compare_exchange_strong(&loaded, &none, 1)) load_start();
|
||||
const int64_t now = hos_now_ns();
|
||||
/* (what was asked for since the last block: a press or two at most. With the row at 0 the options ask for none,
|
||||
and one asked for as it went to 0 is taken and not played) */
|
||||
for (int k = 0, id; k < 8 && (id = rx_option(HOS_OPT_MENU_SOUND, 0)) > 0; k++)
|
||||
if (volume > 0) hos_menu_sound_play(id, now);
|
||||
hos_menu_sound_mix(left, right, frames, hos_menu_sound_gain(volume));
|
||||
}
|
||||
27
host/menu_sound.h
Normal file
27
host/menu_sound.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/* (Triforce) The options' sounds, the game's own interface sounds (menu_sound.c). */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* A BWAV file's samples as interleaved stereo floats at 48 kHz, *frames of them (freed by the caller); NULL when the
|
||||
bytes are not a file this plays. Nothing outside [file, file + size) is read */
|
||||
float *hos_menu_sound_decode(const uint8_t *file, uint64_t size, uint32_t *frames);
|
||||
/* The Menu sounds row's percent as a gain: 0 at 0 and under, 1 at 100 and over */
|
||||
float hos_menu_sound_gain(int percent);
|
||||
/* Starts the sound of a HOS_MENU_SOUND_* (gpu/vk/vk_api.h) on a free voice, else on the oldest. No lock is taken: it
|
||||
may run while another thread mixes. now_ns: the caller's clock (the cursor's tick is not started twice in 180 ms) */
|
||||
void hos_menu_sound_play(int id, int64_t now_ns);
|
||||
/* Adds the playing sounds to `frames` samples of the left and the right channel at 48 kHz, times gain, and moves the
|
||||
voices on (at gain 0 too: a sound goes on unheard) */
|
||||
void hos_menu_sound_mix(float *left, float *right, uint32_t frames, float gain);
|
||||
/* The sound thread, at each block it mixes (host/nn_audio.c render_frame), after the game's own mix and before the
|
||||
Master volume: the sounds the options asked for since the last block are started, and those playing are added to
|
||||
the block's front left and right */
|
||||
void hos_menu_sound_frame(float *left, float *right, uint32_t frames);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
161
host/mkw_glue.c
161
host/mkw_glue.c
@@ -15,24 +15,37 @@
|
||||
The runtime commits what it hands out (image, system region, heap, address regions: hos_commit), because host
|
||||
APIs given guest buffers (ReadFile into a game buffer) fail on reserved pages without raising an exception. A
|
||||
guest or host access to a page nothing committed is committed on first touch, 64 KB at a time, by the handler. */
|
||||
void hos_commit(uint64_t addr, uint64_t size) {
|
||||
if (!size) return;
|
||||
void *hos_mem_commit_begin(uint64_t addr, uint64_t size); /* ((Triforce) hos_mem.c: the census's notes around the commit) */
|
||||
void hos_mem_commit_end(void *had, uint64_t addr, uint64_t size, int committed);
|
||||
int hos_commit(uint64_t addr, uint64_t size) {
|
||||
if (!size) return 1;
|
||||
if (!hos_guest_range(addr, size)) { /* (hookshot: guest pages only; the posix layer's commit changes any page's protection) */
|
||||
hos_log("mem: commit of %#llx bytes at %#llx: outside the guest window", (unsigned long long)size, (unsigned long long)addr);
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
uint64_t lo = addr & ~0xFFFFULL, hi = (addr + size + 0xFFFF) & ~0xFFFFULL;
|
||||
{ /* (HOS_LARGE_PAGES: the heap's large pages are a reservation of their own, committed whole; what lies below or
|
||||
above them is committed by itself) */
|
||||
extern uint64_t hos_large_lo, hos_large_hi;
|
||||
if (hos_large_hi && lo < hos_large_hi && hi > hos_large_lo) {
|
||||
if (lo < hos_large_lo) hos_commit(lo, hos_large_lo - lo);
|
||||
if (hi <= hos_large_hi) return;
|
||||
const uint64_t llo = lo > hos_large_lo ? lo : hos_large_lo, lhi = hi < hos_large_hi ? hi : hos_large_hi;
|
||||
hos_mem_note(HOS_MEM_COMMITTED, llo, lhi - llo); /* ((Triforce) the census: committed already, handed out now) */
|
||||
const int below = lo < hos_large_lo ? hos_commit(lo, hos_large_lo - lo) : 1;
|
||||
if (hi <= hos_large_hi) return below;
|
||||
lo = hos_large_hi;
|
||||
}
|
||||
}
|
||||
if (!VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE)) {
|
||||
const DWORD err = GetLastError();
|
||||
/* ((Triforce) the census: held before the commit, under the lock the give-back's decommit checks under; no longer kept once
|
||||
it is made, and as before if it fails: hos_mem.c hos_mem_commit_begin) */
|
||||
void *const census_had = hos_mem_commit_begin(lo, hi - lo);
|
||||
/* (Triforce: a commit of committed pages resets their protection, the lazy write-back's too: rx_backend.c) */
|
||||
const int io = hos_guest_io_begin(GP(lo), hi - lo);
|
||||
const int committed = VirtualAlloc(GP(lo), (SIZE_T)(hi - lo), MEM_COMMIT, PAGE_READWRITE) != NULL;
|
||||
const DWORD commit_err = committed ? 0 : GetLastError();
|
||||
hos_guest_io_end(io);
|
||||
hos_mem_commit_end(census_had, lo, hi - lo, committed);
|
||||
if (!committed) {
|
||||
const DWORD err = commit_err;
|
||||
hos_log("mem: commit of %#llx bytes at %#llx failed (error %lu)", (unsigned long long)(hi - lo),
|
||||
(unsigned long long)lo, err);
|
||||
if (err == ERROR_COMMITMENT_LIMIT) { /* (1455: RAM and page file full; the game crashes soon after. Said once, on its own thread) */
|
||||
@@ -43,7 +56,9 @@ void hos_commit(uint64_t addr, uint64_t size) {
|
||||
CloseHandle(CreateThread(NULL, 0, hos_out_of_memory_box, NULL, 0, NULL));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
DWORD WINAPI hos_out_of_memory_box(LPVOID unused) {
|
||||
@@ -69,8 +84,9 @@ static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
|
||||
static volatile LONG touched;
|
||||
if (InterlockedIncrement(&touched) <= 16)
|
||||
hos_log("mem: %#llx committed on first touch (nothing handed it out)", (unsigned long long)guest);
|
||||
return VirtualAlloc(GP(guest & ~0xFFFFULL), 0x10000, MEM_COMMIT, PAGE_READWRITE) ? EXCEPTION_CONTINUE_EXECUTION
|
||||
: EXCEPTION_CONTINUE_SEARCH;
|
||||
if (!VirtualAlloc(GP(guest & ~0xFFFFULL), 0x10000, MEM_COMMIT, PAGE_READWRITE)) return EXCEPTION_CONTINUE_SEARCH;
|
||||
hos_mem_note(HOS_MEM_TOUCHED, guest & ~0xFFFFULL, 0x10000); /* ((Triforce) the census: atomics only, no lock) */
|
||||
return EXCEPTION_CONTINUE_EXECUTION;
|
||||
}
|
||||
|
||||
/* A crash nothing handles: what, where (mkw.exe+offset: nx2/dllmap.py reads mkw.map the same way), and the guest
|
||||
@@ -82,6 +98,11 @@ static uint64_t peek_guest(uint64_t a) {
|
||||
}
|
||||
|
||||
static LONG WINAPI report_crash(EXCEPTION_POINTERS *info) {
|
||||
/* (Triforce) one report for a failed run (hos_thread.c hos_ending_enter): another thread that crashes or hits a fatal
|
||||
error while this one reports waits instead of ending the process under it, and a crash inside this thread's own
|
||||
report, or the same crash handed here twice (report_fatal below, then the unhandled filter), is not reported again */
|
||||
int hos_ending_enter(void);
|
||||
if (!hos_ending_enter()) return EXCEPTION_CONTINUE_SEARCH;
|
||||
EXCEPTION_RECORD *rec = info->ExceptionRecord;
|
||||
const uint64_t rip = info->ContextRecord->Rip;
|
||||
HMODULE mod = NULL;
|
||||
@@ -762,3 +783,125 @@ void hos_process_memory(uint64_t *private_bytes, uint64_t *working_set) {
|
||||
*private_bytes = m.PrivateUsage;
|
||||
*working_set = m.WorkingSetSize;
|
||||
}
|
||||
|
||||
/* ---- (Triforce) the guest memory census's thread (host/guest_census.h has why) ---------------------------------------------
|
||||
HOS_MEM_CENSUS=<seconds> (5 by default; 0: none): every that many seconds a "mem: census" line: what the runtime handed out
|
||||
of the guest window by kind (the program and the system region, the heap, the address regions and the game's pages in them,
|
||||
what lies outside them: the physical maps and first touches), what the game counts against the memory it is told it has,
|
||||
the pages it let go that stay committed and what the give-back gave back, the process's commit and working set, and the
|
||||
PC's commit and RAM; then a "mem: census, the renderer" line with the renderer's big holders (gpu/vk/vk_memory.cpp
|
||||
mkwvk_memory_holders). A count of the bits of the gigabytes in use (guest_census.h GC_WALK steps over the rest) and a few
|
||||
system calls, on a thread of its own.
|
||||
HOS_MEM_GIVE_BACK=1 (off by default, not yet run in the game): each pass first gives back up to 256 MB of the pages the game
|
||||
let go that stay committed, where no import holds them (hos_mem.c hos_mem_give_back); =2: only while the PC is short, its
|
||||
commit past 85% of its limit or under 1.5 GB of RAM available */
|
||||
#include "guest_census.h"
|
||||
void hos_mem_census(GcCensus *c); /* (hos_mem.c) */
|
||||
int rx_memory_holders(char *out, size_t n); /* (rx_backend.c: 0 when there is no renderer's part) */
|
||||
static int census_every = 5, census_line = 1, census_give_back;
|
||||
|
||||
/* The process's and the PC's figures, -1 where not known */
|
||||
static void census_figures(GcCensus *c) {
|
||||
c->proc_commit = c->proc_peak = c->working_set = c->sys_commit = c->sys_limit = c->ram_avail = c->ram_total = -1;
|
||||
#ifdef _WIN32
|
||||
typedef struct {
|
||||
DWORD cb, PageFaultCount;
|
||||
SIZE_T PeakWorkingSetSize, WorkingSetSize, QuotaPeakPagedPoolUsage, QuotaPagedPoolUsage,
|
||||
QuotaPeakNonPagedPoolUsage, QuotaNonPagedPoolUsage, PagefileUsage, PeakPagefileUsage, PrivateUsage;
|
||||
} Counters;
|
||||
typedef struct { /* (PERFORMANCE_INFORMATION: pages) */
|
||||
DWORD cb;
|
||||
SIZE_T CommitTotal, CommitLimit, CommitPeak, PhysicalTotal, PhysicalAvailable, SystemCache, KernelTotal, KernelPaged,
|
||||
KernelNonpaged, PageSize;
|
||||
DWORD HandleCount, ProcessCount, ThreadCount;
|
||||
} Perf;
|
||||
typedef BOOL(WINAPI * ProcFn)(HANDLE, Counters *, DWORD);
|
||||
typedef BOOL(WINAPI * PerfFn)(Perf *, DWORD);
|
||||
static ProcFn proc;
|
||||
static PerfFn perf;
|
||||
static int looked;
|
||||
if (!looked) {
|
||||
looked = 1;
|
||||
HMODULE k = GetModuleHandleA("kernel32.dll");
|
||||
proc = (ProcFn)(void *)GetProcAddress(k, "K32GetProcessMemoryInfo");
|
||||
perf = (PerfFn)(void *)GetProcAddress(k, "K32GetPerformanceInfo");
|
||||
}
|
||||
Counters m;
|
||||
memset(&m, 0, sizeof m);
|
||||
m.cb = sizeof m;
|
||||
if (proc && proc(GetCurrentProcess(), &m, sizeof m))
|
||||
c->proc_commit = (int64_t)m.PrivateUsage, c->proc_peak = (int64_t)m.PeakPagefileUsage, c->working_set = (int64_t)m.WorkingSetSize;
|
||||
Perf p;
|
||||
memset(&p, 0, sizeof p);
|
||||
p.cb = sizeof p;
|
||||
if (perf && perf(&p, sizeof p) && p.PageSize) {
|
||||
c->sys_commit = (int64_t)(p.CommitTotal * p.PageSize), c->sys_limit = (int64_t)(p.CommitLimit * p.PageSize);
|
||||
c->ram_avail = (int64_t)(p.PhysicalAvailable * p.PageSize), c->ram_total = (int64_t)(p.PhysicalTotal * p.PageSize);
|
||||
}
|
||||
#else
|
||||
/* (linux: the PC's committed memory and its limit, /proc/meminfo; the process's resident set, /proc/self/status) */
|
||||
FILE *f = fopen("/proc/meminfo", "r");
|
||||
if (f) {
|
||||
char line[128];
|
||||
unsigned long long kb;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
if (sscanf(line, "MemTotal: %llu kB", &kb) == 1) c->ram_total = (int64_t)kb << 10;
|
||||
else if (sscanf(line, "MemAvailable: %llu kB", &kb) == 1) c->ram_avail = (int64_t)kb << 10;
|
||||
else if (sscanf(line, "Committed_AS: %llu kB", &kb) == 1) c->sys_commit = (int64_t)kb << 10;
|
||||
else if (sscanf(line, "CommitLimit: %llu kB", &kb) == 1) c->sys_limit = (int64_t)kb << 10;
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
if ((f = fopen("/proc/self/status", "r"))) {
|
||||
char line[128];
|
||||
unsigned long long kb;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
if (sscanf(line, "VmRSS: %llu kB", &kb) == 1) c->working_set = (int64_t)kb << 10;
|
||||
else if (sscanf(line, "VmHWM: %llu kB", &kb) == 1) c->proc_peak = (int64_t)kb << 10;
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static DWORD WINAPI census_main(void *unused) {
|
||||
(void)unused;
|
||||
hos_host_thread_register_self("hos memory census");
|
||||
for (;;) {
|
||||
Sleep((DWORD)census_every * 1000);
|
||||
GcCensus c;
|
||||
memset(&c, 0, sizeof c);
|
||||
census_figures(&c);
|
||||
if (census_give_back > 0) { /* (first, so that the line says what it did) */
|
||||
const int short_now = (c.sys_limit > 0 && c.sys_commit > c.sys_limit / 100 * 85) ||
|
||||
(c.ram_avail >= 0 && c.ram_avail < (1536LL << 20));
|
||||
if (census_give_back == 1 || short_now) {
|
||||
if (hos_mem_give_back(256ULL << 20) < 0) {
|
||||
hos_log("mem: the give-back is off: the renderer cannot say which pages its imports hold (no mkwvk_guest_release)");
|
||||
census_give_back = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!census_line) continue;
|
||||
hos_mem_census(&c);
|
||||
char line[1024];
|
||||
gc_line(line, sizeof line, &c);
|
||||
hos_log("mem: %s", line);
|
||||
char holders[1000];
|
||||
if (rx_memory_holders(holders, sizeof holders) > 0) hos_log("mem: census, the renderer (MB): %s", holders);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void hos_mem_census_start(void) {
|
||||
const char *e = getenv("HOS_MEM_CENSUS"), *g = getenv("HOS_MEM_GIVE_BACK");
|
||||
census_line = !(e && atoi(e) <= 0);
|
||||
census_every = e && atoi(e) > 0 ? atoi(e) : 5;
|
||||
census_give_back = g ? atoi(g) : 0;
|
||||
if (!census_line && census_give_back <= 0) return;
|
||||
char every[32] = "off";
|
||||
if (census_line) snprintf(every, sizeof every, "every %d s", census_every);
|
||||
hos_log("mem: census %s; give-back %s (HOS_MEM_CENSUS, HOS_MEM_GIVE_BACK)", every,
|
||||
census_give_back == 1 ? "on" : census_give_back >= 2 ? "while the PC is short" : "off");
|
||||
CloseHandle(CreateThread(NULL, 0, census_main, NULL, 0, NULL));
|
||||
}
|
||||
|
||||
288
host/mod_cheats.c
Normal file
288
host/mod_cheats.c
Normal file
@@ -0,0 +1,288 @@
|
||||
/* (Triforce) The cheats of the mods' cheat files, after NX2navi's Cheats tab: read at the start, switched on the System
|
||||
tab, run once a game frame.
|
||||
|
||||
host/mods.c hands over each mod's file for this build of the game, mods\<name>\cheats\<build id>.txt (the first 8
|
||||
bytes of main's build id, as Atmosphere and the emulators name it), in the mods' order. host/mod_cheats.h reads it and
|
||||
has the interpreter and what the format is; this file has what is the game's: its memory, the buttons, the rows and
|
||||
which cheats are on.
|
||||
|
||||
- The memory a cheat reads and writes is the game's program image and its heap, each access checked against them
|
||||
(mod_cheat_readable); a write into a module's code is dropped, as the code is compiled ahead of time. A cheat that
|
||||
stores into the code outright (opcode 0 into main's .text) is not run at all and says so on its row: NX2navi built
|
||||
such cheats into the game, which this translator cannot do (a code patch can: nx2/code_mods.py).
|
||||
The regions a cheat names are where the game is told they are (host/hos_svc.c GetInfo): main at the module main's
|
||||
base (gen/hos_image.c), the heap and the alias region at HOS_HEAP_BASE, the address space at 0x08000000.
|
||||
- A cheat is off until its row switches it on, and one that was switched on stays on at the next start, as NX2navi
|
||||
kept them: triforce_cheats.txt in the game folder has a line for each cheat that is on, the FNV-1a 32 of
|
||||
"<mod>/<name>" (a name's second cheat in a file: "<name>#2") and that text. A master code ({name}) always runs,
|
||||
before the file's other cheats.
|
||||
- The rows are the menu's (gpu/vk/mod_cheat_rows.h): at the first frame the renderer answers, the cheats are named to
|
||||
it (HOS_OPT_MODCHEAT_TEXT, _ADD, _COUNT), and every frame it is asked what the player switched (HOS_OPT_MODCHEAT_ASK).
|
||||
Without that renderer the cheats that are on still run.
|
||||
- hos_mod_cheats_frame is called at each present of the game's (host/nvn_native.c), on the thread that presents: the
|
||||
game's other threads run meanwhile, as they do under a cheat on the console. */
|
||||
#include "hos.h"
|
||||
#include "hos_paths.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
#include "mod_cheats.h"
|
||||
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
extern volatile uint64_t hos_npad_buttons; /* nn_hid.c: the buttons the game was last given */
|
||||
|
||||
enum { MAX_CHEATS = 512, MAX_FILES = 32, MAX_ROWS = 96 /* (mod_cheat_rows.h kSlots) */, FILE_MOST = 1 << 20,
|
||||
LONG_NAME = 28 /* (bytes of a name a row surely shows beside its value) */ };
|
||||
typedef enum { KIND_HEADING, KIND_SWITCH, KIND_MASTER, KIND_REFUSED } Kind;
|
||||
typedef struct {
|
||||
ModCheat c;
|
||||
Kind kind;
|
||||
int file, on, row; /* row: its row on the System tab, -1 none */
|
||||
int same; /* 1, or which of the file's cheats of this name it is */
|
||||
uint32_t key; /* FNV-1a 32 of "<mod>/<name>", "<mod>/<name>#<same>" from the second on */
|
||||
int said_read, said_drop, said_steps; /* (each said once in the log) */
|
||||
} Cheat;
|
||||
typedef struct {
|
||||
char mod[160];
|
||||
ModCheatState state;
|
||||
int first, n;
|
||||
} File;
|
||||
static Cheat cheats[MAX_CHEATS];
|
||||
static File files[MAX_FILES];
|
||||
static int ncheats, nfiles, nrows, running; /* running: cheats that run each frame (masters and the ones on) */
|
||||
static int row_cheat[MAX_ROWS];
|
||||
|
||||
static const char *last_sep(const char *s) {
|
||||
const char *b = strrchr(s, '\\'), *f = strrchr(s, '/');
|
||||
return b > f ? b : f;
|
||||
}
|
||||
/* a file of the game folder */
|
||||
static int game_file(char *out, size_t cap, const char *name) {
|
||||
char exe[MAX_PATH];
|
||||
const DWORD n = hos_game_path_a(exe, sizeof exe);
|
||||
const char *slash = n && n < sizeof exe ? last_sep(exe) : NULL;
|
||||
if (!slash) return 0;
|
||||
return snprintf(out, cap, "%.*s%s", (int)(slash + 1 - exe), exe, name) < (int)cap;
|
||||
}
|
||||
|
||||
/* ---- which cheats are on: triforce_cheats.txt ---- */
|
||||
#define ON_FILE "triforce_cheats.txt"
|
||||
static void on_load(void) {
|
||||
char path[MAX_PATH + 32], line[512];
|
||||
if (!game_file(path, sizeof path, ON_FILE)) return;
|
||||
FILE *f = fopen(path, "r");
|
||||
if (!f) return;
|
||||
while (fgets(line, sizeof line, f)) {
|
||||
char *end = NULL;
|
||||
const unsigned long key = strtoul(line, &end, 16);
|
||||
if (line[0] == '#' || end != line + 8) continue;
|
||||
for (int i = 0; i < ncheats; i++)
|
||||
if (cheats[i].kind == KIND_SWITCH && cheats[i].key == (uint32_t)key) cheats[i].on = 1;
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
static void on_save(void) {
|
||||
char path[MAX_PATH + 32], fresh[MAX_PATH + 40];
|
||||
if (!game_file(path, sizeof path, ON_FILE)) return;
|
||||
snprintf(fresh, sizeof fresh, "%s.new", path);
|
||||
FILE *f = fopen(fresh, "wb"); /* (its lines end the same on Windows and Linux) */
|
||||
if (!f) {
|
||||
hos_log("cheats: %s could not be written: the switches are as they are until the game ends", path);
|
||||
return;
|
||||
}
|
||||
fputs("# The mods' cheats that are switched on (the System tab's rows), a line each: kept for the next start.\n", f);
|
||||
for (int i = 0; i < ncheats; i++) {
|
||||
const Cheat *c = &cheats[i];
|
||||
if (c->kind != KIND_SWITCH || !c->on) continue;
|
||||
if (c->same > 1) fprintf(f, "%08x %s/%s#%d\n", c->key, files[c->file].mod, c->c.name, c->same);
|
||||
else fprintf(f, "%08x %s/%s\n", c->key, files[c->file].mod, c->c.name);
|
||||
}
|
||||
const int ok = !ferror(f);
|
||||
if (fclose(f) || !ok || !MoveFileExA(fresh, path, MOVEFILE_REPLACE_EXISTING)) {
|
||||
remove(fresh);
|
||||
hos_log("cheats: %s could not be written: the switches are as they are until the game ends", path);
|
||||
}
|
||||
}
|
||||
|
||||
static void count_running(void) {
|
||||
running = 0;
|
||||
for (int i = 0; i < ncheats; i++) running += cheats[i].kind == KIND_MASTER || (cheats[i].kind == KIND_SWITCH && cheats[i].on);
|
||||
}
|
||||
|
||||
/* ---- the files (host/mods.c, at the start, before the game's threads) ---- */
|
||||
static ModCheatLayout game_layout(void) {
|
||||
extern const uint64_t hos_code[][2]; /* gen/hos_image.c: each module's .text */
|
||||
extern const int hos_code_count;
|
||||
const ModCheatLayout l = {hos_image_base, hos_image_end, HOS_HEAP_BASE, hos_heap_size, hos_code, hos_code_count};
|
||||
return l;
|
||||
}
|
||||
|
||||
/* One mod's cheat file for this build (mod: the mod's name as host/mods.c has it) */
|
||||
void hos_mod_cheats_add(const char *mod, const char *path) {
|
||||
extern const uint64_t hos_main_base; /* gen/hos_image.c */
|
||||
if (nfiles == MAX_FILES || ncheats == MAX_CHEATS) {
|
||||
hos_log("cheats: %s: not read (%d cheat files and %d cheats are the most)", path, MAX_FILES, MAX_CHEATS);
|
||||
return;
|
||||
}
|
||||
FILE *f = fopen(path, "rb");
|
||||
char *text = f ? (char *)malloc(FILE_MOST + 1) : NULL;
|
||||
const size_t len = text ? fread(text, 1, FILE_MOST + 1, f) : 0;
|
||||
if (f) fclose(f);
|
||||
if (!text || !len || len > FILE_MOST) {
|
||||
hos_log("cheats: %s: %s", path, !f ? "cannot be read" : !text ? "no memory" : len ? "larger than 1 MB: not a cheat file" : "empty");
|
||||
free(text);
|
||||
return;
|
||||
}
|
||||
/* (a word is 8 digits and what parts it from the next: the file holds no more than len / 8 of them) */
|
||||
uint32_t *pool = (uint32_t *)malloc((len / 8 + 1) * sizeof *pool); /* (kept: the cheats' code) */
|
||||
ModCheat *parsed = (ModCheat *)malloc(sizeof *parsed * (size_t)(MAX_CHEATS - ncheats));
|
||||
if (!pool || !parsed) {
|
||||
free(text), free(pool), free(parsed);
|
||||
return;
|
||||
}
|
||||
int cut = 0;
|
||||
const int n = mod_cheat_parse(text, len, parsed, MAX_CHEATS - ncheats, pool, (int)(len / 8 + 1), &cut);
|
||||
free(text);
|
||||
File *file = &files[nfiles];
|
||||
memset(file, 0, sizeof *file);
|
||||
snprintf(file->mod, sizeof file->mod, "%s", mod);
|
||||
file->first = ncheats, file->n = n;
|
||||
const ModCheatLayout l = game_layout();
|
||||
int counts[KIND_REFUSED + 1] = {0};
|
||||
for (int i = 0; i < n; i++) {
|
||||
Cheat *c = &cheats[ncheats + i];
|
||||
memset(c, 0, sizeof *c);
|
||||
c->c = parsed[i];
|
||||
c->file = nfiles, c->row = -1;
|
||||
uint64_t at = 0;
|
||||
if (c->c.why[0]) c->kind = KIND_REFUSED;
|
||||
else if (mod_cheat_is_heading(&c->c)) c->kind = KIND_HEADING;
|
||||
else if (mod_cheat_stores_code(c->c.words, c->c.n, &l, hos_main_base, &at)) {
|
||||
c->kind = KIND_REFUSED;
|
||||
snprintf(c->c.why, sizeof c->c.why, "it writes the game's code (main+%#llx), which is compiled ahead of time",
|
||||
(unsigned long long)(at - hos_main_base));
|
||||
} else c->kind = c->c.master ? KIND_MASTER : KIND_SWITCH;
|
||||
/* (its switch's number: the name's second cheat in the file is "<name>#2", so each keeps its own) */
|
||||
char named[400];
|
||||
c->same = 1;
|
||||
for (int k = 0; k < i; k++) c->same += !strcmp(parsed[k].name, parsed[i].name);
|
||||
if (c->same > 1) snprintf(named, sizeof named, "%s/%s#%d", mod, c->c.name, c->same);
|
||||
else snprintf(named, sizeof named, "%s/%s", mod, c->c.name);
|
||||
c->key = mod_cheat_hash(named);
|
||||
counts[c->kind]++;
|
||||
if (c->kind == KIND_REFUSED && counts[KIND_REFUSED] <= 24)
|
||||
hos_log("cheats: %s: \"%s\" (line %d) cannot run: %s", mod, c->c.name, c->c.line, c->c.why);
|
||||
}
|
||||
free(parsed);
|
||||
ncheats += n, nfiles++;
|
||||
hos_log("cheats: %s: %d cheats in %s: %d to switch, %d always on, %d that cannot run, %d headings%s", mod, n, path, counts[KIND_SWITCH],
|
||||
counts[KIND_MASTER], counts[KIND_REFUSED], counts[KIND_HEADING], cut ? " (the file has more: the rest is left out)" : "");
|
||||
}
|
||||
|
||||
/* Once every mod's file is read: which cheats were left on, and their rows (a heading has none) */
|
||||
void hos_mod_cheats_ready(void) {
|
||||
if (!ncheats) return;
|
||||
on_load();
|
||||
int on = 0, unlisted = 0;
|
||||
for (int i = 0; i < ncheats; i++) {
|
||||
if (cheats[i].kind == KIND_HEADING) continue;
|
||||
if (nrows < MAX_ROWS) row_cheat[nrows] = i, cheats[i].row = nrows++;
|
||||
else unlisted++;
|
||||
on += cheats[i].kind == KIND_SWITCH && cheats[i].on;
|
||||
}
|
||||
count_running();
|
||||
hos_log("cheats: %d rows on the System tab, %d cheats left on from before (" ON_FILE ")%s", nrows, on,
|
||||
unlisted ? "; more cheats than rows: the last ones have none and stay as they are" : "");
|
||||
}
|
||||
|
||||
/* ---- the rows ---- */
|
||||
static void name_rows(void) {
|
||||
for (int r = 0; r < nrows; r++) {
|
||||
const Cheat *c = &cheats[row_cheat[r]];
|
||||
char line[400];
|
||||
if (c->kind == KIND_REFUSED) snprintf(line, sizeof line, "Not supported: %s. From the mod %s.", c->c.why, files[c->file].mod);
|
||||
else if (c->kind == KIND_MASTER) snprintf(line, sizeof line, "Always on: the master code of the mod %s.", files[c->file].mod);
|
||||
else snprintf(line, sizeof line, "From the mod %s.", files[c->file].mod);
|
||||
for (const char *p = c->c.name; *p; p++) rx_option_set(HOS_OPT_MODCHEAT_TEXT, (unsigned char)*p);
|
||||
rx_option_set(HOS_OPT_MODCHEAT_TEXT, '\n');
|
||||
if (strlen(c->c.name) > LONG_NAME) { /* (a row has room for so much of a name: the whole of it is in its line) */
|
||||
rx_option_set(HOS_OPT_MODCHEAT_TEXT, '"');
|
||||
for (const char *p = c->c.name; *p; p++) rx_option_set(HOS_OPT_MODCHEAT_TEXT, (unsigned char)*p);
|
||||
for (const char *p = "\". "; *p; p++) rx_option_set(HOS_OPT_MODCHEAT_TEXT, (unsigned char)*p);
|
||||
}
|
||||
for (const char *p = line; *p; p++) rx_option_set(HOS_OPT_MODCHEAT_TEXT, (unsigned char)*p);
|
||||
rx_option_set(HOS_OPT_MODCHEAT_ADD, c->kind == KIND_REFUSED ? HOS_MODCHEAT_REFUSED : c->kind == KIND_MASTER ? HOS_MODCHEAT_ALWAYS
|
||||
: c->on ? HOS_MODCHEAT_ON : 0);
|
||||
}
|
||||
rx_option_set(HOS_OPT_MODCHEAT_COUNT, nrows);
|
||||
}
|
||||
static void switched(int row, int on) {
|
||||
if (row < 0 || row >= nrows) return;
|
||||
Cheat *c = &cheats[row_cheat[row]];
|
||||
if (c->kind != KIND_SWITCH || c->on == on) return;
|
||||
c->on = on;
|
||||
c->said_read = c->said_drop = c->said_steps = 0;
|
||||
count_running();
|
||||
on_save();
|
||||
hos_log("cheats: %s: \"%s\" %s", files[c->file].mod, c->c.name, on ? "on" : "off");
|
||||
}
|
||||
|
||||
/* ---- the game's memory ---- */
|
||||
static int mem_read(void *ctx, uint64_t a, unsigned n, void *out) {
|
||||
if (!mod_cheat_readable((const ModCheatLayout *)ctx, a, n)) return 0;
|
||||
memcpy(out, GP(a), n);
|
||||
return 1;
|
||||
}
|
||||
static int mem_write(void *ctx, uint64_t a, unsigned n, const void *in) {
|
||||
if (!mod_cheat_writable((const ModCheatLayout *)ctx, a, n)) return 0;
|
||||
if (memcmp(GP(a), in, n)) { /* (unchanged: not written, and not marked for the renderer) */
|
||||
memcpy(GP(a), in, n);
|
||||
HOS_WRITTEN(GP(a), n);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void run_cheat(Cheat *c, File *file, const ModCheatLayout *l, const ModCheatMem *m, uint64_t keys) {
|
||||
ModCheatRun r;
|
||||
const int end = mod_cheat_run(c->c.words, c->c.n, &file->state, m, keys, &r);
|
||||
if (end == MOD_CHEAT_STOP_READ && !c->said_read) {
|
||||
c->said_read = 1;
|
||||
hos_log("cheats: %s: \"%s\" reads %#llx, which is not the game's memory (a pointer that is not set yet?): it goes on once it is",
|
||||
file->mod, c->c.name, (unsigned long long)r.addr);
|
||||
}
|
||||
if (end == MOD_CHEAT_STOP_STEPS && !c->said_steps) {
|
||||
c->said_steps = 1;
|
||||
hos_log("cheats: %s: \"%s\" ran %d opcodes in one frame (a loop without end?): stopped for the frame, each frame", file->mod,
|
||||
c->c.name, MOD_CHEAT_STEPS);
|
||||
}
|
||||
if (r.dropped && !c->said_drop) {
|
||||
c->said_drop = 1;
|
||||
hos_log("cheats: %s: \"%s\" writes %#llx, which is %s: nothing is written there", file->mod, c->c.name, (unsigned long long)r.addr,
|
||||
mod_cheat_readable(l, r.addr, 1) ? "the game's code (compiled ahead of time)" : "not the game's memory");
|
||||
}
|
||||
}
|
||||
|
||||
/* host/nvn_native.c, at each present of the game's */
|
||||
void hos_mod_cheats_frame(void) {
|
||||
extern const uint64_t hos_main_base; /* gen/hos_image.c */
|
||||
static int named;
|
||||
if (!ncheats) return;
|
||||
if (!named && rx_option(HOS_OPT_MODCHEAT_ASK, -1) >= 0) named = 1, name_rows(); /* (the renderer is there now) */
|
||||
for (int k = 0, ask; named && k < 8 && (ask = rx_option(HOS_OPT_MODCHEAT_ASK, 0)) > 1; k++) switched(ask / 2 - 1, ask & 1);
|
||||
if (!running) return;
|
||||
const ModCheatLayout l = game_layout();
|
||||
/* (the regions: as host/hos_svc.c's GetInfo tells the game) */
|
||||
const ModCheatMem m = {(void *)&l, mem_read, mem_write, {hos_main_base, HOS_HEAP_BASE, HOS_HEAP_BASE, 0x08000000ULL}};
|
||||
const uint64_t keys = __atomic_load_n(&hos_npad_buttons, __ATOMIC_RELAXED);
|
||||
for (int f = 0; f < nfiles; f++) {
|
||||
File *file = &files[f];
|
||||
mod_cheat_frame(&file->state);
|
||||
for (int pass = 0; pass < 2; pass++) /* (the master codes first, then the cheats that are on, in the file's order) */
|
||||
for (int i = file->first; i < file->first + file->n; i++) {
|
||||
Cheat *c = &cheats[i];
|
||||
if (pass ? c->kind == KIND_SWITCH && c->on : c->kind == KIND_MASTER) run_cheat(c, file, &l, &m, keys);
|
||||
}
|
||||
}
|
||||
}
|
||||
469
host/mod_cheats.h
Normal file
469
host/mod_cheats.h
Normal file
@@ -0,0 +1,469 @@
|
||||
/* (Triforce) The mods' cheat files: the reader and the interpreter (host/mod_cheats.c has the files, the game's memory
|
||||
and the rows; tests/test_mod_cheats.py compiles this alone against a stand-in memory).
|
||||
|
||||
A cheat file is the text file Atmosphere and the emulators take, mods/<name>/cheats/<build id>.txt, the build id the
|
||||
first 8 bytes of main's in hex: a cheat is a name in square brackets followed by its code, words of 8 hex digits; a
|
||||
name in braces is a master code, which always runs. The code is a program for a small machine over the game's
|
||||
memory: 16 registers, 16 saved values, static registers, and the opcodes below. It is run once a game frame, here by
|
||||
an interpreter on the game's presenting thread: nothing of it is compiled into the game, so a cheat file needs no
|
||||
build. (NX2navi compiled in the cheats that write the game's instructions, as patch sets its translator could switch.
|
||||
This translator has none: such a cheat is refused here, and what it changes can be made a code patch, nx2/code_mods.py.)
|
||||
|
||||
The opcodes, by the first word's first digit (T a width of 1, 2, 4 or 8 bytes; M a region, 0 main, 1 the heap, 2 the
|
||||
alias region, 3 the address space; A an offset of 40 bits; V a value, two words when T is 8):
|
||||
0TMR00AA AAAAAAAA V store V at M + A + register R
|
||||
1TMC00AA AAAAAAAA V if the value at M + A <C> V (C: 1 >, 2 >=, 3 <, 4 <=, 5 ==, 6 !=) ... 20000000
|
||||
20000000 / 21000000 the end of an if / its else
|
||||
300R0000 VVVVVVVV a loop begins, register R its count; 310R0000 ends it (R one less, again while not 0)
|
||||
400R0000 VVVVVVVV VVVVVVVV register R = V
|
||||
5TMR00AA AAAAAAAA register R = the value at M + A; 5T0R10AA AAAAAAAA at R + A
|
||||
6T0RIor0 VVVVVVVV VVVVVVVV store V at register R (+ register r when o is 1); I: R goes on by T after
|
||||
7T0RC000 VVVVVVVV R = R <C> V (C: 0 +, 1 -, 2 *, 3 <<, 4 >>)
|
||||
8kkkkkkk if the buttons k are held ... 20000000
|
||||
9TCRS0s0 / 9TCRS100 V R = S <C> s, or V (C: 0 to 4 as above, 5 and, 6 or, 7 not, 8 xor, 9 the value of S itself)
|
||||
ATSRIOxa (aaaaaaaa) store register S at an address by O: 0 R, 1 R + register x, 2 R + a, 3 region x + R,
|
||||
4 region x + a, 5 region x + R + a; I: R goes on by T after
|
||||
C0TcSX.. (..) if register S <c> an operand by X: 0 the value at region + a, 1 at region + register,
|
||||
2 at register + a, 3 at register + register, 4 a value V, 5 another register
|
||||
C10D0Sx0 x 0: register D = saved S; 1: saved D = register S; 2: saved D = 0; 3: register D = 0
|
||||
C2x0XXXX the same for every register whose bit is set in XXXX
|
||||
C3000XXx static register XX: below 0x80 read into register x, from 0x80 written from it
|
||||
Every other opcode (the 1 with an offset register, the 5 with one, the float operations of 9, C4 on, FF0, FF1 and
|
||||
FFF) is refused when the file is read, the whole cheat with a line that names the word: never passed over, which
|
||||
would run what follows it as something else.
|
||||
|
||||
As Atmosphere's machine does: registers and saved values are 0 at the start of a frame and are shared by the cheats
|
||||
of one file in that frame (a master code can set up a register for the others); the static registers stay; a value
|
||||
narrower than 8 bytes loaded with opcode 5 leaves the register's upper bytes as they were. Not as it does: a cheat's
|
||||
ifs and loops end with the cheat (one cheat's missing end does not swallow the next); a read outside the game's
|
||||
memory ends that cheat for the frame (a pointer that is not set yet: what follows would work on garbage), a write
|
||||
outside it or into the game's code is dropped; a cheat that runs past MOD_CHEAT_STEPS opcodes in a frame (a loop that
|
||||
never ends) is stopped for that frame.
|
||||
|
||||
The game's memory for a cheat is its program image and its heap (ModCheatLayout): both are committed whole
|
||||
(host/hos_mem.c), so the check is a few compares and no access can fault. The code of the modules is read but never
|
||||
written: it is compiled ahead of time, a write there would change nothing but what the game reads of itself. */
|
||||
#ifndef MOD_CHEATS_H
|
||||
#define MOD_CHEATS_H
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
enum { MOD_CHEAT_NAME = 64, MOD_CHEAT_WHY = 120, MOD_CHEAT_STEPS = 1 << 16 };
|
||||
enum { MOD_CHEAT_MAIN, MOD_CHEAT_HEAP, MOD_CHEAT_ALIAS, MOD_CHEAT_ASLR, MOD_CHEAT_REGIONS };
|
||||
|
||||
/* ---- the game's memory ---- */
|
||||
typedef struct ModCheatLayout {
|
||||
uint64_t image_base, image_end; /* the program image: every module, code and data */
|
||||
uint64_t heap_base, heap_size;
|
||||
const uint64_t (*code)[2]; /* the modules' code, [from, to) each */
|
||||
int ncode;
|
||||
} ModCheatLayout;
|
||||
|
||||
/* 1 when the n bytes at a are the game's: all of them in its heap, or all in its image. No sum that could wrap */
|
||||
static inline int mod_cheat_readable(const ModCheatLayout *l, uint64_t a, uint64_t n) {
|
||||
if (a >= l->heap_base && a - l->heap_base < l->heap_size) return n <= l->heap_size - (a - l->heap_base);
|
||||
if (a >= l->image_base && a < l->image_end) return n <= l->image_end - a;
|
||||
return 0;
|
||||
}
|
||||
/* 1 when the n bytes at a (readable) touch a module's code */
|
||||
static inline int mod_cheat_in_code(const ModCheatLayout *l, uint64_t a, uint64_t n) {
|
||||
for (int i = 0; i < l->ncode; i++)
|
||||
if (a < l->code[i][1] && n > 0 && a + (n - 1) >= l->code[i][0]) return 1;
|
||||
return 0;
|
||||
}
|
||||
static inline int mod_cheat_writable(const ModCheatLayout *l, uint64_t a, uint64_t n) {
|
||||
return mod_cheat_readable(l, a, n) && !mod_cheat_in_code(l, a, n);
|
||||
}
|
||||
|
||||
/* what the interpreter reads and writes through: 1 when done, 0 when the bytes are not the game's (nothing was read or
|
||||
written). base: where each region begins */
|
||||
typedef struct ModCheatMem {
|
||||
void *ctx;
|
||||
int (*read)(void *ctx, uint64_t addr, unsigned n, void *out);
|
||||
int (*write)(void *ctx, uint64_t addr, unsigned n, const void *in);
|
||||
uint64_t base[MOD_CHEAT_REGIONS];
|
||||
} ModCheatMem;
|
||||
|
||||
/* ---- the opcodes ---- */
|
||||
static inline int mod_cheat_width_ok(unsigned w) { return w == 1 || w == 2 || w == 4 || w == 8; }
|
||||
/* an opcode that begins an if: 1, 8, C0 */
|
||||
static inline int mod_cheat_begins_if(uint32_t f) { return f >> 28 == 1 || f >> 28 == 8 || f >> 24 == 0xC0; }
|
||||
|
||||
/* The words of the opcode at w (left of them remain); 0 when it is cut short, not supported or has a bad width, with
|
||||
the reason in why (when given) */
|
||||
static inline int mod_cheat_op_len(const uint32_t *w, int left, char *why, size_t cap) {
|
||||
const uint32_t f = w[0];
|
||||
const unsigned t = f >> 28, width = (f >> 24) & 0xF, d5 = (f >> 20) & 0xF, d4 = (f >> 16) & 0xF, d3 = (f >> 12) & 0xF,
|
||||
d2 = (f >> 8) & 0xF, d1 = (f >> 4) & 0xF;
|
||||
const char *bad = NULL; /* (not supported: what about it) */
|
||||
int n = 1, sized = 0; /* (sized: the opcode has a width that must be 1, 2, 4 or 8) */
|
||||
switch (t) {
|
||||
case 0: n = width == 8 ? 4 : 3, sized = 1; if (d5 >= MOD_CHEAT_REGIONS) bad = "its memory region"; break;
|
||||
case 1:
|
||||
n = width == 8 ? 4 : 3, sized = 1;
|
||||
if (d5 >= MOD_CHEAT_REGIONS) bad = "its memory region";
|
||||
else if (d4 < 1 || d4 > 6) bad = "its comparison";
|
||||
else if (d3 || d2) bad = "the form with an offset register";
|
||||
break;
|
||||
case 2: if (width > 1) bad = "this form of opcode 2"; break;
|
||||
case 3: n = width == 0 ? 2 : 1; if (width > 1) bad = "this form of opcode 3"; break;
|
||||
case 4: n = 3; break;
|
||||
case 5:
|
||||
n = 2, sized = 1;
|
||||
if (d3 > 1) bad = "the form with a second register";
|
||||
else if (!d3 && d5 >= MOD_CHEAT_REGIONS) bad = "its memory region";
|
||||
break;
|
||||
case 6: n = 3, sized = 1; break;
|
||||
case 7: n = 2, sized = 1; if (d3 > 4) bad = "its operation"; break;
|
||||
case 8: break;
|
||||
case 9: n = d2 ? (width == 8 ? 3 : 2) : 1, sized = 1; if (d5 > 9) bad = "its operation (the float ones are not supported)"; break;
|
||||
case 0xA:
|
||||
sized = 1;
|
||||
n = d2 == 2 || d2 == 4 || d2 == 5 ? 2 : 1;
|
||||
if (d2 > 5) bad = "its address form";
|
||||
else if (d2 >= 3 && d1 >= MOD_CHEAT_REGIONS) bad = "its memory region";
|
||||
break;
|
||||
case 0xC:
|
||||
switch (width) { /* (the second digit: which of the C opcodes) */
|
||||
case 0: /* C0TcSX..: the width is the third digit */
|
||||
if (!mod_cheat_width_ok(d5)) {
|
||||
if (why) snprintf(why, cap, "opcode %08X has a bad width", f);
|
||||
return 0;
|
||||
}
|
||||
n = d2 == 0 || d2 == 2 ? 2 : d2 == 4 ? (d5 == 8 ? 3 : 2) : 1;
|
||||
if (d4 < 1 || d4 > 6) bad = "its comparison";
|
||||
else if (d2 > 5) bad = "its operand";
|
||||
else if (d2 <= 1 && d1 >= MOD_CHEAT_REGIONS) bad = "its memory region";
|
||||
break;
|
||||
case 1: if (d1 > 3) bad = "its operation"; break;
|
||||
case 2: if (d5 > 3) bad = "its operation"; break;
|
||||
case 3: break;
|
||||
default: bad = "this opcode"; break;
|
||||
}
|
||||
break;
|
||||
default: bad = "this opcode"; break;
|
||||
}
|
||||
if (bad) {
|
||||
if (why) snprintf(why, cap, "opcode %08X is not supported (%s)", f, bad);
|
||||
return 0;
|
||||
}
|
||||
if (sized && !mod_cheat_width_ok(width)) {
|
||||
if (why) snprintf(why, cap, "opcode %08X has a bad width", f);
|
||||
return 0;
|
||||
}
|
||||
if (n > left) {
|
||||
if (why) snprintf(why, cap, "opcode %08X is cut short (%d of its %d words)", f, left, n);
|
||||
return 0;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* 1 when every opcode of a cheat is one the interpreter runs, else 0 with the reason in why */
|
||||
static inline int mod_cheat_check(const uint32_t *w, int n, char *why, size_t cap) {
|
||||
for (int i = 0; i < n;) {
|
||||
const int len = mod_cheat_op_len(w + i, n - i, why, cap);
|
||||
if (!len) return 0;
|
||||
i += len;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 1 when the cheat has an opcode 0 that stores into a module's code (region main, the offset register left out of the
|
||||
sum: it is 0 unless a cheat before it set it): *at is that address. Such a cheat is a patch of the game's code */
|
||||
static inline int mod_cheat_stores_code(const uint32_t *w, int n, const ModCheatLayout *l, uint64_t main_base, uint64_t *at) {
|
||||
for (int i = 0; i < n;) {
|
||||
const int len = mod_cheat_op_len(w + i, n - i, NULL, 0);
|
||||
if (!len) return 0;
|
||||
if (w[i] >> 28 == 0 && ((w[i] >> 20) & 0xF) == MOD_CHEAT_MAIN) {
|
||||
const uint64_t a = main_base + (((uint64_t)(w[i] & 0xFF) << 32) | w[i + 1]);
|
||||
if (mod_cheat_in_code(l, a, (w[i] >> 24) & 0xF)) {
|
||||
if (at) *at = a;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
i += len;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ---- the interpreter ---- */
|
||||
typedef struct ModCheatState { /* one file's */
|
||||
uint64_t reg[16], saved[16]; /* 0 at the start of a frame (mod_cheat_frame) */
|
||||
uint64_t stat[0x100]; /* the static registers: they stay */
|
||||
} ModCheatState;
|
||||
static inline void mod_cheat_frame(ModCheatState *s) {
|
||||
memset(s->reg, 0, sizeof s->reg);
|
||||
memset(s->saved, 0, sizeof s->saved);
|
||||
}
|
||||
|
||||
enum {
|
||||
MOD_CHEAT_RAN, /* to its end */
|
||||
MOD_CHEAT_STOP_READ, /* stopped at a read outside the game's memory (ModCheatRun.addr) */
|
||||
MOD_CHEAT_STOP_STEPS, /* stopped after MOD_CHEAT_STEPS opcodes */
|
||||
MOD_CHEAT_STOP_BAD /* an opcode the check refuses (the caller runs checked cheats only) */
|
||||
};
|
||||
typedef struct ModCheatRun {
|
||||
int steps, writes, dropped; /* opcodes run; stores done; stores the memory refused */
|
||||
uint64_t addr; /* the read that stopped it, or the last store refused */
|
||||
} ModCheatRun;
|
||||
|
||||
static inline uint64_t mod_cheat_mask(unsigned width, uint64_t v) { return width >= 8 ? v : v & ((1ULL << (8 * width)) - 1); }
|
||||
static inline int mod_cheat_compare(unsigned c, uint64_t a, uint64_t b) {
|
||||
switch (c) {
|
||||
case 1: return a > b;
|
||||
case 2: return a >= b;
|
||||
case 3: return a < b;
|
||||
case 4: return a <= b;
|
||||
case 5: return a == b;
|
||||
case 6: return a != b;
|
||||
default: return 0;
|
||||
}
|
||||
}
|
||||
static inline uint64_t mod_cheat_math(unsigned op, uint64_t a, uint64_t b) {
|
||||
switch (op) {
|
||||
case 0: return a + b;
|
||||
case 1: return a - b;
|
||||
case 2: return a * b;
|
||||
case 3: return b < 64 ? a << b : 0;
|
||||
case 4: return b < 64 ? a >> b : 0;
|
||||
case 5: return a & b;
|
||||
case 6: return a | b;
|
||||
case 7: return ~a;
|
||||
case 8: return a ^ b;
|
||||
default: return a; /* (9: the value itself) */
|
||||
}
|
||||
}
|
||||
/* From i, inside an if that is being skipped: the place after its end, or (to_else) after its else */
|
||||
static inline int mod_cheat_skip(const uint32_t *w, int n, int i, int to_else) {
|
||||
int depth = 0;
|
||||
while (i < n) {
|
||||
const int len = mod_cheat_op_len(w + i, n - i, NULL, 0);
|
||||
if (!len) return n;
|
||||
const uint32_t f = w[i];
|
||||
i += len;
|
||||
if (mod_cheat_begins_if(f)) depth++;
|
||||
else if (f >> 28 == 2 && ((f >> 24) & 0xF) == 0) {
|
||||
if (!depth) return i;
|
||||
depth--;
|
||||
} else if (f >> 28 == 2 && !depth && to_else) return i;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
static inline void mod_cheat_store(const ModCheatMem *m, ModCheatRun *r, uint64_t a, unsigned width, uint64_t v) {
|
||||
if (m->write(m->ctx, a, width, &v)) r->writes++; /* (the value's low bytes: the host is little-endian, as the game) */
|
||||
else r->dropped++, r->addr = a;
|
||||
}
|
||||
|
||||
/* One cheat's code run once: w its n words, s its file's state, keys the buttons held (nn::hid's bits, as the format's
|
||||
masks). Returns MOD_CHEAT_*; *r says what it did */
|
||||
static inline int mod_cheat_run(const uint32_t *w, int n, ModCheatState *s, const ModCheatMem *m, uint64_t keys, ModCheatRun *r) {
|
||||
int loop_top[16] = {0};
|
||||
uint64_t *reg = s->reg;
|
||||
memset(r, 0, sizeof *r);
|
||||
for (int i = 0; i < n;) {
|
||||
if (++r->steps > MOD_CHEAT_STEPS) return MOD_CHEAT_STOP_STEPS;
|
||||
const int len = mod_cheat_op_len(w + i, n - i, NULL, 0);
|
||||
if (!len) return MOD_CHEAT_STOP_BAD;
|
||||
const uint32_t f = w[i];
|
||||
const unsigned t = f >> 28, width = (f >> 24) & 0xF, d5 = (f >> 20) & 0xF, d4 = (f >> 16) & 0xF, d3 = (f >> 12) & 0xF,
|
||||
d2 = (f >> 8) & 0xF, d1 = (f >> 4) & 0xF, d0 = f & 0xF;
|
||||
const uint64_t rel = ((uint64_t)(f & 0xFF) << 32) | (len > 1 ? w[i + 1] : 0); /* (the 40-bit offset of 0, 1 and 5) */
|
||||
const uint64_t small = ((uint64_t)d0 << 32) | (len > 1 ? w[i + 1] : 0); /* (the 36-bit one of A and C0) */
|
||||
uint64_t v = 0;
|
||||
int next = i + len;
|
||||
switch (t) {
|
||||
case 0:
|
||||
mod_cheat_store(m, r, m->base[d5] + rel + reg[d4], width, width == 8 ? (uint64_t)w[i + 2] << 32 | w[i + 3] : w[i + 2]);
|
||||
break;
|
||||
case 1:
|
||||
if (!m->read(m->ctx, m->base[d5] + rel, width, &v) ||
|
||||
!mod_cheat_compare(d4, v, mod_cheat_mask(width, width == 8 ? (uint64_t)w[i + 2] << 32 | w[i + 3] : w[i + 2])))
|
||||
next = mod_cheat_skip(w, n, next, 1);
|
||||
break;
|
||||
case 2: /* (an end: nothing to do. An else reached by running the if's own part: on to its end) */
|
||||
if (width == 1) next = mod_cheat_skip(w, n, next, 0);
|
||||
break;
|
||||
case 3:
|
||||
if (width == 0) {
|
||||
reg[d4] = w[i + 1];
|
||||
loop_top[d4] = next;
|
||||
} else if (--reg[d4]) {
|
||||
next = loop_top[d4];
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
reg[d4] = (uint64_t)w[i + 1] << 32 | w[i + 2];
|
||||
break;
|
||||
case 5: {
|
||||
const uint64_t a = d3 ? reg[d4] + rel : m->base[d5] + rel;
|
||||
if (!m->read(m->ctx, a, width, &v)) {
|
||||
r->addr = a;
|
||||
return MOD_CHEAT_STOP_READ;
|
||||
}
|
||||
memcpy(®[d4], &v, width); /* (the low bytes: the rest of the register stays, as Atmosphere's) */
|
||||
break;
|
||||
}
|
||||
case 6:
|
||||
mod_cheat_store(m, r, reg[d4] + (d2 ? reg[d1] : 0), width, (uint64_t)w[i + 1] << 32 | w[i + 2]);
|
||||
if (d3) reg[d4] += width;
|
||||
break;
|
||||
case 7:
|
||||
reg[d4] = mod_cheat_mask(width, mod_cheat_math(d3, reg[d4], w[i + 1]));
|
||||
break;
|
||||
case 8:
|
||||
if ((keys & (f & 0x0FFFFFFF)) != (f & 0x0FFFFFFF)) next = mod_cheat_skip(w, n, next, 1);
|
||||
break;
|
||||
case 9: {
|
||||
const uint64_t b = d2 ? mod_cheat_mask(width, width == 8 ? (uint64_t)w[i + 1] << 32 | w[i + 2] : w[i + 1]) : reg[d1];
|
||||
reg[d4] = mod_cheat_mask(width, mod_cheat_math(d5, reg[d3], b));
|
||||
break;
|
||||
}
|
||||
case 0xA: {
|
||||
const uint64_t a = d2 == 0 ? reg[d4] : d2 == 1 ? reg[d4] + reg[d1] : d2 == 2 ? reg[d4] + small
|
||||
: d2 == 3 ? m->base[d1] + reg[d4] : d2 == 4 ? m->base[d1] + small : m->base[d1] + reg[d4] + small;
|
||||
mod_cheat_store(m, r, a, width, reg[d5]);
|
||||
if (d3) reg[d4] += width;
|
||||
break;
|
||||
}
|
||||
default: /* 0xC: the check let nothing else through */
|
||||
if (width == 0) { /* C0TcSX..: T is d5, c d4, S d3, X d2 */
|
||||
const uint64_t mine = mod_cheat_mask(d5, reg[d3]);
|
||||
uint64_t other = 0;
|
||||
int have = 1;
|
||||
if (d2 == 4) other = mod_cheat_mask(d5, d5 == 8 ? (uint64_t)w[i + 1] << 32 | w[i + 2] : w[i + 1]);
|
||||
else if (d2 == 5) other = mod_cheat_mask(d5, reg[d1]);
|
||||
else {
|
||||
const uint64_t a = d2 == 0 ? m->base[d1] + small : d2 == 1 ? m->base[d1] + reg[d0]
|
||||
: d2 == 2 ? reg[d1] + small : reg[d1] + reg[d0];
|
||||
have = m->read(m->ctx, a, d5, &other);
|
||||
}
|
||||
if (!have || !mod_cheat_compare(d4, mine, other)) next = mod_cheat_skip(w, n, next, 1);
|
||||
} else if (width == 1) { /* C10D0Sx0: D is d4, S d2, x d1 */
|
||||
if (d1 == 0) reg[d4] = s->saved[d2];
|
||||
else if (d1 == 1) s->saved[d4] = reg[d2];
|
||||
else if (d1 == 2) s->saved[d4] = 0;
|
||||
else reg[d4] = 0;
|
||||
} else if (width == 2) { /* C2x0XXXX: x is d5 */
|
||||
for (unsigned k = 0; k < 16; k++) {
|
||||
if (!(f >> k & 1)) continue;
|
||||
if (d5 == 0) reg[k] = s->saved[k];
|
||||
else if (d5 == 1) s->saved[k] = reg[k];
|
||||
else if (d5 == 2) s->saved[k] = 0;
|
||||
else reg[k] = 0;
|
||||
}
|
||||
} else { /* C3000XXx */
|
||||
const unsigned at = (f >> 4) & 0xFF;
|
||||
if (at < 0x80) reg[d0] = s->stat[at];
|
||||
else s->stat[at] = reg[d0];
|
||||
}
|
||||
break;
|
||||
}
|
||||
i = next;
|
||||
}
|
||||
return MOD_CHEAT_RAN;
|
||||
}
|
||||
|
||||
/* ---- the file ---- */
|
||||
typedef struct ModCheat {
|
||||
char name[MOD_CHEAT_NAME]; /* as the file has it, UTF-8, at most 63 bytes of it */
|
||||
int master; /* {name}: always runs */
|
||||
int line; /* the line its name is on */
|
||||
const uint32_t *words;
|
||||
int n;
|
||||
char why[MOD_CHEAT_WHY]; /* why it cannot run ("" when it can): a word that is none, an opcode */
|
||||
} ModCheat;
|
||||
|
||||
static inline int mod_cheat_space(char ch) { return ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\f' || ch == '\v'; }
|
||||
|
||||
/* The cheats of a file's text (len bytes; a UTF-8 mark at its start is passed over), at most cap of them, their words
|
||||
in pool (pool_cap of them: a file with more loses its last cheats, *cut_short says so). What stands before the first
|
||||
name is passed over; in a cheat, anything but words of 8 hex digits refuses that cheat (why), and so does an opcode
|
||||
the interpreter does not run. Returns how many */
|
||||
static inline int mod_cheat_parse(const char *text, size_t len, ModCheat *out, int cap, uint32_t *pool, int pool_cap, int *cut_short) {
|
||||
int n = 0, used = 0, line = 1;
|
||||
size_t i = len >= 3 && !memcmp(text, "\xEF\xBB\xBF", 3) ? 3 : 0;
|
||||
if (cut_short) *cut_short = 0;
|
||||
while (i < len) {
|
||||
const char ch = text[i];
|
||||
if (ch == '\n') line++;
|
||||
if (mod_cheat_space(ch) || !ch) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (ch == '[' || ch == '{') { /* a name, up to its closing bracket on the same line */
|
||||
const char close = ch == '[' ? ']' : '}';
|
||||
size_t e = i + 1;
|
||||
while (e < len && text[e] != '\n' && text[e] != close) e++;
|
||||
if (e >= len || text[e] != close) { /* (no closing bracket: not a name. The line is passed over) */
|
||||
if (n && !out[n - 1].why[0])
|
||||
snprintf(out[n - 1].why, sizeof out[n - 1].why, "line %d: a name without its closing %c", line, close);
|
||||
i = e;
|
||||
continue;
|
||||
}
|
||||
if (n == cap) {
|
||||
if (cut_short) *cut_short = 1;
|
||||
break;
|
||||
}
|
||||
ModCheat *c = &out[n++];
|
||||
memset(c, 0, sizeof *c);
|
||||
size_t a = i + 1, b = e;
|
||||
while (a < b && mod_cheat_space(text[a])) a++;
|
||||
while (b > a && mod_cheat_space(text[b - 1])) b--;
|
||||
size_t k = b - a < sizeof c->name - 1 ? b - a : sizeof c->name - 1;
|
||||
while (k && k < b - a && ((unsigned char)text[a + k] & 0xC0) == 0x80) k--; /* (not in the middle of a letter) */
|
||||
memcpy(c->name, text + a, k);
|
||||
c->master = ch == '{';
|
||||
c->line = line;
|
||||
c->words = pool + used;
|
||||
i = e + 1;
|
||||
continue;
|
||||
}
|
||||
size_t e = i; /* a word */
|
||||
while (e < len && !mod_cheat_space(text[e]) && text[e] != '[' && text[e] != '{') e++;
|
||||
if (e == i) e++;
|
||||
if (n) {
|
||||
ModCheat *c = &out[n - 1];
|
||||
uint32_t v = 0;
|
||||
int hex = e - i == 8;
|
||||
for (size_t k = i; hex && k < e; k++) {
|
||||
const char d = text[k];
|
||||
const int digit = d >= '0' && d <= '9' ? d - '0' : d >= 'a' && d <= 'f' ? d - 'a' + 10 : d >= 'A' && d <= 'F' ? d - 'A' + 10 : -1;
|
||||
if (digit < 0) hex = 0;
|
||||
else v = v << 4 | (uint32_t)digit;
|
||||
}
|
||||
if (!hex) {
|
||||
if (!c->why[0]) { /* (the first such word is the one named) */
|
||||
char word[25];
|
||||
const size_t wn = e - i < 24 ? e - i : 24;
|
||||
memcpy(word, text + i, wn);
|
||||
word[wn] = 0;
|
||||
snprintf(c->why, sizeof c->why, "line %d: \"%s\" is not a code word (8 hex digits)", line, word);
|
||||
}
|
||||
} else if (used == pool_cap) {
|
||||
if (cut_short) *cut_short = 1;
|
||||
if (!c->why[0]) snprintf(c->why, sizeof c->why, "the file has too many code words");
|
||||
} else {
|
||||
pool[used++] = v;
|
||||
c->n++;
|
||||
}
|
||||
}
|
||||
i = e;
|
||||
}
|
||||
for (int k = 0; k < n; k++)
|
||||
if (!out[k].why[0]) mod_cheat_check(out[k].words, out[k].n, out[k].why, sizeof out[k].why);
|
||||
return n;
|
||||
}
|
||||
|
||||
/* A cheat that does nothing: no code, or the lone end (20000000) cheat files give their headings */
|
||||
static inline int mod_cheat_is_heading(const ModCheat *c) {
|
||||
return !c->why[0] && (c->n == 0 || (c->n == 1 && c->words[0] == 0x20000000u));
|
||||
}
|
||||
|
||||
/* FNV-1a 32 of a string: a cheat's switch is kept by the number of "<mod>/<name>" (mod_cheats.c) */
|
||||
static inline uint32_t mod_cheat_hash(const char *s) {
|
||||
uint32_t h = 0x811C9DC5u;
|
||||
for (; *s; s++) h = (h ^ (uint8_t)*s) * 0x01000193u;
|
||||
return h;
|
||||
}
|
||||
#endif
|
||||
128
host/mod_code.h
Normal file
128
host/mod_code.h
Normal file
@@ -0,0 +1,128 @@
|
||||
/* (Triforce) The mods' code patches (.ips, .pchtxt) and what the game was built with.
|
||||
|
||||
The game's code is compiled ahead of time, so a patch of it is applied when the game is built (nx2/code_mods.py), and
|
||||
the build writes what it did into the game folder, triforce_code_mods.txt: a line per patch file, tab separated,
|
||||
|
||||
applied <FNV-1a 64 of the file, 16 hex digits> <its size> <its path under the mods folder> <module> <bytes changed>
|
||||
refused <the same three> <why>
|
||||
|
||||
(lines that start with # are notes). At its start the game holds that against the mods folder as it is now
|
||||
(host/mods.c): a patch file the build did not have, or had with other contents, waits for the next build, and so does
|
||||
one that is built in and is gone from the folder or switched off since. Nothing here changes the game: it is what the
|
||||
log says, so that a mod that seems to do nothing can be told from one that waits for a build.
|
||||
|
||||
No Windows calls and no host state: tests/test_mod_code.py compiles it alone. */
|
||||
#ifndef MOD_CODE_H
|
||||
#define MOD_CODE_H
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MOD_CODE_RECORD "triforce_code_mods.txt"
|
||||
enum { MOD_CODE_PATH = 520, MOD_CODE_WHY = 200 };
|
||||
|
||||
typedef struct ModCodeEntry {
|
||||
int applied; /* 0: the build refused it (why) */
|
||||
uint64_t fnv, size;
|
||||
char path[MOD_CODE_PATH]; /* "<mod>/exefs/<file>", as the build wrote it */
|
||||
char why[MOD_CODE_WHY];
|
||||
int seen; /* the mods folder has the file now (mod_code_state) */
|
||||
} ModCodeEntry;
|
||||
|
||||
/* what a patch file of the mods folder is to the game as built */
|
||||
enum {
|
||||
MOD_CODE_BUILT, /* built in, as it is now */
|
||||
MOD_CODE_CHANGED, /* built in with other contents: the next build has it as it is now */
|
||||
MOD_CODE_REFUSED, /* the build refused it as it is now (why) */
|
||||
MOD_CODE_WAITS /* the build did not have it, or refused other contents: the next build looks at it */
|
||||
};
|
||||
|
||||
static inline uint64_t mod_code_fnv(const void *data, size_t n) {
|
||||
const uint8_t *p = (const uint8_t *)data;
|
||||
uint64_t h = 0xcbf29ce484222325ull;
|
||||
for (size_t i = 0; i < n; i++) h = (h ^ p[i]) * 0x100000001b3ull;
|
||||
return h;
|
||||
}
|
||||
|
||||
static inline int mod_code_is_patch(const char *name) { /* (a file name: .ips or .pchtxt, any case) */
|
||||
const size_t n = strlen(name);
|
||||
static const char *const ends[] = {".ips", ".pchtxt"};
|
||||
for (int k = 0; k < 2; k++) {
|
||||
const size_t m = strlen(ends[k]);
|
||||
if (n <= m) continue;
|
||||
size_t i = 0;
|
||||
while (i < m && (name[n - m + i] | 0x20) == ends[k][i]) i++;
|
||||
if (i == m) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The record's entries from its text (changed in place: a line's fields are cut apart), at most cap of them. A line
|
||||
that is not one of the two kinds, or whose numbers cannot be read, is passed over. Returns how many */
|
||||
static inline int mod_code_parse(char *text, ModCodeEntry *out, int cap) {
|
||||
int n = 0;
|
||||
for (char *line = text; line && *line && n < cap;) {
|
||||
char *end = strchr(line, '\n'), *next = end ? end + 1 : NULL;
|
||||
if (end) *end = 0;
|
||||
size_t len = strlen(line);
|
||||
if (len && line[len - 1] == '\r') line[--len] = 0;
|
||||
char *field[6] = {0};
|
||||
int nf = 0;
|
||||
if (*line != '#')
|
||||
for (char *p = line; nf < 6; nf++) {
|
||||
field[nf] = p;
|
||||
char *tab = strchr(p, '\t');
|
||||
if (!tab) { nf++; break; }
|
||||
*tab = 0;
|
||||
p = tab + 1;
|
||||
}
|
||||
const int applied = nf >= 5 && !strcmp(field[0], "applied"), refused = nf >= 5 && !strcmp(field[0], "refused");
|
||||
if (applied || refused) {
|
||||
char *e1 = NULL, *e2 = NULL;
|
||||
ModCodeEntry *e = &out[n];
|
||||
memset(e, 0, sizeof *e);
|
||||
e->applied = applied;
|
||||
e->fnv = strtoull(field[1], &e1, 16);
|
||||
e->size = strtoull(field[2], &e2, 10);
|
||||
if (*field[1] && !*e1 && *field[2] && !*e2 && *field[3] && strlen(field[3]) < sizeof e->path) {
|
||||
memcpy(e->path, field[3], strlen(field[3]) + 1);
|
||||
if (refused) {
|
||||
size_t wn = strlen(field[4]);
|
||||
if (wn > sizeof e->why - 1) wn = sizeof e->why - 1;
|
||||
memcpy(e->why, field[4], wn);
|
||||
e->why[wn] = 0;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
}
|
||||
line = next;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* A patch file that is in the mods folder now (path as the build names it, its contents' number and size) against the
|
||||
record: MOD_CODE_*, and for MOD_CODE_REFUSED the build's reason in *why. The entry is marked seen */
|
||||
static inline int mod_code_state(ModCodeEntry *e, int n, const char *path, uint64_t fnv, uint64_t size, const char **why) {
|
||||
if (why) *why = "";
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (strcmp(e[i].path, path)) continue;
|
||||
e[i].seen = 1;
|
||||
const int same = e[i].fnv == fnv && e[i].size == size;
|
||||
if (e[i].applied) return same ? MOD_CODE_BUILT : MOD_CODE_CHANGED;
|
||||
if (!same) return MOD_CODE_WAITS;
|
||||
if (why) *why = e[i].why;
|
||||
return MOD_CODE_REFUSED;
|
||||
}
|
||||
return MOD_CODE_WAITS;
|
||||
}
|
||||
|
||||
/* The next entry from *at on that is built into the game and that mod_code_state was not asked about: its file is gone
|
||||
from the mods folder, or its mod is switched off. NULL when there is no more */
|
||||
static inline const ModCodeEntry *mod_code_gone(const ModCodeEntry *e, int n, int *at) {
|
||||
while (*at < n) {
|
||||
const ModCodeEntry *x = &e[(*at)++];
|
||||
if (x->applied && !x->seen) return x;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
208
host/mods.c
208
host/mods.c
@@ -1,17 +1,23 @@
|
||||
/* Mods, in the layout emulators use, so RomFS mods made for them work as they are:
|
||||
<mods>/<name>/romfs/... files that replace (or add to) the game's RomFS
|
||||
<mods>/<name>/exefs/... .ips and .pchtxt patches of the game's code: applied when the game is built
|
||||
<mods>/<name>/cheats/<id>.txt a cheat file for the build of the game with that id: run by host/mod_cheats.c
|
||||
<mods> is the "mods" folder beside the executable, or HOS_MODS=<dir>. A mod may sit deeper, as emulators keep them
|
||||
(<mods>/contents/<title id>/<name>/romfs): any folder holding a romfs or exefs folder is a mod. Folders whose names
|
||||
start with '-' or '_' are left out (off). When two mods have the same file, the one whose path sorts last wins (name
|
||||
them 01-..., 02-... to order them).
|
||||
(<mods>/contents/<title id>/<name>/romfs): any folder holding a romfs, exefs or cheats folder is a mod. Folders whose
|
||||
names start with '-' or '_' are left out (off). When two mods have the same file, the one whose path sorts last wins
|
||||
(name them 01-..., 02-... to order them).
|
||||
Every mod file is indexed once at the start (no disk lookups per open); nn_fs.c asks hos_mods_romfs for each rom:
|
||||
path and lists a folder's mod-only entries with the game's own (hos_mods_dir_extras). HOS_FS_OVERLAY (nn_fs.c) is
|
||||
looked at after this, so a test overlay wins over mods.
|
||||
exefs patches (.pchtxt, .ips) are not applied: the code is compiled ahead of time from the original instructions, and
|
||||
the game folder has no module map (image.json) to place data patches with. A mod's exefs folder is logged as left out.
|
||||
(Triforce) exefs patches cannot be applied here: the code is compiled ahead of time. A build applies them to the code it
|
||||
translates (nx2/code_mods.py walks the mods folder as find_mods below does) and writes what it did into the game
|
||||
folder; the start holds that against the mods folder as it is now (mod_code.h) and says in the log which patch files
|
||||
are built in and which wait for a build: one added or edited since, and one that is built in and has been taken away
|
||||
or switched off, which the game has until it is built again.
|
||||
From Skybound's loader (NotRexed/SkyboundTotk a64-kit/hos/mods.c, MIT): its RomFS part. */
|
||||
#include "hos.h"
|
||||
#include "hos_paths.h"
|
||||
#include "mod_code.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -19,7 +25,7 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
typedef struct { char *key; char *host; uint64_t size; int is_dir, logged; } Entry; /* key: romfs path, lower case, '/' */
|
||||
typedef struct { char *key; char *host; uint64_t size; int is_dir, logged; volatile uint64_t need; } Entry; /* key: romfs path, lower case, '/'; need: hos_mods_table_size's */
|
||||
static Entry *table;
|
||||
static size_t table_cap, table_n;
|
||||
|
||||
@@ -109,7 +115,7 @@ static int index_romfs(const char *host_dir, const char *rel, int depth) {
|
||||
return files;
|
||||
}
|
||||
|
||||
/* the mod folders under root, sorted (a mod: a folder holding romfs or exefs) */
|
||||
/* the mod folders under root, sorted (a mod: a folder holding romfs, exefs or cheats) */
|
||||
typedef struct { char path[MAX_PATH]; char name[MAX_PATH]; } Mod;
|
||||
static Mod *mods;
|
||||
static int nmods, cap_mods;
|
||||
@@ -119,7 +125,9 @@ static void find_mods(const char *dir, const char *name, int depth) {
|
||||
const int has_romfs = GetFileAttributesA(probe) != INVALID_FILE_ATTRIBUTES;
|
||||
snprintf(probe, sizeof probe, "%s\\exefs", dir);
|
||||
const int has_exefs = GetFileAttributesA(probe) != INVALID_FILE_ATTRIBUTES;
|
||||
if ((has_romfs || has_exefs) && depth > 0) {
|
||||
snprintf(probe, sizeof probe, "%s\\cheats", dir); /* (Triforce: a mod of cheats alone) */
|
||||
const int has_cheats = GetFileAttributesA(probe) != INVALID_FILE_ATTRIBUTES;
|
||||
if ((has_romfs || has_exefs || has_cheats) && depth > 0) {
|
||||
if (nmods == cap_mods) mods = (Mod *)realloc(mods, sizeof *mods * (size_t)(cap_mods = cap_mods ? cap_mods * 2 : 16));
|
||||
snprintf(mods[nmods].path, MAX_PATH, "%s", dir);
|
||||
snprintf(mods[nmods].name, MAX_PATH, "%s", name);
|
||||
@@ -147,9 +155,113 @@ static void find_mods(const char *dir, const char *name, int depth) {
|
||||
}
|
||||
static int mod_cmp(const void *a, const void *b) { return _stricmp(((const Mod *)a)->name, ((const Mod *)b)->name); }
|
||||
|
||||
/* hos_main.c, once the image is loaded (image_path: kept for exefs patches, which are not applied here) */
|
||||
void hos_mods_init(const char *image_path) {
|
||||
(void)image_path;
|
||||
/* (Triforce) a file's bytes, at most `most` of them (a larger file: NULL), with a 0 after them; freed by the caller */
|
||||
static char *read_whole(const char *path, size_t most, size_t *len) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
char *data = NULL;
|
||||
size_t n = 0, cap = 0;
|
||||
for (;;) { /* (in pieces: no room taken for more than the file has) */
|
||||
if (n == cap) {
|
||||
cap = cap ? cap * 2 : 4096;
|
||||
char *more = cap <= most + 4096 ? (char *)realloc(data, cap + 1) : NULL;
|
||||
if (!more) {
|
||||
free(data);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
data = more;
|
||||
}
|
||||
const size_t got = fread(data + n, 1, cap - n, f);
|
||||
if (!got) break;
|
||||
n += got;
|
||||
}
|
||||
fclose(f);
|
||||
if (n > most) {
|
||||
free(data);
|
||||
return NULL;
|
||||
}
|
||||
data[n] = 0;
|
||||
*len = n;
|
||||
return data;
|
||||
}
|
||||
|
||||
/* (Triforce) The mods' code patches against what the game was built with (mod_code.h, nx2/code_mods.py): the build's
|
||||
record from the game folder, a line in the log for each patch file of a mod, and how many wait for a build */
|
||||
enum { BUILT_MOST = 512 };
|
||||
static ModCodeEntry *built;
|
||||
static int nbuilt, code_waits;
|
||||
static void code_record_load(void) {
|
||||
char path[MAX_PATH + 32];
|
||||
hos_game_path_a(path, MAX_PATH);
|
||||
char *slash = (char *)last_sep(path);
|
||||
if (!slash) return;
|
||||
snprintf(slash + 1, sizeof path - (size_t)(slash + 1 - path), "%s", MOD_CODE_RECORD);
|
||||
size_t len = 0;
|
||||
char *text = read_whole(path, 1 << 20, &len);
|
||||
if (!text) return; /* (no record: a game built without code patches) */
|
||||
built = (ModCodeEntry *)calloc(BUILT_MOST, sizeof *built);
|
||||
if (built) nbuilt = mod_code_parse(text, built, BUILT_MOST);
|
||||
free(text);
|
||||
}
|
||||
static void 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;
|
||||
do {
|
||||
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || !mod_code_is_patch(fd.cFileName)) continue;
|
||||
char path[MAX_PATH], rel[MOD_CODE_PATH];
|
||||
snprintf(path, sizeof path, "%s\\exefs\\%s", mod->path, fd.cFileName);
|
||||
snprintf(rel, sizeof rel, "%s/exefs/%s", mod->name, fd.cFileName);
|
||||
size_t len = 0;
|
||||
char *data = read_whole(path, 16 << 20, &len);
|
||||
const char *why = "";
|
||||
const int state = data ? mod_code_state(built, nbuilt, rel, mod_code_fnv(data, len), len, &why) : MOD_CODE_WAITS;
|
||||
free(data);
|
||||
if (state == MOD_CODE_BUILT) hos_log("mods: %s is built into the game", rel);
|
||||
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;
|
||||
} while (FindNextFileA(f, &fd));
|
||||
FindClose(f);
|
||||
}
|
||||
/* (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) {
|
||||
int at = 0;
|
||||
for (const ModCodeEntry *e; (e = mod_code_gone(built, nbuilt, &at)) != NULL; code_waits++)
|
||||
hos_log("mods: %s is built into the game, and is gone from the mods folder or switched off: the game has it until it is built again",
|
||||
e->path);
|
||||
if (code_waits)
|
||||
hos_log("mods: %d code patches wait for a build: they are applied when the game is built. Build it again for the mods folder as "
|
||||
"it is now (Triforce Setup, or nx2/make_game.py --update with the game folder)", code_waits);
|
||||
}
|
||||
|
||||
/* (Triforce) a mod's cheat file for this build of the game, cheats\<the first 16 digits of main's build id>.txt (the
|
||||
name's case does not matter), to host/mod_cheats.c; one named after another build is said and left */
|
||||
static void cheat_files(const Mod *mod) {
|
||||
void hos_mod_cheats_add(const char *mod, const char *path);
|
||||
char pat[MAX_PATH], id[17];
|
||||
snprintf(id, sizeof id, "%.16s", hos_main_build_id);
|
||||
snprintf(pat, sizeof pat, "%s\\cheats\\*", mod->path);
|
||||
WIN32_FIND_DATAA fd;
|
||||
HANDLE f = FindFirstFileA(pat, &fd);
|
||||
if (f == INVALID_HANDLE_VALUE) return;
|
||||
do {
|
||||
const size_t n = strlen(fd.cFileName);
|
||||
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || n != 20 || _stricmp(fd.cFileName + 16, ".txt")) continue;
|
||||
if (strspn(fd.cFileName, "0123456789abcdefABCDEF") != 16) continue; /* (not named after a build: not a cheat file) */
|
||||
char path[MAX_PATH];
|
||||
snprintf(path, sizeof path, "%s\\cheats\\%s", mod->path, fd.cFileName);
|
||||
if (strlen(id) == 16 && !_strnicmp(fd.cFileName, id, 16)) hos_mod_cheats_add(mod->name, path);
|
||||
else hos_log("mods: %s/cheats/%s is for another build of the game (this one is %s): not read", mod->name, fd.cFileName, id);
|
||||
} while (FindNextFileA(f, &fd));
|
||||
FindClose(f);
|
||||
}
|
||||
|
||||
static void mods_scan(void) {
|
||||
char root[MAX_PATH];
|
||||
if (getenv("HOS_MODS")) {
|
||||
snprintf(root, sizeof root, "%s", getenv("HOS_MODS"));
|
||||
@@ -170,14 +282,23 @@ void hos_mods_init(const char *image_path) {
|
||||
char dir[MAX_PATH];
|
||||
snprintf(dir, sizeof dir, "%s\\romfs", mods[i].path);
|
||||
const int files = index_romfs(dir, "", 0);
|
||||
snprintf(dir, sizeof dir, "%s\\exefs", mods[i].path);
|
||||
if (GetFileAttributesA(dir) != INVALID_FILE_ATTRIBUTES)
|
||||
hos_log("mods: %s: exefs patches are left out (the code is compiled ahead of time)", mods[i].name);
|
||||
code_patches(&mods[i]); /* (Triforce: its exefs patches, built in or waiting) */
|
||||
cheat_files(&mods[i]); /* (and its cheat file) */
|
||||
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);
|
||||
}
|
||||
|
||||
/* hos_main.c, once the image is loaded (image_path: not used; exefs patches are applied when the game is built) */
|
||||
void hos_mods_init(const char *image_path) {
|
||||
void hos_mod_cheats_ready(void);
|
||||
(void)image_path;
|
||||
code_record_load();
|
||||
mods_scan();
|
||||
code_patches_gone();
|
||||
hos_mod_cheats_ready(); /* (mod_cheats.c: every mod's cheat file is read) */
|
||||
}
|
||||
|
||||
/* nn_fs.c: the mod file for a RomFS path ("UI/x.zs", no mount, no leading slash), or 0 */
|
||||
int hos_mods_romfs(const char *rel, char *out, size_t cap) {
|
||||
char key[MAX_PATH];
|
||||
@@ -193,6 +314,65 @@ int hos_mods_romfs(const char *rel, char *out, size_t cap) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ((Triforce) 11-17) The size a zstd file holds once unpacked: its frame header's content size (0: not a zstd frame, or
|
||||
the size not written) */
|
||||
static uint64_t zs_content_size(const char *host_path) {
|
||||
FILE *f = fopen(host_path, "rb");
|
||||
if (!f) return 0;
|
||||
uint8_t h[18] = {0};
|
||||
const size_t n = fread(h, 1, sizeof h, f);
|
||||
fclose(f);
|
||||
if (n < 6 || h[0] != 0x28 || h[1] != 0xb5 || h[2] != 0x2f || h[3] != 0xfd) return 0;
|
||||
const int fcs_flag = h[4] >> 6, single = h[4] >> 5 & 1, dict = h[4] & 3;
|
||||
const size_t at = 5 + (single ? 0 : 1) + (size_t)(dict == 3 ? 4 : dict);
|
||||
const size_t len = fcs_flag == 0 ? (single ? 1 : 0) : fcs_flag == 1 ? 2 : fcs_flag == 2 ? 4 : 8;
|
||||
if (!len || at + len > n) return 0;
|
||||
uint64_t v = 0;
|
||||
for (size_t i = 0; i < len; i++) v |= (uint64_t)h[at + i] << (8 * i);
|
||||
return len == 2 ? v + 256 : v;
|
||||
}
|
||||
|
||||
/* ((Triforce) 11-17) The memory the game makes for loading a file is the ResourceSizeTable's figure for its path (the
|
||||
lifted f_801b6340 asks the table, f_836e3d30, and this is called with the answer: nx2/oot_patches.py patch_mod_sizes).
|
||||
For a file a mod serves, the table's figure (the game's, or the one the mod ships) can be too small: a model with more
|
||||
materials and shapes than the one it replaces needs more beside its own bytes, and the game does not check the
|
||||
allocations that then fail (the Dark Link mod's Deku Shield and Slingshot crashed loading a child save, a write to
|
||||
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 */
|
||||
uint64_t hos_mods_table_size(uint64_t size, uint64_t path) {
|
||||
if (!table_n || !path) return size;
|
||||
const char *p = (const char *)GP(path);
|
||||
char rel[MAX_PATH], key[MAX_PATH];
|
||||
size_t i = 0;
|
||||
for (; i + 4 < sizeof rel && p[i]; i++) rel[i] = p[i];
|
||||
rel[i] = 0;
|
||||
snprintf(key, sizeof key, "%s.zs", rel); /* (the table names the unpacked file; the RomFS has it packed) */
|
||||
lower_slash(key);
|
||||
Entry *e = find(key);
|
||||
int packed = 1;
|
||||
if (!e) {
|
||||
snprintf(key, sizeof key, "%s", rel);
|
||||
lower_slash(key);
|
||||
e = find(key), packed = 0;
|
||||
}
|
||||
if (!e || e->is_dir) return size;
|
||||
uint64_t need = e->need;
|
||||
if (!need) {
|
||||
const uint64_t real = packed ? zs_content_size(e->host) : e->size;
|
||||
if (!real) return size; /* (a packed file whose header has no size: the table's figure, as the game has it) */
|
||||
const uint64_t room = real / 8 > (128u << 10) ? real / 8 : (128u << 10);
|
||||
need = (real + room + (64u << 10) + 0xfff) & ~(uint64_t)0xfff;
|
||||
if (need > 0xfffff000u) need = 0xfffff000u;
|
||||
e->need = need;
|
||||
static volatile LONG told;
|
||||
if (need > (uint32_t)size && InterlockedIncrement(&told) <= 100)
|
||||
hos_log("mods: %s gets %llu bytes to load in (the size table's %llu; the file %llu unpacked)", rel,
|
||||
(unsigned long long)need, (unsigned long long)(uint32_t)size, (unsigned long long)real);
|
||||
}
|
||||
return need > (uint32_t)size ? need : size;
|
||||
}
|
||||
|
||||
/* prompts.c: 1 when a mod has a file whose key (lower case, '/') starts with prefix and ends with suffix */
|
||||
int hos_mods_has(const char *prefix, const char *suffix) {
|
||||
const size_t pn = strlen(prefix), sn = strlen(suffix);
|
||||
|
||||
@@ -108,13 +108,14 @@ static int handheld(void) {
|
||||
return v;
|
||||
}
|
||||
/* (hookshot) The Insert menu's Output resolution (output_res=, HOS_OUTPUT_RES over it), docked only: 0 the game's 2048 x 1152,
|
||||
1 2560 x 1440, 2 3840 x 2160, 3 1920 x 1080, 4 5120 x 1440, 5 5120 x 2880. The game's frame is its display size (2560 x 1440 as shipped;
|
||||
1 2560 x 1440, 2 3840 x 2160, 3 1920 x 1080, 4 5120 x 1440, 5 5120 x 2880, 6 5120 x 2160 ((Triforce) after the others: a
|
||||
saved number keeps its size). The game's frame is its display size (2560 x 1440 as shipped;
|
||||
nx2/oot_patches.py apply_output_size asks hos_output_size) times its graphics scale (GetCurrentGraphicsScale: 0 x1, 1 x0.8,
|
||||
2 x0.667). Here it saw scale 1 (x0 left at the call before's answer), so 2048 x 1152; each explicit size uses x1. */
|
||||
static int output_mode(void) {
|
||||
static int v = -1;
|
||||
if (v < 0) {
|
||||
static const char *const names[] = {"2048x1152 (the game's)", "2560x1440", "3840x2160", "1920x1080", "5120x1440", "5120x2880"};
|
||||
static const char *const names[] = {"2048x1152 (the game's)", "2560x1440", "3840x2160", "1920x1080", "5120x1440", "5120x2880", "5120x2160"};
|
||||
v = getenv("HOS_OUTPUT_RES") ? atoi(getenv("HOS_OUTPUT_RES")) : ini_option("output_res", 0);
|
||||
if ((unsigned)v >= sizeof names / sizeof *names || handheld()) v = 0;
|
||||
hos_log("oe: output resolution %s", names[v]);
|
||||
@@ -127,6 +128,7 @@ uint32_t hos_output_size(int height) {
|
||||
case 3: return height ? 1080 : 1920;
|
||||
case 4: return height ? 1440 : 5120;
|
||||
case 5: return height ? 2880 : 5120;
|
||||
case 6: return height ? 2160 : 5120; /* ((Triforce) 5120 x 2160) */
|
||||
default: return height ? 1440 : 2560;
|
||||
}
|
||||
}
|
||||
@@ -151,7 +153,9 @@ uint64_t hos_rt_pool_size(uint64_t game) {
|
||||
if (wide_px > px) px = wide_px;
|
||||
const uint64_t scaled = px > 2560 * 1440 ? (game * px / (2560 * 1440) + 0xfffff) & ~0xfffffULL : game;
|
||||
const uint64_t minimum = 512ULL << 20, five_k = 900ULL << 20;
|
||||
const int is_five_k = hos_output_size(0) == 5120 && hos_output_size(1) == 2880;
|
||||
/* ((Triforce) and the 5120 x 2160 output: scaled, its pool is 915 MB, more than any pool known to pass the game's reservation
|
||||
at a 5120-wide output; 900 MB ran the 3D at the full 5120 x 2880, a third more pixels) */
|
||||
const int is_five_k = hos_output_size(0) == 5120 && hos_output_size(1) >= 2160;
|
||||
extern int hos_scene_rgba16f(void);
|
||||
/* (local, RenoDX port) the scene's colour target at 8 bytes a pixel in place of 4: room for it and its copies, at 3840 x 2160 33 MB each */
|
||||
const uint64_t rgba16f = hos_scene_rgba16f() ? 128ULL << 20 : 0;
|
||||
@@ -162,8 +166,8 @@ uint64_t hos_rt_pool_size(uint64_t game) {
|
||||
if (!logged++) {
|
||||
hos_log("applet: render target pool %llu MB (the game's %llu MB)", (unsigned long long)(v >> 20), (unsigned long long)(game >> 20));
|
||||
if (v != asked)
|
||||
hos_log("applet: render target pool: %llu MB asked (the 5120x2880 output with an RGBA16F scene), kept to %llu MB: more fails the game's reservation at boot",
|
||||
(unsigned long long)(asked >> 20), (unsigned long long)(v >> 20));
|
||||
hos_log("applet: render target pool: %llu MB asked (the %ux%u output), kept to %llu MB: more fails the game's reservation at boot",
|
||||
(unsigned long long)(asked >> 20), hos_output_size(0), hos_output_size(1), (unsigned long long)(v >> 20));
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#endif
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_OPT_VOLUME: the menu's Master volume) */
|
||||
#include "hos_paths.h" /* (the game folder: triforce_options.ini, au_menu_output) */
|
||||
#include "menu_sound.h" /* ((Triforce) the options' own sounds, added to each block: render_frame) */
|
||||
|
||||
void hos_system_event_init(uint64_t key, int autoclear);
|
||||
void hos_system_event_signal(uint64_t key);
|
||||
@@ -589,6 +590,9 @@ static void render_frame(int16_t *pcm, int channels) { /* 240 frames of the de
|
||||
lim_env = want < lim_env ? want : lim_env + (want - lim_env) * 0.0004f;
|
||||
for (int ch = 0; ch < nch; ch++) out[ch][i] *= lim_in * lim_env * lim_out;
|
||||
}
|
||||
/* (Triforce) the options' own sounds, the game's interface sounds (menu_sound.c), into the front left and right:
|
||||
after the limiter, which is the game's mix's own, and before the Master volume, which is over them too */
|
||||
hos_menu_sound_frame(out[0], out[1], FRAME);
|
||||
{ /* (Triforce) the Insert menu's Master volume, over everything the game mixed and after the limiter, so the
|
||||
limiter works on the game's own levels. A change is ramped across the frame (5 ms): no click */
|
||||
static float master_now = -1.0f;
|
||||
@@ -1566,7 +1570,9 @@ HLE(_ZN2nn5audio16AppendWaveBufferEPNS0_9VoiceTypeEPKNS0_10WaveBufferE) {
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (f) {
|
||||
fwrite(v->coefs, 1, 32, f);
|
||||
const int io = hos_guest_io_begin(GP(w.buffer), (uint64_t)w.size); /* (Triforce: rx_backend.c) */
|
||||
fwrite(GP(w.buffer), 1, (size_t)w.size, f);
|
||||
hos_guest_io_end(io);
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
144
host/nn_fs.c
144
host/nn_fs.c
@@ -186,6 +186,35 @@ static int host_path(const char *guest_mem, char *out, size_t cap, int *writable
|
||||
/* For other OS layer parts that open game files themselves (videos): "mount:/a/b" -> host path, 0 if unknown. */
|
||||
int hos_fs_host_path(const char *guest, char *out, size_t cap) { return host_path(guest, out, cap, NULL); }
|
||||
|
||||
/* (Triforce) A file of the game's RomFS whole, for a part of the host that reads one itself on a thread that may wait for
|
||||
the disk (menu_sound.c: the game's interface sounds). rel is its path from the RomFS root ("Sound/Resource/Stream/
|
||||
x.bwav"); a mod's file is read where a mod has it (mods.c), as the game's own opens find it. From the RomFS folder,
|
||||
not through the mounts: the game mounts rom: as it starts, and this may be asked before. NULL when the file is not
|
||||
there, is empty or has more than `most` bytes; freed by the caller */
|
||||
uint8_t *hos_fs_rom_file(const char *rel, uint64_t most, uint64_t *size) {
|
||||
char path[HOS_FS_PATH];
|
||||
if (!hos_romfs_dir || !guest_path_ok(rel)) return NULL;
|
||||
if (!hos_mods_romfs(rel, path, sizeof path) &&
|
||||
(size_t)snprintf(path, sizeof path, "%s/%s", hos_romfs_dir, rel) >= sizeof path)
|
||||
return NULL;
|
||||
for (char *p = path; *p; p++) if (*p == '/') *p = '\\'; /* (as host_path) */
|
||||
const HANDLE h = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE) return NULL;
|
||||
LARGE_INTEGER sz = {0};
|
||||
uint8_t *data = NULL;
|
||||
if (GetFileSizeEx(h, &sz) && sz.QuadPart > 0 && (uint64_t)sz.QuadPart <= most && (data = (uint8_t *)malloc((size_t)sz.QuadPart)) != NULL) {
|
||||
uint64_t done = 0;
|
||||
DWORD got = 0;
|
||||
while (done < (uint64_t)sz.QuadPart && ReadFile(h, data + done, (DWORD)((uint64_t)sz.QuadPart - done), &got, NULL) && got)
|
||||
done += got;
|
||||
if (done == (uint64_t)sz.QuadPart) *size = done;
|
||||
else free(data), data = NULL;
|
||||
}
|
||||
CloseHandle(h);
|
||||
return data;
|
||||
}
|
||||
|
||||
/* (hookshot) a guest string: "" unless it lies in guest memory and ends within FS_PATH_MAX bytes */
|
||||
static const char *gstr(uint64_t a) {
|
||||
if (!a || a >= (uint64_t)HOS_SPACE_END) return "";
|
||||
@@ -263,12 +292,16 @@ HLE(_ZN2nn2fs18IsSaveDataExistingENS_13ApplicationIdERKNS_7account3UidE) {
|
||||
working copy as it is then) runs on the game's thread; the second (written out to the disk, put in place, the backup) on a
|
||||
thread of its own, which the next Commit, a mount of the save and the game's end wait for. */
|
||||
#include "save_tx.h"
|
||||
#include "save_tx_share.h" /* (the newest save read for an export, an import that waits for the next start: below) */
|
||||
typedef struct { char name[32]; char uid[40]; SaveTxPaths p; } TxMount;
|
||||
static TxMount tx_mounts[4];
|
||||
static int ntx;
|
||||
static SRWLOCK tx_lock = SRWLOCK_INIT; /* (tx_mounts, tx_thread and every step of a transaction) */
|
||||
static HANDLE tx_thread; /* (the thread finishing the last Commit, or NULL) */
|
||||
static SaveTxPaths tx_finish_paths;
|
||||
static int tx_plain; /* (a save was mounted without its working copy: the game writes it in place) */
|
||||
static int tx_import_looked; /* (the run's first mount looked for an import that waits) */
|
||||
static char tx_import_note[HOS_FS_PATH + 160]; /* (what that mount did with one: hos_fs_save_import_note) */
|
||||
|
||||
static DWORD WINAPI tx_finish_main(LPVOID arg) {
|
||||
(void)arg;
|
||||
@@ -293,13 +326,35 @@ void hos_fs_save_finish(void) {
|
||||
tx_wait_locked();
|
||||
ReleaseSRWLockExclusive(&tx_lock);
|
||||
}
|
||||
/* (triforce) The run's first mount of a save, once save_tx_mount put the disk right (ok: its answer): an import from the
|
||||
System tab that waited for this start (save_tx_share.h) becomes the newest save, the one it replaces kept first, and
|
||||
save_tx_mount runs again: it puts the import in place and makes the working copy from it. What was done is kept as a
|
||||
sentence, for the log and the menu (hos_fs_save_import_note). With tx_lock held; the mount's answer */
|
||||
static int tx_import_at_mount(const SaveTxPaths *p, int ok) {
|
||||
if (tx_import_looked) return ok;
|
||||
tx_import_looked = 1; /* (a later mount in the same run is not a start: the game has its progress in memory by then) */
|
||||
if (!ok) return ok;
|
||||
char kept[HOS_FS_PATH];
|
||||
const int r = tx_share_import_apply(p, kept, sizeof kept);
|
||||
if (r == 1) ok = save_tx_mount(p, NULL, 0);
|
||||
if (r == 1 && kept[0])
|
||||
snprintf(tx_import_note, sizeof tx_import_note,
|
||||
"The imported save was put in place at this start. The save it replaced is kept in %s", kept);
|
||||
else if (r == 1) snprintf(tx_import_note, sizeof tx_import_note, "The imported save was put in place at this start.");
|
||||
else if (r == -1)
|
||||
snprintf(tx_import_note, sizeof tx_import_note,
|
||||
"The imported save was not put in place: the save it replaces could not be kept first. It waits for the next start.");
|
||||
else if (r == -2) snprintf(tx_import_note, sizeof tx_import_note, "What waited as an imported save was not a save: it was dropped.");
|
||||
return ok;
|
||||
}
|
||||
/* MountSaveData's root for uid: the working copy (1, in root), or 0: the save folder itself, as before */
|
||||
static int tx_mount(const char *name, const char *uid, char *root, size_t cap) {
|
||||
if (!save_tx_enabled()) return 0;
|
||||
static int at_exit;
|
||||
int ok = 0;
|
||||
int ok = 0, locked = 1;
|
||||
char note[256] = "";
|
||||
AcquireSRWLockExclusive(&tx_lock);
|
||||
const int import_said = tx_import_note[0] != 0;
|
||||
if (!at_exit) at_exit = 1, atexit(hos_fs_save_finish);
|
||||
tx_wait_locked();
|
||||
int i;
|
||||
@@ -315,7 +370,10 @@ static int tx_mount(const char *name, const char *uid, char *root, size_t cap) {
|
||||
TxMount *m = &tx_mounts[ntx];
|
||||
save_tx_paths(&m->p, hos_user_dir, uid);
|
||||
const int away = hos_vcore_away();
|
||||
/* ((Triforce) the save's lock before the save is read: a copy Setup makes of it is waited for, up to 30 s; save_tx.h) */
|
||||
if (!ntx) locked = save_tx_lock(hos_user_dir, 30000);
|
||||
ok = save_tx_mount(&m->p, note, sizeof note);
|
||||
ok = tx_import_at_mount(&m->p, ok);
|
||||
if (away) hos_vcore_back();
|
||||
if (ok) {
|
||||
snprintf(m->name, sizeof m->name, "%s", name);
|
||||
@@ -324,15 +382,91 @@ static int tx_mount(const char *name, const char *uid, char *root, size_t cap) {
|
||||
ntx++;
|
||||
}
|
||||
}
|
||||
if (!ok) tx_plain = 1;
|
||||
ReleaseSRWLockExclusive(&tx_lock);
|
||||
if (locked == 0) hos_log("fs: save: another program held %s\\save.lock for 30 s (Setup copying a save?): the save is mounted anyway", hos_user_dir);
|
||||
else if (locked < 0) hos_log("fs: save: %s\\save.lock could not be made: Setup cannot tell that the game runs", hos_user_dir);
|
||||
if (note[0]) hos_log("fs: save: %s", note);
|
||||
if (!import_said && tx_import_note[0]) hos_log("fs: save: %s", tx_import_note);
|
||||
if (!ok) hos_log("fs: save: no working copy could be made: the save folder itself is mounted, and saves are written in place");
|
||||
return ok;
|
||||
}
|
||||
/* (triforce) Sharing a save, for the System tab's rows and the bug capture (host/save_share.c; save_tx_share.h has what is
|
||||
done on the disk). With tx_lock held: the folders of the save the game mounted, or before its mount those of the one
|
||||
user's folder under <user>/save. 0: there is none yet */
|
||||
static int tx_share_paths(SaveTxPaths *p) {
|
||||
if (ntx) {
|
||||
*p = tx_mounts[0].p;
|
||||
return 1;
|
||||
}
|
||||
char pat[HOS_FS_PATH];
|
||||
WIN32_FIND_DATAA fd;
|
||||
int found = 0;
|
||||
if (!hos_user_dir) return 0;
|
||||
snprintf(pat, sizeof pat, "%s\\save\\*", hos_user_dir);
|
||||
HANDLE f = FindFirstFileA(pat, &fd);
|
||||
if (f == INVALID_HANDLE_VALUE) return 0;
|
||||
do { /* (a user's folder: its id, 32 hex digits) */
|
||||
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) continue;
|
||||
if (strlen(fd.cFileName) != 32 || strspn(fd.cFileName, "0123456789abcdefABCDEF") != 32) continue;
|
||||
save_tx_paths(p, hos_user_dir, fd.cFileName);
|
||||
found = 1;
|
||||
} while (!found && FindNextFileA(f, &fd));
|
||||
FindClose(f);
|
||||
return found;
|
||||
}
|
||||
/* The save the next start would load, read whole with no Commit under way: fn gets each file, its path below the save
|
||||
with / between the parts (it only keeps the bytes: tx_lock is held, and a Commit of the game waits). The files read;
|
||||
0: no save yet; -1: one could not be read; -2: the save is no transaction in this run (HOS_SAVE_TX=0, or no working
|
||||
copy: the game writes its files in place, and a copy could catch one half written) */
|
||||
int hos_fs_save_read(int (*fn)(void *arg, const char *rel, const void *data, size_t n), void *arg) {
|
||||
if (!save_tx_enabled()) return -2;
|
||||
SaveTxPaths p;
|
||||
char newest[HOS_FS_PATH];
|
||||
int files = 0;
|
||||
AcquireSRWLockExclusive(&tx_lock);
|
||||
tx_wait_locked();
|
||||
if (tx_plain) files = -2;
|
||||
else if (tx_share_paths(&p) && tx_share_newest(&p, newest, sizeof newest)) files = tx_share_read(newest, "", 0, fn, arg);
|
||||
ReleaseSRWLockExclusive(&tx_lock);
|
||||
return files;
|
||||
}
|
||||
/* (the folders alone, for the import's calls: their own folders under <tx>, which no Commit touches, so written without
|
||||
tx_lock and without holding a Commit up) */
|
||||
static int tx_share_paths_now(SaveTxPaths *p) {
|
||||
if (!save_tx_enabled()) return 0;
|
||||
AcquireSRWLockExclusive(&tx_lock);
|
||||
const int found = tx_share_paths(p);
|
||||
ReleaseSRWLockExclusive(&tx_lock);
|
||||
return found;
|
||||
}
|
||||
/* An imported save, checked by the caller (host/save_pack.c), left to wait for the next start. 1: it waits; 0: it could not
|
||||
be written; -2: no save to put it under (HOS_SAVE_TX=0, or the game has not made its save folder yet) */
|
||||
int hos_fs_save_import_stage(int n, const char *const *rels, const uint8_t *const *data, const size_t *sizes) {
|
||||
SaveTxPaths p;
|
||||
return tx_share_paths_now(&p) ? tx_share_import_stage(&p, n, rels, data, sizes) : -2;
|
||||
}
|
||||
int hos_fs_save_import_waits(void) {
|
||||
SaveTxPaths p;
|
||||
return tx_share_paths_now(&p) && tx_share_import_waits(&p);
|
||||
}
|
||||
int hos_fs_save_import_cancel(void) { /* 1: none waits any more */
|
||||
SaveTxPaths p;
|
||||
return !tx_share_paths_now(&p) || tx_share_import_cancel(&p);
|
||||
}
|
||||
/* What this run's first mount did with an import that waited, as a sentence in out; 0 and "" when none did */
|
||||
int hos_fs_save_import_note(char *out, size_t cap) {
|
||||
AcquireSRWLockExclusive(&tx_lock);
|
||||
snprintf(out, cap, "%s", tx_import_note);
|
||||
ReleaseSRWLockExclusive(&tx_lock);
|
||||
return out[0] != 0;
|
||||
}
|
||||
void hos_save_share_start(void); /* host/save_share.c */
|
||||
/* MountSaveData(char const*, account::Uid const&) */
|
||||
HLE(_ZN2nn2fs13MountSaveDataEPKcRKNS_7account3UidE) {
|
||||
char sub[64], root[HOS_FS_PATH];
|
||||
snprintf(sub, sizeof sub, "save\\%016llx%016llx", (unsigned long long)G64(ARG(1) + 8), (unsigned long long)G64(ARG(1)));
|
||||
hos_save_share_start(); /* (triforce: the System tab's save rows answer from the first save mount on) */
|
||||
if (tx_mount(gstr(ARG(0)), sub + 5, root, sizeof root)) { RET(mount(gstr(ARG(0)), root, 1) ? 0 : R_NOT_MOUNTED); return; }
|
||||
RET(user_mount(gstr(ARG(0)), sub) ? 0 : R_NOT_MOUNTED);
|
||||
}
|
||||
@@ -491,6 +625,7 @@ static uint64_t read_at(Ctx *c, uint64_t id, int64_t off, uint64_t buf, uint64_t
|
||||
hos_sleep_ns(delay_ns);
|
||||
}
|
||||
const int away = hos_vcore_away(); /* (as OpenFile: no core held through the read) */
|
||||
const int io = hos_guest_io_begin(GP(buf), size); /* (Triforce: the kernel writes there: rx_backend.c) */
|
||||
while (done < size) {
|
||||
OVERLAPPED ov = {0};
|
||||
int64_t o = off + (int64_t)done;
|
||||
@@ -500,6 +635,7 @@ static uint64_t read_at(Ctx *c, uint64_t id, int64_t off, uint64_t buf, uint64_t
|
||||
if (!ReadFile(fh->h, GP(buf + done), chunk, &got, &ov) || !got) break;
|
||||
done += got;
|
||||
}
|
||||
hos_guest_io_end(io);
|
||||
if (away) hos_vcore_back();
|
||||
HOS_WRITTEN(GP(buf), (size_t)done); /* (into guest memory: the GPU may read it) */
|
||||
{ /* HOS_FS_WATCH=<lo hex>:<hi hex> (debugging): reads into that guest range, with the file */
|
||||
@@ -542,7 +678,11 @@ HLE(_ZN2nn2fs9WriteFileENS0_10FileHandleElPKvmRKNS0_11WriteOptionE) {
|
||||
ov.OffsetHigh = (DWORD)(ARG(1) >> 32);
|
||||
DWORD put = 0;
|
||||
/* (hookshot) from guest memory only, and the whole size: a DWORD cut wrote less than asked and said so */
|
||||
if (ARG(3) <= 0xFFFFFFFFu && hos_guest_range(ARG(2), ARG(3))) WriteFile(fh->h, GP(ARG(2)), (DWORD)ARG(3), &put, &ov);
|
||||
if (ARG(3) <= 0xFFFFFFFFu && hos_guest_range(ARG(2), ARG(3))) {
|
||||
const int io = hos_guest_io_begin(GP(ARG(2)), ARG(3)); /* (Triforce: the kernel reads there: rx_backend.c) */
|
||||
WriteFile(fh->h, GP(ARG(2)), (DWORD)ARG(3), &put, &ov);
|
||||
hos_guest_io_end(io);
|
||||
}
|
||||
RET(put == ARG(3) ? 0 : 0x2EE202);
|
||||
}
|
||||
HLE(_ZN2nn2fs11SetFileSizeENS0_10FileHandleEl) {
|
||||
|
||||
211
host/nn_hid.c
211
host/nn_hid.c
@@ -268,6 +268,28 @@ static int mouse_stick(int32_t *rx, int32_t *ry, int pointing) {
|
||||
host/posix/display_sdl.c). tests/test_alt_enter.py */
|
||||
static int kmap_down(int vk, int (*down)(int)) { return down(vk) && !(vk == VK_RETURN && down(VK_MENU)); }
|
||||
|
||||
#ifndef __EMSCRIPTEN__
|
||||
/* (Triforce) The program whose window is in front, for game_in_front's log line: its file's name alone, never its folder
|
||||
(a user's name) or the window's title (a document's or a page's name). "?" where Windows does not say.
|
||||
tests/test_input_background.py has its own */
|
||||
static void front_program(DWORD pid, char *name, int size) {
|
||||
snprintf(name, (size_t)size, "?");
|
||||
#ifdef _WIN32
|
||||
HANDLE p = pid ? OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid) : NULL;
|
||||
if (!p) return;
|
||||
char path[MAX_PATH];
|
||||
DWORD n = sizeof path;
|
||||
if (QueryFullProcessImageNameA(p, 0, path, &n)) {
|
||||
const char *base = strrchr(path, '\\');
|
||||
snprintf(name, (size_t)size, "%s", base ? base + 1 : path);
|
||||
}
|
||||
CloseHandle(p);
|
||||
#else
|
||||
(void)pid;
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
static int game_in_front(void) {
|
||||
DWORD pid = 0;
|
||||
HWND w = GetForegroundWindow();
|
||||
@@ -275,8 +297,21 @@ static int game_in_front(void) {
|
||||
int front = pid == GetCurrentProcessId();
|
||||
#ifndef __EMSCRIPTEN__
|
||||
static int last_normal_was_us = 1, was_front = 1;
|
||||
static int64_t none_since, changed_ns;
|
||||
const int64_t now = hos_now_ns();
|
||||
/* (Triforce) No window in front at all: Windows answers so for a moment while the window in front changes, and a
|
||||
notification or an overlay coming and going is such a change. Taken as "not in front", each such moment let every
|
||||
button go and centred the sticks for a read, with the game still in front: a held shield dropped, a run stopped.
|
||||
The answer before it stands for a quarter of a second of that; longer (a locked PC) is not in front */
|
||||
if (!w) {
|
||||
if (!none_since) none_since = now;
|
||||
if (now - none_since < 250000000LL) return was_front;
|
||||
} else {
|
||||
none_since = 0;
|
||||
}
|
||||
LONG_PTR ex = 0;
|
||||
if (w && !front) {
|
||||
const LONG_PTR ex = GetWindowLongPtrA(w, GWL_EXSTYLE);
|
||||
ex = GetWindowLongPtrA(w, GWL_EXSTYLE);
|
||||
const int overlay = (ex & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE)) != 0;
|
||||
if (overlay && last_normal_was_us) front = 1;
|
||||
else last_normal_was_us = 0;
|
||||
@@ -284,10 +319,17 @@ static int game_in_front(void) {
|
||||
last_normal_was_us = 1;
|
||||
}
|
||||
if (front != was_front) {
|
||||
char cls[64] = "";
|
||||
/* (Triforce) for a report of a controller that "disconnects": which program took the front (its file's name and its
|
||||
window's extended styles, by which an overlay is told from a window), and for how long the game was behind it:
|
||||
the controllers do not count all that time unless Controller in background is on */
|
||||
char cls[64] = "", program[64];
|
||||
if (w) GetClassNameA(w, cls, sizeof cls);
|
||||
hos_log("hid: game window %s (foreground window: process %lu, class \"%s\")", front ? "in front" : "not in front",
|
||||
(unsigned long)pid, cls);
|
||||
front_program(pid, program, sizeof program);
|
||||
hos_log("hid: game window %s (foreground window: process %lu, class \"%s\", program %s, styles %#lx)%s",
|
||||
front ? "in front" : "not in front", (unsigned long)pid, cls, program, (unsigned long)ex,
|
||||
front ? "" : ": the controllers do not count until it is in front again, unless Controller in background is on");
|
||||
if (front && changed_ns) hos_log("hid: the game window was not in front for %.1f s", (now - changed_ns) / 1e9);
|
||||
changed_ns = now;
|
||||
was_front = front;
|
||||
}
|
||||
#endif
|
||||
@@ -366,6 +408,11 @@ static void live_input(void) {
|
||||
if (!strncmp(line, "!flasharm", 9)) rx_flash_dark_arm();
|
||||
extern volatile int hos_oot_live_action; /* host/hos_main.c: "!cheat <action>", as the Cheats page sends */
|
||||
if (!strncmp(line, "!cheat ", 7)) __atomic_store_n(&hos_oot_live_action, atoi(line + 7), __ATOMIC_RELAXED);
|
||||
/* ((Triforce) "!type <text>": typed into the menu as the keyboard's characters would be (host/display.c; ASCII; "\b"
|
||||
a Backspace), for the Mods screen's search in test runs, which have no window to type into) */
|
||||
if (!strncmp(line, "!type ", 6) && rx_option(HOS_OPT_TEXT_WANTED, 0))
|
||||
for (const char *t = line + 6; *t && *t != '\r' && *t != '\n'; t++)
|
||||
rx_option_set(HOS_OPT_TEXT_CHAR, t[0] == '\\' && t[1] == 'b' ? (t++, 8) : (unsigned char)*t);
|
||||
void hos_oot_live_crawl(const char *line); /* host/hos_main.c: "!banc", "!pos", "!hold", "!where" (the pipeline crawl) */
|
||||
if (!strncmp(line, "!banc ", 6) || !strncmp(line, "!give ", 6) || !strncmp(line, "!items", 6) || !strncmp(line, "!pos ", 5) || !strncmp(line, "!hold ", 6) ||
|
||||
!strncmp(line, "!where", 6))
|
||||
@@ -621,6 +668,22 @@ int32_t hos_stick_axis(int16_t v, int zone) {
|
||||
const int32_t a = v < -32767 ? -32767 : v, t = cut[(unsigned)zone < 6 ? zone : 0];
|
||||
return (a > t || a < -t) ? a : 0;
|
||||
}
|
||||
/* (Triforce, after NX2navi's lstick_deadzone and rstick_deadzone) A stick's own dead zone, the Insert menu's "Left stick
|
||||
dead zone" and "Right stick dead zone" (stick 0 the left, 1 the right): -1 none of its own, "As Stick dead zone", the
|
||||
default; else 0 to 50, percent of an axis's travel. The option is one more than the row's value, so that 0, which is
|
||||
also what an option table that does not know the rows answers, is none of its own. */
|
||||
int hos_stick_own(int stick) {
|
||||
const int v = rx_option(stick ? HOS_OPT_RSTICK_ZONE : HOS_OPT_LSTICK_ZONE, 0) - 1;
|
||||
return v < 0 ? -1 : v > 50 ? 50 : v;
|
||||
}
|
||||
/* An axis cut at its stick's dead zone: the stick's own (own, percent: 5 cuts where the Stick dead zone row's 5% does)
|
||||
or, when it has none (-1), the menu's Stick dead zone (zone), which is hos_stick_axis as it was. Per axis and with no
|
||||
rescaling of the travel left, as that row: the game's own dead zone still follows. 0 cuts nothing. */
|
||||
int32_t hos_stick_axis_own(int16_t v, int zone, int own) {
|
||||
if (own < 0) return hos_stick_axis(v, zone);
|
||||
const int32_t a = v < -32767 ? -32767 : v, t = ((own > 50 ? 50 : own) * 32767 + 50) / 100;
|
||||
return (a > t || a < -t) ? a : 0;
|
||||
}
|
||||
|
||||
static void pads_read(uint64_t *buttons, int32_t stick[4]) {
|
||||
static int64_t next_read, retry[XUSER_MAX_COUNT];
|
||||
@@ -696,7 +759,8 @@ static void pads_read(uint64_t *buttons, int32_t stick[4]) {
|
||||
int32_t v[4];
|
||||
int rx_option(int id, int otherwise);
|
||||
const int zone = rx_option(11, 0); /* (the menu's Stick dead zone) */
|
||||
for (int i = 0; i < 4; i++) v[i] = hos_stick_axis(thumb[i], zone);
|
||||
const int own[2] = {hos_stick_own(0), hos_stick_own(1)}; /* ((Triforce) each stick's own, -1 none) */
|
||||
for (int i = 0; i < 4; i++) v[i] = hos_stick_axis_own(thumb[i], zone, own[i >> 1]);
|
||||
if (rx_option(16, 0)) v[2] = -v[2];
|
||||
if (rx_option(17, 0)) v[3] = -v[3];
|
||||
stick_max(s, v);
|
||||
@@ -733,7 +797,17 @@ static void pads_read(uint64_t *buttons, int32_t stick[4]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
InterlockedExchange(&pad_raw_now, (LONG)raw);
|
||||
{ /* ((Triforce) 11-17) the menus' pad bits also say where the left stick is pushed, past half its travel (vk_api.h
|
||||
HOS_PAD_LS_*): the overlay's lists move by the stick as the game's do. Not the game's: raw itself is unchanged */
|
||||
uint64_t menu = raw;
|
||||
if (s[1] > 16384) menu |= 1ULL << HOS_PAD_LS_UP;
|
||||
if (s[1] < -16384) menu |= 1ULL << HOS_PAD_LS_DOWN;
|
||||
if (s[0] < -16384) menu |= 1ULL << HOS_PAD_LS_LEFT;
|
||||
if (s[0] > 16384) menu |= 1ULL << HOS_PAD_LS_RIGHT;
|
||||
if (s[3] > 16384) menu |= 1ULL << HOS_PAD_RS_UP;
|
||||
if (s[3] < -16384) menu |= 1ULL << HOS_PAD_RS_DOWN;
|
||||
InterlockedExchange(&pad_raw_now, (LONG)menu);
|
||||
}
|
||||
/* Back and Start together open the Insert menu (gpu/vk/vk_menu.cpp, for pads whose Home button the system
|
||||
keeps): they reach the game only one at a time */
|
||||
const uint64_t chord = 1ULL << HOS_PAD_BACK | 1ULL << HOS_PAD_START;
|
||||
@@ -748,6 +822,25 @@ static void pads_read(uint64_t *buttons, int32_t stick[4]) {
|
||||
stick_max(stick, s);
|
||||
}
|
||||
|
||||
/* (Triforce) Whether XInput has a controller now, for the Button prompts row's Auto (host/prompts.c auto_face), which asks
|
||||
once, at the game's start, and only when SDL names none: a controller read through XInput alone (HOS_NO_SDL=1, or no
|
||||
SDL beside the game) keeps the game's own icons, and only a PC with no controller at all gets the keys' */
|
||||
int hos_xinput_connected(void) {
|
||||
XInputGetStateFn get = xinput_get;
|
||||
#ifdef _WIN32 /* (as pads_read: System32's) */
|
||||
if (!get) {
|
||||
HMODULE x = LoadLibraryExA("xinput1_4.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
||||
if (!x) x = LoadLibraryExA("xinput9_1_0.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
||||
if (x) get = (XInputGetStateFn)GetProcAddress(x, "XInputGetState");
|
||||
}
|
||||
#endif
|
||||
for (DWORD p = 0; get && p < XUSER_MAX_COUNT; p++) {
|
||||
XINPUT_STATE xs;
|
||||
if (get(p, &xs) == ERROR_SUCCESS) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Whether the controllers count while another window is in front: HOS_INPUT_BACKGROUND=1 or 0 decides when it is set,
|
||||
otherwise the Insert menu's "Controller in background" row */
|
||||
static int pads_in_background(const char *env, int menu) {
|
||||
@@ -768,18 +861,49 @@ static void mouse_buttons_arm(int captured) { /* (once a read) */
|
||||
static const int vks[5] = {VK_LBUTTON, VK_RBUTTON, VK_MBUTTON, VK_XBUTTON1, VK_XBUTTON2};
|
||||
for (int i = 0; i < 5; i++) mouse_armed[i] = captured && (mouse_armed[i] || !key(vks[i]));
|
||||
}
|
||||
/* (Triforce, after NX2navi's walk_key) The walk key's code, 0 none: the Insert menu's Walk key row, set at each read
|
||||
(read_input, walk_key_code). While a key is the walk key it is no control's key: Shift, by default B's, would press B
|
||||
with every walking step. What the Controls page binds it to is not touched, and counts again once the row is Off or
|
||||
on another key */
|
||||
static int walk_vk;
|
||||
/* whether a control's key is down: a key always (kmap_down: not Enter with Alt), a mouse button only with mouse (captured
|
||||
and armed) */
|
||||
and armed); the walk key never */
|
||||
static int bound_down(int i, int mouse) {
|
||||
for (int k = 0; k < 2; k++) {
|
||||
const int vk = (int)key_bind[i][k];
|
||||
if (vk <= 0) continue;
|
||||
if (vk <= 0 || vk == walk_vk) continue;
|
||||
const int m = mouse_vk_slot(vk);
|
||||
if (m >= 0 ? mouse && mouse_armed[m] && key(vk) : kmap_down(key_bind[i][k], key)) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* (Triforce, after NX2navi's walk_key and walk_scale) A key is down or up, so the movement keys push the left stick all
|
||||
the way and on the keyboard alone Link always runs. The walk key, while held with them, makes it a part of the way.
|
||||
The Insert menu's Walk key row's values, in its order: Off, then Caps Lock (no control's key by default), Shift (B's),
|
||||
Ctrl (the left stick press's) and Tab (one of Plus's). Alt is not among them: Windows takes an Alt let go alone for
|
||||
its window menu, and beeps at a key pressed with it (host/display.c hands both on to it) */
|
||||
static int walk_key_code(int row) {
|
||||
static const int codes[] = {0, VK_CAPITAL, VK_SHIFT, VK_CONTROL, VK_TAB};
|
||||
return row > 0 && row < (int)(sizeof codes / sizeof *codes) ? codes[row] : 0;
|
||||
}
|
||||
/* The movement keys' left stick (keys[0] and keys[1], each -32767, 0 or 32767) while the walk key is held: percent, the
|
||||
Walk scale row's, of all the way, 20 to 90 (0 or less, as from an option table that does not know the row: 51, the
|
||||
row's default). Two keys at once push a diagonal: each axis then gets the scale over the square root of 2, so the
|
||||
stick is as far from its centre as with one key, the same walk in all eight directions (unscaled, two keys are the
|
||||
corner of the square, past the stick's round edge, as before). The camera's keys (keys[2], keys[3]) stay: a walk is
|
||||
the left stick's */
|
||||
static void walk_keys(int32_t keys[4], int percent) {
|
||||
if (percent <= 0) percent = 51;
|
||||
if (percent < 20) percent = 20;
|
||||
if (percent > 90) percent = 90;
|
||||
const int both = keys[0] && keys[1];
|
||||
for (int k = 0; k < 2; k++) {
|
||||
keys[k] = keys[k] * percent / 100;
|
||||
if (both) keys[k] = keys[k] * 46341 / 65536; /* (65536 over the square root of 2) */
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t flight_input;
|
||||
static uint64_t free_camera_axes;
|
||||
static int free_camera_height;
|
||||
@@ -854,7 +978,7 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
return;
|
||||
}
|
||||
{ /* (Triforce) The options' tabs inside the game's own Options screen (host/oot_menu.c, gpu/vk/vk_menu.cpp): one of
|
||||
our tabs left with Back (the gamepad input B is bound to, or Esc) is the game's Back as well, as in its own
|
||||
our tabs left with Back (the gamepad input B is bound to, Esc, or the mouse's right button) is the game's Back as well, as in its own
|
||||
menus one Back leaves the screen from any tab. The press itself never reaches the game (below), so B is
|
||||
tapped for it. A tab left with L or R is the game's own tab: no tap */
|
||||
int hos_oot_options_front(void);
|
||||
@@ -862,7 +986,8 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
const int open = rx_option(1, 0);
|
||||
if (was_open && !open && hos_oot_options_front()) {
|
||||
const int b = hos_binding(HOS_BIND_PAD, HOS_SW_B, 0);
|
||||
if ((b >= 0 && b < HOS_PAD_N && ((uint32_t)pad_raw_now >> b & 1)) || (GetAsyncKeyState(VK_ESCAPE) & 0x8000))
|
||||
if ((b >= 0 && b < HOS_PAD_N && ((uint32_t)pad_raw_now >> b & 1)) || (GetAsyncKeyState(VK_ESCAPE) & 0x8000) ||
|
||||
(GetAsyncKeyState(VK_RBUTTON) & 0x8000))
|
||||
hos_hid_tap("0:B/80");
|
||||
}
|
||||
was_open = open;
|
||||
@@ -884,6 +1009,7 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
player_used();
|
||||
}
|
||||
binds_load(); /* (the keys: key_default's, or triforce_options.ini's; the mouse's buttons once its capture is known) */
|
||||
walk_vk = walk_key_code(rx_option(HOS_OPT_WALK_KEY, 0)); /* ((Triforce) before the keys are read: it is none of theirs) */
|
||||
for (int i = 0; i < HOS_SW_N; i++)
|
||||
if (bound_down(i, 0)) *buttons |= 1ULL << i, player_used();
|
||||
int32_t mouse[4] = {0};
|
||||
@@ -912,9 +1038,12 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
/* (the left stick's and the camera's keys: WASD and the arrows or IJKL by default) */
|
||||
const int cx = bound_down(HOS_KEY_CAM_RIGHT, captured) - bound_down(HOS_KEY_CAM_LEFT, captured),
|
||||
cy = bound_down(HOS_KEY_CAM_UP, captured) - bound_down(HOS_KEY_CAM_DOWN, captured);
|
||||
const int32_t keys[4] = {(bound_down(HOS_KEY_MOVE_RIGHT, captured) - bound_down(HOS_KEY_MOVE_LEFT, captured)) * 32767,
|
||||
(bound_down(HOS_KEY_MOVE_UP, captured) - bound_down(HOS_KEY_MOVE_DOWN, captured)) * 32767,
|
||||
cx * 32767, cy * 32767};
|
||||
int32_t keys[4] = {(bound_down(HOS_KEY_MOVE_RIGHT, captured) - bound_down(HOS_KEY_MOVE_LEFT, captured)) * 32767,
|
||||
(bound_down(HOS_KEY_MOVE_UP, captured) - bound_down(HOS_KEY_MOVE_DOWN, captured)) * 32767,
|
||||
cx * 32767, cy * 32767};
|
||||
/* ((Triforce) the walk key held: the movement keys push the left stick a part of the way. The keys' alone: a
|
||||
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();
|
||||
}
|
||||
@@ -922,6 +1051,7 @@ static void read_input(uint64_t *buttons, int32_t stick[4]) {
|
||||
static void vibration_send(uint64_t handle, uint64_t value);
|
||||
volatile int64_t hos_hid_last_read;
|
||||
|
||||
volatile uint64_t hos_npad_buttons; /* (Triforce) the buttons of the last state the game was given: host/mod_cheats.c */
|
||||
/* GetNpadStates(State* out, int count, unsigned int const& npadId): the latest state first */
|
||||
static void npad_states(Ctx *c) {
|
||||
uint64_t out = ARG(0);
|
||||
@@ -947,8 +1077,61 @@ static void npad_states(Ctx *c) {
|
||||
(unsigned long long)c->x[30]);
|
||||
}
|
||||
}
|
||||
{ /* (Triforce) the Scan amiibo page's choice is taken here, whatever the game shows (host/nn_nfp.c) */
|
||||
void hos_amiibo_poll(void);
|
||||
hos_amiibo_poll();
|
||||
}
|
||||
if (connected) {
|
||||
read_input(&buttons, stick);
|
||||
{ /* (Triforce) the Mods entry under a game list's last button (host/oot_menu.c, gpu/vk/vk_menu.cpp): while the
|
||||
game's cursor is on that last button and our menu is shut, Down (the D-pad's, or the left stick held down)
|
||||
is the entry's, not the game's: the renderer takes the cursor onto Mods at its first frame */
|
||||
int hos_oot_mods_down(void);
|
||||
static int was_down;
|
||||
/* (a press only: the Down that brought the game's cursor onto the last button, still held, is not one) */
|
||||
const int down = (int)(buttons >> B_DOWN & 1) || stick[1] < -16000;
|
||||
if (!rx_option(1, 0) && hos_oot_mods_down() && down) {
|
||||
buttons &= ~(1ULL << B_DOWN);
|
||||
stick[1] = 0;
|
||||
if (!was_down) rx_option_set(HOS_OPT_MODS_ENTER, 1);
|
||||
}
|
||||
was_down = down;
|
||||
/* While the Mods entry or its screen has the cursor the game gets no input (a pad's is the menu's already,
|
||||
above, and the keys' too, key()). What is left is a test's scripted presses: they go to the menu as a pad's
|
||||
would (HOS_OPT_MODS_PRESS), not to the game. Buttons held as the cursor goes back to the game are not the
|
||||
game's until let go (the Up that left Mods does not move the game's cursor too) */
|
||||
static uint64_t prev, held_over;
|
||||
static int sel_was, stick_over;
|
||||
const int sel = rx_option(HOS_OPT_MODS_SELECTED, 0) != 0;
|
||||
if (sel) {
|
||||
static const struct { int bit, press; } map[] = {{B_UP, HOS_MODS_PRESS_UP}, {B_DOWN, HOS_MODS_PRESS_DOWN},
|
||||
{B_LEFT, HOS_MODS_PRESS_LEFT}, {B_RIGHT, HOS_MODS_PRESS_RIGHT}, {B_A, HOS_MODS_PRESS_A}, {B_B, HOS_MODS_PRESS_B},
|
||||
{B_L, HOS_MODS_PRESS_L}, {B_R, HOS_MODS_PRESS_R}};
|
||||
const uint64_t fresh = buttons & ~prev;
|
||||
int bits = 0;
|
||||
for (int i = 0; i < (int)(sizeof map / sizeof *map); i++)
|
||||
if (fresh >> map[i].bit & 1) bits |= map[i].press;
|
||||
{ /* ((Triforce) 11-17) and the left stick pushed past half its travel, as the D-pad */
|
||||
static int stick_was;
|
||||
const int dir = stick[1] > 16384 ? HOS_MODS_PRESS_UP : stick[1] < -16384 ? HOS_MODS_PRESS_DOWN
|
||||
: stick[0] < -16384 ? HOS_MODS_PRESS_LEFT : stick[0] > 16384 ? HOS_MODS_PRESS_RIGHT : 0;
|
||||
if (dir && dir != stick_was) bits |= dir;
|
||||
stick_was = dir;
|
||||
}
|
||||
if (bits) rx_option_set(HOS_OPT_MODS_PRESS, bits);
|
||||
prev = buttons;
|
||||
buttons = 0;
|
||||
memset(stick, 0, 4 * sizeof *stick);
|
||||
} else {
|
||||
if (sel_was) held_over = prev, stick_over = 1;
|
||||
held_over &= buttons;
|
||||
buttons &= ~held_over;
|
||||
if (stick_over && stick[0] > -12000 && stick[0] < 12000 && stick[1] > -12000 && stick[1] < 12000) stick_over = 0;
|
||||
if (stick_over) stick[0] = stick[1] = 0;
|
||||
prev = 0;
|
||||
}
|
||||
sel_was = sel;
|
||||
}
|
||||
{ /* (Triforce) the sprint button's press is its own while Link plays (host/oot_sprint.c) */
|
||||
void hos_oot_sprint_pad(uint64_t *buttons);
|
||||
hos_oot_sprint_pad(&buttons);
|
||||
@@ -980,6 +1163,8 @@ static void npad_states(Ctx *c) {
|
||||
if (connected) buttons = hos_title_input(buttons);
|
||||
uint64_t hos_frame_step_buttons(uint64_t buttons); /* nn_vi.c: the frame step of the wheel and quick menus */
|
||||
if (connected) buttons = hos_frame_step_buttons(buttons);
|
||||
/* (Triforce) the buttons the game is given, for the mods' cheats that run while buttons are held (host/mod_cheats.c) */
|
||||
if (connected) __atomic_store_n(&hos_npad_buttons, buttons, __ATOMIC_RELAXED);
|
||||
int64_t s = ++sampling;
|
||||
for (int i = 0; i < count; i++) {
|
||||
uint64_t e = out + (uint64_t)i * STATE_SIZE;
|
||||
|
||||
196
host/nn_nfp.c
Normal file
196
host/nn_nfp.c
Normal file
@@ -0,0 +1,196 @@
|
||||
/* (Triforce) nn::nfp: an amiibo from a file.
|
||||
|
||||
The game's amiibo sequence asks its NfpMgr for a reader and waits while there is none; with one, it starts detection,
|
||||
reads the figure's TagInfo and ModelInfo and waits for the figure to be lifted off (host/amiibo.h says what it reads
|
||||
of them, and has the readers' states). Until now every nn::nfp function was the lift's stub, which answers 0 and
|
||||
writes nothing: no reader, so the sequence waited for ever.
|
||||
|
||||
The player puts dumps of their own figures into a folder "amiibo" in the game folder and chooses one on the Debug
|
||||
tab's Scan amiibo page (gpu/vk/vk_menu.cpp). The page sends the file's name (HOS_OPT_AMIIBO_ASK, a byte a read, a line
|
||||
feed at its end; a line of its own for "none"), read here at the game's pad read. From the first figure chosen on, two
|
||||
readers are listed and the game is told that the readers changed (the event of AttachAvailabilityChangeEvent, which
|
||||
its NfpMgr thread asks about while it has none); the figure goes onto the reader the game looks for one with. Until
|
||||
a figure is chosen every function here answers as the stub did, so a player who chooses none gets the game as it was.
|
||||
The page is told what became of the figure (HOS_OPT_AMIIBO_SAID, NFP_SAID_*).
|
||||
|
||||
Only what a tag keeps in the clear is read from a dump (the serial number and which figure it is), so no key is
|
||||
needed, none is looked for and none is read. The game's own code asks for nothing else (host/amiibo.h); the SDK's
|
||||
functions for a figure's owner, nickname and saved data stay the stubs they were. */
|
||||
#include "hos.h"
|
||||
#include "amiibo.h"
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <windows.h> /* (SRWLOCK; linux: host/posix's) */
|
||||
|
||||
int rx_option(int id, int otherwise); /* rx_backend.c */
|
||||
void rx_option_set(int id, int value);
|
||||
void hos_system_event_signal(uint64_t event); /* nn_os_sync.c: by the event's address in the game */
|
||||
int hos_share_game_dir(char *out, size_t cap); /* save_share.c: the game folder, no separator at its end */
|
||||
|
||||
/* (what the page is told is what host/amiibo.h says became of the figure) */
|
||||
_Static_assert(NFP_SAID_NONE == HOS_AMIIBO_NONE && NFP_SAID_WAITING == HOS_AMIIBO_WAITING && NFP_SAID_ON_READER == HOS_AMIIBO_ON_READER &&
|
||||
NFP_SAID_READ == HOS_AMIIBO_READ && NFP_SAID_REFUSED == HOS_AMIIBO_REFUSED && NFP_SAID_NO_FILE == HOS_AMIIBO_NO_FILE,
|
||||
"HOS_AMIIBO_* (gpu/vk/vk_api.h) and NFP_SAID_* (host/amiibo.h)");
|
||||
|
||||
static SRWLOCK nfp_lock = SRWLOCK_INIT;
|
||||
static Nfp nfp = {.holder = -1};
|
||||
static uint64_t availability_event; /* the SystemEventType the game gave AttachAvailabilityChangeEvent, 0 none */
|
||||
|
||||
/* the reader a call names (its DeviceHandle is the argument's first eight bytes), -1 when it is none of ours: the
|
||||
caller then answers as the stub did */
|
||||
static int reader_of(uint64_t handle_ptr) { return handle_ptr ? nfp_reader(&nfp, G64(handle_ptr)) : -1; }
|
||||
|
||||
HLE(_ZN2nn3nfp10InitializeEv) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
nfp_restart(&nfp);
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(0);
|
||||
}
|
||||
HLE(_ZN2nn3nfp8FinalizeEv) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
nfp_restart(&nfp);
|
||||
availability_event = 0; /* (the game's event goes with its NfpMgr: never signalled after this) */
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(0);
|
||||
}
|
||||
/* AttachAvailabilityChangeEvent(os::SystemEventType*): only kept. The game clears and asks it as before (nn_os_sync.c
|
||||
makes an event of an address at its first use); it is signalled when the first figure is chosen */
|
||||
HLE(_ZN2nn3nfp29AttachAvailabilityChangeEventEPNS_2os15SystemEventTypeE) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
availability_event = ARG(0);
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(0);
|
||||
}
|
||||
/* ListDevices(DeviceHandle* out, int* count, int max): eight bytes a handle. Nothing written while none is listed */
|
||||
HLE(_ZN2nn3nfp11ListDevicesEPNS0_12DeviceHandleEPii) {
|
||||
uint64_t handles[NFP_READERS];
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int max = (int)(int32_t)ARG(2);
|
||||
const int n = ARG(0) && ARG(1) ? nfp_list(&nfp, handles, max < NFP_READERS ? max : NFP_READERS) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
for (int i = 0; i < n; i++) G64(ARG(0) + 8ULL * i) = handles[i];
|
||||
if (n > 0) G32(ARG(1)) = (uint32_t)n;
|
||||
RET(0);
|
||||
}
|
||||
/* GetNpadId(unsigned* out, DeviceHandle const&) */
|
||||
HLE(_ZN2nn3nfp9GetNpadIdEPjRKNS0_12DeviceHandleE) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(1));
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
if (reader >= 0 && ARG(0)) G32(ARG(0)) = nfp_reader_npad(reader);
|
||||
RET(0);
|
||||
}
|
||||
/* GetDeviceState(DeviceHandle const&) -> DeviceState */
|
||||
HLE(_ZN2nn3nfp14GetDeviceStateERKNS0_12DeviceHandleE) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(0));
|
||||
const int state = reader >= 0 ? nfp_state(&nfp, reader, hos_now_ns()) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(state);
|
||||
}
|
||||
HLE(_ZN2nn3nfp14StartDetectionERKNS0_12DeviceHandleE) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(0));
|
||||
const uint32_t result = reader >= 0 ? nfp_start(&nfp, reader, hos_now_ns()) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(result);
|
||||
}
|
||||
HLE(_ZN2nn3nfp13StopDetectionERKNS0_12DeviceHandleE) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(0));
|
||||
const uint32_t result = reader >= 0 ? nfp_stop(&nfp, reader) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(result);
|
||||
}
|
||||
/* GetTagInfo(TagInfo* out, DeviceHandle const&) */
|
||||
HLE(_ZN2nn3nfp10GetTagInfoEPNS0_7TagInfoERKNS0_12DeviceHandleE) {
|
||||
uint8_t info[NFP_TAG_INFO_SIZE];
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(1));
|
||||
const uint32_t result = reader >= 0 ? nfp_tag_info(&nfp, reader, hos_now_ns(), info) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
if (reader >= 0 && !result && ARG(0)) memcpy(GP(ARG(0)), info, sizeof info);
|
||||
RET(result);
|
||||
}
|
||||
/* Mount(DeviceHandle const&, ModelType, MountTarget) */
|
||||
HLE(_ZN2nn3nfp5MountERKNS0_12DeviceHandleENS0_9ModelTypeENS0_11MountTargetE) {
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(0));
|
||||
const uint32_t result = reader >= 0 ? nfp_mount(&nfp, reader, hos_now_ns()) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
RET(result);
|
||||
}
|
||||
/* GetModelInfo(ModelInfo* out, DeviceHandle const&) */
|
||||
HLE(_ZN2nn3nfp12GetModelInfoEPNS0_9ModelInfoERKNS0_12DeviceHandleE) {
|
||||
uint8_t info[NFP_MODEL_INFO_SIZE];
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
const int reader = reader_of(ARG(1));
|
||||
const uint32_t result = reader >= 0 ? nfp_model_info(&nfp, reader, hos_now_ns(), info) : 0;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
if (reader >= 0 && !result && ARG(0)) memcpy(GP(ARG(0)), info, sizeof info);
|
||||
if (reader >= 0 && !result) hos_log("nfp: the game read the figure's ModelInfo");
|
||||
RET(result);
|
||||
}
|
||||
|
||||
/* The dump "name" of the game folder's amiibo folder: NFP_SAID_WAITING when it is a figure's (*tag filled), else
|
||||
NFP_SAID_NO_FILE or NFP_SAID_REFUSED. At most AMIIBO_FILE_MAX bytes of it are read */
|
||||
static int load_dump(const char *name, AmiiboTag *tag) {
|
||||
char dir[600], path[900];
|
||||
uint8_t data[AMIIBO_FILE_MAX + 1];
|
||||
if (!amiibo_name_ok(name) || !hos_share_game_dir(dir, sizeof dir)) return NFP_SAID_NO_FILE;
|
||||
snprintf(path, sizeof path, "%s/amiibo/%s", dir, name);
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NFP_SAID_NO_FILE;
|
||||
const size_t n = fread(data, 1, sizeof data, f); /* (one byte more than a dump may have: a longer file is refused) */
|
||||
fclose(f);
|
||||
const int why = amiibo_parse(data, n, tag);
|
||||
if (why != AMIIBO_OK) {
|
||||
hos_log("nfp: amiibo/%s is not taken: %s", name, why == AMIIBO_BAD_SIZE ? "not the size of a figure's dump (532, 540 or 572 bytes)"
|
||||
: "it does not hold a figure's serial number and number");
|
||||
return NFP_SAID_REFUSED;
|
||||
}
|
||||
return NFP_SAID_WAITING;
|
||||
}
|
||||
|
||||
/* The Scan amiibo page's choice, taken at the game's pad read (host/nn_hid.c; every 50 ms at most): a file's name, or
|
||||
an empty line for none. And the page told what became of the figure when that changed */
|
||||
void hos_amiibo_poll(void) {
|
||||
static int64_t next;
|
||||
static int told = -1;
|
||||
static char line[256];
|
||||
static size_t used;
|
||||
static SRWLOCK one = SRWLOCK_INIT; /* (the pad is read by more than one of the game's threads) */
|
||||
const int64_t now = hos_now_ns();
|
||||
if (now < __atomic_load_n(&next, __ATOMIC_RELAXED) || !TryAcquireSRWLockExclusive(&one)) return;
|
||||
__atomic_store_n(&next, now + 50000000LL, __ATOMIC_RELAXED);
|
||||
int byte;
|
||||
while ((byte = rx_option(HOS_OPT_AMIIBO_ASK, 0)) > 0) {
|
||||
if (byte != '\n') {
|
||||
if (used + 1 < sizeof line) line[used++] = (char)byte;
|
||||
continue;
|
||||
}
|
||||
line[used] = 0, used = 0;
|
||||
AmiiboTag tag;
|
||||
const int loaded = line[0] ? load_dump(line, &tag) : NFP_SAID_NONE;
|
||||
uint64_t event = 0;
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
if (loaded == NFP_SAID_WAITING) {
|
||||
if (nfp_choose(&nfp, &tag)) event = availability_event;
|
||||
} else {
|
||||
nfp_take_away(&nfp);
|
||||
nfp.said = loaded;
|
||||
}
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
if (loaded == NFP_SAID_WAITING)
|
||||
hos_log("nfp: amiibo/%s chosen (%s dump): on the reader when the game looks for a figure%s", line,
|
||||
tag.decrypted ? "a decrypted" : "a tag's", event ? "; the game is told a reader is there now" : "");
|
||||
if (event) hos_system_event_signal(event);
|
||||
}
|
||||
AcquireSRWLockExclusive(&nfp_lock);
|
||||
for (int i = 0; nfp.listed && i < NFP_READERS; i++) nfp_step(&nfp, i, now); /* (a figure lifted off when its time is up) */
|
||||
const int said = nfp.said;
|
||||
ReleaseSRWLockExclusive(&nfp_lock);
|
||||
if (said != told) rx_option_set(HOS_OPT_AMIIBO_SAID, told = said);
|
||||
ReleaseSRWLockExclusive(&one);
|
||||
}
|
||||
313
host/nn_vi.c
313
host/nn_vi.c
@@ -2,6 +2,9 @@
|
||||
layer, and the display's vsync event, signaled at 60 Hz by a host thread. HDR and VRR are reported unsupported,
|
||||
so the game takes its SDR path. Frames are paced at the native NVN layer's window acquire (the end of this file). */
|
||||
#include "hos.h"
|
||||
#include "pace_wait.h" /* ((Triforce) the acquire's wait to the frame's time: pace_sleep) */
|
||||
#include "step_steady.h" /* ((Triforce) HOS_STEP_STEADY=1: realtime_step) */
|
||||
#include "pace_steady.h" /* ((Triforce) the Steady frame rate row: steady_period) */
|
||||
#include "../gpu/vk/vk_api.h" /* (HOS_OPT_HDR_*: the Insert menu's HDR, answered by the renderer) */
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
@@ -20,9 +23,10 @@ static HANDLE vsync_thread;
|
||||
static int fps_max(void); /* (below: HOS_FPS_MAX) */
|
||||
static DWORD WINAPI vsync_main(void *p) {
|
||||
(void)p;
|
||||
const int64_t period = 1000000000LL / fps_max();
|
||||
int64_t next = hos_now_ns() + period;
|
||||
int64_t next = hos_now_ns() + 1000000000LL / fps_max();
|
||||
for (;;) {
|
||||
/* (asked each time: with the cap on Monitor's rate it is 60 until the renderer says the display's, fps_max) */
|
||||
const int64_t period = 1000000000LL / fps_max();
|
||||
const int64_t now = hos_now_ns();
|
||||
if (next > now) hos_sleep_ns(next - now);
|
||||
next += period;
|
||||
@@ -111,25 +115,50 @@ static volatile int present_interval = 1;
|
||||
own limiter (+0xfc) and the vsync event go up to it, and the game's time step is the frames' own time (realtime_step), so the game is
|
||||
not sped up. Unset or 60: 60, as on the console */
|
||||
int hos_ini_option(const char *key, int otherwise); /* (nn_applet.c) */
|
||||
static volatile LONG64 presents;
|
||||
/* The cap for the Insert menu's Frame rate cap (row: frame_rate_cap=, 0 the console's 60, then 90, 120, 144, 165, 240), HOS_FPS_MAX
|
||||
(env) over it. */
|
||||
/* (Triforce, after NX2navi's limits under 60: -1 45, -2 40, -3 30, for a PC or a handheld's display that holds one
|
||||
of them steadily where 60 swings. The pacing follows the cap and the game's time step is real time (realtime_step),
|
||||
as when a PC cannot hold 60: the game keeps its speed) */
|
||||
/* (Triforce, after NX2navi's fps_limit = display: 6, Monitor's rate, is the refresh rate of the display the window is on. display:
|
||||
what the renderer says of it (HOS_OPT_DISPLAY_CAP: rounded, 30 to 240), 0 when the rate cannot be read (60 then), under 0
|
||||
while there is no renderer to ask: the answer is then 0, not settled yet. NX2navi's "unlimited" is not offered: the time step
|
||||
here (realtime_step) stops at 1/240 s, and past it the game would run faster than the clock) */
|
||||
static int fps_cap_of(int row, const char *env, int display) {
|
||||
static const int caps[] = {60, 90, 120, 144, 165, 240}, low[] = {45, 40, 30};
|
||||
int want = env ? atoi(env) : row >= 0 && row < 6 ? caps[row] : row < 0 && row >= -3 ? low[-row - 1] : 60;
|
||||
if (!env && row == 6) {
|
||||
if (display < 0) return 0;
|
||||
want = display < 1 ? 60 : display < 30 ? 30 : display > 240 ? 240 : display;
|
||||
}
|
||||
return want > 60 ? (want > 240 ? 240 : want) : want >= 20 && want < 60 ? want : 60;
|
||||
}
|
||||
static int fps_max(void) {
|
||||
static int v = -1;
|
||||
if (v < 0) {
|
||||
/* (the Insert menu's Frame rate cap, frame_rate_cap=: 0 the console's 60, then 90, 120, 144, 165, 240; HOS_FPS_MAX over it) */
|
||||
/* (Triforce, after NX2navi's limits under 60: -1 45, -2 40, -3 30, for a PC or a handheld's display that holds one
|
||||
of them steadily where 60 swings. The pacing follows the cap and the game's time step is real time (realtime_step),
|
||||
as when a PC cannot hold 60: the game keeps its speed) */
|
||||
static const int caps[] = {60, 90, 120, 144, 165, 240}, low[] = {45, 40, 30};
|
||||
const int i = hos_ini_option("frame_rate_cap", 0);
|
||||
/* (the options file once: with Monitor's rate this is asked again until the renderer is up. Until then the cap is 60 and
|
||||
nothing is settled; the game presents nothing before the renderer is up (hos_renderer_wait), so the pacing, the game's
|
||||
limiter and its time step all start with the settled cap. A renderer that never answers (the WebGPU one) leaves 60
|
||||
at the first frame presented) */
|
||||
static int row = -100;
|
||||
if (row == -100) row = hos_ini_option("frame_rate_cap", 0);
|
||||
const char *e = getenv("HOS_FPS_MAX");
|
||||
const int want = e ? atoi(e) : i >= 0 && i < 6 ? caps[i] : i < 0 && i >= -3 ? low[-i - 1] : 60;
|
||||
v = want > 60 ? (want > 240 ? 240 : want) : want >= 20 && want < 60 ? want : 60;
|
||||
const int monitor = !e && row == 6;
|
||||
int rx_option(int id, int otherwise);
|
||||
int display = monitor ? rx_option(HOS_OPT_DISPLAY_CAP, -1) : 0;
|
||||
if (display < 0 && presents) display = 0;
|
||||
const int cap = fps_cap_of(row, e, display);
|
||||
if (!cap) return 60;
|
||||
v = cap;
|
||||
if (monitor && display > 0) hos_log("vi: frame rate cap: the monitor's rate, %d", v);
|
||||
if (monitor && display <= 0) hos_log("vi: frame rate cap: the monitor's rate is not known: %d", v);
|
||||
if (v > 60) hos_log("vi: frame rate cap %d: pacing, the game's limiter and its time step follow it", v);
|
||||
if (v < 60) hos_log("vi: frame rate cap %d: pacing follows it, the game keeps real time", v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
static int64_t next_frame_at, window_start, frames_in_window;
|
||||
static volatile LONG64 presents;
|
||||
int hos_vi_native_interval(Ctx *c, int requested) {
|
||||
(void)c;
|
||||
/* (hookshot) OoT's frame-rate controller asks for 2, 3 or 4 vsyncs a frame whenever one runs long (480 changes a minute
|
||||
@@ -155,18 +184,107 @@ int hos_vi_native_interval(Ctx *c, int requested) {
|
||||
that behaviour */
|
||||
static int64_t shown_at[16];
|
||||
static volatile LONG64 shown_count;
|
||||
/* (Triforce, after NX2navi's one limiter a frame: its runtime/frame_rate.c frame_step_cap_ns, vk_dlssg.c vk_dlssg_pace and
|
||||
rtss_limit.c) Another limiter may hold this frame in the acquire's wait's place: RivaTuner Statistics Server, which is
|
||||
the game's limiter wherever it is installed, runs and is seen holding the game (gpu/vk/vk_pace.cpp, rtss_limit.cpp), or
|
||||
NVIDIA Reflex's sleep, with DLSS frame generation and the Display tab's Reflex frame limit row (gpu/vk/vk_dlssg.cpp
|
||||
pace). The renderer is asked where the frame starts, with the period this file paces by and whether a movie plays, once
|
||||
a frame (the game may acquire again before it presents: the frame's answer stays). 1: that limiter holds this frame,
|
||||
RivaTuner's wait seen to be taken at the frame's presents and Reflex's taken inside the call, and the wait below steps
|
||||
aside for this frame alone. 0: the wait below, as before; that is the answer without a renderer to ask, without
|
||||
RivaTuner, and while a movie plays. One or the other for every frame, never both and never neither.
|
||||
HOS_PACE_HANDOFF=0: never asked (and RivaTuner then gets no limit from the game). */
|
||||
static int64_t pace_period_ns; /* (the period hos_vi_native_presented last paced by) */
|
||||
/* ((Triforce) for the Steady frame rate row, steady_period below: how long the acquire's own wait took since the last
|
||||
present, on the presenting thread and on any other (OoT acquires on its main thread and presents on its Presentation
|
||||
Thread), and whether another limiter held a frame since then. The presenting thread is the one steady_period runs on) */
|
||||
static int64_t frame_waited_own_ns;
|
||||
static volatile LONG64 frame_waited_other_ns;
|
||||
static volatile LONG frame_handed;
|
||||
static _Thread_local int presenting_thread;
|
||||
/* (Triforce) When a movie player last updated (hos_movie_frames, below, which the game's movie players call at each of
|
||||
their updates): within a quarter second of one, a movie plays. NX2navi's pacer holds a movie itself while RivaTuner keeps
|
||||
the play limit, and so does this one */
|
||||
static volatile LONG64 movie_seen_ns;
|
||||
static void movie_seen(void) { __atomic_store_n(&movie_seen_ns, (LONG64)hos_now_ns(), __ATOMIC_RELAXED); }
|
||||
static int movie_plays(void) {
|
||||
const int64_t seen = (int64_t)__atomic_load_n(&movie_seen_ns, __ATOMIC_RELAXED);
|
||||
return seen && hos_now_ns() - seen < 250000000LL;
|
||||
}
|
||||
static int pace_handed(void) {
|
||||
static int on = -1, held;
|
||||
static LONG64 asked = -1;
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
if (on < 0) on = !(getenv("HOS_PACE_HANDOFF") && !atoi(getenv("HOS_PACE_HANDOFF")));
|
||||
if (!on) return 0;
|
||||
const LONG64 frame = presents;
|
||||
if (frame == asked) return held;
|
||||
asked = frame;
|
||||
rx_option_set(HOS_OPT_PACE_PERIOD, (int)pace_period_ns);
|
||||
rx_option_set(HOS_OPT_PACE_MOVIE, movie_plays());
|
||||
held = rx_option(HOS_OPT_PACE_START, 0) == 1;
|
||||
if (held) __atomic_store_n(&frame_handed, 1, __ATOMIC_RELAXED); /* ((Triforce) steady_period: not this file's wait's frame) */
|
||||
return held;
|
||||
}
|
||||
/* (Triforce, after NX2navi's sleep_until: pace_wait.h) The acquire's wait to the frame's time: the timer up to a margin
|
||||
before it and the rest yielded away, so that the frame starts on its time and not up to half a millisecond after it.
|
||||
HOS_PACE_SPIN=<microseconds>: another margin; =0: the timer for the whole wait, as before. Every 5 s in which frames
|
||||
waited, how long after their time the waits ended (vi: frame waits), for comparing the two */
|
||||
static void pace_sleep(int64_t until, int64_t now) {
|
||||
static int64_t margin = -1, waits, late_sum, late_max, since;
|
||||
if (margin < 0) {
|
||||
const char *e = getenv("HOS_PACE_SPIN");
|
||||
margin = e && *e ? atoll(e) * 1000 : PACE_WAIT_MARGIN_NS;
|
||||
if (margin < 0) margin = 0;
|
||||
}
|
||||
if (margin) hos_sleep_until_ns(until, margin);
|
||||
else hos_sleep_ns(until - now);
|
||||
const int64_t after = hos_now_ns(), late = after > until ? after - until : 0;
|
||||
/* ((Triforce) steady_period: not what the frame needed) */
|
||||
if (presenting_thread) frame_waited_own_ns += after - now;
|
||||
else __atomic_fetch_add(&frame_waited_other_ns, (LONG64)(after - now), __ATOMIC_RELAXED);
|
||||
waits++, late_sum += late;
|
||||
if (late > late_max) late_max = late;
|
||||
if (!since) since = after;
|
||||
if (after - since >= 5000000000LL) {
|
||||
char how[64];
|
||||
if (margin) snprintf(how, sizeof how, "the timer to %.2f ms before, the rest yielded", margin / 1e6);
|
||||
else snprintf(how, sizeof how, "the timer for the whole wait: HOS_PACE_SPIN=0");
|
||||
hos_log("vi: frame waits: %lld in %.1f s ended %.0f us after their time on average, %.0f us at most (%s)",
|
||||
(long long)waits, (after - since) / 1e9, late_sum / 1e3 / waits, late_max / 1e3, how);
|
||||
waits = late_sum = late_max = 0, since = after;
|
||||
}
|
||||
}
|
||||
void hos_vi_native_acquire(int active) {
|
||||
static int no_pacing = -1, double_buffered = -1;
|
||||
if (no_pacing < 0) no_pacing = getenv("HOS_NO_PACING") != NULL;
|
||||
if (double_buffered < 0) double_buffered = getenv("HOS_VI_DOUBLE") != NULL;
|
||||
if (no_pacing || !next_frame_at) return;
|
||||
{ /* (Triforce: another limiter holds this frame: its wait was taken in the asking, or is at the frame's present) */
|
||||
static int handed_before;
|
||||
if (pace_handed()) { handed_before = 1; return; }
|
||||
if (handed_before) {
|
||||
/* (the first frame back from another limiter. Its frames' times were not this file's: where they came sooner than
|
||||
the period, a limiter that let go, each present still put the display queue a whole period on, so the queue's times
|
||||
ran ahead of the clock by all the time not waited, and the wait below would be that long, once, a freeze. The
|
||||
queue is moved back, its frames as far apart as they were, to where this file's own pacing has it at most: its
|
||||
newest frame shown no later than a period from now for each texture the game may be ahead by) */
|
||||
const int64_t now = hos_now_ns(), n = shown_count;
|
||||
const int ahead = !double_buffered && active >= 2 ? active - 1 : 1;
|
||||
const int64_t over = (n ? shown_at[(n - 1) & 15] : 0) - (now + pace_period_ns * ahead);
|
||||
handed_before = 0;
|
||||
if (over > 0) for (int i = 0; i < 16; i++) shown_at[i] -= over;
|
||||
if (next_frame_at > now + pace_period_ns) next_frame_at = now + pace_period_ns;
|
||||
}
|
||||
}
|
||||
int64_t until = next_frame_at;
|
||||
if (!double_buffered && active >= 2) {
|
||||
const int64_t n = shown_count, frame = n - active + 1; /* (the frame whose showing frees this texture) */
|
||||
until = frame >= 0 && frame < n ? shown_at[frame & 15] : 0;
|
||||
}
|
||||
const int64_t now = hos_now_ns();
|
||||
if (until > now) hos_sleep_ns(until - now);
|
||||
if (until > now) pace_sleep(until, now);
|
||||
}
|
||||
/* (mkw-recomp) MKW's frame pacer (0x806bcbb0, x19 when it sets the interval: nvn_native.c): its measured frame time
|
||||
(+0x128, ticks), its running excess (+0x218) and interval (+0x214), logged with the frame rate */
|
||||
@@ -259,6 +377,7 @@ volatile uint32_t hos_frame_seconds_bits = 0; /* (the frame's seconds as a flo
|
||||
HOS_FPS_MOVIE=0 (for comparisons). Called by the lifted f_83b7c710 (nx2/oot_patches.py). */
|
||||
uint32_t hos_movie_frames(uint64_t player) {
|
||||
enum { PLACES = 16 };
|
||||
movie_seen(); /* (Triforce: a movie plays, for the frame pacing: pace_handed) */
|
||||
const uint32_t bits = __atomic_load_n(&hos_frame_seconds_bits, __ATOMIC_RELAXED); /* (realtime_step writes it at each present) */
|
||||
if (!bits) return 1;
|
||||
static SRWLOCK lock = SRWLOCK_INIT;
|
||||
@@ -429,9 +548,10 @@ static void realtime_step(int64_t now) {
|
||||
static double samples[5], carry;
|
||||
static unsigned count, next;
|
||||
static long long steps;
|
||||
static StepSteady steady; /* ((Triforce) HOS_STEP_STEADY=1, below) */
|
||||
const double dt = (now - last) / 1e9;
|
||||
last = now;
|
||||
if (dt <= 0 || dt > 0.25) { count = next = 0; carry = 0; return; } /* (a load: the average starts again) */
|
||||
if (dt <= 0 || dt > 0.25) { count = next = 0; carry = 0; step_steady_reset(&steady); return; } /* (a load: the average starts again) */
|
||||
/* (HOS_REALTIME_WINDOW=<1-5>: frames averaged for the step; 1: each step the last frame's own time) */
|
||||
static int window = -1;
|
||||
if (window < 0) {
|
||||
@@ -450,8 +570,25 @@ static void realtime_step(int64_t now) {
|
||||
static int timed_step = -1;
|
||||
if (timed_step < 0) timed_step = !(getenv("HOS_FPS_STEP") && !atoi(getenv("HOS_FPS_STEP")));
|
||||
const int timed = fps_max() > 60 && timed_step && hos_step_probe;
|
||||
const int top = timed ? 60 : fps_max();
|
||||
/* (Triforce, after NX2navi's steady step: host/step_steady.h) HOS_STEP_STEADY (on unless =0): the step is not the mean of the last five
|
||||
intervals but that of the last 12 without their longest and shortest, the paced period where it is within 3% of it, and
|
||||
what the display showed and the game was not given repaid at a 64th a frame (10% of the step at most), within the bounds
|
||||
each path below gives it: a hitch is paid back over most of a second, not in the next five frames, and a cap the game
|
||||
holds steps it by the period exactly. =0: the mean of the last five, as before */
|
||||
double secs_avg = sum / count;
|
||||
{
|
||||
static int steady_on = -1;
|
||||
if (steady_on < 0) {
|
||||
/* ((Triforce, 11-15) On by default, as NX2navi's STEP_STEADY: the lab's A/B cost nothing at the Market; its gain is
|
||||
at hitches (tests/test_step_steady.py). HOS_STEP_STEADY=0: the mean of the last five, as before) */
|
||||
steady_on = !(getenv("HOS_STEP_STEADY") && *getenv("HOS_STEP_STEADY") && atoi(getenv("HOS_STEP_STEADY")) == 0);
|
||||
if (steady_on) hos_log("vi: the steady step: 12 intervals without their longest and shortest, the paced period within 3%%, a hitch repaid slowly (HOS_STEP_STEADY=0: off)");
|
||||
}
|
||||
if (steady_on) secs_avg = step_steady(&steady, dt, (double)pace_period_ns / 1e9, timed ? 1.0 / 240.0 : 1.0 / top, 4.0 / 60.0);
|
||||
}
|
||||
if (timed) {
|
||||
const double secs = sum / count;
|
||||
const double secs = secs_avg;
|
||||
const float f = (float)(secs > 4.0 / 60.0 ? 4.0 / 60.0 : secs < 1.0 / 240.0 ? 1.0 / 240.0 : secs);
|
||||
uint32_t bits;
|
||||
memcpy(&bits, &f, 4);
|
||||
@@ -459,8 +596,7 @@ static void realtime_step(int64_t now) {
|
||||
} else {
|
||||
__atomic_store_n(&hos_frame_seconds_bits, 0, __ATOMIC_RELAXED);
|
||||
}
|
||||
const int top = timed ? 60 : fps_max();
|
||||
const double ratio = sum / count * 60.0;
|
||||
const double ratio = secs_avg * 60.0;
|
||||
int *slot = (int *)GP(mod + 0x2c8);
|
||||
if (*slot < 15 || *slot > (fps_max() > 60 ? fps_max() : 60)) { /* (under a cap below 60 it starts at the game's 60) */
|
||||
static int logged;
|
||||
@@ -474,6 +610,128 @@ static void realtime_step(int64_t now) {
|
||||
*slot = fps;
|
||||
if (!(steps++ % 3000)) hos_log("vi: real-time step on: ratio %.3f, game's frame rate %d", ratio, fps);
|
||||
}
|
||||
/* (Triforce) The Steady frame rate row (steady_frame_rate=, HOS_FPS_STEADY=<0-2> over it; read at the start): 0 Off, the frames
|
||||
paced by the cap as before. 1 (any rate: a display with variable refresh, or VSync off) and 2 (steps of the display's refresh,
|
||||
for VSync without variable refresh): the period the frames are paced by is one this PC holds in the scene, lowered quickly when
|
||||
frames run late and raised slowly when there is room, never shorter than the cap's (base; host/pace_steady.h has the rule and
|
||||
its reasons), and the renderer holds the game's pictures to it (HOS_OPT_PACE_STEADY, gpu/vk/vk_pace.cpp steady_floor_ns,
|
||||
present_smooth.h present_smooth_paced_at): the acquire's wait evens the frames' starts and with them the game's time step
|
||||
(realtime_step's steady step snaps to the period), the present's hold their pictures.
|
||||
What a frame took and needed (pace_steady_measure): its present interval, and the pacing's waits in it, the acquire's
|
||||
(pace_sleep, on this thread or another) and the present's hold (the renderer says how long). A frame another limiter held
|
||||
(pace_handed) is not measured, and the cap's period is back until the game's own wait holds the frames again. A line at
|
||||
each change, and every 5 s what the rule was given (steady_line) */
|
||||
typedef struct { /* (what steady_line says: the frames since its last line) */
|
||||
int64_t interval[2048], took[2048], need[2048], own, other, held, since;
|
||||
int n, frames;
|
||||
int64_t hitches, loads, unmeasured, kept, settling;
|
||||
} SteadyWindow;
|
||||
/* (the 5 s line's last part: the probes' measure, or the wait before a raise without them) */
|
||||
static const char *steady_line_rule(const PaceSteady *s) {
|
||||
static char text[160];
|
||||
if (s->probe)
|
||||
snprintf(text, sizeof text, "%lld probes (%lld found a new scene), the scene's last took %.2f / %.2f ms unpaced "
|
||||
"(p50 / p90)", (long long)s->probes, (long long)s->scenes, s->probe_p50 / 1e6, s->probe_p90 / 1e6);
|
||||
else snprintf(text, sizeof text, "a raise waits for %.0f s since the last change (HOS_FPS_STEADY_PROBE=0)", s->hold_ns / 1e9);
|
||||
return text;
|
||||
}
|
||||
static void steady_line(SteadyWindow *w, PaceSteady *s, int64_t now) {
|
||||
if (!w->since) w->since = now;
|
||||
if (now - w->since < 5000000000LL) return;
|
||||
unsigned late = 0;
|
||||
if (s->count >= PACE_STEADY_SHORT)
|
||||
for (unsigned i = 0; i < PACE_STEADY_SHORT; i++) late += s->took[i] > pace_steady_held(s);
|
||||
const int n = w->n, f = w->frames ? w->frames : 1;
|
||||
#define STEADY_Q(a, q) (n ? pace_steady_select(a, n, (int)(n * (q)) < n ? (int)(n * (q)) : n - 1) / 1e6 : 0.0)
|
||||
const double iv50 = STEADY_Q(w->interval, 0.5), took50 = STEADY_Q(w->took, 0.5), took90 = STEADY_Q(w->took, 0.9);
|
||||
const double need50 = STEADY_Q(w->need, 0.5), need90 = STEADY_Q(w->need, 0.9);
|
||||
#undef STEADY_Q
|
||||
hos_log("vi: steady frame rate: %.1f fps held; the last %.1f s, %d frames: took %.2f / %.2f ms (p50 / p90), needed %.2f / "
|
||||
"%.2f, present interval p50 %.2f; a frame's waits: the acquire's %.2f ms on the presenting thread, %.2f on others, the "
|
||||
"present's hold %.2f; %lld kept, left out %lld hitches, %lld loads, %lld unmeasured, %lld settling after a load; %u of "
|
||||
"the last %d kept late; %lld raised, %lld lowered so far, %s",
|
||||
1e9 / (double)pace_steady_held(s), (now - w->since) / 1e9, w->frames, took50, took90, need50, need90, iv50,
|
||||
w->own / 1e6 / f, w->other / 1e6 / f, w->held / 1e6 / f, (long long)(s->kept - w->kept),
|
||||
(long long)(s->hitches - w->hitches), (long long)(s->loads - w->loads), (long long)(s->unmeasured - w->unmeasured),
|
||||
(long long)(s->settling - w->settling), late, PACE_STEADY_SHORT, (long long)s->raises, (long long)s->lowerings,
|
||||
steady_line_rule(s));
|
||||
w->n = w->frames = 0, w->own = w->other = w->held = 0, w->since = now;
|
||||
w->hitches = s->hitches, w->loads = s->loads, w->unmeasured = s->unmeasured, w->kept = s->kept;
|
||||
w->settling = s->settling;
|
||||
}
|
||||
static int64_t steady_period(int64_t base, int64_t now) {
|
||||
static int mode = -1, cap;
|
||||
static PaceSteady steady;
|
||||
static SteadyWindow window;
|
||||
static int64_t last;
|
||||
static long long frames;
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
presenting_thread = 1; /* (pace_sleep: the acquire's waits on this thread add to the present's hold) */
|
||||
const int64_t own = frame_waited_own_ns, interval = last ? now - last : 0;
|
||||
const int64_t other = (int64_t)__atomic_exchange_n(&frame_waited_other_ns, 0, __ATOMIC_RELAXED);
|
||||
const int handed = __atomic_exchange_n(&frame_handed, 0, __ATOMIC_RELAXED) != 0;
|
||||
frame_waited_own_ns = 0, last = now;
|
||||
if (mode < 0) {
|
||||
const char *e = getenv("HOS_FPS_STEADY");
|
||||
mode = e && *e ? atoi(e) : hos_ini_option("steady_frame_rate", PACE_STEADY_OFF);
|
||||
if (mode < PACE_STEADY_OFF || mode > PACE_STEADY_REFRESH) mode = PACE_STEADY_OFF;
|
||||
}
|
||||
if (mode == PACE_STEADY_OFF) return base;
|
||||
frames++;
|
||||
if (cap != fps_max()) {
|
||||
/* (the set, once the cap is settled: on Monitor's rate fps_max is 60 until the renderer answers. Steps of the refresh
|
||||
wait for the renderer to say the display's rate, half a minute of frames at most: then the set of any rate) */
|
||||
int refresh = mode == PACE_STEADY_REFRESH ? rx_option(HOS_OPT_DISPLAY_CAP, -1) : 0;
|
||||
if (refresh < 0 && frames < 1800) return base;
|
||||
pace_steady_init(&steady, mode, fps_max(), refresh);
|
||||
cap = fps_max();
|
||||
/* ((Triforce, test31) HOS_FPS_STEADY_PROBE=0: the rate by the frames' own measure as they run, as before the probes) */
|
||||
const char *pe = getenv("HOS_FPS_STEADY_PROBE");
|
||||
steady.probe = !(pe && *pe && atoi(pe) == 0);
|
||||
hos_log("vi: steady frame rate: %s, %d rates from the cap's %d fps to %.1f, %s (%s)",
|
||||
mode == PACE_STEADY_REFRESH && refresh > 0 ? "steps of the display's refresh" : "any rate", steady.n,
|
||||
cap, 1e9 / (double)steady.periods[steady.n - 1],
|
||||
steady.probe ? "chosen by unpaced probes" : "chosen by the frames as they run (HOS_FPS_STEADY_PROBE=0)",
|
||||
mode == PACE_STEADY_REFRESH && refresh <= 0 ? "the display's refresh rate is not known" : "HOS_FPS_STEADY=0: off");
|
||||
}
|
||||
const int64_t held = (int64_t)rx_option(HOS_OPT_PACE_STEADY, 0) * 1000; /* (the present's hold, since the last present) */
|
||||
if (handed) {
|
||||
if (steady.at || steady.count) pace_steady_reset(&steady), hos_log("vi: steady frame rate: another limiter holds the frames");
|
||||
rx_option_set(HOS_OPT_PACE_STEADY, 0);
|
||||
window.frames++;
|
||||
steady_line(&window, &steady, now);
|
||||
return base;
|
||||
}
|
||||
const int64_t before = pace_steady_held(&steady);
|
||||
int64_t took, need;
|
||||
pace_steady_measure(interval, own + held, other, &took, &need);
|
||||
if (other > 0) steady.one_step = 1; /* (the acquire on another thread: what a frame needs is not always known) */
|
||||
if (interval > 0 && interval <= PACE_STEADY_LOAD_NS && window.n < 2048) {
|
||||
window.interval[window.n] = interval, window.took[window.n] = took, window.need[window.n] = need;
|
||||
window.n++;
|
||||
}
|
||||
window.frames++, window.own += own, window.other += other, window.held += held;
|
||||
if (pace_steady_feed(&steady, took, need, interval)) {
|
||||
const int64_t p = pace_steady_held(&steady);
|
||||
if (steady.probe)
|
||||
hos_log("vi: steady frame rate: %.1f fps (was %.1f): the last three probes' %u frames took %.2f ms unpaced at the 90th "
|
||||
"percentile, %.2f at the median (%lld probes, %lld raised, %lld lowered)", 1e9 / (double)p, 1e9 / (double)before,
|
||||
steady.probe_count, steady.probe_p90 / 1e6, steady.probe_p50 / 1e6, (long long)steady.probes,
|
||||
(long long)steady.raises, (long long)steady.lowerings);
|
||||
else
|
||||
hos_log("vi: steady frame rate: %.1f fps (was %.1f): the last %u frames needed %.2f ms at the 90th percentile, %.2f at "
|
||||
"the 95th (%lld raised, %lld lowered, %lld hitches left out; the next raise waits %.0f s at least)", 1e9 / (double)p,
|
||||
1e9 / (double)before, steady.count, pace_steady_quantile(&steady, PACE_STEADY_RING, 0.90) / 1e6,
|
||||
pace_steady_quantile(&steady, PACE_STEADY_RING, 0.95) / 1e6, (long long)steady.raises,
|
||||
(long long)steady.lowerings, (long long)steady.hitches, steady.hold_ns / 1e9);
|
||||
}
|
||||
steady_line(&window, &steady, now);
|
||||
const int64_t p = pace_steady_period(&steady) > base ? pace_steady_period(&steady) : base;
|
||||
rx_option_set(HOS_OPT_PACE_PERIOD, (int)p);
|
||||
rx_option_set(HOS_OPT_PACE_STEADY, 1);
|
||||
return p;
|
||||
}
|
||||
/* HOS_PRESENT_TRACE=<csv>: native game present-call returns, not GPU completion or display times.
|
||||
Uses the existing frame ID/clock; a forced stop can lose the last second of buffered present records. */
|
||||
static void present_trace(int64_t frame, int64_t now) {
|
||||
@@ -510,7 +768,12 @@ void hos_vi_native_presented(void) {
|
||||
if (ms < 0) ms = getenv("HOS_FRAME_SLEEP_MS") ? atoi(getenv("HOS_FRAME_SLEEP_MS")) : 0;
|
||||
if (ms) hos_sleep_ns(ms * 1000000LL);
|
||||
}
|
||||
const int64_t period = 1000000000LL / fps_max() * present_interval, now = hos_now_ns();
|
||||
/* (a cap under 60 paces by itself, whatever interval the game asked for while Monitor's rate was not known yet) */
|
||||
const int interval = fps_max() < 60 ? 1 : present_interval;
|
||||
const int64_t now = hos_now_ns();
|
||||
/* ((Triforce) the Steady frame rate row: a period this PC holds in the scene, never shorter than the cap's) */
|
||||
const int64_t period = steady_period(1000000000LL / fps_max() * interval, now);
|
||||
pace_period_ns = period; /* (Triforce: what another limiter is given to hold in this file's place, pace_handed) */
|
||||
realtime_step(now);
|
||||
/* (behind by more than a frame: the next one is paced from now, with no burst to catch up) */
|
||||
next_frame_at = next_frame_at && now - next_frame_at < period ? next_frame_at + period : now + period;
|
||||
@@ -534,17 +797,29 @@ void hos_vi_native_presented(void) {
|
||||
/* (present-to-present intervals of the window, in buckets: which frames miss their vsync) */
|
||||
static int64_t last_present;
|
||||
static int hist[6];
|
||||
/* ((Triforce) and against the period the frames are paced by, which the buckets above, made for 60, do not see at a cap
|
||||
of 240 (every interval under 15.5 ms): from under three quarters of it to over twice it, and the intervals' mean
|
||||
distance from it, the frame-time spread a change of the pacing shows in) */
|
||||
static int rel[6];
|
||||
static int64_t off_sum;
|
||||
if (last_present) {
|
||||
const int64_t d = now - last_present;
|
||||
const int64_t d = now - last_present, off = d > period ? d - period : period - d;
|
||||
hist[d < 15500000 ? 0 : d < 17800000 ? 1 : d < 22000000 ? 2 : d < 30000000 ? 3 : d < 40000000 ? 4 : 5]++;
|
||||
rel[d * 4 < period * 3 ? 0 : d * 20 < period * 19 ? 1 : d * 20 <= period * 21 ? 2 : d * 4 <= period * 5 ? 3 : d <= period * 2 ? 4 : 5]++;
|
||||
off_sum += off;
|
||||
}
|
||||
last_present = now;
|
||||
if (now - window_start >= 5000000000LL) {
|
||||
hos_log("vi: %lld frames presented (%.1f fps over the last %.1f s, interval %d)", (long long)n,
|
||||
frames_in_window * 1e9 / (double)(now - window_start), (now - window_start) / 1e9, present_interval);
|
||||
frames_in_window * 1e9 / (double)(now - window_start), (now - window_start) / 1e9, interval);
|
||||
hos_log("vi: present intervals: <15.5 ms %d, 15.5-17.8 %d, 17.8-22 %d, 22-30 %d, 30-40 %d, over 40 %d", hist[0],
|
||||
hist[1], hist[2], hist[3], hist[4], hist[5]);
|
||||
const int counted = rel[0] + rel[1] + rel[2] + rel[3] + rel[4] + rel[5];
|
||||
hos_log("vi: frame intervals against the paced period of %.3f ms: under 0.75 of it %d, 0.75-0.95 %d, 0.95-1.05 %d, "
|
||||
"1.05-1.25 %d, 1.25-2 %d, over 2 %d; %.3f ms from it on average", period / 1e6, rel[0], rel[1], rel[2], rel[3],
|
||||
rel[4], rel[5], counted ? off_sum / 1e6 / counted : 0.0);
|
||||
memset(hist, 0, sizeof hist);
|
||||
memset(rel, 0, sizeof rel), off_sum = 0;
|
||||
window_start = now, frames_in_window = 0;
|
||||
const uint64_t pacer = hos_mkw_pacer, freq = pacer ? G64(0x837de940) : 0; /* (MKW only: nvn_native.c) */
|
||||
if (pacer && freq)
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
Ioctl numbers are Linux-style: nr bits 0-7, group 8-15, size 16-29, direction 30-31. Structures and values follow
|
||||
suyu's nvdrv (src/core/hle/service/nvdrv, GPL-2.0-or-later) and switchbrew "NV services". */
|
||||
#include "hos.h"
|
||||
#include "guest_bounds.h" /* ((Triforce) hos_range_ok: MAP_BUFFER_EX's remap check) */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -407,7 +408,10 @@ static uint32_t ioctl_as_gpu(uint32_t nr, uint64_t p, uint64_t size) {
|
||||
const uint64_t va = G64(p + 32);
|
||||
LOCK();
|
||||
const int i = map_floor(va);
|
||||
const int ok = i >= 0 && va + G64(p + 16) + G64(p + 24) <= maps[i].va + maps[i].size;
|
||||
/* ((Triforce) inside the mapping counted from its start: va + offset + size wrapped past 2^64 for a huge
|
||||
offset or size and passed) */
|
||||
const uint64_t at = i >= 0 ? va - maps[i].va : 0, off = G64(p + 16), size = G64(p + 24);
|
||||
const int ok = i >= 0 && hos_range_ok(at, off, maps[i].size) && hos_range_ok(at + off, size, maps[i].size);
|
||||
UNLOCK();
|
||||
MAP_LOG("nvdrv: remap GPU %#llx +%#llx size %#llx kind %#x: %s", (unsigned long long)va,
|
||||
(unsigned long long)G64(p + 16), (unsigned long long)G64(p + 24), G32(p + 4), ok ? "ok" : "no mapping");
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
own procs (program builders, native images, sparse tile layouts, recording modes). The SDK's exports reach the
|
||||
same functions as the proc table (nvn2_dispatch, from gen/nvn2_exports.c). */
|
||||
#include "hos.h"
|
||||
#include "guest_bounds.h" /* ((Triforce) hos_span_ok: the game's first and count checked without an int overflow) */
|
||||
#include "../gpu/render/rx_nvn.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -1054,10 +1055,14 @@ static void dump_stage(uint64_t code, uint64_t ctl) {
|
||||
MEMORY_BASIC_INFORMATION mbi;
|
||||
while (n < 0x10000 && VirtualQuery(GP(code + n), &mbi, sizeof mbi) && mbi.State == MEM_COMMIT) n += 0x1000;
|
||||
FILE *f = fopen(path, "wb");
|
||||
int io = hos_guest_io_begin(GP(code), n); /* (Triforce: the kernel reads guest memory here: rx_backend.c) */
|
||||
if (f) { fwrite(GP(code), 1, (size_t)n, f); fclose(f); }
|
||||
hos_guest_io_end(io);
|
||||
snprintf(path, sizeof path, "%s/%010llx.ctl", dir, (unsigned long long)code);
|
||||
f = fopen(path, "wb");
|
||||
io = hos_guest_io_begin(GP(ctl), 0x800);
|
||||
if (f) { fwrite(GP(ctl), 1, 0x800, f); fclose(f); }
|
||||
hos_guest_io_end(io);
|
||||
}
|
||||
static int set_shaders(nvn_prog *o, int n, const uint8_t *sd, int stride) {
|
||||
if (n < 0 || n > 6) return 0;
|
||||
@@ -1918,7 +1923,7 @@ NV(nvnCommandBufferClearDepthStencil) {
|
||||
}
|
||||
NV(nvnCommandBufferSetViewports) {
|
||||
const int first = I(1), n = I(2);
|
||||
if (n < 0 || first < 0 || first + n > 16) die("SetViewports %d+%d", first, n);
|
||||
if (!hos_span_ok(first, n, 16)) die("SetViewports %d+%d", first, n); /* ((Triforce) first + n in an int wrapped) */
|
||||
nvn_p_viewports *r = REC(CB(P(0)), NOP_VIEWPORTS, nvn_p_viewports);
|
||||
r->first = first, r->n = n;
|
||||
memcpy(r->v, PN(3, 16 * (size_t)n), 16 * (size_t)n);
|
||||
@@ -2010,7 +2015,7 @@ NV(nvnCommandBufferBindVertexStreamState) {
|
||||
hw_fix(o, 4 * (16 + (uint64_t)n));
|
||||
}
|
||||
static void bind_vbufs(nvn_cmd *o, int first, int n, const NVNbufferRange *rg) {
|
||||
if (n < 0 || first < 0 || first + n > 16) die("BindVertexBuffers %d+%d", first, n);
|
||||
if (!hos_span_ok(first, n, 16)) die("BindVertexBuffers %d+%d", first, n); /* ((Triforce) as SetViewports) */
|
||||
nvn_p_bind_vbuf *r = RECN(o, NOP_BIND_VBUF, nvn_p_bind_vbuf, r, n);
|
||||
r->first = first, r->n = n;
|
||||
memcpy(r->r, rg, sizeof *rg * (size_t)n);
|
||||
@@ -2564,6 +2569,8 @@ NV(nvnQueuePresentTexture) {
|
||||
extern volatile LONG64 hos_present_count; /* display.c */
|
||||
InterlockedIncrement64(&hos_present_count);
|
||||
hos_vi_native_presented();
|
||||
void hos_mod_cheats_frame(void); /* (Triforce) mod_cheats.c: the mods' cheat files, once a game frame; nothing without one */
|
||||
hos_mod_cheats_frame();
|
||||
}
|
||||
|
||||
/* ---- NVN2 (mkw-recomp): what the Switch 2's NVN adds, as MKW uses it ---- */
|
||||
|
||||
261
host/oot_menu.c
261
host/oot_menu.c
@@ -13,6 +13,7 @@
|
||||
and hands it to the focus manager (*(screen+32)+96, its vtable +48), when the manager takes the player's input.
|
||||
HOS_MENU_TRACE=1: the focus changes, the screens and what the mouse does logged. */
|
||||
#include "hos.h"
|
||||
#include "guest_bounds.h" /* ((Triforce) hos_range_ok: set_item_key's buffer) */
|
||||
/* (Triforce) The game's own Options screen (the layout SystemOption_00; the title's Options and the pause menu's are the one
|
||||
screen), by its vtable (HOS_MENU_TRACE in a headless run, 2026-11-10: the screen of 15 entries that starts to run when
|
||||
Options is chosen, from the title and from the pause menu alike, and stops with Back; L and R do nothing on it). While
|
||||
@@ -150,7 +151,8 @@ static int set_item_key(Ctx *c, uint64_t screen, const char *key) {
|
||||
peek(screen + 840, &mode, 4);
|
||||
const size_t n = strlen(key);
|
||||
A64Fn fn = a64_lookup(0x833fda00ULL);
|
||||
if (!fn || !buf || buf + n + 1 > HOS_GUEST_SPACE || room <= (int32_t)n || mode != 0) return 0;
|
||||
/* ((Triforce) the buffer's end without buf + n + 1, which wrapped past 2^64 for a pointer read wrong) */
|
||||
if (!fn || !buf || !hos_range_ok(buf, n + 1, HOS_GUEST_SPACE) || room <= (int32_t)n || mode != 0) return 0;
|
||||
memcpy(GP(buf), key, n + 1);
|
||||
const Ctx saved = *c;
|
||||
c->x[0] = screen, c->x[1] = screen + 760, c->x[30] = 0;
|
||||
@@ -244,6 +246,15 @@ int hos_oot_options_front(void) {
|
||||
const LONG64 t = InterlockedCompareExchange64(&options_seen_ns, 0, 0);
|
||||
return t && (LONG64)hos_now_ns() - t < 200000000LL;
|
||||
}
|
||||
/* (Triforce) the renderer is told of the Options screen (its tabs over the header): HOS_OPTIONS_TABS=0 keeps it untold */
|
||||
static int options_tabs_on(void) {
|
||||
static int tabs = -1;
|
||||
if (tabs < 0) {
|
||||
const char *e = getenv("HOS_OPTIONS_TABS");
|
||||
tabs = !(e && *e == '0');
|
||||
}
|
||||
return tabs;
|
||||
}
|
||||
static volatile uint64_t hud_screen;
|
||||
static int map_floors(int *cur, int *top, int *bottom) {
|
||||
int32_t f[3];
|
||||
@@ -311,14 +322,58 @@ static void list_boxes(Hit *h) {
|
||||
memcpy(h->rect, out, (size_t)h->n * sizeof *out);
|
||||
}
|
||||
|
||||
/* (Triforce) The game's own Options screen (OPTIONS_SCREEN_VTABLE): its rows as the mouse takes them. The screen keeps a
|
||||
widget for each row in one of three lists by the row's kind, as its focus handler (f_83423f00, the vtable's +0x170) and
|
||||
its setup (f_83424450) walk them by the kind in the row's entry (+4: 0, 1, 2): +712 the count and +720 the widgets of
|
||||
kind 0, +728 and +736 of kind 1, +696 and +704 of kind 2. A widget's button is f_80df5410(*(*(widget+24)+56),
|
||||
*(widget+32)), as f_833eff40 and the focus handler find it: the one the focus handler is handed, the button of the
|
||||
screen's list (its focus at +56). What a kind is, from their setups: kind 1's (f_833ef590) reads the row's count of values
|
||||
(entry+16) and kind 0's (f_833eff50) does not: the two-choice toggles (On | Off) and the choices of more (< value >);
|
||||
kind 2, the third look the screen has, the plates (amiibo, ZELDA NOTES; the screen's query f_83424580 asks whether the
|
||||
focus is on one). (Read from the lifted code, not traced: HOS_MENU_TRACE logs the kind each click finds.) The value's
|
||||
panel, in the layout's units (from the screen as shown, as gpu/vk/vk_menu.cpp draws its own rows like it: the list's
|
||||
middle at x -730, the panel 804 wide with its middle 500 right of that): x -632 to 172, a toggle's halves meeting at
|
||||
0.632 of it (x -124), a choice's arrows at its ends. The rows are shown from y 760 down to -960 (the header above, 200
|
||||
units: our tabs over it; the guides below): a row laid out past that (scrolled away) is not pointed at */
|
||||
enum { OPT_KIND_NONE = -1, OPT_TOGGLE = 0, OPT_CHOICE = 1, OPT_PLATE = 2 };
|
||||
#define OPT_VALUE_X0 (-632.0f)
|
||||
#define OPT_VALUE_X1 172.0f
|
||||
#define OPT_TOGGLE_MEET (-124.0f)
|
||||
#define OPT_ROWS_TOP 760.0f
|
||||
#define OPT_ROWS_BOTTOM (-960.0f)
|
||||
#define OPT_HEADER_Y 880.0f
|
||||
static int options_screen(uint64_t screen) { return peek64(screen) == OPTIONS_SCREEN_VTABLE; }
|
||||
static int options_row_kind(Ctx *c, uint64_t screen, uint64_t button) {
|
||||
static const struct { int at, kind; } lists[] = {{712, OPT_TOGGLE}, {728, OPT_CHOICE}, {696, OPT_PLATE}};
|
||||
for (int l = 0; l < 3; l++) {
|
||||
int32_t n = 0;
|
||||
peek(screen + (uint64_t)lists[l].at, &n, 4);
|
||||
const uint64_t widgets = peek64(screen + (uint64_t)lists[l].at + 8);
|
||||
for (int i = 0; i < n && i < 64; i++) {
|
||||
const uint64_t w = peek64(widgets + 8ull * (unsigned)i), holder = peek64(w + 24), pane = peek64(holder + 56);
|
||||
if (w && pane && guest_call(c, 0x80df5410ULL, pane, peek64(w + 32), NULL, NULL) == button) return lists[l].kind;
|
||||
}
|
||||
}
|
||||
return OPT_KIND_NONE;
|
||||
}
|
||||
/* the pad's press a click on an Options row is, at x (the layout's units): on a toggle's or a choice's value the left part
|
||||
Left and the right part Right (a toggle's half is chosen so, a choice steps down or up; at an end the game does nothing);
|
||||
on a plate, a value's name, or a row of no kind found, A */
|
||||
static const char *options_click(int kind, float x) {
|
||||
if ((kind != OPT_TOGGLE && kind != OPT_CHOICE) || x < OPT_VALUE_X0 || x > OPT_VALUE_X1) return "A";
|
||||
return x < (kind == OPT_TOGGLE ? OPT_TOGGLE_MEET : (OPT_VALUE_X0 + OPT_VALUE_X1) / 2) ? "LEFT" : "RIGHT";
|
||||
}
|
||||
|
||||
static void hit_test(uint64_t screen, Hit *h) {
|
||||
h->n = 0, h->hit = 0;
|
||||
each_entry(screen, 400, shown_entry, h);
|
||||
list_boxes(h);
|
||||
if (h->y > -1080 && h->y < -960 && h->x >= 1600 && h->x < 1900) return; /* (the "Back" prompt's: no button's) */
|
||||
const int options = options_screen(screen);
|
||||
for (int i = 0; i < h->n; i++) {
|
||||
const Rect *r = &h->rect[i];
|
||||
if (h->x < r->x0 || h->x > r->x1 || h->y < r->y0 || h->y > r->y1) continue;
|
||||
if (options && ((r->y0 + r->y1) / 2 > OPT_ROWS_TOP || (r->y0 + r->y1) / 2 < OPT_ROWS_BOTTOM)) continue; /* (scrolled away) */
|
||||
const float area = (r->x1 - r->x0) * (r->y1 - r->y0);
|
||||
if (!h->hit || area < h->area) h->hit = h->button[i], h->area = area; /* (the smallest: a slot inside a panel) */
|
||||
}
|
||||
@@ -342,6 +397,7 @@ static void focus_entry(int i, uint64_t entry, uint64_t button, void *arg); /*
|
||||
frame the lock is busy is passed, not waited for); the focus hook's writes take it too */
|
||||
static SRWLOCK mouse_lock = SRWLOCK_INIT;
|
||||
static void mouse_tick_locked(Ctx *c, uint64_t screen);
|
||||
static int mods_entry_hit(float x, float y); /* ((Triforce) below: the Mods entry under a list's last button) */
|
||||
/* (mouse_owner: the thread in mouse_tick: the game's focus handler may run inside its setter) */
|
||||
static void mouse_tick(Ctx *c, uint64_t screen) {
|
||||
if (!TryAcquireSRWLockExclusive(&mouse_lock)) return;
|
||||
@@ -501,8 +557,9 @@ static void mouse_tick_locked(Ctx *c, uint64_t screen) {
|
||||
const int step = wheel < 0 ? 1 : -1, want = (k < 3 ? k : 3) * step;
|
||||
/* (the rows in view: the screen's height, or Threads of Time's list frame, y -756 to 594 (from the page as
|
||||
shown): the game takes a row as shown once its centre is in it, and scrolls a focused row only that far) */
|
||||
const int threads = peek64(screen) == 0x84c04a18ULL;
|
||||
const float view_lo = threads ? -760.0f : -1080.0f, view_hi = threads ? 598.0f : 1080.0f;
|
||||
const int threads = peek64(screen) == 0x84c04a18ULL, options = options_screen(screen); /* ((Triforce) and the Options' list) */
|
||||
const float view_lo = threads ? -760.0f : options ? OPT_ROWS_BOTTOM : -1080.0f;
|
||||
const float view_hi = threads ? 598.0f : options ? OPT_ROWS_TOP : 1080.0f;
|
||||
int first = -1, last = -1; /* (the rows shown: their first and last in the order) */
|
||||
for (int j = 0; j < n_rows; j++) {
|
||||
const float cy = (hit.rect[order[j]].y0 + hit.rect[order[j]].y1) / 2;
|
||||
@@ -542,6 +599,9 @@ static void mouse_tick_locked(Ctx *c, uint64_t screen) {
|
||||
if (cy >= scroll_lo && cy <= scroll_hi) refocus_ns = now;
|
||||
}
|
||||
if (refocus_ns && now >= refocus_ns) refocus_ns = 0, scroll_row = 0, moved = 1, asked = 0;
|
||||
/* ((Triforce) the Options screen's header is our tabs' (gpu/vk/vk_menu.cpp draw_game_tab: the renderer reads the same
|
||||
clicks through hos_mouse_pointer, display.c): neither pointed at nor clicked here) */
|
||||
if (options_screen(screen) && options_tabs_on() && hit.y >= OPT_HEADER_Y) moved = 0, pressed &= ~(uint32_t)HOS_MOUSE_LEFT;
|
||||
if (moved || (pressed & HOS_MOUSE_LEFT)) hit_test(screen, &hit);
|
||||
else hit.hit = 0;
|
||||
/* hover: the focus to the button under the mouse, only as it moves (the pad keeps the focus it moved) */
|
||||
@@ -697,6 +757,12 @@ static void mouse_tick_locked(Ctx *c, uint64_t screen) {
|
||||
if (tap[0]) hos_hid_tap(tap);
|
||||
if (trace) hos_log("menu: panel page %d click (%.0f, %.0f): %s", page, hit.x, hit.y, tap[0] ? tap : "-");
|
||||
}
|
||||
/* ((Triforce) a click on the Mods entry under a list's last button: the renderer opens Mods; the game is not given it) */
|
||||
if ((pressed & HOS_MOUSE_LEFT) && !hit.hit && mods_entry_hit(hit.x, hit.y)) {
|
||||
rx_option_set(HOS_OPT_MODS_ENTER, 2);
|
||||
pressed &= ~(uint32_t)HOS_MOUSE_LEFT;
|
||||
if (trace) hos_log("menu: the Mods entry clicked (%.0f, %.0f)", hit.x, hit.y);
|
||||
}
|
||||
/* The "Back" prompt at the bottom right (B), on every screen with one (the title's load list, the pause menu, the
|
||||
map; in the layout's units, measured from their screens): a click there off the buttons */
|
||||
if ((pressed & HOS_MOUSE_LEFT) && !hit.hit && hit.y > -1080 && hit.y < -960 && hit.x >= 1600 && hit.x < 1900) {
|
||||
@@ -705,7 +771,17 @@ static void mouse_tick_locked(Ctx *c, uint64_t screen) {
|
||||
}
|
||||
/* a click: the button under it decided (A: after the focus has landed, at the next update, when it moved it), the
|
||||
right button back (B) */
|
||||
if ((pressed & HOS_MOUSE_LEFT) && hit.hit) hos_hid_tap(just || hit.hit == asked ? "0.05:A/80" : "0:A/80");
|
||||
/* ((Triforce) on the Options screen the press is by the row's kind and where on it: options_click) */
|
||||
if ((pressed & HOS_MOUSE_LEFT) && hit.hit) {
|
||||
const int kind = options_screen(screen) ? options_row_kind(c, screen, hit.hit) : OPT_KIND_NONE;
|
||||
const char *press = options_click(kind, hit.x);
|
||||
char tap[32];
|
||||
snprintf(tap, sizeof tap, "%s:%s/80", just || hit.hit == asked ? "0.05" : "0", press);
|
||||
hos_hid_tap(tap);
|
||||
if (trace && options_screen(screen))
|
||||
hos_log("menu: Options: a click (%.0f, %.0f) on a row of kind %d (0 toggle, 1 choice, 2 plate, -1 none found): %s",
|
||||
hit.x, hit.y, kind, tap);
|
||||
}
|
||||
if (pressed & HOS_MOUSE_RIGHT) hos_hid_tap("0:B/80");
|
||||
}
|
||||
|
||||
@@ -737,10 +813,158 @@ static int button_rect(uint64_t button, Rect *r) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ---- (Triforce) The Mods entry under a game list's last button: the pause menu's System page (Save, Load, Options,
|
||||
Special Actions, Title Screen: its buttons left of the Basic Controls panel, x under 0) and the title's list (by its
|
||||
screen's vtable). Each frame such a list runs the renderer is told its last button's box and whether the game's cursor
|
||||
is on it (HOS_OPT_MODS_*, gpu/vk/vk_api.h); nn_hid.c then keeps Down from the game (hos_oot_mods_down) and the renderer
|
||||
takes the cursor onto Mods. A click on the entry opens it (mouse_tick_locked) */
|
||||
#define TITLE_LIST_VTABLE 0x84c106e8ULL
|
||||
static volatile LONG64 mods_last_ns; /* (the last frame a list's last button had the game's cursor) */
|
||||
static Rect mods_entry; /* (the entry's box, as the renderer draws it: under the last button by the pitch) */
|
||||
static volatile LONG64 mods_entry_ns;
|
||||
typedef struct { Rect r[16]; uint64_t b[16]; int n, pause; } ModsList;
|
||||
static void mods_list_entry(int i, uint64_t entry, uint64_t button, void *arg) {
|
||||
(void)i, (void)entry;
|
||||
ModsList *l = arg;
|
||||
Rect r;
|
||||
if (l->n >= 16 || !button_rect(button, &r)) return;
|
||||
if (l->pause) { /* (the System page's buttons, Pa_Button_*, in its left column; the other pages' slots are in the list too, off the screen) */
|
||||
char name[32];
|
||||
const float cx = (r.x0 + r.x1) / 2;
|
||||
if (cx > 0 || cx < -1920 || strncmp(text_at(peek64(button + 0x18), name, sizeof name), "Pa_Button_", 10)) return;
|
||||
}
|
||||
l->r[l->n] = r, l->b[l->n++] = button;
|
||||
}
|
||||
int rx_option(int id, int otherwise); /* rx_backend.c */
|
||||
/* While Mods has the cursor the game's last button is let go as the game itself lets a button go when its cursor moves on
|
||||
(11-17, a workflow over the lift, its findings checked by skeptics; then the lab): the focus controller (the manager
|
||||
*(*(screen+32)+96), its controllers at +40 and +48: +120 the current entry, +128 a move asked, not made) calls the old
|
||||
button's "cursor left" (vtable+336, f_80afe540: an OFF event in the button's own queue at +58..+62, and the button linked
|
||||
into its screen's update list, which plays its focus-out: +56 3 -> 2 -> 0) and the new one's "cursor entered" (vtable+328,
|
||||
f_80aa9150: an ON event). The controller is left alone: its +120 stays on the last button, so the game's cursor is where it
|
||||
was when Mods closes, and "cursor entered" then lights it again (the pause menu's own focus handler, f_802ed660, replays the
|
||||
System option's look and help text with it). Raw writes of the state bytes do nothing: the events drive it.
|
||||
Decided under seen_lock (mods_list_tick), done after it (hos_oot_menu_tick): ModsCall */
|
||||
static struct { uint64_t screen, button; int64_t since_ns; } mods_held; /* (seen_lock) */
|
||||
typedef struct { int op; uint64_t screen, button; int title; } ModsCall; /* (op 1 let go, 2 give back) */
|
||||
static int mods_button_vt(uint64_t vt) { /* (the framework's button classes: setState f_80ad5310 at +248 in each) */
|
||||
static const uint64_t v[] = {0x84c41280ULL, 0x84c416e8ULL, 0x84c42808ULL, 0x84c42990ULL, 0x84c42b00ULL,
|
||||
0x84c42c90ULL, 0x84c42e08ULL, 0x84c42fb8ULL, 0x84c431c8ULL, 0x84c43338ULL};
|
||||
for (int i = 0; i < 10; i++)
|
||||
if (vt == v[i]) return 1;
|
||||
return 0;
|
||||
}
|
||||
/* the button the game's cursor is on for this screen (0: none); *pending: a move asked of the controller and not made yet */
|
||||
static uint64_t mods_cursor_button(uint64_t screen, int *pending) {
|
||||
const uint64_t mgr = peek64(peek64(screen + 32) + 96);
|
||||
*pending = 0;
|
||||
for (uint64_t k = 0; mgr && k < 2; k++) {
|
||||
const uint64_t ctrl = peek64(mgr + 40 + 8 * k), e = ctrl ? peek64(ctrl + 120) : 0;
|
||||
if (!e || peek64(e + 32) != screen) continue;
|
||||
*pending = peek64(ctrl + 128) != 0;
|
||||
return peek64(e + 24);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#define PAUSE_SCREEN_VTABLE 0x84c0baa8ULL
|
||||
static void mods_call(Ctx *c, const ModsCall *m) {
|
||||
const uint64_t b = m->button;
|
||||
if (!m->op || !b || !mods_button_vt(peek64(b))) return;
|
||||
uint8_t st = 0;
|
||||
uint32_t fl = 0;
|
||||
peek(b + 56, &st, 1), peek(b + 64, &fl, 4);
|
||||
int called = 0;
|
||||
if (m->op == 1 && (st == 1 || st == 3) && (fl & 0x2)) {
|
||||
guest_call(c, peek64(peek64(b) + 336), b, 0, NULL, NULL), called = 1;
|
||||
/* (the System page: its option's own "chosen" look rewound, as the game does to every option not focused, f_833ee800) */
|
||||
if (!m->title && peek64(m->screen) == PAUSE_SCREEN_VTABLE) {
|
||||
const uint64_t sys = peek64(m->screen + 800);
|
||||
for (uint64_t k = 56; sys && k <= 88; k += 8) {
|
||||
const uint64_t w = peek64(sys + k);
|
||||
if (w && peek64(w + 96) == b && peek64(w + 72)) { guest_call(c, 0x833d90f0ULL, w, 0, NULL, NULL); break; }
|
||||
}
|
||||
}
|
||||
} else if (m->op == 2 && (st == 0 || st == 2) && (fl & 0x11) == 0x11) {
|
||||
guest_call(c, peek64(peek64(b) + 328), b, 0, NULL, NULL), called = 1;
|
||||
}
|
||||
if (trace) {
|
||||
uint8_t st2 = 0;
|
||||
peek(b + 56, &st2, 1);
|
||||
hos_log("menu: mods entry: the game's last button %s (state %u -> %u, flags %#x)%s", m->op == 1 ? "let go" : "given back",
|
||||
st, st2, fl, called ? "" : ": not called");
|
||||
}
|
||||
}
|
||||
static void mods_list_tick(uint64_t screen, int title, ModsCall *call) {
|
||||
ModsList l = {0};
|
||||
l.pause = !title;
|
||||
each_entry(screen, 400, mods_list_entry, &l);
|
||||
if (l.n < 2) return;
|
||||
int last = 0, above = -1;
|
||||
for (int i = 1; i < l.n; i++)
|
||||
if ((l.r[i].y0 + l.r[i].y1) < (l.r[last].y0 + l.r[last].y1)) last = i;
|
||||
for (int i = 0; i < l.n; i++)
|
||||
if (i != last && (above < 0 || (l.r[i].y0 + l.r[i].y1) < (l.r[above].y0 + l.r[above].y1))) above = i;
|
||||
const Rect r = l.r[last];
|
||||
const float pitch = (l.r[above].y0 + l.r[above].y1) / 2 - (r.y0 + r.y1) / 2;
|
||||
if (!(pitch > 20 && pitch < 600)) return;
|
||||
/* The button's state byte (+56) is its focus animation's: 0 none, 1 coming, 3 focused, 2 going. While Mods has the
|
||||
cursor the game gets no input (nn_hid.c) and its last button is let go (mods_held, above) */
|
||||
uint8_t state = 0;
|
||||
peek(l.b[last] + 56, &state, 1);
|
||||
const int64_t now = hos_now_ns();
|
||||
{
|
||||
const int sel = rx_option(HOS_OPT_MODS_SELECTED, 0) != 0;
|
||||
int pending = 0;
|
||||
const uint64_t cur = mods_cursor_button(screen, &pending), btn = l.b[last];
|
||||
if (mods_held.button && (mods_held.screen != screen || mods_held.button != btn)) memset(&mods_held, 0, sizeof mods_held);
|
||||
if (sel && !mods_held.button && (state == 1 || state == 3) && cur == btn && !pending) {
|
||||
*call = (ModsCall){1, screen, btn, title};
|
||||
mods_held.screen = screen, mods_held.button = btn, mods_held.since_ns = 0;
|
||||
} else if (sel && mods_held.button) {
|
||||
mods_held.since_ns = 0;
|
||||
if ((state == 1 || state == 3) && cur == btn) *call = (ModsCall){1, screen, btn, title}; /* (lit again: again) */
|
||||
} else if (!sel && mods_held.button) {
|
||||
if (!mods_held.since_ns) mods_held.since_ns = now;
|
||||
if (cur == btn && !pending) *call = (ModsCall){2, screen, btn, title}, memset(&mods_held, 0, sizeof mods_held);
|
||||
else if (cur || now - mods_held.since_ns > 1000000000LL) memset(&mods_held, 0, sizeof mods_held); /* (moved on) */
|
||||
}
|
||||
}
|
||||
{ /* ((Triforce) 11-17, his picture: Continue opened the Load list over the title, whose list still runs, and "Mods" stayed
|
||||
on it) The entry only while the game's cursor is on this list (its focus controller's entry is this screen's), or
|
||||
Mods has the cursor */
|
||||
int pend = 0;
|
||||
if (!mods_cursor_button(screen, &pend) && !mods_held.button) return;
|
||||
}
|
||||
/* (coming or there; 2 is its focus going to another button. Let go for Mods, it is still the game's cursor's place) */
|
||||
const int focused = state == 1 || state == 3 || (mods_held.button && mods_held.button == l.b[last]);
|
||||
if (focused) InterlockedExchange64(&mods_last_ns, (LONG64)now);
|
||||
mods_entry = (Rect){r.x0, r.y0 - pitch, r.x1, r.y1 - pitch};
|
||||
InterlockedExchange64(&mods_entry_ns, (LONG64)now);
|
||||
rx_option_set(HOS_OPT_MODS_X0, (int)r.x0), rx_option_set(HOS_OPT_MODS_X1, (int)r.x1);
|
||||
rx_option_set(HOS_OPT_MODS_Y0, (int)r.y0), rx_option_set(HOS_OPT_MODS_Y1, (int)r.y1);
|
||||
rx_option_set(HOS_OPT_MODS_PITCH, (int)pitch);
|
||||
rx_option_set(HOS_OPT_MODS_LIST, HOS_MODS_LIST_SHOWN | (focused ? HOS_MODS_LIST_LAST_FOCUSED : 0) | (title ? HOS_MODS_LIST_TITLE : 0));
|
||||
static unsigned logged;
|
||||
if (trace && logged++ % 120 == 0)
|
||||
hos_log("menu: mods entry: %s list of %d, last button (%.0f, %.0f)-(%.0f, %.0f), pitch %.0f, focused %d", title ? "the title's" : "System's",
|
||||
l.n, r.x0, r.y0, r.x1, r.y1, pitch, focused);
|
||||
}
|
||||
/* nn_hid.c: the game's cursor is on a list's last button now (the frame before at most): its Down is the Mods entry's */
|
||||
int hos_oot_mods_down(void) {
|
||||
const LONG64 t = InterlockedCompareExchange64(&mods_last_ns, 0, 0);
|
||||
return t && (LONG64)hos_now_ns() - t < 80000000LL;
|
||||
}
|
||||
static int mods_entry_hit(float x, float y) {
|
||||
const LONG64 t = InterlockedCompareExchange64(&mods_entry_ns, 0, 0);
|
||||
return t && (LONG64)hos_now_ns() - t < 100000000LL && x >= mods_entry.x0 && x <= mods_entry.x1 && y >= mods_entry.y0 && y <= mods_entry.y1;
|
||||
}
|
||||
|
||||
/* ---- The screens f_802f1380 runs for (the HUD's, the menus', the title's: each frame each). The mouse is for the one
|
||||
in front: of those that ran in the last 100 ms with a focused button shown (or one gone for under 0.5 s: the map's
|
||||
list loses it for 6 to 24 frames as it moves), the one that came last (a dialog over the menu that opened it). Whether a screen has a focused button is looked at 10 times a second (each look reads its list) */
|
||||
typedef struct { uint64_t screen, vt; int64_t first_ns, seen_ns, lost_ns; int focus, running; unsigned n; } Seen;
|
||||
list loses it for 6 to 24 frames as it moves), the one that came last (a dialog over the menu that opened it). Whether a screen has a focused button is looked at 10 times a second (each look reads its list).
|
||||
(Triforce) The game's Options screen has the mouse with no focused button found as well: our tabs over its header take
|
||||
the mouse (gpu/vk/vk_menu.cpp), and a screen the mouse is not freed for leaves it the camera's, captured and hidden */
|
||||
typedef struct { uint64_t screen, vt; int64_t first_ns, seen_ns, lost_ns; int focus, running, options; unsigned n; } Seen;
|
||||
static Seen seen[24];
|
||||
static SRWLOCK seen_lock = SRWLOCK_INIT; /* (the screens' updates run on several of the game's threads at once) */
|
||||
|
||||
@@ -748,12 +972,16 @@ static void focus_entry(int i, uint64_t entry, uint64_t button, void *arg) {
|
||||
(void)i, (void)entry;
|
||||
uint8_t state = 0;
|
||||
Rect r;
|
||||
if (!*(int *)arg && peek(button + 56, &state, 1) && state && button_rect(button, &r)) *(int *)arg = 1;
|
||||
/* ((Triforce) a button let go while the Mods entry has the cursor still counts: the menu is there, mods_held) */
|
||||
if (!*(int *)arg && peek(button + 56, &state, 1) && (state || button == mods_held.button) && button_rect(button, &r)) *(int *)arg = 1;
|
||||
}
|
||||
static Seen *screen_seen(uint64_t screen, int64_t now) {
|
||||
Seen *free_one = NULL;
|
||||
for (int i = 0; i < (int)(sizeof seen / sizeof *seen); i++) {
|
||||
if (seen[i].screen == screen) return &seen[i];
|
||||
/* ((Triforce) a screen's memory is another screen's later (the Load list's was the Options' in a traced run): another
|
||||
vtable there is a new screen, which came last. Kept as the one before, it came first, under the screens it is over) */
|
||||
if (seen[i].screen == screen && seen[i].vt == peek64(screen)) return &seen[i];
|
||||
if (seen[i].screen == screen) { free_one = &seen[i]; break; }
|
||||
if (!free_one || seen[i].seen_ns < free_one->seen_ns) free_one = &seen[i]; /* (the one gone longest) */
|
||||
}
|
||||
memset(free_one, 0, sizeof *free_one);
|
||||
@@ -781,15 +1009,15 @@ void hos_oot_menu_tick(Ctx *c, uint64_t screen) {
|
||||
me->seen_ns = now;
|
||||
/* (Triforce: the game's Options screen runs: the renderer's tabs over its header. Its vtable as it is now, not me->vt:
|
||||
a screen's memory is another screen's later, the Load list ran at the Options' address in the traced run) */
|
||||
if (peek64(screen) == OPTIONS_SCREEN_VTABLE) {
|
||||
static int tabs = -1;
|
||||
if (tabs < 0) {
|
||||
const char *e = getenv("HOS_OPTIONS_TABS");
|
||||
tabs = !(e && *e == '0');
|
||||
}
|
||||
me->options = options_screen(screen);
|
||||
if (me->options) {
|
||||
InterlockedExchange64(&options_seen_ns, (LONG64)now);
|
||||
if (tabs) rx_option_set(HOS_OPT_GAME_OPTIONS, 1);
|
||||
if (options_tabs_on()) rx_option_set(HOS_OPT_GAME_OPTIONS, 1);
|
||||
}
|
||||
/* ((Triforce) the Mods entry: the pause menu's System page, the title's list; its call into the game after the lock) */
|
||||
ModsCall mods_now = {0};
|
||||
if (peek64(screen) == TITLE_LIST_VTABLE) mods_list_tick(screen, 1, &mods_now);
|
||||
else if (screen == pause_screen_now() && page_now == 3) mods_list_tick(screen, 0, &mods_now);
|
||||
for (int i = 0; i < (int)(sizeof seen / sizeof *seen); i++) /* (a screen with no update for 200 ms has stopped) */
|
||||
if (seen[i].running && now - seen[i].seen_ns > 200000000) {
|
||||
seen[i].running = 0;
|
||||
@@ -817,7 +1045,7 @@ void hos_oot_menu_tick(Ctx *c, uint64_t screen) {
|
||||
}
|
||||
const Seen *front = NULL;
|
||||
for (int i = 0; i < (int)(sizeof seen / sizeof *seen); i++)
|
||||
if (seen[i].screen && (seen[i].focus || now - seen[i].lost_ns < 500000000 || panel_page_vt(seen[i].vt) >= 0) &&
|
||||
if (seen[i].screen && (seen[i].focus || now - seen[i].lost_ns < 500000000 || panel_page_vt(seen[i].vt) >= 0 || seen[i].options) &&
|
||||
seen[i].running && now - seen[i].seen_ns < 100000000 &&
|
||||
(!front || seen[i].first_ns > front->first_ns)) /* (its focus gone for under 0.5 s: a list's moment between rows) */
|
||||
front = &seen[i];
|
||||
@@ -831,6 +1059,7 @@ void hos_oot_menu_tick(Ctx *c, uint64_t screen) {
|
||||
const int gone = !front && none_since && now - none_since > 50000000;
|
||||
none_since = front ? 0 : none_since ? none_since : now;
|
||||
ReleaseSRWLockExclusive(&seen_lock);
|
||||
mods_call(c, &mods_now);
|
||||
if (gone) hos_mouse_game_menu_gone();
|
||||
if (mine) mouse_tick(c, screen);
|
||||
if (screen == pause_screen_now()) InterlockedIncrement(&ticks);
|
||||
|
||||
@@ -26,7 +26,8 @@
|
||||
enum { OOT_HUD_MIDDLE = 0, OOT_HUD_LEFT = -1, OOT_HUD_RIGHT = 1 };
|
||||
|
||||
/* The screens that keep the whole picture, by their class (the first word of the screen object), a bit each: bit k of
|
||||
the keep mask keeps screen k whole (the Insert menu's Ultrawide menus row clears the menus' bits 1 and 3). */
|
||||
the keep mask keeps screen k whole (the Insert menu's Ultrawide menus row clears the menus' bit 1; the two fades
|
||||
are never squeezed: one colour in the middle alone leaves the picture showing at the sides). */
|
||||
#define OOT_HUD_KEEP_N 4
|
||||
static const uint64_t oot_hud_keep_classes[OOT_HUD_KEEP_N] = {
|
||||
0x84c0d098ULL, /* the item wheel's background */
|
||||
|
||||
361
host/pace_steady.h
Normal file
361
host/pace_steady.h
Normal file
@@ -0,0 +1,361 @@
|
||||
/* (Triforce) The Steady frame rate row: the period the game's frames are paced by, lowered to one this PC holds in the
|
||||
scene and raised again when it has room (host/nn_vi.c steady_period; the present side's floor at it,
|
||||
gpu/vk/present_smooth.h present_smooth_paced_at). Plain C with no system call, so that tests/test_pace_steady.py runs it on
|
||||
frame times of its own and on the lab's recorded ones.
|
||||
|
||||
Under a cap the PC does not hold (240, with the Market's frames 10.5 ms long and 13.9 at p99) no frame waits, and every
|
||||
one goes out the moment it is done: the intervals are the frames' own work, which swings from frame to frame. Here the
|
||||
period is one the frames fit in, so they wait for it: the game's own wait (hos_vi_native_acquire) starts them a period
|
||||
apart, the steady step (step_steady.h) snaps the game's time step to the period, and the present holds a picture that
|
||||
comes early to the period after the last one (present_smooth_paced_at: the start alone leaves the presents as uneven
|
||||
as the work after it).
|
||||
|
||||
What a frame took and needed (the caller measures both from its present interval and the pacing's waits in it, the
|
||||
acquire's and the present's hold: pace_steady_measure): took, the interval less the longest of those waits, for whether
|
||||
it was late (a frame that waited anywhere was on time); needed, the interval less all of them, for how much room there
|
||||
is. Needed is kept for the last PACE_STEADY_RING frames that are not hitches, took for the last PACE_STEADY_SHORT. A
|
||||
hitch is a frame that took over PACE_STEADY_HITCH times the median of what the last PACE_STEADY_SHORT frames took,
|
||||
hitches among them, or a present interval over PACE_STEADY_LOAD_NS (a load), and it is left out: a load or a one-off
|
||||
spike moves nothing, and a scene that stays heavier is the median within half that many frames. ((Triforce, test26) The
|
||||
median was once that of the frames kept alone: in the lab's run the title's frames, which waited most of their 4.2 ms,
|
||||
made it about 2 ms, every Market frame of 11 ms was then a hitch, never kept, and the median never moved: the rate stayed
|
||||
at the cap for good.) For PACE_STEADY_SETTLE_NS after a load the frames are not the scene's measure either (the lab's
|
||||
Market came in with 40 frames of 15 to 20 ms before its own 10.6 ms ones, and the rate went from the cap to 60 fps where
|
||||
80 held): they count for the median, so that the scene is its measure when the settling ends, and nothing else.
|
||||
- Lowered quickly: when a quarter of the last PACE_STEADY_SHORT frames took more than the period, to the first
|
||||
period of the set that their 90th percentile fits in with 2% to spare (at least one step), half a second after the
|
||||
last change at the soonest. After a load, once it has settled, the first lowering waits for PACE_STEADY_FRESH frames
|
||||
of the new scene and takes the 90th percentile of those (about 1.4 s of the Market's frames).
|
||||
- Raised slowly: once the period has held PACE_STEADY_UP_HOLD_NS since a raise (or the backoff since a lowering), to
|
||||
the shortest period of the set that the 90th percentile of what the frames kept needed (PACE_STEADY_UP_MIN of them at
|
||||
least) fits in with 5% to spare (the next one only, where the game acquires on another thread: one_step). The gaps
|
||||
between a quarter of the frames late (lowered), a tenth with 2% to spare (where a lowering goes) and a tenth with 5% to
|
||||
spare (raised) are the hysteresis: a scene whose 90th percentile is within 3% of a rate's period stays where it is
|
||||
rather than going up and down with its slower swings. ((Triforce, test27) It was the 95th percentile with 5% to spare,
|
||||
and the lab's Market, 11.7 ms at the 90th, 12.3 with 5%, would have stayed at 72 where 80 held.)
|
||||
- Not hunting: a lowering within PACE_STEADY_FAILED_NS of a raise says the raise was wrong, and the wait before the
|
||||
next raise doubles (PACE_STEADY_BACKOFF_NS, then 12, 24 ... PACE_STEADY_BACKOFF_MAX_NS); a minute with no lowering
|
||||
after a raise brings it back, and so does a load (a new scene: the lab's tour came into the Market with the backoff
|
||||
of the areas before, and stayed at 62 fps where 74 held).
|
||||
- Never past the owner's cap: the set's shortest period is the cap's.
|
||||
The set: PACE_STEADY_ANY, rates that suit a display with variable refresh (or with VSync off): the cap and the rates of
|
||||
pace_steady_rates under it, about a tenth apart; PACE_STEADY_REFRESH, whole divisions of the display's refresh rate under
|
||||
the cap (240 Hz: 240, 120, 80, 60, 48, 40, 34.3, 30), for VSync without variable refresh, where a frame then stays on the
|
||||
screen the same number of refreshes every time. 30 fps is the floor of both. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PACE_STEADY_RING 512 /* (the frames the raise looks at) */
|
||||
#define PACE_STEADY_SHORT 64 /* (the frames the lowering looks at) */
|
||||
#define PACE_STEADY_UP_MIN 256 /* (frames kept before a raise is weighed) */
|
||||
#define PACE_STEADY_HITCH 2.5 /* (times the recent median: a hitch, left out) */
|
||||
#define PACE_STEADY_LOAD_NS 250000000LL /* (a present interval this long is a load, left out) */
|
||||
#define PACE_STEADY_DOWN_GAP_NS 500000000LL /* (between a change and a lowering, at least) */
|
||||
#define PACE_STEADY_UP_HOLD_NS 3000000000LL /* (between a raise and the next raise, at least) */
|
||||
#define PACE_STEADY_BACKOFF_NS 6000000000LL /* (between a lowering and a raise, at first) */
|
||||
#define PACE_STEADY_BACKOFF_MAX_NS 120000000000LL
|
||||
#define PACE_STEADY_FAILED_NS 30000000000LL /* (a lowering this soon after a raise doubles the backoff) */
|
||||
#define PACE_STEADY_CALM_NS 60000000000LL /* (this long with no lowering after a raise: the backoff is back to its first) */
|
||||
#define PACE_STEADY_SETTLE_NS 2000000000LL /* (after a load: not the scene's measure yet) */
|
||||
#define PACE_STEADY_FRESH 128 /* (frames of the new scene the first lowering after a load takes its measure of) */
|
||||
#define PACE_STEADY_MAX_PERIODS 24
|
||||
#define PACE_STEADY_MIN_FPS 30
|
||||
#define PACE_STEADY_PROBE_FRAMES 24 /* (frames a probe measures, unpaced) */
|
||||
#define PACE_STEADY_PROBE_SKIP 8 /* (frames at its start it lets go first: the last waits, the time step's catching up) */
|
||||
#define PACE_STEADY_PROBE_RING 72 /* (the last three probes' frames: their 90th percentile is the measure) */
|
||||
#define PACE_STEADY_PROBE_EVERY_NS 12000000000LL /* (a probe at least this often) */
|
||||
#define PACE_STEADY_PROBE_GAP_NS 2000000000LL /* (frames late again: a probe this long after the last at the soonest, then
|
||||
twice as long each time it changes nothing, up to PACE_STEADY_PROBE_EVERY_NS) */
|
||||
|
||||
enum { PACE_STEADY_OFF, PACE_STEADY_ANY, PACE_STEADY_REFRESH };
|
||||
_Static_assert(PACE_STEADY_RING % PACE_STEADY_SHORT == 0, "took[] follows ring[]'s index");
|
||||
|
||||
typedef struct {
|
||||
int64_t ring[PACE_STEADY_RING]; /* (what the frames kept needed, ns) */
|
||||
unsigned count, next;
|
||||
int64_t took[PACE_STEADY_SHORT]; /* (what the last frames kept took, ns: next % PACE_STEADY_SHORT is the oldest) */
|
||||
int64_t recent[PACE_STEADY_SHORT]; /* (what the last frames took, hitches too: the median a hitch is measured by) */
|
||||
unsigned recent_count, recent_next;
|
||||
int64_t periods[PACE_STEADY_MAX_PERIODS]; /* (the set, shortest first: periods[0] is the cap's) */
|
||||
int n, at; /* (how many; the one in force) */
|
||||
int64_t since_ns; /* (since the last change) */
|
||||
int64_t hold_ns; /* (what a raise waits for, since the last change) */
|
||||
int64_t backoff_ns; /* (what a raise waits for after a lowering) */
|
||||
int64_t since_up_ns; /* (since the last raise; -1 none yet) */
|
||||
int64_t frames, changes, raises, lowerings, hitches, kept;
|
||||
int64_t loads, unmeasured; /* (left out too: present intervals over PACE_STEADY_LOAD_NS; none, or nothing left) */
|
||||
int64_t settle_ns, settling; /* (what is left of the settling after a load; the frames left out for it) */
|
||||
int fresh; /* (a load since the last change: the frames kept are all the new scene's) */
|
||||
int one_step; /* (set by the caller: a raise goes one rate at a time, pace_steady_measure says why) */
|
||||
/* (the probes, set by the caller with probe: pace_steady_probed) */
|
||||
int probe; /* (1: the rate is the probes'; 0: the frames' own measure as they run, as before) */
|
||||
int probing, probe_due; /* (frames left of the probe under way, 0 none; a probe at the next frame) */
|
||||
int64_t probe_ring[PACE_STEADY_PROBE_RING];
|
||||
unsigned probe_count, probe_next;
|
||||
int64_t probe_now[2 * PACE_STEADY_PROBE_FRAMES]; /* (the probe under way's frames) */
|
||||
unsigned probe_now_count, probe_len; /* (how many so far; how many it measures) */
|
||||
int64_t scenes; /* (probes that found a new scene) */
|
||||
int64_t since_probe_ns, probe_gap_ns, probes, probe_p50, probe_p90;
|
||||
int64_t scratch[PACE_STEADY_RING]; /* (the percentiles' working copy) */
|
||||
} PaceSteady;
|
||||
|
||||
/* The rates of PACE_STEADY_ANY, from the fastest */
|
||||
static const int pace_steady_rates[] = {240, 200, 180, 160, 144, 120, 110, 100, 90, 80, 72, 66, 60, 55, 50, 45, 40, 36, 30};
|
||||
|
||||
/* The k-th smallest of a[0..n-1] (reorders a) */
|
||||
static inline int64_t pace_steady_select(int64_t *a, int n, int k) {
|
||||
int lo = 0, hi = n - 1;
|
||||
while (lo < hi) {
|
||||
const int64_t pivot = a[lo + (hi - lo) / 2];
|
||||
int i = lo, j = hi;
|
||||
while (i <= j) {
|
||||
while (a[i] < pivot) i++;
|
||||
while (a[j] > pivot) j--;
|
||||
if (i <= j) {
|
||||
const int64_t t = a[i];
|
||||
a[i++] = a[j], a[j--] = t;
|
||||
}
|
||||
}
|
||||
if (k <= j) hi = j;
|
||||
else if (k >= i) lo = i;
|
||||
else break;
|
||||
}
|
||||
return a[k];
|
||||
}
|
||||
/* The q quantile (0..1) of the last `last` frames kept (all of them for last >= count) */
|
||||
static inline int64_t pace_steady_quantile(PaceSteady *s, unsigned last, double q) {
|
||||
const unsigned n = last < s->count ? last : s->count;
|
||||
if (!n) return 0;
|
||||
for (unsigned i = 0; i < n; i++) s->scratch[i] = s->ring[(s->next + PACE_STEADY_RING - n + i) % PACE_STEADY_RING];
|
||||
int k = (int)(q * n);
|
||||
if (k > (int)n - 1) k = (int)n - 1;
|
||||
return pace_steady_select(s->scratch, (int)n, k);
|
||||
}
|
||||
|
||||
/* The set for a mode, under the cap (fps): refresh, the display's refresh rate (Hz; 0 or less when it is not known, and
|
||||
PACE_STEADY_REFRESH then takes PACE_STEADY_ANY's set). The period in force is the cap's: the frames start as they did */
|
||||
static inline void pace_steady_init(PaceSteady *s, int mode, int cap, int refresh) {
|
||||
memset(s, 0, sizeof *s);
|
||||
if (cap < 1) cap = 60;
|
||||
s->periods[s->n++] = 1000000000LL / cap;
|
||||
if (mode == PACE_STEADY_REFRESH && refresh > 0) {
|
||||
for (int k = 1; s->n < PACE_STEADY_MAX_PERIODS; k++) {
|
||||
const int64_t p = 1000000000LL * k / refresh;
|
||||
if (p * PACE_STEADY_MIN_FPS > 1000000000LL) break;
|
||||
if (p > s->periods[s->n - 1]) s->periods[s->n++] = p;
|
||||
}
|
||||
if (s->n > 1 && refresh % cap) {
|
||||
/* (a cap that is no whole division of the refresh, 144 on 240 Hz: its own period gives way to the next division,
|
||||
so that every period of the set is one; the owner's cap is still never passed) */
|
||||
memmove(s->periods, s->periods + 1, sizeof s->periods[0] * (size_t)--s->n);
|
||||
}
|
||||
} else {
|
||||
for (unsigned i = 0; i < sizeof pace_steady_rates / sizeof pace_steady_rates[0] && s->n < PACE_STEADY_MAX_PERIODS; i++) {
|
||||
const int64_t p = 1000000000LL / pace_steady_rates[i];
|
||||
if (p > s->periods[s->n - 1]) s->periods[s->n++] = p;
|
||||
}
|
||||
}
|
||||
s->backoff_ns = PACE_STEADY_BACKOFF_NS;
|
||||
s->hold_ns = PACE_STEADY_UP_HOLD_NS;
|
||||
s->since_up_ns = -1;
|
||||
s->probe_gap_ns = PACE_STEADY_PROBE_GAP_NS;
|
||||
s->probe_due = 1;
|
||||
}
|
||||
|
||||
/* The period the frames are paced by now: the cap's while a probe is under way */
|
||||
static inline int64_t pace_steady_period(const PaceSteady *s) { return s->probing ? s->periods[0] : s->periods[s->at]; }
|
||||
/* The period the rule holds, a probe or not */
|
||||
static inline int64_t pace_steady_held(const PaceSteady *s) { return s->periods[s->at]; }
|
||||
|
||||
/* Another limiter held the frames (RivaTuner, Reflex): what was measured is not the frames' work. The cap's period again,
|
||||
and nothing kept; the backoff stays */
|
||||
static inline void pace_steady_reset(PaceSteady *s) {
|
||||
s->count = s->next = 0;
|
||||
s->recent_count = s->recent_next = 0;
|
||||
s->at = 0;
|
||||
s->since_ns = 0;
|
||||
s->hold_ns = PACE_STEADY_UP_HOLD_NS;
|
||||
s->probing = 0, s->probe_due = 1, s->probe_count = s->probe_next = 0, s->probe_gap_ns = PACE_STEADY_PROBE_GAP_NS;
|
||||
}
|
||||
|
||||
/* What a frame took and needed (above), from its present interval and the pacing's waits in it: own, those on the presenting
|
||||
thread (the acquire's, where the game acquires there too, and the present's hold); other, the acquire's on other threads.
|
||||
- took: the interval less the longest wait: a frame that waited anywhere was on time, and one that waited nowhere took
|
||||
its whole interval.
|
||||
- needed: the interval less all of them. OoT acquires on its main thread and presents on its Presentation Thread, one
|
||||
after the other: the main thread waits in the acquire, builds the frame, the Presentation Thread replays and presents
|
||||
it, and only then does the main thread acquire again. The lab's Market at 60 fps (test27): 16.5 ms intervals, the
|
||||
acquire's wait 5.8 ms and the hold 0.3, leaving 10.4, the frame time it runs at with no pacing (10.6). (A version
|
||||
that took the two threads' waits for side by side, the interval less the smaller, came to 16.2 there and was never
|
||||
raised.) Where the waits do overlap (two threads working side by side) they would add up to more than the frame had
|
||||
to spare and it would need too little: where they add up to the whole interval or more, the longest is taken; and with
|
||||
the acquire on another thread a raise goes one rate at a time (PaceSteady.one_step), so that a raise too far is late
|
||||
within half a second, lowered again, and the backoff grows */
|
||||
static inline void pace_steady_measure(int64_t interval, int64_t own, int64_t other, int64_t *took, int64_t *need) {
|
||||
const int64_t longest = own > other ? own : other;
|
||||
*took = interval - longest;
|
||||
*need = interval - (own + other < interval ? own + other : longest);
|
||||
}
|
||||
|
||||
/* The q quantile of a[0..n-1], through the scratch copy */
|
||||
static inline int64_t pace_steady_quantile_of(PaceSteady *s, const int64_t *a, unsigned n, double q) {
|
||||
if (!n) return 0;
|
||||
memcpy(s->scratch, a, sizeof a[0] * n);
|
||||
int k = (int)(q * n);
|
||||
if (k > (int)n - 1) k = (int)n - 1;
|
||||
return pace_steady_select(s->scratch, (int)n, k);
|
||||
}
|
||||
|
||||
/* ((Triforce, test31) The probes, PaceSteady.probe. A frame paced slower does more work than it does unpaced: the lab's
|
||||
Market took 11.6 ms a frame unpaced (85 fps) and 15.5 ms paced at 55 fps (18.2 ms), whether the game does more in a longer
|
||||
time step or the PC's clocks come down while it idles. The frames' own measure as they ran then chased the rate down: held
|
||||
at 60 they needed 16 ms and 60 was the rate; a raise was weighed by what they needed at the slower rate, never by what they
|
||||
would at the faster one (test29 to test31 held 45 to 60 fps where 72 to 80 held).
|
||||
So the rate is chosen by what the frames take unpaced: a probe runs PACE_STEADY_PROBE_SKIP + PACE_STEADY_PROBE_FRAMES
|
||||
frames at the cap (pace_steady_period gives the cap's period meanwhile: no wait, no hold; twice as many measured for the
|
||||
first probe of a scene, at the start and after a load), takes what its measured frames took (hitches left out) into the
|
||||
ring of the last three probes, and the rate becomes the first that ring's 90th percentile fits in with 2% to spare where
|
||||
that is slower than the one held, or the first it fits in with 5% to spare where that is faster. A probe whose median is
|
||||
over 30% off the ring's is a new scene: the probes before it go (the frames a probe takes in a row swing together, and
|
||||
15% came up about once a minute in a Market of 10.6 ms swinging by a millisecond). A probe comes at the start, after a
|
||||
load has settled, at least every PACE_STEADY_PROBE_EVERY_NS (some 32 frames, under half a second, every 12 s), and when a quarter of the
|
||||
last 64 frames ran late (PACE_STEADY_PROBE_GAP_NS after the last at the soonest, twice that each time it changes
|
||||
nothing): frames late do not lower the rate by themselves any more, so it is never under what a probe shows the PC
|
||||
holds. 1 when the rate changed */
|
||||
static inline int pace_steady_probed(PaceSteady *s) {
|
||||
s->count = s->next = 0; /* (frames late are counted afresh at the rate held from here) */
|
||||
if (s->probe_now_count < PACE_STEADY_PROBE_FRAMES / 2) return 0; /* (a probe of hitches: nothing measured) */
|
||||
const int64_t now50 = pace_steady_quantile_of(s, s->probe_now, s->probe_now_count, 0.5);
|
||||
if (s->probe_count) {
|
||||
const int64_t ring50 = pace_steady_quantile_of(s, s->probe_ring, s->probe_count, 0.5);
|
||||
if (now50 * 100 > ring50 * 130 || now50 * 130 < ring50 * 100) s->probe_count = s->probe_next = 0, s->scenes++;
|
||||
}
|
||||
for (unsigned i = 0; i < s->probe_now_count; i++) {
|
||||
s->probe_ring[s->probe_next] = s->probe_now[i];
|
||||
s->probe_next = (s->probe_next + 1) % PACE_STEADY_PROBE_RING;
|
||||
if (s->probe_count < PACE_STEADY_PROBE_RING) s->probe_count++;
|
||||
}
|
||||
const int64_t p90 = pace_steady_quantile_of(s, s->probe_ring, s->probe_count, 0.90);
|
||||
s->probe_p90 = p90, s->probe_p50 = pace_steady_quantile_of(s, s->probe_ring, s->probe_count, 0.5);
|
||||
int lower = 0, raise = 0;
|
||||
while (lower < s->n - 1 && s->periods[lower] < p90 * 102 / 100) lower++;
|
||||
while (raise < s->n - 1 && s->periods[raise] < p90 * 105 / 100) raise++;
|
||||
const int to = lower > s->at ? lower : raise < s->at ? raise : s->at;
|
||||
if (to == s->at) {
|
||||
s->probe_gap_ns *= 2;
|
||||
if (s->probe_gap_ns > PACE_STEADY_PROBE_EVERY_NS) s->probe_gap_ns = PACE_STEADY_PROBE_EVERY_NS;
|
||||
return 0;
|
||||
}
|
||||
if (to > s->at) s->lowerings++;
|
||||
else s->raises++;
|
||||
s->at = to, s->changes++, s->since_ns = 0, s->probe_gap_ns = PACE_STEADY_PROBE_GAP_NS;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* A frame presented: took_ns and need_ns, what it took and needed (above; 0 or less: not measured), interval_ns, its present
|
||||
interval. 1 when the period changed (lowered or raised), else 0 */
|
||||
static inline int pace_steady_feed(PaceSteady *s, int64_t took_ns, int64_t need_ns, int64_t interval_ns) {
|
||||
s->frames++;
|
||||
if (interval_ns > 0) s->since_ns += interval_ns, s->since_probe_ns += interval_ns;
|
||||
if (s->since_up_ns >= 0 && interval_ns > 0) s->since_up_ns += interval_ns;
|
||||
if (s->since_up_ns > PACE_STEADY_CALM_NS) s->backoff_ns = PACE_STEADY_BACKOFF_NS;
|
||||
if (interval_ns > PACE_STEADY_LOAD_NS) {
|
||||
/* (a load: a new scene, which the raises that failed in the last one say nothing of. The backoff starts again; the
|
||||
frames kept stay, and the lowering takes the new scene's measure within a second if it is heavier) */
|
||||
s->backoff_ns = PACE_STEADY_BACKOFF_NS;
|
||||
if (s->hold_ns > s->backoff_ns) s->hold_ns = s->backoff_ns;
|
||||
s->loads++;
|
||||
s->settle_ns = PACE_STEADY_SETTLE_NS; /* (and the frames kept start again once it has settled) */
|
||||
s->count = s->next = 0;
|
||||
s->fresh = 1;
|
||||
/* (with the probes: the last scene's measure goes, a probe under way stops, and one comes once it has settled) */
|
||||
s->probe_count = s->probe_next = 0, s->probing = 0, s->probe_due = 1, s->probe_gap_ns = PACE_STEADY_PROBE_GAP_NS;
|
||||
return 0;
|
||||
}
|
||||
if (interval_ns <= 0 || took_ns <= 0 || need_ns <= 0) {
|
||||
s->unmeasured++;
|
||||
return 0;
|
||||
}
|
||||
s->recent[s->recent_next] = took_ns;
|
||||
s->recent_next = (s->recent_next + 1) % PACE_STEADY_SHORT;
|
||||
if (s->recent_count < PACE_STEADY_SHORT) s->recent_count++;
|
||||
if (s->settle_ns > 0) {
|
||||
s->settle_ns -= interval_ns;
|
||||
s->settling++;
|
||||
return 0;
|
||||
}
|
||||
const int hitch = s->recent_count >= 16 &&
|
||||
(double)took_ns > PACE_STEADY_HITCH * (double)pace_steady_quantile_of(s, s->recent, s->recent_count, 0.5);
|
||||
if (s->probe) {
|
||||
if (s->probing) { /* (a probe's frame: unpaced) */
|
||||
const int measured = s->probing <= (int)s->probe_len;
|
||||
s->probing--;
|
||||
if (hitch) s->hitches++;
|
||||
else if (measured && s->probe_now_count < 2 * PACE_STEADY_PROBE_FRAMES) s->probe_now[s->probe_now_count++] = took_ns;
|
||||
return s->probing ? 0 : pace_steady_probed(s);
|
||||
}
|
||||
unsigned late = 0;
|
||||
if (!hitch) {
|
||||
s->kept++;
|
||||
s->took[s->next % PACE_STEADY_SHORT] = took_ns;
|
||||
s->ring[s->next] = need_ns;
|
||||
s->next = (s->next + 1) % PACE_STEADY_RING;
|
||||
if (s->count < PACE_STEADY_RING) s->count++;
|
||||
if (s->count >= PACE_STEADY_SHORT)
|
||||
for (unsigned i = 0; i < PACE_STEADY_SHORT; i++) late += s->took[i] > s->periods[s->at];
|
||||
} else s->hitches++;
|
||||
if (s->probe_due || s->since_probe_ns >= PACE_STEADY_PROBE_EVERY_NS ||
|
||||
(late * 4 >= PACE_STEADY_SHORT && s->since_probe_ns >= s->probe_gap_ns)) {
|
||||
/* (the first of a scene measures twice as many: its 90th percentile is the rate until the next) */
|
||||
s->probe_len = s->probe_count ? PACE_STEADY_PROBE_FRAMES : 2 * PACE_STEADY_PROBE_FRAMES;
|
||||
s->probing = PACE_STEADY_PROBE_SKIP + (int)s->probe_len, s->probe_now_count = 0;
|
||||
s->probe_due = 0, s->since_probe_ns = 0, s->probes++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (hitch) {
|
||||
s->hitches++;
|
||||
return 0;
|
||||
}
|
||||
s->kept++;
|
||||
s->took[s->next % PACE_STEADY_SHORT] = took_ns;
|
||||
s->ring[s->next] = need_ns;
|
||||
s->next = (s->next + 1) % PACE_STEADY_RING;
|
||||
if (s->count < PACE_STEADY_RING) s->count++;
|
||||
if (s->fresh && s->count >= 2 * PACE_STEADY_FRESH) s->fresh = 0; /* (the scene held the rate: its measure is the usual) */
|
||||
if (s->count < PACE_STEADY_SHORT) return 0;
|
||||
const int64_t period = s->periods[s->at];
|
||||
/* (after a load the first lowering waits for PACE_STEADY_FRESH frames of the new scene and takes their measure: the 90th
|
||||
percentile of 64 frames swings by half a millisecond from one stretch to the next, a rate either way) */
|
||||
if (s->at < s->n - 1 && s->since_ns >= PACE_STEADY_DOWN_GAP_NS && !(s->fresh && s->count < PACE_STEADY_FRESH)) {
|
||||
unsigned late = 0;
|
||||
for (unsigned i = 0; i < PACE_STEADY_SHORT; i++) late += s->took[i] > period;
|
||||
if (late * 4 >= PACE_STEADY_SHORT) {
|
||||
const int64_t want = (s->fresh ? pace_steady_quantile(s, s->count, 0.90)
|
||||
: pace_steady_quantile_of(s, s->took, PACE_STEADY_SHORT, 0.90)) * 102 / 100;
|
||||
s->fresh = 0;
|
||||
int to = s->at + 1;
|
||||
while (to < s->n - 1 && s->periods[to] < want) to++;
|
||||
if (s->since_up_ns >= 0 && s->since_up_ns < PACE_STEADY_FAILED_NS) {
|
||||
s->backoff_ns *= 2;
|
||||
if (s->backoff_ns > PACE_STEADY_BACKOFF_MAX_NS) s->backoff_ns = PACE_STEADY_BACKOFF_MAX_NS;
|
||||
}
|
||||
s->at = to, s->since_ns = 0, s->hold_ns = s->backoff_ns, s->since_up_ns = -1;
|
||||
s->changes++, s->lowerings++;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
/* (weighed every 16th frame: a percentile of a thousand frames is not for every frame) */
|
||||
if (s->at > 0 && s->since_ns >= s->hold_ns && s->count >= PACE_STEADY_UP_MIN && !(s->frames & 15)) {
|
||||
const int64_t want = pace_steady_quantile(s, PACE_STEADY_RING, 0.90) * 105 / 100;
|
||||
int to = s->at;
|
||||
while (to > 0 && s->periods[to - 1] >= want && !(s->one_step && to < s->at)) to--;
|
||||
if (to < s->at) {
|
||||
s->at = to, s->since_ns = 0, s->hold_ns = PACE_STEADY_UP_HOLD_NS, s->since_up_ns = 0;
|
||||
s->changes++, s->raises++, s->fresh = 0;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
40
host/pace_wait.h
Normal file
40
host/pace_wait.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/* (Triforce, after NX2navi's runtime/nvn_api_obj.c sleep_until: its behaviour, not its code) The game's frame wait to
|
||||
its time, without the timer's lateness (host/nn_vi.c hos_vi_native_acquire, hos_thread.c hos_sleep_until_ns). Plain C
|
||||
and no system call, so that tests/test_pace_wait.py runs it against a clock and a timer of its own.
|
||||
|
||||
The frame wait slept on the high-resolution waitable timer for the whole wait (hos_sleep_ns), and that timer never
|
||||
wakes early but wakes up to about 0.5 ms late (hos_thread.c sleep_ns; NX2navi measured 0.3 to 0.5 ms on Windows). The
|
||||
frame then started late by that much, and since the pacing keeps its phase the frame after it came that much sooner:
|
||||
at a Frame rate cap of 240 a period is 4.17 ms, and 0.5 ms is an eighth of it, one frame long, the next short. So the
|
||||
timer now takes the wait up to a margin before the frame's time, the margin the timer may be late plus 0.1 ms, and the
|
||||
rest is yielded away on the clock (SwitchToThread, as hos_sleep_ns does for waits under a millisecond), as NX2navi's
|
||||
pacer and Triforce's own frame generation display thread (gpu/vk/vk_framegen.cpp display_main) wait. The yielding
|
||||
costs at most the margin a frame, and a millisecond more where the whole wait is shorter than the timer's floor and
|
||||
the margin together; a frame that is late already waits for nothing, as before.
|
||||
HOS_PACE_SPIN=0: the timer for the whole wait, as before; =<microseconds>: another margin. */
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
|
||||
#define PACE_WAIT_MARGIN_NS 600000LL /* (the timer's lateness, 0.5 ms, and 0.1 ms) */
|
||||
#define PACE_WAIT_TIMER_MIN_NS 1000000LL /* (hos_thread.c sleep_ns: under a millisecond it yields, it takes no timer) */
|
||||
|
||||
/* The part of a wait with left_ns to go to take on the timer: all of it but the margin, where that is a wait the timer
|
||||
takes (at least PACE_WAIT_TIMER_MIN_NS); 0: the rest is yielded away */
|
||||
static inline int64_t pace_wait_timed(int64_t left_ns, int64_t margin_ns) {
|
||||
if (margin_ns < 0) margin_ns = 0;
|
||||
return left_ns - margin_ns >= PACE_WAIT_TIMER_MIN_NS ? left_ns - margin_ns : 0;
|
||||
}
|
||||
|
||||
/* Waits until the clock reads `until`: the timer's part (timer(ns), never shorter than PACE_WAIT_TIMER_MIN_NS), then
|
||||
yield() until the time. A timer that woke later than the margin leaves nothing to yield: the wait ends then, never
|
||||
later than the timer alone would have ended it */
|
||||
static inline void pace_wait_until(int64_t until, int64_t margin_ns, int64_t (*now)(void), void (*timer)(int64_t),
|
||||
void (*yield)(void)) {
|
||||
for (;;) {
|
||||
const int64_t left = until - now();
|
||||
if (left <= 0) return;
|
||||
const int64_t timed = pace_wait_timed(left, margin_ns);
|
||||
if (timed) timer(timed);
|
||||
else yield();
|
||||
}
|
||||
}
|
||||
112
host/pad_sdl.c
112
host/pad_sdl.c
@@ -40,11 +40,17 @@ static struct {
|
||||
int32_t id;
|
||||
uint16_t rumble_lo, rumble_hi;
|
||||
int64_t rumble_ns;
|
||||
/* (Triforce) the triggers' motors (trigger_poll): what they were last sent and when, when to ask a pad that refused
|
||||
again, and what the log has said of this pad (1 refused, 2 plays) */
|
||||
uint16_t trigger;
|
||||
uint8_t trigger_said;
|
||||
int64_t trigger_ns, trigger_retry_ns;
|
||||
uint8_t hd, hd_kind, hd_ever; /* HD rumble (hd_rumble.h): HD_RUMBLE_UNKNOWN / ON / OFF, the pad's kind, sent once */
|
||||
uint8_t hd_sent[HD_RUMBLE_PACKET_SIZE];
|
||||
int64_t hd_ns;
|
||||
int motion, motion_on;
|
||||
int64_t motion_retry_ns, used_ns;
|
||||
int64_t opened_ns; /* (Triforce) when SDL opened it, for the log's line when it goes */
|
||||
uint64_t input_sig;
|
||||
unsigned rested; /* (hookshot) the sticks' axes seen at rest since it was opened, a bit each: only those steer */
|
||||
signed char held[4]; /* (the way each axis was last held, -1, 0 or 1: one held the other way has passed its rest) */
|
||||
@@ -75,12 +81,25 @@ static int motion_head, motion_count, motion_live;
|
||||
static volatile LONG gyro_status; /* 0 no controller, 1 controllers but none with a gyro, 2 one with a gyro */
|
||||
|
||||
int32_t hos_stick_axis(int16_t v, int zone); /* nn_hid.c: the menu's Stick dead zone, as XInput's pads */
|
||||
/* ((Triforce) nn_hid.c: a stick's own dead zone, the menu's Left and Right stick dead zone, -1 none; an axis cut at it,
|
||||
or at the menu's Stick dead zone when its stick has none) */
|
||||
int hos_stick_own(int stick);
|
||||
int32_t hos_stick_axis_own(int16_t v, int zone, int own);
|
||||
|
||||
static int32_t stick_value(int16_t v, int flip, int zone) {
|
||||
const int32_t a = hos_stick_axis(v, zone);
|
||||
static int32_t stick_value(int16_t v, int flip, int zone, int own) {
|
||||
const int32_t a = hos_stick_axis_own(v, zone, own);
|
||||
return flip ? -a : a;
|
||||
}
|
||||
|
||||
/* (Triforce) Whether an axis is at rest, for the poll's rule that an axis steers once it has been seen at rest, when its
|
||||
stick has a dead zone of its own. That zone can be smaller than any stick's rest: with 0 an axis is never exactly at
|
||||
rest, and with a few percent a stick resting just past it would steer only after a push the other way. So an axis
|
||||
inside the default dead zone (XInput's 24%, hos_stick_axis's zone 0) is at rest whatever smaller zone its stick cuts
|
||||
at: the rule asks what it has always asked at the default, and a phantom axis, held far outside it, still never
|
||||
steers. Without a zone of its own (-1) this adds nothing: rest is the Stick dead zone row's, as before. raw: the axis
|
||||
as SDL gives it. */
|
||||
static int stick_rests(int16_t raw, int own) { return own >= 0 && hos_stick_axis(raw, 0) == 0; }
|
||||
|
||||
/* SDL's controller support started; 0 when it could not be */
|
||||
static int pads_start(void) {
|
||||
/* poll in the background, whatever window is in front (nn_hid.c decides when input counts) */
|
||||
@@ -122,10 +141,24 @@ static int rumble_due(unsigned lo, unsigned hi, unsigned sent_lo, unsigned sent_
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The Insert menu's Rumble strength (percent; 0 Off) on the motors' 0..65535, heavy and light together: up to 100 each
|
||||
motor is scaled on its own (100 sends them as they are); above, when the stronger would pass the motor's top, both
|
||||
are scaled by the same factor so that it just reaches it, keeping the balance between the motors that the frequency
|
||||
split chose. Off stops a rumble that is playing (the poll sends the change). */
|
||||
/* (Triforce, after NX2navi's rumble_curve) The Insert menu's Vibration curve (hundredths, 20 to 150) on one motor's
|
||||
0..65535, before the Rumble strength: a power on the motor's strength taken as 0 to 1. 100 leaves it as it is, for
|
||||
every value. Under 100 the weak rumbles, which is most of what OoT sends, come up towards the strong ones (at 60 a
|
||||
tenth of the motor's strength becomes a quarter); over 100 they go down. Silence and the motor's top stay where they
|
||||
are, and a stronger rumble never comes out the weaker of two. 0 or less is no curve (an option table that does not
|
||||
know the row answers 0). */
|
||||
static unsigned rumble_curve(unsigned value, int curve) {
|
||||
if (value >= 0xFFFFu) return 0xFFFFu;
|
||||
if (!value || curve <= 0 || curve == 100) return value;
|
||||
const double power = (curve < 20 ? 20 : curve > 150 ? 150 : curve) / 100.0;
|
||||
const double v = pow(value / 65535.0, power) * 65535.0 + 0.5;
|
||||
return v >= 65535.0 ? 0xFFFFu : (unsigned)v;
|
||||
}
|
||||
|
||||
/* The Insert menu's Rumble strength (percent; 0 Off, up to 300) on the motors' 0..65535, heavy and light together: up
|
||||
to 100 each motor is scaled on its own (100 sends them as they are); above, when the stronger would pass the motor's
|
||||
top, both are scaled by the same factor so that it just reaches it, keeping the balance between the motors that the
|
||||
frequency split chose. Off stops a rumble that is playing (the poll sends the change). */
|
||||
static void rumble_strength(unsigned heavy, unsigned light, int percent, uint16_t out[2]) {
|
||||
if (percent <= 0) {
|
||||
out[0] = out[1] = 0;
|
||||
@@ -142,6 +175,33 @@ static void rumble_strength(unsigned heavy, unsigned light, int percent, uint16_
|
||||
out[1] = (uint16_t)l;
|
||||
}
|
||||
|
||||
/* (Triforce, after NX2navi's rumble_triggers) The motors in the triggers of an Xbox One or Series pad, with the Insert
|
||||
menu's Trigger vibration on: both play the light motor's rumble, sent when it changes and again before SDL's effect
|
||||
ends, as the motors' own is (rumble_due). want: that strength, 0 with the row off. A pad whose triggers were never
|
||||
sent a rumble is never sent a stop either, so with the row off nothing is called at all; turned off while they play,
|
||||
they are stopped. A pad without such motors, a driver that cannot reach them and an SDL without the call all refuse:
|
||||
said once a pad in the log, and that pad is asked again two seconds later at the soonest (a write that failed once
|
||||
comes back; one that never will costs a call every two seconds of rumble). */
|
||||
static void trigger_poll(int i, unsigned want, long long now) {
|
||||
if (now < pads[i].trigger_retry_ns) return;
|
||||
if (!rumble_due(want, want, pads[i].trigger, pads[i].trigger, now, pads[i].trigger_ns)) return;
|
||||
const int ok = hos_sdl_pad_rumble_triggers(pads[i].gc, (uint16_t)want, (uint16_t)want, want ? 1000 : 0) != 0;
|
||||
if (want && !(pads[i].trigger_said >> ok & 1)) {
|
||||
pads[i].trigger_said |= (uint8_t)(1 << ok);
|
||||
if (ok) hos_log("hid: trigger vibration on \"%s\"", hos_sdl_pad_name(pads[i].gc));
|
||||
else hos_log("hid: \"%s\" took no trigger vibration (%s)", hos_sdl_pad_name(pads[i].gc),
|
||||
hos_sdl_pad_has_trigger_rumble() ? hos_sdl_error() : "this SDL has no call for it");
|
||||
}
|
||||
if (!ok) { /* (nothing plays there, as far as is known: the next rumble asks again, not before the two seconds) */
|
||||
pads[i].trigger = 0;
|
||||
pads[i].trigger_ns = 0;
|
||||
pads[i].trigger_retry_ns = now + 2000000000LL;
|
||||
return;
|
||||
}
|
||||
pads[i].trigger = (uint16_t)want;
|
||||
pads[i].trigger_ns = now;
|
||||
}
|
||||
|
||||
/* A pad SDL just opened: HD rumble when it is a Switch Pro Controller or a Joy-Con and the SDL has the effect call
|
||||
(HOS_HD_RUMBLE=0: never); whether SDL's driver takes it shows with the first packet. */
|
||||
static void hd_open(int i, void *gc) {
|
||||
@@ -288,13 +348,18 @@ static void motion_poll(int64_t now, int count) {
|
||||
/* one poll: controllers plugged in or gone (each second), then every open one's buttons, sticks and rumble */
|
||||
static void pads_poll(void) {
|
||||
static int64_t next_scan = 0;
|
||||
static int64_t gone_ns; /* (Triforce) when a controller last disconnected, for the log */
|
||||
hos_sdl_pads_update();
|
||||
const int64_t now = hos_now_ns();
|
||||
if (now >= next_scan) { /* new controllers (and ones that went away) */
|
||||
next_scan = now + 1000000000LL;
|
||||
for (int i = 0; i < MAX_PADS; i++)
|
||||
if (pads[i].gc && !hos_sdl_pad_connected(pads[i].gc)) {
|
||||
hos_log("hid: controller \"%s\" disconnected (SDL)", hos_sdl_pad_name(pads[i].gc));
|
||||
/* (Triforce) how long it had been connected: a pad that leaves after seconds or minutes, again and
|
||||
again, is its link, its battery or another program taking it, and the log then shows the rhythm */
|
||||
hos_log("hid: controller \"%s\" disconnected (SDL), %.0f s after it connected", hos_sdl_pad_name(pads[i].gc),
|
||||
(now - pads[i].opened_ns) / 1e9);
|
||||
gone_ns = now;
|
||||
hos_sdl_pad_close(pads[i].gc);
|
||||
pads[i].gc = NULL;
|
||||
pads[i].rumble_lo = pads[i].rumble_hi = 0;
|
||||
@@ -318,6 +383,8 @@ static void pads_poll(void) {
|
||||
pads[slot].id = hos_sdl_pad_id(gc);
|
||||
pads[slot].rumble_lo = pads[slot].rumble_hi = 0;
|
||||
pads[slot].rumble_ns = 0;
|
||||
pads[slot].trigger = 0, pads[slot].trigger_said = 0;
|
||||
pads[slot].trigger_ns = pads[slot].trigger_retry_ns = 0;
|
||||
hd_open(slot, gc);
|
||||
pads[slot].motion = hos_sdl_pad_has_sensor(gc, HOS_SDL_SENSOR_GYRO) && hos_sdl_pad_has_sensor(gc, HOS_SDL_SENSOR_ACCEL);
|
||||
pads[slot].motion_on = 0;
|
||||
@@ -329,6 +396,9 @@ static void pads_poll(void) {
|
||||
pads[slot].has_bias = 0;
|
||||
hos_log("hid: controller \"%s\" connected (SDL)%s", hos_sdl_pad_name(gc),
|
||||
pads[slot].motion ? ", with a gyroscope" : "");
|
||||
pads[slot].opened_ns = now;
|
||||
if (gone_ns && now - gone_ns < 60000000000LL)
|
||||
hos_log("hid: that is %.1f s after a controller disconnected", (now - gone_ns) / 1e9);
|
||||
}
|
||||
}
|
||||
uint64_t b = 0;
|
||||
@@ -336,8 +406,13 @@ static void pads_poll(void) {
|
||||
int count = 0;
|
||||
int rx_option(int id, int otherwise);
|
||||
const int strength = rx_option(9, 100); /* (the menu's Rumble strength: the motors' and HD rumble's) */
|
||||
/* ((Triforce) the menu's Vibration curve: the motors' alone. A Switch controller's HD rumble has its own,
|
||||
hd_rumble.h's hd_rumble_felt, and plays the game's values by it whatever this row says) */
|
||||
const int curve = rx_option(HOS_OPT_RUMBLE_CURVE, 100);
|
||||
const int triggers = rx_option(HOS_OPT_RUMBLE_TRIGGERS, 0) != 0; /* ((Triforce) the menu's Trigger vibration) */
|
||||
uint16_t motors[2];
|
||||
rumble_strength((uint16_t)rumble_low, (uint16_t)rumble_high, strength, motors);
|
||||
rumble_strength(rumble_curve((uint16_t)rumble_low, curve), rumble_curve((uint16_t)rumble_high, curve), strength,
|
||||
motors);
|
||||
const uint16_t lo = motors[0], hi = motors[1];
|
||||
uint8_t side[3][4]; /* (the HD rumble pads': each side's bytes, then both merged for a single Joy-Con) */
|
||||
{
|
||||
@@ -363,18 +438,23 @@ static void pads_poll(void) {
|
||||
const int inv_x = rx_option(16, 0);
|
||||
const int inv_y = rx_option(17, 0);
|
||||
const int z = rx_option(11, 0); /* (the menu's Stick dead zone: nn_hid.c's hos_stick_axis) */
|
||||
int32_t v[4] = {stick_value(hos_sdl_pad_axis(gc, AX_LX), 0, z),
|
||||
stick_value(hos_sdl_pad_axis(gc, AX_LY), 1, z),
|
||||
stick_value(hos_sdl_pad_axis(gc, AX_RX), inv_x, z),
|
||||
stick_value(hos_sdl_pad_axis(gc, AX_RY), inv_y ? 0 : 1, z)};
|
||||
const int own[2] = {hos_stick_own(0), hos_stick_own(1)}; /* ((Triforce) each stick's own, -1 none) */
|
||||
const int16_t raw[4] = {hos_sdl_pad_axis(gc, AX_LX), hos_sdl_pad_axis(gc, AX_LY), hos_sdl_pad_axis(gc, AX_RX),
|
||||
hos_sdl_pad_axis(gc, AX_RY)};
|
||||
int32_t v[4] = {stick_value(raw[0], 0, z, own[0]),
|
||||
stick_value(raw[1], 1, z, own[0]),
|
||||
stick_value(raw[2], inv_x, z, own[1]),
|
||||
stick_value(raw[3], inv_y ? 0 : 1, z, own[1])};
|
||||
/* (hookshot) An axis steers once it has been seen at rest (inside the dead zone) since the pad was opened. The sticks are
|
||||
the strongest of all the pads', so a device whose axis reads held for good (a mapping that puts a trigger or an
|
||||
unused axis on a stick, a second virtual pad) turned the camera or walked Link in circles on its own, with the
|
||||
player's pad at rest (#1673: Xbox 360 pad, CachyOS; #1661: GameSir 2 Lite, wired). A real stick rests at the start. */
|
||||
/* (a worn stick resting just past a small dead zone counts once it is pushed the other way: it went past its rest) */
|
||||
/* ((Triforce) with a dead zone of its stick's own, an axis inside the default one is at rest too: stick_rests) */
|
||||
for (int k = 0; k < 4; k++) {
|
||||
const signed char way = (signed char)(v[k] > 0 ? 1 : v[k] < 0 ? -1 : 0);
|
||||
if (!way || (pads[i].held[k] && way != pads[i].held[k])) pads[i].rested |= 1u << k;
|
||||
if (!way || (pads[i].held[k] && way != pads[i].held[k]) || stick_rests(raw[k], own[k >> 1]))
|
||||
pads[i].rested |= 1u << k;
|
||||
pads[i].held[k] = way;
|
||||
if (!(pads[i].rested >> k & 1)) v[k] = 0;
|
||||
}
|
||||
@@ -384,7 +464,10 @@ static void pads_poll(void) {
|
||||
uint64_t sig = 0;
|
||||
for (int k = BTN_A; k <= BTN_RIGHT; k++)
|
||||
if (hos_sdl_pad_button(gc, k)) sig |= 1ULL << k;
|
||||
for (int k = 0; k < 4; k++) sig |= (uint64_t)(v[k] > 0 ? 1 : v[k] < 0 ? 2 : 0) << (32 + 2 * k);
|
||||
for (int k = 0; k < 4; k++) { /* ((Triforce) a resting stick's noise past a small zone of its own is no push) */
|
||||
const int32_t u = stick_rests(raw[k], own[k >> 1]) ? 0 : v[k];
|
||||
sig |= (uint64_t)(u > 0 ? 1 : u < 0 ? 2 : 0) << (32 + 2 * k);
|
||||
}
|
||||
if (sig != pads[i].input_sig) pads[i].input_sig = sig, pads[i].used_ns = now;
|
||||
}
|
||||
if (hd_poll(i, side, now)) continue;
|
||||
@@ -394,6 +477,7 @@ static void pads_poll(void) {
|
||||
pads[i].rumble_hi = hi;
|
||||
pads[i].rumble_ns = now;
|
||||
}
|
||||
trigger_poll(i, triggers ? hi : 0, now); /* ((Triforce) the light motor's rumble, in the triggers too) */
|
||||
}
|
||||
motion_poll(now, count);
|
||||
AcquireSRWLockExclusive(&state_lock);
|
||||
|
||||
130
host/prompts.c
130
host/prompts.c
@@ -5,8 +5,13 @@
|
||||
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. The game reads the font once, at its start:
|
||||
a new face or new controls show at the next start.
|
||||
(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) 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
|
||||
@@ -25,6 +30,7 @@
|
||||
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_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) */
|
||||
@@ -34,7 +40,7 @@ int hos_mods_has(const char *prefix, const char *suffix); /* mods.c */
|
||||
#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"};
|
||||
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};
|
||||
@@ -58,6 +64,21 @@ static int contains(const char *text, const char *word) { /* (any case) */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* (Triforce) Auto's face: the first controller's, from its name (SDL's; "": it has none). With none the keyboard and mouse
|
||||
are what the player has: their keys' icons, where the Switch's were shown before. xinput: XInput has a controller that
|
||||
SDL does not name (HOS_NO_SDL=1, or no SDL beside the game): the game's own icons, as before.
|
||||
The game reads its icons once, at its start, so Auto is chosen once, then: it cannot follow the device used last during
|
||||
play, as NX2navi's prompts setting could not (a fixed choice, after a restart). 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) {
|
||||
if (!name[0]) return xinput ? HOS_PROMPT_SWITCH : 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) */
|
||||
}
|
||||
|
||||
/* HOS_PROMPT_* (faces[]) */
|
||||
static int choose_face(const char *dir) {
|
||||
char path[MAX_PATH], line[80];
|
||||
@@ -79,14 +100,9 @@ static int choose_face(const char *dir) {
|
||||
hos_sdl_pad_first_name(name, sizeof name);
|
||||
if (!name[0]) Sleep(50);
|
||||
}
|
||||
int face;
|
||||
if (!name[0]) face = HOS_PROMPT_SWITCH; /* (no controller: the keyboard, the game's own icons; or XInput without SDL) */
|
||||
else if (contains(name, "dualsense") || contains(name, "ps5")) face = HOS_PROMPT_PS5;
|
||||
else if (contains(name, "dualshock") || contains(name, "ps4") || contains(name, "playstation")) face = HOS_PROMPT_PS4;
|
||||
else if (contains(name, "steam deck")) face = HOS_PROMPT_STEAMDECK;
|
||||
else if (contains(name, "nintendo") || contains(name, "switch") || contains(name, "joy-con")) face = HOS_PROMPT_SWITCH;
|
||||
else face = HOS_PROMPT_XBOX; /* (Xbox pads, and the many that copy their layout) */
|
||||
hos_log("prompts: auto: controller \"%s\": %s", name[0] ? name : "(none)", faces[face]);
|
||||
const int xinput = !name[0] && hos_xinput_connected();
|
||||
const int face = auto_face(name, xinput);
|
||||
hos_log("prompts: auto: controller \"%s\": %s", name[0] ? name : xinput ? "(XInput's, no name)" : "(none)", faces[face]);
|
||||
return face;
|
||||
}
|
||||
|
||||
@@ -114,20 +130,36 @@ static unsigned cmap4_glyph(const uint8_t *st, size_t n, unsigned code) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* each Switch button's segment pointed at its pad input's icon; how many */
|
||||
static int cmap4_bind(uint8_t *st, size_t n) {
|
||||
/* (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; b++) {
|
||||
const int pad = hos_binding(HOS_BIND_PAD, b, 0);
|
||||
if (pad < 0 || pad >= HOS_PAD_N) continue; /* (unbound: the default icon stays) */
|
||||
const unsigned gid = cmap4_glyph(st, n, PAD_BASE + (unsigned)pad);
|
||||
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) == switch_code[b] && rd16(ends + 2 * i) == switch_code[b] && !rd16(ranges + 2 * i)) {
|
||||
wr16(deltas + 2 * i, (gid - switch_code[b]) & 0xFFFF);
|
||||
if (rd16(starts + 2 * i) == code && rd16(ends + 2 * i) == code && !rd16(ranges + 2 * i)) {
|
||||
wr16(deltas + 2 * i, (gid - code) & 0xFFFF);
|
||||
done++;
|
||||
break;
|
||||
}
|
||||
@@ -135,8 +167,35 @@ static int cmap4_bind(uint8_t *st, size_t n) {
|
||||
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) {
|
||||
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++) {
|
||||
@@ -156,7 +215,7 @@ static int bind_font(uint8_t *otf, size_t n) {
|
||||
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);
|
||||
done = cmap4_bind(cmap + so, sl <= len - so ? sl : len - so, face);
|
||||
}
|
||||
return done;
|
||||
}
|
||||
@@ -327,8 +386,8 @@ static int build_textures(char *out, size_t cap) {
|
||||
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];
|
||||
for (int b = 0; b < HOS_SW_N; b++) bind[b] = hos_binding(HOS_BIND_PAD, b, 0);
|
||||
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);
|
||||
@@ -351,7 +410,7 @@ static int build_textures(char *out, size_t cap) {
|
||||
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 this pad's controls%s", faces[face], nruns,
|
||||
hos_log("prompts: %s textures, %zu runs for these controls%s", faces[face], nruns,
|
||||
ok ? "" : ": could not be written");
|
||||
free(runs);
|
||||
free(t);
|
||||
@@ -400,9 +459,10 @@ 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];
|
||||
for (int b = 0; b < HOS_SW_N; b++) bind[b] = hos_binding(HOS_BIND_PAD, b, 0);
|
||||
ring_places(bind, place);
|
||||
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);
|
||||
@@ -518,12 +578,24 @@ static int build(char *out, size_t cap) {
|
||||
}
|
||||
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);
|
||||
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/External.bfotf.zs", dir);
|
||||
const int ok = write_zstd_raw(out, b, (size_t)n);
|
||||
free(b);
|
||||
hos_log("prompts: %s icons, %d buttons on this pad's controls%s", faces[face], bound, ok ? "" : ": could not be written");
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -295,6 +295,28 @@ static void flush_early(void) {
|
||||
ReleaseSRWLockExclusive(&early_lock);
|
||||
}
|
||||
|
||||
/* (Triforce) The lazy write-back (the Debug tab's row, gpu/vk/vk_memory.cpp): pages of guest memory may have no access while
|
||||
the GPU's newest bytes for them are in VRAM. The CPU's own touch faults and the renderer pays it first, but the kernel,
|
||||
handed such a page by ReadFile, WriteFile and the like, fails or reads old bytes, with no fault. So every host call that
|
||||
gives guest memory to the kernel, or changes guest pages' state (a commit, a discard, a decommit), calls
|
||||
hos_guest_io_begin with the range first and hos_guest_io_end with what it returned after it: what is owed there is copied
|
||||
down, and no page is protected until the end. With the row off, or without the Vulkan back end, both return at once.
|
||||
tests/test_lazy_wb_host.py finds the kernel calls in host/ that do neither. */
|
||||
static int (*volatile lazy_io_begin)(const void *, uint64_t); /* (the back end's mkwvk_lazy_io_begin, once it runs) */
|
||||
static void (*volatile lazy_io_end)(int);
|
||||
int hos_guest_io_begin(const void *p, uint64_t n) {
|
||||
int (*begin)(const void *, uint64_t) = lazy_io_begin;
|
||||
if (!begin || !n) return 0;
|
||||
const int away = hos_vcore_away(); /* (it may wait for the GPU: no virtual core held meanwhile, as rx_guest_imported) */
|
||||
const int held = begin(p, n);
|
||||
if (away) hos_vcore_back();
|
||||
return held;
|
||||
}
|
||||
void hos_guest_io_end(int held) {
|
||||
void (*end)(int) = lazy_io_end;
|
||||
if (held && end) end(held);
|
||||
}
|
||||
|
||||
/* dll: mkw_vk.dll (our Vulkan back end) or mkw_eden.dll (Eden's renderer driven natively, gpu/eden): one export */
|
||||
static const MkwVkRenderer *load_vulkan(const char *dll) {
|
||||
char path[MAX_PATH];
|
||||
@@ -325,6 +347,10 @@ int rx_start(void *hwnd) {
|
||||
const MkwVkRenderer *r = load_vulkan(dll);
|
||||
if (r && r->start(hwnd)) {
|
||||
vk = r;
|
||||
/* (Triforce: the lazy write-back's two calls, hos_guest_io_begin above; a back end without them has none) */
|
||||
lazy_io_end = (void (*)(int))(void *)GetProcAddress(vk_module, "mkwvk_lazy_io_end");
|
||||
if (lazy_io_end)
|
||||
lazy_io_begin = (int (*)(const void *, uint64_t))(void *)GetProcAddress(vk_module, "mkwvk_lazy_io_begin");
|
||||
hos_log("rx: renderer: %s (%s)", eden ? "Eden's" : "Vulkan", dll);
|
||||
flush_early();
|
||||
return 1;
|
||||
@@ -502,6 +528,38 @@ int rx_guest_imported(uint64_t lo, uint64_t hi) {
|
||||
if (away) hos_vcore_back();
|
||||
return r;
|
||||
}
|
||||
/* (Triforce) The census's give-back (hos_mem.c census_offer): release(lo, hi) is run by the Vulkan back end with its import
|
||||
locks held, and only where no import holds the pages (its mkwvk_guest_release, found by name: MkwVkHost and MkwVkRenderer
|
||||
stay as they are), so that none can lock them between the question and the decommit. 1: given back, 0: not (an import holds
|
||||
them, or release refused), -2: a back end without the export (nothing given back). Without the Vulkan back end nothing
|
||||
imports guest memory (rx_guest_imported's answer then), and release runs at once */
|
||||
int rx_guest_release(uint64_t lo, uint64_t hi, int (*release)(uint64_t lo, uint64_t hi)) {
|
||||
if (!vk) return release(lo, hi) ? 1 : 0;
|
||||
static int (*volatile fn)(uint64_t, uint64_t, int (*)(uint64_t, uint64_t));
|
||||
static volatile int looked;
|
||||
if (!looked) {
|
||||
fn = (int (*)(uint64_t, uint64_t, int (*)(uint64_t, uint64_t)))(void *)GetProcAddress(vk_module, "mkwvk_guest_release");
|
||||
looked = 1;
|
||||
}
|
||||
if (!fn) return -2;
|
||||
const int away = hos_vcore_away(); /* (as rx_guest_imported: the back end's pool lock) */
|
||||
const int r = fn(lo, hi, release);
|
||||
if (away) hos_vcore_back();
|
||||
return r == 1 ? 1 : 0;
|
||||
}
|
||||
/* (Triforce) The census's second line (mkw_glue.c census_main): the Vulkan back end's big holders of memory, as text (its
|
||||
mkwvk_memory_holders, found by name). Its length, 0 when there is none */
|
||||
int rx_memory_holders(char *out, size_t n) {
|
||||
if (!vk || !n) return 0;
|
||||
static int (*volatile fn)(char *, size_t);
|
||||
static volatile int looked;
|
||||
if (!looked) {
|
||||
fn = (int (*)(char *, size_t))(void *)GetProcAddress(vk_module, "mkwvk_memory_holders");
|
||||
looked = 1;
|
||||
}
|
||||
out[0] = 0;
|
||||
return fn ? fn(out, n) : 0;
|
||||
}
|
||||
/* (nvn_native.c: the CPU set an event's value) in order with the submits, for the GPU's waits on it */
|
||||
int rx_option(int id, int otherwise) { return vk && vk->option ? vk->option(id) : otherwise; }
|
||||
void rx_option_set(int id, int value) { if (vk && vk->option_set) vk->option_set(id, value); }
|
||||
|
||||
204
host/save_pack.c
Normal file
204
host/save_pack.c
Normal file
@@ -0,0 +1,204 @@
|
||||
/* (triforce) What a save export holds and what an import takes (save_pack.h). */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "save_pack.h"
|
||||
|
||||
#define GAME "The Legend of Zelda: Ocarina of Time"
|
||||
#define MANIFEST_MAX 65536
|
||||
|
||||
static int lower(int c) { return c >= 'A' && c <= 'Z' ? c + 32 : c; }
|
||||
|
||||
static int same_text(const char *a, const char *b) { /* ASCII, any case */
|
||||
while (*a && lower((unsigned char)*a) == lower((unsigned char)*b)) a++, b++;
|
||||
return !*a && !*b;
|
||||
}
|
||||
|
||||
static int ends_with(const char *s, const char *tail) {
|
||||
const size_t n = strlen(s), k = strlen(tail);
|
||||
return n >= k && same_text(s + n - k, tail);
|
||||
}
|
||||
|
||||
/* Key files never go into a zip or out of one, wherever a stray copy sits in a save folder. */
|
||||
static int key_file(const char *rel) { return ends_with(rel, ".keys") || ends_with(rel, ".keys.txt"); }
|
||||
|
||||
int save_pack_save_file(const char *rel) {
|
||||
if (!strcmp(rel, "account.sav") || !strcmp(rel, "option.sav")) return 1;
|
||||
return !strncmp(rel, "slot_", 5) && rel[5] >= '0' && rel[5] <= '9' && rel[6] >= '0' && rel[6] <= '9' &&
|
||||
!strcmp(rel + 7, "/progress.sav");
|
||||
}
|
||||
|
||||
/* (a part's name up to its first dot is what Windows takes for a device: "NUL", "nul.sav", "COM1.txt") */
|
||||
static int device_name(const char *part, size_t n) {
|
||||
static const char *const devices[] = {"CON", "PRN", "AUX", "NUL", "CONIN$", "CONOUT$", "CLOCK$"};
|
||||
size_t stem = 0;
|
||||
while (stem < n && part[stem] != '.') stem++;
|
||||
for (size_t d = 0; d < sizeof devices / sizeof *devices; d++) {
|
||||
size_t k = 0;
|
||||
while (k < stem && devices[d][k] && lower((unsigned char)part[k]) == lower((unsigned char)devices[d][k])) k++;
|
||||
if (k == stem && !devices[d][k]) return 1;
|
||||
}
|
||||
if (stem == 4 && part[3] >= '0' && part[3] <= '9') {
|
||||
const int c0 = lower((unsigned char)part[0]), c1 = lower((unsigned char)part[1]), c2 = lower((unsigned char)part[2]);
|
||||
if ((c0 == 'c' && c1 == 'o' && c2 == 'm') || (c0 == 'l' && c1 == 'p' && c2 == 't')) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int save_pack_safe_rel(const char *rel) {
|
||||
const size_t len = strlen(rel);
|
||||
if (!len || len > 100) return 0;
|
||||
int parts = 0;
|
||||
for (const char *p = rel;;) {
|
||||
const char *slash = strchr(p, '/');
|
||||
const size_t k = slash ? (size_t)(slash - p) : strlen(p);
|
||||
if (!k || ++parts > 4 || p[0] == '.' || p[k - 1] == '.' || device_name(p, k)) return 0;
|
||||
for (size_t i = 0; i < k; i++) {
|
||||
const char c = p[i];
|
||||
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-'))
|
||||
return 0;
|
||||
}
|
||||
if (!slash) return 1;
|
||||
p = slash + 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* ---- an export ---- */
|
||||
void save_pack_begin(SavePack *p, const struct tm *local) {
|
||||
memset(p, 0, sizeof *p);
|
||||
sz_writer_init(&p->w, local->tm_year + 1900, local->tm_mon + 1, local->tm_mday, local->tm_hour, local->tm_min, local->tm_sec);
|
||||
}
|
||||
|
||||
int save_pack_add_save(SavePack *p, const char *rel, const void *data, size_t n) {
|
||||
if (key_file(rel)) return 1;
|
||||
char name[128];
|
||||
if (!save_pack_safe_rel(rel) || snprintf(name, sizeof name, "save/%s", rel) >= (int)sizeof name) return 0;
|
||||
sz_writer_add(&p->w, name, data, n);
|
||||
p->files++;
|
||||
return !p->w.failed;
|
||||
}
|
||||
|
||||
void save_pack_add_report(SavePack *p, const char *name, const void *data, size_t n) {
|
||||
char member[160];
|
||||
if (snprintf(member, sizeof member, "bug-report/%s", name) < (int)sizeof member) sz_writer_add(&p->w, member, data, n);
|
||||
}
|
||||
|
||||
uint8_t *save_pack_end(SavePack *p, const char *version, const struct tm *utc, int bug_report, size_t *n) {
|
||||
char ver[96], manifest[1024];
|
||||
sz_json_escape(version ? version : "unknown", ver, sizeof ver);
|
||||
const int mn = snprintf(manifest, sizeof manifest,
|
||||
"{\n \"format\": \"" SAVE_PACK_FORMAT "\",\n \"format_version\": %d,\n \"game\": \"" GAME "\",\n"
|
||||
" \"title_id\": \"" SAVE_PACK_TITLE_ID "\",\n \"triforce\": \"%s\",\n"
|
||||
" \"exported\": \"%04d-%02d-%02dT%02d:%02d:%02dZ\",\n \"exported_by\": \"game\",\n"
|
||||
" \"files\": %d,\n \"bug_report\": %s\n}\n",
|
||||
SAVE_PACK_VERSION, ver, utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday, utc->tm_hour, utc->tm_min,
|
||||
utc->tm_sec, p->files, bug_report ? "true" : "false");
|
||||
sz_writer_add(&p->w, "manifest.json", manifest, (size_t)mn);
|
||||
return sz_writer_finish(&p->w, n);
|
||||
}
|
||||
|
||||
/* ---- an import ---- */
|
||||
static const SzEntry *find_member(const SzEntry *e, int count, const char *name) {
|
||||
for (int i = 0; i < count; i++)
|
||||
if (!strcmp(e[i].name, name)) return &e[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* The manifest's verdict on the zip; out gets who made it and when, *files how many files of the save it counts (-1: it
|
||||
does not say). */
|
||||
static SavePackResult check_manifest(const uint8_t *zip, size_t n, const SzEntry *e, int count, SavePackSave *out, long long *files) {
|
||||
const SzEntry *m = find_member(e, count, "manifest.json");
|
||||
if (!m || m->usize > MANIFEST_MAX) return SAVE_PACK_NOT_A_SAVE;
|
||||
int err;
|
||||
char *json = (char *)sz_extract(zip, n, m, &err);
|
||||
if (!json) return err == SZ_E_MEMORY ? SAVE_PACK_NO_MEMORY : SAVE_PACK_DAMAGED;
|
||||
char format[32] = "", title[32] = "", ver[64] = "", when[32] = "";
|
||||
long long version = 0;
|
||||
SavePackResult r = SAVE_PACK_OK;
|
||||
if (!sz_json_int(json, "files", files)) *files = -1;
|
||||
const int has_format = sz_json_str(json, "format", format, sizeof format);
|
||||
const int ours = has_format && !strcmp(format, SAVE_PACK_FORMAT), navi = has_format && !strcmp(format, SAVE_PACK_FORMAT_NX2NAVI);
|
||||
if ((!ours && !navi) || !sz_json_int(json, "format_version", &version) || version < 1) r = SAVE_PACK_NOT_A_SAVE;
|
||||
else if (version > SAVE_PACK_VERSION) r = SAVE_PACK_NEWER;
|
||||
else if (!sz_json_str(json, "title_id", title, sizeof title) || !same_text(title, SAVE_PACK_TITLE_ID)) r = SAVE_PACK_OTHER_GAME;
|
||||
if (ours || navi) {
|
||||
sz_json_str(json, ours ? "triforce" : "nx2navi", ver, sizeof ver);
|
||||
sz_json_str(json, "exported", when, sizeof when);
|
||||
snprintf(out->made_by, sizeof out->made_by, "%s %s", ours ? "Triforce" : "NX2navi", ver[0] ? ver : "?");
|
||||
snprintf(out->exported, sizeof out->exported, "%.10s", when[0] ? when : "?");
|
||||
}
|
||||
free(json);
|
||||
return r;
|
||||
}
|
||||
|
||||
void save_pack_free(SavePackSave *s) {
|
||||
for (int i = 0; i < s->count; i++) free(s->rels ? s->rels[i] : NULL), free(s->data ? s->data[i] : NULL);
|
||||
free(s->rels), free(s->data), free(s->sizes);
|
||||
s->rels = NULL, s->data = NULL, s->sizes = NULL, s->count = 0;
|
||||
}
|
||||
|
||||
SavePackResult save_pack_open(const uint8_t *zip, size_t n, SavePackSave *out) {
|
||||
memset(out, 0, sizeof *out);
|
||||
if (n > SAVE_PACK_MAX_ZIP) return SAVE_PACK_UNSAFE;
|
||||
SzEntry *e = NULL;
|
||||
int count = 0;
|
||||
long long said = -1;
|
||||
const int zr = sz_read_dir(zip, n, &e, &count);
|
||||
SavePackResult r = zr == SZ_OK ? check_manifest(zip, n, e, count, out, &said)
|
||||
: zr == SZ_E_NOT_ZIP ? SAVE_PACK_NOT_A_SAVE
|
||||
: zr == SZ_E_MEMORY ? SAVE_PACK_NO_MEMORY
|
||||
: SAVE_PACK_DAMAGED;
|
||||
/* the save's members: all of them checked before any is unpacked */
|
||||
int files = 0, has_save = 0;
|
||||
uint64_t bytes = 0;
|
||||
for (int i = 0; r == SAVE_PACK_OK && i < count; i++) {
|
||||
if (strncmp(e[i].name, "save/", 5) || e[i].is_dir) continue;
|
||||
const char *rel = e[i].name + 5;
|
||||
if (e[i].is_link || !save_pack_safe_rel(rel)) r = SAVE_PACK_UNSAFE;
|
||||
else if (key_file(rel)) continue;
|
||||
for (int k = 0; r == SAVE_PACK_OK && k < i; k++) /* (a name twice, in any case: one file on Windows) */
|
||||
if (!strncmp(e[k].name, "save/", 5) && same_text(e[k].name, e[i].name)) r = SAVE_PACK_UNSAFE;
|
||||
bytes += e[i].usize;
|
||||
if (++files > SAVE_PACK_MAX_FILES || bytes > SAVE_PACK_MAX_BYTES) r = SAVE_PACK_UNSAFE;
|
||||
has_save |= save_pack_save_file(rel);
|
||||
}
|
||||
if (r == SAVE_PACK_OK && !has_save) r = SAVE_PACK_NOT_A_SAVE;
|
||||
/* (the manifest counts the save's files: a member gone from the directory, or one too many, is damage) */
|
||||
if (r == SAVE_PACK_OK && said >= 0 && said != files) r = SAVE_PACK_DAMAGED;
|
||||
if (r == SAVE_PACK_OK) {
|
||||
out->rels = calloc((size_t)files, sizeof *out->rels);
|
||||
out->data = calloc((size_t)files, sizeof *out->data);
|
||||
out->sizes = calloc((size_t)files, sizeof *out->sizes);
|
||||
if (!out->rels || !out->data || !out->sizes) r = SAVE_PACK_NO_MEMORY;
|
||||
}
|
||||
for (int i = 0; r == SAVE_PACK_OK && i < count; i++) {
|
||||
if (strncmp(e[i].name, "save/", 5) || e[i].is_dir || key_file(e[i].name + 5)) continue;
|
||||
int err;
|
||||
const int k = out->count;
|
||||
out->data[k] = sz_extract(zip, n, &e[i], &err);
|
||||
const size_t len = strlen(e[i].name + 5) + 1;
|
||||
out->rels[k] = out->data[k] ? malloc(len) : NULL;
|
||||
if (!out->data[k]) r = err == SZ_E_MEMORY ? SAVE_PACK_NO_MEMORY : SAVE_PACK_DAMAGED;
|
||||
else if (!out->rels[k]) r = SAVE_PACK_NO_MEMORY;
|
||||
else memcpy(out->rels[k], e[i].name + 5, len);
|
||||
out->sizes[k] = (size_t)e[i].usize;
|
||||
out->count++; /* (counted either way: save_pack_free frees what this one got) */
|
||||
}
|
||||
free(e);
|
||||
if (r != SAVE_PACK_OK) save_pack_free(out);
|
||||
return r;
|
||||
}
|
||||
|
||||
const char *save_pack_why(SavePackResult r) {
|
||||
static const char *const why[] = {
|
||||
"It is a save export.",
|
||||
"Not enough memory to read it.",
|
||||
"It is not a save export of Triforce or NX2navi.",
|
||||
"It is a save of another game.",
|
||||
"It was made by a newer version: update Triforce first.",
|
||||
"It is damaged.",
|
||||
"It holds paths outside the save, links, or far more than a save.",
|
||||
};
|
||||
return (unsigned)r < sizeof why / sizeof *why ? why[r] : "It could not be read.";
|
||||
}
|
||||
83
host/save_pack.h
Normal file
83
host/save_pack.h
Normal file
@@ -0,0 +1,83 @@
|
||||
/* (triforce) What a save export holds and what an import takes (save_pack.c). The zip, as NX2navi's Export save writes its own
|
||||
(its docs/launcher.md, "Sharing a save"), so one reader takes both:
|
||||
|
||||
manifest.json {"format": "triforce-save", "format_version": 1, "game", "title_id", "triforce" (the version),
|
||||
"exported" (UTC), "exported_by": "game", "files", "bug_report"}
|
||||
save/... the save's files as the game keeps them (account.sav, option.sav, slot_00/progress.sav, ...)
|
||||
bug-report/... with a bug report: the logs, triforce_options.ini and triforce_hang.txt; an import leaves them out
|
||||
|
||||
An import takes a zip whose manifest names this format or NX2navi's ("nx2navi-save"), a version up to SAVE_PACK_VERSION
|
||||
and this game's title id; whose save/ paths are plain names that stay inside the save (save_pack_safe_rel), no links, no
|
||||
name twice, at most SAVE_PACK_MAX_FILES files and SAVE_PACK_MAX_BYTES, as many as the manifest counts; and that holds a
|
||||
file of this game's save. Every member's CRC-32 is checked as it is unpacked, all of them before the caller writes
|
||||
anything. Never a key file, either way.
|
||||
Plain C on the C library and save_zip.c: tests/test_save_zip.py builds the two alone. */
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "save_zip.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SAVE_PACK_FORMAT "triforce-save"
|
||||
#define SAVE_PACK_FORMAT_NX2NAVI "nx2navi-save"
|
||||
#define SAVE_PACK_VERSION 1
|
||||
#define SAVE_PACK_TITLE_ID "0400486024dec000" /* (the game's title id, as the manifests write it) */
|
||||
#define SAVE_PACK_MAX_FILES 256 /* (a save is 14 files) */
|
||||
#define SAVE_PACK_MAX_BYTES (64u << 20) /* (and about 1 MB) */
|
||||
#define SAVE_PACK_MAX_ZIP (96u << 20)
|
||||
|
||||
/* ---- an export ---- */
|
||||
typedef struct {
|
||||
SzWriter w;
|
||||
int files; /* (the save's files added so far) */
|
||||
} SavePack;
|
||||
/* local: the time the members are stamped with */
|
||||
void save_pack_begin(SavePack *p, const struct tm *local);
|
||||
/* A file of the save, rel its path below the save folder with / between its parts. 1: added (or a key file, left out);
|
||||
0: the path is not one a save has, or no memory */
|
||||
int save_pack_add_save(SavePack *p, const char *rel, const void *data, size_t n);
|
||||
/* A file for a bug report, as bug-report/<name> */
|
||||
void save_pack_add_report(SavePack *p, const char *name, const void *data, size_t n);
|
||||
/* The manifest written and the zip finished: *n bytes the caller frees, NULL when something could not be added. version:
|
||||
the build's; utc: the export's time */
|
||||
uint8_t *save_pack_end(SavePack *p, const char *version, const struct tm *utc, int bug_report, size_t *n);
|
||||
|
||||
/* ---- an import ---- */
|
||||
typedef enum {
|
||||
SAVE_PACK_OK,
|
||||
SAVE_PACK_NO_MEMORY,
|
||||
SAVE_PACK_NOT_A_SAVE, /* not a zip, no manifest, another format, or no file of this game's save in it */
|
||||
SAVE_PACK_OTHER_GAME, /* the manifest names another game */
|
||||
SAVE_PACK_NEWER, /* a format version this build does not know */
|
||||
SAVE_PACK_DAMAGED, /* the zip's structure, a member's data or its CRC-32 is wrong */
|
||||
SAVE_PACK_UNSAFE, /* a path outside the save, a link, a name twice, or far more than a save */
|
||||
} SavePackResult;
|
||||
typedef struct {
|
||||
int count;
|
||||
char **rels; /* each file's path below the save folder, / between its parts */
|
||||
uint8_t **data;
|
||||
size_t *sizes;
|
||||
char made_by[96]; /* "Triforce v0.4.0" or "NX2navi 0.12.0", from the manifest ("?" for what it lacks) */
|
||||
char exported[32]; /* the export's day, as the manifest has it: 2026-11-15 */
|
||||
} SavePackSave;
|
||||
/* The save in the zip zip[0..n), checked whole. On SAVE_PACK_OK out holds its files (save_pack_free lets them go); otherwise
|
||||
out holds none, with made_by and exported filled when the manifest could be read */
|
||||
SavePackResult save_pack_open(const uint8_t *zip, size_t n, SavePackSave *out);
|
||||
void save_pack_free(SavePackSave *s);
|
||||
/* Why an import was refused, as a sentence for the menu and the log */
|
||||
const char *save_pack_why(SavePackResult r);
|
||||
|
||||
/* 1: rel is a path a save's file can have: one to four parts of ASCII letters, digits, '.', '_' and '-' with / between
|
||||
them, none starting or ending with a dot, none a Windows device name (NUL, COM1, "aux.sav"), 100 bytes at most */
|
||||
int save_pack_safe_rel(const char *rel);
|
||||
/* 1: a file only this game's save has (account.sav, option.sav, slot_NN/progress.sav) */
|
||||
int save_pack_save_file(const char *rel);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
393
host/save_share.c
Normal file
393
host/save_share.c
Normal file
@@ -0,0 +1,393 @@
|
||||
/* (triforce) Sharing a save from the System tab, after NX2navi's menu rows (its docs/launcher.md, "Sharing a save"): Export save
|
||||
writes the save as a zip into the player's Downloads folder, Export save for a bug report adds the logs, the settings and
|
||||
the freeze report to it, and Import a save takes such a zip (Triforce's or NX2navi's) from the game folder's save-import
|
||||
folder and leaves its save to wait for the next start, where it becomes the save with the one it replaces kept first
|
||||
(host/save_tx_share.h, host/nn_fs.c). No game file goes into a zip.
|
||||
|
||||
The rows are the renderer's (gpu/vk/vk_menu.cpp), the work is done here, on one thread of this file's own, so the game
|
||||
and its picture go on meanwhile: the thread asks the menu ten times a second what was chosen (rx_option
|
||||
HOS_OPT_SHARE_ASK), does it, and tells the row what to show under its help (HOS_OPT_SHARE_TEXT a byte at a time, then
|
||||
HOS_OPT_SHARE_SAID). It starts with the game's first save mount (nn_fs.c). The bug capture (host/bug_capture.c) is
|
||||
written on the same thread, and its save is this file's export.
|
||||
|
||||
The save in a zip is the one the next start would load, read with no Commit under way (hos_fs_save_read): the files of
|
||||
one commit, never the working copy the game is writing. With HOS_SAVE_TX=0 the game writes its save in place and
|
||||
nothing is exported: a copy could catch a file half written.
|
||||
|
||||
HOS_SAVE_EXPORT_DIR=<folder>: where the zips go, in place of the Downloads folder (a test's, or a player's own). */
|
||||
#include "hos_paths.h" /* (first: hos_game_path_a, and windows.h on Windows) */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "../gpu/vk/vk_api.h"
|
||||
#include "save_pack.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#define SEP "\\"
|
||||
#else
|
||||
#define SEP "/"
|
||||
#endif
|
||||
#define SHARE_PATH 1024
|
||||
#define LOG_MAX (16u << 20) /* (of a log in a bug report: more than that is a trace run's, cut in its middle) */
|
||||
|
||||
void hos_log(const char *fmt, ...);
|
||||
void hos_log_flush(void);
|
||||
int rx_option(int id, int otherwise);
|
||||
void rx_option_set(int id, int value);
|
||||
const char *hos_version(void); /* hos_main.c */
|
||||
/* nn_fs.c */
|
||||
int hos_fs_save_read(int (*fn)(void *arg, const char *rel, const void *data, size_t n), void *arg);
|
||||
int hos_fs_save_import_stage(int n, const char *const *rels, const uint8_t *const *data, const size_t *sizes);
|
||||
int hos_fs_save_import_waits(void);
|
||||
int hos_fs_save_import_cancel(void);
|
||||
int hos_fs_save_import_note(char *out, size_t cap);
|
||||
/* bug_capture.c */
|
||||
int hos_bug_capture_ask(const char *trigger);
|
||||
void hos_bug_capture_key(void);
|
||||
void hos_bug_capture_tick(void);
|
||||
void hos_bug_capture_work(void);
|
||||
|
||||
/* The game folder (the log, the settings and the saves are in it: hos_paths.h), with no separator at its end. 0: unknown */
|
||||
int hos_share_game_dir(char *out, size_t cap) {
|
||||
char exe[MAX_PATH];
|
||||
DWORD n = hos_game_path_a(exe, MAX_PATH);
|
||||
if (!n || n >= MAX_PATH) return 0;
|
||||
while (n && exe[n - 1] != '\\' && exe[n - 1] != '/') n--;
|
||||
if (n < 2) return 0;
|
||||
exe[n - 1] = 0;
|
||||
return snprintf(out, cap, "%s", exe) < (int)cap;
|
||||
}
|
||||
|
||||
static int is_dir(const char *path) {
|
||||
const DWORD at = GetFileAttributesA(path);
|
||||
return at != INVALID_FILE_ATTRIBUTES && (at & FILE_ATTRIBUTE_DIRECTORY);
|
||||
}
|
||||
|
||||
/* The player's Downloads folder. 0: not known, or not there */
|
||||
static int downloads_dir(char *out, size_t cap) {
|
||||
const char *forced = getenv("HOS_SAVE_EXPORT_DIR");
|
||||
if (forced && *forced) return snprintf(out, cap, "%s", forced) < (int)cap;
|
||||
#ifdef _WIN32
|
||||
/* (Windows' known folder: a player may have moved Downloads to another drive. shell32 and ole32 are looked up here, so
|
||||
the game links neither) */
|
||||
static const struct { uint32_t a; uint16_t b, c; uint8_t d[8]; } downloads = { /* FOLDERID_Downloads */
|
||||
0x374DE290, 0x123F, 0x4565, {0x91, 0x64, 0x39, 0xC4, 0x92, 0x5E, 0x46, 0x7B}};
|
||||
typedef HRESULT (WINAPI *known_fn)(const void *, DWORD, HANDLE, wchar_t **);
|
||||
typedef void (WINAPI *free_fn)(void *);
|
||||
HMODULE shell = LoadLibraryExA("shell32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
||||
HMODULE ole = LoadLibraryExA("ole32.dll", NULL, LOAD_LIBRARY_SEARCH_SYSTEM32);
|
||||
known_fn known = shell ? (known_fn)(void *)GetProcAddress(shell, "SHGetKnownFolderPath") : NULL;
|
||||
free_fn co_free = ole ? (free_fn)(void *)GetProcAddress(ole, "CoTaskMemFree") : NULL;
|
||||
wchar_t *wide = NULL;
|
||||
int ok = 0;
|
||||
if (known && co_free && known(&downloads, 0, NULL, &wide) >= 0 && wide)
|
||||
ok = WideCharToMultiByte(CP_UTF8, 0, wide, -1, out, (int)cap, NULL, NULL) > 0;
|
||||
if (wide && co_free) co_free(wide);
|
||||
return ok && is_dir(out);
|
||||
#else
|
||||
const char *home = getenv("HOME");
|
||||
return home && home[0] == '/' && snprintf(out, cap, "%s/Downloads", home) < (int)cap && is_dir(out);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* A file whole: malloc'd, *n its size, NULL when it is not there. One longer than max keeps its first sixteenth and its
|
||||
end, with a line between them that says what was left out (a log: its start names the build and the GPU, its end has
|
||||
what happened) */
|
||||
static uint8_t *read_file(const char *path, size_t max, size_t *n) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return NULL;
|
||||
_fseeki64(f, 0, SEEK_END);
|
||||
const long long size = _ftelli64(f);
|
||||
uint8_t *data = size >= 0 ? (uint8_t *)malloc((size_t)(size > (long long)max ? max : size) + 96) : NULL;
|
||||
size_t got = 0;
|
||||
if (data && size <= (long long)max) {
|
||||
_fseeki64(f, 0, SEEK_SET);
|
||||
got = fread(data, 1, (size_t)size, f);
|
||||
} else if (data) {
|
||||
const size_t head = max / 16, tail = max - head;
|
||||
_fseeki64(f, 0, SEEK_SET);
|
||||
got = fread(data, 1, head, f);
|
||||
got += (size_t)snprintf((char *)data + got, 96, "\n[... %lld bytes left out here ...]\n", size - (long long)max);
|
||||
_fseeki64(f, size - (long long)tail, SEEK_SET);
|
||||
got += fread(data + got, 1, tail, f);
|
||||
}
|
||||
fclose(f);
|
||||
*n = got;
|
||||
return data;
|
||||
}
|
||||
|
||||
/* ---- the save's files, kept while nn_fs.c holds its lock and packed after ---- */
|
||||
typedef struct {
|
||||
char rel[104];
|
||||
uint8_t *data;
|
||||
size_t n;
|
||||
} SaveFile;
|
||||
typedef struct {
|
||||
SaveFile *f;
|
||||
int count, cap, left_out;
|
||||
} SaveFiles;
|
||||
|
||||
static int keep_file(void *arg, const char *rel, const void *data, size_t n) {
|
||||
SaveFiles *s = (SaveFiles *)arg;
|
||||
if (!save_pack_safe_rel(rel)) { /* (not a name a save's file has: something of the player's own in the folder) */
|
||||
s->left_out++;
|
||||
return 1;
|
||||
}
|
||||
if (s->count == s->cap) {
|
||||
const int cap = s->cap ? s->cap * 2 : 32;
|
||||
SaveFile *f = (SaveFile *)realloc(s->f, (size_t)cap * sizeof *f);
|
||||
if (!f) return 0;
|
||||
s->f = f, s->cap = cap;
|
||||
}
|
||||
SaveFile *f = &s->f[s->count];
|
||||
f->data = (uint8_t *)malloc(n ? n : 1);
|
||||
if (!f->data) return 0;
|
||||
memcpy(f->data, data, n);
|
||||
f->n = n;
|
||||
snprintf(f->rel, sizeof f->rel, "%s", rel);
|
||||
s->count++;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void files_free(SaveFiles *s) {
|
||||
for (int i = 0; i < s->count; i++) free(s->f[i].data);
|
||||
free(s->f);
|
||||
memset(s, 0, sizeof *s);
|
||||
}
|
||||
|
||||
/* The zip's bytes written as <path>.part and renamed when whole: nothing of that name is half a zip. 1: written */
|
||||
static int write_whole(const char *path, const uint8_t *data, size_t n) {
|
||||
char part[SHARE_PATH + 8];
|
||||
snprintf(part, sizeof part, "%s.part", path);
|
||||
FILE *f = fopen(part, "wb");
|
||||
if (!f) return 0;
|
||||
int ok = fwrite(data, 1, n, f) == n;
|
||||
if (fflush(f)) ok = 0;
|
||||
if (fclose(f)) ok = 0;
|
||||
if (ok && !MoveFileExA(part, path, 0)) ok = 0;
|
||||
if (!ok) DeleteFileA(part);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* The save the next start would load as Triforce-save-<date>-<time>.zip in dir (made when missing), with the logs, the
|
||||
settings and the freeze report in a bug report's. 1: written, its path in path; 0: nothing written. *files: the save's
|
||||
files in it, or why it has none: 0 the game has not saved yet, -1 one could not be read, -2 the save is written in
|
||||
place in this run (hos_fs_save_read). A bug report is written without a save too; a plain export is not */
|
||||
int hos_save_export(const char *dir, int bug_report, char *path, size_t cap, int *files) {
|
||||
path[0] = 0;
|
||||
SaveFiles kept;
|
||||
memset(&kept, 0, sizeof kept);
|
||||
*files = hos_fs_save_read(keep_file, &kept);
|
||||
if (*files > 0) *files = kept.count;
|
||||
if (*files <= 0 && !bug_report) {
|
||||
files_free(&kept);
|
||||
return 0;
|
||||
}
|
||||
const time_t now = time(NULL);
|
||||
struct tm local, utc;
|
||||
localtime_s(&local, &now);
|
||||
gmtime_s(&utc, &now);
|
||||
SavePack pack;
|
||||
save_pack_begin(&pack, &local);
|
||||
for (int i = 0; *files > 0 && i < kept.count; i++) save_pack_add_save(&pack, kept.f[i].rel, kept.f[i].data, kept.f[i].n);
|
||||
const int left_out = kept.left_out;
|
||||
files_free(&kept);
|
||||
char game[MAX_PATH], file[MAX_PATH + 40];
|
||||
if (bug_report && hos_share_game_dir(game, sizeof game)) {
|
||||
static const char *const names[] = {"triforce_log.txt", "triforce_log.1.txt", "triforce_options.ini", "triforce_hang.txt"};
|
||||
hos_log_flush(); /* (the lines queued so far: the log in the zip ends where the export began) */
|
||||
for (size_t i = 0; i < sizeof names / sizeof *names; i++) {
|
||||
size_t n = 0;
|
||||
snprintf(file, sizeof file, "%s" SEP "%s", game, names[i]);
|
||||
uint8_t *data = read_file(file, LOG_MAX, &n);
|
||||
if (data) save_pack_add_report(&pack, names[i], data, n);
|
||||
free(data);
|
||||
}
|
||||
}
|
||||
size_t zn = 0;
|
||||
uint8_t *zip = save_pack_end(&pack, hos_version(), &utc, bug_report, &zn);
|
||||
if (!zip) {
|
||||
hos_log("save: export: no memory for the zip");
|
||||
return 0;
|
||||
}
|
||||
CreateDirectoryA(dir, NULL);
|
||||
char stem[64];
|
||||
snprintf(stem, sizeof stem, "Triforce-save-%04d%02d%02d-%02d%02d%02d%s", local.tm_year + 1900, local.tm_mon + 1, local.tm_mday,
|
||||
local.tm_hour, local.tm_min, local.tm_sec, bug_report ? "-bug-report" : "");
|
||||
int ok = 0;
|
||||
for (int k = 1; k < 100 && !ok; k++) { /* (two exports in one second: "(2)") */
|
||||
const int len = k == 1 ? snprintf(path, cap, "%s" SEP "%s.zip", dir, stem) : snprintf(path, cap, "%s" SEP "%s (%d).zip", dir, stem, k);
|
||||
if (len < 0 || (size_t)len >= cap || len >= SHARE_PATH) break;
|
||||
if (GetFileAttributesA(path) != INVALID_FILE_ATTRIBUTES) continue;
|
||||
ok = write_whole(path, zip, zn);
|
||||
if (!ok) break;
|
||||
}
|
||||
free(zip);
|
||||
if (!ok) {
|
||||
hos_log("save: export: the zip could not be written into %s", dir);
|
||||
path[0] = 0;
|
||||
return 0;
|
||||
}
|
||||
hos_log("save: exported %d save files%s to %s (%zu bytes%s)", *files > 0 ? *files : 0, bug_report ? " with the logs and the settings" : "",
|
||||
path, zn, left_out ? "; files that are not a save's were left out" : "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ---- what the rows show: a line under the row's help ---- */
|
||||
static void say(int row, const char *text) {
|
||||
for (const char *p = text; *p; p++) rx_option_set(HOS_OPT_SHARE_TEXT, (unsigned char)*p);
|
||||
rx_option_set(HOS_OPT_SHARE_SAID, row);
|
||||
}
|
||||
|
||||
void hos_save_share_say(int row, const char *text) { say(row, text); } /* (bug_capture.c's row, and its notice) */
|
||||
|
||||
static void do_export(int bug_report) {
|
||||
const int row = bug_report ? HOS_SHARE_EXPORT_BUG : HOS_SHARE_EXPORT;
|
||||
char dir[SHARE_PATH], path[SHARE_PATH], line[SHARE_PATH + 200];
|
||||
int files = 0;
|
||||
const int downloads = downloads_dir(dir, sizeof dir);
|
||||
int ok = (downloads || hos_share_game_dir(dir, sizeof dir)) && hos_save_export(dir, bug_report, path, sizeof path, &files);
|
||||
if (!ok && downloads && (files > 0 || bug_report) && hos_share_game_dir(dir, sizeof dir)) /* (Downloads cannot be written: beside the game) */
|
||||
ok = hos_save_export(dir, bug_report, path, sizeof path, &files);
|
||||
if (ok && files > 0) snprintf(line, sizeof line, "Saved as %s", path);
|
||||
else if (ok) snprintf(line, sizeof line, "Saved as %s: the logs and the settings, without a save (%s).", path,
|
||||
files == 0 ? "the game has not saved yet" : files == -1 ? "it could not be read" : "it is written in place in this run");
|
||||
else if (files == 0) snprintf(line, sizeof line, "There is no save yet: save in the game first.");
|
||||
else if (files == -1) snprintf(line, sizeof line, "A file of the save could not be read. Nothing was written.");
|
||||
else if (files == -2)
|
||||
snprintf(line, sizeof line, "Not in this run: the save is written in place (HOS_SAVE_TX=0), and a copy could catch a file half written.");
|
||||
else snprintf(line, sizeof line, "The zip could not be written into %s", dir);
|
||||
say(row, line);
|
||||
}
|
||||
|
||||
/* The zip to import: the newest .zip of dir (by its time, then by its name: an export's name has its date). 1: its name in name */
|
||||
static int find_zip(const char *dir, char *name, size_t cap) {
|
||||
char pat[SHARE_PATH];
|
||||
WIN32_FIND_DATAA fd;
|
||||
uint64_t best = 0;
|
||||
name[0] = 0;
|
||||
snprintf(pat, sizeof pat, "%s" SEP "*.zip", dir);
|
||||
HANDLE f = FindFirstFileA(pat, &fd);
|
||||
if (f == INVALID_HANDLE_VALUE) return 0;
|
||||
do {
|
||||
if (fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) continue;
|
||||
const uint64_t when = (uint64_t)fd.ftLastWriteTime.dwHighDateTime << 32 | fd.ftLastWriteTime.dwLowDateTime;
|
||||
if (name[0] && (when < best || (when == best && strcmp(fd.cFileName, name) <= 0))) continue;
|
||||
if (strlen(fd.cFileName) >= cap) continue;
|
||||
best = when;
|
||||
snprintf(name, cap, "%s", fd.cFileName);
|
||||
} while (FindNextFileA(f, &fd));
|
||||
FindClose(f);
|
||||
return name[0] != 0;
|
||||
}
|
||||
|
||||
static void do_import(void) {
|
||||
char game[MAX_PATH], dir[MAX_PATH + 16], name[260], path[SHARE_PATH], line[SHARE_PATH + 400];
|
||||
if (!hos_share_game_dir(game, sizeof game)) {
|
||||
say(HOS_SHARE_IMPORT, "The game folder is not known: nothing was imported.");
|
||||
return;
|
||||
}
|
||||
snprintf(dir, sizeof dir, "%s" SEP "save-import", game);
|
||||
CreateDirectoryA(dir, NULL); /* (there for the player to find) */
|
||||
if (!find_zip(dir, name, sizeof name) || snprintf(path, sizeof path, "%s" SEP "%s", dir, name) >= (int)sizeof path) {
|
||||
snprintf(line, sizeof line, "Put a save export of Triforce or NX2navi (a .zip) into %s first, then choose this row again.", dir);
|
||||
say(HOS_SHARE_IMPORT, line);
|
||||
return;
|
||||
}
|
||||
size_t n = 0;
|
||||
uint8_t *zip = read_file(path, SAVE_PACK_MAX_ZIP + 1, &n); /* (a byte past the most a save's zip may have: refused as too large) */
|
||||
const int was_read = zip != NULL;
|
||||
SavePackSave save;
|
||||
memset(&save, 0, sizeof save);
|
||||
const SavePackResult r = was_read ? save_pack_open(zip, n, &save) : SAVE_PACK_DAMAGED;
|
||||
free(zip);
|
||||
if (r != SAVE_PACK_OK) { /* (refused whole: nothing of it was written anywhere) */
|
||||
hos_log("save: import of %s refused: %s", path, was_read ? save_pack_why(r) : "It could not be read.");
|
||||
snprintf(line, sizeof line, "%s was not imported. %s", name, was_read ? save_pack_why(r) : "It could not be read.");
|
||||
say(HOS_SHARE_IMPORT, line);
|
||||
return;
|
||||
}
|
||||
const int staged = hos_fs_save_import_stage(save.count, (const char *const *)save.rels, (const uint8_t *const *)save.data, save.sizes);
|
||||
const int count = save.count;
|
||||
char made[sizeof save.made_by], when[sizeof save.exported];
|
||||
memcpy(made, save.made_by, sizeof made), memcpy(when, save.exported, sizeof when);
|
||||
save_pack_free(&save);
|
||||
if (staged != 1) {
|
||||
hos_log("save: import of %s: %s", path, staged == -2 ? "no save folder to put it under in this run" : "its files could not be written");
|
||||
snprintf(line, sizeof line,
|
||||
staged == -2 ? "%s was not imported: the game has no save folder yet, or writes its save in place in this run (HOS_SAVE_TX=0)."
|
||||
: "%s was not imported: its files could not be written to wait for the next start.", name);
|
||||
say(HOS_SHARE_IMPORT, line);
|
||||
return;
|
||||
}
|
||||
/* (the zip out of the way, so the folder holds only what is still to import; kept, for the player) */
|
||||
char done_dir[MAX_PATH + 32], done[SHARE_PATH];
|
||||
snprintf(done_dir, sizeof done_dir, "%s" SEP "imported", dir);
|
||||
CreateDirectoryA(done_dir, NULL);
|
||||
snprintf(done, sizeof done, "%s" SEP "%s", done_dir, name);
|
||||
const int moved = MoveFileExA(path, done, MOVEFILE_REPLACE_EXISTING) != 0;
|
||||
rx_option_set(HOS_OPT_SHARE_IMPORT, 1);
|
||||
hos_log("save: import: %d files of %s (%s, exported %s) wait for the next start%s", count, path, made, when,
|
||||
moved ? "; the zip went to the imported folder" : "");
|
||||
snprintf(line, sizeof line, "%s (%s, exported %s) becomes your save at the next start: restart the game. The save it replaces is "
|
||||
"kept first, in the user folder's save-backup. Choose this row again to cancel.", name, made, when);
|
||||
say(HOS_SHARE_IMPORT, line);
|
||||
}
|
||||
|
||||
static void do_import_cancel(void) {
|
||||
const int gone = hos_fs_save_import_cancel();
|
||||
rx_option_set(HOS_OPT_SHARE_IMPORT, gone ? 0 : 1);
|
||||
hos_log("save: import: %s", gone ? "cancelled, the save stays as it is" : "could not be cancelled (its folder is in use)");
|
||||
say(HOS_SHARE_IMPORT, gone ? "The import was cancelled: your save stays as it is. The zip is in the save-import folder's imported folder."
|
||||
: "The import could not be cancelled: its folder is in use. Try again.");
|
||||
}
|
||||
|
||||
/* ---- the thread ---- */
|
||||
static HANDLE share_wake; /* (set to look at once, without waiting for the next tenth of a second) */
|
||||
static volatile LONG share_started;
|
||||
|
||||
static DWORD WINAPI share_main(LPVOID arg) {
|
||||
(void)arg;
|
||||
int told = 0;
|
||||
for (;;) {
|
||||
WaitForSingleObject(share_wake, 100);
|
||||
if (!told && rx_option(1, -1) >= 0) { /* (the menu is there now: what the Import row shows from the start) */
|
||||
told = 1;
|
||||
char note[SHARE_PATH + 200];
|
||||
const int waits = hos_fs_save_import_waits();
|
||||
rx_option_set(HOS_OPT_SHARE_IMPORT, waits);
|
||||
if (hos_fs_save_import_note(note, sizeof note)) say(HOS_SHARE_IMPORT, note);
|
||||
else if (waits) say(HOS_SHARE_IMPORT, "An imported save waits for the next start. Choose this row to cancel it.");
|
||||
}
|
||||
hos_bug_capture_tick(); /* (the frames shown so far: a capture's frame rate) */
|
||||
switch (rx_option(HOS_OPT_SHARE_ASK, 0)) {
|
||||
case HOS_SHARE_EXPORT: do_export(0); break;
|
||||
case HOS_SHARE_EXPORT_BUG: do_export(1); break;
|
||||
case HOS_SHARE_IMPORT: do_import(); break;
|
||||
case HOS_SHARE_IMPORT_CANCEL: do_import_cancel(); break;
|
||||
case HOS_SHARE_CAPTURE:
|
||||
if (!hos_bug_capture_ask("the System tab's row")) say(HOS_SHARE_CAPTURE, "A capture is being written already.");
|
||||
break;
|
||||
case HOS_SHARE_CAPTURE_KEY: hos_bug_capture_key(); break;
|
||||
}
|
||||
hos_bug_capture_work(); /* (a capture asked for, by F8 or by its row: written here) */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* (bug_capture.c: F8 was pressed, not a tenth of a second later) */
|
||||
void hos_save_share_wake(void) {
|
||||
if (share_wake) SetEvent(share_wake);
|
||||
}
|
||||
|
||||
/* nn_fs.c, at the game's first save mount, and a bug capture before it: the rows answer from now on. Once */
|
||||
void hos_save_share_start(void) {
|
||||
if (InterlockedCompareExchange(&share_started, 1, 0)) return;
|
||||
share_wake = CreateEventA(NULL, FALSE, FALSE, NULL);
|
||||
HANDLE t = CreateThread(NULL, 0, share_main, NULL, 0, NULL);
|
||||
if (t) CloseHandle(t);
|
||||
else hos_log("save: the save rows' thread could not be started: Export and Import do nothing in this run");
|
||||
}
|
||||
@@ -292,6 +292,40 @@ static int save_tx_enabled(void) {
|
||||
return !(e && e[0] == '0');
|
||||
}
|
||||
|
||||
/* (Triforce) The save's lock between the game and Setup: <user>\save.lock, opened without sharing at the run's first save
|
||||
mount and held until the process ends (Windows lets go of it however the game ends). Setup's copy of a save (setup/Guard.cs
|
||||
SaveLock, setup/Saves.cs Copy) holds the same file across the whole copy: it found a running game by its process alone, so a
|
||||
game started after that look and before the copy's end mounted half a save and wrote it back over the copy when it closed.
|
||||
A game started while Setup copies waits here for the lock, up to wait_ms. 1: held; 0: another program held it all that time
|
||||
(the save is mounted anyway, as before, and the log says so); -1: it could not be made (nothing is locked: as before).
|
||||
HOS_SAVE_LOCK=0: none taken (2). */
|
||||
#ifndef ERROR_SHARING_VIOLATION /* (host/posix/windows.h has neither: no such refusal there, the lock is always had) */
|
||||
#define ERROR_SHARING_VIOLATION 32L
|
||||
#endif
|
||||
#ifndef ERROR_LOCK_VIOLATION
|
||||
#define ERROR_LOCK_VIOLATION 33L
|
||||
#endif
|
||||
static HANDLE save_tx_lock_handle = INVALID_HANDLE_VALUE;
|
||||
static int save_tx_lock(const char *user, int wait_ms) {
|
||||
const char *e = getenv("HOS_SAVE_LOCK");
|
||||
if (e && e[0] == '0') return 2;
|
||||
if (save_tx_lock_handle != INVALID_HANDLE_VALUE) return 1;
|
||||
char p[HOS_FS_PATH];
|
||||
if (!user || snprintf(p, sizeof p, "%s\\save.lock", user) >= (int)sizeof p) return -1;
|
||||
tx_make_dirs(user);
|
||||
for (int waited = 0;; waited += 50) {
|
||||
const HANDLE h = CreateFileA(p, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (h != INVALID_HANDLE_VALUE) {
|
||||
save_tx_lock_handle = h;
|
||||
return 1;
|
||||
}
|
||||
const DWORD why = GetLastError();
|
||||
if (why != ERROR_SHARING_VIOLATION && why != ERROR_LOCK_VIOLATION) return -1;
|
||||
if (waited >= wait_ms) return 0;
|
||||
Sleep(50);
|
||||
}
|
||||
}
|
||||
|
||||
/* The first mount of the save in a run: the disk put right after a run that died, then the working copy made from the
|
||||
committed save. 1: mount p->work; 0: the working copy could not be made, mount p->save as before. note (may be NULL) gets
|
||||
a line for the log when the recovery found something to do */
|
||||
|
||||
200
host/save_tx_share.h
Normal file
200
host/save_tx_share.h
Normal file
@@ -0,0 +1,200 @@
|
||||
/* (triforce) Sharing a save, the part on the save's own folders: reading the newest save whole for an export or a bug capture,
|
||||
and an imported save that waits for the next start. Included by nn_fs.c after save_tx.h (its helpers and its long-path file
|
||||
functions are used here); tests/test_save_share.py compiles the two alone.
|
||||
|
||||
The newest save is what the next start would load: the highest ready-<n> (a finished commit not in place yet), else the
|
||||
committed save. nn_fs.c reads it with no Commit under way (its tx_lock held, the last Commit's thread ended), so every
|
||||
file comes from one commit: never the working copy the game is writing.
|
||||
|
||||
An import from the System tab is not put in place while the game runs: the game keeps its progress in memory and writes
|
||||
it back at its next save, over files changed under it. Its files are written to <tx>/import-new and that folder renamed
|
||||
to <tx>/import when it is whole; Commits leave both alone. The first mount of the next run, after save_tx_mount put the
|
||||
disk right, copies the save about to be replaced to <backups>/import-<UTC date-time> (a name tx_backups_prune does not
|
||||
take: it stays until the player deletes it), renames import to the next ready-<n>, and mounts again: the import is then
|
||||
a finished commit, put in place as one, and the committed save it replaces becomes one of the usual backups as well. A
|
||||
start that dies in between leaves either the import still waiting (it is applied again, with a second backup) or a
|
||||
finished commit. Triforce Setup's Convert saves deletes <tx> after it replaced the save, and a waiting import with it:
|
||||
what was converted last is the save. */
|
||||
#pragma once
|
||||
|
||||
#define TX_SHARE_FILE_MAX (64u << 20) /* (a save file is some 100 KB: more than this is not one) */
|
||||
|
||||
static void tx_share_import_paths(const SaveTxPaths *p, char *waiting, char *writing, size_t cap) {
|
||||
snprintf(waiting, cap, "%s\\import", p->tx);
|
||||
snprintf(writing, cap, "%s\\import-new", p->tx);
|
||||
}
|
||||
|
||||
/* 1: the folder holds a file only this game's save has (account.sav, option.sav, slot_00/progress.sav: what Triforce
|
||||
Setup takes for a save too) */
|
||||
static int tx_share_is_save(const char *dir) {
|
||||
static const char *const files[] = {"account.sav", "option.sav", "slot_00\\progress.sav"};
|
||||
for (size_t i = 0; i < sizeof files / sizeof *files; i++) {
|
||||
char path[HOS_FS_PATH];
|
||||
if (snprintf(path, sizeof path, "%s\\%s", dir, files[i]) < (int)sizeof path && tx_exists(path) && !tx_is_dir(path)) return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The save the next start loads, in out. 0: there is none yet */
|
||||
static int tx_share_newest(const SaveTxPaths *p, char *out, size_t cap) {
|
||||
if (tx_ready_highest(p, out, cap) && tx_is_dir(out) && !tx_empty(out)) return 1;
|
||||
snprintf(out, cap, "%s", p->save);
|
||||
return tx_is_dir(out) && !tx_empty(out);
|
||||
}
|
||||
|
||||
/* A file whole: malloc'd, *n its size. NULL when it cannot be read or is no save file's size */
|
||||
static void *tx_share_read_file(const char *path, size_t *n) {
|
||||
HANDLE in = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (in == INVALID_HANDLE_VALUE) return NULL;
|
||||
LARGE_INTEGER size;
|
||||
size.QuadPart = 0;
|
||||
char *data = NULL;
|
||||
if (GetFileSizeEx(in, &size) && size.QuadPart >= 0 && size.QuadPart <= (LONGLONG)TX_SHARE_FILE_MAX)
|
||||
data = (char *)malloc((size_t)size.QuadPart + 1);
|
||||
size_t done = 0;
|
||||
while (data && done < (size_t)size.QuadPart) {
|
||||
DWORD got = 0;
|
||||
const size_t left = (size_t)size.QuadPart - done;
|
||||
if (!ReadFile(in, data + done, (DWORD)(left > (1u << 20) ? (1u << 20) : left), &got, NULL) || !got) break;
|
||||
done += got;
|
||||
}
|
||||
CloseHandle(in);
|
||||
if (data && done != (size_t)size.QuadPart) free(data), data = NULL; /* (shorter than it said: not read) */
|
||||
if (data) *n = done;
|
||||
return data;
|
||||
}
|
||||
|
||||
/* Every file below dir given to fn whole, with its path below dir (rel: "" at the top; / between the parts). Links are
|
||||
left out, as tx_copy_tree leaves them. The files given, or -1: one could not be read, or fn answered 0 */
|
||||
static int tx_share_read(const char *dir, const char *rel, int depth, int (*fn)(void *arg, const char *rel, const void *data, size_t n),
|
||||
void *arg) {
|
||||
char pat[HOS_FS_PATH];
|
||||
WIN32_FIND_DATAA fd;
|
||||
snprintf(pat, sizeof pat, "%s\\*", dir);
|
||||
HANDLE f = FindFirstFileA(pat, &fd);
|
||||
if (f == INVALID_HANDLE_VALUE) return tx_is_dir(dir) ? 0 : -1;
|
||||
int files = 0;
|
||||
do {
|
||||
if (!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, "..") || (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) continue;
|
||||
char sub[HOS_FS_PATH], name[HOS_FS_PATH];
|
||||
if (snprintf(sub, sizeof sub, "%s\\%s", dir, fd.cFileName) >= (int)sizeof sub ||
|
||||
snprintf(name, sizeof name, "%s%s%s", rel, rel[0] ? "/" : "", fd.cFileName) >= (int)sizeof name) {
|
||||
files = -1;
|
||||
break;
|
||||
}
|
||||
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
const int below = depth < 4 ? tx_share_read(sub, name, depth + 1, fn, arg) : -1;
|
||||
files = below < 0 ? -1 : files + below;
|
||||
} else {
|
||||
size_t n = 0;
|
||||
void *data = tx_share_read_file(sub, &n);
|
||||
const int taken = data && fn(arg, name, data, n);
|
||||
free(data);
|
||||
files = taken ? files + 1 : -1;
|
||||
}
|
||||
} while (files >= 0 && FindNextFileA(f, &fd));
|
||||
FindClose(f);
|
||||
return files;
|
||||
}
|
||||
|
||||
/* One file of an import written out to the disk, its folders made. 1: written */
|
||||
static int tx_share_write_file(const char *dir, const char *rel, const void *data, size_t n) {
|
||||
char path[HOS_FS_PATH];
|
||||
if (snprintf(path, sizeof path, "%s\\%s", dir, rel) >= (int)sizeof path) return 0;
|
||||
char *last = NULL;
|
||||
for (char *q = path + strlen(dir); *q; q++)
|
||||
if (*q == '/' || *q == '\\') *q = '\\', last = q;
|
||||
if (last && last > path + strlen(dir)) { /* (a slot's folder) */
|
||||
*last = 0;
|
||||
tx_make_dirs(path);
|
||||
*last = '\\';
|
||||
}
|
||||
HANDLE out = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
||||
if (out == INVALID_HANDLE_VALUE) return 0;
|
||||
int ok = 1;
|
||||
for (size_t done = 0; ok && done < n;) {
|
||||
DWORD put = 0;
|
||||
const size_t left = n - done;
|
||||
if (!WriteFile(out, (const char *)data + done, (DWORD)(left > (1u << 20) ? (1u << 20) : left), &put, NULL) || !put) ok = 0;
|
||||
done += put;
|
||||
}
|
||||
if (ok && !FlushFileBuffers(out)) ok = 0;
|
||||
CloseHandle(out);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int tx_share_import_waits(const SaveTxPaths *p) {
|
||||
char waiting[HOS_FS_PATH], writing[HOS_FS_PATH];
|
||||
tx_share_import_paths(p, waiting, writing, sizeof waiting);
|
||||
return tx_is_dir(waiting);
|
||||
}
|
||||
|
||||
/* Drops a waiting import. 1: none waits any more */
|
||||
static int tx_share_import_cancel(const SaveTxPaths *p) {
|
||||
char waiting[HOS_FS_PATH], writing[HOS_FS_PATH];
|
||||
tx_share_import_paths(p, waiting, writing, sizeof waiting);
|
||||
if (tx_exists(writing)) tx_delete_tree(writing);
|
||||
if (tx_exists(waiting)) tx_delete_tree(waiting);
|
||||
return !tx_exists(waiting);
|
||||
}
|
||||
|
||||
/* The n files of a checked import (rels: their paths below the save, / between the parts) written to wait for the next
|
||||
start, in place of one that waited. 1: it waits whole; 0: it could not be written, and what waited before still does */
|
||||
static int tx_share_import_stage(const SaveTxPaths *p, int n, const char *const *rels, const uint8_t *const *data, const size_t *sizes) {
|
||||
char waiting[HOS_FS_PATH], writing[HOS_FS_PATH];
|
||||
tx_share_import_paths(p, waiting, writing, sizeof waiting);
|
||||
if (tx_exists(writing)) tx_delete_tree(writing);
|
||||
tx_make_dirs(writing);
|
||||
int ok = tx_is_dir(writing) && tx_empty(writing);
|
||||
for (int i = 0; ok && i < n; i++) ok = tx_share_write_file(writing, rels[i], data[i], sizes[i]);
|
||||
ok = ok && tx_share_is_save(writing);
|
||||
if (ok && tx_exists(waiting)) { /* (the import before it: replaced by this one) */
|
||||
tx_delete_tree(waiting);
|
||||
ok = !tx_exists(waiting);
|
||||
}
|
||||
if (ok && !tx_move(writing, waiting, 3)) ok = 0;
|
||||
if (!ok) tx_delete_tree(writing);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/* The first mount of a run, after save_tx_mount: a waiting import made the newest save, the save it replaces copied to the
|
||||
backups first (its folder in backup when there was one to keep). 1: done, the import is the highest ready-<n> and
|
||||
save_tx_mount is to run again; 0: none waited; -1: the save could not be backed up or the import not renamed, it waits
|
||||
on and nothing changed; -2: what waited was no save and was dropped */
|
||||
static int tx_share_import_apply(const SaveTxPaths *p, char *backup, size_t cap) {
|
||||
char waiting[HOS_FS_PATH], writing[HOS_FS_PATH], newest[HOS_FS_PATH];
|
||||
if (backup && cap) backup[0] = 0;
|
||||
tx_share_import_paths(p, waiting, writing, sizeof waiting);
|
||||
if (tx_exists(writing)) tx_delete_tree(writing); /* (an import the run before did not finish writing: never whole) */
|
||||
if (!tx_exists(waiting)) return 0;
|
||||
if (!tx_is_dir(waiting) || !tx_share_is_save(waiting)) {
|
||||
tx_delete_tree(waiting);
|
||||
return -2;
|
||||
}
|
||||
if (tx_share_newest(p, newest, sizeof newest)) {
|
||||
struct tm t;
|
||||
const time_t now = tx_now();
|
||||
#ifdef _WIN32
|
||||
gmtime_s(&t, &now);
|
||||
#else
|
||||
gmtime_r(&now, &t);
|
||||
#endif
|
||||
char stamp[64], part[HOS_FS_PATH], to[HOS_FS_PATH];
|
||||
strftime(stamp, sizeof stamp, "import-%Y%m%d-%H%M%S", &t);
|
||||
tx_make_dirs(p->backups);
|
||||
snprintf(part, sizeof part, "%s\\import-new", p->backups);
|
||||
snprintf(to, sizeof to, "%s\\%s", p->backups, stamp);
|
||||
for (int k = 2; tx_exists(to) && k < 100; k++) snprintf(to, sizeof to, "%s\\%s-%d", p->backups, stamp, k);
|
||||
if (tx_exists(part)) tx_delete_tree(part); /* (a backup a start that died did not finish) */
|
||||
if (tx_exists(part) || tx_exists(to) || !tx_copy_tree(newest, part, 1) || tx_crashed() || !tx_move(part, to, 3)) {
|
||||
if (!save_tx_dead) tx_delete_tree(part);
|
||||
return -1;
|
||||
}
|
||||
if (backup) snprintf(backup, cap, "%s", to);
|
||||
}
|
||||
char ready[HOS_FS_PATH];
|
||||
snprintf(ready, sizeof ready, "%s\\ready-%u", p->tx, tx_ready_highest(p, NULL, 0) + 1);
|
||||
if (tx_crashed() || !tx_move(waiting, ready, 3)) return -1;
|
||||
return 1;
|
||||
}
|
||||
673
host/save_zip.c
Normal file
673
host/save_zip.c
Normal file
@@ -0,0 +1,673 @@
|
||||
/* (triforce) A zip file in memory (save_zip.h): the save export's and the bug capture's. Little-endian fields as the zip
|
||||
format (PKWARE APPNOTE) lays them out; deflate as RFC 1951 defines it. After NX2navi's runtime/save_zip.c, its owner's
|
||||
own file; here a member whose name is not plain ASCII is marked as UTF-8 (general purpose bit 11), which is what the
|
||||
names are, and the reader takes a member only when its name before its bytes is its name in the directory. */
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "save_zip.h"
|
||||
|
||||
/* ---- CRC-32 (the zip polynomial, reflected), one table built on first use ---- */
|
||||
static uint32_t crc_table[256];
|
||||
static volatile int crc_ready;
|
||||
|
||||
uint32_t sz_crc32(uint32_t crc, const void *data, size_t n) {
|
||||
if (!crc_ready) { /* two threads may build it at once: they write the same values */
|
||||
for (uint32_t i = 0; i < 256; i++) {
|
||||
uint32_t c = i;
|
||||
for (int k = 0; k < 8; k++) c = c & 1 ? 0xedb88320u ^ (c >> 1) : c >> 1;
|
||||
crc_table[i] = c;
|
||||
}
|
||||
__atomic_store_n(&crc_ready, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
const uint8_t *p = data;
|
||||
crc = ~crc;
|
||||
while (n--) crc = crc_table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
|
||||
return ~crc;
|
||||
}
|
||||
|
||||
/* ---- deflate tables shared by both directions: length codes 257..285 and distance codes 0..29 ---- */
|
||||
static const uint16_t len_base[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27,
|
||||
31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
|
||||
static const uint8_t len_extra[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
|
||||
static const uint16_t dist_base[30] = {1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129,
|
||||
193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
|
||||
static const uint8_t dist_extra[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
|
||||
|
||||
/* ---- inflate ---- */
|
||||
typedef struct {
|
||||
const uint8_t *in;
|
||||
size_t n, pos;
|
||||
uint64_t bitbuf;
|
||||
int bitcnt, bad;
|
||||
uint8_t *out;
|
||||
size_t cap, at;
|
||||
} Inflate;
|
||||
|
||||
/* need (0..16) bits, the first read in the lowest bit; past the end of the input: 0 and bad set. */
|
||||
static unsigned get_bits(Inflate *s, int need) {
|
||||
while (s->bitcnt < need) {
|
||||
if (s->pos >= s->n) {
|
||||
s->bad = 1;
|
||||
return 0;
|
||||
}
|
||||
s->bitbuf |= (uint64_t)s->in[s->pos++] << s->bitcnt;
|
||||
s->bitcnt += 8;
|
||||
}
|
||||
unsigned v = (unsigned)(s->bitbuf & ((1u << need) - 1));
|
||||
s->bitbuf >>= need;
|
||||
s->bitcnt -= need;
|
||||
return v;
|
||||
}
|
||||
|
||||
/* A canonical Huffman code: how many codes of each length, and the symbols in code order. */
|
||||
typedef struct {
|
||||
uint16_t count[16];
|
||||
uint16_t symbol[288];
|
||||
} Huff;
|
||||
|
||||
/* The code of lengths[0..n) (0: the symbol is unused). 0 when usable: no length is over-subscribed (an incomplete
|
||||
code is allowed, as a stream with a single distance code has one; a code it lacks then reads as damaged). */
|
||||
static int huff_build(Huff *h, const uint8_t *lengths, int n) {
|
||||
uint16_t offs[16];
|
||||
memset(h->count, 0, sizeof h->count);
|
||||
for (int i = 0; i < n; i++) h->count[lengths[i]]++;
|
||||
h->count[0] = 0;
|
||||
int left = 1;
|
||||
for (int len = 1; len < 16; len++) {
|
||||
left = (left << 1) - h->count[len];
|
||||
if (left < 0) return -1;
|
||||
}
|
||||
offs[1] = 0;
|
||||
for (int len = 1; len < 15; len++) offs[len + 1] = offs[len] + h->count[len];
|
||||
for (int i = 0; i < n; i++)
|
||||
if (lengths[i]) h->symbol[offs[lengths[i]]++] = (uint16_t)i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The next symbol of code h, the bits read one at a time (Huffman codes are packed from their first bit); -1 damaged. */
|
||||
static int huff_decode(Inflate *s, const Huff *h) {
|
||||
int code = 0, first = 0, index = 0;
|
||||
for (int len = 1; len < 16; len++) {
|
||||
code |= (int)get_bits(s, 1);
|
||||
if (s->bad) return -1;
|
||||
int count = h->count[len];
|
||||
if (code - first < count) return h->symbol[index + (code - first)];
|
||||
index += count;
|
||||
first = (first + count) << 1;
|
||||
code <<= 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* The symbols of one block until its end code. */
|
||||
static int inflate_codes(Inflate *s, const Huff *lit, const Huff *dist) {
|
||||
for (;;) {
|
||||
int sym = huff_decode(s, lit);
|
||||
if (sym < 0) return -1;
|
||||
if (sym < 256) {
|
||||
if (s->at >= s->cap) return -1;
|
||||
s->out[s->at++] = (uint8_t)sym;
|
||||
continue;
|
||||
}
|
||||
if (sym == 256) return 0;
|
||||
sym -= 257;
|
||||
if (sym >= 29) return -1;
|
||||
size_t len = len_base[sym] + get_bits(s, len_extra[sym]);
|
||||
int d = huff_decode(s, dist);
|
||||
if (d < 0 || d >= 30) return -1;
|
||||
size_t back = dist_base[d] + get_bits(s, dist_extra[d]);
|
||||
if (s->bad || back > s->at || len > s->cap - s->at) return -1;
|
||||
for (size_t i = 0; i < len; i++, s->at++) s->out[s->at] = s->out[s->at - back]; /* overlapping on purpose */
|
||||
}
|
||||
}
|
||||
|
||||
static int inflate_stored(Inflate *s) {
|
||||
s->bitbuf = 0, s->bitcnt = 0; /* the rest of the current byte */
|
||||
if (s->n - s->pos < 4) return -1;
|
||||
unsigned len = s->in[s->pos] | s->in[s->pos + 1] << 8, nlen = s->in[s->pos + 2] | s->in[s->pos + 3] << 8;
|
||||
s->pos += 4;
|
||||
if (len != (~nlen & 0xffffu) || len > s->n - s->pos || len > s->cap - s->at) return -1;
|
||||
memcpy(s->out + s->at, s->in + s->pos, len);
|
||||
s->pos += len, s->at += len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int inflate_fixed(Inflate *s) {
|
||||
static Huff lit, dist;
|
||||
static volatile int built;
|
||||
if (!built) { /* two threads may build them at once: they write the same values */
|
||||
uint8_t l[288];
|
||||
int i = 0;
|
||||
for (; i < 144; i++) l[i] = 8;
|
||||
for (; i < 256; i++) l[i] = 9;
|
||||
for (; i < 280; i++) l[i] = 7;
|
||||
for (; i < 288; i++) l[i] = 8;
|
||||
huff_build(&lit, l, 288);
|
||||
for (i = 0; i < 30; i++) l[i] = 5;
|
||||
huff_build(&dist, l, 30);
|
||||
__atomic_store_n(&built, 1, __ATOMIC_RELEASE);
|
||||
}
|
||||
return inflate_codes(s, &lit, &dist);
|
||||
}
|
||||
|
||||
static int inflate_dynamic(Inflate *s) {
|
||||
static const uint8_t order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
||||
uint8_t lengths[320];
|
||||
Huff lit, dist;
|
||||
int nlen = (int)get_bits(s, 5) + 257, ndist = (int)get_bits(s, 5) + 1, ncode = (int)get_bits(s, 4) + 4;
|
||||
if (s->bad || nlen > 286 || ndist > 30) return -1;
|
||||
memset(lengths, 0, sizeof lengths);
|
||||
for (int i = 0; i < ncode; i++) lengths[order[i]] = (uint8_t)get_bits(s, 3);
|
||||
if (s->bad || huff_build(&lit, lengths, 19)) return -1; /* the code lengths' own code, in lit for now */
|
||||
for (int i = 0; i < nlen + ndist;) {
|
||||
int sym = huff_decode(s, &lit);
|
||||
if (sym < 0) return -1;
|
||||
if (sym < 16) {
|
||||
lengths[i++] = (uint8_t)sym;
|
||||
continue;
|
||||
}
|
||||
int repeat, value = 0;
|
||||
if (sym == 16) {
|
||||
if (!i) return -1;
|
||||
value = lengths[i - 1];
|
||||
repeat = 3 + (int)get_bits(s, 2);
|
||||
} else if (sym == 17) {
|
||||
repeat = 3 + (int)get_bits(s, 3);
|
||||
} else {
|
||||
repeat = 11 + (int)get_bits(s, 7);
|
||||
}
|
||||
if (s->bad || i + repeat > nlen + ndist) return -1;
|
||||
while (repeat--) lengths[i++] = (uint8_t)value;
|
||||
}
|
||||
if (!lengths[256]) return -1; /* a block without its end code */
|
||||
if (huff_build(&lit, lengths, nlen) || huff_build(&dist, lengths + nlen, ndist)) return -1;
|
||||
return inflate_codes(s, &lit, &dist);
|
||||
}
|
||||
|
||||
int sz_inflate(const uint8_t *src, size_t n, uint8_t *dst, size_t dst_n) {
|
||||
Inflate s = {.in = src, .n = n, .out = dst, .cap = dst_n};
|
||||
for (int last = 0; !last;) {
|
||||
last = (int)get_bits(&s, 1);
|
||||
int type = (int)get_bits(&s, 2), r;
|
||||
if (s.bad) return -1;
|
||||
if (type == 0) r = inflate_stored(&s);
|
||||
else if (type == 1) r = inflate_fixed(&s);
|
||||
else if (type == 2) r = inflate_dynamic(&s);
|
||||
else r = -1;
|
||||
if (r || s.bad) return -1;
|
||||
}
|
||||
return s.at == dst_n ? 0 : -1;
|
||||
}
|
||||
|
||||
/* ---- deflate: greedy LZ77 over a 32 KiB window (hash chains of 3-byte keys), the fixed Huffman codes ---- */
|
||||
typedef struct {
|
||||
uint8_t *out;
|
||||
size_t at, cap;
|
||||
uint64_t bitbuf;
|
||||
int bitcnt;
|
||||
} BitOut;
|
||||
|
||||
static void put_bits(BitOut *b, uint32_t v, int n) {
|
||||
b->bitbuf |= (uint64_t)v << b->bitcnt;
|
||||
b->bitcnt += n;
|
||||
while (b->bitcnt >= 8) {
|
||||
if (b->at < b->cap) b->out[b->at] = (uint8_t)b->bitbuf;
|
||||
b->at++;
|
||||
b->bitbuf >>= 8;
|
||||
b->bitcnt -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* A Huffman code goes out from its first bit: reversed into the LSB-first stream. */
|
||||
static void put_code(BitOut *b, uint32_t code, int len) {
|
||||
uint32_t r = 0;
|
||||
for (int i = 0; i < len; i++) r |= ((code >> i) & 1u) << (len - 1 - i);
|
||||
put_bits(b, r, len);
|
||||
}
|
||||
|
||||
static void put_literal(BitOut *b, int sym) { /* a literal/length symbol in the fixed code */
|
||||
if (sym < 144) put_code(b, 0x30u + (uint32_t)sym, 8);
|
||||
else if (sym < 256) put_code(b, 0x190u + (uint32_t)(sym - 144), 9);
|
||||
else if (sym < 280) put_code(b, (uint32_t)(sym - 256), 7);
|
||||
else put_code(b, 0xc0u + (uint32_t)(sym - 280), 8);
|
||||
}
|
||||
|
||||
static void put_match(BitOut *b, size_t len, size_t back) {
|
||||
int i = 28;
|
||||
while (len_base[i] > len) i--;
|
||||
put_literal(b, 257 + i);
|
||||
put_bits(b, (uint32_t)(len - len_base[i]), len_extra[i]);
|
||||
int d = 29;
|
||||
while (dist_base[d] > back) d--;
|
||||
put_code(b, (uint32_t)d, 5);
|
||||
put_bits(b, (uint32_t)(back - dist_base[d]), dist_extra[d]);
|
||||
}
|
||||
|
||||
#define WINDOW 32768u
|
||||
#define HASH_BITS 15
|
||||
#define CHAIN 48 /* candidates tried per position: logs repeat a lot, saves little */
|
||||
#define MAX_MATCH 258
|
||||
|
||||
uint8_t *sz_deflate(const uint8_t *src, size_t n, size_t *out_n) {
|
||||
BitOut b = {.cap = n + n / 8 + 64}; /* a literal takes at most 9 bits */
|
||||
b.out = malloc(b.cap);
|
||||
int32_t *head = malloc(sizeof(int32_t) << HASH_BITS), *prev = malloc(sizeof(int32_t) * WINDOW);
|
||||
if (!b.out || !head || !prev) {
|
||||
free(b.out), free(head), free(prev);
|
||||
return NULL;
|
||||
}
|
||||
for (size_t i = 0; i < (1u << HASH_BITS); i++) head[i] = -1;
|
||||
put_bits(&b, 1, 1); /* the last block */
|
||||
put_bits(&b, 1, 2); /* fixed Huffman codes */
|
||||
#define HASH(p) ((((uint32_t)(p)[0] << 10) ^ ((uint32_t)(p)[1] << 5) ^ (p)[2]) & ((1u << HASH_BITS) - 1))
|
||||
size_t i = 0;
|
||||
while (i < n) {
|
||||
size_t best = 0, back = 0;
|
||||
if (n - i >= 3) {
|
||||
uint32_t h = HASH(src + i);
|
||||
int32_t cand = head[h];
|
||||
size_t limit = n - i < MAX_MATCH ? n - i : MAX_MATCH;
|
||||
for (int tries = 0; cand >= 0 && i - (size_t)cand <= WINDOW && tries < CHAIN; tries++) {
|
||||
const uint8_t *a = src + cand, *c = src + i;
|
||||
if (a[best] == c[best]) { /* only a longer match can win */
|
||||
size_t k = 0;
|
||||
while (k < limit && a[k] == c[k]) k++;
|
||||
if (k > best) {
|
||||
best = k, back = i - (size_t)cand;
|
||||
if (k == limit) break;
|
||||
}
|
||||
}
|
||||
int32_t next = prev[cand & (WINDOW - 1)];
|
||||
if (next >= cand) break; /* an older entry overwritten by a newer one */
|
||||
cand = next;
|
||||
}
|
||||
}
|
||||
size_t step = best >= 3 ? best : 1;
|
||||
if (best >= 3) put_match(&b, best, back);
|
||||
else put_literal(&b, src[i]);
|
||||
for (size_t k = 0; k < step; k++, i++) /* every position covered goes into the chains */
|
||||
if (n - i >= 3) {
|
||||
uint32_t h = HASH(src + i);
|
||||
prev[i & (WINDOW - 1)] = head[h];
|
||||
head[h] = (int32_t)i;
|
||||
}
|
||||
}
|
||||
#undef HASH
|
||||
put_literal(&b, 256);
|
||||
put_bits(&b, 0, 7); /* out to the byte */
|
||||
free(head), free(prev);
|
||||
if (b.at > b.cap) { /* cannot happen with the bound above; kept as the guard put_bits relies on */
|
||||
free(b.out);
|
||||
return NULL;
|
||||
}
|
||||
*out_n = b.at;
|
||||
return b.out;
|
||||
}
|
||||
|
||||
/* ---- writing a zip ---- */
|
||||
static void put16(uint8_t *p, unsigned v) { p[0] = (uint8_t)v, p[1] = (uint8_t)(v >> 8); }
|
||||
static void put32(uint8_t *p, uint32_t v) { put16(p, v & 0xffff), put16(p + 2, v >> 16); }
|
||||
static unsigned get16(const uint8_t *p) { return p[0] | p[1] << 8; }
|
||||
static uint32_t get32(const uint8_t *p) { return get16(p) | (uint32_t)get16(p + 2) << 16; }
|
||||
|
||||
static uint8_t *room(SzWriter *w, size_t k) {
|
||||
if (w->failed) return NULL;
|
||||
if (w->n + k > 0xffffffffu) { /* offsets and sizes are 32-bit without the 64-bit extension */
|
||||
w->failed = 1;
|
||||
return NULL;
|
||||
}
|
||||
if (w->n + k > w->cap) {
|
||||
size_t cap = w->cap ? w->cap : 1 << 16;
|
||||
while (cap < w->n + k) cap *= 2;
|
||||
uint8_t *nb = realloc(w->buf, cap);
|
||||
if (!nb) {
|
||||
w->failed = 1;
|
||||
return NULL;
|
||||
}
|
||||
w->buf = nb, w->cap = cap;
|
||||
}
|
||||
uint8_t *p = w->buf + w->n;
|
||||
w->n += k;
|
||||
return p;
|
||||
}
|
||||
|
||||
void sz_writer_init(SzWriter *w, int year, int month, int day, int hour, int minute, int second) {
|
||||
memset(w, 0, sizeof *w);
|
||||
if (year < 1980) year = 1980, month = 1, day = 1, hour = minute = second = 0;
|
||||
w->dos_time = (uint16_t)(hour << 11 | minute << 5 | second / 2);
|
||||
w->dos_date = (uint16_t)((year - 1980) << 9 | month << 5 | day);
|
||||
}
|
||||
|
||||
void sz_writer_add(SzWriter *w, const char *name, const void *data, size_t n) {
|
||||
if (w->failed) return;
|
||||
size_t name_n = strlen(name);
|
||||
if (n > 0xffffffffu || name_n >= 0xffff) {
|
||||
w->failed = 1;
|
||||
return;
|
||||
}
|
||||
if (w->count == w->cap_m) {
|
||||
int cap = w->cap_m ? w->cap_m * 2 : 32;
|
||||
SzMember *m = realloc(w->m, (size_t)cap * sizeof *m);
|
||||
if (!m) {
|
||||
w->failed = 1;
|
||||
return;
|
||||
}
|
||||
w->m = m, w->cap_m = cap;
|
||||
}
|
||||
SzMember *m = &w->m[w->count];
|
||||
m->name = malloc(name_n + 1);
|
||||
if (!m->name) {
|
||||
w->failed = 1;
|
||||
return;
|
||||
}
|
||||
memcpy(m->name, name, name_n + 1);
|
||||
w->count++;
|
||||
m->crc = sz_crc32(0, data, n);
|
||||
m->usize = (uint32_t)n;
|
||||
size_t packed_n = 0;
|
||||
uint8_t *packed = n >= 64 ? sz_deflate(data, n, &packed_n) : NULL;
|
||||
if (packed && packed_n >= n) free(packed), packed = NULL; /* stored when deflate does not help */
|
||||
m->method = packed ? 8 : 0;
|
||||
m->flags = 0;
|
||||
for (size_t i = 0; i < name_n; i++)
|
||||
if ((unsigned char)name[i] >= 0x80) m->flags = 0x0800; /* (bit 11: the name is UTF-8, not code page 437) */
|
||||
m->csize = (uint32_t)(packed ? packed_n : n);
|
||||
m->offset = (uint32_t)w->n;
|
||||
uint8_t *h = room(w, 30 + name_n);
|
||||
if (h) {
|
||||
put32(h, 0x04034b50);
|
||||
put16(h + 4, 20); /* version needed: deflate */
|
||||
put16(h + 6, m->flags);
|
||||
put16(h + 8, m->method);
|
||||
put16(h + 10, w->dos_time);
|
||||
put16(h + 12, w->dos_date);
|
||||
put32(h + 14, m->crc);
|
||||
put32(h + 18, m->csize);
|
||||
put32(h + 22, m->usize);
|
||||
put16(h + 26, (unsigned)name_n);
|
||||
put16(h + 28, 0);
|
||||
memcpy(h + 30, name, name_n);
|
||||
}
|
||||
uint8_t *d = room(w, m->csize);
|
||||
if (d && m->csize) memcpy(d, packed ? packed : data, m->csize);
|
||||
free(packed);
|
||||
}
|
||||
|
||||
uint8_t *sz_writer_finish(SzWriter *w, size_t *n) {
|
||||
size_t cd_at = w->n;
|
||||
for (int i = 0; i < w->count && !w->failed; i++) {
|
||||
SzMember *m = &w->m[i];
|
||||
size_t name_n = strlen(m->name);
|
||||
uint8_t *c = room(w, 46 + name_n);
|
||||
if (!c) break;
|
||||
put32(c, 0x02014b50);
|
||||
put16(c + 4, 20); /* made by: MS-DOS attributes, version 2.0 */
|
||||
put16(c + 6, 20);
|
||||
put16(c + 8, m->flags);
|
||||
put16(c + 10, m->method);
|
||||
put16(c + 12, w->dos_time);
|
||||
put16(c + 14, w->dos_date);
|
||||
put32(c + 16, m->crc);
|
||||
put32(c + 20, m->csize);
|
||||
put32(c + 24, m->usize);
|
||||
put16(c + 28, (unsigned)name_n);
|
||||
put16(c + 30, 0); /* extra */
|
||||
put16(c + 32, 0); /* comment */
|
||||
put16(c + 34, 0); /* disk */
|
||||
put16(c + 36, 0); /* internal attributes */
|
||||
put32(c + 38, 0); /* external attributes */
|
||||
put32(c + 42, m->offset);
|
||||
memcpy(c + 46, m->name, name_n);
|
||||
}
|
||||
size_t cd_n = w->n - cd_at;
|
||||
uint8_t *e = w->count < 0xffff ? room(w, 22) : (w->failed = 1, NULL);
|
||||
if (e) {
|
||||
put32(e, 0x06054b50);
|
||||
put16(e + 4, 0);
|
||||
put16(e + 6, 0);
|
||||
put16(e + 8, (unsigned)w->count);
|
||||
put16(e + 10, (unsigned)w->count);
|
||||
put32(e + 12, (uint32_t)cd_n);
|
||||
put32(e + 16, (uint32_t)cd_at);
|
||||
put16(e + 20, 0);
|
||||
}
|
||||
for (int i = 0; i < w->count; i++) free(w->m[i].name);
|
||||
free(w->m);
|
||||
uint8_t *out = w->failed ? NULL : w->buf;
|
||||
if (w->failed) free(w->buf);
|
||||
*n = w->failed ? 0 : w->n;
|
||||
memset(w, 0, sizeof *w);
|
||||
return out;
|
||||
}
|
||||
|
||||
/* ---- reading a zip ---- */
|
||||
int sz_read_dir(const uint8_t *zip, size_t n, SzEntry **out, int *count) {
|
||||
*out = NULL, *count = 0;
|
||||
if (n < 22) return SZ_E_NOT_ZIP;
|
||||
/* the end record: its signature within the last 22 bytes plus the longest comment */
|
||||
size_t e = n - 22, stop = n > 22 + 0xffff ? n - 22 - 0xffff : 0;
|
||||
while (get32(zip + e) != 0x06054b50 || 22 + get16(zip + e + 20) != n - e) {
|
||||
if (e == stop) return SZ_E_NOT_ZIP;
|
||||
e--;
|
||||
}
|
||||
unsigned total = get16(zip + e + 10);
|
||||
uint32_t cd_n = get32(zip + e + 12), cd_at = get32(zip + e + 16);
|
||||
if (get16(zip + e + 4) || get16(zip + e + 6) || get16(zip + e + 8) != total) return SZ_E_UNSUPPORTED; /* spanned */
|
||||
if (total == 0xffff || cd_n == 0xffffffffu || cd_at == 0xffffffffu) return SZ_E_UNSUPPORTED; /* the 64-bit zip */
|
||||
if (cd_at > e || cd_n > e - cd_at) return SZ_E_DAMAGED;
|
||||
SzEntry *list = calloc(total ? total : 1, sizeof *list);
|
||||
if (!list) return SZ_E_MEMORY;
|
||||
size_t p = cd_at;
|
||||
int r = SZ_OK;
|
||||
for (unsigned i = 0; i < total && r == SZ_OK; i++) {
|
||||
if (p + 46 > cd_at + cd_n || get32(zip + p) != 0x02014b50) {
|
||||
r = SZ_E_DAMAGED;
|
||||
break;
|
||||
}
|
||||
const uint8_t *c = zip + p;
|
||||
unsigned made_by = get16(c + 4), flags = get16(c + 8), name_n = get16(c + 28);
|
||||
size_t next = p + 46 + name_n + get16(c + 30) + get16(c + 32);
|
||||
uint32_t ext = get32(c + 38), local = get32(c + 42);
|
||||
SzEntry *en = &list[i];
|
||||
en->method = (uint16_t)get16(c + 10);
|
||||
en->crc = get32(c + 16);
|
||||
en->csize = get32(c + 20);
|
||||
en->usize = get32(c + 24);
|
||||
if (next > cd_at + cd_n) r = SZ_E_DAMAGED;
|
||||
else if (flags & 1 || (en->method != 0 && en->method != 8) || name_n >= sizeof en->name ||
|
||||
en->csize == 0xffffffffu || en->usize == 0xffffffffu || local == 0xffffffffu)
|
||||
r = SZ_E_UNSUPPORTED; /* encrypted, another method, a long name, a 64-bit member */
|
||||
if (r) break;
|
||||
memcpy(en->name, c + 46, name_n);
|
||||
en->name[name_n] = 0;
|
||||
if (memchr(en->name, 0, name_n)) {
|
||||
r = SZ_E_DAMAGED;
|
||||
break;
|
||||
}
|
||||
en->is_dir = name_n && en->name[name_n - 1] == '/';
|
||||
en->is_link = (made_by >> 8) == 3 && ((ext >> 16) & 0170000) == 0120000; /* Unix: S_IFLNK */
|
||||
if (local > cd_at || cd_at - local < 30 || get32(zip + local) != 0x04034b50) {
|
||||
r = SZ_E_DAMAGED;
|
||||
break;
|
||||
}
|
||||
uint64_t data = (uint64_t)local + 30 + get16(zip + local + 26) + get16(zip + local + 28);
|
||||
if (data > cd_at || en->csize > cd_at - data) {
|
||||
r = SZ_E_DAMAGED;
|
||||
break;
|
||||
}
|
||||
/* (triforce) the name is in the zip twice, before the member's bytes and in the directory, and nothing else guards
|
||||
either copy: one that differs from the other is damage (Python's zipfile refuses such a zip too) */
|
||||
if (get16(zip + local + 26) != name_n || memcmp(zip + local + 30, c + 46, name_n)) {
|
||||
r = SZ_E_DAMAGED;
|
||||
break;
|
||||
}
|
||||
en->data = data;
|
||||
p = next;
|
||||
}
|
||||
if (r) {
|
||||
free(list);
|
||||
return r;
|
||||
}
|
||||
*out = list, *count = (int)total;
|
||||
return SZ_OK;
|
||||
}
|
||||
|
||||
uint8_t *sz_extract(const uint8_t *zip, size_t n, const SzEntry *e, int *err) {
|
||||
if (e->data > n || e->csize > n - e->data || e->usize > (uint64_t)SIZE_MAX - 1) {
|
||||
*err = SZ_E_DAMAGED;
|
||||
return NULL;
|
||||
}
|
||||
uint8_t *out = malloc((size_t)e->usize + 1);
|
||||
if (!out) {
|
||||
*err = SZ_E_MEMORY;
|
||||
return NULL;
|
||||
}
|
||||
const uint8_t *src = zip + e->data;
|
||||
int bad = e->method == 0 ? e->csize != e->usize : sz_inflate(src, (size_t)e->csize, out, (size_t)e->usize) != 0;
|
||||
if (!bad && e->method == 0) memcpy(out, src, (size_t)e->usize);
|
||||
if (!bad && sz_crc32(0, out, (size_t)e->usize) != e->crc) bad = 1;
|
||||
if (bad) {
|
||||
free(out);
|
||||
*err = SZ_E_DAMAGED;
|
||||
return NULL;
|
||||
}
|
||||
out[e->usize] = 0;
|
||||
*err = SZ_OK;
|
||||
return out;
|
||||
}
|
||||
|
||||
/* ---- the manifest's JSON ---- */
|
||||
static const char *ws(const char *p) {
|
||||
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') p++;
|
||||
return p;
|
||||
}
|
||||
|
||||
static int hex4(const char *p) {
|
||||
int v = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
char c = p[i];
|
||||
int d = c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1;
|
||||
if (d < 0) return -1;
|
||||
v = v << 4 | d;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/* The string at p (its opening quote) into out (NULL: skipped); *cut set when it did not fit. The byte after its
|
||||
closing quote, NULL when it is not a string. */
|
||||
static const char *json_string(const char *p, char *out, size_t cap, int *cut) {
|
||||
if (*p++ != '"') return NULL;
|
||||
size_t k = 0;
|
||||
for (;;) {
|
||||
char c = *p++;
|
||||
if (!c || (unsigned char)c < 0x20) return NULL;
|
||||
if (c == '"') break;
|
||||
if (c == '\\') {
|
||||
char esc = *p++;
|
||||
switch (esc) {
|
||||
case '"': case '\\': case '/': c = esc; break;
|
||||
case 'b': c = '\b'; break;
|
||||
case 'f': c = '\f'; break;
|
||||
case 'n': c = '\n'; break;
|
||||
case 'r': c = '\r'; break;
|
||||
case 't': c = '\t'; break;
|
||||
case 'u': {
|
||||
int v = hex4(p);
|
||||
if (v < 0) return NULL;
|
||||
p += 4;
|
||||
c = v > 0 && v < 0x80 ? (char)v : '?';
|
||||
break;
|
||||
}
|
||||
default: return NULL;
|
||||
}
|
||||
}
|
||||
if (out && k + 1 < cap) out[k++] = c;
|
||||
else if (out && cut) *cut = 1;
|
||||
}
|
||||
if (out && cap) out[k] = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
/* Past one value: a string, an object or array (nested), or a number or literal. */
|
||||
static const char *json_skip(const char *p) {
|
||||
if (*p == '"') return json_string(p, NULL, 0, NULL);
|
||||
if (*p == '{' || *p == '[') {
|
||||
int depth = 0;
|
||||
while (*p) {
|
||||
if (*p == '"') {
|
||||
p = json_string(p, NULL, 0, NULL);
|
||||
if (!p) return NULL;
|
||||
continue;
|
||||
}
|
||||
if (*p == '{' || *p == '[') depth++;
|
||||
else if ((*p == '}' || *p == ']') && !--depth) return p + 1;
|
||||
p++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
const char *s = p;
|
||||
while (*p && *p != ',' && *p != '}' && *p != ']' && *p != ' ' && *p != '\t' && *p != '\r' && *p != '\n') p++;
|
||||
return p > s ? p : NULL;
|
||||
}
|
||||
|
||||
/* The value of key at the top level of the object json, NULL when it has none. */
|
||||
static const char *json_find(const char *json, const char *key) {
|
||||
const char *p = ws(json);
|
||||
if (*p++ != '{') return NULL;
|
||||
for (;;) {
|
||||
p = ws(p);
|
||||
char k[128];
|
||||
int cut = 0;
|
||||
p = json_string(p, k, sizeof k, &cut);
|
||||
if (!p) return NULL;
|
||||
p = ws(p);
|
||||
if (*p++ != ':') return NULL;
|
||||
p = ws(p);
|
||||
if (!cut && !strcmp(k, key)) return p;
|
||||
p = json_skip(p);
|
||||
if (!p) return NULL;
|
||||
p = ws(p);
|
||||
if (*p++ != ',') return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
int sz_json_str(const char *json, const char *key, char *out, size_t cap) {
|
||||
const char *v = json_find(json, key);
|
||||
return v && *v == '"' && json_string(v, out, cap, NULL) != NULL;
|
||||
}
|
||||
|
||||
int sz_json_int(const char *json, const char *key, long long *out) {
|
||||
const char *v = json_find(json, key);
|
||||
if (!v) return 0;
|
||||
int neg = *v == '-';
|
||||
v += neg;
|
||||
if (*v < '0' || *v > '9') return 0;
|
||||
long long r = 0;
|
||||
for (; *v >= '0' && *v <= '9'; v++) {
|
||||
if (r > (0x7fffffffffffffffll - 9) / 10) return 0;
|
||||
r = r * 10 + (*v - '0');
|
||||
}
|
||||
if (*v == '.' || *v == 'e' || *v == 'E') return 0;
|
||||
*out = neg ? -r : r;
|
||||
return 1;
|
||||
}
|
||||
|
||||
char *sz_json_escape(const char *s, char *out, size_t cap) {
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
size_t k = 0;
|
||||
for (; *s; s++) {
|
||||
unsigned char c = (unsigned char)*s;
|
||||
char tmp[7];
|
||||
size_t m = 0;
|
||||
if (c == '"' || c == '\\') tmp[m++] = '\\', tmp[m++] = (char)c;
|
||||
else if (c < 0x20) tmp[m++] = '\\', tmp[m++] = 'u', tmp[m++] = '0', tmp[m++] = '0', tmp[m++] = hex[c >> 4],
|
||||
tmp[m++] = hex[c & 15];
|
||||
else tmp[m++] = (char)c;
|
||||
if (k + m >= cap) break;
|
||||
memcpy(out + k, tmp, m);
|
||||
k += m;
|
||||
}
|
||||
if (cap) out[k] = 0;
|
||||
return out;
|
||||
}
|
||||
71
host/save_zip.h
Normal file
71
host/save_zip.h
Normal file
@@ -0,0 +1,71 @@
|
||||
/* (triforce) A zip file in memory, for the save export and the bug capture (save_pack.c, save_share.c, bug_capture.c): members
|
||||
stored or deflated, read back with their CRC-32 checked, raw deflate both ways, and the top-level keys of a small JSON
|
||||
object (the export's manifest). After NX2navi's runtime/save_zip.c, which its owner wrote; member names are marked as
|
||||
UTF-8 here when they are not plain ASCII. Plain C on the C library alone: tests/test_save_zip.py builds it by itself and
|
||||
checks it against Python's zipfile and zlib. */
|
||||
#pragma once
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32_t sz_crc32(uint32_t crc, const void *data, size_t n); /* the zip CRC-32; crc 0 to start, the last result to go on */
|
||||
|
||||
/* Raw deflate (RFC 1951) of src[0..n): one block of the fixed Huffman codes over LZ77 matches. A malloc'd buffer of
|
||||
*out_n bytes, NULL when out of memory. */
|
||||
uint8_t *sz_deflate(const uint8_t *src, size_t n, size_t *out_n);
|
||||
/* Raw deflate data src[0..n) into exactly dst_n bytes at dst: stored, fixed and dynamic blocks. 0 on success, -1 when
|
||||
the data is damaged, ends early or holds another number of bytes. */
|
||||
int sz_inflate(const uint8_t *src, size_t n, uint8_t *dst, size_t dst_n);
|
||||
|
||||
/* ---- writing: the members go into one buffer as they come, then the central directory after them ---- */
|
||||
typedef struct {
|
||||
char *name;
|
||||
uint32_t crc, csize, usize, offset;
|
||||
uint16_t method, flags;
|
||||
} SzMember;
|
||||
typedef struct {
|
||||
uint8_t *buf;
|
||||
size_t n, cap;
|
||||
SzMember *m;
|
||||
int count, cap_m;
|
||||
int failed; /* out of memory, or past the 4 GiB a zip without its 64-bit extension holds */
|
||||
uint16_t dos_time, dos_date;
|
||||
} SzWriter;
|
||||
/* Every member gets this local time (the zip's DOS time stamp, two-second steps). */
|
||||
void sz_writer_init(SzWriter *w, int year, int month, int day, int hour, int minute, int second);
|
||||
/* A member: name relative with '/' separators, UTF-8; its bytes deflated when that makes them smaller, else stored. */
|
||||
void sz_writer_add(SzWriter *w, const char *name, const void *data, size_t n);
|
||||
/* The whole archive (*n bytes; the caller frees it), NULL when any add failed. Frees the writer's own memory either way. */
|
||||
uint8_t *sz_writer_finish(SzWriter *w, size_t *n);
|
||||
|
||||
/* ---- reading ---- */
|
||||
enum { SZ_OK, SZ_E_NOT_ZIP, SZ_E_DAMAGED, SZ_E_UNSUPPORTED, SZ_E_MEMORY };
|
||||
typedef struct {
|
||||
char name[256]; /* as stored: '/' separators */
|
||||
uint32_t crc;
|
||||
uint64_t csize, usize, data; /* data: the offset of its bytes in the zip */
|
||||
uint16_t method;
|
||||
int is_dir, is_link; /* a folder entry (name ends in '/'); a symbolic link (a Unix mode in its attributes) */
|
||||
} SzEntry;
|
||||
/* The members of the zip zip[0..n) from its central directory: a malloc'd array in *out (the caller frees it) and its
|
||||
length in *count. SZ_OK or why not: not a zip, damaged, or what this reader leaves out (encryption, a 64-bit zip, a
|
||||
method but stored and deflate, a name of 256 bytes or more). */
|
||||
int sz_read_dir(const uint8_t *zip, size_t n, SzEntry **out, int *count);
|
||||
/* The bytes of member e, checked against its CRC-32: malloc'd with a NUL after them (the caller frees them), NULL with
|
||||
*err set to the SZ_E_* reason. */
|
||||
uint8_t *sz_extract(const uint8_t *zip, size_t n, const SzEntry *e, int *err);
|
||||
|
||||
/* ---- the manifest: a JSON object whose values the reader needs are strings and integers at its top level ---- */
|
||||
/* The string value of key into out (cut to cap - 1 bytes; \u escapes outside ASCII become '?'). 1 when found. */
|
||||
int sz_json_str(const char *json, const char *key, char *out, size_t cap);
|
||||
/* The integer value of key. 1 when found and an integer. */
|
||||
int sz_json_int(const char *json, const char *key, long long *out);
|
||||
/* s as the inside of a JSON string ('"', '\' and control characters escaped) into out, cut to fit cap. Returns out. */
|
||||
char *sz_json_escape(const char *s, char *out, size_t cap);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
64
host/step_steady.h
Normal file
64
host/step_steady.h
Normal file
@@ -0,0 +1,64 @@
|
||||
/* (Triforce, after NX2navi's runtime/frame_rate.c ocarina_step and its notes, chrg6's: the rule is copied) The game's
|
||||
time step kept steady while it follows the clock (host/nn_vi.c realtime_step, with HOS_STEP_STEADY=1). Plain C with no
|
||||
system call, so that tests/test_step_steady.py runs it on display intervals of its own.
|
||||
|
||||
The step is the mean of the last five present intervals (HOS_REALTIME_WINDOW): it moves with every one of them. A
|
||||
frame of 50 ms at a cap of 240 put the next five steps at 13 ms, three periods, so the game caught all of that frame's
|
||||
time up within 21 ms, a lurch after every hitch; intervals of 5 and 30 ms in turn gave steps of 15 and 20 ms in turn
|
||||
(NX2navi, the same game). Nothing in the game was written for a step that changes from frame to frame: its physics
|
||||
takes the step as the length of the next solve while the bodies' velocities come from the step before. So:
|
||||
- the base is the mean of the last 12 intervals without their longest and shortest: one late frame and the short
|
||||
one after it leave it alone, intervals alternating in a period of 2, 3 or 4 frames give one value, and a real
|
||||
change of frame rate arrives within 12 frames;
|
||||
- a base within 3% of the period the frames are paced by is that period: at a cap the game holds, the step is the
|
||||
same number every frame (at 60, exactly the one frame the game ships with);
|
||||
- what the display showed and the game was not given is owed (the late frame cut from the base, the 3% of the snap).
|
||||
Past half a step of it the step repays a 64th of the excess a frame, 10% of the step at most, so the game still
|
||||
follows the clock and a hitch is paid back over the next second or so rather than in five frames. More than a
|
||||
quarter second owed either way is dropped: the game runs slower than the clock, as it does for a longer frame.
|
||||
The step stays within the caller's bounds, and what the bounds took or gave is owed too. */
|
||||
#pragma once
|
||||
#include <math.h>
|
||||
|
||||
#define STEP_STEADY_WINDOW 12 /* (the intervals the base is the mean of, but for their longest and shortest) */
|
||||
#define STEP_STEADY_SNAP 0.03 /* (a base this near the paced period is the period) */
|
||||
#define STEP_STEADY_REPAY_MAX 0.10 /* (of the step: the most a frame repays) */
|
||||
#define STEP_STEADY_OWED_MAX 0.25 /* (seconds owed either way at most) */
|
||||
|
||||
typedef struct {
|
||||
double ring[STEP_STEADY_WINDOW];
|
||||
double owed; /* (seconds the display showed and the game was not given; under 0: given ahead) */
|
||||
unsigned count, next;
|
||||
} StepSteady;
|
||||
|
||||
/* A load, a debugger stop, a movie's start: the intervals before say nothing about the next */
|
||||
static inline void step_steady_reset(StepSteady *s) { s->count = s->next = 0, s->owed = 0; }
|
||||
|
||||
/* The step for a display interval dt (seconds, over 0): period, the period the frames are paced by (0: none known); lo and
|
||||
hi, the step's bounds */
|
||||
static inline double step_steady(StepSteady *s, double dt, double period, double lo, double hi) {
|
||||
if (dt > hi) dt = hi;
|
||||
s->ring[s->next++ % STEP_STEADY_WINDOW] = dt;
|
||||
if (s->count < STEP_STEADY_WINDOW) s->count++;
|
||||
double sum = 0, least = s->ring[0], most = s->ring[0];
|
||||
for (unsigned i = 0; i < s->count; i++) {
|
||||
sum += s->ring[i];
|
||||
if (s->ring[i] < least) least = s->ring[i];
|
||||
if (s->ring[i] > most) most = s->ring[i];
|
||||
}
|
||||
double base = s->count >= 4 ? (sum - least - most) / (s->count - 2) : sum / s->count;
|
||||
if (period > 0 && fabs(base - period) <= STEP_STEADY_SNAP * period) base = period;
|
||||
const double over = fabs(s->owed) - base * 0.5;
|
||||
double repay = 0;
|
||||
if (over > 0) {
|
||||
repay = over / 64 < STEP_STEADY_REPAY_MAX * base ? over / 64 : STEP_STEADY_REPAY_MAX * base;
|
||||
if (s->owed < 0) repay = -repay;
|
||||
}
|
||||
double step = base + repay;
|
||||
if (step < lo) step = lo;
|
||||
if (step > hi) step = hi;
|
||||
s->owed += dt - step;
|
||||
if (s->owed > STEP_STEADY_OWED_MAX) s->owed = STEP_STEADY_OWED_MAX;
|
||||
if (s->owed < -STEP_STEADY_OWED_MAX) s->owed = -STEP_STEADY_OWED_MAX;
|
||||
return step;
|
||||
}
|
||||
Reference in New Issue
Block a user