From 873903d98f9f91e3cede8939469dc2d8eb1b517c Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 16 Nov 2026 20:11:43 +0300 Subject: [PATCH] The Mods tab order decides code sites too, conflicts show per mod, and each mod can be used without its code Load order: mods_exefs_apply ranks each mod's patch sets by its position (rt_patch_rank) and runtime/patches.c fills the pick byte of every contested site from the sets that are on, lowest rank first; a tie (two files of one mod) goes to the later file, as emulators apply a mod's files in name order. Reorder and restart, no rebuild. The pick is redone when an ordered set is switched at run time. Nothing changes for sites one set patches. Conflicts: one table (mods_conflict) of winner, loser, part (files, data bytes, code sites) and count. romfs.c fills it for files, mods_exefs_apply for the data bytes two mods stored and for the contested code sites. The log has both directions per entry and one line per contested code site; each row of the Mods tab lists what the mod overrides and what overrides it. Use without its code: a per-mod switch under the row of a mod with a code module or an ExeFS patch file the port cannot apply (mods_no_code in totk.ini, off by default). On, that code is left out and the rest of the mod is used; the row says what is left out and why. mods_without_code = 1 holds it on for every mod and now covers the unsupported patch files too. --probe-mods: order to winner at contested sites in both orders, the same-mod tie, a switched-off mod, a run-time switch of the winner, the conflict table for data and code, the per-mod switch. Co-Authored-By: Claude Opus 5.5 --- docs/mods.md | 33 +++++- docs/settings.md | 4 +- runtime/cfg.c | 4 +- runtime/host.h | 19 +++ runtime/mods.c | 236 ++++++++++++++++++++++++++++++++------ runtime/mods.h | 35 +++++- runtime/mods_probe.c | 122 +++++++++++++++++++- runtime/patches.c | 106 +++++++++++++++-- runtime/romfs.c | 11 +- runtime/ui/menu.cpp | 48 ++++++-- runtime/ui_text.c | 120 ++++++++++++------- runtime/ui_text.h | 15 ++- tests/mods_romfs_check.py | 11 +- totk.ini.example | 13 ++- 14 files changed, 650 insertions(+), 127 deletions(-) diff --git a/docs/mods.md b/docs/mods.md index 5a5a9aee..f369035c 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -34,11 +34,27 @@ packs, gameplay and text changes from GameBanana and similar sites, TKMM's merge ## Order and switches The Mods tab lists every folder in `mods`. Select a mod and press A or Enter to switch it on or off. The top face button -or Ctrl+Up moves it up, the left face button or Ctrl+Down moves it down. When two mods change the same file, the -higher one wins; the lower mod's row says how many of its files the higher one covers. Changes apply after a restart. +or Ctrl+Up moves it up, the left face button or Ctrl+Down moves it down. Changes apply after a restart. -The menu saves the order and the switches in `totk.ini`, under `[mods]`, by folder name: `mods_order` and -`mods_disabled`. A folder the list does not name yet, such as a new or renamed one, goes below the named ones. +The order decides every conflict, as in PC mod managers: when two mods change the same game file, the same bytes of the +game's data or the same instruction of the game's code, the higher one wins. Moving a mod and restarting is enough; the +game is not rebuilt for it. Each row lists what the mod wins and loses, by mod: "3 of its files come from X, which is +higher in the list", "It overrides 2 code patch sites of Y, which is lower in the list", and the same for game data. +`logs\totk.log` has the same as `mods: X: overrides Y at 2 code sites` and `mods: Y: 2 code sites overridden by X`, and +one line per contested code site. + +The menu saves the order and the switches in `totk.ini`, under `[mods]`, by folder name: `mods_order`, +`mods_disabled` and `mods_no_code`. A folder the list does not name yet, such as a new or renamed one, goes below the +named ones. + +### Mods with code the port cannot run + +Some mods bring code the port cannot run: their own Switch code (a subsdk plugin or a replaced main, as UltraCam ships) +or a code patch in a form the port cannot apply. Such a mod stays off as a whole, because its other files may expect +that code. Under its row the Mods tab then shows **Use without its code** (off by default). Switched on, the code +the port cannot run is left out and the rest of the mod loads after a restart: its game files, cheats and the code +patches that work. The mod's row says which part is left out and why. If the game misbehaves with it, switch it off +again. `mods_without_code = 1` under `[mods]` holds the switch on for every mod. ### Mods that change the same files @@ -71,6 +87,11 @@ The rebuild needs the setup folder the game was installed from. When that folder A code patch over one of the port's own patches (the 60 FPS patch, the resolution patch and the other entries under **Built into the port**) turns that built-in patch off while the mod is on. The mod's row names it. +When two mods patch the same instruction, the code of the higher one runs there; the other mod's patches run +everywhere else. The rebuild builds every mod's patch in, so a new order needs only a restart. Two patch files of one +mod that patch the same instruction: the one later by path inside the mod wins, as emulators apply a mod's files in +name order, each over the one before. A cheat that changes the same instruction runs instead of both while it is on. + A patch file may also change the game's data (constants and tables in its `.rodata` and `.data`). Those bytes are stored into the loaded game at start, before it runs. When two mods write the same bytes, the higher one wins, as with files. @@ -113,8 +134,8 @@ keeps its textures at a quarter of the size. | This folder has no romfs, exefs or cheats files. | The archive was unpacked one level too deep (`mods\Mod\Mod\romfs`), or it holds no game files. | Move the folders up so `romfs` sits directly in the mod's folder. | | ... files carry another game version in their names. | The files are named for another update (`.Product..`) and the game's own files have no such name. They are still served under those names, as on a console, because the game sometimes asks for one; another update's resource size table is the exception and only lends its entries. | Nothing if the mod works. If it does nothing, download its release for your game update. | | Its code patches (exefs) are made for another game version. | The patch files name another update's build id. | Same: get the release for your update. | -| This mod brings its own game code (a subsdk plugin or a replaced main). | Mods such as UltraCam ship compiled Switch code. The port cannot load it. | The whole mod stays off. **Use mods without their code** on the Mods tab (`mods_without_code = 1` under `[mods]`) uses the rest of it, which may expect the missing code. | -| This mod patches the game code (exefs) in a way the port cannot apply. | The patch writes the game's data, not its code, or changes code in a form the port cannot build. | No workaround; the reason follows on the row. | +| This mod brings its own game code (a subsdk plugin or a replaced main). | Mods such as UltraCam ship compiled Switch code. The port cannot load it. | The whole mod stays off. **Use without its code** under its row uses the rest of it, which may expect the missing code (see Mods with code the port cannot run). | +| This mod patches the game code (exefs) in a way the port cannot apply. | The patch changes code in a form the port cannot build, or bytes the port cannot change at start. The reason follows on the row. | The whole mod stays off. **Use without its code** under its row leaves that patch file out and uses the rest of the mod. | | The folder name contains \|. | The mod list uses `\|` between names. | Rename the folder. | A mod never stops the game from starting. Whatever the port cannot use is left out and shown on the Mods tab. diff --git a/docs/settings.md b/docs/settings.md index 599273ff..b887a1af 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -97,7 +97,9 @@ Many rows below are developer tools that only act together with another setting | `menu_font_fontconfig` (default `1`) | On Linux, `1` asks fontconfig which font file draws each menu language's name, and `0` keeps the fixed distribution paths (`/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc` and four more). The menu merges that file into its font atlas. | `0` is for a system whose fontconfig resolves to a font the menu's parser cannot read; the log then says `menu: system font ... left out`. Has no effect on Windows, where the file names are fixed, and none on a language the ASCII font already draws. The atlas is rebuilt when the window size or the menu language changes. `TOTK_MENU_FONT` names a file directly and wins over both. | Safe | | `mods_dir` (default `mods`) | Folder that holds one subfolder per player mod, beside `totk.exe`. | Mods found there replace game files; see `docs/mods.md`. | Caution | | `mods_disabled` | Names of mods that are switched off, separated by `\|`. | Listed mods are not applied; the Mods tab writes this line. | Safe | -| `mods_order` | Mod names, top first, separated by `\|`; the top mod wins a file that two mods replace. | Changes which mod's file the game gets; unlisted mods go below the listed ones, by name. | Safe | +| `mods_no_code` | Names of mods used without their code part, separated by `\|`; the Mods tab's switch "Use ... without its code" writes it. | A listed mod's code modules and the ExeFS patch files the port cannot apply are left out and the rest of the mod loads, where otherwise the whole mod stays off. Its files may expect the missing code. | Caution | +| `mods_order` | Mod names, top first, separated by `\|`; the top mod wins a file, a byte of game data or an instruction of game code that two mods change. | Changes which mod's file, data or code the game gets, at the next start; unlisted mods go below the listed ones, by name. | Safe | +| `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 | | `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 | diff --git a/runtime/cfg.c b/runtime/cfg.c index 4b0b8496..38d58207 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -53,7 +53,7 @@ static const char *const known[] = { "GYRO_BIAS_PAD", "GYRO_BIAS_X", "GYRO_BIAS_Y", "GYRO_BIAS_Z", "GYRO_CALIBRATION", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LOD_STEADY", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MEMX", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG", - "MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MODS_WITHOUT_CODE", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", + "MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_NO_CODE", "MODS_ORDER", "MODS_REBUILD", "MODS_WITHOUT_CODE", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OPTREF_GUARD", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POINTER_IMAGE", @@ -88,7 +88,7 @@ static const struct { const char *key, *section; } sections[] = { {"DISCORD_RPC", "discord"}, {"DISCORD_APP_ID", "discord"}, {"DISCORD_IMAGE", "discord"}, {"SHADOW_SCALE", "display"}, {"SHADOW_RANGE", "display"}, {"GRASS_RANGE", "display"}, {"CONTACT_SHADOWS", "display"}, {"PCSS", "display"}, - {"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_REBUILD", "mods"}, {"MODS_WITHOUT_CODE", "mods"}, + {"MODS_DIR", "mods"}, {"MODS_ORDER", "mods"}, {"MODS_DISABLED", "mods"}, {"MODS_NO_CODE", "mods"}, {"MODS_REBUILD", "mods"}, {"MODS_WITHOUT_CODE", "mods"}, {"TEX_HEAP_EXTRA_MB", "mods"}, {"MENU_LANG", "language"}, {"MENU_FONT_FONTCONFIG", "language"}, {"LANG", "language"}, {"OPERATION_MODE", "language"}, {"VOLUME", "audio"}, {"VOLUME_MUSIC", "audio"}, {"VOLUME_SFX", "audio"}, {"VOLUME_VOICE", "audio"}, {"AUDIO_CHANNELS", "audio"}, diff --git a/runtime/host.h b/runtime/host.h index a5a25ed4..152e1b82 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -320,6 +320,25 @@ void rt_patch_disable(int set); // before the guest s void rt_patch_switch(int set, int on); // at run time (live settings): each site takes the new code // the next time it runs void *rt_patch_page(void); // the page the patched code reads (NULL without the table) +// Contested sites (recomp/patches.py `ordered`): a site two or more ordered sets patch runs the one that is on with the +// lowest rank (a tie: the one later in the patch file); rt_patch_pick[] holds its place, 1 up, 0 for none. +typedef struct { + const char *module; // "main" + uint32_t offset; // the site's module offset + const int16_t *sets; // its ordered sets, in the order its C tests them + int nsets; +} RtPatchPick; +void rt_patch_rank(int set, int rank); // an ordered set's rank (0 first: the Mods tab's position); + // unranked sets come after every ranked one +void rt_patch_picks(void); // every contested site's pick from the sets on and their + // ranks; rt_patch_switch of an ordered set redoes its sites +int rt_patch_pick_count(void); +const RtPatchPick *rt_patch_pick_at(int i); // NULL out of range +int rt_patch_pick_winner(int i); // the place in its sets of the set that runs there, -1 none +const char *rt_patch_name(int set); // NULL out of range +// --probe-mods: a set table of its own (names, n sets, its contested sites), every set off and unranked; names NULL +// returns to the generated table. +void rt_patch_probe_table(const char *const *names, int n, const RtPatchPick *picks, int npicks); void frame_step_init(void); // after rt_patches_init: the real-time step's first value int frame_step_realtime(void); // the real-time step is on: presents ignore the interval uint64_t frame_step_cap_ns(void); // with the real-time step: the shortest frame in ns diff --git a/runtime/mods.c b/runtime/mods.c index 9a63860c..3f99e8dd 100644 --- a/runtime/mods.c +++ b/runtime/mods.c @@ -9,7 +9,14 @@ // The order and the switches are totk.ini keys (cfg.c): TOTK_MODS_ORDER lists names top first, separated by '|' (a // Windows file name cannot hold '|'; a folder whose name does on Linux is listed as unusable); a folder it does not name // goes below the named ones, by name. TOTK_MODS_DISABLED lists the switched-off names. The menu changes the list in -// place and writes both keys; the scan's state is kept to tell the menu a restart is due. +// place and writes both keys; the scan's state is kept to tell the menu a restart is due. TOTK_MODS_NO_CODE lists the +// mods used without their code part (their switch "use without its code"; TOTK_MODS_WITHOUT_CODE = 1 is that switch on +// for every mod). +// +// The order decides every conflict, top mod winning: romfs.c serves the top mod's file, mods_exefs_apply stores the +// top mod's data bytes last and ranks each mod's patch sets by its position (rt_patch_rank), so at a code site several +// mods patch the top one's code runs (patches.c rt_patch_picks). What a mod won or lost goes into one conflict table +// (mods_conflict), which the menu and the log read. // // ExeFS patch files: an .ips or .pchtxt file in the exefs folder or in atmosphere/exefs_patches// is for the game // when its build id is main's (an IPS file's name, a pchtxt file's @nsobid line); others are another version's and @@ -21,7 +28,7 @@ // // Code modules: an exefs/subsdk* file is a plugin that patches main from its own start-up on the Switch, and a replaced // exefs/main is another game code; the port cannot load either (ModInfo.code names them): their mod stays off unless -// mods_without_code = 1. +// it is used without its code, which also leaves out the ExeFS patch files the port cannot apply. #include #include #include @@ -40,7 +47,10 @@ static ModInfo *mods; static int nmods; static char dir[2048]; static char start_order[MAX_MODS][256]; // names in the scan's order and their switches, for mods_changed -static int start_enabled[MAX_MODS]; +static int start_enabled[MAX_MODS], start_no_code[MAX_MODS]; +#define MAX_CONFLICTS 1024 +static ModConflict conflicts[MAX_CONFLICTS]; +static int nconflicts; // 1 when a file lies anywhere under path. static int has_file_cb(const char *name, int kind, uint64_t size, void *ctx); @@ -142,10 +152,12 @@ static const char *problem_name(ModProblem p) { switch (p) { case MOD_EMPTY: return "empty"; case MOD_PLUGIN: - return "has a code module (subsdk or main), not supported; the whole mod stays off (mods_without_code = 1 uses " - "the rest of it)"; + return "has a code module (subsdk or main), not supported; the whole mod stays off (its switch \"use without " + "its code\" uses the rest of it)"; case MOD_EXEFS_LATER: return "ExeFS patches wait for a rebuild of the game code; the whole mod stays off"; - case MOD_EXEFS_UNSUPPORTED: return "an ExeFS patch file cannot be applied; the whole mod stays off"; + case MOD_EXEFS_UNSUPPORTED: + return "an ExeFS patch file cannot be applied; the whole mod stays off (its switch \"use without its code\" " + "uses the rest of it)"; case MOD_EXEFS_OTHER: return "its ExeFS patches are for another game version"; case MOD_BAD_NAME: return "its folder name holds '|', which the order list uses; rename the folder"; default: return "ok"; @@ -286,7 +298,8 @@ static int exefs_file_cb(const char *leaf, int kind, uint64_t size, void *ctx) { return 1; } if (!strcmp(kind_s, "unsupported")) { - if (!w->unsupported) snprintf(m->note, sizeof m->note, "%s: %s", leaf, note); + if (!m->unsupported) snprintf(m->note, sizeof m->note, "%s: %s", leaf, note); + m->unsupported++; w->unsupported = 1; } else if (!strcmp(kind_s, "code") && m->nsets < (int)(sizeof m->sets / sizeof *m->sets)) { m->sets[m->nsets++] = (uint32_t)strtoul(key, NULL, 16); @@ -339,6 +352,7 @@ static int scan_cb(const char *name, int kind, uint64_t size, void *ctx) { } ModInfo *m = &mods[nmods]; memset(m, 0, sizeof *m); + m->id = nmods; char path[2100]; if (snprintf(m->name, sizeof m->name, "%s", name) >= (int)sizeof m->name || snprintf(path, sizeof path, "%s/%s", dir, name) >= (int)sizeof path) { @@ -353,17 +367,20 @@ static int scan_cb(const char *name, int kind, uint64_t size, void *ctx) { if (m->exefs[0]) plat_fs_list(m->exefs, exefs_cb, m); if (m->cheats[0]) plat_fs_list(m->cheats, cheats_cb, &m->kind); int unsupported = exefs_check(m, path); - // A mod whose code part the port cannot use is off as a whole: its RomFS files may expect that code. A code - // module (m->code) never loads; with mods_without_code = 1 the rest of the mod is used, when it has a rest. - int rest = m->kind & (MOD_ROMFS | MOD_EXEFS | MOD_CHEATS); - int code_off = m->code[0] && (!cfg_bool("TOTK_MODS_WITHOUT_CODE") || !rest); - m->problem = strchr(m->name, '|') ? MOD_BAD_NAME - : code_off ? MOD_PLUGIN - : unsupported ? MOD_EXEFS_UNSUPPORTED - : m->pending ? MOD_EXEFS_LATER - : m->nsets || m->ndata || m->kind & (MOD_ROMFS | MOD_CHEATS) ? MOD_OK - : m->kind & MOD_EXEFS ? MOD_EXEFS_OTHER - : MOD_EMPTY; + // A mod whose code part the port cannot use is off as a whole: its RomFS files may expect that code. Used without + // its code (its switch, or mods_without_code = 1 for every mod), the code modules (m->code) and the ExeFS patch + // files the port cannot apply are left out and the rest of the mod is used, when it has a rest. + m->no_code = listed(cfg_get("TOTK_MODS_NO_CODE"), m->name); + m->without_code = m->no_code || cfg_bool("TOTK_MODS_WITHOUT_CODE"); + int usable = m->nsets || m->ndata || m->kind & (MOD_ROMFS | MOD_CHEATS); + int code_off = m->code[0] && (!m->without_code || !(m->kind & (MOD_ROMFS | MOD_EXEFS | MOD_CHEATS))); + m->problem = strchr(m->name, '|') ? MOD_BAD_NAME + : code_off ? MOD_PLUGIN + : unsupported && (!m->without_code || !usable) ? MOD_EXEFS_UNSUPPORTED + : m->pending ? MOD_EXEFS_LATER + : usable ? MOD_OK + : m->kind & MOD_EXEFS ? MOD_EXEFS_OTHER + : MOD_EMPTY; m->enabled = !listed(disabled, m->name); nmods++; return 1; @@ -426,14 +443,18 @@ void mods_scan(void) { for (int i = 0; i < nmods; i++) { snprintf(start_order[i], sizeof start_order[i], "%s", mods[i].name); start_enabled[i] = mods[i].enabled; + start_no_code[i] = mods[i].no_code; const ModInfo *m = &mods[i]; rt_log("mods: %s: %s,%s%s%s%s%s, %s%s%s", m->name, m->enabled ? "on" : "off", m->kind & MOD_ROMFS ? " romfs" : "", m->kind & MOD_EXEFS ? " exefs" : "", m->kind & MOD_SUBSDK ? " subsdk" : "", m->kind & MOD_CHEATS ? " cheats" : "", m->kind ? "" : " no files", problem_name(m->problem), m->note[0] ? ": " : "", m->note); if (m->code[0] && m->problem != MOD_PLUGIN) - rt_log("mods: %s: its code module (%s) does not load (mods_without_code = 1); the rest of the mod is used", + rt_log("mods: %s: used without its code: its code module (%s) does not load; the rest of the mod is used", m->name, m->code); + if (m->unsupported && m->problem != MOD_EXEFS_UNSUPPORTED) + rt_log("mods: %s: used without its code: %u ExeFS patch files the port cannot apply are left out (%s); the " + "rest of the mod is used", m->name, m->unsupported, m->note); } rt_log("mods: %d folders in %s", nmods, dir); } @@ -575,34 +596,164 @@ void mods_rescan(void) { free(mods); mods = NULL; nmods = 0; + nconflicts = 0; scanned = 0; } -// Switches the patch set mod_ on; 0 when this build lacks it (logged). -static int set_on(const ModInfo *m, uint32_t key) { +// ---- the conflict table + +void mods_conflict(int winner, int loser, ModPart part, uint32_t count) { + for (int i = 0; i < nconflicts; i++) { + ModConflict *c = &conflicts[i]; + if (c->winner == winner && c->loser == loser && c->part == part) { + c->count += count; + return; + } + } + if (nconflicts == MAX_CONFLICTS) return; // a mod list with more pairs than that: the rest goes unshown + conflicts[nconflicts++] = (ModConflict){winner, loser, part, count}; +} + +int mods_conflict_count(void) { return nconflicts; } +const ModConflict *mods_conflict_at(int i) { return i >= 0 && i < nconflicts ? &conflicts[i] : NULL; } + +const ModInfo *mods_by_id(int id) { + for (int i = 0; i < nmods; i++) + if (mods[i].id == id) return &mods[i]; + return NULL; +} + +void mods_conflicts_log(ModPart part) { + static const char *const unit[MOD_PARTS] = {"files", "bytes of main's data", "code sites"}; + for (int i = 0; i < nconflicts; i++) { + const ModConflict *c = &conflicts[i]; + const ModInfo *w = mods_by_id(c->winner), *l = mods_by_id(c->loser); + if (c->part != part || !w || !l) continue; + rt_log("mods: %s: overrides %s at %u %s", w->name, l->name, c->count, unit[part]); + rt_log("mods: %s: %u %s overridden by %s", l->name, c->count, unit[part], w->name); + } +} + +// Switches the patch set mod_ on and gives it rank; -1 when this build lacks it (logged). +static int set_on(const ModInfo *m, uint32_t key, int rank) { char name[32]; snprintf(name, sizeof name, "mod_%08x", key); int set = rt_patch_set(name); - if (set >= 0) rt_patch_switch(set, 1); - else rt_log("mods: %s: patch set %s is not in this build", m->name, name); - return set >= 0; + if (set >= 0) { + rt_patch_rank(set, rank); + rt_patch_switch(set, 1); + } else { + rt_log("mods: %s: patch set %s is not in this build", m->name, name); + } + return set; +} + +// The mod in use whose ExeFS patch file has the patch set, NULL for a set of no mod. +static const ModInfo *set_owner(int set) { + const char *name = rt_patch_name(set); + if (!name || strncmp(name, "mod_", 4)) return NULL; + uint32_t key = (uint32_t)strtoul(name + 4, NULL, 16); + for (int i = 0; i < nmods; i++) + for (int k = 0; start_enabled[i] && mods[i].problem == MOD_OK && k < mods[i].nsets; k++) + if (mods[i].sets[k] == key) return &mods[i]; + return NULL; +} + +// The contested code sites (patches.c): which mod's code runs at each, logged, and one site per losing mod in the +// conflict table. Two files of one mod on one site are no conflict: the later file in the mod runs (the tie rule of +// rt_patch_picks, as emulators apply a mod's files in name order, each over the one before). +static void code_conflicts(void) { + int logged = 0, contested = 0; + for (int i = 0; i < rt_patch_pick_count(); i++) { + const RtPatchPick *p = rt_patch_pick_at(i); + int w = rt_patch_pick_winner(i); + const ModInfo *wm = w >= 0 ? set_owner(p->sets[w]) : NULL; + if (!wm) continue; + char lost[512] = ""; + const ModInfo *seen[16]; + int nseen = 0; + for (int j = 0; j < p->nsets; j++) { + const ModInfo *lm = j != w && rt_patch_enabled(p->sets[j]) ? set_owner(p->sets[j]) : NULL; + int dup = !lm || lm == wm; + for (int k = 0; !dup && k < nseen; k++) dup = seen[k] == lm; + if (dup) continue; + if (nseen < 16) seen[nseen++] = lm; + mods_conflict(wm->id, lm->id, MOD_PART_CODE, 1); + size_t at = strlen(lost); + snprintf(lost + at, sizeof lost - at, "%s%s", at ? ", " : "", lm->name); + } + if (!*lost) continue; + contested++; + if (logged++ < 32) + rt_log("mods: code site %s+%#x: %s's patch runs; %s's stays off (higher in the list wins)", p->module, + p->offset, wm->name, lost); + } + if (logged > 32) rt_log("mods: %d more contested code sites", logged - 32); + if (contested) mods_conflicts_log(MOD_PART_CODE); +} + +// The data bytes stored at start, by mod: the overlaps are data conflicts. +typedef struct { uint64_t addr; int n, id, pos; } DataSpan; +static DataSpan *spans; +static int nspans, cap_spans, span_id, span_pos; +static int (*span_store)(uint64_t addr, const uint8_t *bytes, int n); + +static int record_store(uint64_t addr, const uint8_t *bytes, int n) { + if (!(span_store ? span_store : store_main)(addr, bytes, n)) return 0; + if (nspans == cap_spans) { + int cap = cap_spans ? 2 * cap_spans : 256; + DataSpan *grown = realloc(spans, (size_t)cap * sizeof *spans); + if (!grown) return 1; // stored; only its conflict goes unseen + spans = grown, cap_spans = cap; + } + spans[nspans++] = (DataSpan){addr, n, span_id, span_pos}; + return 1; +} + +static int by_addr(const void *a, const void *b) { + uint64_t x = ((const DataSpan *)a)->addr, y = ((const DataSpan *)b)->addr; + return (x > y) - (x < y); +} + +// Bytes two mods both wrote: the higher one's stayed (mods_exefs_apply stores bottom first). +static void data_conflicts(void) { + qsort(spans, (size_t)nspans, sizeof *spans, by_addr); + int any = 0; + for (int i = 0; i < nspans; i++) { + uint64_t end = spans[i].addr + (uint64_t)spans[i].n; + for (int j = i + 1; j < nspans && spans[j].addr < end; j++) { + if (spans[j].id == spans[i].id) continue; + uint64_t jend = spans[j].addr + (uint64_t)spans[j].n; + uint32_t n = (uint32_t)((jend < end ? jend : end) - spans[j].addr); + const DataSpan *hi = spans[i].pos < spans[j].pos ? &spans[i] : &spans[j]; + mods_conflict(hi->id, hi == &spans[i] ? spans[j].id : spans[i].id, MOD_PART_DATA, n); + any = 1; + } + } + free(spans); + spans = NULL; + nspans = cap_spans = 0; + if (any) mods_conflicts_log(MOD_PART_DATA); } void mods_exefs_apply(uint64_t base, uint64_t size, uint64_t code_end, int (*store)(uint64_t addr, const uint8_t *bytes, int n)) { - // bottom first: the top mod's data writes land last and win, as its RomFS files do. Patch sets are only switches - // (rt_patch_switch), so their order does not matter. + // bottom first: the top mod's data writes land last and win, as its RomFS files do. Patch sets are switches + // (rt_patch_switch); the mod's position ranks them, so where two mods patch one code site the higher one's runs + // (rt_patch_picks after the loop). + span_store = store; for (int i = nmods - 1; i >= 0; i--) { const ModInfo *m = &mods[i]; if (!start_enabled[i] || m->problem != MOD_OK || !(m->nsets || m->ndata)) continue; // the data first: a mod whose data writes cannot all be stored keeps its code patches off too, as half of a // patch file is worse than none - long stored = size ? mods_exefs_data(m, base, size, code_end, store) : 0; + span_id = m->id, span_pos = i; + long stored = size ? mods_exefs_data(m, base, size, code_end, record_store) : 0; if (stored < 0) { rt_log("mods: %s: its ExeFS patch files stay off: their data writes could not all be stored", m->name); continue; } - for (int k = 0; k < m->nsets; k++) set_on(m, m->sets[k]); + for (int k = 0; k < m->nsets; k++) set_on(m, m->sets[k], i); char over[256]; snprintf(over, sizeof over, "%s", m->overrides); for (char *s = strtok(over, ","); s; s = strtok(NULL, ",")) { @@ -613,6 +764,10 @@ void mods_exefs_apply(uint64_t base, uint64_t size, uint64_t code_end, rt_log("mods: %s: %d ExeFS patch files on, %ld bytes of main's data stored", m->name, m->nsets + m->ndata, stored); } + span_store = NULL; + data_conflicts(); + rt_patch_picks(); + code_conflicts(); } void mods_exefs_switch(void) { @@ -638,12 +793,13 @@ const char *mods_dir(void) { return dir; } int mods_count(void) { return nmods; } ModInfo *mods_at(int i) { return i >= 0 && i < nmods ? &mods[i] : NULL; } -// TOTK_MODS_ORDER as every listed name top first; TOTK_MODS_DISABLED as the switched-off names. A name holding '|' -// would split into two; it is left out of both (its mod is unusable anyway). +// TOTK_MODS_ORDER as every listed name top first; TOTK_MODS_DISABLED as the switched-off names; TOTK_MODS_NO_CODE as +// the names used without their code. A name holding '|' would split into two; it is left out of all (its mod is +// unusable anyway). static void write_keys(void) { size_t cap = (size_t)nmods * 257 + 1; - char *order = calloc(cap, 1), *off = calloc(cap, 1); - if (order && off) { + char *order = calloc(cap, 1), *off = calloc(cap, 1), *no_code = calloc(cap, 1); + if (order && off && no_code) { for (int i = 0; i < nmods; i++) { if (mods[i].problem == MOD_BAD_NAME) continue; if (*order) strcat(order, "|"); @@ -652,12 +808,18 @@ static void write_keys(void) { if (*off) strcat(off, "|"); strcat(off, mods[i].name); } + if (mods[i].no_code) { + if (*no_code) strcat(no_code, "|"); + strcat(no_code, mods[i].name); + } } cfg_set("TOTK_MODS_ORDER", order); cfg_set("TOTK_MODS_DISABLED", off); + cfg_set("TOTK_MODS_NO_CODE", no_code); } free(order); free(off); + free(no_code); } void mods_set_enabled(int i, int on) { @@ -666,6 +828,12 @@ void mods_set_enabled(int i, int on) { write_keys(); } +void mods_set_no_code(int i, int on) { + if (i < 0 || i >= nmods) return; + mods[i].no_code = on != 0; + write_keys(); +} + void mods_move(int i, int delta) { int j = i + delta; if (i < 0 || i >= nmods || j < 0 || j >= nmods || i == j) return; @@ -677,6 +845,8 @@ void mods_move(int i, int delta) { int mods_changed(void) { for (int i = 0; i < nmods; i++) - if (strcmp(mods[i].name, start_order[i]) || mods[i].enabled != start_enabled[i]) return 1; + if (strcmp(mods[i].name, start_order[i]) || mods[i].enabled != start_enabled[i] || + mods[i].no_code != start_no_code[i]) + return 1; return 0; } diff --git a/runtime/mods.h b/runtime/mods.h index 033dc89c..f400310d 100644 --- a/runtime/mods.h +++ b/runtime/mods.h @@ -5,7 +5,9 @@ // switched with their mod (mods_exefs_switch), and data writes stored into main before the game runs (mods_exefs_data). // The list is scanned once at start (romfs.c overlay_init); // its order and switches live in totk.ini [mods] (TOTK_MODS_ORDER: names top first, separated by '|'; -// TOTK_MODS_DISABLED: names switched off). The top mod wins a file two mods replace. +// TOTK_MODS_DISABLED: names switched off; TOTK_MODS_NO_CODE: names used without their code part). The top mod wins +// whatever two mods change: a file, bytes of main's data, a code site (patches.c rt_patch_picks); the conflict table +// (mods_conflict_at) records who won over whom, for the menu and the log. #pragma once #include @@ -18,7 +20,8 @@ typedef enum { // rest of such a mod is used, without those code modules (ModInfo.code names them) MOD_EXEFS_LATER, // has ExeFS patches for this game's main that the game was built without: the rebuild at the // next start builds them in; the whole mod stays off until then - MOD_EXEFS_UNSUPPORTED, // an ExeFS patch file the port cannot apply (note says why): the whole mod stays off + MOD_EXEFS_UNSUPPORTED, // an ExeFS patch file the port cannot apply (note says why): the whole mod stays off, + // unless it is used without its code (ModInfo.without_code) and has a usable rest MOD_EXEFS_OTHER, // only ExeFS patches, all for another game version MOD_BAD_NAME, // its folder name holds '|', the separator of the order keys } ModProblem; @@ -29,14 +32,18 @@ typedef struct { char exefs[2048]; // UTF-8 path of its ExeFS folder; "" none char cheats[2048]; // UTF-8 path of its cheats folder; "" none int kind, enabled; // kind: MOD_SUBSDK for any code module + int id; // its place in the scan: names it in the conflict table, whatever the menu moves + int no_code; // its switch "use without its code" (TOTK_MODS_NO_CODE) + int without_code; // the scan's: no_code or TOTK_MODS_WITHOUT_CODE (every mod); the code parts it cannot run + // (code modules, unsupported ExeFS patch files) are left out and the rest is used char code[64]; // its code modules in exefs ("subsdk9", "main, subsdk9"): never loaded + uint32_t unsupported; // its ExeFS patch files the port cannot apply (the first one's reason in note) ModProblem problem; uint32_t replaced, added, overridden; // romfs.c: files served (replacing / added), files a higher mod serves uint32_t other_version; // files named for another game version: RomFS *.Product..* the RomFS // lacks (served under their own names; another version's size table is // not) and ExeFS patches for another build (not used) - char overridden_by[256]; // the first higher mod that took one of its files - char note[256]; // MOD_EXEFS_UNSUPPORTED: why + char note[256]; // an unsupported ExeFS patch file: which and why uint32_t pending, pending_key; // ExeFS patch files the build lacks; FNV-1a of their names and contents int nsets; uint32_t sets[16]; // the built patch sets mod_ of its ExeFS patch files @@ -47,6 +54,20 @@ typedef struct { char overrides[256]; // the port's own patch sets its patches replace, comma separated } ModInfo; +// What two mods both change, the higher one winning: files (romfs.c), bytes of main's data and code sites +// (mods_exefs_apply). One entry per winner, loser and part, as at start. +typedef enum { MOD_PART_FILES, MOD_PART_DATA, MOD_PART_CODE, MOD_PARTS } ModPart; +typedef struct { + int winner, loser; // ModInfo.id + ModPart part; + uint32_t count; // files, bytes, code sites +} ModConflict; +void mods_conflict(int winner, int loser, ModPart part, uint32_t count); // adds count to the pair's entry +int mods_conflict_count(void); +const ModConflict *mods_conflict_at(int i); +void mods_conflicts_log(ModPart part); // one line each way per entry of that part +const ModInfo *mods_by_id(int id); + void mods_scan(void); // once; later calls return at once // Every switched-on mod folder's RomFS root, without the rest of the scan (no ExeFS checks, no order): for mem_init, // which runs before the paths mods_scan needs are set (resolution.c mod_tex_heap_kib). cb gets the name and root. @@ -57,10 +78,12 @@ ModInfo *mods_at(int i); // 0 = top of the order; NULL out of range // Menu edits (present path). They change the list mods_at shows and write TOTK_MODS_ORDER / TOTK_MODS_DISABLED with // cfg_set; romfs.c used the start state, so they apply at the next start. void mods_set_enabled(int i, int on); +void mods_set_no_code(int i, int on); // "use without its code" (TOTK_MODS_NO_CODE) void mods_move(int i, int delta); // -1 up, +1 down; nothing at the ends int mods_changed(void); // 1 when the order or a switch differs from the start -// After ldr_load and rt_patches_init, before any game code: the patch sets of the enabled mods' ExeFS patches on, the -// port's own sets they replace off, and their data writes stored into main (mods_exefs_data). +// After ldr_load and rt_patches_init, before any game code: the patch sets of the enabled mods' ExeFS patches on and +// ranked by the order (rt_patch_rank: the top mod's code runs where several patch one site), the port's own sets they +// replace off, and their data writes stored into main (mods_exefs_data); the data and code conflicts recorded. void mods_exefs_switch(void); // What mods_exefs_switch does with main's image at base (size bytes, code below code_end; size 0: no data stored), for // every enabled mod from the bottom one up, so that the top mod's data writes win where two mods write the same bytes. diff --git a/runtime/mods_probe.c b/runtime/mods_probe.c index 6e27cc30..cc223bfc 100644 --- a/runtime/mods_probe.c +++ b/runtime/mods_probe.c @@ -121,18 +121,132 @@ static void probe_data(const char *root) { mods_exefs_apply(base, sizeof img, 8, store_buffer); CHECK(img[0x28] == 0xAA, "two mods write the same byte: the top mod's byte stays"); CHECK(!memcmp(img + 0x10, "ABC", 3) && !img[0x30], "the other mods' data as each one stores it alone"); + const ModInfo *xm = by_name("X"), *ym = by_name("Y"); + CHECK(mods_conflict_count() == 1 && xm && ym && mods_conflict_at(0)->winner == ym->id && + mods_conflict_at(0)->loser == xm->id && mods_conflict_at(0)->part == MOD_PART_DATA && + mods_conflict_at(0)->count == 1, + "the conflict table: Y overrides X at one byte of data"); } mods_probe_cache(NULL, NULL); remove_tree(root); } -// A scan of mods with a code module the port cannot run, under mods_without_code. +// "mod_": the name tools/exefs_patches.py gives the patch set of the file at rel (mod folder name / path in it). +static void set_name(char *out, size_t cap, const char *rel) { snprintf(out, cap, "mod_%08x", fnv(rel, strlen(rel))); } + +// Code sites several mods patch (patches.c rt_patch_picks): a set table of the probe's own, with the sets of the ExeFS +// patch files of A (one file), B (two files) and C (one, switched off) and three contested sites: +// 0: A and B's first file the higher mod's set runs; a conflict of B over A or A over B +// 1: B's two files the later file in the mod runs; no conflict +// 2: C and A C is off, so A's runs; no conflict +static void probe_code(const char *root) { + remove_tree(root); + plat_file_mkdir_p(root); + const char *bid = *mods_main_build_id() ? mods_main_build_id() : "0"; + char a[160], b1[160], b2[160], c[160]; + snprintf(a, sizeof a, "A/exefs/%s.ips", bid); + snprintf(b1, sizeof b1, "B/atmosphere/exefs_patches/one/%s.ips", bid); + snprintf(b2, sizeof b2, "B/atmosphere/exefs_patches/two/%s.ips", bid); + snprintf(c, sizeof c, "C/exefs/%s.ips", bid); + const char *files[] = {a, b1, b2, c}; + static char names[5][16]; + static const char *const table[] = {"fps60", names[1], names[2], names[3], names[4]}; + char status[512] = ""; + for (int i = 0; i < 4; i++) { + put(root, files[i]); + set_name(names[i + 1], sizeof names[i + 1], files[i]); + size_t at = strlen(status); + snprintf(status + at, sizeof status - at, "%08X\t%08X\tcode\t-\t\n", fnv(files[i], strlen(files[i])), + fnv("probe", 5)); + } + static const int16_t s0[] = {1, 2}, s1[] = {2, 3}, s2[] = {4, 1}; + static const RtPatchPick picks[] = {{"main", 0x100, s0, 2}, {"main", 0x200, s1, 2}, {"main", 0x300, s2, 2}}; + mods_probe_cache(status, ""); + cfg_set("TOTK_MODS_DIR", root); + cfg_set("TOTK_MODS_DISABLED", "C"); + for (int round = 0; round < 2; round++) { + cfg_set("TOTK_MODS_ORDER", round ? "A|B" : "B|A"); + mods_rescan(); + mods_scan(); + rt_patch_probe_table(table, 5, picks, 3); + mods_exefs_apply(0, 0, 0, NULL); + const ModInfo *am = by_name("A"), *bm = by_name("B"); + CHECK(am && bm && am->nsets == 1 && bm->nsets == 2 && am->problem == MOD_OK, "code scan: A one set, B two"); + if (!am || !bm) break; + const ModInfo *hi = round ? am : bm, *lo = round ? bm : am; + CHECK(rt_patch_pick_winner(0) == (round ? 0 : 1), round ? "A above B: A's code runs at their site" + : "B above A: B's code runs at their site"); + CHECK(rt_patch_pick_winner(1) == 1, "two files of one mod on a site: the later file's code runs"); + CHECK(rt_patch_pick_winner(2) == 1 && !rt_patch_enabled(4), "a switched-off mod's set stays off"); + CHECK(mods_conflict_count() == 1 && mods_conflict_at(0)->winner == hi->id && + mods_conflict_at(0)->loser == lo->id && mods_conflict_at(0)->part == MOD_PART_CODE && + mods_conflict_at(0)->count == 1, + "the conflict table: the higher mod overrides the lower one at one code site, and only there"); + // the winner switched off at run time: the site takes the other mod's set + rt_patch_switch(round ? 1 : 2, 0); + CHECK(rt_patch_pick_winner(0) == (round ? 1 : 0), "a contested site follows a switch of its winner"); + } + // the table itself: one entry per pair and part, counts adding up + mods_conflict(7, 8, MOD_PART_FILES, 2); + mods_conflict(7, 8, MOD_PART_FILES, 3); + const ModConflict *last = mods_conflict_at(mods_conflict_count() - 1); + CHECK(last && last->winner == 7 && last->count == 5, "mods_conflict adds to the pair's entry"); + rt_patch_probe_table(NULL, 0, NULL, 0); + cfg_set("TOTK_MODS_DISABLED", ""); + mods_probe_cache(NULL, NULL); + remove_tree(root); +} + +// Mods with code the port cannot run (a code module, an ExeFS patch file it cannot apply): used without that code +// by their own switch (mods_no_code) or by mods_without_code for every mod. static void probe_without_code(const char *root) { remove_tree(root); plat_file_mkdir_p(root); + const char *bid = *mods_main_build_id() ? mods_main_build_id() : "0"; + char u[160], v[160]; + snprintf(u, sizeof u, "U/exefs/%s.ips", bid); // with RomFS files: the rest is usable + snprintf(v, sizeof v, "V/exefs/%s.ips", bid); // nothing else static const char *const files[] = {"E/exefs/subsdk9", "H/romfs/h.bin", "H/exefs/subsdk9", "I/romfs/i.bin", - "I/exefs/main"}; + "I/exefs/main", "U/romfs/u.bin"}; for (size_t i = 0; i < sizeof files / sizeof *files; i++) put(root, files[i]); + put(root, u); + put(root, v); + char status[256]; + snprintf(status, sizeof status, "%08X\t%08X\tunsupported\t-\tit writes outside main\n" + "%08X\t%08X\tunsupported\t-\tit writes outside main\n", fnv(u, strlen(u)), fnv("probe", 5), + fnv(v, strlen(v)), fnv("probe", 5)); + mods_probe_cache(status, ""); + cfg_set("TOTK_MODS_NO_CODE", "H|U|V"); + cfg_set("TOTK_MODS_DIR", root); + cfg_set("TOTK_MODS_ORDER", ""); + cfg_set("TOTK_MODS_DISABLED", ""); + mods_rescan(); + mods_scan(); + { + ModInfo *h = by_name("H"), *im = by_name("I"), *um = by_name("U"), *vm = by_name("V"); + CHECK(h && im && um && vm, "per-mod scan: four mods"); + if (h && im && um && vm) { + CHECK(h->problem == MOD_OK && h->no_code && EQ(h->code, "subsdk9"), + "its own switch: RomFS + subsdk9 is used without the code module"); + CHECK(im->problem == MOD_PLUGIN && !im->no_code, "a mod without the switch stays off"); + CHECK(um->problem == MOD_OK && um->unsupported == 1 && um->note[0], + "its own switch: RomFS + an unsupported patch file is used without that file"); + CHECK(vm->problem == MOD_EXEFS_UNSUPPORTED, "an unsupported patch file and nothing else stays off"); + int ii = 0; + while (mods_at(ii) != im) ii++; + mods_set_no_code(ii, 1); + CHECK(EQ(cfg_get("TOTK_MODS_NO_CODE"), "H|I|U|V") && mods_changed(), "the switch writes mods_no_code"); + } + } + cfg_set("TOTK_MODS_NO_CODE", ""); + cfg_set("TOTK_MODS_WITHOUT_CODE", "0"); + mods_rescan(); + mods_scan(); + CHECK(by_name("U") && by_name("U")->problem == MOD_EXEFS_UNSUPPORTED, "without the switch an unsupported file " + "keeps the whole mod off"); + remove_tree(root); + plat_file_mkdir_p(root); + for (size_t i = 0; i < 5; i++) put(root, files[i]); mods_probe_cache("", ""); cfg_set("TOTK_MODS_DIR", root); cfg_set("TOTK_MODS_ORDER", ""); @@ -151,7 +265,8 @@ static void probe_without_code(const char *root) { mods_rescan(); mods_scan(); h = by_name("H"); - CHECK(h && h->problem == MOD_PLUGIN, "without mods_without_code a mod with a code module stays off"); + CHECK(h && h->problem == MOD_PLUGIN && !h->without_code, "without mods_without_code a mod with a code module stays " + "off"); cfg_set("TOTK_MODS_WITHOUT_CODE", ""); mods_probe_cache(NULL, NULL); remove_tree(root); @@ -246,6 +361,7 @@ int probe_mods(void) { } probe_data(root); probe_without_code(root); + probe_code(root); printf("mods: %s\n", fails ? "FAIL" : "ok"); int table = restbl_probe(); return fails || table ? 1 : 0; diff --git a/runtime/patches.c b/runtime/patches.c index a54f9c36..6279bbf5 100644 --- a/runtime/patches.c +++ b/runtime/patches.c @@ -8,6 +8,12 @@ // code reaches it with one adrp, as MacNativeGames' data page after main (games/totk/runtime/game_patches.c). Its // layout belongs to the sets that use it (frame_rate.c: +0 the 30 Hz ratio, +4 the frame step in seconds, +8 the // step in 60 Hz ticks). +// +// Contested sites: a site that two or more ordered sets patch (the mods' ExeFS patch files, tools/exefs_patches.py) +// tests one byte, rt_patch_pick[site], instead of each set's switch. rt_patch_picks fills it from the sets that are on +// and the ranks their owner gave them (mods.c: the Mods tab's position), so the load order decides which mod's code +// runs there, as it decides RomFS files and data writes. Generic over what owns a set: a part kind that patches code +// (stage 2's plugin hooks) ranks its sets the same way. // Probe modes never call rt_patches_init: every set stays off. #include "cfg.h" #include "platform/platform.h" @@ -24,8 +30,12 @@ #ifndef RT_PATCH_COUNT #define RT_PATCH_COUNT 0 #endif +#ifndef RT_PATCH_PICK_COUNT +#define RT_PATCH_PICK_COUNT 0 +#endif uint8_t rt_patch_on[RT_PATCH_COUNT > 0 ? RT_PATCH_COUNT : 1]; // read by the generated C at every patched site +uint8_t rt_patch_pick[RT_PATCH_PICK_COUNT > 0 ? RT_PATCH_PICK_COUNT : 1]; // read at every contested site #if RT_PATCH_COUNT static const struct { const char *name, *env; int on; } sets[] = { @@ -35,6 +45,26 @@ static const struct { const char *name, *env; int on; } sets[] = { }; static void *page; #endif +#if RT_PATCH_PICK_COUNT +#define X(i, m, o, ...) static const int16_t pick_sets_##i[] = {__VA_ARGS__}; +RT_PATCH_PICKS(X) +#undef X +static const RtPatchPick gen_picks[] = { +#define X(i, m, o, ...) {m, o, pick_sets_##i, (int)(sizeof pick_sets_##i / sizeof *pick_sets_##i)}, + RT_PATCH_PICKS(X) +#undef X +}; +#else +static const RtPatchPick *const gen_picks = NULL; +#endif +static uint16_t gen_rank[RT_PATCH_COUNT > 0 ? RT_PATCH_COUNT : 1]; // rank + 1; 0 unranked + +// The table in use: the generated one, or --probe-mods' own (rt_patch_probe_table). +static int nset = RT_PATCH_COUNT, npick = RT_PATCH_PICK_COUNT; +static uint8_t *on = rt_patch_on, *pick = rt_patch_pick; +static uint16_t *rank = gen_rank; +static const RtPatchPick *picks = gen_picks; +static const char *const *probe_names; void rt_patches_init(void) { #if RT_PATCH_COUNT @@ -53,26 +83,82 @@ void rt_patches_init(void) { if (n >= sizeof list) n = sizeof list - 1; } rt_log("patches: %s (page %#llx)", list, RT_PATCH_PAGE); + rt_patch_picks(); // a set the environment switched on runs at its sites, unranked +#endif +} + +const char *rt_patch_name(int set) { + if (set < 0 || set >= nset) return NULL; + if (probe_names) return probe_names[set]; +#if RT_PATCH_COUNT + return sets[set].name; +#else + return NULL; #endif } int rt_patch_set(const char *name) { -#if RT_PATCH_COUNT - for (int i = 0; i < RT_PATCH_COUNT; i++) - if (!strcmp(sets[i].name, name)) return i; -#endif - (void)name; + for (int i = 0; i < nset; i++) + if (!strcmp(rt_patch_name(i), name)) return i; return -1; } -int rt_patch_enabled(int set) { return set >= 0 && set < RT_PATCH_COUNT && rt_patch_on[set]; } +int rt_patch_enabled(int set) { return set >= 0 && set < nset && on[set]; } -void rt_patch_disable(int set) { - if (set >= 0 && set < RT_PATCH_COUNT) rt_patch_on[set] = 0; +// The place in p's sets of the one that runs: on, the lowest rank, a tie to the later one; -1 when none is on. +static int winner(const RtPatchPick *p) { + int best = -1; + unsigned best_key = 0; + for (int j = 0; j < p->nsets; j++) { + int s = p->sets[j]; + if (s < 0 || s >= nset || !on[s]) continue; + unsigned key = rank[s] ? rank[s] : 0x10000u; + if (best < 0 || key <= best_key) best = j, best_key = key; + } + return best; } -void rt_patch_switch(int set, int on) { - if (set >= 0 && set < RT_PATCH_COUNT) __atomic_store_n(&rt_patch_on[set], (uint8_t)(on != 0), __ATOMIC_RELAXED); +// The picks of the contested sites set touches (every site for -1). +static void repick(int set) { + for (int i = 0; i < npick; i++) { + int has = set < 0; + for (int j = 0; !has && j < picks[i].nsets; j++) has = picks[i].sets[j] == set; + if (has) __atomic_store_n(&pick[i], (uint8_t)(winner(&picks[i]) + 1), __ATOMIC_RELAXED); + } +} + +void rt_patch_disable(int set) { + if (set >= 0 && set < nset) on[set] = 0; + repick(set); +} + +void rt_patch_switch(int set, int value) { + if (set < 0 || set >= nset) return; + __atomic_store_n(&on[set], (uint8_t)(value != 0), __ATOMIC_RELAXED); + repick(set); +} + +void rt_patch_rank(int set, int r) { + if (set >= 0 && set < nset) rank[set] = (uint16_t)(r < 0 ? 1 : r > 0xFFFE ? 0xFFFF : r + 1); +} + +void rt_patch_picks(void) { repick(-1); } +int rt_patch_pick_count(void) { return npick; } +const RtPatchPick *rt_patch_pick_at(int i) { return i >= 0 && i < npick ? &picks[i] : NULL; } +int rt_patch_pick_winner(int i) { return i >= 0 && i < npick ? (int)__atomic_load_n(&pick[i], __ATOMIC_RELAXED) - 1 : -1; } + +void rt_patch_probe_table(const char *const *names, int n, const RtPatchPick *p, int np) { + if (probe_names) { + free(on); + free(pick); + free(rank); + } + probe_names = NULL; + nset = RT_PATCH_COUNT, npick = RT_PATCH_PICK_COUNT; + on = rt_patch_on, pick = rt_patch_pick, rank = gen_rank, picks = gen_picks; + if (!names) return; + on = calloc((size_t)n + 1, 1), pick = calloc((size_t)np + 1, 1), rank = calloc((size_t)n + 1, sizeof *rank); + probe_names = names, nset = n, npick = np, picks = p; } void *rt_patch_page(void) { diff --git a/runtime/romfs.c b/runtime/romfs.c index 65968259..2a024e36 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -467,7 +467,8 @@ static const char *owner_name(const Overlay *o) { return o->mod ? mods_at(o->mod - 1)->name : o->prio ? "romfs_mods" : "the port's patches"; } -// One overlay per path: the highest prio serves it; the others are dropped and counted on their mod. +// One overlay per path: the highest prio serves it; the others are dropped and counted on their mod, and a file one +// mod takes from another goes into the conflict table (mods_conflict). static void overlay_contest(void) { if (!noverlay) return; qsort(overlays, noverlay, sizeof *overlays, contest_order); @@ -478,7 +479,7 @@ static void overlay_contest(void) { if (o->mod) { ModInfo *m = mods_at(o->mod - 1); m->overridden++; - if (!m->overridden_by[0]) snprintf(m->overridden_by, sizeof m->overridden_by, "%s", owner_name(w)); + if (w->mod) mods_conflict(mods_at(w->mod - 1)->id, m->id, MOD_PART_FILES, 1); } else if (w->mod) { rt_log("RomFS overlay: %s from mod %s replaces the one of %s", o->name, owner_name(w), owner_name(o)); } @@ -509,10 +510,10 @@ static void mods_overlay_log(void) { for (int k = 0; k < mods_count(); k++) { const ModInfo *m = mods_at(k); if (!m->enabled || !(m->kind & MOD_ROMFS) || m->problem != MOD_OK) continue; - rt_log("mods: %s: %u files served (%u replaced, %u added), %u overridden%s%s, %u for another game version", - m->name, m->replaced + m->added, m->replaced, m->added, m->overridden, m->overridden ? " by " : "", - m->overridden_by, m->other_version); + rt_log("mods: %s: %u files served (%u replaced, %u added), %u overridden, %u for another game version", + m->name, m->replaced + m->added, m->replaced, m->added, m->overridden, m->other_version); } + mods_conflicts_log(MOD_PART_FILES); } static int overlay_order(const void *a, const void *b) { diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index f9543f06..921386c4 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -1241,10 +1241,26 @@ static void tab_mods(float scale) { const char *after = mods_changed() ? menu_text(UI_MENU_AFTER_RESTART) : nullptr; for (int i = 0; i < n; i++) { const ModInfo *m = mods_at(i); - char id[32], desc[1536]; + char id[32], desc[4096]; snprintf(id, sizeof id, "##mod%d", i); - char over[512] = "", other[512] = ""; - if (m->overridden) snprintf(over, sizeof over, menu_text(UI_MENU_MODS_OVERRIDDEN), m->overridden, m->overridden_by); + char over[2048] = "", other[512] = ""; + // what it won or lost to another mod, files, data and code alike (the conflict table, as at start) + static const UiTextId lost[MOD_PARTS] = {UI_MENU_MODS_OVERRIDDEN, UI_MENU_MODS_DATA_OVERRIDDEN, + UI_MENU_MODS_CODE_OVERRIDDEN}, + won[MOD_PARTS] = {UI_MENU_MODS_OVERRIDES_FILES, UI_MENU_MODS_OVERRIDES_DATA, + UI_MENU_MODS_OVERRIDES_CODE}; + for (int c = 0; c < mods_conflict_count(); c++) { + const ModConflict *e = mods_conflict_at(c); + bool lose = e->loser == m->id; + if (!lose && e->winner != m->id) continue; + const ModInfo *them = mods_by_id(lose ? e->winner : e->loser); + if (!them) continue; + size_t at = strlen(over); + if (at) at += (size_t)snprintf(over + at, sizeof over - at, "\n\n"); + if (at < sizeof over) + snprintf(over + at, sizeof over - at, menu_text(lose ? lost[e->part] : won[e->part]), e->count, + them->name); + } if (m->other_version) snprintf(other, sizeof other, menu_text(UI_MENU_MODS_OTHER_VERSION), m->other_version); char cheat[256] = ""; if (m->kind & MOD_CHEATS) { // its cheats as read at start, or the file they come from @@ -1254,9 +1270,11 @@ static void tab_mods(float scale) { if (count) snprintf(cheat, sizeof cheat, menu_text(UI_MENU_MODS_CHEATS), count); else snprintf(cheat, sizeof cheat, menu_text(UI_MENU_MODS_CHEATS_FILE), cheats_file()); } - char code[512] = "", problem[512] = "", skipped[512] = ""; - if (m->code[0] && m->problem != MOD_PLUGIN) // used without that code (mods_without_code = 1) + char code[512] = "", problem[512] = "", skipped[512] = "", left[512] = ""; + if (m->code[0] && m->problem != MOD_PLUGIN) // used without its code snprintf(skipped, sizeof skipped, menu_text(UI_MENU_MODS_CODE_SKIPPED), m->code); + if (m->unsupported && m->problem != MOD_EXEFS_UNSUPPORTED) + snprintf(left, sizeof left, menu_text(UI_MENU_MODS_EXEFS_SKIPPED), m->unsupported, m->note); if (m->nsets && m->overrides[0]) snprintf(code, sizeof code, menu_text(UI_MENU_MODS_REPLACES_BUILTIN), m->nsets, m->overrides); else if (m->nsets || m->ndata) @@ -1273,7 +1291,7 @@ static void tab_mods(float scale) { default: break; } int k = snprintf(desc, sizeof desc, menu_text(UI_MENU_MODS_ROW_DESC), m->replaced, m->added); - const char *parts[] = {over, other, cheat, code, skipped, problem, menu_text(UI_MENU_MODS_MOVE_HINT)}; + const char *parts[] = {over, other, cheat, code, skipped, left, problem, menu_text(UI_MENU_MODS_MOVE_HINT)}; for (const char *part : parts) if (*part && k >= 0 && k < (int)sizeof desc) k += snprintf(desc + k, sizeof desc - k, "\n\n%s", part); const char *note = m->enabled && m->problem != MOD_OK ? menu_text(UI_MENU_MODS_UNUSED) : after; @@ -1287,12 +1305,22 @@ static void tab_mods(float scale) { snprintf(id, sizeof id, "##mod%d", to); opt_focus_key(id); // the cursor follows the mod opt_save_soon(); + continue; // m is the moved mod's neighbour now; its switch row comes next frame + } + // under a mod with code the port cannot run (a code module, an ExeFS patch file it cannot apply): its switch + // "use without its code"; mods_without_code = 1 holds it on for every mod + if (m->code[0] || m->unsupported) { + char nid[32], label[320]; + snprintf(nid, sizeof nid, "##modnc%d", i); + snprintf(label, sizeof label, menu_text(UI_MENU_MODS_WITHOUT_CODE), m->name); + bool all = cfg_bool("TOTK_MODS_WITHOUT_CODE") != 0; + if (all) opt_disable_next(UI_MENU_MODS_WITHOUT_CODE_ALL); + if (opt_toggle_text(nid, label, menu_text(UI_MENU_MODS_WITHOUT_CODE_DESC), all || m->no_code) && !all) { + mods_set_no_code(i, !m->no_code); + opt_save_soon(); + } } } - // The switch for mods with their own code shows once a mod with a code module is there (or the switch is on) - bool code_mods = cfg_bool("TOTK_MODS_WITHOUT_CODE"); - for (int i = 0; i < n && !code_mods; i++) code_mods = mods_at(i)->code[0] != 0; - if (code_mods) opt_toggle("TOTK_MODS_WITHOUT_CODE", UI_MENU_MODS_WITHOUT_CODE, UI_MENU_MODS_WITHOUT_CODE_DESC); bool rebuild = cheats_rebuild_pending() && opt_button("##restart", UI_MENU_CHEATS_REBUILD, UI_MENU_CHEATS_REBUILD_DESC); static uint64_t open_failed; // plat_now_ns until which the button says it failed diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 2d6b1edc..17f33965 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -219,12 +219,12 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_MODS_ROW_DESC] = "Game files it replaces: %u. New files it adds: %u.", [UI_MENU_MODS_OVERRIDDEN] = "%u of its files come from %s, which is higher in the list.", [UI_MENU_MODS_MOVE_HINT] = "The top face button or Ctrl+Up moves it up, the left face button or Ctrl+Down moves it " - "down. When two mods change the same file, the higher one wins. Changes apply after a " - "restart.", + "down. When two mods change the same file, game data or code, the higher one wins. " + "Changes apply after a restart.", [UI_MENU_MODS_EMPTY] = "This folder has no romfs, exefs or cheats files.", [UI_MENU_MODS_SUBSDK] = "This mod brings its own game code (a subsdk plugin or a replaced main). The port cannot " - "load code modules, so the whole mod stays off. \"Use mods without their code\" below uses " - "the rest of it.", + "load code modules, so the whole mod stays off. Its switch \"Use ... without its code\" " + "below uses the rest of it.", [UI_MENU_MODS_EXEFS_LATER] = "This mod patches the game code (exefs). The game builds the patches in when it " "starts next (a rebuild of a few minutes); until then the whole mod stays off.", [UI_MENU_MODS_CHEATS] = "Cheats: %d. Switch them on the Cheats tab.", @@ -578,19 +578,30 @@ static const char *const text_en[UI_TEXT_COUNT] = { "builds them in (a few minutes) and starts the game again. Progress since your " "last save is lost.", [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "This mod patches the game code (exefs) in a way the port cannot apply, so the " - "whole mod stays off. %s", + "whole mod stays off. Its switch \"Use ... without its code\" below uses the " + "rest of it. %s", [UI_MENU_MODS_EXEFS_OTHER] = "Its code patches (exefs) are made for another game version.", [UI_MENU_MODS_EXEFS_ON] = "Code patches in use: %d files.", [UI_MENU_MODS_REPLACES_BUILTIN] = "Code patches in use: %d files. They replace the port's own patch %s, which " "stays off while this mod is on.", [UI_MENU_MODS_CODE_SKIPPED] = "Its own game code (%s) does not load: the rest of the mod is used. Switch the mod " "off if the game misbehaves with it.", - [UI_MENU_MODS_WITHOUT_CODE] = "Use mods without their code", - [UI_MENU_MODS_WITHOUT_CODE_DESC] = "A mod that brings its own game code (a subsdk plugin or a replaced main) " - "stays off as a whole, because the port cannot run that code. On: such a mod is " - "used without that code: its RomFS files, cheats and ExeFS patches load. Its " - "files may expect the missing code; switch the mod off if the game misbehaves. " - "Applies at the next start.", + [UI_MENU_MODS_WITHOUT_CODE] = "Use %s without its code", + [UI_MENU_MODS_WITHOUT_CODE_DESC] = "The mod above brings code the port cannot run: its own game code (a subsdk " + "plugin or a replaced main) or code patches (exefs) the port cannot apply. Off: " + "the whole mod stays off. On: that code is left out and the rest of the mod is " + "used: its RomFS files, cheats and the code patches that work. Its files may " + "expect the missing code; switch it off again if the game misbehaves. Applies " + "at the next start.", + [UI_MENU_MODS_WITHOUT_CODE_ALL] = "On for every mod: mods_without_code = 1 in totk.ini.", + [UI_MENU_MODS_EXEFS_SKIPPED] = "%u of its code patch files (exefs) cannot be applied and are left out: the rest of " + "the mod is used. %s", + [UI_MENU_MODS_OVERRIDES_FILES] = "%u of its files replace those of %s, which is lower in the list.", + [UI_MENU_MODS_DATA_OVERRIDDEN] = "%u bytes of its game data changes come from %s, which is higher in the list.", + [UI_MENU_MODS_OVERRIDES_DATA] = "%u bytes of its game data changes replace those of %s, which is lower in the " + "list.", + [UI_MENU_MODS_CODE_OVERRIDDEN] = "%u of its code patch sites are overridden by %s, which is higher in the list.", + [UI_MENU_MODS_OVERRIDES_CODE] = "It overrides %u code patch sites of %s, which is lower in the list.", }; static const char *const text_ru[UI_TEXT_COUNT] = { @@ -779,11 +790,12 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_MODS_ROW_DESC] = "Заменяет файлов игры: %u. Добавляет новых файлов: %u.", [UI_MENU_MODS_OVERRIDDEN] = "%u его файлов берутся из мода %s, он выше в списке.", [UI_MENU_MODS_MOVE_HINT] = "Верхняя кнопка или Ctrl+Вверх поднимает мод, левая кнопка или Ctrl+Вниз опускает. Если " - "два мода меняют один файл, побеждает верхний. Изменения действуют после перезапуска.", + "два мода меняют один файл, данные или код игры, побеждает верхний. Изменения действуют " + "после перезапуска.", [UI_MENU_MODS_EMPTY] = "В этой папке нет файлов romfs, exefs или cheats.", [UI_MENU_MODS_SUBSDK] = "Этот мод приносит свой код игры (плагин subsdk или заменённый main). Порт не загружает " - "модули кода, поэтому мод выключен целиком. «Моды без их кода» ниже включает остальную " - "часть мода.", + "модули кода, поэтому мод выключен целиком. Его переключатель «... без его кода» ниже " + "включает остальную часть мода.", [UI_MENU_MODS_EXEFS_LATER] = "Этот мод меняет код игры (exefs). Игра соберёт эти патчи при следующем запуске " "(пересборка на несколько минут); до тех пор мод выключен целиком.", [UI_MENU_MODS_CHEATS] = "Читов: %d. Они включаются на вкладке «Читы».", @@ -1141,19 +1153,29 @@ static const char *const text_ru[UI_TEXT_COUNT] = { "собирает их (несколько минут) и снова запускает игру. Прогресс после последнего " "сохранения будет потерян.", [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "Этот мод меняет код игры (exefs) так, как порт применить не может, поэтому мод " - "выключен целиком. %s", + "выключен целиком. Его переключатель «... без его кода» ниже включает остальную " + "часть мода. %s", [UI_MENU_MODS_EXEFS_OTHER] = "Его патчи кода (exefs) сделаны для другой версии игры.", [UI_MENU_MODS_EXEFS_ON] = "Патчи кода в работе: файлов: %d.", [UI_MENU_MODS_REPLACES_BUILTIN] = "Патчи кода в работе: файлов: %d. Они заменяют собственный патч порта %s, " "который выключен, пока этот мод включён.", [UI_MENU_MODS_CODE_SKIPPED] = "Его собственный код игры (%s) не загружается: используется остальная часть мода. " "Выключите мод, если игра с ним ведёт себя странно.", - [UI_MENU_MODS_WITHOUT_CODE] = "Моды без их кода", - [UI_MENU_MODS_WITHOUT_CODE_DESC] = "Мод со своим кодом игры (плагин subsdk или заменённый main) выключается " - "целиком, потому что порт не может запустить этот код. Вкл.: такой мод " - "используется без этого кода — загружаются его файлы RomFS, читы и патчи ExeFS. " - "Его файлы могут рассчитывать на отсутствующий код; выключите мод, если игра " - "ведёт себя странно. Применяется при следующем запуске.", + [UI_MENU_MODS_WITHOUT_CODE] = "%s без его кода", + [UI_MENU_MODS_WITHOUT_CODE_DESC] = "Мод выше приносит код, который порт не может запустить: свой код игры (плагин " + "subsdk или заменённый main) или патчи кода (exefs), которые порт не может " + "применить. Выкл.: мод выключен целиком. Вкл.: этот код пропускается, а " + "остальная часть мода используется — его файлы RomFS, читы и работающие патчи " + "кода. Его файлы могут рассчитывать на отсутствующий код; выключите снова, если " + "игра ведёт себя странно. Применяется при следующем запуске.", + [UI_MENU_MODS_WITHOUT_CODE_ALL] = "Включено для всех модов: mods_without_code = 1 в totk.ini.", + [UI_MENU_MODS_EXEFS_SKIPPED] = "Патчи кода (exefs), которые порт не может применить, пропущены (файлов: %u): " + "используется остальная часть мода. %s", + [UI_MENU_MODS_OVERRIDES_FILES] = "%u его файлов заменяют файлы мода %s, он ниже в списке.", + [UI_MENU_MODS_DATA_OVERRIDDEN] = "%u байт его изменений данных игры берутся из мода %s, он выше в списке.", + [UI_MENU_MODS_OVERRIDES_DATA] = "%u байт его изменений данных игры заменяют изменения мода %s, он ниже в списке.", + [UI_MENU_MODS_CODE_OVERRIDDEN] = "%u мест его патчей кода перекрыты модом %s, он выше в списке.", + [UI_MENU_MODS_OVERRIDES_CODE] = "Он перекрывает %u мест патчей кода мода %s, он ниже в списке.", }; static const char *const text_pt_br[UI_TEXT_COUNT] = { @@ -1175,11 +1197,12 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_MODS_ROW_DESC] = "Arquivos do jogo que substitui: %u. Arquivos novos que adiciona: %u.", [UI_MENU_MODS_OVERRIDDEN] = "%u dos seus arquivos vêm de %s, que está acima na lista.", [UI_MENU_MODS_MOVE_HINT] = "O botão de cima ou Ctrl+Cima sobe o mod, o botão da esquerda ou Ctrl+Baixo desce. " - "Quando dois mods mudam o mesmo arquivo, vale o de cima. As mudanças valem após reiniciar.", + "Quando dois mods mudam o mesmo arquivo, dado ou código do jogo, vale o de cima. As " + "mudanças valem após reiniciar.", [UI_MENU_MODS_EMPTY] = "Esta pasta não tem arquivos romfs, exefs ou cheats.", - [UI_MENU_MODS_SUBSDK] = "Este mod traz código próprio do jogo (um plugin subsdk ou um main substituído). O port não " - "carrega módulos de código, então o mod inteiro fica desligado. \"Usar mods sem o código " - "deles\", abaixo, usa o resto dele.", + [UI_MENU_MODS_SUBSDK] = "Este mod traz código próprio do jogo (um plugin subsdk ou um main substituído). O port " + "não carrega módulos de código, então o mod inteiro fica desligado. A chave \"Usar ... sem " + "o código dele\", abaixo, usa o resto dele.", [UI_MENU_MODS_EXEFS_LATER] = "Este mod altera o código do jogo (exefs). O jogo compila esses patches na próxima " "vez que iniciar (uma recompilação de alguns minutos); até lá o mod inteiro fica " "desligado.", @@ -1739,20 +1762,32 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { "jogo; o instalador os compila (alguns minutos) e inicia o jogo de novo. O " "progresso desde o último salvamento é perdido.", [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "Este mod altera o código do jogo (exefs) de um jeito que o port não consegue " - "aplicar, então o mod inteiro fica desligado. %s", + "aplicar, então o mod inteiro fica desligado. A chave \"Usar ... sem o código " + "dele\", abaixo, usa o resto dele. %s", [UI_MENU_MODS_EXEFS_OTHER] = "Os patches de código dele (exefs) são para outra versão do jogo.", [UI_MENU_MODS_EXEFS_ON] = "Patches de código em uso: %d arquivos.", [UI_MENU_MODS_REPLACES_BUILTIN] = "Patches de código em uso: %d arquivos. Eles substituem o patch do próprio port " "%s, que fica desligado enquanto este mod está ligado.", [UI_MENU_MODS_CODE_SKIPPED] = "O código próprio do jogo dele (%s) não é carregado: o resto do mod é usado. " "Desligue o mod se o jogo se comportar mal com ele.", - [UI_MENU_MODS_WITHOUT_CODE] = "Usar mods sem o código deles", - [UI_MENU_MODS_WITHOUT_CODE_DESC] = "Um mod que traz código próprio do jogo (um plugin subsdk ou um main " - "substituído) fica desligado por inteiro, porque o port não consegue rodar esse " - "código. Ligado: esse mod é usado sem esse código; os arquivos RomFS, cheats e " - "patches de ExeFS dele carregam. Os arquivos dele podem depender do código que " - "falta; desligue o mod se o jogo se comportar mal. Vale na próxima " + [UI_MENU_MODS_WITHOUT_CODE] = "Usar %s sem o código dele", + [UI_MENU_MODS_WITHOUT_CODE_DESC] = "O mod acima traz código que o port não consegue rodar: código próprio do jogo " + "(um plugin subsdk ou um main substituído) ou patches de código (exefs) que o " + "port não consegue aplicar. Desligado: o mod inteiro fica desligado. Ligado: " + "esse código fica de fora e o resto do mod é usado: os arquivos RomFS, cheats e " + "os patches de código que funcionam. Os arquivos dele podem depender do código " + "que falta; desligue de novo se o jogo se comportar mal. Vale na próxima " "inicialização.", + [UI_MENU_MODS_WITHOUT_CODE_ALL] = "Ligado para todos os mods: mods_without_code = 1 no totk.ini.", + [UI_MENU_MODS_EXEFS_SKIPPED] = "%u arquivos de patch de código (exefs) dele não podem ser aplicados e ficam de " + "fora: o resto do mod é usado. %s", + [UI_MENU_MODS_OVERRIDES_FILES] = "%u dos seus arquivos substituem os de %s, que está abaixo na lista.", + [UI_MENU_MODS_DATA_OVERRIDDEN] = "%u bytes das suas mudanças nos dados do jogo vêm de %s, que está acima na lista.", + [UI_MENU_MODS_OVERRIDES_DATA] = "%u bytes das suas mudanças nos dados do jogo substituem as de %s, que está abaixo " + "na lista.", + [UI_MENU_MODS_CODE_OVERRIDDEN] = "%u pontos dos seus patches de código são sobrepostos por %s, que está acima na " + "lista.", + [UI_MENU_MODS_OVERRIDES_CODE] = "Ele sobrepõe %u pontos dos patches de código de %s, que está abaixo na lista.", }; // Simplified Chinese: this row covers the software keyboard and the whole menu, so the menu and the overlay read Chinese @@ -1781,11 +1816,9 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_MODS_OPEN_DESC] = "显示游戏加载模组的文件夹。不存在时会创建它。", [UI_MENU_MODS_ROW_DESC] = "替换的游戏文件:%u。新增的文件:%u。", [UI_MENU_MODS_OVERRIDDEN] = "它的 %u 个文件改用列表中更靠上的 %s 的文件。", - [UI_MENU_MODS_MOVE_HINT] = "上方按钮或 Ctrl+上 上移,左侧按钮或 Ctrl+下 下移。两个模组修改同一个文件时,以靠上的为准。" - "更改在重启后生效。", + [UI_MENU_MODS_MOVE_HINT] = "上方按钮或 Ctrl+上 上移,左侧按钮或 Ctrl+下 下移。两个模组修改同一个文件、游戏数据或代码时,以靠上的为准。更改在重启后生效。", [UI_MENU_MODS_EMPTY] = "此文件夹中没有 romfs、exefs 或 cheats 文件。", - [UI_MENU_MODS_SUBSDK] = "此模组带有自己的游戏代码(subsdk 插件或替换的 main)。移植版无法加载代码模块,因此整个模组保持关闭。" - "打开下方的“不带代码使用模组”即可使用其余部分。", + [UI_MENU_MODS_SUBSDK] = "此模组带有自己的游戏代码(subsdk 插件或替换的 main)。移植版无法加载代码模块,因此整个模组保持关闭。打开下方它的“不带代码使用 ...”开关即可使用其余部分。", [UI_MENU_MODS_EXEFS_LATER] = "此模组修改游戏代码(exefs)。游戏会在下次启动时把这些补丁构建进去(重新构建需要几分钟);在此之前整个模组保持关闭。", [UI_MENU_MODS_CHEATS] = "金手指:%d 个。在“金手指”标签页中开关。", [UI_MENU_MODS_CHEATS_FILE] = "此游戏版本的金手指在启动时从此模组的 cheats\\%s 读取。", @@ -2420,15 +2453,20 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_CHEAT_UNSUPPORTED_DESC] = "此金手指不能用于此游戏版本:", [UI_MENU_CHEATS_REBUILD] = "立即重新构建游戏", [UI_MENU_CHEATS_REBUILD_DESC] = "有些金手指会修改游戏代码,但尚未构建进游戏:它们是新增或修改的,或上次重新构建失败或被取消。将关闭游戏;安装程序会把它们构建进去(几分钟)并重新启动游戏。上次存档之后的进度会丢失。", - [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "此模组修改游戏代码(exefs)的方式本移植无法应用,因此整个模组保持关闭。%s", + [UI_MENU_MODS_EXEFS_UNSUPPORTED] = "此模组修改游戏代码(exefs)的方式本移植无法应用,因此整个模组保持关闭。打开下方它的“不带代码使用 ...”开关即可使用其余部分。%s", [UI_MENU_MODS_EXEFS_OTHER] = "它的代码补丁(exefs)是为其他游戏版本制作的。", [UI_MENU_MODS_EXEFS_ON] = "正在使用的代码补丁:%d 个文件。", [UI_MENU_MODS_REPLACES_BUILTIN] = "正在使用的代码补丁:%d 个文件。它们取代本移植自带的补丁 %s,此模组开启时该补丁保持关闭。", [UI_MENU_MODS_CODE_SKIPPED] = "它自带的游戏代码(%s)不会加载:模组的其余部分照常使用。如果游戏因此表现异常,请关闭此模组。", - [UI_MENU_MODS_WITHOUT_CODE] = "不带代码使用模组", - [UI_MENU_MODS_WITHOUT_CODE_DESC] = "带有自己游戏代码(subsdk 插件或替换的 main)的模组会整体关闭,因为移植版无法运行这些代码。" - "开启后:此类模组在没有这些代码的情况下使用,其 RomFS 文件、金手指和 ExeFS 补丁照常加载。" - "它的文件可能依赖缺失的代码;如果游戏表现异常,请关闭该模组。下次启动时生效。", + [UI_MENU_MODS_WITHOUT_CODE] = "不带代码使用 %s", + [UI_MENU_MODS_WITHOUT_CODE_DESC] = "上方的模组带有移植版无法运行的代码:它自己的游戏代码(subsdk 插件或替换的 main),或移植版无法应用的代码补丁(exefs)。关闭:整个模组保持关闭。开启:跳过这些代码,使用模组的其余部分:其 RomFS 文件、金手指和可用的代码补丁。它的文件可能依赖缺失的代码;如果游戏表现异常,请再次关闭。下次启动时生效。", + [UI_MENU_MODS_WITHOUT_CODE_ALL] = "已对所有模组开启:totk.ini 中 mods_without_code = 1。", + [UI_MENU_MODS_EXEFS_SKIPPED] = "它有 %u 个代码补丁文件(exefs)无法应用,已跳过:模组的其余部分照常使用。%s", + [UI_MENU_MODS_OVERRIDES_FILES] = "它的 %u 个文件取代了列表中更靠下的 %s 的文件。", + [UI_MENU_MODS_DATA_OVERRIDDEN] = "它对游戏数据的修改中有 %u 字节改用列表中更靠上的 %s 的修改。", + [UI_MENU_MODS_OVERRIDES_DATA] = "它对游戏数据的修改中有 %u 字节取代了列表中更靠下的 %s 的修改。", + [UI_MENU_MODS_CODE_OVERRIDDEN] = "它有 %u 处代码补丁位置被列表中更靠上的 %s 覆盖。", + [UI_MENU_MODS_OVERRIDES_CODE] = "它覆盖了 %u 处代码补丁位置,这些位置原属于列表中更靠下的 %s。", }; #define SWKBD_ONLY(name, ...) static const char *const name[UI_TEXT_COUNT] = {SWKBD(__VA_ARGS__)} diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 142a92c0..2423b4dd 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -84,15 +84,24 @@ typedef enum { UI_MENU_MODS_NONE, // no mod folder yet: where to unpack one UI_MENU_MODS_OPEN, UI_MENU_MODS_OPEN_DESC, UI_MENU_MODS_ROW_DESC, // printf: files it replaces, files it adds (as of this start) - UI_MENU_MODS_OVERRIDDEN, // printf: files a higher mod serves instead, that mod's name + UI_MENU_MODS_OVERRIDDEN, // printf: files a higher mod serves instead, that mod's name (the conflict table) UI_MENU_MODS_MOVE_HINT, UI_MENU_MODS_EMPTY, UI_MENU_MODS_SUBSDK, UI_MENU_MODS_EXEFS_LATER, UI_MENU_MODS_BAD_NAME, UI_MENU_MODS_EXEFS_UNSUPPORTED, // printf: the file and why UI_MENU_MODS_EXEFS_OTHER, UI_MENU_MODS_EXEFS_ON, // printf: its ExeFS patch files in use UI_MENU_MODS_REPLACES_BUILTIN, // printf: its ExeFS patch files in use, the port's own patch sets they turn off - UI_MENU_MODS_CODE_SKIPPED, // printf: its code modules in exefs, which do not load (mods_without_code = 1) - UI_MENU_MODS_WITHOUT_CODE, UI_MENU_MODS_WITHOUT_CODE_DESC, // the TOTK_MODS_WITHOUT_CODE switch + UI_MENU_MODS_CODE_SKIPPED, // printf: its code modules in exefs, which do not load (used without its code) + UI_MENU_MODS_WITHOUT_CODE, // printf: the mod's name; its switch "use without its code" (TOTK_MODS_NO_CODE) + UI_MENU_MODS_WITHOUT_CODE_DESC, + UI_MENU_MODS_WITHOUT_CODE_ALL, // why that switch cannot be changed: TOTK_MODS_WITHOUT_CODE=1 + UI_MENU_MODS_EXEFS_SKIPPED, // printf: its ExeFS patch files left out (used without its code), the first's reason + // The conflict table (mods.h ModConflict), printf: the count, the other mod's name + UI_MENU_MODS_OVERRIDES_FILES, // files it serves instead of a lower mod's (UI_MENU_MODS_OVERRIDDEN: the reverse) + UI_MENU_MODS_DATA_OVERRIDDEN, // bytes of main's data a higher mod wrote over its own + UI_MENU_MODS_OVERRIDES_DATA, // bytes of main's data it wrote over a lower mod's + UI_MENU_MODS_CODE_OVERRIDDEN, // code sites where a higher mod's patch runs instead of its own + UI_MENU_MODS_OVERRIDES_CODE, // code sites where its patch runs instead of a lower mod's UI_MENU_MODS_OTHER_VERSION, // printf: files named for another game version, read only when asked for by name UI_MENU_MODS_UNUSED, // under the switch of a mod the port cannot use UI_MENU_MODS_CHEATS, // printf: its cheats as read at start diff --git a/tests/mods_romfs_check.py b/tests/mods_romfs_check.py index d5b36d2a..3434ad71 100644 --- a/tests/mods_romfs_check.py +++ b/tests/mods_romfs_check.py @@ -161,10 +161,13 @@ def main(): code, log = run(args.exe, mods, f'Top|Low|{UNI}|Case') check(code == 0, 'probe-fs passes with five mods (served bytes equal the winners\' files)', log) for line in ('mods: Top: 2 files served (1 replaced, 1 added), 0 overridden', - 'mods: Low: 1 files served (1 replaced, 0 added), 2 overridden by Top, 1 for another game version', - 'mods: Case: 1 files served (0 replaced, 1 added), 1 overridden by Top, 1 for another game version', - f'mods: {UNI}: 0 files served (0 replaced, 0 added), 1 overridden by Low', - 'mods: Lite: 1 files served (0 replaced, 1 added), 0 overridden'): + 'mods: Low: 1 files served (1 replaced, 0 added), 2 overridden, 1 for another game version', + 'mods: Case: 1 files served (0 replaced, 1 added), 1 overridden, 1 for another game version', + f'mods: {UNI}: 0 files served (0 replaced, 0 added), 1 overridden', + 'mods: Lite: 1 files served (0 replaced, 1 added), 0 overridden', + # the conflict table (runtime/mods.c mods_conflicts_log), each way + 'mods: Top: overrides Low at 2 files', 'mods: Low: 2 files overridden by Top', + 'mods: Top: overrides Case at 1 files', f'mods: {UNI}: 1 files overridden by Low'): check(line in log, line, log) check(p1case not in log, 'the case-flipped path is served as the RomFS path, not added', log) diff --git a/totk.ini.example b/totk.ini.example index d616f567..4f7bfa61 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -466,16 +466,23 @@ # to totk.exe. # mods_dir = # Load order, top first, names separated by |. The in-game menu (Mods) writes it; a mod not listed goes to the bottom. +# The higher mod wins whatever two mods both change: a file, bytes of the game's data, an instruction of its code. +# Read at start. # mods_order = # Mods switched off, names separated by |. # mods_disabled = +# Mods used without their code part, names separated by |: the Mods tab's switch "Use without its code". Such a +# mod's code modules (exefs/subsdk*, a replaced exefs/main) and the ExeFS patch files the port cannot apply are left out +# and the rest of it is used, where otherwise the whole mod stays off. Read at start. +# mods_no_code = # auto: when an enabled mod has cheats that change the game's code and the game was built without them, the game starts # the setup's rebuild at start (a few minutes) and starts again after it. off: the game starts with the code it has. # A rebuild that failed or was cancelled is not started again for the same cheats. Default: auto. # mods_rebuild = auto -# 1: a mod that brings its own game code (exefs/subsdk*, a replaced exefs/main), which the port cannot run, is used -# without that code: its RomFS files, cheats and ExeFS patch files load. Its files may expect the missing code. 0: such -# a mod stays off as a whole. Read at start. Default: 0. +# 1: every mod is used without the code part the port cannot run, as if mods_no_code named them all: its code modules +# (exefs/subsdk*, a replaced exefs/main) and the ExeFS patch files the port cannot apply are left out, its RomFS files, +# 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 # 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):