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
No game data yet boots into the launcher, and Play (re)locates the data
LNPANDY420047dab468
diff --git a/dm/ios/Sources/Launcher.swift b/dm/ios/Sources/Launcher.swiftindex 98170f75..1e21e9c9 100644--- a/dm/ios/Sources/Launcher.swift+++ b/dm/ios/Sources/Launcher.swift@@ -98,7 +98,7 @@ private struct GameDataSection: View {DocumentPicker { url inpicking = falseif dm_store_picked_data(url) {- note = "Saved. The game uses this location from the next start."+ note = "Saved. The game uses this location when you press Play."refresh()} else {note = "That location could not be remembered — try a different one."diff --git a/dm/ios/Sources/front_ios.m b/dm/ios/Sources/front_ios.mindex 9cd03bad..fef3aa8a 100644--- a/dm/ios/Sources/front_ios.m+++ b/dm/ios/Sources/front_ios.m@@ -15,6 +15,7 @@ void dm_gamepad_reset(void);void front_ios_boot(void);int front_ios_boot_ok(void);bool dm_show_launcher(void *view_controller, void (*done)(void)); // Launcher.swift: false = no launcher (no dm/launcher.json, or DM_LAUNCHER=0)+int dm_locate_game_data(void); // main.m: (re)decide where the game data is; NO when there still is nonestatic CAMetalLayer *g_layer;static void front_ios_power_watch(void);@@ -46,6 +47,7 @@ static UILabel *front_ios_fps_label(void);@property (nonatomic, strong) UIView *pad;@property (nonatomic, strong) UILabel *fps;@property (nonatomic) BOOL launched;+@property (nonatomic) BOOL hasLauncher; // the app has a settings screen: a failed Play goes back to it (its Game data section is the way out)- (void)startGame;@endstatic DmRootVC *g_root;@@ -67,6 +69,14 @@ static void launcher_done(void) { [g_root startGame]; }// The game view, the pad and the frame-rate counter are made once the launcher has set the switches they read (DM_SHOW_FPS, DM_METAL_HUD); the host's loading began at app launch (main.m)- (void)startGame {if (self.game) return;+ if (!dm_locate_game_data()) { // no game data (nothing picked, nothing in Documents): the game cannot start+ if (self.hasLauncher) { dm_show_launcher((__bridge void *)self, launcher_done); return; } // back to the settings screen, whose Game data section picks a location+ UILabel *lb = [[UILabel alloc] initWithFrame:CGRectInset(self.view.bounds, 40, 40)];+ lb.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;+ lb.numberOfLines = 0; lb.textAlignment = NSTextAlignmentCenter; lb.textColor = UIColor.whiteColor;+ lb.text = @"Game files not found.\nPut the RomFS folder into this App's Documents as \"romfs\" (Files app), then restart.";+ [self.view addSubview:lb]; return;+ }front_hud_env(); // before the game view makes its device (an env file's DM_METAL_HUD sets it in main.m already)self.game = [[DmGameView alloc] initWithFrame:self.view.bounds];[self.view addSubview:self.game];@@ -97,7 +107,8 @@ static void launcher_done(void) { [g_root startGame]; }[super viewDidAppear:a];if (self.launched || !front_ios_boot_ok()) return;self.launched = YES; g_root = self;- if (!dm_show_launcher((__bridge void *)self, launcher_done)) [self startGame]; // the settings screen first (Launcher.swift), when the app has one+ self.hasLauncher = dm_show_launcher((__bridge void *)self, launcher_done);+ if (!self.hasLauncher) [self startGame]; // the settings screen first (Launcher.swift), when the app has one}- (BOOL)prefersHomeIndicatorAutoHidden { return YES; }- (UIRectEdge)preferredScreenEdgesDeferringSystemGestures { return UIRectEdgeAll; } // Edge gestures need two swipes, to avoid accidental exitsdiff --git a/dm/ios/Sources/main.m b/dm/ios/Sources/main.mindex 1393523e..b546e1bf 100644--- a/dm/ios/Sources/main.m+++ b/dm/ios/Sources/main.m@@ -101,6 +101,41 @@ static NSString *romfs_locate(void) {return nil;}+// DM_ROMFS plus the save/temp folders that follow its location (the per-game layout only when the+// files came from Documents/games/<id>/). Returns NO when there is no game data yet — not fatal:+// the launcher comes up all the same and its Game data section is where a location gets picked+// (a transfer skipped on the Mac, or data on an external drive), and dm_locate_game_data() asks+// again when the player presses Play. `force` lets the Play-time re-location override what boot+// set: a location picked in the launcher this session must win, while an env file's DM_ROMFS /+// DM_SAVE / DM_TEMP (set before this runs, kept only when absent) keeps winning at boot.+static int set_data_env(NSString *romfs, BOOL force) {+ if (!romfs) return 0;+ NSFileManager *fm = NSFileManager.defaultManager;+ NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];+ fprintf(stderr, "boot: game files %s\n", romfs.fileSystemRepresentation);+ setenv("DM_ROMFS", romfs.fileSystemRepresentation, force);+ // Saves and scratch space: Documents/games/<id>/{save,temp} when the per-game layout is in use+ // (so two games in one app never share a save folder), else the old Documents/{save,temp}. An+ // env file still wins at boot — DM_SAVE/DM_TEMP set there are already in the environment and+ // setenv(..., 0) leaves them alone.+ NSString *gdir = game_dir_for(dm_profile()->id);+ // Tie the data folder to where the game files actually came from: if romfs_locate fell back to+ // Documents/romfs, the saves must stay beside it (a game folder without romfs is not in use).+ BOOL per_game = [romfs hasPrefix:gdir];+ NSString *save_dir = per_game ? [gdir stringByAppendingPathComponent:@"save"] : [doc stringByAppendingPathComponent:@"save"];+ NSString *temp_dir = per_game ? [gdir stringByAppendingPathComponent:@"temp"] : [doc stringByAppendingPathComponent:@"temp"];+ for (NSString *d in @[save_dir, temp_dir]) [fm createDirectoryAtPath:d withIntermediateDirectories:YES attributes:nil error:nil];+ setenv("DM_SAVE", save_dir.fileSystemRepresentation, force);+ setenv("DM_TEMP", temp_dir.fileSystemRepresentation, force);+ if (per_game) fprintf(stderr, "boot: per-game data folder %s\n", gdir.fileSystemRepresentation);+ return 1;+}++// front_ios.m calls this from the launcher's Play: (re)decide where the game data is — a location+// picked in the launcher this session takes effect at once — and NO when there still is none, so+// the launcher comes back instead of the game failing inside the host.+int dm_locate_game_data(void) { return set_data_env(romfs_locate(), YES); }+// Boot prerequisites (done before UIApplicationMain): directories / environment variables / log -- the host's early load reads thesestatic int boot_env(void) {NSFileManager *fm = NSFileManager.defaultManager;@@ -134,24 +169,12 @@ static int boot_env(void) {NSString *lang = [NSBundle.mainBundle objectForInfoDictionaryKey:@"DMLanguage"];if (!getenv("DM_LANG") && [lang isKindOfClass:NSString.class] && lang.length) setenv("DM_LANG", lang.UTF8String, 0);fprintf(stderr, "boot: game language %s\n", getenv("DM_LANG") ?: "en-US");- NSString *romfs = romfs_locate();- if (!romfs) { fprintf(stderr, "boot: game files not found (Documents/romfs.img, Documents/romfs or <App>/romfs)\n"); return 0; }- fprintf(stderr, "boot: game files %s\n", romfs.fileSystemRepresentation);- setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0);- // Saves and scratch space: Documents/games/<id>/{save,temp} when the per-game layout is in use- // (so two games in one app never share a save folder), else the old Documents/{save,temp}. An- // env file still wins — DM_SAVE/DM_TEMP set there are already in the environment and- // setenv(..., 0) leaves them alone.+ // No game data yet is not fatal: the launcher comes up all the same, and its Game data section+ // is where a location gets picked; dm_locate_game_data() looks again when Play is pressed.NSString *gdir = game_dir_for(dm_profile()->id);- // Tie the data folder to where the game files actually came from: if romfs_locate fell back to- // Documents/romfs, the saves must stay beside it (a game folder without romfs is not in use).- BOOL per_game = [romfs hasPrefix:gdir];- NSString *save_dir = per_game ? [gdir stringByAppendingPathComponent:@"save"] : [doc stringByAppendingPathComponent:@"save"];- NSString *temp_dir = per_game ? [gdir stringByAppendingPathComponent:@"temp"] : [doc stringByAppendingPathComponent:@"temp"];- for (NSString *d in @[save_dir, temp_dir]) [fm createDirectoryAtPath:d withIntermediateDirectories:YES attributes:nil error:nil];- setenv("DM_SAVE", save_dir.fileSystemRepresentation, 0);- setenv("DM_TEMP", temp_dir.fileSystemRepresentation, 0);- if (per_game) fprintf(stderr, "boot: per-game data folder %s\n", gdir.fileSystemRepresentation);+ NSString *romfs = romfs_locate();+ BOOL per_game = [romfs hasPrefix:gdir]; // messaging nil is NO here: no data means the shared layout+ if (!set_data_env(romfs, NO)) fprintf(stderr, "boot: no game data yet (Documents/romfs.img, Documents/romfs or <App>/romfs); the launcher asks for it\n");NSString *dm = [res stringByAppendingPathComponent:@"dm"];setenv("DM_NVN_SHMAP", [dm stringByAppendingPathComponent:@"shader_map.tsv"].fileSystemRepresentation, 0);// Shader libraries: a game's own <gdir>/msl first (a per-game layout may carry its own), else