Skip to content

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

Check the last RomFS version before skipping iPhone data transfer

thehobbit85b5ce7af7a161
diff --git a/frontend/Sources/NX2Apple/ShellCLI.swift b/frontend/Sources/NX2Apple/ShellCLI.swift
index d1728fa7..27a64f91 100644
--- a/frontend/Sources/NX2Apple/ShellCLI.swift
+++ b/frontend/Sources/NX2Apple/ShellCLI.swift
@@ -35,6 +35,7 @@ enum ShellCLI {
var installer = ShellInstaller(xcrun: o["xcrun"] ?? "/usr/bin/xcrun", repo: o["repo"]!, python: python, root: root,
device: o["device"]!, team: o["team"]!)
installer.deviceName = o["device"]!
+ installer.defaults = defaults
installer.toolPath = BundledTools.path("/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin", python: python)
installer.log = out
installer.stage = { s in
diff --git a/frontend/Sources/NXCore/ShellCopy.swift b/frontend/Sources/NXCore/ShellCopy.swift
index cc7ddafa..6dacfb6b 100644
--- a/frontend/Sources/NXCore/ShellCopy.swift
+++ b/frontend/Sources/NXCore/ShellCopy.swift
@@ -28,3 +28,20 @@ package struct ShellCopyPlan: Equatable {
}
package enum ShellCopyDecision: Equatable { case copy, skip, waitForCable }
+
+extension ShellInstaller {
+ /// The identity of the file we are about to send. The build's existing reader checks the image size and index
+ /// without reading the multi-GB payload. Include the framework's version for updates with an identical index.
+ func imageIdentity(archive: String, titleID: String) -> String? {
+ let tool = EngineLayout.root(repo) + "/" + RepoLayout.romfsImageTool
+ guard FileManager.default.fileExists(atPath: tool) else { return nil }
+ let game = ShellLibrary.load(root: root).games.first { $0.titleID == titleID }
+ let manifest = game.flatMap { FileManager.default.contents(atPath: $0.framework + "/game.manifest.json") }
+ .flatMap { (try? JSONSerialization.jsonObject(with: $0)) as? [String: Any] }
+ let args = ["-I", tool, "--identity", archive + "/romfs.img", titleID,
+ manifest?["display_version"] as? String ?? "", manifest?["package_version"] as? String ?? ""]
+ let result = runner.capture(ProcessLaunch(executable: python, arguments: args))
+ let line = result.out.trimmingCharacters(in: .whitespacesAndNewlines)
+ return result.ok && line.hasPrefix("DMROMFSID1 ") ? line : nil
+ }
+}
diff --git a/frontend/Sources/NXCore/ShellInstall.swift b/frontend/Sources/NXCore/ShellInstall.swift
index 76dafd77..b3b21940 100644
--- a/frontend/Sources/NXCore/ShellInstall.swift
+++ b/frontend/Sources/NXCore/ShellInstall.swift
@@ -312,8 +312,7 @@ package enum ShellInstall {
}
/// The local files to copy: missing on the device, or of another size. Sorted, so the copies run in a stable order.
- /// Path and size only: a file with the same size and other content stays. A hash comparison (a manifest the device keeps next to
- /// the game's folder) can close that gap later.
+ /// The caller also checks the last RomFS transfer before treating a same-size device image as a match.
package static func filesToCopy(local: [String: Int64], device: [String: Int64]) -> [String] {
local.filter { device[$0.key] != $0.value }.map(\.key).sorted()
}
@@ -394,6 +393,7 @@ package struct ShellInstaller {
package var team: String
package var deviceName = "iPhone"
package var lang: Lang = .en
+ package var defaults = UserDefaults.standard
/// Run tools in a process group of their own (the model's Stop ends the group). The terminal does not need it.
package var ownGroup = false
package var toolPath = "/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
@@ -589,7 +589,13 @@ package struct ShellInstaller {
env: nil, label: checkName, step: { _ in nil })
let onDevice = ShellInstall.parseListing(FileManager.default.contents(atPath: listing))
let local = ShellInstall.localFiles(in: archive)
- let missing = ShellInstall.filesToCopy(local: local, device: onDevice)
+ // The listing still catches missing and interrupted copies. Two releases can also have the same image size.
+ let receipt = "shellCopiedRomFS/\(device)/\(bundleID)/\(titleID)"
+ let previous = defaults.string(forKey: receipt)
+ let identity = local["romfs.img"] == nil ? nil : imageIdentity(archive: archive, titleID: titleID)
+ var comparable = onDevice
+ if identity != previous { comparable.removeValue(forKey: "romfs.img") }
+ let missing = ShellInstall.filesToCopy(local: local, device: comparable)
end(Self.checkStageID, checkName, ok: true)
guard !missing.isEmpty else {
outcome.message = fmt("shell.dataAlready", deviceName)
@@ -635,6 +641,8 @@ package struct ShellInstaller {
copyProgress(copiedBytes, needed)
stage(ShellStage(id: Self.copyStageID, name: copyName, state: .running, detail: "\(i + 1)/\(missing.count)",
percent: Double(i) / Double(missing.count) * 100))
+ // Once an overwrite begins, the old receipt cannot vouch for what a failed copy leaves behind.
+ if path == "romfs.img", previous != nil { defaults.removeObject(forKey: receipt) }
let r = run(ShellInstall.copyCommand(xcrun: xcrun, device: device, bundleID: bundleID, titleID: titleID,
source: archive + "/" + path, relativePath: path),
env: nil, label: copyName, step: BuildProgress.installStep)
@@ -651,6 +659,7 @@ package struct ShellInstaller {
outcome.copied = i + 1
copiedBytes += local[path] ?? 0
}
+ if let identity, missing.contains("romfs.img") { defaults.set(identity, forKey: receipt) }
copyProgress(needed, needed)
end(Self.copyStageID, copyName, ok: true)
return outcome
diff --git a/frontend/Sources/NXModel/BuildStore+Install.swift b/frontend/Sources/NXModel/BuildStore+Install.swift
index 31b2577c..f0e4560a 100644
--- a/frontend/Sources/NXModel/BuildStore+Install.swift
+++ b/frontend/Sources/NXModel/BuildStore+Install.swift
@@ -12,6 +12,7 @@ extension BuildStore {
let python = deps.pythonPath.isEmpty ? Toolchain.findPython().path : deps.pythonPath
var installer = ShellInstaller(xcrun: devices.xcrun, repo: repo.path, python: python, root: shellRoot, device: devices.installDevice, team: devices.signingTeam)
installer.deviceName = devices.deviceName(devices.installDevice)
+ installer.defaults = defaults
installer.lang = localizer.lang
installer.ownGroup = true
installer.toolPath = BuildCommands.toolPath("/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin", python: python)
diff --git a/frontend/Tests/NX2AppleTests/ShellInstallTests.swift b/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
index eae88d14..c750e467 100644
--- a/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
+++ b/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
@@ -427,6 +427,7 @@ import NXModel
private func installer(_ f: Fixture) -> ShellInstaller {
var i = ShellInstaller(xcrun: f.xcrun, repo: f.repo, python: "/usr/bin/python3", root: f.root, device: device, team: team)
i.deviceName = "Z"
+ i.defaults = testDefaults
i.pollInterval = 0.02
i.resources = nil
return i
@@ -562,6 +563,76 @@ import NXModel
XCTAssertEqual(calls.filter { $0.hasPrefix("devicectl device install app") }.count, 3)
}
+ private func fixtureWithRomFSReader() throws -> Fixture {
+ let f = try makeFixture()
+ let tool = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent()
+ .deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("engine/" + RepoLayout.romfsImageTool)
+ let tools = f.repo + "/dm/tools"
+ try fm.createDirectory(atPath: tools, withIntermediateDirectories: true)
+ try fm.copyItem(atPath: tool.path, toPath: tools + "/romfs_image.py")
+
+ return f
+ }
+
+ /// One byte of payload and a 24-byte index. Either byte can change independently at the same total size.
+ private func romfsImage(payload: UInt8 = 0, index: UInt8 = 0) -> Data {
+ var header = Data("DMROMFS1".utf8)
+ for word: UInt8 in [33, 24, 1] {
+ header.append(word)
+ header.append(Data(repeating: 0, count: 7))
+ }
+ return header + Data([payload, index]) + Data(repeating: 0, count: 23)
+ }
+
+ func testSameSizeGameUpdateNeedsAnotherCopyUntilItSucceeds() throws {
+ let f = try fixtureWithRomFSReader()
+ let image = f.archive + "/romfs.img"
+ let deviceImage = container + "/org.nxnative.shell/Documents/games/\(titleID)/romfs.img"
+ let first = romfsImage(), update = romfsImage(index: 1)
+ try first.write(to: URL(fileURLWithPath: image))
+ var i = installer(f)
+ XCTAssertTrue(install(i, f).ok)
+ XCTAssertEqual(try Data(contentsOf: URL(fileURLWithPath: deviceImage)), first)
+
+ try update.write(to: URL(fileURLWithPath: image))
+ var plans: [ShellCopyPlan] = []
+ i.decideCopy = { plans.append($0); return .skip }
+ XCTAssertTrue(i.installData(titleID: titleID, archive: f.archive).copySkipped)
+ XCTAssertEqual(plans.map(\.bytes), [Int64(update.count)])
+ XCTAssertEqual(try Data(contentsOf: URL(fileURLWithPath: deviceImage)), first)
+ i.decideCopy = { _ in .copy }
+ try write(state + "/fail-on", "romfs.img")
+ XCTAssertEqual(i.installData(titleID: titleID, archive: f.archive).failure, .shellDataIncomplete)
+ try fm.removeItem(atPath: state + "/fail-on")
+ XCTAssertEqual(i.installData(titleID: titleID, archive: f.archive).copied, 1)
+ XCTAssertEqual(try Data(contentsOf: URL(fileURLWithPath: deviceImage)), update)
+ i.decideCopy = { _ in XCTFail("an unchanged copy needs no prompt"); return .skip }
+ XCTAssertEqual(i.installData(titleID: titleID, archive: f.archive).copied, 0)
+ }
+
+ func testNewGameVersionWithUnchangedIndexAndReinstalledContainerBothNeedTheImage() throws {
+ let f = try fixtureWithRomFSReader()
+ let image = URL(fileURLWithPath: f.archive + "/romfs.img")
+ let manifest = f.framework + "/game.manifest.json"
+ try romfsImage().write(to: image)
+ try write(manifest, #"{"display_version":"1.0","package_version":"v0"}"#)
+ var i = installer(f)
+ XCTAssertEqual(install(i, f).copied, 2)
+ let update = romfsImage(payload: 9)
+ try update.write(to: image)
+ try write(manifest, #"{"display_version":"1.1","package_version":"v65536"}"#)
+ XCTAssertEqual(install(i, f).copied, 1)
+ let docs = container + "/org.nxnative.shell/Documents"
+ XCTAssertEqual(try Data(contentsOf: URL(fileURLWithPath: docs + "/games/\(titleID)/romfs.img")), update)
+
+ // A different paired device has its own copy history, even if its directory listing looks identical.
+ i.device = "00008150-BBBB"
+ try writeDevices(state + "/devices-default.json", [(i.device, "wired")])
+ XCTAssertEqual(i.installData(titleID: titleID, archive: f.archive).copied, 1)
+ try fm.removeItem(atPath: docs)
+ XCTAssertEqual(i.installData(titleID: titleID, archive: f.archive).copied, 2)
+ }
+
func testTeamVariantRetryStoresTheBundleIDAndTheNextRunUsesIt() throws {
let f = try makeFixture()
try write(state + "/taken", "")