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
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: v0lumehi <[email protected]>
Date: Wed, 28 Oct 2026 13:33:53 +0100
Subject: [PATCH] iOS: per-game data folders and a Files-app data pick
main.m locates the RomFS in the picked location, then Documents/games/<id>/, then the old Documents layout; saves and
temp follow it. Launcher.swift gets a Game data section; front_ios.m goes back to it when no data is found.
Upstream-Status: pending
Reason: Several games in one app, game data on an external drive: Documents/games/<id>/{romfs.img,romfs,msl,save,temp} and a Files-app pick of the data folder (needs the runtime profile). iOS twin of seedds/nxnative#460, which is Mac only. Also add-on content: games/<id>/aoc/<index>.img becomes DM_AOC_DIR (a slim IPA carries none, LNPANDY/NX2Apple#202), before the Documents/aoc and <App>/aoc that seedds/nxnative#486 reads, and never over an explicit value. Also DM_CHEATS=games/<id>/cheats (never over an explicit value), where the launcher's Cheats sheet copies a game's cheat files (LNPANDY/NX2Apple#97)
Issue: LNPANDY/NX2Apple#123
---
dm/ios/Sources/DataBookmark.h | 32 ++++++
dm/ios/Sources/DataBookmark.m | 79 ++++++++++++++
dm/ios/Sources/DmIOS-Bridging-Header.h | 7 +-
dm/ios/Sources/Launcher.swift | 63 ++++++++++-
dm/ios/Sources/front_ios.m | 13 ++-
dm/ios/Sources/main.m | 140 ++++++++++++++++++++++---
dm/ios/project.yml | 2 +-
dm/ios/tests/test_aoc_layouts.py | 92 ++++++++++++++++
dm/ios/tests/test_romfs_startup.py | 49 ++++++++-
9 files changed, 454 insertions(+), 23 deletions(-)
create mode 100644 dm/ios/Sources/DataBookmark.h
create mode 100644 dm/ios/Sources/DataBookmark.m
create mode 100644 dm/ios/tests/test_aoc_layouts.py
diff --git a/dm/ios/Sources/DataBookmark.h b/dm/ios/Sources/DataBookmark.h
new file mode 100644
index 0000000..3aef333
--- /dev/null
+++ b/dm/ios/Sources/DataBookmark.h
@@ -0,0 +1,32 @@
+// The game data the player picked from the Files app, remembered across launches.
+//
+// Why this exists: until now the only way to get a game's RomFS onto an iPhone was to copy it into
+// the app's Documents through the Files app — which the Files app can do, but only for a folder
+// *inside* the app's own container. A player who keeps 16 GB of game data on an external drive or
+// in iCloud Drive had to duplicate it into every game's container.
+//
+// iOS does not allow an app to remember a path outside its sandbox. It allows a *bookmark*: the
+// system hands back a token when the player picks a file in the document picker, and the app can
+// resolve that token later — even after a relaunch — and keep reading that file. The token is bound
+// to the app's signing identity, so a re-signed build (a new sideload, a new team) gets a stale one;
+// `stale` below is how that is reported, and the launcher asks the player to pick again rather than
+// failing silently.
+//
+// This file only holds and resolves the bookmark. The picker UI and the call that stores a new one
+// are in LauncherSettings.swift, which is where the rest of the player-facing settings live.
+
+#import <Foundation/Foundation.h>
+
+/// The bookmark the player stored, or nil when none was ever picked.
+NSData *dm_picked_data_bookmark(void);
+
+/// Resolve the stored bookmark to a path, or nil when there is none or it no longer resolves.
+/// `stale` (optional) is set when a bookmark exists but could not be used — the app is not signed
+/// the way it was when the player picked, so the picker has to be shown again.
+NSString *dm_picked_data_path(BOOL *stale);
+
+/// Store a bookmark for a URL the player just picked. Returns NO when the system refuses to make one.
+BOOL dm_store_picked_data(NSURL *url);
+
+/// Forget it (a player who wants the app to go back to its Documents copy).
+void dm_clear_picked_data(void);
diff --git a/dm/ios/Sources/DataBookmark.m b/dm/ios/Sources/DataBookmark.m
new file mode 100644
index 0000000..3dcc383
--- /dev/null
+++ b/dm/ios/Sources/DataBookmark.m
@@ -0,0 +1,79 @@
+// See DataBookmark.h for why this exists.
+
+#import "DataBookmark.h"
+
+static NSString *const kPickedData = @"pickedDataBookmark";
+
+// The one security scope the game data lives under, held from the first resolve on: the host reads
+// the image for the whole session, and a scope released after each read would break the game half
+// way through. Never stopped on purpose — the process exits when the game does.
+static NSURL *openedPick = nil;
+
+NSData *dm_picked_data_bookmark(void) {
+ return [NSUserDefaults.standardUserDefaults dataForKey:kPickedData];
+}
+
+static NSData *fresh_bookmark(NSURL *url) {
+ // The picker grants a security scope that lasts while it is held: the bookmark is written with
+ // the scope on. One written without it (or after the grant expired) reads back on this run and
+ // resolves to nothing from the next launch on — every pick then looks "signed differently".
+ BOOL scoped = [url startAccessingSecurityScopedResource];
+ NSData *bookmark = [url bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:nil];
+ if (scoped) [url stopAccessingSecurityScopedResource];
+ return bookmark;
+}
+
+NSString *dm_picked_data_path(BOOL *stale) {
+ if (stale) *stale = NO;
+ NSData *bookmark = dm_picked_data_bookmark();
+ if (bookmark.length == 0) return nil;
+ BOOL isStale = NO;
+ NSError *error = nil;
+ NSURL *url = [NSURL URLByResolvingBookmarkData:bookmark
+ options:0
+ relativeToURL:nil
+ bookmarkDataIsStale:&isStale
+ error:&error];
+ if (!url) {
+ // A re-signed build gets a bookmark the system will not resolve for it. Keep the data —
+ // the player may install the same signature again — and let the caller ask for a new pick.
+ fprintf(stderr, "boot: the picked game-data bookmark does not resolve (%s)\n",
+ error.localizedDescription.UTF8String ?: "no reason given");
+ if (stale) *stale = YES;
+ return nil;
+ }
+ if (openedPick && ![openedPick.path isEqualToString:url.path]) openedPick = nil;
+ if (!openedPick) [url startAccessingSecurityScopedResource]; // held for the session (see openedPick)
+ openedPick = url;
+ if (isStale) {
+ // The system still resolves it but asks for a rewrite (its own wording: stale). Same
+ // signature, so a fresh bookmark sticks: self-heal instead of making the player pick again.
+ NSData *again = fresh_bookmark(url);
+ if (again) [NSUserDefaults.standardUserDefaults setObject:again forKey:kPickedData];
+ if (stale) *stale = YES;
+ }
+ // The picker can hand back a file that was moved or deleted since. The caller checks what it
+ // needs (an image, a folder with the marker) and falls through if this path no longer holds it.
+ return url.path;
+}
+
+BOOL dm_store_picked_data(NSURL *url) {
+ NSData *bookmark = fresh_bookmark(url);
+ NSError *error = nil;
+ NSURL *back = nil;
+ if (bookmark) back = [NSURL URLByResolvingBookmarkData:bookmark options:0 relativeToURL:nil bookmarkDataIsStale:nil error:&error];
+ if (!bookmark || !back) {
+ // A bookmark that cannot come back must not be saved as if it were good: the launcher would
+ // greet every start with "the location you picked before is no longer readable".
+ fprintf(stderr, "[RED] could not bookmark %s: %s\n", url.path.UTF8String ?: "?",
+ error.localizedDescription.UTF8String ?: (bookmark ? "the bookmark does not resolve again" : "no bookmark data"));
+ return NO;
+ }
+ [NSUserDefaults.standardUserDefaults setObject:bookmark forKey:kPickedData];
+ fprintf(stderr, "launcher: game data set to %s\n", url.path.UTF8String ?: "?");
+ return YES;
+}
+
+void dm_clear_picked_data(void) {
+ [NSUserDefaults.standardUserDefaults removeObjectForKey:kPickedData];
+}
diff --git a/dm/ios/Sources/DmIOS-Bridging-Header.h b/dm/ios/Sources/DmIOS-Bridging-Header.h
index 7ba971c..4ea3995 100644
--- a/dm/ios/Sources/DmIOS-Bridging-Header.h
+++ b/dm/ios/Sources/DmIOS-Bridging-Header.h
@@ -1,3 +1,8 @@
-// The Objective-C functions Launcher.swift and GameMenu.swift call (CloudSaves.h, DmGameMenu.h). project.yml points SWIFT_OBJC_BRIDGING_HEADER here.
+// The Objective-C functions Launcher.swift and GameMenu.swift call (CloudSaves.h, DataBookmark.h, DmGameMenu.h). project.yml points SWIFT_OBJC_BRIDGING_HEADER here.
#import "CloudSaves.h"
+#import "DataBookmark.h"
#import "DmGameMenu.h"
+
+// main.m: set when the game data found is an old shard archive (romfs.idx), which is no longer read. The sentence names the
+// action (re-send the game data as romfs.img from the launcher), or NULL. The launcher's Game data section shows it.
+const char *dm_old_archive_message(void);
diff --git a/dm/ios/Sources/Launcher.swift b/dm/ios/Sources/Launcher.swift
index 517e593..6013b30 100644
--- a/dm/ios/Sources/Launcher.swift
+++ b/dm/ios/Sources/Launcher.swift
@@ -10,6 +10,64 @@ import os
private func info(_ key: String) -> Any? { Bundle.main.object(forInfoDictionaryKey: key) }
+/// Where this game's data comes from, and a picker to change it.
+///
+/// The app can only read files it owns unless the player hands it a location through the document
+/// picker — so a game's RomFS kept on an external drive or in iCloud Drive could not be used at all
+/// before this. Picking an image (romfs.img) or a folder stores a bookmark (DataBookmark.m), and
+/// romfs_locate() prefers it from the next launch on.
+private struct GameDataSection: View {
+ @State private var picked: String = ""
+ @State private var note: String = ""
+ @State private var picking = false
+
+ var body: some View {
+ Section {
+ HStack {
+ Text("Game data")
+ Spacer()
+ Text(picked.isEmpty ? "This app's folder" : (picked as NSString).lastPathComponent)
+ .foregroundStyle(.secondary).lineLimit(1).truncationMode(.middle)
+ }
+ Button(picked.isEmpty ? "Choose game data…" : "Choose a different location…") { picking = true }
+ if !picked.isEmpty {
+ Button("Use the app's own data again", role: .destructive) {
+ dm_clear_picked_data(); refresh(); note = ""
+ }
+ }
+ } footer: {
+ Text(note.isEmpty
+ ? "Pick the game's romfs.img or its RomFS folder — an external drive or iCloud Drive works. The app remembers the choice; a re-signed build asks once more."
+ : note)
+ }
+ .sheet(isPresented: $picking) {
+ DocumentPicker(types: [.item]) { url in
+ picking = false
+ if dm_store_picked_data(url) {
+ note = "Saved. The game uses this location when you press Play."
+ refresh()
+ } else {
+ note = "That location could not be remembered — try a different one."
+ }
+ } onCancel: { picking = false }
+ }
+ .onAppear(perform: refresh)
+ }
+
+ private func refresh() {
+ // ObjC BOOL* is ObjCBool, not Bool: the bookmark header takes an indirect flag for
+ // "the bookmark exists but does not resolve under this signature", which is the one
+ // state worth telling the player about.
+ var stale: ObjCBool = false
+ picked = dm_picked_data_path(&stale) ?? ""
+ if stale.boolValue && picked.isEmpty {
+ note = "The location you picked before is no longer readable (the app was signed differently). Please choose it again."
+ } else if note.isEmpty, let old = dm_old_archive_message() {
+ note = String(cString: old)
+ }
+ }
+}
+
/// Cloud saves: an iCloud Drive folder shared with the Mac app (chosen in its settings window's
/// Cloud saves too). The host brings the save in from there when the game starts and
/// sends it there after each save (dm/host/save_sync.h); the folder is a bookmark (CloudSaves.m).
@@ -61,14 +119,14 @@ private struct CloudSavesSection: View {
}
}
-/// UIDocumentPickerViewController for a folder, in the two callbacks a SwiftUI sheet wants.
+/// UIDocumentPickerViewController for files or folders (`types`), in the two callbacks a SwiftUI sheet wants.
private struct DocumentPicker: UIViewControllerRepresentable {
var types: [UTType] = [.folder]
let onPick: (URL) -> Void
let onCancel: () -> Void
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
- // .open (not .import): the folder stays where it is and the bookmark keeps pointing at it.
+ // .open (not .import): the file or folder stays where it is and the bookmark keeps pointing at it (an import would copy 16 GB of game data into the app's container).
let picker = UIDocumentPickerViewController(forOpeningContentTypes: types, asCopy: false)
picker.allowsMultipleSelection = false
picker.delegate = context.coordinator
@@ -129,6 +187,7 @@ private struct LauncherView: View {
} footer: {
Text("Brings back all settings this game was tested with.")
}
+ GameDataSection()
CloudSavesSection()
Section {
HStack {
diff --git a/dm/ios/Sources/front_ios.m b/dm/ios/Sources/front_ios.m
index cddc294..577e6c4 100644
--- a/dm/ios/Sources/front_ios.m
+++ b/dm/ios/Sources/front_ios.m
@@ -24,6 +24,7 @@
const char *front_ios_boot_message(void); // main.m: why a build that keeps the game files off the IPA found none usable (NULL = the generic hint)
const char *front_ios_device_check(void); // main.m: why this device cannot run the games (its GPU has no BC textures; NULL = supported); makes the first Metal device
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 none
static CAMetalLayer *g_layer;
static void front_ios_notify_setup(void);
@@ -91,6 +92,7 @@ @interface DmRootVC : UIViewController
#if __has_include(<UIKit/UIHingeInteraction.h>)
- (void)hingeChanged:(UIHinge *)hinge API_AVAILABLE(ios(27.1));
#endif
+@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;
- (void)showFPS:(BOOL)on;
@end
@@ -146,6 +148,14 @@ - (void)hingeChanged:(UIHinge *)hinge API_AVAILABLE(ios(27.1)) {
// 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;
+ }
dm_cloud_saves_env(); // DM_SAVE_SYNC from the launcher's Cloud saves pick, before the game's first save mount reads it (CloudSaves.m, save_sync.h)
front_hud_env(); // before the process's first Metal device, which reads MTL_HUD_ENABLED (an env file's DM_METAL_HUD sets it in main.m already; GitHub issue #840)
const char *dev = front_ios_device_check(); // that first device: a GPU without BC textures is refused here, not at launch, so the launcher's HUD switch counts
@@ -213,7 +223,8 @@ - (void)viewDidAppear:(BOOL)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
front_ios_notify_ask();
}
- (BOOL)prefersHomeIndicatorAutoHidden { return YES; }
diff --git a/dm/ios/Sources/main.m b/dm/ios/Sources/main.m
index 7cd198e..bd3bafa 100644
--- a/dm/ios/Sources/main.m
+++ b/dm/ios/Sources/main.m
@@ -8,9 +8,10 @@
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
-#include "game_profile.h" // game profile constants (the RomFS marker path)
+#include "hle_profile.h" // the runtime game profile (DM_PROFILE): the RomFS marker path comes from it
#include "front.h"
#include "romfs_image.h"
+#include "DataBookmark.h"
#include "romfs_identity.h"
#include "player_env.h"
#ifndef DM_PLAYER_BUILD
@@ -41,6 +42,38 @@ static int romfs_identity_accepts(NSString *img) {
if (rc != ROMFS_ID_OK && rc != ROMFS_ID_NONE) fprintf(stderr, "[RED] boot: Documents/%s is not the image this build needs (verdict %d); not used\n", img.lastPathComponent.UTF8String, rc);
return rc == ROMFS_ID_OK || rc == ROMFS_ID_NONE;
}
+// Set when a folder holds an old shard archive (romfs.idx): the launcher's Game data section shows it.
+static char g_old_archive[200];
+const char *dm_old_archive_message(void) { return g_old_archive[0] ? g_old_archive : NULL; }
+// A folder holds this game's files if it has 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 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.
+ if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@(dm_profile()->romfs_marker)]]) return YES;
+ // ROMFSARC shard archives (<dir>/romfs.idx next to romfs.NN) are no longer read: the folder is
+ // reported as holding no game data, and the log and the launcher's Game data section name the action.
+ if ([fm fileExistsAtPath:[d stringByAppendingPathComponent:@"romfs.idx"]]) {
+ snprintf(g_old_archive, sizeof g_old_archive, "The game data here is an old shard archive (romfs.idx), which this version no longer reads. Re-send the game data as romfs.img from the launcher.");
+ fprintf(stderr, "boot: %s holds an old RomFS shard archive (romfs.idx), which is no longer read. Re-send the game data as romfs.img from the launcher\n", d.fileSystemRepresentation);
+ }
+ return NO;
+}
+
+// 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 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)];
+}
+
// The images in Documents: romfs.img in any letter case, romfs.img itself first. The NSP script writes the image as RomFS.img (extract/<version>/RomFS.img) and the Mac's
// file system ignores letter case, but iOS's does not: a copy made through Finder or the Files app keeps the source's name, which an exact lookup of romfs.img never finds
static NSArray<NSString *> *romfs_document_images(NSString *doc) {
@@ -53,14 +86,40 @@ static int romfs_identity_accepts(NSString *img) {
}];
return imgs;
}
+static NSString *g_prog; // a game of several programs: the running program's folder in the app (p<N>, program_env); nil otherwise
static NSString *romfs_locate(NSString *prog) { // prog: a game of several programs, the running program's folder in the app (p<N>); nil otherwise
NSFileManager *fm = NSFileManager.defaultManager;
NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
if (prog) { // the program's files are in the app (Documents holds one image, one program's)
NSString *p = [NSBundle.mainBundle.resourcePath stringByAppendingPathComponent:prog], *img = [p stringByAppendingPathComponent:@"romfs.img"], *d = [p stringByAppendingPathComponent:@"romfs"];
if (romfs_image_complete(img)) return img;
- return [fm fileExistsAtPath:[d stringByAppendingPathComponent:@(DM_GAME_ROMFS_MARKER)]] ? d : nil;
+ return [fm fileExistsAtPath:[d stringByAppendingPathComponent:@(dm_profile()->romfs_marker)]] ? d : nil;
}
+ // What the player picked in the Files app wins over everything: it is the one path they chose
+ // on purpose, possibly on an external drive, and it costs nothing when they never picked one
+ // (the bookmark is absent and this falls straight through). An image or a folder both work,
+ // and a pick that no longer holds this game's data falls through rather than failing the boot.
+ {
+ BOOL stale = NO;
+ NSString *picked = dm_picked_data_path(&stale);
+ if (stale) fprintf(stderr, "boot: the picked game-data location needs to be chosen again (the app is signed differently now)\n");
+ if (picked.length) {
+ if (romfs_image_complete(picked)) { fprintf(stderr, "boot: game data from the picked image %s\n", picked.UTF8String); return picked; }
+ NSString *pimg = [picked stringByAppendingPathComponent:@"romfs.img"];
+ if (romfs_image_complete(pimg)) { fprintf(stderr, "boot: game data from the picked folder %s\n", pimg.UTF8String); return pimg; }
+ if (romfs_dir_ok(picked)) { fprintf(stderr, "boot: game data from the picked folder %s\n", picked.UTF8String); return picked; }
+ fprintf(stderr, "boot: the picked location %s holds no game data for this title; using the app's own\n", picked.UTF8String);
+ }
+ }
+ // 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;
for (NSString *img in romfs_document_images(doc)) {
if (!romfs_image_complete(img) || !romfs_identity_accepts(img)) continue;
// 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
@@ -76,13 +135,48 @@ static int romfs_identity_accepts(NSString *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 ([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 ([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)
+ if (romfs_dir_ok(d)) return d;
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(g_prog), YES); }
+
// Add-on content (DLC): each DLC's RomFS image at <dir>/<index>.img (or a legacy <index>/ folder). Like the game files, Documents/aoc comes before <App>/aoc.
// A DM_AOC_DIR that is already set (env.txt, the launch environment) wins. The host mounts and counts the DLC from DM_AOC_DIR (hle_fs.c, hle_misc.c)
static void aoc_locate(void) {
@@ -124,7 +218,7 @@ static void romfs_identity_report(NSString *doc) {
fprintf(stderr, "[RED] boot: %s\n", g_boot_message);
}
-static NSString *program_env(void);
+static NSString *program_env(NSString **msl_out);
static int boot_env(void) {
NSFileManager *fm = NSFileManager.defaultManager;
NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
@@ -174,12 +268,31 @@ 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 *dm = program_env();
- if (!dm) { romfs_identity_report(doc); fprintf(stderr, "boot: game files not found (Documents/romfs.img, Documents/romfs or <App>/romfs)\n"); return 0; }
- setenv("DM_SAVE", [doc stringByAppendingPathComponent:@"save"].fileSystemRepresentation, 0);
- setenv("DM_TEMP", [doc stringByAppendingPathComponent:@"temp"].fileSystemRepresentation, 0);
+ // 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.
+ // A game of several programs (program_env) takes the running program's files from the app.
+ NSString *msl = nil;
+ NSString *dm = program_env(&msl);
+ NSString *gdir = game_dir_for(dm_profile()->id);
+ NSString *romfs = romfs_locate(g_prog);
+ BOOL per_game = [romfs hasPrefix:gdir]; // messaging nil is NO here: no data means the shared layout
+ if (!set_data_env(romfs, NO)) { if (!romfs) romfs_identity_report(doc); // a slim build says why its romfs.img was not used
+ fprintf(stderr, "boot: no game data yet (Documents/romfs.img, Documents/romfs or <App>/romfs); the launcher asks for it\n"); }
setenv("DM_GUEST_FILE_DIR", NSTemporaryDirectory().fileSystemRepresentation, 0); // DM_GUEST_FILE (hle_svc.c) keeps the guest's memory in files there
+ 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
+ // the app's shared <App>/msl (the program's <App>/msl/p<N> in a game of several programs).
+ NSString *per_game_msl = [gdir stringByAppendingPathComponent:@"msl"];
+ if (per_game && [fm fileExistsAtPath:per_game_msl]) msl = per_game_msl;
+ setenv("DM_MSL_DIR", msl.fileSystemRepresentation, 0);
setenv("DM_FONT_DIR", [[res stringByAppendingPathComponent:@"dm"] stringByAppendingPathComponent:@"font"].fileSystemRepresentation, 0); // Shared-font stand-ins (standard.ttf / nintendo_ext.ttf): iOS lacks the macOS font path
+ // Add-on content beside the game data: the launcher's games/<id>/aoc/ holds <index>.img (the layout hle_fs.c aoc_dir reads), because a slim IPA does not carry it. Taken before
+ // Documents/aoc and <App>/aoc (aoc_locate, seedds/nxnative#486) and never over an explicit DM_AOC_DIR: aoc_locate only fills DM_AOC_DIR when this did not.
+ NSString *per_game_aoc = [gdir stringByAppendingPathComponent:@"aoc"];
+ BOOL aoc_isdir = NO;
+ if (per_game && [fm fileExistsAtPath:per_game_aoc isDirectory:&aoc_isdir] && aoc_isdir) setenv("DM_AOC_DIR", per_game_aoc.fileSystemRepresentation, 0);
+ // Cheats (dm/host/hle_cheats.c, docs/frontends.md "Cheats"): a front end copies the game's cheat files to Documents/games/<id>/cheats, next to its saves. The host never creates the folder and an explicit DM_CHEATS (an env file) wins.
+ setenv("DM_CHEATS", [gdir stringByAppendingPathComponent:@"cheats"].fileSystemRepresentation, 0);
aoc_locate(); // add-on content: Documents/aoc, else <App>/aoc; an empty Documents/aoc explicitly selects no DLC
NSString *pso = [res stringByAppendingPathComponent:@"pso"]; // the pipeline archive made offline (build_ipa.sh, dm/gfx/tools/pso_offline.py): the shader pairs' bases, which the device then does not build
if ([fm fileExistsAtPath:pso]) setenv("DM_PSO_BUNDLE", pso.fileSystemRepresentation, 0);
@@ -194,7 +307,7 @@ static int boot_env(void) {
// The host's profile is the program's (DM_PROGRAM, main.c dm_game_cur). oe::ExecuteProgram records the program and its parameters in Documents/program.bin (DM_EXEC_OUT, hle_oe.c), names it on screen, schedules a
// notification that opens the app (front_ios.m front_program_handoff) and ends; this start reads the record and takes it away, so a later start, after the player quit the game, is at the launcher as on the Switch.
// Started from the record, the settings screen stays away (DM_LAUNCHER=0): the player is on the way into a game. Returns the program's dm folder (the host's modules folder), nil when its game files are missing
-static NSString *program_env(void) {
+static NSString *program_env(NSString **msl_out) {
NSFileManager *fm = NSFileManager.defaultManager;
NSString *doc = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0], *res = NSBundle.mainBundle.resourcePath;
NSString *dmdir = [res stringByAppendingPathComponent:@"dm"], *msl = [res stringByAppendingPathComponent:@"msl"], *prog = nil;
@@ -215,12 +328,7 @@ static int boot_env(void) {
setenv("DM_APP_DM", dmdir.fileSystemRepresentation, 1); // the launcher's settings (Launcher.swift)
fprintf(stderr, "boot: program %d of %s (%zu bytes of launch parameter)\n", n, [[plist stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet] stringByReplacingOccurrencesOfString:@"\n" withString:@" "].UTF8String, d.length > 4 ? (size_t)d.length - 4 : 0);
}
- NSString *romfs = romfs_locate(prog);
- if (!romfs) return nil;
- fprintf(stderr, "boot: game files %s\n", romfs.fileSystemRepresentation);
- setenv("DM_ROMFS", romfs.fileSystemRepresentation, 0);
- setenv("DM_NVN_SHMAP", [dmdir stringByAppendingPathComponent:@"shader_map.tsv"].fileSystemRepresentation, 0);
- setenv("DM_MSL_DIR", msl.fileSystemRepresentation, 0);
+ g_prog = prog; *msl_out = msl; // boot_env locates the game data (romfs_locate(g_prog)), so a game with none yet still reaches the launcher (per-game data folders)
return dmdir;
}
static int g_boot_ok;
diff --git a/dm/ios/project.yml b/dm/ios/project.yml
index 323b5f6..e542cf1 100644
--- a/dm/ios/project.yml
+++ b/dm/ios/project.yml
@@ -83,7 +83,7 @@ targets:
OTHER_CFLAGS: $(inherited) -mcpu=apple-a18 # The host assembly contains LSE atomic instructions, which the default baseline does not accept
CLANG_ENABLE_OBJC_ARC: YES
SWIFT_VERSION: "5.0" # Launcher.swift (SwiftUI)
- SWIFT_OBJC_BRIDGING_HEADER: Sources/DmIOS-Bridging-Header.h # CloudSaves.h (the cloud-save folder pick)
+ SWIFT_OBJC_BRIDGING_HEADER: Sources/DmIOS-Bridging-Header.h # CloudSaves.h (the cloud-save folder pick), DataBookmark.h (the Files app pick)
DEBUG_INFORMATION_FORMAT: dwarf
GCC_WARN_INHIBIT_ALL_WARNINGS: YES
schemes:
diff --git a/dm/ios/tests/test_aoc_layouts.py b/dm/ios/tests/test_aoc_layouts.py
new file mode 100644
index 0000000..3b042cd
--- /dev/null
+++ b/dm/ios/tests/test_aoc_layouts.py
@@ -0,0 +1,92 @@
+"""Run main.m's DLC choice from boot_env (per-game games/<id>/aoc, then aoc_locate) on macOS/Foundation with private Documents and App folders, once per data layout (no device or game files)."""
+from pathlib import Path
+import os
+import subprocess
+import sys
+import tempfile
+import unittest
+
+REPO = Path(__file__).resolve().parents[3]
+
+
[email protected](sys.platform == 'darwin', 'Foundation required')
+class AocLayouts(unittest.TestCase):
+ @classmethod
+ def setUpClass(cls):
+ cls.tools = tempfile.TemporaryDirectory(); cls.addClassCleanup(cls.tools.cleanup)
+ root = Path(cls.tools.name)
+ main = (REPO / 'dm/ios/Sources/main.m').read_text()
+ cls.main = main
+ locator = main[main.index('static void aoc_locate'):main.index('// Boot prerequisites')]
+ choice = main[main.index('NSString *per_game_aoc'):main.index('aoc_locate();') + len('aoc_locate();')]
+ source = root / 'layouts.m'
+ source.write_text('#import <Foundation/Foundation.h>\n#include <stdio.h>\n#include <stdlib.h>\n'
+ 'static NSString *fixture_doc, *fixture_bundle;\n'
+ '@interface FixtureBundle : NSObject\n+ (instancetype)mainBundle;\n- (NSString *)resourcePath;\n@end\n'
+ '@implementation FixtureBundle\n+ (instancetype)mainBundle { return [self new]; }\n'
+ '- (NSString *)resourcePath { return fixture_bundle; }\n@end\n#define NSBundle FixtureBundle\n'
+ '#define NSSearchPathForDirectoriesInDomains(a,b,c) @[fixture_doc]\n' + locator +
+ # argv: Documents, App, the romfs path romfs_locate would have answered
+ '\nint main(int argc, char **argv) { @autoreleasepool { if (argc < 4) return 2;\n'
+ 'fixture_doc = @(argv[1]); fixture_bundle = @(argv[2]); NSString *romfs = @(argv[3]);\n'
+ 'NSFileManager *fm = NSFileManager.defaultManager;\n'
+ 'NSString *gdir = [[fixture_doc stringByAppendingPathComponent:@"games"] stringByAppendingPathComponent:@"0100TEST"];\n'
+ 'BOOL per_game = [romfs hasPrefix:gdir];\n' + choice +
+ '\nconst char *e = getenv("DM_AOC_DIR"); puts(e ? e : "(none)"); return 0; }}\n')
+ cls.probe = root / 'layouts'
+ subprocess.run(['clang', '-fobjc-arc', '-Wall', '-Werror', str(source), '-framework', 'Foundation', '-o', str(cls.probe)], check=True)
+
+ def choose(self, doc, app, romfs, env=None):
+ e = {k: v for k, v in os.environ.items() if k != 'DM_AOC_DIR'}; e.update(env or {})
+ r = subprocess.run([str(self.probe), str(doc), str(app), str(romfs)], capture_output=True, text=True, env=e)
+ self.assertEqual(r.returncode, 0, r.stderr); return r.stdout.strip()
+
+ def folders(self, t):
+ doc = Path(t) / 'Documents'; app = Path(t) / 'App'; game = doc / 'games' / '0100TEST'
+ game.mkdir(parents=True); app.mkdir(); return doc, app, game
+
+ def test_the_layout_test_reads_the_real_per_game_decision(self):
+ self.assertIn('BOOL per_game = [romfs hasPrefix:gdir];', self.main)
+
+ def test_root_layout_finds_documents_aoc(self):
+ """Documents/romfs.img with Documents/aoc/2.img beside it: issue #316 logged 'reporting 0 add-on contents (no DM_AOC_DIR)'."""
+ with tempfile.TemporaryDirectory() as t:
+ doc, app, game = self.folders(t)
+ (doc / 'aoc').mkdir(); (doc / 'aoc' / '2.img').write_bytes(b'x'); (doc / 'romfs.img').write_bytes(b'x')
+ self.assertEqual(self.choose(doc, app, doc / 'romfs.img'), str(doc / 'aoc'))
+
+ def test_root_layout_ignores_a_per_game_aoc_it_is_not_using(self):
+ with tempfile.TemporaryDirectory() as t:
+ doc, app, game = self.folders(t)
+ (doc / 'aoc').mkdir(); (game / 'aoc').mkdir()
+ self.assertEqual(self.choose(doc, app, doc / 'romfs.img'), str(doc / 'aoc'))
+
+ def test_root_layout_falls_back_to_the_bundled_aoc_then_none(self):
+ with tempfile.TemporaryDirectory() as t:
+ doc, app, game = self.folders(t)
+ self.assertEqual(self.choose(doc, app, doc / 'romfs.img'), '(none)')
+ (app / 'aoc').mkdir()
+ self.assertEqual(self.choose(doc, app, doc / 'romfs.img'), str(app / 'aoc'))
+
+ def test_per_game_layout_takes_its_own_aoc_before_documents_aoc(self):
+ with tempfile.TemporaryDirectory() as t:
+ doc, app, game = self.folders(t)
+ (doc / 'aoc').mkdir(); (game / 'aoc').mkdir(); (app / 'aoc').mkdir()
+ self.assertEqual(self.choose(doc, app, game / 'romfs.img'), str(game / 'aoc'))
+
+ def test_per_game_layout_without_its_own_aoc_uses_documents_aoc(self):
+ with tempfile.TemporaryDirectory() as t:
+ doc, app, game = self.folders(t)
+ (doc / 'aoc').mkdir()
+ self.assertEqual(self.choose(doc, app, game / 'romfs.img'), str(doc / 'aoc'))
+
+ def test_explicit_dm_aoc_dir_wins_in_both_layouts(self):
+ """env.txt sets DM_AOC_DIR (setenv without overwrite) before this runs, so a variable already in the environment stands for it."""
+ with tempfile.TemporaryDirectory() as t:
+ doc, app, game = self.folders(t)
+ (doc / 'aoc').mkdir(); (game / 'aoc').mkdir(); (app / 'aoc').mkdir()
+ for romfs in (doc / 'romfs.img', game / 'romfs.img'):
+ self.assertEqual(self.choose(doc, app, romfs, {'DM_AOC_DIR': '/elsewhere'}), '/elsewhere')
+
+
+if __name__ == '__main__': unittest.main()
diff --git a/dm/ios/tests/test_romfs_startup.py b/dm/ios/tests/test_romfs_startup.py
index efc36f4..4792b5e 100644
--- a/dm/ios/tests/test_romfs_startup.py
+++ b/dm/ios/tests/test_romfs_startup.py
@@ -24,12 +24,19 @@ class Startup(unittest.TestCase):
source = root / 'locator.m'
source.write_text('#import <Foundation/Foundation.h>\n#include <stdio.h>\n#include "romfs_image.h"\n#include "romfs_identity.h"\n'
'static NSString *fixture_doc, *fixture_tmp, *fixture_bundle;\n#define DM_GAME_ROMFS_MARKER "marker"\n'
+ 'typedef struct { const char *id, *romfs_marker; } fixture_profile; /* the fork reads both from the runtime profile */\nstatic const fixture_profile *dm_profile(void) { static const fixture_profile p = {"0100000000000000", "marker"}; return &p; }\n'
'@interface FixtureBundle : NSObject\n+ (instancetype)mainBundle;\n- (NSString *)resourcePath;\n@end\n'
'@implementation FixtureBundle\n+ (instancetype)mainBundle { return [self new]; }\n'
'- (NSString *)resourcePath { return fixture_bundle; }\n@end\n#define NSBundle FixtureBundle\n'
- '#define NSSearchPathForDirectoriesInDomains(a,b,c) @[fixture_doc]\n#define NSTemporaryDirectory() fixture_tmp\n' + locator +
+ '#define NSSearchPathForDirectoriesInDomains(a,b,c) @[fixture_doc]\n#define NSTemporaryDirectory() fixture_tmp\n'
+ # The picked-location bookmark (DataBookmark.m): the fixture answers it from a
+ # variable, so a test can put the player's choice anywhere on disk.
+ 'static NSString *fixture_picked;\n'
+ 'NSString *dm_picked_data_path(BOOL *stale) { if (stale) *stale = NO; return fixture_picked; }\n' + locator +
'\nint main(int argc, char **argv) { @autoreleasepool { if (argc < 3) return 2;\n'
- 'fixture_doc = @(argv[1]); fixture_tmp = @(argv[2]); fixture_bundle = argc > 3 ? @(argv[3]) : @"/nonexistent-fixture"; NSString *p = romfs_locate(nil);\n'
+ 'fixture_doc = @(argv[1]); fixture_tmp = @(argv[2]); fixture_bundle = argc > 3 ? @(argv[3]) : @"/nonexistent-fixture";\n'
+ 'if (argc > 4) fixture_picked = @(argv[4]); // the location the player picked, when a test sets one\n'
+ 'NSString *p = romfs_locate(nil);\n'
'if (!p) return 1; puts(p.fileSystemRepresentation); return 0; }}\n')
cls.locator = root / 'locator'
subprocess.run(['clang', '-fobjc-arc', '-Wall', '-Werror', '-I', str(REPO / 'dm/host'),
@@ -120,5 +127,43 @@ class Startup(unittest.TestCase):
(doc / 'romfs.img').write_bytes(b'cut off') # an incomplete exact name: the complete other spelling
self.assertEqual(locate().stdout.strip(), str(doc / 'RomFS.img'))
+ def test_the_location_the_player_picked_wins_over_everything(self):
+ """The Files-app pick (DataBookmark): an image, a folder holding one, or a plain tree."""
+ with tempfile.TemporaryDirectory() as temporary:
+ root = Path(temporary); doc = root / 'Documents'; doc.mkdir()
+ tmp = root / 'temp'; tmp.mkdir(); bundle = root / 'App'; bundle.mkdir()
+ source = root / 'source'; source.mkdir(); (source / 'marker').write_text('resource')
+ image.pack(source, bundle / 'romfs.img')
+ # something the app would otherwise use, so "wins" means something
+ image.pack(source, doc / 'romfs.img')
+ external = root / 'external'; external.mkdir()
+
+ def locate(picked=None):
+ argv = [str(self.locator), str(doc), str(tmp), str(bundle)] + ([str(picked)] if picked else [])
+ result = subprocess.run(argv, capture_output=True, text=True)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ return result.stdout.strip()
+
+ # 1. the picked path IS the image
+ image.pack(source, external / 'mine.img')
+ self.assertEqual(locate(external / 'mine.img'), str(external / 'mine.img'))
+ # 2. the picked path is a folder holding romfs.img
+ image.pack(source, external / 'romfs.img')
+ self.assertEqual(locate(external), str(external / 'romfs.img'))
+ # 3. the picked path is a plain RomFS tree
+ (external / 'romfs.img').unlink(); (external / 'marker').write_text('resource')
+ self.assertEqual(locate(external), str(external))
+ # 4. a picked path holding none of this game's data falls through to the app's own copy
+ empty = root / 'empty'; empty.mkdir()
+ self.assertEqual(locate(empty), str(doc / 'romfs.img'))
+
+ def test_per_game_aoc_folder_is_taken_before_the_bundled_one(self):
+ """Slim builds carry no add-on content: games/<id>/aoc (the layout hle_fs.c aoc_dir reads) must reach DM_AOC_DIR, before aoc_locate takes Documents/aoc or <App>/aoc, and never over an explicit value."""
+ main = (REPO / 'dm/ios/Sources/main.m').read_text()
+ per_game = main.index('setenv("DM_AOC_DIR", per_game_aoc.fileSystemRepresentation, 0)')
+ bundled = main.index('\n aoc_locate();')
+ self.assertLess(per_game, bundled)
+ self.assertIn('per_game && [fm fileExistsAtPath:per_game_aoc', main) # only when the per-game folder is in use
+
if __name__ == '__main__': unittest.main()