Skip to content

Cheats that check the game's code before they write and keep working after an update - #677

Open
davdxpx wants to merge 14 commits into
igawa6:mainfrom
davdxpx:mncf-cheats
Open

davdxpx wants to merge 14 commits into
igawa6:mainfrom
davdxpx:mncf-cheats

Conversation

@davdxpx

@davdxpx davdxpx commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Today a cheat file is made for one build of a game. When the game is updated, its cheats either stop or, worse, write the bytes they were made for into places that now hold other code, which can crash the game or break a save. Nothing checks what is there before a cheat writes, a cheat that cannot run gives no reason, and a number inside a code cheat (an amount, a speed) can only be changed by editing hex.

This PR keeps every cheat a player adds as one file of the Mistel Native Cheat Format (.mncf, an open format under the MIT License, spec). An Atmosphère, Eden, Yuzu or Ryujinx file (a zip, a text file, a folder) is converted as it is added, using the game's own code from the pack. What that gives players:

  • Checked before every write. Each cheat that changes the game's code names the original code at its place, taken from the player's own game. Peanuts compares it before it writes, so a cheat never writes into code it was not made for.
  • Ready for game updates. A cheat finds its place by the code around it, not by a fixed address, so after an update it keeps working wherever that code is still there. One file holds every version of the game.
  • Says why. A cheat that cannot work here is listed, off, with the reason in a sentence ("Its place was not found in this version of the game", "It runs once when you turn it on, which Peanuts cannot do yet").
  • Numbers you can set, in code too. A number inside an instruction (movz) becomes a field in the game's Mods tab, with its range and unit.
  • Descriptions, warnings, relations. A cheat can say what it does, that it changes the save or may crash the game, which cheats it needs and which it cannot be on with.
  • Export that carries your setup, or goes to any emulator. Export asks how: the Mistel cheat file, recommended (.mncf, or an .mncp pack for several sources), keeps every cheat for every version with the ones that are on and the numbers set, so another device starts the same way. Or a ZIP in Atmosphère's folders that every other emulator reads, one cheat file for each version; the dialog says before anything is written which cheats an Atmosphère file cannot hold, and why.

Nothing changes in the host's cheat engine (Parts A and B): each file is made ready for the build that runs and handed to the engine as Atmosphère code, word for word the same code where the file has it.

Builds on main 0.1.8, which has the mod menu (#436 to #441), the user saves (#432, #442) and the game page (#445 to #447) this PR extends; the diff shows only its own commits.

1. Adding cheats

An .mncf: the file and what it brings After adding: what works on this version
02-add-mncf 03-added-mncf
An Atmosphère zip: it becomes a Mistel cheat file After converting: what the conversion made of it
04-add-atmosphere 05-added-converted
  • Add takes .mncf and .mncp besides .zip, .txt and folders. An .mncf is kept byte for byte, so a signature stays good. Only a file signed by a key Peanuts trusts gets a mark ("Signed by ..."); an unsigned file is shown as any other. Peanuts trusts no key yet.
  • The dialog of an .mncf counts what the file brings from the file itself: its cheats and versions, how many check the code before they write, how many follow game updates, the numbers to set, the ones that say what they do.
  • An Atmosphère file becomes one .mncf for all of its builds, with the game, each build's version, where it came from (the zip's name, the site its credits name) and its authors: the people its credits name, else the name of the player who adds it (Settings › Users), else none. Lines of the file that are not code are kept in its description. The wait says what is being done: with a large game, finding each cheat's place in the game's code takes a while (about 30 s for a 97 MB main on the AYN Thor).
  • After adding, a sheet says how many of the cheats work on the game's version and how many cannot, in place of a toast.

2. The list and the reasons

Mods & Cheats: Export beside Add Not possible here, Changes your save, Hold ZL Why one cannot work here
01-area-export-beside-add 06-list-with-reasons 07-why-not-here
  • Export sits beside Add in the area's bar while there is a source to export.
  • Every cheat of a source is listed. One that cannot work here has "Not possible here", and its details and the second screen say why. A source none of whose cheats is for this version is shown as for another version, as before.
  • touches_save and may_crash show as chips. Switching a cheat on switches on what it requires and off what it cannot be on with (its excludes and its radio), on the page and in the game's tab.

