mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-24 13:45:57 +00:00
The kept resource size table is off by default, and its key covers the running totk.exe, the RomFS image and every overlay
restbl_cache now defaults to 0. Its key adds the path and identity of the running exe (a dev or source build that changes the merge code keeps the VERSION string, so the port version alone served the old table), the RomFS image's identity, the number of shipped tables and the overlays of the port's own folders, which the old filter left out. The table's SHA-256 is taken from the bytes the build wrote instead of a read back from disk. A miss logs why the table is built again. totk.ini.example and docs/settings.md describe the setting, its default and what the key does not see (a rewrite that keeps size and write time). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
05d46f3538
commit
3b97936ed3
3 files changed
+76
-46
No files matched your search
+1
-1
@@ -115,12 +115,12 @@ Many rows below are developer tools that only act together with another setting
|
||||
| `mods_without_code` (default `0`) | `1` uses every mod without its code part, as if `mods_no_code` named them all. | See `mods_no_code`. | Caution |
|
||||
| `pipeline_cache` | Path of the Vulkan pipeline cache file (default `pipeline-cache.bin` beside `totk.exe`). | Moves the cache; a new path starts empty and pipelines are built again. | Caution |
|
||||
| `profile_dir` | Folder of the game profile, which holds the user account id that save data is named after; the default is a `profile` folder beside `totk.exe`, else the build-time path. | Another folder means a different set of saves. | Save risk |
|
||||
| `restbl_cache` (default `0`) | `1`: with player mods, the resource size table merged at the last start is served again when nothing it was built from changed: every mod file and shipped table (path, size, write time, file id), the mod list and its order, the RomFS image and `totk.exe` itself. `<profile>/mods/` keeps the table and its `.key`. `0` builds it at every start. | Saves the merge at start: the pass over every mod file and the mods' packs (about 0.9 s with The Second Quest, 3,300 files; more with a texture pack). A changed, added or removed mod file, another order, a new build or game version builds the table again; a damaged kept table or key is built again too. A tool that rewrites a mod file with the same size and write time is not seen. | Caution |
|
||||
| `romfs` | Folder of the game's RomFS (with `romfs.bin`); a relative path is relative to `totk.exe`'s folder. | Without it, a `romfs` folder next to `totk.exe` is used, else the build-time path; a missing image stops the start with a message. | Save risk |
|
||||
| `romfs_index` | Path of `romfs-index.bin`, which `tools/romfs_index.py` wrote; the default is beside `totk.exe`. | The index is checked against the image by SHA-256 and size; a mismatch stops the start. | Caution |
|
||||
| `romfs_mmap` | `1` maps the RomFS image into memory instead of reading it with positional reads. | Changes how the image is read; the log reports when the mapped path is on. | Caution |
|
||||
| `romfs_mods` | Folder of an overlay with a `manifest.tsv` of sizes and SHA-256 hashes; its files are served in place of the originals. | A missing file, size or hash mismatch, or a changed original stops the start; player mods win over this overlay. | Save risk |
|
||||
| `romfs_patches` | Folder of an overlay with a `manifest.tsv`, served in place of the original files; the default is `romfs-patches` beside the index. | Same checks as `romfs_mods`; while it is set the Xbox and PC button prompts are off, and the menu says so. | Save risk |
|
||||
| `restbl_cache` (default `1`) | With player mods, the resource size table merged from their files at the last start is served again when every mod file, shipped table and the port version are the same (`<profile>/mods/` keeps the table and its `.key`). `0` builds it at every start. | Saves the pass over every mod file at start (about 7 s with an HD texture pack and The Second Quest). A changed, added or removed mod file builds the table again. | Safe |
|
||||
| `romfs_proof_dir` | Folder for the `.romfs-verified-v1` proof file of a full verification; the default is beside the index. | Only moves one small file. | Safe |
|
||||
| `romfs_verify` | `full` hashes the whole RomFS image at start; `force` hashes it again even when a proof file exists. | Start takes many minutes for a 17 GB image; a proof file is written so later starts skip the hash. | Caution |
|
||||
| `shader_cache` | Folder of the translated shader cache (default `shader-cache` beside `totk.exe`). | A new folder starts empty, so shaders are translated again during play. | Caution |
|
||||
|
||||
+70
-45
@@ -25,7 +25,8 @@
|
||||
//
|
||||
// The resource size table: with a player mod enabled, the game's table is rebuilt at start with the tables the mods
|
||||
// ship merged in and the entries of the mods' files raised to what they need (restbl_overlay), the members of their
|
||||
// packs too (restbl_packs), and served from <profile>/mods/ in place of the game's or a mod's table.
|
||||
// packs too (restbl_packs), and served from <profile>/mods/ in place of the game's or a mod's table. TOTK_RESTBL_CACHE=1
|
||||
// serves the table of the last start instead when every input of the build is the same (restbl_cache_key).
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -761,14 +762,23 @@ static int restbl_packs(Restbl *out, const Restbl *base, int *packs, int *lines)
|
||||
return raised;
|
||||
}
|
||||
|
||||
// ---- the merged table across starts. Building it opens every mod file and unpacks the mods' packs: about 7 s with an
|
||||
// HD texture pack and The Second Quest (13,725 files). A start whose inputs are the same as the last one's serves the
|
||||
// table that start wrote. The key is a SHA-256 over this code's version, the port version, the game's table entry, and
|
||||
// for every shipped table and every mod or manifest overlay its path, place in the order and file identity (volume,
|
||||
// file id, size, write time). <profile>/mods/<table>.key holds the key, what the build gave (a table or none) and the
|
||||
// SHA-256 of the written table, so a copied profile keeps it; anything else there is a miss and the table is built
|
||||
// again. TOTK_RESTBL_CACHE=0 builds it at every start.
|
||||
#define RESTBL_CACHE_VERSION 2
|
||||
// ---- the merged table across starts (TOTK_RESTBL_CACHE=1, off by default). Building it opens every mod file and
|
||||
// unpacks the mods' packs. A start whose inputs are the same as the last one's serves the table that start wrote.
|
||||
// The key is a SHA-256 over every input restbl_overlay reads:
|
||||
// - the running totk.exe's path and identity: a new build may merge differently, and it carries the port version, the
|
||||
// game version and the RomFS index's SHA-256 (romfs_config.h);
|
||||
// - the RomFS image's path and identity and the game table's index entry: the base table, the game's copies of the
|
||||
// mods' packs and Pack/ZsDic.pack.zs come from the image;
|
||||
// - every shipped table (restbl_collect, mod_walk_cb; also the ones overlay_contest drops) with its owner and identity;
|
||||
// - every overlay overlay_contest kept (player mods, the TOTK_ROMFS_MODS manifest, the port's folders) with its RomFS
|
||||
// path, file path, place in the order (index, prio, mod) and identity.
|
||||
// Identity is the volume, file id, size, last write and creation time. A file's bytes are not hashed: identity is
|
||||
// what the overlay layer already trusts (romfs_open stops the game on a file whose identity changed after the start
|
||||
// check), and hashing a texture pack would cost more than the merge does. <profile>/mods/<table>.key holds the key,
|
||||
// what the build gave (a table or none) and the SHA-256 of the table it wrote; it is replaced atomically after the
|
||||
// table is written. A key file of another size, magic or version, another key, or a table of another SHA-256 is a
|
||||
// miss: the table is built again and both files are replaced, so the cache stays two files.
|
||||
#define RESTBL_CACHE_VERSION 3
|
||||
typedef struct { char magic[8]; uint32_t version, outcome; uint8_t key[32], table[32]; } RestblCache;
|
||||
enum { RESTBL_NONE = 1, RESTBL_TABLE = 2 };
|
||||
|
||||
@@ -778,15 +788,23 @@ static void restbl_cache_paths(char *key, size_t key_cap, char *table, size_t ta
|
||||
need(snprintf(key, key_cap, "%s.key", table) < (int)key_cap, "table key path");
|
||||
}
|
||||
|
||||
static void hash_file_id(Sha256 *h, const char *path) {
|
||||
static void hash_identity(Sha256 *h, const char *path, const Identity *id) {
|
||||
sha256_update(h, path, strlen(path) + 1);
|
||||
sha256_update(h, id, sizeof *id);
|
||||
}
|
||||
|
||||
// 0 when path cannot be opened.
|
||||
static int hash_file_id(Sha256 *h, const char *path) {
|
||||
PlatFile *f = open_read(path);
|
||||
Identity id = {0};
|
||||
if (f) id = identity_of(f), plat_file_close(f);
|
||||
sha256_update(h, path, strlen(path) + 1);
|
||||
sha256_update(h, &id, sizeof id);
|
||||
hash_identity(h, path, &id);
|
||||
return f != NULL;
|
||||
}
|
||||
|
||||
static void restbl_cache_key(uint64_t gi, uint8_t out[32]) {
|
||||
// The key of this start's inputs into out; 0 when the running exe cannot be opened (no key: the table is built).
|
||||
static int restbl_cache_key(uint64_t gi, uint8_t out[32]) {
|
||||
char exe[2100];
|
||||
Sha256 h;
|
||||
sha256_init(&h);
|
||||
static const char tag[] = "totk restbl cache";
|
||||
@@ -794,67 +812,75 @@ static void restbl_cache_key(uint64_t gi, uint8_t out[32]) {
|
||||
sha256_update(&h, tag, sizeof tag);
|
||||
sha256_update(&h, &v, sizeof v);
|
||||
sha256_update(&h, rt_port_version, strlen(rt_port_version) + 1);
|
||||
if (!plat_exe_path(exe, sizeof exe) || !hash_file_id(&h, exe)) return 0;
|
||||
Identity img = identity_of(image);
|
||||
hash_identity(&h, image_file, &img);
|
||||
Entry ge = entry(gi);
|
||||
sha256_update(&h, &ge, sizeof ge);
|
||||
uint64_t n = nshipped;
|
||||
sha256_update(&h, &n, sizeof n);
|
||||
for (size_t s = 0; s < nshipped; s++) {
|
||||
int32_t m[2] = {shipped[s].mod, shipped[s].other};
|
||||
sha256_update(&h, m, sizeof m);
|
||||
hash_file_id(&h, shipped[s].file);
|
||||
}
|
||||
uint64_t n = noverlay;
|
||||
n = noverlay;
|
||||
sha256_update(&h, &n, sizeof n);
|
||||
for (size_t k = 0; k < noverlay; k++) {
|
||||
const Overlay *o = &overlays[k];
|
||||
if (!o->mod && o->prio != 1 && o->index != gi) continue;
|
||||
int64_t place[3] = {o->index == ADDED ? -1 : (int64_t)o->index, o->prio, o->mod};
|
||||
sha256_update(&h, o->name, strlen(o->name) + 1);
|
||||
sha256_update(&h, o->path, strlen(o->path) + 1);
|
||||
sha256_update(&h, place, sizeof place);
|
||||
sha256_update(&h, &o->id, sizeof o->id);
|
||||
hash_identity(&h, o->path, &o->id);
|
||||
}
|
||||
sha256_final(&h, out);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void restbl_cache_store(const uint8_t key[32], int outcome) {
|
||||
// Records the build's result for key: RESTBL_NONE, or RESTBL_TABLE with the SHA-256 of the table it wrote.
|
||||
static void restbl_cache_store(const uint8_t key[32], int outcome, const uint8_t table[32]) {
|
||||
char kp[2300], tp[2300];
|
||||
restbl_cache_paths(kp, sizeof kp, tp, sizeof tp);
|
||||
RestblCache c = {.magic = "TOTKRSZ", .version = RESTBL_CACHE_VERSION, .outcome = (uint32_t)outcome};
|
||||
c.magic[7] = 'C';
|
||||
RestblCache c = {.magic = {'T', 'O', 'T', 'K', 'R', 'S', 'Z', 'C'}, .version = RESTBL_CACHE_VERSION,
|
||||
.outcome = (uint32_t)outcome};
|
||||
memcpy(c.key, key, 32);
|
||||
if (outcome == RESTBL_TABLE) {
|
||||
uint64_t n = 0;
|
||||
uint8_t *z = plat_file_read(tp, &n);
|
||||
if (!z) return;
|
||||
sha256(z, (size_t)n, c.table);
|
||||
free(z);
|
||||
}
|
||||
if (table) memcpy(c.table, table, 32);
|
||||
if (!plat_file_write_atomic(kp, &c, sizeof c))
|
||||
rt_log("RomFS: cannot write %s; the table is built at every start", kp);
|
||||
rt_log("RomFS: cannot write %s; the resource size table is built at every start", kp);
|
||||
}
|
||||
|
||||
// 1 when the last start's result fits: its table is served again (or none, as then).
|
||||
// 1 when the last start's result fits: its table is served again (or none, as then). A miss logs why.
|
||||
static int restbl_cache_use(const uint8_t key[32], Overlay *served, const char *keeps, double t0) {
|
||||
char kp[2300], tp[2300];
|
||||
restbl_cache_paths(kp, sizeof kp, tp, sizeof tp);
|
||||
uint64_t n = 0;
|
||||
RestblCache *c = (RestblCache *)plat_file_read(kp, &n);
|
||||
int hit = c && n == sizeof *c && !memcmp(c->magic, "TOTKRSZC", 8) && c->version == RESTBL_CACHE_VERSION &&
|
||||
!memcmp(c->key, key, 32) && (c->outcome == RESTBL_NONE || c->outcome == RESTBL_TABLE);
|
||||
if (hit && c->outcome == RESTBL_TABLE) {
|
||||
const char *miss = NULL;
|
||||
if (!c || n != sizeof *c || memcmp(c->magic, "TOTKRSZC", 8) || c->version != RESTBL_CACHE_VERSION ||
|
||||
(c->outcome != RESTBL_NONE && c->outcome != RESTBL_TABLE))
|
||||
miss = "no usable key from an earlier start";
|
||||
else if (memcmp(c->key, key, 32))
|
||||
miss = "the mods, the game files or the build changed since the last start";
|
||||
if (!miss && c->outcome == RESTBL_TABLE) {
|
||||
uint64_t zn = 0;
|
||||
uint8_t *z = plat_file_read(tp, &zn), sum[32];
|
||||
if (z) sha256(z, (size_t)zn, sum);
|
||||
hit = z && !memcmp(sum, c->table, 32);
|
||||
if (!hit) free(z);
|
||||
hit = hit && serve_generated(restbl_rel, served, z, (size_t)zn, "mods", "merged resource sizes, kept from the "
|
||||
"last start", keeps);
|
||||
if (!z || memcmp(sum, c->table, 32)) {
|
||||
free(z);
|
||||
miss = "the kept table is missing or damaged";
|
||||
} else if (!serve_generated(restbl_rel, served, z, (size_t)zn, "mods", // frees z
|
||||
"merged resource sizes, kept from the last start", keeps)) {
|
||||
miss = "the kept table cannot be served";
|
||||
}
|
||||
}
|
||||
if (hit)
|
||||
if (miss)
|
||||
rt_log("RomFS: resource size table: built again: %s", miss);
|
||||
else
|
||||
rt_log("RomFS: resource size table: the mods' files are the ones of the last start: %s (%.0f ms)",
|
||||
c->outcome == RESTBL_TABLE ? "its merged table is used" : "they fit the game's entries",
|
||||
(now_s() - t0) * 1000);
|
||||
free(c);
|
||||
return hit;
|
||||
return !miss;
|
||||
}
|
||||
|
||||
// With a player mod (or the TOTK_ROMFS_MODS manifest) serving files: the game's table with every shipped table of this
|
||||
@@ -873,11 +899,8 @@ static void restbl_overlay(void) {
|
||||
Overlay *served_now = NULL;
|
||||
for (size_t k = 0; k < noverlay; k++)
|
||||
if (overlays[k].index == gi) served_now = &overlays[k];
|
||||
int cached = rt_knob("RESTBL_CACHE", "1");
|
||||
if (cached) {
|
||||
restbl_cache_key(gi, cache_key);
|
||||
if (restbl_cache_use(cache_key, served_now, keeps, t0)) return;
|
||||
}
|
||||
int cached = rt_knob("RESTBL_CACHE", "0") && restbl_cache_key(gi, cache_key);
|
||||
if (cached && restbl_cache_use(cache_key, served_now, keeps, t0)) return;
|
||||
Entry ge = entry(gi);
|
||||
void *gz = ge.size < (64u << 20) ? malloc((size_t)ge.size) : NULL;
|
||||
Restbl *base = NULL, *out = NULL;
|
||||
@@ -943,7 +966,7 @@ static void restbl_overlay(void) {
|
||||
if (!merged && !raised && !members && !served) {
|
||||
restbl_free(out);
|
||||
rt_log("RomFS: resource size table: the mods' files fit the game's entries (%.0f ms)", (now_s() - t0) * 1000);
|
||||
if (cached) restbl_cache_store(cache_key, RESTBL_NONE);
|
||||
if (cached) restbl_cache_store(cache_key, RESTBL_NONE, NULL);
|
||||
return;
|
||||
}
|
||||
size_t rn = 0, zn = 0;
|
||||
@@ -958,11 +981,13 @@ static void restbl_overlay(void) {
|
||||
rt_log("RomFS: cannot pack the merged resource size table; %s", keeps);
|
||||
return;
|
||||
}
|
||||
uint8_t sum[32];
|
||||
if (cached) sha256(z, zn, sum); // before serve_generated frees z
|
||||
if (serve_generated(restbl_rel, served, z, zn, "mods", "merged resource sizes", keeps)) {
|
||||
rt_log("RomFS: resource size table: %d tables merged (%d entries), %d entries raised for mod files (%d "
|
||||
"added files estimated), %d for members of %d mod packs, %.0f ms", tables, merged, raised, estimated,
|
||||
members, packs, (now_s() - t0) * 1000);
|
||||
if (cached) restbl_cache_store(cache_key, RESTBL_TABLE);
|
||||
if (cached) restbl_cache_store(cache_key, RESTBL_TABLE, sum);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -571,6 +571,11 @@
|
||||
# cheats and the other patch files load. Its files may expect the missing code. 0: only the mods mods_no_code names.
|
||||
# Read at start. Default: 0.
|
||||
# mods_without_code = 0
|
||||
# 1: the resource size table merged from the mods' files at start is kept in <profile>/mods/ and served again at the
|
||||
# next start when no mod file, shipped table, the mod list or order, the game files or totk.exe changed (a file counts
|
||||
# as changed when its size, write time or file id did). It saves the merge at start. A tool that rewrites a mod file
|
||||
# with the same size and write time is not seen: set 0 while editing mod files that way. Read at start. Default: 0.
|
||||
# restbl_cache = 0
|
||||
# Extra room in MiB (0 to 4096) for the heap the world's textures stream into, for HD texture packs. Packs made for
|
||||
# emulators with 8 GB of DRAM grow it in their own HeapDef table (System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs):
|
||||
# the port reads that growth at start and applies it itself (about 3339 MiB for the HDPacks 4K pack), so such a pack
|
||||
|
||||
Reference in new issue
Block a user