Skip to content

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

macOS: the library finds games installed in /Applications and ~/Applications

Yoan Ancellyb31c08c3c573
diff --git a/frontend/Sources/NSPortBuilder/Library.swift b/frontend/Sources/NSPortBuilder/Library.swift
index 2b546105..95c0844a 100644
--- a/frontend/Sources/NSPortBuilder/Library.swift
+++ b/frontend/Sources/NSPortBuilder/Library.swift
@@ -255,6 +255,22 @@ final class GameLibrary: ObservableObject {
discover()
}
+ /// Where installed games are looked for besides the library folder (tests empty it).
+ var applicationFolders = [URL(fileURLWithPath: "/Applications"),
+ FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("Applications")]
+
+ /// Games moved to /Applications or ~/Applications after a build (the CLI leaves the app
+ /// beside the game, and people install it from there). Top level only, as the Finder
+ /// puts them; the library folder comes first, so its copy wins over an installed one.
+ static func installedApps(in dirs: [URL]) -> [URL] {
+ let fm = FileManager.default
+ return dirs.flatMap { dir in
+ ((try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles])) ?? [])
+ .filter { (try? MacGameBundle.read($0)) != nil }
+ .sorted { $0.path < $1.path }
+ }
+ }
+
/// A bounded scan of build locations, never of a game's multi-GB contents.
static func builtApps(in root: URL) -> [URL] {
let fm = FileManager.default
@@ -316,9 +332,9 @@ final class GameLibrary: ObservableObject {
func discover(repo: String = BuildModel.findRepo()) {
guard !folder.isEmpty, !scanning else { return }
scanning = true
- let root = URL(fileURLWithPath: folder)
+ let root = URL(fileURLWithPath: folder), installed = applicationFolders
DispatchQueue.global(qos: .utility).async {
- let apps = Self.builtApps(in: root)
+ let apps = Self.builtApps(in: root) + Self.installedApps(in: installed)
let sources = Self.sourceGames(in: root, repo: repo)
DispatchQueue.main.async {
let previousSelection = self.selectedID
diff --git a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift
index 7d2b3eca..318ccb56 100644
--- a/frontend/Tests/NSPortBuilderTests/LibraryTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/LibraryTests.swift
@@ -398,6 +398,18 @@ final class LibraryTests: XCTestCase {
XCTAssertEqual(try Data(contentsOf: file), data)
}
+ func testInstalledAppsAreFoundAtTopLevelOnly() throws {
+ let app = try makeApp(titleID)
+ let apps = temporary.appendingPathComponent("Applications")
+ try FileManager.default.createDirectory(at: apps.appendingPathComponent("Games"), withIntermediateDirectories: true)
+ let installed = apps.appendingPathComponent(app.lastPathComponent)
+ try FileManager.default.copyItem(at: app, to: installed)
+ try FileManager.default.moveItem(at: app, to: apps.appendingPathComponent("Games/" + app.lastPathComponent))
+ let missing = temporary.appendingPathComponent("NoSuchFolder")
+ XCTAssertEqual(GameLibrary.installedApps(in: [apps, missing]).map { $0.resolvingSymlinksInPath().path },
+ [installed.resolvingSymlinksInPath().path])
+ }
+
func testFolderDiscoveryPrunesGameContentsAndRemembersLocation() throws {
let app = try makeApp(titleID)
let nested = temporary.appendingPathComponent("My games/Hades/mac")
@@ -411,6 +423,7 @@ final class LibraryTests: XCTestCase {
XCTAssertTrue(GameLibrary.builtApps(in: temporary.appendingPathComponent("unmounted-volume")).isEmpty)
let state = temporary.appendingPathComponent("library.json")
let library = GameLibrary(file: state)
+ library.applicationFolders = [] // the machine's own /Applications is not part of this test
let finished = expectation(description: "Folder scan completes")
let token = library.$scanning.dropFirst().filter { !$0 }.sink { _ in finished.fulfill() }
library.setFolder(temporary)