3. Export

Export: the Mistel cheat file, recommended, or a ZIP The ZIP picked: what goes in What stays out, and why
09-export-dialog 10-export-zip 11-export-zip-left-out
  • Export opens one dialog with two ways. Mistel cheat file has the Recommended badge and is picked: one .mncf, or an .mncp for several sources, with every cheat for every version, the ones that are on as default = true and the numbers set as their values' defaults.
  • ZIP for other emulators: atmosphere/contents/<title ID>/cheats/<build ID>.txt, one file for each version, as Atmosphère, Eden, Yuzu and Ryujinx read them. Each source is lowered with Mistel's Converter.toAtmosphereAll for every build its file names, with the game's own main for the build that runs: its patterns are found there, and every patch and override stub is checked against expect before it is written, so the ZIP never writes over other code. A build the game runs that the file does not name is written from the patterns with that main. Each cheat is the file's own code for that build, else code made from its operations, checked as the evaluator checks them, the pointer at the base of a pointer path too; one with a number set in the game's tab is made from its operations with that number, in the same pass as the others (the converter's preferCheats).
  • What an Atmosphère file cannot hold stays out of the ZIP: a monitor, toggle buttons, a cheat that runs once or for a time, several conditions, a place found by a pattern in a version whose main Peanuts does not have, and a cheat that does not fit the game (its place not found, other code at it). Picking the ZIP shows, before anything is written, how many cheats go in for which version, and calmly how many and which stay out, each with its reason in a sentence, from the converter's stable reason (spec section 15); the Mistel cheat file keeps them. A cheat Peanuts itself cannot run yet but Atmosphère code can say (code Peanuts' rewriter changed, raw code its host does not run) goes in. The ZIP the dialog worked out is the one written, so the game's main is read once.
  • Each file keeps Atmosphère's limits, past which a Switch refuses the whole file (spec section 15, and for size and names Atmosphère's own reader): one master code of at most 256 words for the masters of every source (a master past that stays out, and with it the cheats of its source that need it), at most 127 other entries, group entries counted, and less than 64 KB in all, with each name cut to the 63 bytes Atmosphère keeps. A group starts with its name and ends with an entry of only 20000001 where a cheat in no group follows, as Mistel's converter writes and reads them; where those entries would cost cheats, the file has no groups. A source's names stay as its file has them, with the source's name after a name an earlier source has already. Whatever does not fit is named in the dialog. Peanuts' own engine is not held to these limits: it runs every cheat of a source, however many.

4. In the game

A value of the file as a number field, with its unit
08-ingame-number-field
  • An .mncf cheat with one whole-number value gets Part F's field, with the file's range, step and unit; that works for a number in an instruction too. The number is kept for the next start and goes to the running game as before (the merged file's block, then Part B's reload).

5. How a file runs

  • MncfMain reads the game's own main from the pack: main.patched.bin with the original words of main.orig.bin back at the runs of main.changed.tsv (a pack without orig.bin keeps those runs out of every search).
  • MncfBuild makes a source ready for the build that runs with Mistel's evaluator: each pattern is searched once and the place written into a copy of the file (build/<build ID>.mncf beside the source), with each cheat's state. A later start, the page and the game's tab read that copy without reading main again. The runs Peanuts' rewriter changed are given to the evaluator, which refuses a patch over one (runtime:rewritten, section 11.3) before the game starts.
  • The cheats that can run become Atmosphère code: their own code for this build where the file has it and the evaluator uses it, else code made from their operations. The parts of the format Atmosphère code cannot say (a cheat that runs once or for a time, a toggle combination, several conditions) are refused with the spec's runtime:unsupported:<part> (section 11.1), a monitor with capability-missing:display, each listed with its reason until Peanuts runs these itself.
  • Sources an earlier build of the mod menu kept as Atmosphère files, and upstream's imported/ files, become .mncf at the next start or when the page opens, with the game's main.

