From e69ee44a3afc65c28daab7696a919fc0ed55ae70 Mon Sep 17 00:00:00 2001 From: itaskaev2 <165683571+itaskaev2@users.noreply.github.com> Date: Fri, 6 Nov 2026 18:04:16 +0100 Subject: [PATCH] Grow a content mod's HeapDef when an HD texture pack above it grows the texture heap df9ba65 keeps a content mod's heap sizes when its table is the one served, that is when the mod is above the texture pack. With the pack on top (HDPacks.com's 4kTextures sorts first), the pack's table was still swapped for the game's own, and The Second Quest's larger RSDB and UI heaps were lost: the game crashed at start writing to address 0 in main+0x1d4200. heapdef_overlay now grows the table of the highest switched-on mod that changes other heaps only, and falls back to the game's own table when no mod has one. Co-Authored-By: Claude Opus 5.5 --- runtime/host.h | 2 ++ runtime/resolution.c | 12 ++++++++++-- runtime/romfs.c | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/runtime/host.h b/runtime/host.h index 7bcc152a..b6a5a65a 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -264,6 +264,8 @@ uint64_t res_tex_heap_extra_bytes(void); // TOTK_TEX_HEAP_EXTR // the heap cap (mem_init), 2 MiB steps; 0 = none int res_heapdef_tex_grown(const void *in, size_t n); // the HeapDef table (.byml.zs) grows TextureHeap over the // shipped size (an HD texture pack's) +int res_heapdef_plain(const void *in, size_t n); // a readable HeapDef table whose TextureHeap is not + // grown (a content mod's) int res_heapdef_build(const void *in, size_t n, void **out, size_t *out_n); // the HeapDef table (.byml.zs) with // the grown heaps, malloc'd; 0 = cannot (logged) void res_heapdef_served(void); // romfs.c serves the grown table diff --git a/runtime/resolution.c b/runtime/resolution.c index 20b73d06..3ffa1f7e 100644 --- a/runtime/resolution.c +++ b/runtime/resolution.c @@ -288,8 +288,9 @@ static int heapdef_open(const void *in, size_t n, Byml *b, uint32_t *heaps, cons // for that room and the application pool has none: it crashed at start (issue #673). So before mem_init sizes the pool, // every switched-on mod's HeapDef is read (mods.c mods_peek_romfs: mods_scan needs paths main sets later) and the // largest TextureHeap growth over the shipped size counts as TOTK_TEX_HEAP_EXTRA_MB (the larger of the two, at most -// 4 GiB). romfs.c heapdef_overlay then serves the game's own table grown by that amount in the mod's place, so the -// other heaps keep the sizes of the game version that runs. +// 4 GiB). romfs.c heapdef_overlay then serves another table grown by that amount in the pack's place: the table of the +// highest switched-on mod below it whose texture heap is not grown, else the game's own, so the other heaps keep the +// sizes the content that runs needs. #define HEAPDEF_REL "System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs" static struct { int done; uint64_t kib; } MT; @@ -336,6 +337,13 @@ int res_heapdef_tex_grown(const void *in, size_t n) { return heapdef_tex_kib(in, n, "the mod's table stays the base") > (int32_t)TEX_HEAP_KIB; } +// A table that can stand in for an HD pack's (romfs.c heapdef_overlay): readable, with TextureHeap at most the shipped +// size. A content mod's table (The Second Quest grows RSDB and UI for its own files) is one. +int res_heapdef_plain(const void *in, size_t n) { + int32_t th = heapdef_tex_kib(in, n, "it does not stand in for the texture pack's table"); + return th > 0 && th <= (int32_t)TEX_HEAP_KIB; +} + int res_heapdef_build(const void *in, size_t n, void **out, size_t *out_n) { uint64_t extra = res_heap_extra_kib(), tex = tex_heap_extra_kib(); *out = NULL; diff --git a/runtime/romfs.c b/runtime/romfs.c index 484c6c0d..8ed62cce 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -849,9 +849,34 @@ static void restbl_overlay(void) { // their parents grow. The base is an overlay that replaces the table (a mod's heap sizes stay), else the RomFS // original; the grown table is written to /resolution/ and served in the base's place. A served mod table // whose own TextureHeap is grown (an HD texture pack's, resolution.c res_heapdef_tex_grown) is not the base: its -// growth is already in the amount (resolution.c mod_tex_heap_kib), so the RomFS original grows by it and is served in -// the mod's place. A mod table that changes other heaps only stays the base. +// growth is already in the amount (resolution.c mod_tex_heap_kib). In its place the table of the highest switched-on +// mod below it that changes other heaps only grows by the amount (plain_mod_heapdef: with the pack above The Second +// Quest, the default order for HDPacks.com's "4kTextures", the game's own sizes for RSDB and UI were too small for +// that mod's files and the game crashed loading a save), else the RomFS original; either is served in the pack's +// place. A mod table that changes other heaps only and is served itself stays the base. #define HEAPDEF "System/HeapSize/Product.Nin_NX_NVN.HeapDef.byml.zs" + +// The HeapDef table of the highest switched-on mod that changes other heaps only (malloc'd, *n, *who its name); NULL +// when no mod has one. Mods above the served table ship none, so the first found is the highest below it. +static void *plain_mod_heapdef(size_t *n, const char **who) { + for (int i = 0; i < mods_count(); i++) { + const ModInfo *m = mods_at(i); + char path[2300]; + if (!m->enabled || !m->romfs[0] || + snprintf(path, sizeof path, "%s/%s", m->romfs, HEAPDEF) >= (int)sizeof path) + continue; + uint64_t fn = 0; + uint8_t *f = plat_file_read(path, &fn); + if (!f) continue; + if (fn < (16u << 20) && res_heapdef_plain(f, (size_t)fn)) { + *n = (size_t)fn; + *who = m->name; + return f; + } + free(f); + } + return NULL; +} static void heapdef_overlay(void) { if (!res_heap_extra_kib() && !res_tex_heap_extra_bytes()) return; static const char keeps[] = "the game keeps its own resolution"; @@ -861,8 +886,11 @@ static void heapdef_overlay(void) { size_t out_n = 0; if (in && base && base->mod && res_heapdef_tex_grown(in, n)) { free(in); - in = read_original(HEAPDEF, &n, keeps); - if (in) + const char *who = NULL; + if ((in = plain_mod_heapdef(&n, &who))) + rt_log("RomFS: %s: the HeapDef of %s with the texture heap growth in place of the mod's table", + mods_at(base->mod - 1)->name, who); + else if ((in = read_original(HEAPDEF, &n, keeps))) rt_log("RomFS: %s: the game's own HeapDef with the mod's texture heap growth in place of the mod's table", mods_at(base->mod - 1)->name); }