Skip to content

Play your own Switch games on iPhone/iPad — one-click macOS app, no terminal. | 把 Switch 游戏装进 iPhone:图形界面一键构建,不用命令行。Bring your own game.

iOS shell: copy a game's packed shader image into its framework

Xiaofeng Hanc1e3a76fa8bd
diff --git a/patches/0078-ios-shell-copy-a-game-s-packed-shader-image-into.patch b/patches/0078-ios-shell-copy-a-game-s-packed-shader-image-into.patch
new file mode 100644
index 00000000..2ec32d32
--- /dev/null
+++ b/patches/0078-ios-shell-copy-a-game-s-packed-shader-image-into.patch
@@ -0,0 +1,178 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: Xiaofeng Han <[email protected]>
+Date: Thu, 5 Nov 2026 13:33:19 -0800
+Subject: [PATCH] iOS shell: copy a game's packed shader image into its
+ framework
+
+Since the engine pin 7249ee9 the NSP script packs an NVN game's iOS shader
+libraries into the one file shader/ios/b-<chain>.img (dm/tools/shader_lib.py),
+and the folder b-<chain>/ is gone once it is packed. The DM_FRAMEWORK_ONLY step
+of build_ipa.sh still rsynced the folder $MSL_IOS/$MSLDIR/, so every shell
+framework build stopped at "copy the shader libraries" (rsync: (l)stat: No such
+file or directory).
+
+The step now asks shader_lib.py which form the libraries have (lib_path, as the
+rest of build_ipa.sh does) and copies an image as msl/b-<chain>.img and a folder
+as msl/<MSLDIR>, the way stage_resources.sh's sync_libraries copies them into an
+app: a first copy is an APFS clone, a rebuild rsyncs onto it, and the other form
+and other chains' libraries are removed. The shell's DM_MSL_DIR is the
+framework's msl/, where the host reads b-<chain>.img (nvn_mtl.m), so nothing
+changes on the device side.
+
+test_framework_build.py runs the DM_FRAMEWORK_ONLY block after a stand-in
+xcodebuild on a packed library, a folder, and rebuilds that switch between them.
+
+Co-Authored-By: Claude Opus 5.5 <[email protected]>
+
+Upstream-Status: fork-only
+Reason: the route C shell's framework build (0050, fork-only) copies upstream's packed shader image (7249ee9); upstream has no DM_FRAMEWORK_ONLY
+Issue: LNPANDY/NX2Apple#781
+
+---
+ dm/ios/tests/test_framework_build.py | 82 +++++++++++++++++++++++++++-
+ dm/ios/tools/build_ipa.sh | 15 ++++-
+ 2 files changed, 91 insertions(+), 6 deletions(-)
+
+diff --git a/dm/ios/tests/test_framework_build.py b/dm/ios/tests/test_framework_build.py
+index fee9e53..0dccfbf 100644
+--- a/dm/ios/tests/test_framework_build.py
++++ b/dm/ios/tests/test_framework_build.py
+@@ -1,13 +1,15 @@
+ """Route C M1 (docs/launcher-c-plan.md): the opt-in framework build's packaging and signing, off a device.
+
+-Three parts, each on synthetic inputs, no game files or Xcode:
++Four parts, each on synthetic inputs, no game files or Xcode:
+ - gen_manifest.py writes the schema's keys, and refuses values the schema cannot carry;
+ - build_ipa.sh's --prepare-xcode block copies project-framework.yml in as the project when DM_FRAMEWORK=1, and
+ project.yml when it is not (the default template is never edited);
+ - the framework is signed before the app's executable, the framework's signature carries no entitlements, the app's
+ keeps the Increased Memory Limit request, and the packed IPA has the framework where the app dlopens it. A default
+- pack (no DM_FRAMEWORK) still has no Frameworks/ entry at all.
+-Only the signing and packing steps run (a stand-in app and framework, no Xcode); codesign is required for the last part.
++ pack (no DM_FRAMEWORK) still has no Frameworks/ entry at all;
++ - DM_FRAMEWORK_ONLY puts the game's shader libraries into the framework in the form they have: an NVN game's one file
++ b-<chain>.img (dm/tools/shader_lib.py) as msl/b-<chain>.img, a folder as msl/<folder>, and a rebuild leaves only that one.
++Only the signing and packing steps run (a stand-in app and framework, no Xcode); codesign is required for the third part.
+ """
+ import json
+ import os
+@@ -30,6 +32,9 @@ FRAMEWORK_SIGNING = SCRIPT[SCRIPT.index('# Route C: the framework is signed firs
+ SIGNING = SCRIPT[SCRIPT.index('# The entitlements of an unsigned IPA'):SCRIPT.index('\nP=Payload/DmIOS.app\n')]
+ PACKING = SCRIPT[SCRIPT.index('\nP=Payload/DmIOS.app\n'):]
+ PREPARE = SCRIPT[SCRIPT.index('if [ "$PREPARE_XCODE" = YES ]; then'):SCRIPT.index('\nelse DM_SCHEME_NAME=DmIOS; fi')] + '\nfi\n'
++# DM_FRAMEWORK_ONLY's block, after the helper it finds the shader libraries with
++FRAMEWORK_ONLY = (next(line for line in SCRIPT.splitlines() if line.startswith('lib_path() ')) + '\n' +
++ SCRIPT[SCRIPT.index('# DM_FRAMEWORK_ONLY: the framework target alone'):SCRIPT.index('\nt "xcodebuild (Release, unsigned, iPhone + iPad)"')] + '\n')
+ PREAMBLE = 'set -u\nt() { :; }; die() { echo "$*" >&2; exit 1; }\ngp() { python3 "$PROFILE_TOOL" "$GAME" "$1"; }\n'
+ CAN_SIGN = sys.platform == 'darwin' and shutil.which('codesign') and shutil.which('cc')
+
+@@ -207,5 +212,76 @@ class FrameworkPackagingTests(unittest.TestCase):
+ self.assertEqual(verified.returncode, 0, verified.stdout + verified.stderr)
+
+
[email protected](sys.platform == 'darwin', 'build_ipa.sh copies an image with macOS cp -c (clonefile)')
++class FrameworkOnlyShaderLibrariesTests(unittest.TestCase):
++ """DM_FRAMEWORK_ONLY's copy of the shader libraries into the game's framework, after a stand-in xcodebuild; no Xcode, no game files."""
++ FW = 'DmGame' + GAME
++
++ def setUp(self):
++ self.root = root = Path(tempfile.mkdtemp()).resolve()
++ self.addCleanup(shutil.rmtree, root)
++ (root / 'dm').mkdir()
++ (root / 'dm/tools').symlink_to(REPO / 'dm/tools', target_is_directory=True)
++ (root / 'bin').mkdir()
++ (root / 'bin/xcodebuild').write_text('#!/bin/sh\nexit 0\n')
++ (root / 'bin/xcodebuild').chmod(0o755)
++ built = root / 'dd/Build/Products/Release-iphoneos' / f'{self.FW}.framework' # what the framework target builds
++ built.mkdir(parents=True)
++ (built / self.FW).write_text('dylib')
++ (built / 'game.manifest.json').write_text(json.dumps({'framework_executable': self.FW}))
++ self.out = root / 'out' / f'{self.FW}.framework'
++
++ def library(self, packed, content='shader'):
++ """The NSP script's shader libraries: the folder dmsh_build.py writes, or the one file shader_lib.py packs it into."""
++ folder, image = self.root / 'shaders/b-fixture', self.root / 'shaders/b-fixture.img'
++ shutil.rmtree(folder, ignore_errors=True)
++ image.unlink(missing_ok=True)
++ folder.mkdir(parents=True)
++ (folder / 'fixture_b.metallib').write_text(content)
++ if packed:
++ result = subprocess.run([sys.executable, str(REPO / 'dm/tools/shader_lib.py'), 'pack', str(folder)], capture_output=True, text=True)
++ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
++ self.assertFalse(folder.exists())
++ self.assertTrue(image.is_file())
++ return image if packed else folder
++
++ def build(self):
++ root = self.root
++ result = run_step(FRAMEWORK_ONLY, root, FRAMEWORK_ONLY=str(root / 'out'), FW_NAME=self.FW, O=str(root / 'pkg'), DD=str(root / 'dd'),
++ DM_BUNDLE_ID='org.nxnative.fixture', MSL_IOS=str(root / 'shaders'), MSLDIR='b-fixture', PSO='',
++ ICON=str(root / 'no-icon.jpg'), PATH=f'{root / "bin"}:{os.environ["PATH"]}')
++ self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
++ self.assertTrue((self.out / self.FW).is_file())
++
++ def shaders(self):
++ return sorted(p.name for p in (self.out / 'msl').iterdir())
++
++ def test_a_packed_library_goes_in_as_its_one_file(self):
++ image = self.library(packed=True)
++ self.build()
++ self.assertEqual(self.shaders(), ['b-fixture.img'])
++ self.assertEqual((self.out / 'msl/b-fixture.img').read_bytes(), image.read_bytes())
++
++ def test_a_library_folder_goes_in_as_a_folder(self):
++ self.library(packed=False)
++ self.build()
++ self.assertEqual(self.shaders(), ['b-fixture'])
++ self.assertEqual((self.out / 'msl/b-fixture/fixture_b.metallib').read_text(), 'shader')
++
++ def test_a_rebuild_leaves_only_the_current_library(self):
++ (self.out / 'msl/b-fixture').mkdir(parents=True) # an earlier build's folder, and another chain's library
++ (self.out / 'msl/b-fixture/stale_b.metallib').write_text('stale')
++ (self.out / 'msl/b-otherchain.img').write_text('other chain')
++ self.library(packed=True)
++ self.build()
++ self.assertEqual(self.shaders(), ['b-fixture.img'])
++ image = self.library(packed=True, content='a newer shader')
++ self.build()
++ self.assertEqual((self.out / 'msl/b-fixture.img').read_bytes(), image.read_bytes())
++ self.library(packed=False)
++ self.build()
++ self.assertEqual(self.shaders(), ['b-fixture'])
++
++
+ if __name__ == '__main__':
+ unittest.main()
+diff --git a/dm/ios/tools/build_ipa.sh b/dm/ios/tools/build_ipa.sh
+index 9f67e73..707411c 100755
+--- a/dm/ios/tools/build_ipa.sh
++++ b/dm/ios/tools/build_ipa.sh
+@@ -255,7 +255,7 @@ if [ "$PREPARE_XCODE" = YES ]; then
+ fi
+
+ # DM_FRAMEWORK_ONLY: the framework target alone, unsigned (build_shell.sh's project embeds it and Xcode signs it with the shell), then the
+-# game's per-game resources into it, so one folder is one game: the shader libraries (msl/<MSLDIR>, the shell's DM_MSL_DIR), the pipeline
++# game's per-game resources into it, so one folder is one game: the shader libraries (msl/<MSLDIR> or msl/<MSLDIR>.img, msl/ the shell's DM_MSL_DIR), the pipeline
+ # archive (pso/, DM_PSO_BUNDLE) and the icon the shell's library shows. The game files and add-on content stay out (data, Documents/games/<id>/)
+ if [ -n "$FRAMEWORK_ONLY" ]; then
+ t "xcodebuild (Release, unsigned, the game framework $FW_NAME with its own host)"
+@@ -268,8 +268,17 @@ if [ -n "$FRAMEWORK_ONLY" ]; then
+ [ -f "$BUILT/$FW_NAME" ] || die "no built framework $BUILT"
+ OUTFW=$FRAMEWORK_ONLY/$FW_NAME.framework; mkdir -p "$OUTFW"
+ rsync -a --delete --exclude /msl --exclude /pso --exclude /icon.png -- "$BUILT/" "$OUTFW/" || die "copy the framework to $OUTFW"
+- mkdir -p "$OUTFW/msl/$MSLDIR" && rsync -a --delete -- "$MSL_IOS/$MSLDIR/" "$OUTFW/msl/$MSLDIR/" || die "copy the shader libraries"
+- for old in "$OUTFW/msl/"*; do [ "$old" = "$OUTFW/msl/$MSLDIR" ] || rm -rf "$old"; done
++ # An NVN game's libraries may be the one file b-<chain>.img (dm/tools/shader_lib.py): it goes in as msl/<MSLDIR>.img, where the host reads it (nvn_mtl.m), as stage_resources.sh copies it into an app
++ LIB=$(lib_path "$MSL_IOS/$MSLDIR") || die "shader libraries $MSL_IOS/$MSLDIR"
++ mkdir -p "$OUTFW/msl" || die "mkdir $OUTFW/msl"
++ if [ -f "$LIB" ]; then
++ rm -rf "$OUTFW/msl/$MSLDIR"
++ if [ -f "$OUTFW/msl/$MSLDIR.img" ]; then rsync -a -- "$LIB" "$OUTFW/msl/$MSLDIR.img"; else cp -cp -- "$LIB" "$OUTFW/msl/$MSLDIR.img"; fi || die "copy the shader libraries"
++ else
++ rm -f "$OUTFW/msl/$MSLDIR.img"
++ mkdir -p "$OUTFW/msl/$MSLDIR" && rsync -a --delete -- "$LIB/" "$OUTFW/msl/$MSLDIR/" || die "copy the shader libraries"
++ fi
++ for old in "$OUTFW/msl/"*; do case "$old" in "$OUTFW/msl/$MSLDIR"|"$OUTFW/msl/$MSLDIR.img") ;; *) rm -rf "$old";; esac; done
+ if [ -n "$PSO" ]; then mkdir -p "$OUTFW/pso" && rsync -a --delete -- "$PSO/" "$OUTFW/pso/" || die "copy the pipeline archive"; else rm -rf "$OUTFW/pso"; fi
+ if [ -f "$ICON" ]; then sips -s format png -z 512 512 "$ICON" --out "$OUTFW/icon.png" > /dev/null || die "sips: cannot convert the icon $ICON"; else rm -f "$OUTFW/icon.png"; fi
+ python3 -c 'import json, sys; m = json.load(open(sys.argv[1])); sys.exit(m["framework_executable"] != sys.argv[2])' "$OUTFW/game.manifest.json" "$FW_NAME" \
diff --git a/patches/series b/patches/series
index 8a2fffaf..8c569dc6 100644
--- a/patches/series
+++ b/patches/series
@@ -45,3 +45,4 @@
0075-front-mac-hide-the-pointer-before-the-game-takes.patch
0076-host-read-embedded-sections-from-the-image-that.patch
0077-ios-shell-link-the-host-into-each-game-s-framewo.patch
+0078-ios-shell-copy-a-game-s-packed-shader-image-into.patch