Mistel's library

  • The reader, the evaluator, the converter and the writer are Mistel's MIT library (format/lib/java, Java 8, no dependencies), copied unchanged into android/app/src/main/java/io/github/davdxpx/mistel/ with its MIT notice and the commit it came from (MISTEL.sha256). The copy is of Mistel's main (76d283c), with Converted cheats keep working on new builds, and messy cheat files convert instead of failing davdxpx/mistel#11 and Can't hold controller inputs #16, which close the gaps this PR found in the spec and the library: runtime:unsupported:<part> and runtime:rewritten in the evaluator, a converter that keeps a source's notes and EdiZon's sections and needs the master code only where a cheat depends on it, and the way back to Atmosphère: toAtmosphereAll for every build, with the player's values, raw or core chosen for each cheat, checked against a build's main (a pointer's base too), within Atmosphère's limits, with a stable reason for each cheat left out. Can't hold controller inputs #16 also finds patterns faster where many places look alike. peanuts/scripts/mncf-vendor.sh copies a newer one; peanuts/tests/test_mncf_vendor.py fails on any change to the copy, so a fix goes to Mistel first. Only Mistel's format/ folder is MIT; nothing of its GPL tools is here. android/NOTICE.md names it.

Upstream files

  • GameActivity.java: inside Part C's PEANUTS begin/end block, ModCheats.install gets the game and its pack. android/NOTICE.md: one marked paragraph.
  • peanuts/scripts/upstream-update.sh --check flagged Part C's two Peanuts files (CheatLoaderJvmTest, ModCheats.java) since Part C: Peanuts' JVM tests under jvmtest/java/dev/peanuts count as Peanuts-owned now, and ModCheats.java is on peanuts/upstream-unmarked.txt with its reason. All in peanuts/UPSTREAM-EDITS.md.

Tests

  • peanuts/tests/test_cheat_loader.py (CheatLoaderJvmTest, with the library compiled from the copy):
    • a conversion keeps the game, the versions, the origin and the authors (credits, else the player, else none), and the notes in the description;
    • a pack's main with the original words back, and without orig.bin the rewritten runs left out;
    • a file made ready for a build, kept, and read again without main; on another build the place moved with the code;
    • an Atmosphère cheat converted with the game's main works on the next build by its pattern;
    • the spec's reasons (outside, pattern-not-found, capability-missing:display, runtime:unsupported:run.once, runtime:rewritten) and their words;
    • the number field of an .mncf value (the instruction made again for 300, the range kept, the kept number written at a start);
    • relations (radio, requires), export (.mncf with the switches and the number; unchanged bytes when nothing changed; an .mncp whose manifest reads back), a pack with a changed file, a bare .mncf, another game's, one of another format version;
    • the keys of a converted file are the keys its Atmosphère file had, so the switches a player set stay;
    • a ZIP export: Atmosphère's layout with one file for each build (the running one first), one master code, the number set in its instruction, the groups as the spec writes them; a monitor, a cheat that runs once and one whose place is not found stay out with their reasons; a patch made for other code than the game's main holds stays out, so does a pointer whose base lies past its end, and without the game's main a pattern stays out with its own reason; added again, the ZIP gives the same cheats with the same code and the same groups.
    • Atmosphère's limits: of 130 cheats Peanuts runs all 130, each master with its own code, while the ZIP has 127, names the other 3 and leaves out the group entries that would cost cheats; two sources' masters of 210 words each: the second stays out with the cheat of its source that needs it, which is named, and that source's other cheat goes in; 40 cheats of 201 words each: the file stays under 64 KB and names the ones past it, and a long name is cut to 63 bytes.
  • peanuts/tests/test_mod_cheats.py, peanuts/tests/test_mncf_vendor.py, peanuts/tests/test_separators.py.
  • peanuts/scripts/check-min-api.py on the APK: nothing newer than API 33.
  • AYN Thor (Android 13), test app, Pokémon Legends: Z-A 2.0.2 from a build folder (read only), a copy of the save:
    • a real cheat file of the game (not part of this PR, not in any picture) added as a zip: one .mncf, 60 of 60 cheats work on 2.0.2, 57 made from their operations with their original code checked against the game's main, 3 run as their own code;
    • a cheat switched on in Mods & Cheats: the game started with 60 cheats loaded, and the host wrote its 2 code words with the game's threads stopped;
    • in the game's Mods tab a code cheat's number (a movz) set from 50000 to 50001: kept, the merged file's block made again (movz w8, #50001), the game read the file again, and switched on, 1 code word written;
    • Export wrote an .mncf with the two cheats that were on as default = true and the number as the value's default; that file added again gave a second source whose cheats were on as exported;
    • the pictures with made-up files: an .mncf (Example cheats.mncf: 4 of 7 work, 3 cannot with their reasons) and an Atmosphère zip.
  • The dual-screen emulator (Android 13, the launcher without native code), the same made-up file for Metroid Dread 2.1.0 with a stand-in pack (a zero-filled main of the build ID): the export dialog (Bild 9 to 11), and a ZIP written through the system's file picker: atmosphere/contents/010093801237C000/cheats/646761F643AFEBB3.txt with the 4 cheats, "Exported 4 cheats as a ZIP. 3 could not go in." With the library from Mistel's main the same stand-in gives the same 4 cheats and the same 3 reasons (checked on the JVM), so the pictures still hold; its file now has the groups as the spec writes them. A source made ready while the pack had no main was made again once it had one.
  • The Z-A title screen flickers on the test app with and without this PR (upstream main as well); it is not from this change.

