Skip to content

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

Library scan: climb out of a base subfolder when sibling subfolders hold updates or DLC

LNPANDY8c65c261c448
diff --git a/frontend/Sources/NX2Apple/Library.swift b/frontend/Sources/NX2Apple/Library.swift
index c24d8d0c..42d24beb 100644
--- a/frontend/Sources/NX2Apple/Library.swift
+++ b/frontend/Sources/NX2Apple/Library.swift
@@ -447,11 +447,22 @@ final class GameLibrary: ObservableObject {
// The enumerator hands out resolved paths (/private/var/…) while the library folder may not be, so compare resolved.
let rootReal = root.resolvingSymlinksInPath().path
func inside(_ u: URL) -> String { u.resolvingSymlinksInPath().path }
+ let skipDirs: Set<String> = ["romfs", "extract", "tools", "venv", "archive", "harness", "runs", "shader", "xcode"]
+ func holdsFamily(_ folder: URL, except child: String) -> Bool {
+ guard let names = try? FileManager.default.contentsOfDirectory(atPath: folder.path) else { return false }
+ if names.contains(where: family) { return true }
+ for name in names where name != child && !name.hasPrefix(".") {
+ let low = name.lowercased()
+ if skipDirs.contains(low) || low.hasSuffix("_build") || low.hasSuffix("_survey") { continue }
+ if let sub = try? FileManager.default.contentsOfDirectory(atPath: folder.appendingPathComponent(name).path),
+ sub.contains(where: family) { return true }
+ }
+ return false
+ }
while inside(gameRoot) != rootReal, inside(gameRoot).hasPrefix(rootReal) {
let parent = gameRoot.deletingLastPathComponent()
guard inside(parent).hasPrefix(rootReal),
- let names = try? FileManager.default.contentsOfDirectory(atPath: parent.path),
- names.contains(where: family) else { break }
+ holdsFamily(parent, except: gameRoot.lastPathComponent) else { break }
gameRoot = parent
}
// Packages of this game anywhere in that folder, a few levels at most (what the build script itself scans).
@@ -505,7 +516,9 @@ final class GameLibrary: ObservableObject {
for slot in ["update", "dlc", "keys"] where existing.inputs[slot, default: []].isEmpty {
existing.inputs[slot] = source.inputs[slot] ?? []
}
- if existing.sourceFolder.isEmpty { existing.sourceFolder = source.sourceFolder }
+ if existing.sourceFolder.isEmpty || (!source.sourceFolder.isEmpty && URL(fileURLWithPath: existing.sourceFolder).resolvingSymlinksInPath().path.hasPrefix(URL(fileURLWithPath: source.sourceFolder).resolvingSymlinksInPath().path + "/")) {
+ existing.sourceFolder = source.sourceFolder
+ }
}
if existing.ipaPath.isEmpty { existing.ipaPath = source.ipaPath }
self.upsert(existing)
diff --git a/frontend/Tests/NX2AppleTests/LibraryTests.swift b/frontend/Tests/NX2AppleTests/LibraryTests.swift
index 798478c0..53437036 100644
--- a/frontend/Tests/NX2AppleTests/LibraryTests.swift
+++ b/frontend/Tests/NX2AppleTests/LibraryTests.swift
@@ -533,6 +533,47 @@ import NXModel
XCTAssertEqual(zelda.inputs["dlc"], [])
}
+ /// The base sits in "<game>/nsz本体/", the update in "<game>/升级档v1.1.3/" and the DLC in "<game>/DLC/" (sibling
+ /// subfolders under the game's folder, with no package directly in "<game>/"): the climb must still reach "<game>/"
+ /// and a rescan must lift a previously recorded subfolder sourceFolder to it.
+ func testAScanFindsTheUpdateAndDLCInSiblingSubfolders() throws {
+ let fm = FileManager.default
+ let game = temporary.appendingPathComponent("小丑牌")
+ let baseDir = game.appendingPathComponent("nsz本体")
+ let updDir = game.appendingPathComponent("升级档v1.1.3")
+ let dlcDir = game.appendingPathComponent("DLC")
+ for d in [baseDir, updDir, dlcDir] { try fm.createDirectory(at: d, withIntermediateDirectories: true) }
+ let baseNSZ = baseDir.appendingPathComponent("小丑牌[0100CD801CE5E000][US][v0].nsz")
+ let updNSZ = updDir.appendingPathComponent("小丑牌[0100CD801CE5E800][US][v393216].nsz")
+ let dlcNSZ = dlcDir.appendingPathComponent("小丑牌[0100CD801CE5F001][v65536].nsz")
+ for f in [baseNSZ, updNSZ, dlcNSZ] { try Data([0]).write(to: f) }
+
+ let games = GameLibrary.sourceGames(in: temporary, repo: "")
+ let balatro = try XCTUnwrap(games.first { $0.titleID == "0100CD801CE5E000" })
+ XCTAssertEqual(Self.canon(balatro.sourceFolder), Self.canon(game.path), "climbs out of nsz本体 when a sibling subfolder holds the update")
+ XCTAssertEqual(balatro.inputs["base"]?.map(Self.canon), [Self.canon(baseNSZ.path)])
+ XCTAssertEqual(balatro.inputs["update"]?.map(Self.canon), [Self.canon(updNSZ.path)])
+ XCTAssertEqual(balatro.inputs["dlc"]?.map(Self.canon), [Self.canon(dlcNSZ.path)])
+
+ // An earlier scan recorded the base subfolder as sourceFolder and missed the sibling update: discover() repairs both.
+ let state = temporary.appendingPathComponent("library.json")
+ var stale = LibraryGame(titleID: "0100CD801CE5E000", name: "Balatro")
+ stale.inputs = ["base": [baseNSZ.path], "update": [], "dlc": [], "keys": []]
+ stale.sourceFolder = baseDir.path
+ try JSONEncoder().encode([stale]).write(to: state)
+ let library = GameLibrary(file: state)
+ library.applicationFolders = []
+ let finished = expectation(description: "Rescan repairs sibling update and sourceFolder")
+ let token = library.$scanning.dropFirst().filter { !$0 }.sink { _ in finished.fulfill() }
+ library.setFolder(temporary)
+ wait(for: [finished], timeout: 3)
+ withExtendedLifetime(token) {}
+ let repaired = try XCTUnwrap(library.games.first { $0.titleID == "0100CD801CE5E000" })
+ XCTAssertEqual(Self.canon(repaired.sourceFolder), Self.canon(game.path))
+ XCTAssertEqual(repaired.inputs["update"]?.map(Self.canon), [Self.canon(updNSZ.path)])
+ XCTAssertEqual(repaired.inputs["dlc"]?.map(Self.canon), [Self.canon(dlcNSZ.path)])
+ }
+
/// Base, update and DLC in one folder: the folder is its own game folder and the climb goes no further.
func testAScanOfAFlatGameFolderStopsAtThatFolder() throws {
let flat = temporary.appendingPathComponent("Hades")