"""Code mods: the mods' .ips and .pchtxt patches of the game's code, applied before the game is translated (nx2/code_mods.py, nx2/make_game.py; authored modules and patches, no game). A build applies the patches of /mods to its own copy of the game's code, and the lift key hashes that copy. Run here on modules written by this test: - the IPS and IPS32 reader: records, runs, the end's letters as an offset, files cut short, a write into the header; - the IPSwitch (.pchtxt) reader: @nsobid-, offset_shift, @enabled and @disabled, @stop, strings, both byte orders, notes, and a patch line that cannot be read; - the mods folder walked as host/mods.c walks it: names, the folders that are off, the deeper layouts, the order; - a patch for another build refused with a line that names both ids, one that writes outside the module refused whole, two mods' patches of the same bytes (the later name's stay, and the build says so); - the patched module read back by the translator's own loader (third_party/a64-kit/tools/nso.py), where lz4 is there; - the lift key (make_game.py's own lift_key): another key with a patch, the first key again with the patch switched off or gone, the same key for a patch that changes nothing, another for an edited patch; - what the game folder remembers (triforce_code_mods.txt) and what the next build says about it. Read in make_game.py: the patches are applied after the copy for updates is made and before the lift key is taken. python -I -B tests/test_code_mods.py """ import ast import hashlib import os from pathlib import Path import struct import sys import tempfile import types import unittest ROOT = Path(__file__).resolve().parents[1] NX2 = ROOT / "nx2" sys.path.insert(0, str(NX2)) import code_mods # noqa: E402 MAIN_ID = bytes.fromhex("e0f3ccfaf50065ae4ea01eb5aeb13b1a45da3348") + bytes(12) SDK_ID = bytes.fromhex("11223344556677889900aabbccddeeff00112233") + bytes(12) MAIN_HEX = MAIN_ID.hex()[:40] def nso(build_id, text_size=0x400, rodata_size=0x300, data_size=0x200, fill=0x11): """A plain NSO the translator's loader can read: .text at 0, .rodata at 0x1000 (MOD0 and an empty dynamic section at its start), .data at 0x2000; every other byte of a segment is `fill` plus the segment's number""" text = bytearray([fill]) * text_size struct.pack_into("= 4) return;", "fd.cFileName[0] == '.'", "fd.cFileName[0] == '-' || fd.cFileName[0] == '_'", "_stricmp(((const Mod *)a)->name, ((const Mod *)b)->name)", '"%s\\\\cheats"', "depth > 0"): self.assertTrue(needle in mods_c, "host/mods.c no longer has: " + needle) def test_a_mods_files_in_the_order_of_their_names(self): for name in ("b.pchtxt", "A.ips", "c.IPS", "notes.txt", "d.pchtxt.bak"): self.patch("m/exefs/" + name, "x") (self.mods / "m" / "exefs" / "folder.ips").mkdir() self.assertEqual([rel for rel, _ in code_mods.patch_files(str(self.mods))], ["m/exefs/A.ips", "m/exefs/b.pchtxt", "m/exefs/c.IPS"]) def test_no_mods_folder(self): self.assertEqual(code_mods.find_mods(str(self.root / "none")), []) self.assertEqual(code_mods.apply(str(self.exefs), str(self.root / "none"), self.said.append), []) class Apply(Folder): def test_a_patch_of_each_kind(self): before_sdk = (self.exefs / "sdk").read_bytes() self.patch("fps/exefs/%s.ips" % MAIN_HEX, ips([(0x100 + 0x40, b"\x1f\x20\x03\xd5"), (0x100 + 0x1080, (4, 0))])) self.patch("names/exefs/names.pchtxt", "@nsobid-%s\n@flag offset_shift 0x100\n@enabled\n00002010 \"Hi\"\n" % MAIN_HEX) entries = self.apply() self.assertEqual([(e["state"], e["path"], e["module"], e["changed"]) for e in entries], [("applied", "fps/exefs/%s.ips" % MAIN_HEX, "main", 8), ("applied", "names/exefs/names.pchtxt", "main", 2)]) image = self.image() self.assertEqual(image[0x40:0x44], b"\x1f\x20\x03\xd5") self.assertEqual(image[0x1080:0x1084], bytes(4)) self.assertEqual(image[0x2010:0x2012], b"Hi") self.assertEqual((image[0x3F], image[0x44], image[0x107F], image[0x1084], image[0x200F], image[0x2012]), (0x11, 0x11, 0x12, 0x12, 0x13, 0x13)) self.assertEqual((self.exefs / "sdk").read_bytes(), before_sdk) # (a module no patch is for: its file as it was) said = "\n".join(self.said) self.assertIn("code mods: fps/exefs/%s.ips applied to main: 8 bytes changed" % MAIN_HEX, said) self.assertIn("code mods: names/exefs/names.pchtxt applied to main: 2 bytes changed", said) self.assertIn("translated with main patched", said) self.assertFalse((self.exefs / "main.part").exists()) def test_the_file_written_is_a_whole_plain_module(self): self.patch("m/exefs/%s.ips" % MAIN_HEX, ips([(0x100 + 0x2000, b"\xff")])) self.apply() raw = (self.exefs / "main").read_bytes() self.assertEqual(raw[:4], b"NSO0") self.assertEqual(raw[0x40:0x60], MAIN_ID) self.assertEqual(struct.unpack_from("