Not in this PR

  • Peanuts does not run Mistel's pass engine itself yet: monitors on the second screen, toggle combinations, cheats that run once or for a time, and several conditions are listed with their reason. They come with the engine in a later PR.
  • Other formats than Atmosphère's (and its Eden, Yuzu and Ryujinx layouts): Mistel's library converts only Atmosphère so far.
  • A key Peanuts trusts: Mistel has none yet. No list of the player's own trusted keys.
  • Presets and translations of a file are read, not shown. A cheat with several values, a choice list, flags or a float gets no field yet.
  • A mod's own cheat files (<mod>/cheats/*.txt) stay as the mod has them.

Every cheat file a player adds is kept as one Mistel Native Cheat Format
file (.mncf) for all of a game's builds. An Atmosphère, Eden, Yuzu or
Ryujinx file is converted as it is added, with the game's own main from
the pack: each patch names the original code at its place, and its place
is found by a pattern, so a cheat keeps working after a game update and
never writes into code that is not what it was made for. A cheat that
cannot work here says why, in a sentence.

- MncfBuild: a file made ready for the build that runs (Mistel's
  evaluator), kept per build, and written as Atmosphère code for the
  host's cheat engine, so the engine is unchanged.
- MncfConvert: Atmosphère to .mncf with the game, the versions, the
  origin and the authors (the credits, else the player who adds it).
- MncfMain: the game's own main from a pack (main.patched.bin with
  main.orig.bin back at main.changed.tsv's runs).
- .mncf and .mncp files are added as they are; a signature stays good,
  and only a verified one gets a mark.
- A value of an .mncf cheat (a number in an instruction too) gets the
  in-game number field; relations (requires, excludes, radio) apply.
- Export writes .mncf (one source) or .mncp (several), with the cheats
  that are on and the numbers set.
- Mistel's MIT library is copied unchanged under io/github/davdxpx/mistel
  (peanuts/scripts/mncf-vendor.sh, checked by test_mncf_vendor.py).
…it brings

- Export sits in the bar beside Add on the Cheats tab, on the game page's
  Mods & Cheats area and on its own page, while there is a source to
  export.
- The Add dialog of an .mncf opens with a card for the file (its name,
  who made it, who signed it when a trusted key did) and what the file
  brings, counted from it: its cheats and versions, the ones checked
  before they write, the ones that follow game updates, the numbers to
  set, the ones that say what they do.
