Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.
Swift apple-siliconcustom-formathomebrewiosipaipadipadosiphonemetalnative-portnintendo-switchnxnativeromfsswitch-games
NSP script: hand a Switch 2 package (.nup) to nxbuild too (#749)
v0lumehifd5c38b7bb61
diff --git a/patches/0073-nsp-script-hand-the-extraction-and-the-shader-sp.patch b/patches/0073-nsp-script-hand-the-extraction-and-the-shader-sp.patchindex 07072e1a..b54d3d2f 100644--- a/patches/0073-nsp-script-hand-the-extraction-and-the-shader-sp.patch+++ b/patches/0073-nsp-script-hand-the-extraction-and-the-shader-sp.patch@@ -19,22 +19,30 @@ the profile's table rows for the split) run the standard step, with a note.nxbuild's own red lines stay in its log, since the build did not fail.Without the variable nothing changes.+A Switch 2 package (.nup) goes to nxb-extract --nup (no keys) the same way: the+version's image, ExeFS and control data land in extract/<version>, the DLC+images in aoc/, in the layout pack_nup.py writes. The script keeps its own+reuse key for them (the NUP's size and time, the version, the tool), since+nxb-extract writes no RomFS.img.inputs.json. The checks are ExeFS/main,+RomFS.img, the marker and the '<program ID> version <v>:' line of its log. A+failed or unsupported run, or a failed check, runs pack_nup.py instead.+Upstream-Status: forwarded seedds/nxnative#1325-Reason: the launcher's opt-in experimental builder (nxbuild) runs the extraction and the shader split+Reason: the launcher's opt-in experimental builder (nxbuild) runs the extraction (NSP and NUP) and the shader splitIssue: LNPANDY/NX2Apple#749---- .../tests/test_nsp_experimental_builder.py | 156 +++++++++++++++++++ .../tests/test_nsp_experimental_builder.py | 211 ++++++++++++++++++docs/env_switches.tsv | 1 +- scripts/build_ipa_from_nsp.py | 116 ++++++++++++-- 3 files changed, 269 insertions(+), 4 deletions(-)+ scripts/build_ipa_from_nsp.py | 164 +++++++++++++-+ 3 files changed, 370 insertions(+), 6 deletions(-)create mode 100644 dm/tools/tests/test_nsp_experimental_builder.pydiff --git a/dm/tools/tests/test_nsp_experimental_builder.py b/dm/tools/tests/test_nsp_experimental_builder.pynew file mode 100644-index 0000000..9005508+index 0000000..444e186--- /dev/null+++ b/dm/tools/tests/test_nsp_experimental_builder.py-@@ -0,0 +1,156 @@+@@ -0,0 +1,211 @@+"""NSP script, the experimental builder (scripts/build_ipa_from_nsp.py, DM_EXPERIMENTAL_BUILDER and NXB_BIN): which nxbuild binary a stage takes, and that a stage it does not+support, fails or gets wrong runs the standard way. The nxbuild binaries are stand-in scripts in a temporary folder: no game files, keys or nxbuild needed."""+import importlib.util@@ -44,6 +52,7 @@ index 0000000..9005508+import sys+import tempfile+import unittest++from types import SimpleNamespace+from unittest import mock++REPO = Path(__file__).resolve().parents[3]@@ -165,6 +174,60 @@ index 0000000..9005508+ self.assertIn(f'--update {upd} {tmp} --updates-only', args)+ self.assertFalse(tmp.with_name('v1.partial.nxb-base').exists(), 'the base\'s scratch folder goes')+++ def nup(self, body, kind=None, version=0, marker='marker.txt'): # a NUP (stand-in: the path and the version's package) through nxb-extract --nup++ src = self.t / 'src'; src.mkdir(exist_ok=True); (src / 'marker.txt').write_text('x')++ romfs_image.pack(src, self.t / 'image.img')++ if body: tool(self.bin, 'nxb-extract', body.replace('IMAGE', repr(str(self.t / 'image.img')))) # None: the stand-in stays as it is++ path = self.t / 'game.nup'++ if not path.exists(): path.write_bytes(b'NUP\1')++ ex = self.t / 'work' / 'extract'; ex.mkdir(parents=True, exist_ok=True)++ k = SimpleNamespace(kind=nsp.pack_nup.UPDATE if version else nsp.pack_nup.BASE, version=version)++ return nsp.nxb_extract_nup(ex, f'v{version}', k, None, APP, SimpleNamespace(path=path), marker), ex / f'v{version}'++++ NUP_WRITES = ("a = sys.argv; ver = a[a.index('--version') + 1]; out = Path(a[a.index('--work') + 1]) / 'extract' / ('v' + ver); (out / 'ExeFS').mkdir(parents=True); (out / 'ExeFS/main').write_bytes(b'NSO0')\n"++ "shutil.copyfile(IMAGE, out / 'RomFS.img'); aoc = Path(a[a.index('--work') + 1]) / 'aoc'; aoc.mkdir(); (aoc / '1.img').write_bytes(b'dlc')\n"++ "print('{app} version ' + ver + ': game.nup (update ' + ver + ')')")++++ def test_a_nup_is_handed_over_with_its_version_and_no_discarded_image(self):++ ok, dst = self.nup(self.NUP_WRITES.format(app=f'{APP:016X}') + f"\nPath({str(self.t / 'args')!r}).write_text(' '.join(sys.argv[1:]))", version=131072)++ self.assertTrue(ok, self.text())++ args = (self.t / 'args').read_text()++ self.assertIn(f'--nup {self.t / "game.nup"} --work {self.t / "work" / "nxb_nup_v131072.partial"} --version 131072', args)++ self.assertNotIn('--discard-images', args)++ self.assertTrue((dst / 'RomFS.img').is_file() and (dst / 'ExeFS' / 'main').is_file())++ self.assertTrue((self.t / 'work' / 'aoc' / '1.img').is_file(), 'the DLC images come from the same run')++ self.assertFalse((self.t / 'work' / 'nxb_nup_v131072.partial').exists())++ self.assertIn('extracted v131072 with nxb-extract', self.text())++++ def test_a_nup_base_is_version_zero_and_an_unchanged_nup_is_reused(self):++ body = self.NUP_WRITES.format(app=f'{APP:016X}') + f"\nPath({str(self.t / 'runs')!r}).open('a').write('x')"++ ok, dst = self.nup(body)++ self.assertTrue(ok, self.text())++ self.assertTrue(ok and (dst / 'RomFS.img').is_file())++ self.said.clear()++ ok, _ = self.nup(None)++ self.assertTrue(ok)++ self.assertEqual((self.t / 'runs').read_text(), 'x', 'the second call did not run nxb-extract again')++ self.assertIn('reusing it', self.text())++++ def test_a_nup_falls_back_on_unsupported_failure_or_a_bad_output(self):++ for body, why in (('sys.exit(4)', 'not supported yet'), ('sys.exit(3)', 'exit code 3'), (self.NUP_WRITES.format(app='0100000000001000'), 'failed the checks'),++ (self.NUP_WRITES.format(app=f'{APP:016X}').replace("(out / 'ExeFS/main').write_bytes(b'NSO0')", 'pass'), 'failed the checks')):++ with self.subTest(why=why, body=body[:30]):++ self.said.clear(); nsp._nxb_noted.clear()++ ok, dst = self.nup(body)++ self.assertFalse(ok)++ self.assertFalse(dst.exists(), 'pack_nup starts from what it finds: nothing of nxb-extract stays')++ self.assertFalse((self.t / 'work' / 'nxb_nup_v0.partial').exists())++ self.assertIn(why, self.text())++ self.assertIn('the standard builder runs it', self.text())++++ def test_a_nup_without_the_builder_runs_the_standard_way(self):++ with mock.patch.dict(os.environ, {'DM_EXPERIMENTAL_BUILDER': '0'}):++ ok, dst = self.nup(self.NUP_WRITES.format(app=f'{APP:016X}'))++ self.assertFalse(ok)++ self.assertFalse(dst.exists())+++ def split(self, rows):+ tool(self.bin, 'nxb-split', "a = sys.argv; o = lambda k: Path(a[a.index(k) + 1])\n"+ "o('--pkg').mkdir(parents=True)\n"@@ -204,7 +267,7 @@ index 83e5942..732fea6 100644DM_FIXED_TIME switch `=<seconds>`: the user clock always returns that time and the host time zone is pinned to UTC, for deterministic comparisonDM_FOCUS_KEEP switch `1` keeps the game in focus while its Mac window is up, whichever app is active (minimizing still counts); `scripts/regress.py` sets it, so another app taking the key window during a run does not pause a game that stops out of focusdiff --git a/scripts/build_ipa_from_nsp.py b/scripts/build_ipa_from_nsp.py-index 486a464..21da48b 100755+index 486a464..ed02d0d 100755--- a/scripts/build_ipa_from_nsp.py+++ b/scripts/build_ipa_from_nsp.py@@ -33,6 +33,7 @@@@ -215,7 +278,7 @@ index 486a464..21da48b 100755import ast, collections, contextlib, ctypes, errno, getpass, pwd, hashlib, importlib.util, json, os, re, shlex, shutil, struct, subprocess, sys, tempfile, threading, time, typesfrom pathlib import Pathsys.path.insert(0, str(Path(__file__).resolve().parents[1] / "dm/tools"))-@@ -425,6 +426,104 @@ def free_gb(p):+@@ -425,6 +426,141 @@ def free_gb(p):return shutil.disk_usage(p).free / 2**30@@ -226,6 +289,7 @@ index 486a464..21da48b 100755+# output that fails the checks below run the standard way, with a note in the log. Without the variable nothing changes.+NXB_UNSUPPORTED = 4 # nxbuild's exit code for "not supported yet": nothing it wrote is an output (nxbuild's docs/integration-requests.md)+_nxb_noted = set()++_nxb_nups = set() # NUPs whose versions nxb-extract wrote (its aoc/ images are the DLC's: extract_aoc does not pack them again)+++def nxb_note(msg): # one note per message and build: a missing binary is named once, not per version@@ -262,6 +326,42 @@ index 486a464..21da48b 100755+ return False++++def nxb_extract_nup(ex, name, k, base, app, n, marker):++ """One version of a NUP through nxb-extract (no keys: `--nup`) into ex/name, the layout pack_version writes, and the DLC images into <work>/aoc. True when its output passed the checks (ExeFS/main,++ RomFS.img, the marker, the log's '<program ID> version <v>:' line); False leaves nothing of it, for pack_nup. nxb-extract writes no reuse stamp: this keeps its own (the NUP's size and time, the version, the tool)"""++ tool = nxb_tool('extract')++ if not tool: return False++ work, dst = ex.parent, ex / name++ ver = k.version if k.kind == pack_nup.UPDATE else 0++ try: st, tst = n.path.stat(), Path(tool[0]).stat()++ except OSError: return False++ keyfile = dst / 'RomFS.img.nxb-nup.key'++ key = fingerprint(('nxb-nup', str(n.path), st.st_size, st.st_mtime_ns, ver, [str(c) for c in tool], tst.st_size, tst.st_mtime_ns))++ if reused(keyfile, key) is not None and (dst / 'ExeFS' / 'main').is_file() and (dst / 'RomFS.img').is_file():++ _nxb_nups.add(n.path); say(f'experimental builder: {name} was extracted with nxb-extract and the NUP is unchanged: reusing it'); return True++ rmtree(keyfile)++ log, scratch = work / 'logs' / f'nxb_extract_{name}.log', work / f'nxb_nup_{name}.partial'++ rmtree(scratch)++ ok = nxb_run([*tool, '--nup', n.path, '--work', scratch, '--version', ver], log, f'extract {name}') # never --discard-images: the image is the output++ out = scratch / 'extract' / f'v{ver}'++ if ok:++ try:++ whole = (out / 'ExeFS' / 'main').is_file() and (out / 'RomFS.img').is_file() and (not marker or (rfs.source(out) / marker).exists())++ said = re.search(rf'^{app:016X} version {ver}: ', log.read_text(errors='replace'), re.M) is not None++ except (OSError, ValueError, struct.error): whole = said = False++ if whole and said:++ swap_in(out, dst)++ if (scratch / 'aoc').is_dir() and n.path not in _nxb_nups: swap_in(scratch / 'aoc', work / 'aoc') # the images are the same in every version's run++ _nxb_nups.add(n.path)++ keyfile.write_text(key + '\n')++ rmtree(scratch)++ say(f'experimental builder: extracted {name} with nxb-extract')++ return True++ nxb_note(f'extract {name}: the output of nxb-extract failed the checks (see {log}), the standard builder runs it')++ rmtree(scratch)++ return False+++++def nxb_extract(keys, base, upd, tmp, logs, name, app, marker):+ """The extraction of one version through nxb-extract into tmp (the layout nx_extract writes: ExeFS/, RomFS.img, control.nacp, icon.jpg). True when its output passed the checks;+ False leaves no tmp, for nx_extract. Only plain NSPs of one title: an XCI, NSZ or merged package, another program or a damaged NSP go the standard way"""@@ -320,7 +420,7 @@ index 486a464..21da48b 100755def configure_dotnet(env):"""Let standalone .NET apphosts find the runtime used by dotnet (Homebrew installs it outside the registered locations)."""if env.get('DOTNET_ROOT'): return-@@ -1255,7 +1354,8 @@ def extract(nx, keys, ex, name, base, upd, cmp_dir, logs, g, app): # the build+@@ -1255,7 +1391,8 @@ def extract(nx, keys, ex, name, base, upd, cmp_dir, logs, g, app): # the builddef poll():n, b = tree_size(tmp); return {'done': b, 'note': f'{n:,} files'}PROG.stage(f'extract {name}', poll=poll, unit='B', total=last[1] if last else base.stat().st_size + (upd.stat().st_size if upd and upd != base else 0), est=not last)@@ -330,7 +430,36 @@ index 486a464..21da48b 100755swap_in(tmp, dst)n, b = extracted(log) or (0, 0)PROG.end(f'{n:,} files, {amount(b, "B")}')-@@ -2189,9 +2289,17 @@ def split_shaders(py, repo, roms, mains, pkg, env, logs, g, provisional=False, o+@@ -1295,13 +1432,15 @@ def scan_nup(path, want=None): # a NUP as scan_nsps gives a folder of NSPs, wi+ return app, {'base': base, 'updates': sorted(vers.items()), 'dlc': [(tid, None, k) for tid, k in sorted(dlc.items())], 'nup': n}+++-def extract_nup(ex, name, k, base, app, n): # one version of a NUP (k: its base or an update package) into the layout extract makes; the code modules and the image are kept while unchanged++def extract_nup(ex, name, k, base, app, n, marker=None): # one version of a NUP (k: its base or an update package) into the layout extract makes; the code modules and the image are kept while unchanged+ dst = ex / name; part = dst / 'RomFS.img.part'+ def poll():+ try: return {'done': part.stat().st_size}+ except OSError: return None+ try:+ PROG.stage(f'unpack {name}', poll=poll, unit='B', total=n.tree_bytes(pack_nup.romfs_of(k, base)), est=True)++ if nxb_extract_nup(ex, name, k, base, app, n, marker): # the experimental builder, when it is on and can (else nothing happens there)++ PROG.end(amount((dst / 'RomFS.img').stat().st_size, 'B')); return dst+ pack_nup.pack_version(n, k, base, app, dst, say=lambda s: None)+ except pack_nup.Error as e: raise BuildError(str(e)) from e+ PROG.end(amount((dst / 'RomFS.img').stat().st_size, 'B'))+@@ -1334,6 +1473,11 @@ def extract_aoc(nx, keys, repo, work, app, fam, logs):+ if not fam['dlc']: return None+ if fam.get('nup'):+ PROG.stage('unpack DLC', step='unpacking')++ if fam['nup'].path in _nxb_nups and all((work / 'aoc' / f'{tid - app - 0x1000}.img').is_file() for tid, _, _ in fam['dlc']): # nxb-extract wrote them with the versions++ want = {f'{tid - app - 0x1000}.img' for tid, _, _ in fam['dlc']}++ for f in (work / 'aoc').iterdir():++ if f.name not in want: rmtree(f) # DLC no longer in the container++ PROG.end(f'{len(fam["dlc"])} DLC'); return work / 'aoc'+ try: pack_nup.pack_dlc(fam['nup'], app, {tid: k for tid, _, k in fam['dlc']}, work / 'aoc', say=lambda s: None)+ except pack_nup.Error as e: raise BuildError(str(e)) from e+ PROG.end(f'{len(fam["dlc"])} DLC')+@@ -2189,9 +2333,17 @@ def split_shaders(py, repo, roms, mains, pkg, env, logs, g, provisional=False, otmp = pkg.with_name(pkg.name + '.partial'); rmtree(tmp); (tmp / 'tables').mkdir(parents=True)def counted(ln): # shader_split.py's 'progress: <done>/<total>'if m := re.fullmatch(r'progress: (\d+)/(\d+)', ln): PROG.set(done=int(m[1]), total=int(m[2]), unit='files')@@ -351,3 +480,12 @@ index 486a464..21da48b 100755m = re.search(r'shaders: (\d+) \(', text)bad = re.search(r'^\[RED\] (\d+) file\(s\) could not be read.*$', text, re.M) # the split leaves out a file no reader reads (its own red line per file in the log) and goes onif bad and not provisional: # the profile's tables were split from every file: one that does not read now is a reader change+@@ -3122,7 +3274,7 @@ def main(args=None):+ dirs = {}+ for ver in versions: # each merged version streams directly into its own image+ b, u = picks[ver]+- dirs[ver] = extract_nup(ex, ver, u or b, b, app, fam['nup']) if nup else extract_dump(ex, ver, u or b, app) if dump else extract(nx, keys, ex, ver, b, u, None, logs, g, app)++ dirs[ver] = extract_nup(ex, ver, u or b, b, app, fam['nup'], g.get('romfs_marker')) if nup else extract_dump(ex, ver, u or b, app) if dump else extract(nx, keys, ex, ver, b, u, None, logs, g, app)+ if not nup and not dump: extract_html(nx, keys, dirs[ver], b, u, logs, app)+ check_versions_aarch64(dirs, logs, g, nacp.name(dirs[versions[0]] / 'control.nacp') or f'{app:016X}') # before build_mismatch, derive_profile and build_sources read the modules+ bad = [] if derived else [f'{v}: {x}' for v in versions for x in build_mismatch(dirs[v], v, g)] # every module's build ID must be the one the profile registers for its versiondiff --git a/release-notes/v1.1.1.md b/release-notes/v1.1.1.mdindex 4852829b..1663d36b 100644--- a/release-notes/v1.1.1.md+++ b/release-notes/v1.1.1.md@@ -24,7 +24,7 @@- **iPhone Duo side column**: the Controllers and Settings chips and Back sit in a narrow column next to the screen, placed by the areas the system reserves. Phones without such an area look as before. Checked in the Simulator only (#622, #633)- **Preparing game**: after Play, the shell shows "Preparing game…" while it unpacks the shaders and hands over to the engine, and ignores the controller until then. By @Silverhza (#652)- **Windows launcher without git**: the Qt launcher in the Actions zip carries the engine tree, so a player needs no git and no checkout of the repository (#615, #616)-- **Experimental builder (off by default)**: Settings, Build tools, Use experimental builder runs the extraction and the shader split with nxbuild, a build tool written in Rust. For the games tested, the files are byte for byte the same as before, and the two stages take seconds, not minutes. Any other case, and any failure, uses the standard builder. Checked with Super Mario Bros. Wonder (#749, #752)+- **Experimental builder (off by default)**: Settings, Build tools, Use experimental builder runs the extraction and the shader split with nxbuild, a build tool written in Rust. For the games tested, the files are byte for byte the same as before, and the two stages take seconds, not minutes. It also reads Switch 2 packages (.nup) and needs no keys for them. Any other case, and any failure, uses the standard builder. Checked with Super Mario Bros. Wonder (#749, #752). The .nup path was checked with a synthetic package and, on the nxbuild side, with the Ocarina of Time package by the image's digest, not in the launcher- **Split a Joy-Con pair into two controllers (Mac)**: the Controllers sheet now has "Split Joy-Con into two controllers" (off by default). When on, a Bluetooth Joy-Con pair becomes two sideways controllers (Joy-Con (L) as Player 1 and Joy-Con (R) as Player 2) with rotated sticks and 6-axis motion, SL and SR mapped to L and R, and separate left and right handle rumble (#740, #745)- **Italian texts**: 24 texts of the launcher are reworded to read less literally, and the Switch 2 texts say engine like the rest. By @zatteogit and @Dino0005 (#687, #704)@@ -65,7 +65,7 @@- **iPhone Duo 侧边栏**:「控制器」和「设置」按钮以及返回键放在屏幕旁的窄列里,位置由系统保留的区域决定。没有这种区域的手机外观不变。只在模拟器里检查过(#622、#633)- **正在准备游戏**:点 Play 后,壳 App 在解包着色器并交给引擎期间显示「Preparing game…」,并在此期间忽略手柄。作者 @Silverhza(#652)- **没有 git 的 Windows 启动器**:Actions zip 里的 Qt 启动器自带引擎目录,玩家不需要 git,也不需要仓库检出(#615、#616)-- **实验性构建器(默认关闭)**:「设置」>「构建工具」>「使用实验性构建器」会用 nxbuild(一个用 Rust 编写的构建工具)完成解包和着色器拆分。对已测试的游戏,输出文件与以前逐字节相同,这两步只需几秒到几十秒,而不是几分钟。其他情况以及出现故障时,仍使用标准构建器。已用 Super Mario Bros. Wonder 检查(#749、#752)+- **实验性构建器(默认关闭)**:「设置」>「构建工具」>「使用实验性构建器」会用 nxbuild(一个用 Rust 编写的构建工具)完成解包和着色器拆分。对已测试的游戏,输出文件与以前逐字节相同,这两步只需几秒到几十秒,而不是几分钟。它也能读取 Switch 2 软件包(.nup),这时不需要密钥。其他情况以及出现故障时,仍使用标准构建器。已用 Super Mario Bros. Wonder 检查(#749、#752)。.nup 路径用合成软件包检查过,并在 nxbuild 一侧用 Ocarina of Time 软件包按镜像摘要检查过,尚未在启动器中检查- **将 Joy-Con 拆分为两个手柄(Mac)**:「控制器」面板新增「将 Joy-Con 拆分为两个手柄」开关(默认关闭)。开启后,蓝牙连接的一对 Joy-Con 会拆分为两个独立横握手柄(左 Joy-Con 为玩家 1、右 Joy-Con 为玩家 2),具备横握摇杆与六轴体感旋转、SL 与 SR 映射为 L 与 R,以及左右独立震动反馈(#740、#745)- **意大利语文字**:启动器的 24 条文字改得不再那么直译,Switch 2 的文字也和其余部分一样用 engine。作者 @zatteogit 和 @Dino0005(#687、#704)