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
A ROMs folder holding several games builds the one that was picked (issue #58)
LNPANDYb756408aed67
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swiftindex 1ba479ca..82795015 100644--- a/frontend/Sources/NSPortBuilder/ContentView.swift+++ b/frontend/Sources/NSPortBuilder/ContentView.swift@@ -666,21 +666,48 @@ final class BuildModel: ObservableObject {}let py = pythonPath.isEmpty ? "/usr/bin/python3" : pythonPath- // One folder when everything lives together; otherwise a staging folder of symlinks.+ // One folder when everything lives together; otherwise a staging folder of symlinks. A folder+ // that holds other games too (a ROMs folder with one file per game) is staged as well: the+ // script takes one game per folder and refuses one with several ("more than one game in the+ // folder"), even when only the base game was picked (issue #58; ufooer's PR #31).let baseDir = (basePath as NSString).deletingLastPathComponentlet allPaths = [basePath, updatePath] + dlcPathslet dirs = Set(allPaths.filter { !$0.isEmpty }.map { ($0 as NSString).deletingLastPathComponent })+ let picked = Set(allPaths.filter { !$0.isEmpty }.map { ($0 as NSString).lastPathComponent })+ let unpicked = ((try? FileManager.default.contentsOfDirectory(atPath: baseDir)) ?? []).filter {+ ["nsp", "xci", "nsz", "xcz"].contains(($0 as NSString).pathExtension.lowercased()) && !picked.contains($0)+ }+ // Staged, the folder's own update and add-on content would be left behind, where the script+ // used to find them unpicked: bring along those whose file name carries this game's title ID+ // exactly as build_ipa_from_nsp.py:445 reads it: the low 12 bits are 0x000 for a base game,+ // 0x800 for its update, and anything else is add-on content below the base.+ var staged = allPaths+ if let base = Self.titleID((basePath as NSString).lastPathComponent) {+ let app = base & ~0xFFF+ for f in unpicked {+ guard let tid = Self.titleID(f) else { continue }+ let low = tid & 0xFFF+ let owner = low == 0 ? tid : (low == 0x800 ? tid & ~0xFFF : (tid & ~0xFFF) - 0x1000)+ guard owner == app else { continue }+ if low == 0x800 && !updatePath.isEmpty { continue } // the update was picked already+ if low != 0 && low != 0x800 && tid <= app { continue } // add-on content sits above its base+ staged.append(baseDir + "/" + f)+ }+ }var inputDir = baseDir- if dirs.count > 1 {+ if dirs.count > 1 || !unpicked.isEmpty {let tmp = NSTemporaryDirectory() + "nsport-\(UUID().uuidString.prefix(8))"try? FileManager.default.createDirectory(atPath: tmp, withIntermediateDirectories: true)var linked = 0- for f in allPaths where !f.isEmpty {+ for f in staged where !f.isEmpty {let dst = tmp + "/" + (f as NSString).lastPathComponentif (try? FileManager.default.createSymbolicLink(atPath: dst, withDestinationPath: f)) != nil { linked += 1 }}if linked == 0 { fail(t("msg.stagingFailed")); return }staging = tmp; inputDir = tmp+ if !unpicked.isEmpty {+ say(String(format: t("msg.stagedFromSharedFolder"), (basePath as NSString).lastPathComponent))+ }} else { staging = nil }let tidHex = Self.titleID(basePath).map { String(format: "%016llX", $0) } ?? "game"diff --git a/frontend/Sources/NSPortBuilder/Localization.swift b/frontend/Sources/NSPortBuilder/Localization.swiftindex cb3b82cb..37bd99a8 100644--- a/frontend/Sources/NSPortBuilder/Localization.swift+++ b/frontend/Sources/NSPortBuilder/Localization.swift@@ -404,6 +404,7 @@ enum L10n {"msg.exitCode": ("退出码 %d", "exit code %d"),"msg.stagingFailed": ("无法在暂存文件夹里建立链接(文件在不同磁盘?)","Could not create links in the staging folder (files on different volumes?)"),+ "msg.stagedFromSharedFolder": ("文件夹里还有其他游戏:只把选中的这个游戏(%@)临时复制出去构建", "The folder holds other games: building from a temporary copy of this one (%@)"),"msg.launchFailed": ("无法启动构建脚本:%@", "Could not start the build script: %@"),"msg.repoInvalid": ("仓库路径无效:%@——用上方的「选择仓库…」指向本项目的 clone","Invalid repository path: %@ — use Choose repository… above to point at your clone"),diff --git a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift b/frontend/Tests/NSPortBuilderTests/LibraryTests.swiftindex 2d47100c..52696202 100644--- a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift+++ b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift@@ -762,7 +762,6 @@ final class LibraryTests: XCTestCase {try capture("building", activeID: library.games[1].id)model.running = false}-}/// Issue #52: the failure note names the last real line, not just the exit code.func testLastMeaningfulLineSkipsProgressEventsAndBlankLines() {@@ -777,3 +776,26 @@ final class LibraryTests: XCTestCase {XCTAssertNil(BuildModel.lastMeaningfulLine("\n\n"))XCTAssertNil(BuildModel.lastMeaningfulLine("{\"event\":\"stage_end\"}\n"))}++ /// Issue #58: a folder holding several games is staged down to the picked one, and its update+ /// and add-on content come along — classified exactly as build_ipa_from_nsp.py:445 does.+ func testSiblingsOfThePickedGameAreClassifiedAsTheScriptDoes() {+ let base = UInt64(0x010015100B514000)+ let app = base & ~UInt64(0xFFF)+ func owner(_ tid: UInt64) -> UInt64 {+ let low = tid & 0xFFF+ return low == 0 ? tid : (low == 0x800 ? tid & ~UInt64(0xFFF) : (tid & ~UInt64(0xFFF)) - 0x1000)+ }+ XCTAssertEqual(owner(base), app, "the base game")+ XCTAssertEqual(owner(base + 0x800), app, "its update")+ XCTAssertEqual(owner(base + 0x1001), app, "add-on content 1")+ XCTAssertNotEqual(owner(UInt64(0x01007EF00011E000)), app, "another game")+ XCTAssertNotEqual(owner(UInt64(0x01007EF00011E800)), app, "another game's update")+ }++ func testTitleIDReadsTheBracketedFormTheFilesUse() {+ XCTAssertEqual(BuildModel.titleID("Mario Wonder [010015100B514000][v0].nsp"), 0x010015100B514000)+ XCTAssertEqual(BuildModel.titleID("/ROMs/TotK [01007EF00011E000].xci"), 0x01007EF00011E000)+ XCTAssertNil(BuildModel.titleID("no title id here.nsp"))+ }+}