- An Atmosphère pick says it becomes a Mistel cheat file, and the wait
  while it is converted says what is being done.
- After adding, a sheet says how many of the cheats work on the game's
  version, how many cannot (listed with the reason), and what the file
  brings, in place of a toast.
…owned

upstream-update.sh --check flagged every hunk of the mod menu's
CheatLoaderJvmTest (android/app/src/jvmtest/java/dev/peanuts) and of
ModCheats.java (a Peanuts file in upstream's launcher package) since Part
C. The JVM tests of dev.peanuts join the owned paths, as dev/peanuts does
under main; ModCheats.java goes on the allow-list with its reason.
…hat Peanuts cannot run yet

The copy is of Mistel's branch mncf-1-peanuts-gaps (9280c89), the fixes of the
gaps this PR found; mncf-vendor.sh copies it from Mistel's main once igawa6#11 is merged.

- The evaluator refuses what Peanuts' engine lacks with the spec's
  runtime:unsupported:<part> (run once or for a time, toggle buttons, several
  conditions, ...), and a patch over a run Peanuts' rewriter changed with
  runtime:rewritten, so MncfBuild no longer checks those itself.
- The converter reads notes, EdiZon's sections and two builds of one version
  itself, so MncfConvert passes each Atmosphère file as it is.
- A number set in the game's tab reaches toAtmosphere as the player's value,
  checked: one a cheat cannot take leaves only that cheat out.
…e before the game is built is kept

- A source whose file is not made ready for the build yet, or cannot be read,
  may name no other build: its line said "For another version" with the first
  of none, and the page closed. It now says what it is.
- A file made ready while the pack has no main yet (a game not built) was not
  kept, so the page looked it up again on every redraw and stayed at "Looking
  up its cheats". It is kept now, marked, and made again once the pack has
  its main, so its patterns are searched then.
…ther emulator reads

Export opens one dialog with two ways the cheats leave Peanuts:

- Mistel cheat file, with a Recommended badge and picked: one .mncf (an .mncp
  for several sources) with every cheat for every version, the ones that are
  on and the numbers set, as before.
- ZIP for other emulators: atmosphere/contents/<title ID>/cheats/<build ID>.txt,
  one file for each version, as Atmosphere, Eden, Yuzu and Ryujinx read them
  (MncfExport.zip). Each cheat is lowered with Mistel's toAtmosphere for every
  build its file names, and for the running build from the file made ready
  for it: the code Peanuts runs, else the file's own code for the build, else
  code made from its operations, with the number set in the game's tab. One
  master code a file, the groups between EdiZon's section markers, at most
  127 cheats.

What an Atmosphere file cannot hold (a monitor, toggle buttons, a run once
or for a time, several conditions, a place found by a pattern in a version
whose main Peanuts does not have) and what does not fit the game stay out.
Picking the ZIP shows, before anything is written, how many cheats go in for
which version and which stay out, each with its reason.

CheatLoaderJvmTest: the ZIP's layout, its master, number and sections, what
stays out and why, two builds, and the round trip: the ZIP added again gives
the same cheats with the same code and groups.
…inst the game's main, with stable reasons

The library is now davdxpx/mistel#11 at 797d095: toAtmosphereAll writes
every build of a file, and with a build's main it finds the patterns and
checks every patch and override stub against expect before writing, and
Export.reasons() gives each cheat left out a stable reason (spec section 15).

- MncfExport.zip lowers each source with toAtmosphereAll and gives it the
  game's own main for the build that runs, so a cheat whose place holds other
  code stays out of the ZIP (expect-mismatch); a build the game runs that the
  file does not name is written from the patterns with that main.
- The dialog's list of what stays out words those reasons, never shows them.
- MncfBuild reads the converter's codes instead of its English sentences, and
  maps them to Peanuts' reasons (runtime:unsupported:<part>, ...).
- The dialog's ZIP is the one written, so the game's main is read once.

CheatLoaderJvmTest: a patch made for other code stays out with its reason;
without the game's main a pattern stays out with its own.
… Atmosphère's limits

Mistel's library is copied from its main (76d283c), which has davdxpx/mistel#11 and igawa6#16.

- Each source is lowered in one pass: its own raw code for the build first, code made from its
  operations for a cheat with a number set (the converter's preferCheats).
- With the game's main, the pointer at the base of a pointer path is checked too: one past
  main's end stays out of the ZIP with its reason.
- The ZIP keeps Atmosphère's limits across sources, past which a Switch refuses the whole file:
  one master code of at most 256 words (a master past it stays out, with the cheats of its
  source that need it), at most 127 entries, group entries counted, less than 64 KB, and names
  of at most 63 bytes. What goes past them is named in the dialog.
- Groups are written as the spec writes them: the group's name as an empty entry, and an entry
  of only 20000001 where a cheat in no group follows. Where they would cost cheats, the file has
  no groups.
- Peanuts' own engine is not held to Atmosphère's limits: each master keeps its own code, and a
  source of more than 127 cheats is lowered in parts, so every cheat still runs.
…r names them

The one master entry of a file takes the first master's name, and a group entry its group's
name as it is, cut only to what Atmosphère keeps (spec section 15).
…page, and the ZIP words dmnt's size again

- A cheat refused for a key the reader does not know is refused before its name is read, so it had
  no name, and CheatFile.fromMncf crashed the cheats page on it. It is listed by its id now.
- Since davdxpx/mistel#17 Mistel's converter keeps dmnt's 64 KB itself and leaves such cheats out with
  atmosphere:too-big; the ZIP export had no words for that reason and said the Atmosphère file cannot
  say what the cheat does. CheatLoaderJvmTest's 64 KB check failed with Mistel's main for it.
…eds, shipped like upstream's other third-party code

The library for .mncf cheat files was a full copy of Mistel's format/lib/java inside the launcher's own sources. It now
sits where upstream keeps other people's code: dm/third_party/mistel/ with Mistel's format/LICENSE, a README and a row
in dm/third_party/README.md, compiled with the app by one java.srcDirs line, nothing downloaded at build time.

- Only the files the launcher needs, unmodified, from Mistel's main f25485c: 59 of its 64 files. The pass engine
  (Engine, Pass, Change, Shown) and CheatState are left out, as nothing here reaches them. The patch readers stay,
  because Mistel's Converter uses them. Mistel's main also brings the update keys of a cheat file (spec section 17),
  which its reader checks, and keeps dmnt's 64 KB itself (atmosphere:too-big, worded since the last commit).
- peanuts/scripts/mncf-vendor.sh copies them from a Mistel checkout: the files MISTEL.sha256 lists, the ones Peanuts'
  code names, and every file those name in turn (a class a file declares itself is its own). It writes MISTEL.sha256,
  the README and the row's commit.
- peanuts/tests/test_mncf_vendor.py fails when a file is changed, added or taken away. It also checks the MIT notice,
  the README, the row, the gradle line and the overlay check. With MISTEL set to a Mistel checkout, it compares every
  file with that commit.
- android/NOTICE.md, PEANUTS.md, UPSTREAM-EDITS.md and upstream-update.sh name the new place. test_cheat_loader.py and
  test_mod_cheats.py compile the library from there.
…ckground, and the page says so

The first look at a game after the update made its earlier cheat files .mncf on the main thread: the game's
main is read and every cheat's place looked up in it, which took over a minute for Pokémon Legends: Z-A and
ended in "Peanuts isn't responding" (an ANR in Mistel's pattern search).

- The page, the game page's row and its tile start the conversion on a thread of its own (CheatsPage.convert,
  beside prepare) and no longer wait for it.
- Meanwhile the row says "Making the cheats ready". The source's card shows a progress bar and that it becomes a
  Mistel cheat file once, after this update. Then the page, or else the game's page, is built again with its cheats.
- A start and the page never convert the same files at once: CheatSources.upgrade and migrate take one lock, so a
  start right after opening the page waits for the page's conversion and finds it done.
- A file that cannot be converted is tried again at the next run of the app, not in a loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant