From d4497da8c7d2bc3cd72f5a062ccb1015bbaae142 Mon Sep 17 00:00:00 2001 From: ICUMADETHIS Date: Sun, 22 Nov 2026 08:50:14 +0000 Subject: [PATCH] Preserve plugin image paths in generated patch tables --- recomp/patches.py | 19 +++++++++-- tests/exefs_plugins_test.py | 67 +++++++++++++++++++++++++++++++++++-- tools/exefs_plugins.py | 3 +- 3 files changed, 83 insertions(+), 6 deletions(-) diff --git a/recomp/patches.py b/recomp/patches.py index a3746ac5..170e540f 100644 --- a/recomp/patches.py +++ b/recomp/patches.py @@ -1,7 +1,8 @@ """Code patch table for the generator: guest instructions replaced in the lifted C, switchable at run time. Lifted code cannot be byte-patched when the game runs, so a patch is applied when the C is generated. A patch file -(gen.py --patches) has these kinds of lines; `#` starts a comment, fields are separated by whitespace: +(gen.py --patches) has these kinds of lines; `#` starts a comment, fields are separated by whitespace. A plugin image +path may be a JSON double-quoted string to preserve spaces, `#` and escaped characters: set [ordered] a patch set: a run-time switch =0/1, when it is unset site
[ ...] @@ -77,6 +78,7 @@ entry the runtime watches: their lifted C starts with FN_ENTER (runtime/cpu.h), branch to fnprof_hit while the runtime sets it. A table with hook lines puts FN_ENTER into these functions only; a table without any keeps it in every function, as does gen.py --fn-enter-all (a whole-game function-entry profile). """ +import json from pathlib import Path PAGE = 0x1000 @@ -187,10 +189,21 @@ def load(path, mods): sets, index, sites, spins, hooks, ordered, plugins = [], {}, {}, {}, {}, set(), {} bases = {n: (b, s) for n, b, s in mods} for no, raw in enumerate(Path(path).read_text(encoding='utf-8').splitlines(), 1): - f = raw.split('#', 1)[0].split() + where = f'{path}:{no}' + prefix = raw.split(None, 4) + if len(prefix) == 5 and prefix[0] == 'plugin' and prefix[4].startswith('"'): + try: + image, end = json.JSONDecoder().raw_decode(prefix[4]) + except json.JSONDecodeError as e: + raise PatchError(f'{where}: invalid quoted plugin image: {e.msg}') from None + tail = prefix[4][end:].strip() + if tail and not tail.startswith('#'): + raise PatchError(f'{where}: unexpected text after quoted plugin image') + f = prefix[:4] + [image] + else: + f = raw.split('#', 1)[0].split() if not f: continue - where = f'{path}:{no}' if f[0] == 'set' and (len(f) == 4 or len(f) == 5 and f[4] == 'ordered'): if f[1] in index: raise PatchError(f'{where}: set {f[1]} defined twice') diff --git a/tests/exefs_plugins_test.py b/tests/exefs_plugins_test.py index 071df40b..2f6d4bd0 100644 --- a/tests/exefs_plugins_test.py +++ b/tests/exefs_plugins_test.py @@ -10,6 +10,7 @@ Run with .venv (lz4 and numpy, as recomp/): python tests/exefs_plugins_test.py import hashlib import json import struct +import subprocess import sys import tempfile import unittest @@ -355,6 +356,47 @@ class Lines(unittest.TestCase): rec['modules'][2]['image'] = 'plugin0.img' return xp.plugin_lines(rec, xp.hooks_of(hooks, 'plugin0'), 'mod_x', d, 'plugin0') + def test_image_path_roundtrips_through_the_patch_table(self): + rec = record() + mods = [(m['name'], m['base'], m['size']) for m in rec['modules']] + with tempfile.TemporaryDirectory() as d: + for folder in ('plain', 'with spaces', 'with#hash', 'caf\u00e9 #1'): + with self.subTest(folder=folder): + dest = Path(d) / folder + dest.mkdir() + image = dest / 'plugin0.img' + image.write_bytes(rec['images']['plugin0']) + rec['modules'][2]['image'] = image.name + lines = xp.plugin_lines(rec, [], 'mod_x', dest, 'plugin0') + patch = dest / 'patches.txt' + patch.write_text('set mod_x TOTK_MODPATCH_X 0 ordered\n' + '\n'.join(lines), encoding='utf-8') + actual = patches.load(patch, mods).plugins['plugin0']['image'] + self.assertEqual(actual, image.resolve().as_posix()) + self.assertEqual(Path(actual).read_bytes(), rec['images']['plugin0']) + + def test_quoted_image_and_legacy_unquoted_paths(self): + mods = [('main', MAIN, 0x200000), ('plugin0', PLUGIN, 0x14000)] + with tempfile.TemporaryDirectory() as d: + patch = Path(d) / 'patches.txt' + for image, field in (('C:/folder #1/plugin.img', '"C:/folder #1/plugin.img" # comment'), + ('C:/plain/plugin.img', 'C:/plain/plugin.img # comment'), + (r'C:\with space\plugin.img', json.dumps(r'C:\with space\plugin.img')), + (r'C:\plain\plugin.img', r'C:\plain\plugin.img')): + with self.subTest(field=field): + patch.write_text(f'set x ENV 0\nplugin x plugin0 {"0" * 64} {field}\n') + self.assertEqual(patches.load(patch, mods).plugins['plugin0']['image'], image) + + def test_malformed_quoted_image_names_the_patch_line(self): + mods = [('main', MAIN, 0x200000), ('plugin0', PLUGIN, 0x14000)] + with tempfile.TemporaryDirectory() as d: + patch = Path(d) / 'patches.txt' + for field in ('"unfinished', '"image.img" extra', '"image.img" "#not a comment"'): + with self.subTest(field=field): + patch.write_text(f'set x ENV 0\nplugin x plugin0 {"0" * 64} {field}\n') + with self.assertRaises(patches.PatchError) as error: + patches.load(patch, mods) + self.assertIn(f'{patch}:2:', str(error.exception)) + def test_inline(self): out = self.lines(record(original=0xAA0003F3)) self.assertIn(f'site mod_x main {SITE_OFF:x} aa0003f3 @call:plugin0+{STUB_OFF:x} aa0003f3', out) @@ -511,13 +553,34 @@ class Generator(unittest.TestCase): n = Nso(str(self.plugin_file)) self.sha = hashlib.sha256(bytes(img[n.text[0]:n.text[0] + n.text[1]])).hexdigest() - def table(self, site_line, extra=()): + def table(self, site_line, extra=(), image=None): + image = image or self.d / 'plugin0.img' text = '\n'.join(['set mod_x TOTK_MODPATCH_X 0 ordered', - f'plugin mod_x plugin0 {self.sha} {(self.d / "plugin0.img").as_posix()}', + f'plugin mod_x plugin0 {self.sha} {json.dumps(image.as_posix())}', f'expect mod_x main {FN:x} aa0003f3 14000000', site_line, *extra]) + '\n' (self.d / 'p.txt').write_text(text) return patches.load(self.d / 'p.txt', self.mods) + def test_generator_cli_reads_image_in_a_folder_with_spaces_and_hash(self): + # The direct-emitter fixture needs a discoverable caller for a full CLI lift. + main = self.d / 'exefs' / 'main' + raw = bytearray(main.read_bytes()) + struct.pack_into('