Skip to content

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

sync: align hle_fs (15 anchors) and rewrite main.m's per-game RomFS for upstream's image-based locate

LNPANDYcc57723d1780
diff --git a/dm/games/010020D01AD24000/smoke.tsv b/dm/games/010020D01AD24000/smoke.tsv
new file mode 100644
index 00000000..538ce518
--- /dev/null
+++ b/dm/games/010020D01AD24000/smoke.tsv
@@ -0,0 +1,10 @@
+# Fresh save into the opening room. Leave the character still for screenshots; movement/jump/pause are checked separately in the RomFS image comparison.
+save new
+keys f180:A,f360:A,f540:A,f720:A,f900:A
+secs 100
+quick 75
+reach 1200@65
+window 65 30
+shots 68,72,95
+hang 20
+prebuild off
diff --git a/dm/host/romfs_image.h b/dm/host/romfs_image.h
new file mode 100644
index 00000000..5da6b445
--- /dev/null
+++ b/dm/host/romfs_image.h
@@ -0,0 +1,72 @@
+// DMROMFS1's index loader, shared by the host mount and iOS startup. Startup must validate the same index before replacing a working folder.
+#pragma once
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+typedef struct { uint64_t a, b; uint32_t name; uint16_t nlen; uint8_t type, pad; } __attribute__((packed)) img_ent; // directory: a = first child, b = children; file: a = offset, b = size
+_Static_assert(sizeof(img_ent) == 24, "RomFS image entry layout");
+typedef struct { int fd; uint64_t n; img_ent *e; const char *names; } romfs_img;
+
+static void romfs_image_close(romfs_img *m) {
+ if (m) { close(m->fd); free(m->e); free(m); }
+}
+static int romfs_image_read(int fd, void *buf, size_t size, off_t off) {
+ size_t got = 0;
+ while (got < size) {
+ ssize_t n = pread(fd, (char *)buf + got, size - got, off + (off_t)got);
+ if (n < 0 && errno == EINTR) continue;
+ if (n <= 0) { if (!n) errno = EINVAL; return 0; }
+ got += (size_t)n;
+ }
+ return 1;
+}
+// NULL for an incomplete or invalid image (errno EINVAL), an I/O error, or allocation failure. The caller owns the descriptor and index on success.
+static romfs_img *romfs_image_open(const char *path) {
+ int fd = open(path, O_RDONLY); struct stat st; uint8_t h[32]; romfs_img *m = NULL; uint64_t ioff, isz, n;
+ if (fd < 0) return NULL;
+ if (fstat(fd, &st) || !romfs_image_read(fd, h, sizeof h, 0)) goto bad;
+ errno = EINVAL;
+ if (memcmp(h, "DMROMFS1", 8) || st.st_size < (off_t)sizeof h) goto bad;
+ memcpy(&ioff, h + 8, 8); memcpy(&isz, h + 16, 8); memcpy(&n, h + 24, 8);
+ if (!n || n > UINT32_MAX || ioff < sizeof h || ioff > (uint64_t)st.st_size || isz != (uint64_t)st.st_size - ioff || isz > SIZE_MAX || n > isz / sizeof(img_ent)) goto bad;
+ if (!(m = calloc(1, sizeof *m))) goto bad;
+ m->fd = fd;
+ if (!(m->e = malloc((size_t)isz)) || !romfs_image_read(fd, m->e, (size_t)isz, (off_t)ioff)) goto bad;
+ m->n = n; m->names = (const char *)m->e + n * sizeof(img_ent);
+ uint64_t pool = isz - n * sizeof(img_ent), next_child = 1;
+ errno = EINVAL;
+ if (m->e[0].type || m->e[0].nlen) goto bad;
+ for (uint64_t k = 0; k < n; k++) {
+ const img_ent *x = &m->e[k];
+ if ((uint64_t)x->name + x->nlen > pool || x->type > 1 || x->pad) goto bad;
+ const char *name = m->names + x->name;
+ if (k && (!x->nlen || x->nlen > 0x300 || memchr(name, 0, x->nlen) || memchr(name, '/', x->nlen) || (x->nlen == 1 && name[0] == '.') || (x->nlen == 2 && !memcmp(name, "..", 2)))) goto bad;
+ if (x->type) { if (x->a < sizeof h || x->a > ioff || x->b > ioff - x->a) goto bad; }
+ else {
+ // Breadth-first child runs partition entries 1..n-1; reject cycles, aliases and unreachable entries.
+ if (x->a != next_child || x->a <= k || x->a > n || x->b > n - x->a) goto bad;
+ next_child += x->b;
+ }
+ }
+ if (next_child != n) goto bad;
+ for (uint64_t k = 0; k < n; k++) {
+ const img_ent *d = &m->e[k]; if (d->type) continue;
+ for (uint64_t j = 1; j < d->b; j++) {
+ const img_ent *a = &m->e[d->a + j - 1], *b = a + 1;
+ size_t len = a->nlen < b->nlen ? a->nlen : b->nlen;
+ int c = memcmp(m->names + a->name, m->names + b->name, len);
+ if (c > 0 || (!c && a->nlen >= b->nlen)) goto bad;
+ }
+ }
+ return m;
+bad: {
+ int e = errno ? errno : EINVAL;
+ if (m) romfs_image_close(m); else close(fd);
+ errno = e; return NULL;
+ }
+}
diff --git a/scripts/sync_upstream.py b/scripts/sync_upstream.py
index 48a5c271..dee5fc61 100644
--- a/scripts/sync_upstream.py
+++ b/scripts/sync_upstream.py
@@ -670,9 +670,94 @@ static int g_bc_cpu; // decode BC on the CPU: no GPU support and the profi
done(f'nvn_mtl.m {name}')
return t
-MAIN_M_ROMFS = ('static NSString *romfs_locate(void) {\n NSFileManager *fm = NSFileManager.defaultManager;\n NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];\n for (NSString *d in @[[doc stringByAppendingPathComponent:@"romfs"], [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs"]])\n if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]) return d; // only a directory containing the game profile\'s romfs_marker path counts (Mario: System/Resource, the resource size table)\n return nil;\n}\n\n', '// A folder holds this game\'s files if it has a ROMFSARC index or the profile\'s marker path.\n// Shared by romfs_locate and the per-game layout below, so both answer the same question.\nstatic BOOL romfs_dir_ok(NSString *d) {\n NSFileManager *fm = NSFileManager.defaultManager;\n // A ROMFSARC archive: <dir>/romfs.idx next to romfs.NN shards. The host opens it when DM_ROMFS\n // points at the base; the marker check does not apply (there are no directories to walk).\n if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@"romfs.idx"]]) return YES;\n // A plain directory tree: only one containing the game profile\'s romfs_marker path counts\n // (Mario: System/Resource, the resource size table). Runtime profile: the marker is a profile\n // value now; the compiled macro is only what dm_profile() falls back to without DM_PROFILE.\n return [fm fileExistsAtPath:[d stringByAppendingPathComponent:@(dm_profile()->romfs_marker)]];\n}\n\n// Where one game\'s data lives under the per-game layout: Documents/games/<title ID>/, holding\n// romfs/ the archive (romfs.idx + shards) or a plain tree\n// msl/ this game\'s Metal shader libraries (optional; the app\'s own dm/ is used otherwise)\n// save/ temp/ this game\'s saves and scratch space\n// The layout exists so several games can share one installed app and so the game data can sit on\n// an external drive the Files app exposes (a folder copied there keeps this shape).\nstatic NSString *game_dir_for(const char *title_id) {\n NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];\n return [[doc stringByAppendingPathComponent:@"games"] stringByAppendingPathComponent:@(title_id)];\n}\n\nstatic NSString *romfs_locate(void) {\n NSFileManager *fm = NSFileManager.defaultManager;\n NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];\n // Per-game first: Documents/games/<id>/romfs — what the launcher-style layout produces, and what\n // lets a user keep several games\' data side by side (or on an external drive).\n NSString *per_game = [game_dir_for(dm_profile()->id) stringByAppendingPathComponent:@"romfs"];\n if ([fm fileExistsAtPath:per_game] && romfs_dir_ok(per_game)) return per_game;\n for (NSString *d in @[[doc stringByAppendingPathComponent:@"romfs"], [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs"]]) {\n if (romfs_dir_ok(d)) return d;\n }\n return nil;\n}\n\n')
-MAIN_M_ENV = (' setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0);\n setenv("DM_SAVE", [doc stringByAppendingPathComponent:@"save"].fileSystemRepresentation, 0);\n setenv("DM_TEMP", [doc stringByAppendingPathComponent:@"temp"].fileSystemRepresentation, 0);\n NSString *dm = [res stringByAppendingPathComponent:@"dm"];\n setenv("DM_NVN_SHMAP", [dm stringByAppendingPathComponent:@"shader_map.tsv"].fileSystemRepresentation, 0);\n setenv("DM_MSL_DIR", [res stringByAppendingPathComponent:@"msl"].fileSystemRepresentation, 0);\n', ' setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0);\n // Saves and scratch space: Documents/games/<id>/{save,temp} when the per-game layout is in use\n // (so two games in one app never share a save folder), else the old Documents/{save,temp}. An\n // env file still wins — DM_SAVE/DM_TEMP set there are already in the environment and\n // setenv(..., 0) leaves them alone.\n NSString *gdir = game_dir_for(dm_profile()->id);\n // Tie the data folder to where the game files actually came from: if romfs_locate fell back to\n // Documents/romfs, the saves must stay beside it (a game folder without romfs is not in use).\n BOOL per_game = [romfs hasPrefix:gdir];\n NSString *save_dir = per_game ? [gdir stringByAppendingPathComponent:@"save"] : [doc stringByAppendingPathComponent:@"save"];\n NSString *temp_dir = per_game ? [gdir stringByAppendingPathComponent:@"temp"] : [doc stringByAppendingPathComponent:@"temp"];\n for (NSString *d in @[save_dir, temp_dir]) [fm createDirectoryAtPath:d withIntermediateDirectories:YES attributes:nil error:nil];\n setenv("DM_SAVE", save_dir.fileSystemRepresentation, 0);\n setenv("DM_TEMP", temp_dir.fileSystemRepresentation, 0);\n if (per_game) fprintf(stderr, "boot: per-game data folder %s\\n", gdir.fileSystemRepresentation);\n NSString *dm = [res stringByAppendingPathComponent:@"dm"];\n setenv("DM_NVN_SHMAP", [dm stringByAppendingPathComponent:@"shader_map.tsv"].fileSystemRepresentation, 0);\n // Shader libraries: a game\'s own <gdir>/msl first (a per-game layout may carry its own), else\n // the app\'s shared <App>/msl.\n NSString *per_game_msl = [gdir stringByAppendingPathComponent:@"msl"];\n NSString *msl = (per_game && [fm fileExistsAtPath:per_game_msl]) ? per_game_msl : [res stringByAppendingPathComponent:@"msl"];\n setenv("DM_MSL_DIR", msl.fileSystemRepresentation, 0);\n')
+MAIN_M_ROMFS = ('''static NSString *romfs_locate(void) {
+ NSFileManager *fm = NSFileManager.defaultManager;
+ NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
+ NSString *img = [doc stringByAppendingPathComponent:@"romfs.img"];
+ if (romfs_image_complete(img)) {
+ // A folder copy from before images is now dead weight of up to many GB (often cut off partway). Deleting its 300,000 files here took longer than iOS allows an app to start
+ // (Tears of the Kingdom: killed on a black screen), so it is moved aside at once and deleted in the background, leftovers of an interrupted delete included
+ NSString *old = [doc stringByAppendingPathComponent:@"romfs"], *tmp = NSTemporaryDirectory();
+ if ([fm fileExistsAtPath:old] && [fm moveItemAtPath:old toPath:[tmp stringByAppendingPathComponent:[@"romfs-old-" stringByAppendingString:NSUUID.UUID.UUIDString]] error:nil])
+ fprintf(stderr, "boot: Documents/romfs moved aside for deletion (Documents/romfs.img replaces it)\\n");
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
+ NSFileManager *bfm = [NSFileManager new];
+ for (NSString *n in [bfm contentsOfDirectoryAtPath:tmp error:nil]) if ([n hasPrefix:@"romfs-old-"]) [bfm removeItemAtPath:[tmp stringByAppendingPathComponent:n] error:nil];
+ });
+ return img;
+ }
+ NSString *bundledImage = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs.img"];
+ NSString *documentFolder = [doc stringByAppendingPathComponent:@"romfs"];
+ if ([fm fileExistsAtPath:[documentFolder stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]) return documentFolder;
+ if (romfs_image_complete(bundledImage)) return bundledImage;
+ for (NSString *d in @[[doc stringByAppendingPathComponent:@"romfs"], [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs"]])
+ if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@DM_GAME_ROMFS_MARKER]]) return d; // only a directory containing the game profile\'s romfs_marker path counts (Mario: System/Resource, the resource size table)
+ return nil;
+}
+
+''', '''// A folder holds this game\'s files if it has a ROMFSARC index or the profile\'s marker path.
+// Shared by romfs_locate and the per-game layout below, so both answer the same question.
+static BOOL romfs_dir_ok(NSString *d) {
+ NSFileManager *fm = NSFileManager.defaultManager;
+ // A ROMFSARC archive: <dir>/romfs.idx next to romfs.NN shards. The host opens it when DM_ROMFS
+ // points at the base; the marker check does not apply (there are no directories to walk).
+ if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@"romfs.idx"]]) return YES;
+ // A plain directory tree: only one containing the game profile\'s romfs_marker path counts
+ // (Mario: System/Resource, the resource size table). Runtime profile: the marker is a profile
+ // value now; the compiled macro is only what dm_profile() falls back to without DM_PROFILE.
+ return [fm fileExistsAtPath:[d stringByAppendingPathComponent:@(dm_profile()->romfs_marker)]];
+}
+
+// Where one game\'s data lives under the per-game layout: Documents/games/<title ID>/, holding
+// romfs.img the whole RomFS as one image file (what scripts/push_romfs.sh sends), or
+// romfs/ a ROMFSARC archive or a plain tree, for copies made before images
+// msl/ this game\'s Metal shader libraries (optional; the app\'s own dm/ is used otherwise)
+// save/ temp/ this game\'s saves and scratch space
+// The layout exists so several games can share one installed app and so the game data can sit on
+// an external drive the Files app exposes (a folder copied there keeps this shape).
+static NSString *game_dir_for(const char *title_id) {
+ NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
+ return [[doc stringByAppendingPathComponent:@"games"] stringByAppendingPathComponent:@(title_id)];
+}
+static NSString *romfs_locate(void) {
+ NSFileManager *fm = NSFileManager.defaultManager;
+ NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
+ // Per-game first: Documents/games/<id>/romfs.img (one file, what push_romfs.sh sends) or
+ // Documents/games/<id>/romfs/ — the launcher-style layout, and what lets a user keep several
+ // games\' data side by side or on an external drive. The marker check inside romfs_dir_ok is
+ // what keeps a stray folder from being taken for this game.
+ NSString *gdir = game_dir_for(dm_profile()->id);
+ NSString *gimg = [gdir stringByAppendingPathComponent:@"romfs.img"];
+ if (romfs_image_complete(gimg)) return gimg;
+ NSString *gfolder = [gdir stringByAppendingPathComponent:@"romfs"];
+ if ([fm fileExistsAtPath:gfolder] && romfs_dir_ok(gfolder)) return gfolder;
+ NSString *img = [doc stringByAppendingPathComponent:@"romfs.img"];
+ if (romfs_image_complete(img)) {
+ // A folder copy from before images is now dead weight of up to many GB (often cut off partway). Deleting its 300,000 files here took longer than iOS allows an app to start
+ // (Tears of the Kingdom: killed on a black screen), so it is moved aside at once and deleted in the background, leftovers of an interrupted delete included
+ NSString *old = [doc stringByAppendingPathComponent:@"romfs"], *tmp = NSTemporaryDirectory();
+ if ([fm fileExistsAtPath:old] && [fm moveItemAtPath:old toPath:[tmp stringByAppendingPathComponent:[@"romfs-old-" stringByAppendingString:NSUUID.UUID.UUIDString]] error:nil])
+ fprintf(stderr, "boot: Documents/romfs moved aside for deletion (Documents/romfs.img replaces it)\\n");
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
+ NSFileManager *bfm = [NSFileManager new];
+ for (NSString *n in [bfm contentsOfDirectoryAtPath:tmp error:nil]) if ([n hasPrefix:@"romfs-old-"]) [bfm removeItemAtPath:[tmp stringByAppendingPathComponent:n] error:nil];
+ });
+ return img;
+ }
+ NSString *bundledImage = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs.img"];
+ NSString *documentFolder = [doc stringByAppendingPathComponent:@"romfs"];
+ if ([fm fileExistsAtPath:[documentFolder stringByAppendingPathComponent:@(dm_profile()->romfs_marker)]]) return documentFolder;
+ if (romfs_image_complete(bundledImage)) return bundledImage;
+ for (NSString *d in @[[doc stringByAppendingPathComponent:@"romfs"], [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:@"romfs"]])
+ if (romfs_dir_ok(d)) return d;
+ return nil;
+}
+
+''')
+
+
+MAIN_M_ENV = (' setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0);\n setenv("DM_SAVE", [doc stringByAppendingPathComponent:@"save"].fileSystemRepresentation, 0);\n setenv("DM_TEMP", [doc stringByAppendingPathComponent:@"temp"].fileSystemRepresentation, 0);\n NSString *dm = [res stringByAppendingPathComponent:@"dm"];\n setenv("DM_NVN_SHMAP", [dm stringByAppendingPathComponent:@"shader_map.tsv"].fileSystemRepresentation, 0);\n setenv("DM_MSL_DIR", [res stringByAppendingPathComponent:@"msl"].fileSystemRepresentation, 0);\n', ' setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0);\n // Saves and scratch space: Documents/games/<id>/{save,temp} when the per-game layout is in use\n // (so two games in one app never share a save folder), else the old Documents/{save,temp}. An\n // env file still wins — DM_SAVE/DM_TEMP set there are already in the environment and\n // setenv(..., 0) leaves them alone.\n NSString *gdir = game_dir_for(dm_profile()->id);\n // Tie the data folder to where the game files actually came from: if romfs_locate fell back to\n // Documents/romfs, the saves must stay beside it (a game folder without romfs is not in use).\n BOOL per_game = [romfs hasPrefix:gdir];\n NSString *save_dir = per_game ? [gdir stringByAppendingPathComponent:@"save"] : [doc stringByAppendingPathComponent:@"save"];\n NSString *temp_dir = per_game ? [gdir stringByAppendingPathComponent:@"temp"] : [doc stringByAppendingPathComponent:@"temp"];\n for (NSString *d in @[save_dir, temp_dir]) [fm createDirectoryAtPath:d withIntermediateDirectories:YES attributes:nil error:nil];\n setenv("DM_SAVE", save_dir.fileSystemRepresentation, 0);\n setenv("DM_TEMP", temp_dir.fileSystemRepresentation, 0);\n if (per_game) fprintf(stderr, "boot: per-game data folder %s\\n", gdir.fileSystemRepresentation);\n NSString *dm = [res stringByAppendingPathComponent:@"dm"];\n setenv("DM_NVN_SHMAP", [dm stringByAppendingPathComponent:@"shader_map.tsv"].fileSystemRepresentation, 0);\n // Shader libraries: a game\'s own <gdir>/msl first (a per-game layout may carry its own), else\n // the app\'s shared <App>/msl.\n NSString *per_game_msl = [gdir stringByAppendingPathComponent:@"msl"];\n NSString *msl = (per_game && [fm fileExistsAtPath:per_game_msl]) ? per_game_msl : [res stringByAppendingPathComponent:@"msl"];\n setenv("DM_MSL_DIR", msl.fileSystemRepresentation, 0);\n')
def patch_main_m(t):
"""romfs_locate + the per-game data layout in dm/ios/Sources/main.m.
@@ -760,7 +845,7 @@ MARKERS = {
'dm/ios/tools/build_ipa.sh': 'DM_FULL_ROMFS',
'scripts/build_ipa_from_nsp.py': '--work-dir',
'dm/gfx/tools/shader_split.py': "ue_inline_shaders(d, f'/{rel}#{label}', out)",
- 'dm/ios/Sources/main.m': 'romfs.idx',
+ 'dm/ios/Sources/main.m': 'romfs_dir_ok',
'dm/gfx/nvn/nvn_mtl.m': 'if (on_sub_thread()) rec_publish();',
'dm/games/0100A00019DE0000/game.tsv': 'build v1 1.0.137848',
'dm/host/dm.h': 'hle_profile.h',