Skip to content

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

Shell install: take the framework the build wrote, not one named after the build key (#515)

Haseenesssc619ae9cf70c
diff --git a/frontend/Sources/NXCore/ShellInstall.swift b/frontend/Sources/NXCore/ShellInstall.swift
index bafa35c6..691d1b95 100644
--- a/frontend/Sources/NXCore/ShellInstall.swift
+++ b/frontend/Sources/NXCore/ShellInstall.swift
@@ -145,6 +145,19 @@ package enum ShellInstall {
workDir + "/shell-framework/DmGame\(titleID).framework"
}
+ /// The framework the build wrote in `<work>/shell-framework`. The script names it after the title ID it read from the game
+ /// data, which a file name without a title ID does not carry (the build key is then the name plus a hash, #515), so the
+ /// handoff takes the folder's own `DmGame*.framework`: the newest when an older build left a second one. nil when there is none.
+ package static func builtFramework(workDir: String, fm: FileManager = .default) -> String? {
+ let folder = workDir + "/shell-framework"
+ let names = ((try? fm.contentsOfDirectory(atPath: folder)) ?? [])
+ .filter { $0.hasPrefix("DmGame") && $0.hasSuffix(".framework") && $0.count > "DmGame.framework".count }
+ func modified(_ name: String) -> Date {
+ ((try? fm.attributesOfItem(atPath: folder + "/" + name))?[.modificationDate] as? Date) ?? .distantPast
+ }
+ return names.max { (modified($0), $0) < (modified($1), $1) }.map { folder + "/" + $0 }
+ }
+
/// The part of the build script's argument list that makes the game's framework instead of an Xcode project.
package static func buildArguments(workDir: String, team: String) -> [String] {
["--shell-framework", workDir + "/shell-framework"] + (team.isEmpty ? [] : ["--team-id", team])
diff --git a/frontend/Sources/NXModel/BuildStore+Archive.swift b/frontend/Sources/NXModel/BuildStore+Archive.swift
index 782b8c8d..1f2d0486 100644
--- a/frontend/Sources/NXModel/BuildStore+Archive.swift
+++ b/frontend/Sources/NXModel/BuildStore+Archive.swift
@@ -20,7 +20,8 @@ extension BuildStore {
/// <work>/extract/v<N>/RomFS where N is the profile's package version, so take
/// the highest v* that actually has a RomFS instead of hardcoding one game.
/// `then` continues the build with the archive's folder (installing on a device).
- package func packArchive(workDir: String, then: @escaping (String) -> Void) {
+ /// `titleID` names the folder (the framework's title ID); the build key when it is not given.
+ package func packArchive(workDir: String, titleID: String? = nil, then: @escaping (String) -> Void) {
running = true
let fm = FileManager.default
let extract = workDir + "/extract"
@@ -42,7 +43,7 @@ extension BuildStore {
// exposes on an external drive) then needs no renaming, and several games' folders can sit
// side by side in one app.
let gamesRoot = (outDir.isEmpty ? (basePath as NSString).deletingLastPathComponent : outDir) + "/games"
- let tid = TitleID.buildKey(basePath)
+ let tid = titleID.flatMap { $0.isEmpty ? nil : $0 } ?? TitleID.buildKey(basePath)
// games/<id>/ holds romfs.img and aoc/ side by side: main.m's per-game lookup takes the
// image, and the host mounts DLC from aoc/<index>.img (hle_fs.c aoc_dir). One folder, which
// the user copies to the phone or keeps on an external drive as a whole.
diff --git a/frontend/Sources/NXModel/BuildStore+Run.swift b/frontend/Sources/NXModel/BuildStore+Run.swift
index d5706b51..9fd66138 100644
--- a/frontend/Sources/NXModel/BuildStore+Run.swift
+++ b/frontend/Sources/NXModel/BuildStore+Run.swift
@@ -219,10 +219,14 @@ extension BuildStore {
self.cleanupStaging()
} else {
// The game's archive, then its framework into the one shared app on the device
- let tid = TitleID.buildKey(self.basePath)
+ // The framework the build wrote, and the title ID in its name: the script names it after the game
+ // data's title ID, which the build key lacks when the file name has none (#515)
+ let key = TitleID.buildKey(self.basePath)
let name = ((self.basePath as NSString).lastPathComponent as NSString).deletingPathExtension
- let fw = ShellInstall.frameworkPath(workDir: self.effectiveWorkDir, titleID: tid)
- self.packArchive(workDir: self.effectiveWorkDir) {
+ let fw = ShellInstall.builtFramework(workDir: self.effectiveWorkDir)
+ ?? ShellInstall.frameworkPath(workDir: self.effectiveWorkDir, titleID: key)
+ let tid = ShellInstall.titleID(ofFramework: fw)
+ self.packArchive(workDir: self.effectiveWorkDir, titleID: tid) {
if self.exportsShell { self.exportShell(framework: fw, titleID: tid, name: name) }
else { self.installShell(framework: fw, titleID: tid, name: name, archive: $0) }
}
diff --git a/frontend/Tests/NX2AppleTests/ShellInstallTests.swift b/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
index b7ce5d57..564a8447 100644
--- a/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
+++ b/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
@@ -145,6 +145,28 @@ import NXModel
home: "/h", user: "u")["DM_SHELL_PACKAGE"])
}
+ /// #515: a file name without a title ID gives a build key the build never names its framework after. The handoff takes the
+ /// framework the build wrote, and its title ID, not the key.
+ func testBuiltFrameworkIsTheOneTheBuildWrote() throws {
+ let name = "The_Legend_of_Zelda_Breath_of_the_Wild-7603d90e"
+ let key = TitleID.buildKey("/in/" + name + ".nsp")
+ XCTAssertFalse(key.hasPrefix("01"))
+ let work = try makeTree(["shell-framework/DmGame01007EF00011E000.framework/game.manifest.json", "shell-framework/notes.txt"])
+ let fw = try XCTUnwrap(ShellInstall.builtFramework(workDir: work))
+ XCTAssertEqual(fw, work + "/shell-framework/DmGame01007EF00011E000.framework")
+ XCTAssertNotEqual(fw, ShellInstall.frameworkPath(workDir: work, titleID: key))
+ XCTAssertEqual(ShellInstall.titleID(ofFramework: fw), "01007EF00011E000")
+
+ // An older build's framework beside it: the newest one is the build's
+ let old = work + "/shell-framework/DmGame0100000000010000.framework"
+ try FileManager.default.createDirectory(atPath: old, withIntermediateDirectories: true)
+ try FileManager.default.setAttributes([.modificationDate: Date(timeIntervalSinceNow: -3600)], ofItemAtPath: old)
+ XCTAssertEqual(ShellInstall.builtFramework(workDir: work), fw)
+
+ XCTAssertNil(ShellInstall.builtFramework(workDir: try makeTree(["shell-framework/notes.txt"])))
+ XCTAssertNil(ShellInstall.builtFramework(workDir: try makeTree([])))
+ }
+
func testCommandsAreExact() {
let root = "/R/NX2Apple-shell"
XCTAssertEqual(ShellInstall.buildArguments(workDir: "/W", team: "T"), ["--shell-framework", "/W/shell-framework", "--team-id", "T"])