Skip to content

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

Shell folder: default to Application Support, move an old one out of the output folder once (#427)

v0lumehi0b4d8f9bd4b8
diff --git a/frontend/README.md b/frontend/README.md
index 4bc953a6..ce0b036a 100644
--- a/frontend/README.md
+++ b/frontend/README.md
@@ -74,7 +74,13 @@ Xcode, a development certificate (sign in to Xcode with your Apple ID under Sett
and a device paired with this Mac. Choose the device under **Install on** and the **Signing team**
next to it before the build. The build script makes only the game's framework
(`--shell-framework`), and the shared app holds the games. The launcher keeps the list of games in
-`shell.json` in the `NX2Apple-shell` folder (next to the output folder, saved as `shellRoot`),
+`shell.json` in the shell folder, `~/Library/Application Support/NX2Apple/shell` (saved as
+`shellRoot`, it holds the games' frameworks, the project, derived data and the banner cache). Older
+versions made it as `NX2Apple-shell` inside the output folder. The launcher moves such a folder
+there once at start (a rename on the same volume, shell.json's paths are rewritten), and only when
+it is the folder the launcher made, shell.json can be read and the new place is free. On any doubt
+the old folder stays where it is and keeps being used, and nothing is deleted. Uninstalling the
+launcher leaves this folder alone. The launcher
generates the app's project with `dm/ios/tools/build_shell.sh --prepare-xcode`, signs it with
`xcodebuild -allowProvisioningUpdates` and installs it over the one already there with
`xcrun devicectl device install app`, which keeps the app's Documents. The game files are packed
diff --git a/frontend/Sources/NX2Apple/NX2AppleApp.swift b/frontend/Sources/NX2Apple/NX2AppleApp.swift
index 9be142b0..44367e4d 100644
--- a/frontend/Sources/NX2Apple/NX2AppleApp.swift
+++ b/frontend/Sources/NX2Apple/NX2AppleApp.swift
@@ -10,6 +10,9 @@ enum Entry {
if CommandLine.arguments.contains("--install-shell") {
exit(ShellCLI.run(Array(CommandLine.arguments.dropFirst())))
}
+ // A shared app's folder that an older version made inside the output folder moves to Application Support once (#427).
+ // Here and not in the stores: a test that builds the stores must never move a folder in the real home.
+ ShellRootMigration.migrateSavedRoot()
NX2AppleApp.main()
}
}
diff --git a/frontend/Sources/NXCore/ShellInstall.swift b/frontend/Sources/NXCore/ShellInstall.swift
index 691d1b95..6b355542 100644
--- a/frontend/Sources/NXCore/ShellInstall.swift
+++ b/frontend/Sources/NXCore/ShellInstall.swift
@@ -69,18 +69,36 @@ package struct ShellLibrary: Codable, Equatable {
root.hasPrefix("/") && (root as NSString).pathComponents.filter { $0 != "/" }.count >= 2
}
- /// The root the next install would use, without saving it. The Settings page shows it before any game is chosen,
- /// and that look must not decide where builds save (#492).
+ /// The root the next install would use, without saving it or moving anything. The Settings page shows it before any game is
+ /// chosen, and that look must not decide where builds save (#492). Since #427 the default no longer depends on the output
+ /// folder: it is `ShellRootMigration.defaultRoot`. A root saved by an older version shows until `resolveRoot` moves it.
package static func previewRoot(defaults: UserDefaults = .standard, parent: String, home: String = NSHomeDirectory()) -> String {
if let saved = defaults.string(forKey: "shellRoot"), rootIsUsable(saved) { return saved }
- return parentFolder(parent, home: home) + "/NX2Apple-shell"
+ return ShellRootMigration.defaultRoot(home: home)
}
- /// The shell root: the saved one (`shellRoot` in `defaults`), else `<parent>/NX2Apple-shell`, which is then saved so the shell
- /// stays where it was first made, whatever output folder a later build uses. A saved root at a volume root is repaired.
+ /// The shell root: the saved one (`shellRoot` in `defaults`), else `~/Library/Application Support/NX2Apple/shell`, which is then
+ /// saved. A root that an older version made inside the output folder (`<folder>/NX2Apple-shell`) moves there once, when that is
+ /// safe (`ShellRootMigration`), and otherwise stays where it is. `parent` is where an older version would have made it. A saved
+ /// root at a volume root is repaired (#492).
package static func resolveRoot(defaults: UserDefaults = .standard, parent: String, home: String = NSHomeDirectory()) -> String {
- if let saved = defaults.string(forKey: "shellRoot"), rootIsUsable(saved) { return saved }
- let root = parentFolder(parent, home: home) + "/NX2Apple-shell"
+ if let saved = defaults.string(forKey: "shellRoot"), rootIsUsable(saved) {
+ return ShellRootMigration.isLegacy(saved, home: home) ? settle(legacy: saved, defaults: defaults, home: home) : saved
+ }
+ let legacy = parentFolder(parent, home: home) + "/" + ShellRootMigration.legacyName
+ if FileManager.default.fileExists(atPath: legacy) { return settle(legacy: legacy, defaults: defaults, home: home) }
+ let root = ShellRootMigration.defaultRoot(home: home)
+ defaults.set(root, forKey: "shellRoot")
+ return root
+ }
+
+ /// Moves an old root to the default place and saves the answer. When the old folder has to stay, it stays the saved root.
+ private static func settle(legacy: String, defaults: UserDefaults, home: String) -> String {
+ let root: String
+ switch ShellRootMigration.migrate(from: legacy, to: ShellRootMigration.defaultRoot(home: home)) {
+ case .moved, .nothingToMove: root = ShellRootMigration.defaultRoot(home: home)
+ case .left: root = legacy
+ }
defaults.set(root, forKey: "shellRoot")
return root
}
diff --git a/frontend/Sources/NXCore/ShellRootMigration.swift b/frontend/Sources/NXCore/ShellRootMigration.swift
new file mode 100644
index 00000000..5fbae753
--- /dev/null
+++ b/frontend/Sources/NXCore/ShellRootMigration.swift
@@ -0,0 +1,92 @@
+import Foundation
+
+// LNPANDY/NX2Apple#427: the shared app's folder (frameworks, shell.json, banner cache) used to sit inside the game output folder,
+// the one place a player cleans. It now lives in ~/Library/Application Support/NX2Apple/shell. An old install moves once, and
+// only when that is safe: the folder is the one the launcher made itself (`NX2Apple-shell`), it is a plain folder with a readable
+// shell.json, the new place is free and on the same volume, so the move is a rename and never a copy. On any doubt the old folder
+// stays where it is and keeps being used. Nothing is deleted.
+
+package enum ShellRootMigration {
+ /// The name of the folder older versions made inside the output folder.
+ package static let legacyName = "NX2Apple-shell"
+
+ /// `~/Library/Application Support/NX2Apple/shell`, next to the engine trees.
+ package static func defaultRoot(home: String = NSHomeDirectory()) -> String {
+ home + "/Library/Application Support/NX2Apple/shell"
+ }
+
+ /// Whether a root is one this launcher made inside an output folder, which is the only kind that moves. A root the player
+ /// chose themselves (any other name) stays.
+ package static func isLegacy(_ root: String, home: String = NSHomeDirectory()) -> Bool {
+ (root as NSString).lastPathComponent == legacyName && root != defaultRoot(home: home)
+ }
+
+ /// At launch: a saved root of the old kind moves now, so every screen shows the same folder from the start.
+ package static func migrateSavedRoot(defaults: UserDefaults = .standard, home: String = NSHomeDirectory()) {
+ guard let saved = defaults.string(forKey: "shellRoot"), isLegacy(saved, home: home) else { return }
+ _ = ShellLibrary.resolveRoot(defaults: defaults, parent: "", home: home)
+ }
+
+ package enum Outcome: Equatable {
+ /// The folder moved and shell.json now names the new place.
+ case moved
+ /// There was no folder to move (a player deleted it), so the new place is simply used.
+ case nothingToMove
+ /// The old folder stays and keeps being used. The text says why (for the log and the tests).
+ case left(String)
+ }
+
+ /// Moves `old` to `new` when that is safe, see the file's header. `new` must not exist.
+ package static func migrate(from old: String, to new: String, fm: FileManager = .default) -> Outcome {
+ guard fm.fileExists(atPath: old) || (try? fm.destinationOfSymbolicLink(atPath: old)) != nil else {
+ // Gone from a folder that is there: a player deleted it. Gone with its folder (an external drive that is not
+ // mounted): it may come back, so it stays the saved root.
+ return fm.fileExists(atPath: (old as NSString).deletingLastPathComponent) ? .nothingToMove : .left("its folder is not there")
+ }
+ // A symbolic link, a file or an alias is not the folder we made: leave it.
+ guard let type = (try? fm.attributesOfItem(atPath: old))?[.type] as? FileAttributeType, type == .typeDirectory,
+ (try? fm.destinationOfSymbolicLink(atPath: old)) == nil else { return .left("not a plain folder") }
+ guard !fm.fileExists(atPath: new), (try? fm.destinationOfSymbolicLink(atPath: new)) == nil else { return .left("the new place exists") }
+ guard let entries = try? fm.contentsOfDirectory(atPath: old) else { return .left("cannot be read") }
+ let stateFile = ShellLibrary.stateFile(root: old)
+ if entries.contains("shell.json") {
+ guard let data = fm.contents(atPath: stateFile), (try? JSONSerialization.jsonObject(with: data)) is [String: Any] else {
+ return .left("shell.json cannot be read")
+ }
+ } else if !entries.filter({ $0 != ".DS_Store" }).isEmpty {
+ return .left("no shell.json, so not a shell folder we know")
+ }
+ let parent = (new as NSString).deletingLastPathComponent
+ do { try fm.createDirectory(atPath: parent, withIntermediateDirectories: true) } catch { return .left("cannot make \(parent)") }
+ guard sameVolume(old, parent) else { return .left("another volume") }
+ do { try fm.moveItem(atPath: old, toPath: new) } catch { return .left("cannot move: \(error.localizedDescription)") }
+ guard rewritePaths(root: new, from: old, to: new, fm: fm) else {
+ // The ledger still names the old place. Put everything back rather than leave a split state.
+ if (try? fm.moveItem(atPath: new, toPath: old)) == nil { return .left("moved, but shell.json could not be rewritten") }
+ return .left("shell.json could not be rewritten")
+ }
+ return .moved
+ }
+
+ /// shell.json's framework paths that lie inside the old root now lie inside the new one. A framework in a build folder, and
+ /// every other key, stay as they are. The file is read as plain JSON so a key this version does not know is not lost.
+ package static func rewritePaths(root: String, from old: String, to new: String, fm: FileManager = .default) -> Bool {
+ let file = ShellLibrary.stateFile(root: root)
+ guard fm.fileExists(atPath: file) else { return true }
+ guard let data = fm.contents(atPath: file), var json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { return false }
+ var games = json["games"] as? [[String: Any]] ?? []
+ for i in games.indices {
+ if let path = games[i]["framework"] as? String, path.hasPrefix(old + "/") { games[i]["framework"] = new + path.dropFirst(old.count) }
+ }
+ if json["games"] != nil { json["games"] = games }
+ guard let out = try? JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]) else { return false }
+ return (try? out.write(to: URL(fileURLWithPath: file), options: .atomic)) != nil
+ }
+
+ private static func sameVolume(_ a: String, _ b: String) -> Bool {
+ let key = URLResourceKey.volumeIdentifierKey
+ guard let x = try? URL(fileURLWithPath: a).resourceValues(forKeys: [key]).volumeIdentifier,
+ let y = try? URL(fileURLWithPath: b).resourceValues(forKeys: [key]).volumeIdentifier else { return false }
+ return x.isEqual(y)
+ }
+}
diff --git a/frontend/Sources/NXCore/Uninstall.swift b/frontend/Sources/NXCore/Uninstall.swift
index 79808455..089e900f 100644
--- a/frontend/Sources/NXCore/Uninstall.swift
+++ b/frontend/Sources/NXCore/Uninstall.swift
@@ -4,7 +4,8 @@ import Foundation
/// player's games and keys left alone. What goes:
///
/// - the state folder `~/Library/Application Support/NS Port Builder/` (settings, the venv, library file)
-/// - engine trees this launcher installed (`~/Library/Application Support/NX2Apple/engine/`)
+/// - engine trees this launcher installed (`~/Library/Application Support/NX2Apple/engine/`). The shared app's folder beside
+/// it (`NX2Apple/shell`, #427) stays: its frameworks cost a full rebuild, like the games they were built from.
/// - 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)
@@ -39,7 +40,14 @@ package enum Uninstall {
let state = home + "/Library/Application Support/NS Port Builder"
if fm.fileExists(atPath: state), !holdsPlayerFiles(state) { p.stateFolders.append(state) }
let engine = home + "/Library/Application Support/NX2Apple"
- if fm.fileExists(atPath: engine), !holdsPlayerFiles(engine) { p.stateFolders.append(engine) }
+ if fm.fileExists(atPath: engine), !holdsPlayerFiles(engine) {
+ // The shared app's folder stays. With it there, only the other entries go.
+ if fm.fileExists(atPath: engine + "/shell") {
+ for name in ((try? fm.contentsOfDirectory(atPath: engine)) ?? []).sorted() where name != "shell" { p.stateFolders.append(engine + "/" + name) }
+ } else {
+ 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.
diff --git a/frontend/Tests/NX2AppleTests/ShellExportTests.swift b/frontend/Tests/NX2AppleTests/ShellExportTests.swift
index 732b959a..75153f96 100644
--- a/frontend/Tests/NX2AppleTests/ShellExportTests.swift
+++ b/frontend/Tests/NX2AppleTests/ShellExportTests.swift
@@ -125,7 +125,7 @@ import NXModel
let framework = path("work/shell-framework/DmGame\(titleID).framework")
try write(framework + "/game.manifest.json", "{}")
try write(framework + "/DmGame\(titleID)", "executable")
- return Fixture(xcrun: try makeXcrun(), repo: try makeRepo(entitlements: entitlements), framework: framework, root: path("NX2Apple-shell"))
+ return Fixture(xcrun: try makeXcrun(), repo: try makeRepo(entitlements: entitlements), framework: framework, root: path("shell-root"))
}
/// No device and no team: the installer is made the way the export needs it.
diff --git a/frontend/Tests/NX2AppleTests/ShellInstallTests.swift b/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
index 564a8447..ceb3c3dc 100644
--- a/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
+++ b/frontend/Tests/NX2AppleTests/ShellInstallTests.swift
@@ -58,10 +58,18 @@ import NXModel
XCTAssertEqual(Set(json?.keys.map { $0 } ?? []), ["bundleID", "team", "games"])
}
- func testShellRootIsDerivedOnceAndKept() throws {
+ /// #427: the default no longer depends on the output folder, and what is saved stays.
+ func testShellRootDefaultsToApplicationSupportAndIsKept() throws {
let defaults = UserDefaults(suiteName: "nspb-shell-" + UUID().uuidString)!
- XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Volumes/A"), "/Volumes/A/NX2Apple-shell")
- XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Volumes/B"), "/Volumes/A/NX2Apple-shell")
+ let home = temporary.path + "/home"
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Volumes/A", home: home),
+ home + "/Library/Application Support/NX2Apple/shell")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Volumes/B", home: home),
+ home + "/Library/Application Support/NX2Apple/shell")
+ XCTAssertEqual(defaults.string(forKey: "shellRoot"), home + "/Library/Application Support/NX2Apple/shell")
+ // a root the player chose themselves is not touched
+ defaults.set("/Volumes/Games/Cache/shell", forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Volumes/B", home: home), "/Volumes/Games/Cache/shell")
}
/// #492: the Settings page reads the path before a game is chosen; the empty parent put the shell at the root of the
@@ -69,28 +77,29 @@ import NXModel
func testAnEmptyOrRootParentNeverSavesTheShellAtTheVolumeRoot() {
let defaults = UserDefaults(suiteName: "nspb-shell-" + UUID().uuidString)!
let home = "/Users/tester"
- XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "", home: home), "/Users/tester/NX2Apple-shell")
- XCTAssertEqual(defaults.string(forKey: "shellRoot"), "/Users/tester/NX2Apple-shell")
- defaults.removeObject(forKey: "shellRoot")
- XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/", home: home), "/Users/tester/NX2Apple-shell")
- defaults.removeObject(forKey: "shellRoot")
- XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "relative/games", home: home), "/Users/tester/NX2Apple-shell")
+ let root = "/Users/tester/Library/Application Support/NX2Apple/shell"
+ for parent in ["", "/", "relative/games"] {
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: parent, home: home), root)
+ XCTAssertEqual(defaults.string(forKey: "shellRoot"), root)
+ defaults.removeObject(forKey: "shellRoot")
+ }
}
func testASavedRootAtTheVolumeRootIsRepaired() {
let defaults = UserDefaults(suiteName: "nspb-shell-" + UUID().uuidString)!
defaults.set("/NX2Apple-shell", forKey: "shellRoot")
- XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Users/tester/games"),
- "/Users/tester/games/NX2Apple-shell")
- XCTAssertEqual(defaults.string(forKey: "shellRoot"), "/Users/tester/games/NX2Apple-shell") // the bad value is replaced
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults, parent: "/Users/tester/games", home: "/Users/tester"),
+ "/Users/tester/Library/Application Support/NX2Apple/shell")
+ XCTAssertEqual(defaults.string(forKey: "shellRoot"), "/Users/tester/Library/Application Support/NX2Apple/shell") // the bad value is replaced
}
func testThePreviewDoesNotSaveARoot() {
let defaults = UserDefaults(suiteName: "nspb-shell-" + UUID().uuidString)!
- XCTAssertEqual(ShellLibrary.previewRoot(defaults: defaults, parent: "/Volumes/A"), "/Volumes/A/NX2Apple-shell")
+ XCTAssertEqual(ShellLibrary.previewRoot(defaults: defaults, parent: "/Volumes/A", home: "/Users/tester"),
+ "/Users/tester/Library/Application Support/NX2Apple/shell")
XCTAssertNil(defaults.string(forKey: "shellRoot"))
defaults.set("/Volumes/A/NX2Apple-shell", forKey: "shellRoot")
- XCTAssertEqual(ShellLibrary.previewRoot(defaults: defaults, parent: "/Volumes/B"), "/Volumes/A/NX2Apple-shell")
+ XCTAssertEqual(ShellLibrary.previewRoot(defaults: defaults, parent: "/Volumes/B"), "/Volumes/A/NX2Apple-shell") // an old root shows until it moves
}
func testRootNeedsAWritableParent() throws {
@@ -398,7 +407,7 @@ import NXModel
try write(archive + "/romfs.img", "0123456789")
try write(archive + "/aoc/1.img", "abcd")
try write(archive + "/.packstamp", "stamp")
- let made = Fixture(xcrun: try makeXcrun(), repo: try makeRepo(), framework: framework, archive: archive, root: path("NX2Apple-shell"))
+ let made = Fixture(xcrun: try makeXcrun(), repo: try makeRepo(), framework: framework, archive: archive, root: path("shell-root"))
try writeDevices(state + "/devices-default.json", [(device, "wired")])
return made
}
diff --git a/frontend/Tests/NXCoreTests/ShellRootMigrationTests.swift b/frontend/Tests/NXCoreTests/ShellRootMigrationTests.swift
new file mode 100644
index 00000000..e60d31cf
--- /dev/null
+++ b/frontend/Tests/NXCoreTests/ShellRootMigrationTests.swift
@@ -0,0 +1,149 @@
+import XCTest
+@testable import NXCore
+
+/// LNPANDY/NX2Apple#427: the shared app's folder moves out of the output folder, once, and only when that is safe. Every test
+/// runs in a temporary folder with its own `home`, so the real Application Support folder is never touched.
+final class ShellRootMigrationTests: XCTestCase {
+ private let fm = FileManager.default
+ private var base: String!
+ private var home: String { base + "/home" }
+ private var out: String { base + "/Games" }
+ private var oldRoot: String { out + "/NX2Apple-shell" }
+ private var newRoot: String { home + "/Library/Application Support/NX2Apple/shell" }
+
+ override func setUpWithError() throws {
+ base = fm.temporaryDirectory.path + "/nspb-migrate-" + UUID().uuidString
+ try fm.createDirectory(atPath: out, withIntermediateDirectories: true)
+ try fm.createDirectory(atPath: home, withIntermediateDirectories: true)
+ }
+
+ override func tearDownWithError() throws { try? fm.removeItem(atPath: base) }
+
+ private func defaults() -> UserDefaults { UserDefaults(suiteName: "nspb-migrate-" + UUID().uuidString)! }
+
+ /// An old shell folder: a mirrored framework (listed in shell.json), a framework in a build folder, and the other folders.
+ @discardableResult
+ private func makeOldShell(extra: [String: Any] = [:]) throws -> (mirror: String, build: String) {
+ let mirror = oldRoot + "/frameworks/DmGame0100AAAA.framework"
+ let build = out + "/build/DmGame0100BBBB.framework"
+ for dir in [mirror, build, oldRoot + "/derived-data", oldRoot + "/project"] {
+ try fm.createDirectory(atPath: dir, withIntermediateDirectories: true)
+ }
+ try Data("shader".utf8).write(to: URL(fileURLWithPath: mirror + "/game.manifest.json"))
+ var json: [String: Any] = ["bundleID": "org.nxnative.shell.TEAM", "team": "TEAM",
+ "games": [["titleID": "0100AAAA", "name": "A", "framework": mirror],
+ ["titleID": "0100BBBB", "name": "B", "framework": build]]]
+ for (k, v) in extra { json[k] = v }
+ try JSONSerialization.data(withJSONObject: json).write(to: URL(fileURLWithPath: oldRoot + "/shell.json"))
+ return (mirror, build)
+ }
+
+ func testAnOldFolderMovesAndShellJsonFollows() throws {
+ let (_, build) = try makeOldShell(extra: ["futureKey": 7])
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), newRoot)
+ XCTAssertEqual(d.string(forKey: "shellRoot"), newRoot)
+ XCTAssertFalse(fm.fileExists(atPath: oldRoot), "moved, not copied")
+ XCTAssertTrue(fm.fileExists(atPath: newRoot + "/frameworks/DmGame0100AAAA.framework/game.manifest.json"))
+ XCTAssertTrue(fm.fileExists(atPath: newRoot + "/derived-data"))
+ let library = ShellLibrary.load(root: newRoot)
+ XCTAssertEqual(library.games.map(\.framework), [newRoot + "/frameworks/DmGame0100AAAA.framework", build],
+ "the mirror follows, a framework in a build folder stays")
+ XCTAssertEqual(library.team, "TEAM")
+ let raw = try JSONSerialization.jsonObject(with: Data(contentsOf: URL(fileURLWithPath: newRoot + "/shell.json"))) as? [String: Any]
+ XCTAssertEqual(raw?["futureKey"] as? Int, 7, "a key this version does not know is kept")
+ }
+
+ func testTheSecondCallLeavesTheNewFolderAlone() throws {
+ try makeOldShell()
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ _ = ShellLibrary.resolveRoot(defaults: d, parent: out, home: home)
+ try Data("later".utf8).write(to: URL(fileURLWithPath: newRoot + "/frameworks/marker"))
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), newRoot)
+ XCTAssertTrue(fm.fileExists(atPath: newRoot + "/frameworks/marker"))
+ }
+
+ /// Defaults that were reset leave the old folder without a saved root: older versions derived it from the output folder.
+ func testAnOldFolderWithNoSavedRootIsFoundNextToTheOutputFolder() throws {
+ try makeOldShell()
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: defaults(), parent: out, home: home), newRoot)
+ XCTAssertTrue(fm.fileExists(atPath: newRoot + "/shell.json"))
+ XCTAssertFalse(fm.fileExists(atPath: oldRoot))
+ }
+
+ func testARootThePlayerChoseIsNotMoved() throws {
+ let chosen = out + "/MyCache"
+ try fm.createDirectory(atPath: chosen, withIntermediateDirectories: true)
+ try Data("{}".utf8).write(to: URL(fileURLWithPath: chosen + "/shell.json"))
+ let d = defaults()
+ d.set(chosen, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), chosen)
+ XCTAssertTrue(fm.fileExists(atPath: chosen + "/shell.json"))
+ XCTAssertFalse(fm.fileExists(atPath: newRoot))
+ }
+
+ func testAFolderThatIsAlreadyThereIsNeverOverwritten() throws {
+ try makeOldShell()
+ try fm.createDirectory(atPath: newRoot, withIntermediateDirectories: true)
+ try Data("mine".utf8).write(to: URL(fileURLWithPath: newRoot + "/keep"))
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), oldRoot, "the old folder keeps being used")
+ XCTAssertTrue(fm.fileExists(atPath: newRoot + "/keep"))
+ XCTAssertTrue(fm.fileExists(atPath: oldRoot + "/shell.json"))
+ XCTAssertEqual(d.string(forKey: "shellRoot"), oldRoot)
+ }
+
+ func testAShellJsonThatCannotBeReadLeavesTheFolder() throws {
+ try makeOldShell()
+ try Data("{ not json".utf8).write(to: URL(fileURLWithPath: oldRoot + "/shell.json"))
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), oldRoot)
+ XCTAssertTrue(fm.fileExists(atPath: oldRoot + "/frameworks/DmGame0100AAAA.framework"))
+ XCTAssertFalse(fm.fileExists(atPath: newRoot))
+ }
+
+ func testAFolderWithFilesButNoShellJsonIsNotOurs() throws {
+ try fm.createDirectory(atPath: oldRoot, withIntermediateDirectories: true)
+ try Data("x".utf8).write(to: URL(fileURLWithPath: oldRoot + "/holiday.mov"))
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), oldRoot)
+ XCTAssertTrue(fm.fileExists(atPath: oldRoot + "/holiday.mov"))
+ }
+
+ func testASymbolicLinkIsNotFollowed() throws {
+ let target = base + "/Elsewhere"
+ try fm.createDirectory(atPath: target, withIntermediateDirectories: true)
+ try Data("{}".utf8).write(to: URL(fileURLWithPath: target + "/shell.json"))
+ try fm.createSymbolicLink(atPath: oldRoot, withDestinationPath: target)
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), oldRoot)
+ XCTAssertTrue(fm.fileExists(atPath: target + "/shell.json"))
+ }
+
+ /// A player who deleted the folder has nothing to move. A folder whose own parent is gone (an unplugged drive) may come back.
+ func testAMissingFolderMovesOnOnlyWhenItsParentIsThere() throws {
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), newRoot)
+
+ let unplugged = base + "/Volumes/Games/NX2Apple-shell"
+ d.set(unplugged, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), unplugged)
+ XCTAssertEqual(d.string(forKey: "shellRoot"), unplugged)
+ }
+
+ func testAnEmptyOldFolderMoves() throws {
+ try fm.createDirectory(atPath: oldRoot, withIntermediateDirectories: true)
+ let d = defaults()
+ d.set(oldRoot, forKey: "shellRoot")
+ XCTAssertEqual(ShellLibrary.resolveRoot(defaults: d, parent: out, home: home), newRoot)
+ XCTAssertTrue(fm.fileExists(atPath: newRoot))
+ XCTAssertFalse(fm.fileExists(atPath: oldRoot))
+ }
+}
diff --git a/frontend/Tests/NXCoreTests/UninstallTests.swift b/frontend/Tests/NXCoreTests/UninstallTests.swift
index 9d014f04..d155745f 100644
--- a/frontend/Tests/NXCoreTests/UninstallTests.swift
+++ b/frontend/Tests/NXCoreTests/UninstallTests.swift
@@ -53,6 +53,21 @@ final class UninstallTests: XCTestCase {
XCTAssertFalse(FileManager.default.fileExists(atPath: checkout), "the launcher's own checkout goes")
}
+ /// #427: the shared app's folder lives beside the engine trees now. Its frameworks cost a full rebuild, so it stays.
+ func testTheSharedAppsFolderStaysWhenTheEngineTreesGo() throws {
+ let support = home.appendingPathComponent("Library/Application Support/NX2Apple")
+ let shell = support.appendingPathComponent("shell")
+ try FileManager.default.createDirectory(at: shell, withIntermediateDirectories: true)
+ try Data("{}".utf8).write(to: shell.appendingPathComponent("shell.json"))
+ let plan = Uninstall.plan(home: home.path, gamesFolder: games.path, checkouts: [])
+ XCTAssertFalse(plan.stateFolders.contains(support.path))
+ XCTAssertTrue(plan.stateFolders.contains(support.path + "/engine"))
+ let result = Uninstall.run(plan, movingAppToTrash: false)
+ XCTAssertTrue(result.failures.isEmpty, result.failures.joined(separator: "\n"))
+ XCTAssertTrue(FileManager.default.fileExists(atPath: shell.appendingPathComponent("shell.json").path))
+ XCTAssertFalse(FileManager.default.fileExists(atPath: support.path + "/engine"))
+ }
+
/// A checkout the player picked from somewhere else is not removed: we cannot know what else it holds.
func testACheckoutFromAnotherFolderIsNotTouched() throws {
let picked = home.appendingPathComponent("Elsewhere/NX2Apple")