Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.
Swift apple-silicond3dmetaldxmtdxvkfexgame-porting-toolkitgamingmac-gamingmacosmetalmoltenvksteamswiftuivkd3d-protonwhisky-alternativewindows-gameswinewine-wrapper
An Epic build switch deletes only the old build's files, as legendary's uninstall does, and launch never takes a build still installed for a leftover
Gabe8c267bc845dc
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swiftindex 5bb26ac..7b7ec25 100644--- a/Sources/HighballApp/AppState.swift+++ b/Sources/HighballApp/AppState.swift@@ -1342,8 +1342,7 @@ final class AppState {loadLibraryCache()rebuildLibrary()steamOwnedRefresh()- finishEpicSwitches()- epicRefresh()+ finishEpicSwitches { [self] in epicRefresh() }gogRefresh()maybeAutoUpdateEngine()if gameDB.byAppID.isEmpty {@@ -3760,15 +3759,9 @@ final class AppState {},deleteOldFiles: { [self] inguard let oldPath else { return }- store.discardSavedRecord(app)- let folder = URL(fileURLWithPath: oldPath, isDirectory: true)- guard EpicBuildSwitch.isSafeToDelete(folder, roots: roots) else {- appendLog("\(item.title): left the old \(oldPlatform) files in \(oldPath), outside Highball's game folders")- return- }- do { try await Task.detached { try FileManager.default.removeItem(at: folder) }.value } catch {- appendLog("\(item.title): could not delete the old \(oldPlatform) files in \(oldPath): \(error.localizedDescription)")- }+ stage = removing+ let result = await Task.detached { store.deleteOldBuild(app, at: oldPath, roots: roots) }.value+ appendLog(Self.oldBuildDeletionLine(item.title, platform: oldPlatform, path: oldPath, result))})do { try await EpicBuildSwitch.run(steps) } catch EpicBuildSwitch.Failure.noNewBuild {throw HighballError.failed(L("Epic lists a Mac version of this game, but has no Mac build to download for your account, so nothing was changed."))@@ -3784,28 +3777,56 @@ final class AppState {bottles.map { $0.driveC.appending(path: "Games", directoryHint: .isDirectory) } + [GOGStore.macGamesDir]}- /// Completes the undo of a build switch Highball quit in the middle of (EpicStore.switchesInFlight):- /// the old build's record goes back from where the switch kept it. Once, at launch.- private func finishEpicSwitches() {- guard !epicSwitchesFinished else { return }+ /// Completes a build switch Highball quit in the middle of (EpicStore.switchesInFlight), off+ /// the main thread, then runs `then` (the Epic refresh, so it sees a record put back): the old+ /// build's record goes back from where the switch kept it, or, when legendary records the new+ /// build in another folder, the old build's files go as legendary's uninstall deletes them+ /// (EpicStore.finishSwitch, deleteOldBuild). Once, at launch.+ private func finishEpicSwitches(then: @escaping @MainActor () -> Void) {+ guard !epicSwitchesFinished else { then(); return }epicSwitchesFinished = true- let store = epicStore- for app in store.switchesInFlight() {- let oldPath = store.savedInstallPath(app)- do {- if try store.restoreSavedRecord(app) {- appendLog("\(app): a switch between Epic builds did not finish, so the old build is back as it was")- } else if let oldPath, !((try? store.installedGames()) ?? []).contains(where: { $0.install_path == oldPath }),- EpicBuildSwitch.isSafeToDelete(URL(fileURLWithPath: oldPath, isDirectory: true), roots: epicInstallRoots),- FileManager.default.fileExists(atPath: oldPath) {- // The new build was in when Highball quit, before the old files went: they go now,- // off the main thread (a game is tens of thousands of files).- appendLog("\(app): a switch between Epic builds finished as Highball quit; deleting the old build's files in \(oldPath)")- Task.detached { try? FileManager.default.removeItem(atPath: oldPath) }+ let store = epicStore, roots = epicInstallRoots+ Task { [self] in+ let lines = await Task.detached { () -> [String] in+ var lines: [String] = []+ for app in store.switchesInFlight() {+ do {+ switch try store.finishSwitch(app) {+ case .restored:+ lines.append("\(app): a switch between Epic builds did not finish, so the old build is back as it was")+ case .deleteOldBuild(let oldPath):+ lines.append("\(app): a switch between Epic builds finished as Highball quit; deleting the old build's files in \(oldPath)")+ lines.append(Self.oldBuildDeletionLine(app, platform: nil, path: oldPath,+ store.deleteOldBuild(app, at: oldPath, roots: roots)))+ case .unreadable:+ lines.append("\(app): a switch between Epic builds did not finish, and legendary's installed.json could not be read, so nothing was changed; Highball tries again when it next opens")+ case .leftAsItWas:+ break+ }+ } catch {+ lines.append("\(app): a switch between Epic builds did not finish, and its old record could not go back yet (\(error.localizedDescription)); Highball tries again when it next opens")+ }}- } catch {- appendLog("\(app): a switch between Epic builds did not finish, and its old record could not go back yet (\(error.localizedDescription)); Highball tries again when it next opens")- }+ return lines+ }.value+ lines.forEach(appendLog)+ then()+ }+ }++ /// The log line for an old build's files after a switch (EpicStore.deleteOldBuild).+ nonisolated static func oldBuildDeletionLine(_ name: String, platform: String?, path: String,+ _ result: EpicStore.OldBuildDeletion) -> String {+ let build = platform.map { "old \($0) files" } ?? "old build's files"+ switch result {+ case .deleted(let files, 0):+ return "\(name): deleted the \(build) in \(path) (\(files) files from its manifest; anything else the game wrote there stays)"+ case .deleted(let files, let failed):+ return "\(name): deleted \(files) of the \(build) in \(path); \(failed) could not be deleted and stay there"+ case .outsideGameFolders:+ return "\(name): left the \(build) in \(path), outside Highball's game folders"+ case .noFileList(let why):+ return "\(name): left the \(build) in \(path): legendary could not list them (\(why)), so nothing there was deleted"}}diff --git a/Sources/HighballKit/EpicBuildSwitch.swift b/Sources/HighballKit/EpicBuildSwitch.swiftindex 3c3f451..15b637f 100644--- a/Sources/HighballKit/EpicBuildSwitch.swift+++ b/Sources/HighballKit/EpicBuildSwitch.swift@@ -107,7 +107,8 @@ extension EpicStore {return false}let restored = try Self.withInstalledLock(configDir) { () throws -> Bool in- var installed = (try? Self.readInstalled(configDir)) ?? [:]+ // No installed.json is legendary with no games; one that cannot be read is not written over.+ var installed = try Self.readInstalledIfThere(configDir) ?? [:]guard installed[appName] == nil else { return false }let manifests = configDir.appending(path: "manifests", directoryHint: .isDirectory)try fm.createDirectory(at: manifests, withIntermediateDirectories: true)@@ -144,6 +145,13 @@ extension EpicStore {.map(\.lastPathComponent).sorted()}+ /// installed.json, or nil when legendary has none yet. Throws when it is there and cannot be read.+ static func readInstalledIfThere(_ configDir: URL) throws -> [String: Any]? {+ let file = configDir.appending(path: "installed.json")+ guard FileManager.default.fileExists(atPath: file.path) else { return nil }+ return try readInstalled(configDir)+ }+static func readInstalled(_ configDir: URL) throws -> [String: Any] {let data = try Data(contentsOf: configDir.appending(path: "installed.json"))guard let installed = try JSONSerialization.jsonObject(with: data) as? [String: Any] else {@@ -178,3 +186,146 @@ extension EpicStore {return try body()}}++// MARK: Finishing a switch, and the old build's files++extension EpicStore {+ /// What launch found for a switch Highball quit in the middle of (switchesInFlight).+ public enum UnfinishedSwitch: Equatable, Sendable {+ /// legendary had no record of the game: the old build's record went back.+ case restored+ /// legendary records the new build in another folder: the old build's files are to go+ /// (deleteOldBuild), from this folder. The kept record stays until they have.+ case deleteOldBuild(String)+ /// legendary still records the old build where it was (Highball quit before its record+ /// was dropped), or nothing was kept: the kept copy is dropped and nothing else changes.+ case leftAsItWas+ /// installed.json could not be read: nothing was changed, and the next launch tries again.+ case unreadable+ }++ /// Completes a switch Highball quit in the middle of, from legendary's installed.json read+ /// directly (no legendary run). The old files are named for deletion only when legendary+ /// records the game in a different folder than the kept record's, so a build still installed+ /// is never taken for a leftover.+ public func finishSwitch(_ appName: String) throws -> UnfinishedSwitch {+ let installed: [String: Any]+ do { installed = try Self.readInstalledIfThere(configDir) ?? [:] } catch { return .unreadable }+ guard let current = installed[appName] as? [String: Any] else {+ return try restoreSavedRecord(appName) ? .restored : .leftAsItWas+ }+ if let old = savedInstallPath(appName), let now = current["install_path"] as? String,+ !Self.samePath(old, now) {+ return .deleteOldBuild(old)+ }+ discardSavedRecord(appName)+ return .leftAsItWas+ }++ static func samePath(_ a: String, _ b: String) -> Bool {+ func norm(_ p: String) -> String {+ URL(fileURLWithPath: p, isDirectory: true).standardizedFileURL.resolvingSymlinksInPath().path.lowercased()+ }+ return norm(a) == norm(b)+ }++ /// How deleting an old build's files went.+ public enum OldBuildDeletion: Equatable, Sendable {+ /// The files the old build's manifests list were deleted, then the folders left empty.+ /// `failed`, the listed files that were there and could not be deleted.+ case deleted(files: Int, failed: Int)+ /// The folder is not inside one of Highball's game folders: nothing was deleted.+ case outsideGameFolders+ /// legendary could not list the old build's files: nothing was deleted.+ case noFileList(String)+ }++ /// Deletes an old build's files once a switch is done, the way legendary's own uninstall does+ /// (lfs/utils.py delete_filelist): only the files the old build's manifests list, from the+ /// copies the switch kept (saveRecord), read offline by `legendary list-files --manifest`,+ /// then the folders that leaves empty, the build's folder last. Saves, settings and mods the+ /// game wrote beside its files stay. Nothing is deleted outside Highball's game folders+ /// (EpicBuildSwitch.isSafeToDelete) or when the list cannot be had. The kept record goes+ /// afterwards either way.+ public func deleteOldBuild(_ appName: String, at oldPath: String, roots: [URL]) -> OldBuildDeletion {+ defer { discardSavedRecord(appName) }+ let folder = URL(fileURLWithPath: oldPath, isDirectory: true)+ guard EpicBuildSwitch.isSafeToDelete(folder, roots: roots) else { return .outsideGameFolders }+ let files: [String]+ do { files = try oldBuildFiles(appName) } catch { return .noFileList(error.localizedDescription) }+ let (deleted, failed) = Self.deleteFileList(files, in: folder)+ return .deleted(files: deleted, failed: failed)+ }++ /// The files the kept manifests of a switch's old build list: the game's and those of its DLC+ /// sharing its folder. Throws without the game's own manifest or when legendary fails.+ func oldBuildFiles(_ appName: String) throws -> [String] {+ let folder = savedRecordFolder(appName)+ guard let data = try? Data(contentsOf: folder.appending(path: "record.json")),+ let entries = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {+ throw HighballError.failed("no kept record of \(appName)'s old build")+ }+ func manifest(_ app: String) -> URL? {+ Self.manifestNames(app, entry: entries[app] as? [String: Any] ?? [:])+ .map { folder.appending(path: "manifests/\($0)") }+ .first { FileManager.default.fileExists(atPath: $0.path) }+ }+ guard let own = manifest(appName) else { throw HighballError.failed("the old build's manifest was not kept") }+ var files: [String] = []+ for file in [own] + entries.keys.filter({ $0 != appName }).sorted().compactMap(manifest) {+ files += try Self.manifestFilenames(capture(["list-files", "--manifest", file.path, "--json"]))+ }+ return files+ }++ /// The `filename`s of `legendary list-files --json`.+ static func manifestFilenames(_ json: String) throws -> [String] {+ guard let list = try JSONSerialization.jsonObject(with: Data(json.utf8)) as? [[String: Any]] else {+ throw HighballError.failed("legendary list-files did not print a list")+ }+ return list.compactMap { $0["filename"] as? String }+ }++ /// legendary's delete_filelist with its delete_root_directory: each listed file is deleted+ /// (found without case, as legendary's case_insensitive_file_search does), then every folder+ /// the list names and the build's folder itself, deepest first, each only when empty. A name+ /// that would leave the folder is skipped. The files deleted and the ones that could not be.+ static func deleteFileList(_ filenames: [String], in folder: URL) -> (deleted: Int, failed: Int) {+ let fm = FileManager.default+ let root = folder.standardizedFileURL.resolvingSymlinksInPath()+ var deleted = 0, failed = 0+ var dirs = Set<String>()+ for name in filenames {+ let parts = name.replacingOccurrences(of: "\\", with: "/").split(separator: "/").map(String.init)+ .filter { $0 != "." }+ guard !parts.isEmpty, !parts.contains("..") else { continue }+ for i in 1..<parts.count { dirs.insert(parts[..<i].joined(separator: "/")) }+ guard let file = findIgnoringCase(parts, in: root) else { continue }+ let parent = file.deletingLastPathComponent().resolvingSymlinksInPath().path.lowercased()+ let base = root.path.lowercased()+ guard parent == base || parent.hasPrefix(base.hasSuffix("/") ? base : base + "/") else { continue }+ do { try fm.removeItem(at: file); deleted += 1 } catch { failed += 1 }+ }+ for dir in dirs.sorted(by: { $0.count > $1.count }) {+ if let url = findIgnoringCase(dir.split(separator: "/").map(String.init), in: root) { rmdir(url.path) }+ }+ rmdir(root.path)+ return (deleted, failed)+ }++ /// A path under `root` matched one name at a time, exactly first, then without case.+ private static func findIgnoringCase(_ parts: [String], in root: URL) -> URL? {+ let fm = FileManager.default+ var url = root+ for part in parts {+ let exact = url.appending(path: part)+ if (try? exact.checkResourceIsReachable()) == true || (try? fm.destinationOfSymbolicLink(atPath: exact.path)) != nil {+ url = exact; continue+ }+ guard let match = (try? fm.contentsOfDirectory(atPath: url.path))?.first(where: { $0.lowercased() == part.lowercased() })+ else { return nil }+ url = url.appending(path: match)+ }+ return url+ }+}diff --git a/Tests/HighballKitTests/EpicBuildSwitchTests.swift b/Tests/HighballKitTests/EpicBuildSwitchTests.swiftindex 9a02226..222ae29 100644--- a/Tests/HighballKitTests/EpicBuildSwitchTests.swift+++ b/Tests/HighballKitTests/EpicBuildSwitchTests.swift@@ -177,4 +177,128 @@ final class EpicBuildSwitchTests: XCTestCase {XCTAssertEqual(EpicStore.manifestNames("A", entry: ["version": "v1:2/3"]), ["A_Windows_v123.manifest", "A_v123.manifest"])XCTAssertEqual(EpicStore.manifestNames("A", entry: [:]), [])}++ // MARK: Finishing at launch, and deleting the old build's files++ /// A switch Highball quit in the middle of, as launch finds it.+ func testLaunchFinishesASwitchOnlyFromARecordItCanRead() throws {+ let (store, config) = try legendaryHome()+ // Quit after the record was kept, before legendary dropped it: the build is still installed.+ try store.saveRecord("Garlic")+ XCTAssertEqual(try store.finishSwitch("Garlic"), .leftAsItWas, "the installed build is never taken for a leftover")+ XCTAssertEqual(store.switchesInFlight(), [])+ // installed.json unreadable: nothing changes, and the marker stays for the next launch.+ try store.saveRecord("Garlic")+ let good = try Data(contentsOf: config.appending(path: "installed.json"))+ try Data("{not json".utf8).write(to: config.appending(path: "installed.json"))+ XCTAssertEqual(try store.finishSwitch("Garlic"), .unreadable)+ XCTAssertEqual(store.switchesInFlight(), ["Garlic"])+ XCTAssertThrowsError(try store.restoreSavedRecord("Garlic"), "an unreadable installed.json is not written over")+ XCTAssertEqual(try Data(contentsOf: config.appending(path: "installed.json")), Data("{not json".utf8))+ try good.write(to: config.appending(path: "installed.json"))+ // Dropped and nothing new in: the old record goes back.+ try dropRecord(config)+ XCTAssertEqual(try store.finishSwitch("Garlic"), .restored)+ XCTAssertEqual(try installed(config)["Garlic"]?["install_path"] as? String, "/E/drive_c/Games/Gungeon")+ // The new build in, in another folder: the old build's files are to go, the marker stays till then.+ try store.saveRecord("Garlic")+ var games = try installed(config)+ games["Garlic"] = ["app_name": "Garlic", "install_path": "/U/Applications/Highball Games/Garlic", "platform": "Mac", "version": "1.0-m"]+ try JSONSerialization.data(withJSONObject: games).write(to: config.appending(path: "installed.json"))+ XCTAssertEqual(try store.finishSwitch("Garlic"), .deleteOldBuild("/E/drive_c/Games/Gungeon"))+ XCTAssertEqual(store.switchesInFlight(), ["Garlic"])+ // The same folder, written differently, is the same build.+ games["Garlic"] = ["app_name": "Garlic", "install_path": "/e/DRIVE_C/Games/Gungeon/", "platform": "Windows", "version": "1.0-w"]+ try JSONSerialization.data(withJSONObject: games).write(to: config.appending(path: "installed.json"))+ XCTAssertEqual(try store.finishSwitch("Garlic"), .leftAsItWas)+ }++ /// A stand-in legendary whose `list-files --manifest <file> --json` prints the file (the test+ /// manifests hold the JSON list), or fails when `fails` is set.+ private func fakeLegendary(_ store: EpicStore, fails: Bool = false) throws {+ try FileManager.default.createDirectory(at: store.binary.deletingLastPathComponent(), withIntermediateDirectories: true)+ try """+ #!/bin/sh+ [ "$1" = list-files ] && [ "$2" = --manifest ] && [ "$4" = --json ] || exit 2+ \(fails ? "echo '[cli] ERROR: bad manifest' >&2; exit 1" : "cat \"$3\"")+ """.write(to: store.binary, atomically: true, encoding: .utf8)+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: store.binary.path)+ }++ /// An old build in a Highball game folder: its manifest's files, a DLC's, and what the game+ /// wrote there itself (a save, a mod).+ private func oldBuild(_ store: EpicStore, _ config: URL) throws -> (root: URL, folder: URL) {+ let root = store.paths.home.appending(path: "E/drive_c/Games", directoryHint: .isDirectory)+ let folder = root.appending(path: "Gungeon", directoryHint: .isDirectory)+ var games = try installed(config)+ games["Garlic"]?["install_path"] = folder.path+ games["GarlicDLC"]?["install_path"] = folder.path+ try JSONSerialization.data(withJSONObject: games).write(to: config.appending(path: "installed.json"))+ try Data(#"[{"filename": "Game.exe"}, {"filename": "Data/Pak0.pak"}, {"filename": "Data/Movies/Intro.bk2"}, {"filename": "../Duck/x"}]"#.utf8)+ .write(to: config.appending(path: "manifests/Garlic_Windows_1.0-w.manifest"))+ try Data(#"[{"filename": "DLC/Pak1.pak"}]"#.utf8).write(to: config.appending(path: "manifests/GarlicDLC_2.manifest"))+ let fm = FileManager.default+ for file in ["GAME.EXE", "Data/Pak0.pak", "Data/Movies/Intro.bk2", "DLC/Pak1.pak", "Saved/slot1.sav", "Data/mods/mod.pak"] {+ let url = folder.appending(path: file)+ try fm.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true)+ try Data("x".utf8).write(to: url)+ }+ try fm.createDirectory(at: root.appending(path: "Duck"), withIntermediateDirectories: true)+ try Data("duck".utf8).write(to: root.appending(path: "Duck/x"))+ return (root, folder)+ }++ /// highball#609: an old build's folder went whole, saves and mods with it. Now only what its+ /// manifests list goes, then the folders that leaves empty, as legendary's uninstall does.+ func testOnlyTheOldBuildsListedFilesAreDeleted() throws {+ let (store, config) = try legendaryHome()+ try fakeLegendary(store)+ let (root, folder) = try oldBuild(store, config)+ try store.saveRecord("Garlic")+ XCTAssertEqual(store.deleteOldBuild("Garlic", at: folder.path, roots: [root]), .deleted(files: 4, failed: 0))+ let fm = FileManager.default+ for gone in ["GAME.EXE", "Data/Pak0.pak", "Data/Movies", "DLC"] {+ XCTAssertFalse(fm.fileExists(atPath: folder.appending(path: gone).path), gone)+ }+ for kept in ["Saved/slot1.sav", "Data/mods/mod.pak"] {+ XCTAssertTrue(fm.fileExists(atPath: folder.appending(path: kept).path), kept)+ }+ XCTAssertTrue(fm.fileExists(atPath: root.appending(path: "Duck/x").path), "a name leaving the folder is skipped")+ XCTAssertEqual(store.switchesInFlight(), [], "the kept record goes once the files have")+ }++ func testAnEmptiedFolderGoesWithIt() throws {+ let (store, config) = try legendaryHome()+ try fakeLegendary(store)+ let (root, folder) = try oldBuild(store, config)+ let fm = FileManager.default+ try fm.removeItem(at: folder.appending(path: "Saved"))+ try fm.removeItem(at: folder.appending(path: "Data/mods"))+ try store.saveRecord("Garlic")+ XCTAssertEqual(store.deleteOldBuild("Garlic", at: folder.path, roots: [root]), .deleted(files: 4, failed: 0))+ XCTAssertFalse(fm.fileExists(atPath: folder.path))+ XCTAssertTrue(fm.fileExists(atPath: root.path))+ }++ /// No list, nothing deleted: a failed legendary, a manifest that was not kept, a folder+ /// outside Highball's game folders.+ func testWithoutAFileListNothingIsDeleted() throws {+ let (store, config) = try legendaryHome()+ try fakeLegendary(store, fails: true)+ let (root, folder) = try oldBuild(store, config)+ try store.saveRecord("Garlic")+ guard case .noFileList = store.deleteOldBuild("Garlic", at: folder.path, roots: [root]) else { return XCTFail("listed") }+ XCTAssertTrue(FileManager.default.fileExists(atPath: folder.appending(path: "GAME.EXE").path))+ XCTAssertEqual(store.switchesInFlight(), [])++ try fakeLegendary(store)+ try store.saveRecord("Garlic")+ try FileManager.default.removeItem(at: store.paths.home.appending(path: "epic-switch/Garlic/manifests/Garlic_Windows_1.0-w.manifest"))+ guard case .noFileList = store.deleteOldBuild("Garlic", at: folder.path, roots: [root]) else { return XCTFail("listed") }+ XCTAssertTrue(FileManager.default.fileExists(atPath: folder.appending(path: "GAME.EXE").path))++ try store.saveRecord("Garlic")+ XCTAssertEqual(store.deleteOldBuild("Garlic", at: folder.path, roots: [folder]), .outsideGameFolders)+ XCTAssertTrue(FileManager.default.fileExists(atPath: folder.appending(path: "GAME.EXE").path))+ }}