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
iPhone testing round: picker bookmarks hold their security scope, the install asks before copying, retried stages keep their row, intermediate extractions are pruned
LNPANDY1887fe93c8da
diff --git a/dm/ios/Sources/DataBookmark.m b/dm/ios/Sources/DataBookmark.mindex 470e2949..d8cf0563 100644--- a/dm/ios/Sources/DataBookmark.m+++ b/dm/ios/Sources/DataBookmark.m@@ -4,10 +4,25 @@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();@@ -19,7 +34,6 @@ NSString *dm_picked_data_path(BOOL *stale) {relativeToURL:nilbookmarkDataIsStale:&isStaleerror:&error];- if (isStale && stale) *stale = YES;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.@@ -28,17 +42,30 @@ NSString *dm_picked_data_path(BOOL *stale) {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.+ if (NSData *again = fresh_bookmark(url)) [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;- NSData *bookmark = [url bookmarkDataWithOptions:0 includingResourceValuesForKeys:nil relativeToURL:nil error:&error];- if (!bookmark) {+ 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 ?: "no reason given");+ error.localizedDescription.UTF8String ?: (bookmark ? "the bookmark does not resolve again" : "no bookmark data"));return NO;}[NSUserDefaults.standardUserDefaults setObject:bookmark forKey:kPickedData];diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swiftindex 5ec6e955..4df5659b 100644--- a/frontend/Sources/NSPortBuilder/ContentView.swift+++ b/frontend/Sources/NSPortBuilder/ContentView.swift@@ -137,6 +137,20 @@ final class BuildModel: ObservableObject {}@Published var installedOn = "" // the device the last build went to (its UDID)@Published var installedAppPath = "" // the signed app that went there+ @Published var transferAsk = false // the transfer-the-game-files question is up: USB from the Mac is slow, so copying is the player's call+ private var transferHandler: ((Bool) -> Void)?++ /// Asks whether to copy the game files to the device now, and runs `then` with the answer on the main queue.+ func askTransfer(_ then: @escaping (Bool) -> Void) {+ transferHandler = then+ transferAsk = true+ }++ func transferAnswer(_ ok: Bool) {+ guard let then = transferHandler else { return }+ transferHandler = nil+ then(ok)+ }private var bundleID = "" // from the script's "bundle ID:" linevar xcrunPath = "/usr/bin/xcrun" // tests stand in for xcodebuild and devicectl@@ -1539,12 +1553,30 @@ final class BuildModel: ObservableObject {return self.finishInstall(String(format: self.t("dev.noProject"), self.shortPath(app + "/Info.plist")))}let tid = Self.titleID(self.basePath).map { String(format: "%016llX", $0) } ?? "game"- self.runTool(id: 1003, name: String(format: self.t("dev.copyStage"), name),- Self.archiveCopy(xcrun: xcrun, device: device, bundle: bundle, archive: archive, titleID: tid), step: Self.installStep) { error in- if let error { return self.finishInstall(String(format: self.t("dev.copyFailed"), error)) }- self.installedOn = device- self.installedAppPath = app- self.finishInstall(nil)+ // Copying the game files over USB is the slowest part of an install and the player may+ // rather hand the data to the device themselves (a fast external drive in Files), so it+ // is their call. Without game files there is nothing to ask about.+ let copy = {+ self.runTool(id: 1003, name: String(format: self.t("dev.copyStage"), name),+ Self.archiveCopy(xcrun: xcrun, device: device, bundle: bundle, archive: archive, titleID: tid), step: Self.installStep) { error in+ if let error { return self.finishInstall(String(format: self.t("dev.copyFailed"), error)) }+ self.installedOn = device+ self.installedAppPath = app+ self.finishInstall(nil)+ }+ }+ guard FileManager.default.fileExists(atPath: archive + "/romfs.img") else { return copy() }+ self.askTransfer { ok in+ guard ok else {+ self.installedOn = device+ self.installedAppPath = app+ self.running = false+ self.done = true+ self.doneNote = String(format: self.t("dev.copySkipped"), name)+ self.cleanupStaging()+ return+ }+ copy()}}}@@ -1640,7 +1672,14 @@ final class BuildModel: ObservableObject {/// its progress in the stage. `then` gets nil on success, else the last error line.private func runTool(id: Int, name: String, _ command: [String], step: @escaping (String) -> (detail: String, percent: Double?)?,then: @escaping (String?) -> Void) {- stages.append(Stage(id: id, name: name, state: .running, started: Date()))+ // A stage that runs again (the team-variant signing retry uses the same id) keeps its row:+ // a second row with that id would never see an update again — every lookup takes the first+ // match — and would spin for ever after the build moved on.+ if let i = stages.firstIndex(where: { $0.id == id }) {+ stages[i] = Stage(id: id, name: name, state: .running, started: Date())+ } else {+ stages.append(Stage(id: id, name: name, state: .running, started: Date()))+ }append("[\(name)] \(command.joined(separator: " "))\n")let p = Process()p.executableURL = URL(fileURLWithPath: command[0])@@ -2130,6 +2169,12 @@ struct BuilderView: View {if let id, id.hasPrefix("set.") { withAnimation(.easeInOut(duration: 0.2)) { proxy.scrollTo(id, anchor: .center) } }}.modifier(GameFilesDrop(enabled: !settings && !m.running, label: m.t("drop.hint")) { m.take(dropped: $0) }) // DropFiles.swift+ .alert(String(format: m.t("dev.askTransferTitle"), m.deviceName(m.installDevice)), isPresented: $m.transferAsk) {+ Button(m.t("dev.transferNow")) { m.transferAnswer(true) }+ Button(m.t("dev.transferSkip"), role: .cancel) { m.transferAnswer(false) }+ } message: {+ Text(m.t("dev.askTransferText"))+ }}.buttonStyle(LauncherSecondaryButtonStyle())}diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swiftindex 0764d70c..7bead8f6 100644--- a/frontend/Sources/NSPortBuilder/Localization.swift+++ b/frontend/Sources/NSPortBuilder/Localization.swift@@ -99,6 +99,11 @@ enum L10n {"dev.installStage": ("\u{5b89}\u{88c5}\u{5230} %@", "Install on %@"),"dev.copyStage": ("复制游戏文件到 %@", "Copy the game files to %@"),"dev.copyFailed": ("复制游戏文件失败:%@", "Copying the game files failed: %@"),+ "dev.askTransferTitle": ("传输游戏文件到 %@?", "Transfer the game files to %@?"),+ "dev.askTransferText": ("Mac 通过 USB 线向 iPhone 传输的速度有限(约 25–70 MB/s,视线缆和设备而定),大游戏需要较长时间。如果游戏文件在高速存储设备上(USB3 移动硬盘、读卡器),把它直接连到 iPhone,在游戏启动器的“选择游戏数据”里选取会更快。", "USB transfer from the Mac is limited (about 25–70 MB/s, depending on cable and device), so a large game takes a while. If the game files are on fast external storage (a USB 3 drive or card reader), connecting it to the iPhone and picking it under “Choose game data” in the launcher is faster."),+ "dev.transferNow": ("传输", "Transfer"),+ "dev.transferSkip": ("暂不传输", "Not now"),+ "dev.copySkipped": ("已安装到 %@;游戏文件未传输", "Installed on %@; the game files were not transferred"),"dev.installed": ("\u{5df2}\u{5b89}\u{88c5}\u{5230} %@", "Installed on %@"),"dev.installedNote": ("\u{6e38}\u{620f}\u{5df2}\u{5728}\u{8bbe}\u{5907}\u{4e0a}\u{ff0c}\u{53ef}\u{4ee5}\u{76f4}\u{63a5}\u{6253}\u{5f00}\u{3002}", "The game is on the device, ready to open."),"dev.open": ("\u{5728} %@ \u{4e0a}\u{6253}\u{5f00}", "Open on %@"),diff --git a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift b/frontend/Tests/NSPortBuilderTests/LibraryTests.swiftindex aa256301..0d7184a9 100644--- a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift+++ b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift@@ -291,9 +291,10 @@ final class LibraryTests: XCTestCase {}/// An iPhone build with a device and a team: the script prepares the Xcode project, romfs.img is packed as for- /// sideloading, xcodebuild signs the app without the game files for the device, devicectl installs it and copies the- /// game's folder into its Documents, and the library opens it there. The tools are stand-ins; nothing is signed.- @MainActor func testBuildSignsAndInstallsOnTheChosenDevice() throws {+ /// sideloading, xcodebuild signs the app without the game files for the device (the first try fails on a taken+ /// App ID, so the retry under the team's own bundle ID runs too), devicectl installs it, the game files go over+ /// once the player answered the transfer question, and the library opens it there. The tools are stand-ins; nothing is signed.+ @MainActor private func runInstallFlow(transfer: Bool) throws -> (BuildModel, [String], URL, URL) {guard FileManager.default.isExecutableFile(atPath: "/usr/bin/python3") else { throw XCTSkip("System Python is required for the process fixture") }let saved = ["repoPath", "buildTarget", "buildOutputDirectory", "installDevice", "signingTeam"].map { ($0, UserDefaults.standard.object(forKey: $0)) }defer { for (key, value) in saved { UserDefaults.standard.set(value, forKey: key) } }@@ -340,6 +341,10 @@ final class LibraryTests: XCTestCase {if a[0] == 'xcodebuild' and '-list' in a:print('Command line invocation:'); print(json.dumps({'project': {'schemes': ['DmIOS', 'Test (0100535012974000)']}}))elif a[0] == 'xcodebuild':+ if 'PRODUCT_BUNDLE_IDENTIFIER=' not in ' '.join(a):+ # App IDs are global: the plain one is registered to another team, and the app retries under a team variant+ print('error: "iOS Team Provisioning Profile" cannot be registered to your development team', flush=True)+ sys.exit(1)app = pathlib.Path(a[a.index('-derivedDataPath') + 1]) / 'Build/Products/Release-iphoneos/DmIOS.app'app.mkdir(parents=True)with (app / 'Info.plist').open('wb') as f: plistlib.dump({'CFBundleIdentifier': 'org.nxnative.test'}, f)@@ -356,16 +361,37 @@ final class LibraryTests: XCTestCase {model.slots[0].paths = [base.path]; model.slots[3].paths = [keys.path]let finished = expectation(description: "Signed and installed")let token = model.$done.filter { $0 }.prefix(1).sink { _ in finished.fulfill() }+ let asked = model.$transferAsk.filter { $0 }.prefix(1).sink { _ in model.transferAnswer(transfer) }model.startBuild()wait(for: [finished], timeout: 20)- withExtendedLifetime(token) {}+ withExtendedLifetime(token) {} ; withExtendedLifetime(asked) {}let log = try String(contentsOf: calls, encoding: .utf8).split(separator: "\n").map(String.init)+ return (model, log, calls, base)+ }++ /// The player may rather hand the game files to the device themselves (a fast external drive in Files):+ /// a skipped copy still counts as installed, and nothing is sent over USB.+ @MainActor func testSkippingTheTransferInstallsWithoutCopying() throws {+ let (model, log, _, _) = try runInstallFlow(transfer: false)+ XCTAssertFalse(log.contains { $0.hasPrefix("devicectl device copy") })+ XCTAssertEqual(model.installedOn, "00008150-AAAA")+ XCTAssertEqual(model.doneNote, String(format: model.t("dev.copySkipped"), "iPhone"))+ XCTAssertEqual(model.stages.filter { $0.id >= 1000 }.map(\.state), [.done, .done, .done]) // pack, sign, install — no copy row+ }++ @MainActor func testBuildSignsAndInstallsOnTheChosenDevice() throws {+ let (model, log, calls, base) = try runInstallFlow(transfer: true)let work = temporary.appendingPathComponent(titleID + "_build").pathXCTAssertTrue(log[0].contains("--prepare-xcode --keep-extracted --team-id TEAM123456"))XCTAssertFalse(log[0].contains(" -o ")) // no IPA: the project goes to the devicelet build = try XCTUnwrap(log.first { $0.hasPrefix("xcodebuild -project") })for part in ["-scheme Test (0100535012974000)", "-destination id=00008150-AAAA", "-derivedDataPath \(work)/xcode-derived-data","-allowProvisioningUpdates", "DEVELOPMENT_TEAM=TEAM123456", "DM_COPY_ROMFS=NO"] { XCTAssertTrue(build.contains(part), part) }+ // The first sign failed on the taken App ID; the retry ran under the team's bundle ID and kept the same stage row.+ let signs = log.filter { $0.hasPrefix("xcodebuild -project") }+ XCTAssertEqual(signs.count, 2)+ XCTAssertTrue(signs[1].contains("PRODUCT_BUNDLE_IDENTIFIER=org.nxnative.\(titleID).TEAM123456"), String(signs[1]))+ XCTAssertEqual(Set(model.stages.filter { $0.id >= 1000 }.map(\.id)).count, 4) // a retried stage keeps its row instead of piling up duplicateslet app = work + "/xcode-derived-data/Build/Products/Release-iphoneos/DmIOS.app"let archive = temporary.path + "/games/\(titleID)"// the archive is packed before signing, the app installed before the archive goes into its Documentsdiff --git a/scripts/build_ipa_from_nsp.py b/scripts/build_ipa_from_nsp.pyindex 89ff8595..dca1d29d 100755--- a/scripts/build_ipa_from_nsp.py+++ b/scripts/build_ipa_from_nsp.py@@ -873,6 +873,19 @@ def extracted(log): # (files, bytes) from the summary lines nx_extract wrote treturn (sum(int(n) for n, _ in rows), int(sum(float(mb) for _, mb in rows) * 2**20)) if rows else None+def prune_intermediate_extracts(dirs, versions, pkg_ver):+ """Every extracted version but the packed one: each holds a complete extra copy of the game (one+ merged RomFS image per version), and only the packed version's files are reused by the next+ build — when its inputs change, the intermediates are extracted again anyway (the packed version+ is merged from them). The shader captures above still read them, so they are dropped only once+ the build is done. A game without an update extracts one version and this deletes nothing."""+ spare = [dirs[v] for v in versions if v != pkg_ver and dirs[v].is_dir()]+ if not spare: return+ say('deleting the intermediate extractions, one full copy of the game each (the packed version keeps its files): '+ + ', '.join(d.name for d in spare))+ for d in spare: rmtree(d)++def extract(nx, keys, ex, name, base, upd, cmp_dir, logs, g): # the build IDs are checked by the caller (build_mismatch)dst, marker, log = ex / name, g.get('romfs_marker'), logs / f'extract_{name}.log'try: reusable = (dst / 'ExeFS' / 'main').exists() and (dst / 'RomFS.img').is_file() and not (dst / 'RomFS').exists() and (rfs.source(dst) / marker).exists()@@ -2148,6 +2161,7 @@ def main(args=None):mac_app(py, repo, out_ipa, app, work, game, patched, host, font, ios, aoc, gfx, lang, g, app_name, app_version, bundle_id, icon, args.mac_sign or MAC_SIGN, env, logs, args.env_file or DM_ENV_FILE,(args.gpu or PSO_GPUS).strip())rmtree(work / 'tmp')+ prune_intermediate_extracts(dirs, versions, pkg_ver)say(f'done: {out_ipa} ({tree_size(out_ipa)[1] / 2**30:.1f} GB); 'f'open it from Finder. Its save is in ~/Library/Application Support/nxnative/{app:016X}/, its log in ~/Library/Logs/nxnative/{app:016X}.log')report_profile_status(g, reg, pkg_ver, derived, provisional, 'Mac app')@@ -2156,6 +2170,7 @@ def main(args=None):host, font = mac_host(py, repo, work, env, logs, g)cmd = mac_run(work, game, patched, host, font, ios, aoc, gfx, lang, env, logs, g, args.env_file or DM_ENV_FILE)rmtree(work / 'tmp')+ prune_intermediate_extracts(dirs, versions, pkg_ver)say(f'done: the game runs on the Mac with this command (its save in {work / "run/save"}, kept from run to run; DM_WINDOW=1 opens a window, DM_AUDIO=1 plays sound):')PROG.say(cmd)report_profile_status(g, reg, pkg_ver, derived, provisional, 'Mac build')@@ -2185,6 +2200,7 @@ def main(args=None):except ValueError as e: raise BuildError(str(e)) from ePROG.end()rmtree(work / 'tmp')+ prune_intermediate_extracts(dirs, versions, pkg_ver)say(f'ready: {workspace}')say(f'game project: {project}')say(f'Xcode derived data: {work / "xcode-derived-data"}')@@ -2199,6 +2215,8 @@ def main(args=None):rmtree(work / 'tmp')if not keep:say(f'deleting the extracted game files: {ex}'); rmtree(ex)+ else:+ prune_intermediate_extracts(dirs, versions, pkg_ver)say(f'done: {out_ipa} ({out_ipa.stat().st_size / 2**30:.1f} GB)')report_profile_status(g, reg, pkg_ver, derived, provisional)