Skip to content

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

Keep the paths of a drive that is not connected, clear them only when a mounted volume lacks the file

v0lumehi8c28d1534468
diff --git a/frontend/Sources/NSPortBuilder/Library.swift b/frontend/Sources/NSPortBuilder/Library.swift
index 2891fd2c..781b15dd 100644
--- a/frontend/Sources/NSPortBuilder/Library.swift
+++ b/frontend/Sources/NSPortBuilder/Library.swift
@@ -244,7 +244,7 @@ final class GameLibrary: ObservableObject {
let folder: String
}
- init(file: URL? = nil) {
+ init(file: URL? = nil, volumes: String = "/Volumes") {
self.file = file ?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
.appendingPathComponent("NS Port Builder/library.json")
if FileManager.default.fileExists(atPath: self.file.path) {
@@ -257,22 +257,32 @@ final class GameLibrary: ObservableObject {
}
catch { self.error = error.localizedDescription }
}
- pruneMissingPaths()
+ pruneMissingPaths(volumes: volumes)
selectedID = games.first?.id
}
- /// Forgets a remembered Mac app, IPA or iOS app that is gone from disk, so nothing points at it. Saves only when something changed;
- /// run on load and after a scan, never from a view.
- func pruneMissingPaths() {
+ /// A path on an external volume (`/Volumes/<name>/...`) whose mount point is absent: the drive is not connected, so the file is
+ /// not known to be gone and its path must stay.
+ static func onUnmountedVolume(_ path: String, volumes: String = "/Volumes") -> Bool {
+ let prefix = volumes.hasSuffix("/") ? volumes : volumes + "/"
+ guard path.hasPrefix(prefix), let name = path.dropFirst(prefix.count).split(separator: "/").first else { return false }
+ return !FileManager.default.fileExists(atPath: prefix + name)
+ }
+
+ /// Forgets a remembered Mac app, IPA or iOS app that is gone from disk, so nothing points at it. A file on a drive that is not
+ /// connected is kept (the game is just not ready until the drive is back); one missing from a mounted volume is cleared. Saves only
+ /// when something changed; run on load and after a scan, never from a view.
+ func pruneMissingPaths(volumes: String = "/Volumes") {
let fm = FileManager.default
+ func gone(_ path: String, plist: Bool = false) -> Bool {
+ !path.isEmpty && !Self.onUnmountedVolume(path, volumes: volumes) && !fm.fileExists(atPath: plist ? path + "/Info.plist" : path)
+ }
var changed = false
for index in games.indices {
let game = games[index]
- if !game.appPath.isEmpty && !fm.fileExists(atPath: game.appPath) { games[index].appPath = ""; changed = true }
- if !game.ipaPath.isEmpty && !fm.fileExists(atPath: game.ipaPath) { games[index].ipaPath = ""; changed = true }
- if !game.iosAppPath.isEmpty && !fm.fileExists(atPath: game.iosAppPath + "/Info.plist") {
- games[index].iosAppPath = ""; games[index].installedDevice = ""; changed = true
- }
+ if gone(game.appPath) { games[index].appPath = ""; changed = true }
+ if gone(game.ipaPath) { games[index].ipaPath = ""; changed = true }
+ if gone(game.iosAppPath, plist: true) { games[index].iosAppPath = ""; games[index].installedDevice = ""; changed = true }
}
if changed { save() }
}
diff --git a/frontend/Tests/NSPortBuilderTests/PrimaryActionTests.swift b/frontend/Tests/NSPortBuilderTests/PrimaryActionTests.swift
index d28e4ce1..11ea593c 100644
--- a/frontend/Tests/NSPortBuilderTests/PrimaryActionTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/PrimaryActionTests.swift
@@ -83,6 +83,29 @@ final class PrimaryActionTests: XCTestCase {
XCTAssertEqual(again.games.count, 2)
}
+ func testPathsOnAnUnmountedVolumeStayAndMissingFilesOnAMountedOneAreCleared() throws {
+ let volumes = temporary.appendingPathComponent("Volumes")
+ try FileManager.default.createDirectory(at: volumes.appendingPathComponent("NX"), withIntermediateDirectories: true) // mounted
+ var away = LibraryGame(titleID: titleID, name: "Away", appPath: volumes.path + "/Unplugged/Games/Hades.app",
+ ipaPath: volumes.path + "/Unplugged/Hades.ipa", iosAppPath: volumes.path + "/Unplugged/ios/Hades.app")
+ away.installedDevice = "UDID"
+ let deleted = LibraryGame(titleID: "0100000000010000", name: "Deleted", appPath: volumes.path + "/NX/Games/Gone.app",
+ ipaPath: volumes.path + "/NX/Gone.ipa")
+ let file = temporary.appendingPathComponent("library.json")
+ try JSONEncoder().encode([away, deleted]).write(to: file)
+ let library = GameLibrary(file: file, volumes: volumes.path) // the load prunes, against this fake /Volumes
+ let a = try XCTUnwrap(library.games.first { $0.name == "Away" }), d = try XCTUnwrap(library.games.first { $0.name == "Deleted" })
+ XCTAssertEqual(a.appPath, volumes.path + "/Unplugged/Games/Hades.app", "the drive is not connected: nothing is forgotten")
+ XCTAssertEqual(a.ipaPath, volumes.path + "/Unplugged/Hades.ipa")
+ XCTAssertEqual(a.iosAppPath, volumes.path + "/Unplugged/ios/Hades.app")
+ XCTAssertEqual(a.installedDevice, "UDID")
+ XCTAssertFalse(a.isReady)
+ XCTAssertEqual([d.appPath, d.ipaPath], ["", ""], "the volume is mounted and the files (and their folder) are gone")
+ XCTAssertTrue(GameLibrary.onUnmountedVolume(volumes.path + "/Unplugged/x", volumes: volumes.path))
+ XCTAssertFalse(GameLibrary.onUnmountedVolume(volumes.path + "/NX/x", volumes: volumes.path))
+ XCTAssertFalse(GameLibrary.onUnmountedVolume("/Users/someone/x.app"))
+ }
+
func testPruneAfterAppIsDeletedWhileRunning() throws {
let app = try makeApp()
let library = GameLibrary(file: temporary.appendingPathComponent("library.json"))