Skip to content

Community ExeFS patches (IPS, IPS32, .pchtxt) go into the rewrite: --exefs-patches - #2297

Closed
chayan-bit wants to merge 1 commit into
seedds:mainfrom
chayan-bit:up/exefs-patches
Closed

chayan-bit wants to merge 1 commit into
seedds:mainfrom
chayan-bit:up/exefs-patches

Conversation

@chayan-bit

Copy link
Copy Markdown
Contributor

What and why

Players use community ExeFS patches (60 fps, resolution, quality) on a Switch, Ryujinx and the yuzu family, and had no way to apply them here: a hand-patched NSO loses the rewriter's checks, and cheats refuse words the rewriter changed. --exefs-patches <folder> (EXEFS_PATCHES; DM_EXEFS_PATCHES for patch_nso.py) names a folder of the player's own IPS, IPS32 and .pchtxt files (Atmosphère's exefs_patches/<name>/<build ID>.ips layout or any other); each module (main, subsdks, NROs) takes those naming its build ID.

  • dm/tools/exefs_patches.py: the parsers, ported from Ryujinx's IPSPatcher.cs, IPSwitchPatcher.cs and MemPatch.cs (MIT). An NSO's offsets count its 0x100-byte header, an NRO's its start (as Atmosphère's ro patcher and Ryujinx's ApplyNroPatches). Where Ryujinx warns and goes on, this stops: a malformed file, a record in the NSO header or past the data segment, two records writing different bytes to one byte.
  • patch_nso.py: the records go in after every cut and before the scan, so patched words are rewritten like the game's own (x18 uses and svcs become sites, exclusive accesses get the ordering of e3ad825). A record over a cut, an observation gate, a frame step or stub site, or a site word written into a data block of the text, stops the rewrite with a [RED] line naming the patch. Patched words are in changed.tsv; <module>.exefs_patches.tsv lists files and records.
  • NSP script: the option, the setting, a console line per module, and the rewrite key covers the patch files. Without the option the output is unchanged.

Checked

  • Tests: dm/tools/tests/test_exefs_patches.py (19, synthetic NSO, NRO and patches built in the test: parsers, matching, conflicts, NRO offsets, a patch over a frame step site, patch_nso.py end to end incl. ldxr → ldaxr and a byte-identical output with an empty folder); every dm/tools/tests file on main and on this branch (the same 9 files fail on this Mac either way, from the local toolchain: .NET 8 vs 10, no swiftc, GNU stat, a global commit hook); ruff and pyright 1.1.414 add no findings.
  • Games run: none. No patch or game file is in the change or its tests.
  • Devices and OS: Apple-silicon Mac, macOS 27.
  • regress.py check --games: not run. Without the option the rewriter's output is byte-identical (tested), so no game changes unless the option is given.

Docs

  • docs/development.md: an "ExeFS patches" section and an example under "The packing script in detail"; docs/env_switches.tsv row for DM_EXEFS_PATCHES
  • A chain tool changed (no)
  • No game files, keys or data extracted from a game in the change or the attachments

Scope discussion: #2294

Demo

exefs-patches

… of the NSP script (EXEFS_PATCHES, or DM_EXEFS_PATCHES for patch_nso.py) names a folder of the player's own IPS, IPS32 and .pchtxt files, in Atmosphère's exefs_patches/<name>/<build ID>.ips layout or any other, and each module (main, the subsdks, the NROs) takes those that name its build ID. Players use these patches for 60 fps, resolution and quality mods on a Switch, Ryujinx and the yuzu family, and until now had no way to apply them here: a patched NSO written by hand would lose the rewriter's checks, and Atmosphère cheats (hle_cheats.c) refuse to write over words the rewriter changed.

dm/tools/exefs_patches.py reads the three formats, its parsers ported from Ryujinx's IPSPatcher.cs, IPSwitchPatcher.cs and MemPatch.cs (MIT): RLE records, @Flag offset_shift, @enabled / @disabled sections, quoted strings with C escapes, @Stop. An NSO's offsets count its 0x100-byte header and an NRO's count from its start, as Atmosphère's ro patcher and Ryujinx's ApplyNroPatches apply them. Where Ryujinx warns and goes on, this stops: a malformed file, a record in the NSO header or past the data segment, and two records that write different bytes to the same byte.

patch_nso.py writes the records after every cut (registered, function-matched and the engine finders') and before its scan, so a patched word is rewritten like the game's own: an x18 use or an svc goes through its site, and an exclusive load or store gets the ordering of e3ad825. A record over a cut, an observation gate (registered, or the address region, StandardAllocator and Unreal ones found later), a frame step or stub site (checked before the stub asserts its original bytes, so the line names the patch), or the engine region's size mov stops the rewrite with a [RED] line, and so does a word the scan rewrites (x18, svc, a system register, sub sp) written into a data block of the text, which the scan leaves alone. Patched words land in changed.tsv like any other changed word, so a cheat does not write over them. <module>.exefs_patches.tsv lists the files and records, the NSP script names on the console what each module took, and its rewrite key covers the patch files by name and content. Without the option nothing changes: the same image, cuts and sites (build_src.sha256 lists exefs_patches.py).

Checked: test_exefs_patches (19 tests: the parsers, matching and conflicts, and patch_nso.py on a synthetic NSO and NRO built in the test: a patched mov x1, x18 and svc become sites and give the same output as the same words in the file, a patched ldxr becomes ldaxr, an NRO's records land at their own offsets, an x18 word in a data block of the text stops the rewrite while the same word in ro is written, a record in the header or over a registered cut or a frame step site stops it with a [RED] line, an empty folder changes nothing), and every dm/tools/tests file before and after: the same 9 files fail on this Mac either way, from its toolchain (.NET 8 against the projects' .NET 10, no swiftc, GNU stat, a global commit hook), and test_docs and test_env_switches pass. No game was patched: no patch or game file is in this change, and the tests use none.
Copilot AI balanced review requested due to automatic review settings November 11, 2026 07:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added rewriter The NSO rewriter and per-build patches packing Building an IPA or a Mac app: the NSP script, extraction, the shader split, the packers tools Tools and scripts (dm/tools, scripts) docs Documentation labels Nov 11, 2026
@roandegraaf

Copy link
Copy Markdown
Contributor

Overlaps #2150 (open since 2026-11-10, stacked on #2149). Both send a mod's .ips / .pchtxt through patch_nso.py so patched words are rewritten like the game's own, and both refuse a patch over a cut, a frame step site or an observation gate. They differ in when it runs and what parses:

this PR #2149 + #2150
When at build time, --exefs-patches <folder> at launch: #2149 applies what it can at load; #2150's mods_rewrite.py runs patch_nso.py with DM_MOD_PATCHES for the rest, cached by digest
Patches from one folder the mods folder (DM_MODS), so on/off and order follow order.txt and the Mods menu
Parser Python port of Ryujinx's (exefs_patches.py) the host's (hle_mods.c); the tool asks the host through DM_MODS_CHECK, with no second copy
Modules main, subsdks, NROs main, sdk, subsdks; no NROs
Wired up NSP script option and setting not yet: nothing in the repo calls the tool

If both land, there are two env names for the same rewriter step (DM_EXEFS_PATCHES / DM_MOD_PATCHES) and two IPS / .pchtxt parsers that can disagree on edge cases. Suggestion: one entry point in patch_nso.py, with one env name and one record format. This PR's NSP script wiring and NRO support stay, and #2150's launch-time tool calls that same entry point. I'll rebase #2150 onto whichever form we settle on.

@seedds

seedds commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Thanks @chayan-bit for porting the IPS, IPS32, and .pchtxt parsers and adding thorough synthetic tests!

Closing this PR as superseded by #3158, which is actively kept up to date on current main and handles IPS, IPS32, and .pchtxt mod patches both at load time and baked into the rewriter before rewriting, tested on Ocarina of Time. We checked both PRs against current main.

Still open: reviewing and landing #3158, and keeping the synthetic parser edge cases from this PR in mind if any coverage gaps come up.

@seedds seedds closed this Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation packing Building an IPA or a Mac app: the NSP script, extraction, the shader split, the packers rewriter The NSO rewriter and per-build patches tools Tools and scripts (dm/tools, scripts)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants