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
Saves: the Mac save is where the launcher now keeps it, beside the app
v0lumehi01b1e5ca83d6
diff --git a/frontend/README.md b/frontend/README.mdindex 86f0832f..3c80326f 100644--- a/frontend/README.md+++ b/frontend/README.md@@ -59,14 +59,16 @@ organizational unit, not the code in its name.## Saves**Manage > Saves…** (a controller's Y) shows a game's save on this Mac-(`~/Library/Application Support/nxnative/<title ID>/save`, from `dm/mac/launcher.c`) and+(where `dm/mac/launcher.c` puts it: `<library folder>/save/<title ID>` beside the app when+its folder holds `games/`, else `~/Library/Application Support/nxnative/<title ID>/save`;+until the app has copied an older `~/Library` save across, that one is shown) andon the device the launcher installed it on (`Documents/games/<title ID>/save` in the app'sdata container, from `dm/ios/Sources/main.m`), each with the date of its newest file, andmarks the newer one. **Mac → iPhone** and **iPhone → Mac** copy one over the other, afterasking. The save being replaced is backed up first, beside the Mac save in-`save-backups/<date> mac` or `<date> device` (the device's is copied to the Mac before-anything is replaced). A game that also keeps device save data has a `save_device`-folder beside `save` (`dm/host/hle_fs.c`), which goes along. The copy is refused while+`<save>-backups/<date> mac` or `<date> device` (the device's is copied to the Mac before+anything is replaced). A game that also keeps device save data has a `<save>_device`+folder beside it (`dm/host/hle_fs.c`), which goes along. The copy is refused whilethe game runs on the Mac; close it on the device too, or it writes its own save over thecopy later. The device side uses the install's route: `devicectl device info files` forthe date and `devicectl device copy from` / `copy to` with the app's data container. Onlydiff --git a/frontend/Sources/NSPortBuilder/SaveTransfer.swift b/frontend/Sources/NSPortBuilder/SaveTransfer.swiftindex 3dd0c9e3..97f44330 100644--- a/frontend/Sources/NSPortBuilder/SaveTransfer.swift+++ b/frontend/Sources/NSPortBuilder/SaveTransfer.swift@@ -24,8 +24,9 @@ struct DeviceControl {/// the route the install takes (devicectl and the app's data container). The save that is replaced is backed up on the Mac/// first, so a copy in the wrong direction loses nothing.///-/// Where the saves are: the Mac app keeps them in ~/Library/Application Support/nxnative/<title ID>/save (dm/mac/launcher.c,-/// DM_SAVE); the iPhone app in Documents/games/<title ID>/save (dm/ios/Sources/main.m), the per-game folder it uses when the+/// Where the saves are: the Mac app keeps them beside itself in <app's folder>/save/<title ID> when that folder also holds+/// games/ (the launcher's layout), else in ~/Library/Application Support/nxnative/<title ID>/save (dm/mac/launcher.c, DM_SAVE;+/// the device save is DM_SAVE + "_device", dm/host/hle_fs.c); the iPhone app in Documents/games/<title ID>/save (dm/ios/Sources/main.m), the per-game folder it uses when the/// game files come from Documents/games/<title ID>/, which is where the launcher's install puts them. A sideloaded IPA with/// the game files inside keeps Documents/save instead, but the launcher knows the bundle ID (the data container) only of the/// apps it installed, so those are the ones it copies to.@@ -36,7 +37,8 @@ struct SaveTransfer {static let folders = ["save", "save_device"]let titleID: String- let mac: URL // the game's Application Support folder on this Mac+ let macSave: URL // the Mac app's DM_SAVE+ var legacySave: URL? // the ~/Library save the Mac app copies into macSave on its next start, while macSave is emptylet device: String // the device's UDIDlet bundle: String // the installed app's bundle ID: its data containerlet devicectl: DeviceControl@@ -54,21 +56,48 @@ struct SaveTransfer {var newest: Date?}- static func macFolder(titleID: String,- support: URL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]) -> URL {- support.appendingPathComponent("nxnative").appendingPathComponent(titleID)+ static func legacyMacSave(titleID: String,+ support: URL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]) -> URL {+ support.appendingPathComponent("nxnative").appendingPathComponent(titleID).appendingPathComponent("save")+ }++ /// The Mac app's DM_SAVE, by launcher.c's rule: beside the app when the app's folder also holds games/, else ~/Library.+ static func macSave(titleID: String, appPath: String,+ support: URL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]) -> (save: URL, legacy: URL?) {+ let legacy = legacyMacSave(titleID: titleID, support: support)+ guard !appPath.isEmpty else { return (legacy, nil) }+ let folder = URL(fileURLWithPath: appPath).deletingLastPathComponent()+ var isDir: ObjCBool = false+ guard FileManager.default.fileExists(atPath: folder.appendingPathComponent("games").path, isDirectory: &isDir), isDir.boolValue else { return (legacy, nil) }+ return (folder.appendingPathComponent("save").appendingPathComponent(titleID), legacy)+ }++ /// A save folder on the Mac: `save` is DM_SAVE itself, `save_device` sits beside it as DM_SAVE + "_device".+ static func macPath(_ folder: String, save: URL) -> URL {+ folder == "save" ? save : URL(fileURLWithPath: save.path + "_device")+ }++ /// Where a save folder is read from: macSave, or while that is still empty the ~/Library save the app brings across.+ func macSource(_ folder: String) -> URL? {+ let current = Self.macPath(folder, save: macSave)+ if Self.newest(in: current) != nil { return current }+ if let legacySave, Self.newest(in: Self.macPath("save", save: macSave)) == nil, Self.newest(in: Self.macPath("save_device", save: macSave)) == nil {+ let old = Self.macPath(folder, save: legacySave)+ if Self.newest(in: old) != nil { return old }+ }+ return nil}var deviceFolder: String { "Documents/games/\(titleID)" }/// Backups go beside the save, one folder per copy: "<date> mac" holds the Mac save a copy from the device replaced,/// "<date> device" the device save a copy from the Mac replaced.- var backups: URL { mac.appendingPathComponent("save-backups") }+ var backups: URL { macSave.deletingLastPathComponent().appendingPathComponent(macSave.lastPathComponent + "-backups") }// ---- reading ----func readMac() -> Contents {Self.folders.reduce(into: Contents()) { c, f in- guard let date = Self.newest(in: mac.appendingPathComponent(f)) else { return }+ guard let source = macSource(f), let date = Self.newest(in: source) else { return }c.folders.append(f)c.newest = max(c.newest ?? date, date)}@@ -104,19 +133,20 @@ struct SaveTransfer {let there = try readDevice()guard !there.folders.isEmpty else { throw Failure.noDeviceSave }let fm = FileManager.default- let incoming = mac.appendingPathComponent(".save-incoming-\(UUID().uuidString)")+ let incoming = macSave.deletingLastPathComponent().appendingPathComponent(".save-incoming-\(UUID().uuidString)")try fm.createDirectory(at: incoming, withIntermediateDirectories: true)defer { try? fm.removeItem(at: incoming) }for f in there.folders {try copyFrom(deviceFolder + "/" + f, to: incoming.appendingPathComponent(f))}var backup: URL?- for f in there.folders where fm.fileExists(atPath: mac.appendingPathComponent(f).path) {+ // A ~/Library save the app has not brought across yet stays where it is: macSave gets the copy, so the app keeps it+ for f in there.folders where fm.fileExists(atPath: Self.macPath(f, save: macSave).path) {let into = try backup ?? makeBackup("mac")backup = into- try fm.moveItem(at: mac.appendingPathComponent(f), to: into.appendingPathComponent(f))+ try fm.moveItem(at: Self.macPath(f, save: macSave), to: into.appendingPathComponent(f))}- for f in there.folders { try fm.moveItem(at: incoming.appendingPathComponent(f), to: mac.appendingPathComponent(f)) }+ for f in there.folders { try fm.moveItem(at: incoming.appendingPathComponent(f), to: Self.macPath(f, save: macSave)) }return backup}@@ -133,7 +163,7 @@ struct SaveTransfer {for f in there.folders { try copyFrom(deviceFolder + "/" + f, to: into.appendingPathComponent(f)) }}for f in here.folders {- let r = devicectl.run(Self.copyToCommand(device: device, bundle: bundle, source: mac.appendingPathComponent(f).path, destination: deviceFolder + "/" + f))+ let r = devicectl.run(Self.copyToCommand(device: device, bundle: bundle, source: (macSource(f) ?? Self.macPath(f, save: macSave)).path, destination: deviceFolder + "/" + f))guard r.ok else { throw Failure.device(Self.lastLine(r.output)) }}return backup@@ -290,9 +320,10 @@ struct SavesView: View {private var transfer: SaveTransfer? {guard let game else { return nil }- let mac = support.map { SaveTransfer.macFolder(titleID: game.titleID, support: $0) } ?? SaveTransfer.macFolder(titleID: game.titleID)+ let mac = support.map { SaveTransfer.macSave(titleID: game.titleID, appPath: game.appPath, support: $0) }+ ?? SaveTransfer.macSave(titleID: game.titleID, appPath: game.appPath)let bundle = NSDictionary(contentsOfFile: game.iosAppPath + "/Info.plist")?["CFBundleIdentifier"] as? String ?? ""- return SaveTransfer(titleID: game.titleID, mac: mac, device: game.installedDevice, bundle: bundle,+ return SaveTransfer(titleID: game.titleID, macSave: mac.save, legacySave: mac.legacy, device: game.installedDevice, bundle: bundle,devicectl: devicectl ?? .xcrun(m.xcrunPath))}diff --git a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift b/frontend/Tests/NSPortBuilderTests/LibraryTests.swiftindex 92c50c8a..b98bf3a8 100644--- a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift+++ b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift@@ -798,7 +798,7 @@ final class LibraryTests: XCTestCase {.write(to: installed.appendingPathComponent("Info.plist"))library.recordCompletedIOS(ipa: "", archive: "", app: installed.path, device: "00008150-AAAA", for: game.id)let support = temporary.appendingPathComponent("Application Support")- let save = SaveTransfer.macFolder(titleID: game.titleID, support: support).appendingPathComponent("save/data.bin")+ let save = SaveTransfer.legacyMacSave(titleID: game.titleID, support: support).appendingPathComponent("data.bin")try FileManager.default.createDirectory(at: save.deletingLastPathComponent(), withIntermediateDirectories: true)try Data("mac".utf8).write(to: save)try FileManager.default.setAttributes([.modificationDate: Date(timeIntervalSinceNow: -3 * 86_400)], ofItemAtPath: save.path)diff --git a/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift b/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swiftindex 3a68543c..2f97cb61 100644--- a/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift+++ b/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift@@ -77,18 +77,41 @@ final class SaveTransferTests: XCTestCase {private func read(_ url: URL) -> String? { (try? Data(contentsOf: url)).map { String(decoding: $0, as: UTF8.self) } }private func setUp(device: FakeDevice) -> SaveTransfer {- let mac = SaveTransfer.macFolder(titleID: titleID, support: temporary.appendingPathComponent("Application Support"))- var t = SaveTransfer(titleID: titleID, mac: mac, device: "00008150-AAAA", bundle: "com.example.game", devicectl: device.control)+ let mac = SaveTransfer.legacyMacSave(titleID: titleID, support: temporary.appendingPathComponent("Application Support"))+ var t = SaveTransfer(titleID: titleID, macSave: mac, device: "00008150-AAAA", bundle: "com.example.game", devicectl: device.control)t.scratch = temporaryt.now = { Date(timeIntervalSince1970: 1_800_000_000) }return t}+ /// The launcher's layout: <folder>/<id>.app beside <folder>/games keeps its save in <folder>/save/<id> (launcher.c), and+ /// reads the ~/Library save only until it has brought it across. A bundle anywhere else keeps ~/Library.+ func testTheMacSaveFollowsTheAppWhenItsFolderHoldsGames() throws {+ let support = temporary.appendingPathComponent("Application Support")+ let library = temporary.appendingPathComponent("Games")+ let app = library.appendingPathComponent("\(titleID).app")+ try fm.createDirectory(at: app, withIntermediateDirectories: true)+ XCTAssertNil(SaveTransfer.macSave(titleID: titleID, appPath: app.path, support: support).legacy, "no games/ beside it: ~/Library")+ try fm.createDirectory(at: library.appendingPathComponent("games"), withIntermediateDirectories: true)+ let paths = SaveTransfer.macSave(titleID: titleID, appPath: app.path, support: support)+ XCTAssertEqual(paths.save.path, library.path + "/save/" + titleID)+ XCTAssertEqual(SaveTransfer.macPath("save_device", save: paths.save).path, library.path + "/save/" + titleID + "_device")++ var t = SaveTransfer(titleID: titleID, macSave: paths.save, legacySave: paths.legacy, device: "00008150-AAAA", bundle: "com.example.game",+ devicectl: FakeDevice(temporary.appendingPathComponent("container")).control)+ t.scratch = temporary+ try write("old", SaveTransfer.legacyMacSave(titleID: titleID, support: support).appendingPathComponent("data.bin"), date: older)+ XCTAssertEqual(t.readMac(), SaveTransfer.Contents(folders: ["save"], newest: older), "the ~/Library save, until the app copies it")+ try write("new", paths.save.appendingPathComponent("data.bin"), date: newer)+ XCTAssertEqual(t.readMac(), SaveTransfer.Contents(folders: ["save"], newest: newer), "then the one beside the app")+ XCTAssertEqual(t.backups.path, library.path + "/save/" + titleID + "-backups")+ }+private let older = Date(timeIntervalSince1970: 1_700_000_000), newer = Date(timeIntervalSince1970: 1_760_000_000)func testTheMacSaveIsTheLaunchersFolderAndItsNewestFile() throws {let support = temporary.appendingPathComponent("Application Support")- XCTAssertEqual(SaveTransfer.macFolder(titleID: titleID, support: support).path, support.path + "/nxnative/" + titleID)+ XCTAssertEqual(SaveTransfer.macSave(titleID: titleID, appPath: "", support: support).save.path, support.path + "/nxnative/" + titleID + "/save")let t = setUp(device: FakeDevice(temporary.appendingPathComponent("container")))XCTAssertEqual(t.readMac(), SaveTransfer.Contents(), "no save yet")try fm.createDirectory(at: t.mac.appendingPathComponent("save"), withIntermediateDirectories: true)@@ -258,3 +281,8 @@ final class SaveTransferTests: XCTestCase {return f.string(from: Date(timeIntervalSince1970: seconds))}}++private extension SaveTransfer {+ /// The ~/Library layout's game folder, which holds save, save_device and save-backups side by side+ var mac: URL { macSave.deletingLastPathComponent() }+}