Skip to content

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

Never pass devicectl's --remove-existing-content

v0lumehiff792f047103
diff --git a/frontend/Sources/NSPortBuilder/CheatFiles.swift b/frontend/Sources/NSPortBuilder/CheatFiles.swift
index 275d7ebe..7267467e 100644
--- a/frontend/Sources/NSPortBuilder/CheatFiles.swift
+++ b/frontend/Sources/NSPortBuilder/CheatFiles.swift
@@ -371,8 +371,8 @@ struct CheatFiles {
}
/// The folder copied to the iPhone or iPad the launcher installed the game on, the way the Saves sheet copies saves
-/// (SaveTransfer: `devicectl device copy to` with the app's data container). The device's folder is replaced by the Mac's, so the
-/// device holds the same files and switches and nothing older beside them.
+/// (SaveTransfer: `devicectl device copy to` with the app's data container). The Mac's files are merged into the device's folder
+/// and nothing is deleted there, so a file the Mac no longer has can stay (#250: devicectl's wipe flag deletes the siblings).
struct CheatTransfer {
let files: CheatFiles
let device: String // the device's UDID
@@ -405,7 +405,7 @@ struct CheatTransfer {
guard r.ok else { throw Failure.device(SaveTransfer.lastLine(r.output)) }
}
- /// The save transfer's copy-to-device command: the destination is the folder itself, and what the Mac does not have goes.
+ /// The save transfer's copy-to-device command: the destination is the folder itself, merged into without deleting anything (a file the Mac no longer has can stay).
static func copyCommand(device: String, bundle: String, source: String, destination: String) -> [String] {
SaveTransfer.copyToCommand(device: device, bundle: bundle, source: source, destination: destination)
}
diff --git a/frontend/Sources/NSPortBuilder/SaveTransfer.swift b/frontend/Sources/NSPortBuilder/SaveTransfer.swift
index e682d235..842d4adf 100644
--- a/frontend/Sources/NSPortBuilder/SaveTransfer.swift
+++ b/frontend/Sources/NSPortBuilder/SaveTransfer.swift
@@ -276,11 +276,13 @@ struct SaveTransfer {
"--domain-type", "appDataContainer", "--domain-identifier", bundle]
}
- /// The destination is the folder itself, as in the install's archiveCopy; the files the Mac does not have go, so the
- /// device holds the Mac's save and nothing older beside it (the backup has the old one).
+ /// The destination is the folder itself, as in the install's archiveCopy. Never `--remove-existing-content true`: on a
+ /// real device it also deletes the destination's siblings (Documents/romfs.img, env.txt), not only its contents (#250).
+ /// Without it devicectl merges and overwrites, so a file the Mac no longer has can stay in the device's folder, which
+ /// costs far less than wiping game data (devicectl has no command to delete a single file). The backup has the old save.
static func copyToCommand(device: String, bundle: String, source: String, destination: String) -> [String] {
["device", "copy", "to", "--device", device, "--source", source, "--destination", destination,
- "--domain-type", "appDataContainer", "--domain-identifier", bundle, "--remove-existing-content", "true"]
+ "--domain-type", "appDataContainer", "--domain-identifier", bundle]
}
// ---- pure helpers ----
diff --git a/frontend/Tests/NSPortBuilderTests/CheatFilesTests.swift b/frontend/Tests/NSPortBuilderTests/CheatFilesTests.swift
index 6bf9b8c3..4a0b45a9 100644
--- a/frontend/Tests/NSPortBuilderTests/CheatFilesTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/CheatFilesTests.swift
@@ -292,11 +292,30 @@ final class CheatFilesTests: XCTestCase {
XCTAssertEqual(args[args.firstIndex(of: "--destination")! + 1], "Documents/games/\(titleID)/cheats")
XCTAssertEqual(args[args.firstIndex(of: "--domain-identifier")! + 1], "com.example.game")
XCTAssertEqual(args[args.firstIndex(of: "--domain-type")! + 1], "appDataContainer")
- XCTAssertEqual(args[args.firstIndex(of: "--remove-existing-content")! + 1], "true", "the device holds the Mac's files and nothing older")
+ XCTAssertFalse(args.contains("--remove-existing-content"), "devicectl's flag also deletes the destination's siblings (#250)")
XCTAssertEqual(args[args.firstIndex(of: "--device")! + 1], "00008150-AAAA")
XCTAssertEqual(try fm.contentsOfDirectory(atPath: temporary.path).filter { $0.hasPrefix("nxnative-cheats-") }, [], "the staged copy is removed")
}
+ /// #250: the cheats push must leave the game's romfs.img, aoc/ and save beside the cheats folder on the device.
+ func testTheCheatsPushLeavesTheGameDataBesideIt() throws {
+ try write("[A]\n00000000\n", "\(build).txt")
+ let device = SaveTransferTests.FakeDevice(temporary.appendingPathComponent("container"))
+ let game = device.container.appendingPathComponent("Documents/games/\(titleID)")
+ for (name, text) in [("romfs.img", "img"), ("aoc/a.bin", "dlc"), ("save/data.bin", "save"), ("cheats/old.txt", "old")] {
+ let url = game.appendingPathComponent(name)
+ try fm.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)
+ try Data(text.utf8).write(to: url)
+ }
+ try CheatTransfer(files: files, device: "00008150-AAAA", bundle: "com.example.game", devicectl: device.control, scratch: temporary).toDevice()
+ func read(_ n: String) -> String? { (try? Data(contentsOf: game.appendingPathComponent(n))).map { String(decoding: $0, as: UTF8.self) } }
+ XCTAssertEqual(read("romfs.img"), "img")
+ XCTAssertEqual(read("aoc/a.bin"), "dlc")
+ XCTAssertEqual(read("save/data.bin"), "save")
+ XCTAssertNotNil(read("cheats/\(build).txt"))
+ XCTAssertFalse(device.calls.contains { $0.contains("--remove-existing-content") })
+ }
+
func testAFailedCopyNamesDevicectlsLastLineAndAnEmptyFolderIsRefused() throws {
XCTAssertThrowsError(try transfer { _ in (true, "") }.toDevice()) { XCTAssertEqual($0 as? CheatTransfer.Failure, .noFiles) }
try write("[A]\n00000000\n", "\(build).txt")
diff --git a/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift b/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift
index ebca2483..bcbe810b 100644
--- a/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/SaveTransferTests.swift
@@ -68,9 +68,19 @@ final class SaveTransferTests: XCTestCase {
return (true, "")
case ["device", "copy", "to"]:
let destination = container.appendingPathComponent(value("--destination", args)!)
- if value("--remove-existing-content", args) == "true" { try? fm.removeItem(at: destination) }
- try! fm.createDirectory(at: destination.deletingLastPathComponent(), withIntermediateDirectories: true)
- try! fm.copyItem(at: URL(fileURLWithPath: value("--source", args)!), to: destination)
+ // What a real device does (#250): the flag deletes everything in the destination's parent, siblings included
+ if value("--remove-existing-content", args) == "true" {
+ let parent = destination.deletingLastPathComponent()
+ for entry in (try? fm.contentsOfDirectory(atPath: parent.path)) ?? [] { try? fm.removeItem(at: parent.appendingPathComponent(entry)) }
+ }
+ // Without it the directory is merged into the destination, files overwritten, nothing else touched
+ try! fm.createDirectory(at: destination, withIntermediateDirectories: true)
+ let source = URL(fileURLWithPath: value("--source", args)!)
+ for entry in try! fm.contentsOfDirectory(atPath: source.path) {
+ let target = destination.appendingPathComponent(entry)
+ try? fm.removeItem(at: target)
+ try! fm.copyItem(at: source.appendingPathComponent(entry), to: target)
+ }
return (true, "")
default:
return (false, "unexpected")
@@ -255,13 +265,45 @@ final class SaveTransferTests: XCTestCase {
XCTAssertEqual(read(backup.appendingPathComponent("save/data.bin")), "device save")
XCTAssertEqual(read(backup.appendingPathComponent("save/stale.bin")), "device stale")
XCTAssertEqual(read(game.appendingPathComponent("save/data.bin")), "mac save")
- XCTAssertFalse(fm.fileExists(atPath: game.appendingPathComponent("save/stale.bin").path))
+ XCTAssertEqual(read(game.appendingPathComponent("save/stale.bin")), "device stale", "merged: devicectl cannot delete one file, and the backup has it")
XCTAssertEqual(read(game.appendingPathComponent("romfs.img")), "img", "the game files beside the save stay")
+ XCTAssertFalse(device.calls.contains { $0.contains("--remove-existing-content") }, "the flag deletes the destination's siblings (#250)")
let copyTo = try XCTUnwrap(device.calls.first { $0.prefix(3) == ["device", "copy", "to"] })
XCTAssertEqual(device.value("--destination", copyTo), "Documents/games/\(titleID)/save", "never the game's folder, which holds romfs.img")
XCTAssertEqual(device.value("--source", copyTo), t.mac.appendingPathComponent("save").path)
}
+ /// #250: a save push must leave the game data in Documents and the game's folder where they are.
+ func testMacToDeviceLeavesTheGameDataOnTheDevice() throws {
+ let device = FakeDevice(temporary.appendingPathComponent("container"))
+ let t = setUp(device: device)
+ let docs = device.container.appendingPathComponent("Documents"), game = docs.appendingPathComponent("games/\(titleID)")
+ try write("image", docs.appendingPathComponent("romfs.img"), date: older)
+ try write("env", docs.appendingPathComponent("env.txt"), date: older)
+ try write("game image", game.appendingPathComponent("romfs.img"), date: older)
+ try write("dlc", game.appendingPathComponent("aoc/a.bin"), date: older)
+ try write("cheat", game.appendingPathComponent("cheats/x.txt"), date: older)
+ try write("device save", game.appendingPathComponent("save/data.bin"), date: older)
+ try write("mac save", t.mac.appendingPathComponent("save/data.bin"), date: newer)
+ _ = try t.macToDevice()
+ XCTAssertEqual(read(game.appendingPathComponent("save/data.bin")), "mac save")
+ XCTAssertEqual(read(docs.appendingPathComponent("romfs.img")), "image")
+ XCTAssertEqual(read(docs.appendingPathComponent("env.txt")), "env")
+ XCTAssertEqual(read(game.appendingPathComponent("romfs.img")), "game image")
+ XCTAssertEqual(read(game.appendingPathComponent("aoc/a.bin")), "dlc")
+ XCTAssertEqual(read(game.appendingPathComponent("cheats/x.txt")), "cheat")
+ }
+
+ /// The stand-in itself: with the flag it wipes the siblings, so a regression to it cannot pass unnoticed.
+ func testTheStandInWipesSiblingsWhenTheFlagIsPassed() throws {
+ let device = FakeDevice(temporary.appendingPathComponent("container"))
+ try write("image", device.container.appendingPathComponent("Documents/romfs.img"), date: older)
+ try write("x", temporary.appendingPathComponent("src/a.bin"), date: older)
+ let args = SaveTransfer.copyToCommand(device: "D", bundle: "b", source: temporary.appendingPathComponent("src").path, destination: "Documents/save") + ["--remove-existing-content", "true"]
+ XCTAssertTrue(device.control.run(args).ok)
+ XCTAssertFalse(fm.fileExists(atPath: device.container.appendingPathComponent("Documents/romfs.img").path))
+ }
+
func testMacToDeviceReplacesNothingWhenTheBackupFails() throws {
let device = FakeDevice(temporary.appendingPathComponent("container"))
let t = setUp(device: device)