Skip to content

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

Uninstaller: keep a checkout that holds games or builds, remove settings

v0lumehi1bc890c644fd
diff --git a/frontend/Sources/NSPortBuilder/ContentView.swift b/frontend/Sources/NSPortBuilder/ContentView.swift
index d59a2cf7..01b8cdab 100644
--- a/frontend/Sources/NSPortBuilder/ContentView.swift
+++ b/frontend/Sources/NSPortBuilder/ContentView.swift
@@ -3520,12 +3520,9 @@ struct BuilderView: View {
// The uninstaller (Uninstall.swift): state folders and the launcher's own checkout go; the
// games folder and prod.keys stay; the app itself goes to the Trash.
row(m.t("uninstall.title"), detail: m.t("uninstall.note")) {
- Button(m.t("uninstall.button")) {
- m.uninstallPlan = Uninstall.plan(home: NSHomeDirectory(), gamesFolder: library.folder,
- checkouts: [m.repoPath])
- }
+ Button(m.t("uninstall.button")) { m.uninstallPlan = uninstallPlan() }
.disabled(m.running)
- .padTarget("set.uninstall", scope: "main") { if !m.running { m.uninstallPlan = Uninstall.plan(home: NSHomeDirectory(), gamesFolder: library.folder, checkouts: [m.repoPath]) } }
+ .padTarget("set.uninstall", scope: "main") { if !m.running { m.uninstallPlan = uninstallPlan() } }
}
divider
}
@@ -3551,6 +3548,11 @@ struct BuilderView: View {
m.lang = Lang.initial()
}
+ private func uninstallPlan() -> Uninstall.Plan {
+ Uninstall.plan(home: NSHomeDirectory(), gamesFolder: library.folder, outputFolder: m.outDir,
+ checkouts: [m.repoPath], defaultsDomain: Bundle.main.bundleIdentifier)
+ }
+
private var tools: some View {
group(m.t("section.tools"), footnote: m.repoStatus.isEmpty ? (m.repoOK ? "" : m.t("repo.firstHint")) : m.repoStatus) {
row(m.t("repo.title"),
diff --git a/frontend/Sources/NSPortBuilder/Uninstall.swift b/frontend/Sources/NSPortBuilder/Uninstall.swift
index 7f60d4db..bc9158e0 100644
--- a/frontend/Sources/NSPortBuilder/Uninstall.swift
+++ b/frontend/Sources/NSPortBuilder/Uninstall.swift
@@ -7,13 +7,17 @@ import AppKit
/// - engine trees this launcher installed (`~/Library/Application Support/NX2Apple/engine/`)
/// - the project checkout the launcher itself cloned (`~/NX2Apple`, only when it is the launcher's own
/// download; a checkout the player made by hand is not touched)
+/// - the launcher's settings (its UserDefaults domain)
///
-/// What stays: the games folder and everything in it, `prod.keys`, Homebrew, and the app itself, which
-/// goes to the Trash when the player confirms — never deleted.
+/// What stays: the games folder and everything in it, the build output folder, `prod.keys`, Homebrew, and the
+/// app itself, which goes to the Trash when the player confirms — never deleted. A folder that holds the games
+/// folder or the build output folder stays as a whole.
enum Uninstall {
struct Plan: Equatable {
var stateFolders: [String] = []
var checkouts: [String] = []
+ /// The UserDefaults domain with the launcher's settings, nil when there is none to remove.
+ var defaultsDomain: String?
/// Where the app lives now (goes to the Trash, not deleted).
var app: String = Bundle.main.bundlePath
}
@@ -22,23 +26,33 @@ enum Uninstall {
/// folders that exist, and a checkout that is the launcher's own download (the clone destination
/// `cloneRepo` uses) rather than one the player picked from anywhere on disk.
/// `home` is passed in (NSHomeDirectory() at the call site): a computed home is not testable.
- nonisolated static func plan(home: String, gamesFolder: String, checkouts: [String]) -> Plan {
+ nonisolated static func plan(home: String, gamesFolder: String, outputFolder: String = "", checkouts: [String],
+ defaultsDomain: String? = nil) -> Plan {
let fm = FileManager.default
- var p = Plan()
+ var p = Plan(defaultsDomain: defaultsDomain)
+ let kept = [gamesFolder, outputFolder].filter { !$0.isEmpty }
+ let holdsPlayerFiles = { (folder: String) in kept.contains { contains(folder, $0) } }
let state = home + "/Library/Application Support/NS Port Builder"
- if fm.fileExists(atPath: state) { p.stateFolders.append(state) }
+ if fm.fileExists(atPath: state), !holdsPlayerFiles(state) { p.stateFolders.append(state) }
let engine = home + "/Library/Application Support/NX2Apple"
- if fm.fileExists(atPath: engine) { p.stateFolders.append(engine) }
+ if fm.fileExists(atPath: engine), !holdsPlayerFiles(engine) { p.stateFolders.append(engine) }
let own = home + "/NX2Apple"
for checkout in checkouts where fm.fileExists(atPath: checkout) {
// The launcher's own clone sits at ~/NX2Apple and is a git checkout with origin = this repo.
// A folder the player picked from anywhere else stays: we cannot know what else lives there.
- guard checkout == own else { continue }
+ guard checkout == own, !holdsPlayerFiles(checkout) else { continue }
if let origin = gitOrigin(checkout), origin.contains("LNPANDY/NX2Apple") { p.checkouts.append(checkout) }
}
return p
}
+ /// Whether `path` is `folder` or lies inside it, after resolving symbolic links and `..`.
+ nonisolated static func contains(_ folder: String, _ path: String) -> Bool {
+ let f = URL(fileURLWithPath: folder).resolvingSymlinksInPath().standardizedFileURL.pathComponents
+ let p = URL(fileURLWithPath: path).resolvingSymlinksInPath().standardizedFileURL.pathComponents
+ return p.count >= f.count && Array(p.prefix(f.count)) == f
+ }
+
nonisolated static func gitOrigin(_ checkout: String) -> String? {
guard let git = BuildModel.gitBinary() else { return nil }
let r = BuildModel.runQuiet([git, "-C", checkout, "remote", "get-url", "origin"], env: BuildModel.gitEnv())
@@ -55,6 +69,10 @@ enum Uninstall {
do { try fm.removeItem(atPath: folder); removed.append(folder) }
catch { failures.append(folder + ": " + error.localizedDescription) }
}
+ if let domain = plan.defaultsDomain {
+ UserDefaults.standard.removePersistentDomain(forName: domain)
+ UserDefaults.standard.synchronize()
+ }
if movingAppToTrash, plan.app.hasSuffix(".app"), plan.app.contains("/Applications/") {
if !fm.fileExists(atPath: plan.app) { failures.append(plan.app + ": not found") }
else if !trash(plan.app) { failures.append(plan.app + ": could not move to the Trash") }
diff --git a/frontend/Tests/NSPortBuilderTests/UninstallTests.swift b/frontend/Tests/NSPortBuilderTests/UninstallTests.swift
index ca971c7d..751e75c6 100644
--- a/frontend/Tests/NSPortBuilderTests/UninstallTests.swift
+++ b/frontend/Tests/NSPortBuilderTests/UninstallTests.swift
@@ -71,6 +71,43 @@ final class UninstallTests: XCTestCase {
XCTAssertTrue(plan.checkouts.isEmpty, "a fork with another origin is the player's own work")
}
+ /// A games folder or build output folder inside ~/NX2Apple keeps the whole checkout: removing it
+ /// would take the player's games or builds along.
+ func testACheckoutHoldingTheGamesOrTheBuildsStays() throws {
+ let checkout = home.appendingPathComponent("NX2Apple")
+ try originFixture(checkout: checkout)
+ let inner = checkout.appendingPathComponent("Games")
+ try FileManager.default.createDirectory(at: inner, withIntermediateDirectories: true)
+ try Data("save".utf8).write(to: inner.appendingPathComponent("save.dat"))
+
+ let gamesInside = Uninstall.plan(home: home.path, gamesFolder: inner.path, checkouts: [checkout.path])
+ XCTAssertTrue(gamesInside.checkouts.isEmpty, "the games folder lives in the checkout")
+ let gamesAsCheckout = Uninstall.plan(home: home.path, gamesFolder: checkout.path + "/", checkouts: [checkout.path])
+ XCTAssertTrue(gamesAsCheckout.checkouts.isEmpty, "the games folder is the checkout itself")
+ let outputInside = Uninstall.plan(home: home.path, gamesFolder: games.path,
+ outputFolder: checkout.appendingPathComponent("builds").path,
+ checkouts: [checkout.path])
+ XCTAssertTrue(outputInside.checkouts.isEmpty, "the build output lives in the checkout")
+ let sibling = Uninstall.plan(home: home.path, gamesFolder: home.appendingPathComponent("NX2AppleGames").path,
+ checkouts: [checkout.path])
+ XCTAssertEqual(sibling.checkouts, [checkout.path], "a folder that only shares the name prefix does not count")
+
+ let result = Uninstall.run(outputInside, movingAppToTrash: false)
+ XCTAssertTrue(result.failures.isEmpty, result.failures.joined(separator: "\n"))
+ XCTAssertTrue(FileManager.default.fileExists(atPath: inner.appendingPathComponent("save.dat").path))
+ }
+
+ /// The settings live in UserDefaults, not in a state folder: the uninstall removes that domain too.
+ func testTheSettingsDomainIsRemoved() throws {
+ let domain = "uninstall-test-" + UUID().uuidString
+ UserDefaults.standard.setPersistentDomain(["buildOutputDirectory": "/somewhere"], forName: domain)
+ defer { UserDefaults.standard.removePersistentDomain(forName: domain) }
+ let plan = Uninstall.plan(home: home.path, gamesFolder: games.path, checkouts: [], defaultsDomain: domain)
+ XCTAssertEqual(plan.defaultsDomain, domain)
+ _ = Uninstall.run(plan, movingAppToTrash: false)
+ XCTAssertNil(UserDefaults.standard.persistentDomain(forName: domain)?["buildOutputDirectory"])
+ }
+
func testThePercentTextAndTrashAreHonestAboutFailures() {
XCTAssertFalse(Uninstall.trash("/nonexistent/" + UUID().uuidString), "a missing path reports failure")
}