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
A build switch keeps legendary's installed.json entries and manifests for the old build aside before it drops the record, and its undo puts them back from disk under legendary's lock, with no network: legendary import exited 0 over failed restores and could not bring a Mac build back. The kept copy marks a switch in flight, and one Highball quit in the middle of is undone at the next launch.
Gabe811e4538980b
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swiftindex 001cae3..900696d 100644--- a/Sources/HighballApp/AppState.swift+++ b/Sources/HighballApp/AppState.swift@@ -1342,6 +1342,7 @@ final class AppState {loadLibraryCache()rebuildLibrary()steamOwnedRefresh()+ finishEpicSwitches()epicRefresh()gogRefresh()maybeAutoUpdateEngine()@@ -3734,21 +3735,32 @@ final class AppState {return { [self] download inlet steps = EpicBuildSwitch.Steps(hasNewBuild: hasNewBuild,+ // legendary's record of the old build is kept aside first (EpicStore.saveRecord),+ // so the undo puts it back from disk: no sign-in, no network, either build.dropOldRecord: oldPath.map { _ in { [self] instage = removing+ try await Task.detached { try store.saveRecord(app) }.valuelet status = try await Task.detached { try store.forgetRecordKeepingFiles(app, onLine: log) }.value- guard status == 0 else { throw HighballError.processFailed(command: "legendary uninstall --keep-files", status: status, output: "see the log above") }+ guard status == 0 else {+ _ = try? await Task.detached { try store.restoreSavedRecord(app) }.value+ throw HighballError.processFailed(command: "legendary uninstall --keep-files", status: status, output: "see the log above")+ }} },install: download,restoreOldRecord: { [self] inguard let oldPath else { return }- let status = try await Task.detached { try store.restoreRecord(app, path: oldPath, platform: oldPlatform, onLine: log) }.value- appendLog(status == 0 ? "\(item.title): the \(oldPlatform) version is back as it was"- : "\(item.title): could not record the \(oldPlatform) version again (legendary import exited with \(status)); its files are still in \(oldPath)")+ do {+ let back = try await Task.detached { try store.restoreSavedRecord(app) }.value+ appendLog(back ? "\(item.title): the \(oldPlatform) version is back as it was"+ : "\(item.title): legendary records the game already, so its record was left as it is")+ } catch {+ appendLog("\(item.title): could not put the \(oldPlatform) version's record back yet (\(error.localizedDescription)); its files are still in \(oldPath), and Highball finishes this when it next opens")+ }epicRefresh()},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")@@ -3764,6 +3776,23 @@ final class AppState {}}+ @ObservationIgnored private var epicSwitchesFinished = false++ /// 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 }+ epicSwitchesFinished = true+ let store = epicStore+ for app in store.switchesInFlight() {+ 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") }+ } 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")+ }+ }+ }+/// Starts an Epic game's Mac build the way legendary launches it, with the sign-in arguments/// Epic's own launcher passes (single use, fetched right before). On the strip, as the GOG/// Mac build and the Windows build start: legendary takes seconds to answer, and a seconddiff --git a/Sources/HighballKit/EpicBuildSwitch.swift b/Sources/HighballKit/EpicBuildSwitch.swiftindex f9b2643..bc990da 100644--- a/Sources/HighballKit/EpicBuildSwitch.swift+++ b/Sources/HighballKit/EpicBuildSwitch.swift@@ -4,9 +4,10 @@ import Foundation////// legendary keeps one build per game and, for a game it has a record of, always installs that/// record's platform, so the old record has to go before the new build can be installed. Only-/// the record goes first: the old files stay on disk until the new build is fully in, so a Stop,-/// a failed download, or legendary falling back to the old platform puts the old record back-/// (`legendary import`) and the game is as it was. The old files are deleted last.+/// the record goes first, kept aside beforehand (EpicStore.saveRecord): the old files stay on disk+/// until the new build is fully in, so a Stop, a failed download, legendary falling back to the+/// old platform, or Highball quitting midway puts the old record back from disk and the game is+/// as it was. The old files are deleted last.public enum EpicBuildSwitch {public struct Steps {/// Epic has the new build for this account. Asked before anything changes.@@ -56,3 +57,117 @@ public enum EpicBuildSwitch {}}}++// MARK: The old record, kept aside++extension EpicStore {+ /// Where a switch keeps the old build's record while it runs, one folder per game: legendary's+ /// installed.json entries for it and the manifests they name. A folder still there when+ /// Highball opens is a switch that never finished (quit mid-download), and its undo is+ /// completed then (restoreSavedRecord).+ var switchesFolder: URL { paths.home.appending(path: "epic-switch", directoryHint: .isDirectory) }++ private func savedRecordFolder(_ appName: String) -> URL {+ switchesFolder.appending(path: Self.cleanFilename(appName), directoryHint: .isDirectory)+ }++ /// Keeps legendary's record of an installed build aside before a switch drops it: the game's+ /// entry in installed.json, its DLC's (which share its folder, and which legendary's uninstall+ /// drops with it), and the manifest files those entries name. Read from disk, no network.+ public func saveRecord(_ appName: String) throws {+ let installed = try Self.readInstalled(configDir)+ guard let game = installed[appName] as? [String: Any] else {+ throw HighballError.failed("legendary has no record of \(appName) to keep")+ }+ let path = game["install_path"] as? String+ let entries = installed.filter { key, value in+ key == appName || (path != nil && (value as? [String: Any])?["install_path"] as? String == path)+ }+ let folder = savedRecordFolder(appName), fm = FileManager.default+ try? fm.removeItem(at: folder)+ try fm.createDirectory(at: folder.appending(path: "manifests"), withIntermediateDirectories: true)+ for name in entries.compactMap({ Self.manifestNames($0.key, entry: $0.value as? [String: Any] ?? [:]) }).joined() {+ let file = configDir.appending(path: "manifests/\(name)")+ if fm.fileExists(atPath: file.path) { try fm.copyItem(at: file, to: folder.appending(path: "manifests/\(name)")) }+ }+ // Written last: a folder without it was never finished, before anything was dropped.+ try JSONSerialization.data(withJSONObject: entries, options: [.sortedKeys]).write(to: folder.appending(path: "record.json"), options: .atomic)+ }++ /// Puts a kept record back into legendary's installed.json and manifests folder, directly, with+ /// no network and no legendary run, under legendary's own lock on installed.json. Nothing is+ /// written while legendary records the game already (the new build finished, or the old+ /// record was never dropped). The kept copy is then removed. Whether the record went back.+ @discardableResult+ public func restoreSavedRecord(_ appName: String) throws -> Bool {+ let folder = savedRecordFolder(appName), fm = FileManager.default+ guard let data = try? Data(contentsOf: folder.appending(path: "record.json")),+ let entries = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any] else {+ try? fm.removeItem(at: folder)+ return false+ }+ let restored = try Self.withInstalledLock(configDir) { () throws -> Bool in+ var installed = (try? Self.readInstalled(configDir)) ?? [:]+ guard installed[appName] == nil else { return false }+ let manifests = configDir.appending(path: "manifests", directoryHint: .isDirectory)+ try fm.createDirectory(at: manifests, withIntermediateDirectories: true)+ for file in (try? fm.contentsOfDirectory(at: folder.appending(path: "manifests"), includingPropertiesForKeys: nil)) ?? [] {+ let target = manifests.appending(path: file.lastPathComponent)+ try? fm.removeItem(at: target)+ try fm.copyItem(at: file, to: target)+ }+ installed.merge(entries) { $1 }+ try JSONSerialization.data(withJSONObject: installed, options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes])+ .write(to: configDir.appending(path: "installed.json"), options: .atomic)+ return true+ }+ try? fm.removeItem(at: folder)+ return restored+ }++ /// Forgets a kept record once the new build is in.+ public func discardSavedRecord(_ appName: String) {+ try? FileManager.default.removeItem(at: savedRecordFolder(appName))+ }++ /// The games whose switch never finished: a kept record is still there.+ public func switchesInFlight() -> [String] {+ ((try? FileManager.default.contentsOfDirectory(at: switchesFolder, includingPropertiesForKeys: nil)) ?? [])+ .filter { FileManager.default.fileExists(atPath: $0.appending(path: "record.json").path) }+ .map(\.lastPathComponent).sorted()+ }++ 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 {+ throw HighballError.failed("legendary's installed.json is not a list of games")+ }+ return installed+ }++ /// The names legendary 0.21.0 gives an entry's manifest (LGDLFS._get_manifest_filename): with+ /// the platform, or the older form without it, which it falls back to.+ static func manifestNames(_ appName: String, entry: [String: Any]) -> [String] {+ guard let version = entry["version"] as? String else { return [] }+ let platform = entry["platform"] as? String ?? "Windows"+ return ["\(appName)_\(platform)_\(version)", "\(appName)_\(version)"].map { cleanFilename($0) + ".manifest" }+ }++ /// legendary's clean_filename: the characters a file name cannot hold, dropped.+ static func cleanFilename(_ name: String) -> String { name.filter { !"<>:\"/\\|?*".contains($0) } }++ /// Runs `body` holding legendary's lock on its install data (installed.json.lock, the+ /// filelock flock every legendary takes before it changes installed.json), so a legendary+ /// still running cannot write over what is put back. Throws when one holds it.+ static func withInstalledLock<T>(_ configDir: URL, _ body: () throws -> T) throws -> T {+ try FileManager.default.createDirectory(at: configDir, withIntermediateDirectories: true)+ let fd = open(configDir.appending(path: "installed.json.lock").path, O_RDWR | O_CREAT, 0o644)+ guard fd >= 0 else { throw HighballError.failed("could not open legendary's install lock") }+ defer { close(fd) }+ guard flock(fd, LOCK_EX | LOCK_NB) == 0 else {+ throw HighballError.failed("legendary is running and holds its install data")+ }+ defer { flock(fd, LOCK_UN) }+ return try body()+ }+}diff --git a/Sources/HighballKit/EpicStore.swift b/Sources/HighballKit/EpicStore.swiftindex 03a63c2..13a7c5e 100644--- a/Sources/HighballKit/EpicStore.swift+++ b/Sources/HighballKit/EpicStore.swift@@ -354,24 +354,14 @@ public struct EpicStore: Sendable {static func uninstallArguments(appName: String) -> [String] { ["uninstall", appName, "-y"] }static func keepFilesUninstallArguments(appName: String) -> [String] { ["uninstall", appName, "-y", "--keep-files", "--skip-uninstaller"] }- static func importArguments(appName: String, path: String, platform: String) -> [String] {- ["import", appName, path, "--platform", platform, "--with-dlcs", "--disable-check"]- }/// Drops legendary's record of an install and keeps its files, so a switch to the other build- /// can be undone (`restoreRecord`) until the new build is in (EpicBuildSwitch).+ /// can be undone (`restoreSavedRecord`) until the new build is in (EpicBuildSwitch).@discardableResultpublic func forgetRecordKeepingFiles(_ appName: String, onLine: (@Sendable (String) -> Void)? = nil) throws -> Int32 {try runStreaming(Self.keepFilesUninstallArguments(appName: appName), onLine: onLine)}- /// Records again an install whose files are still on disk (`legendary import`).- @discardableResult- public func restoreRecord(_ appName: String, path: String, platform: String,- onLine: (@Sendable (String) -> Void)? = nil) throws -> Int32 {- try runStreaming(Self.importArguments(appName: appName, path: path, platform: platform), onLine: onLine)- }-/// Removes a game legendary installed. Legendary owns the Epic install state, so it does the/// deleting and its library stays right; deleting the folder ourselves would leave it/// believing the game is still there (highball#185).diff --git a/Tests/HighballKitTests/EpicBuildSwitchTests.swift b/Tests/HighballKitTests/EpicBuildSwitchTests.swiftindex 2517c4e..fd2f60b 100644--- a/Tests/HighballKitTests/EpicBuildSwitchTests.swift+++ b/Tests/HighballKitTests/EpicBuildSwitchTests.swift@@ -78,4 +78,100 @@ final class EpicBuildSwitchTests: XCTestCase {XCTAssertFalse(EpicBuildSwitch.isSafeToDelete(URL(fileURLWithPath: "/E/drive_c/Games2/X"), roots: roots))XCTAssertFalse(EpicBuildSwitch.isSafeToDelete(URL(fileURLWithPath: "/U/Applications"), roots: roots))}++ // MARK: The old record, kept aside++ /// A fake legendary config: installed.json with a Windows game, its DLC in the same folder and+ /// another game, and the manifests legendary 0.21.0 names after them.+ private func legendaryHome() throws -> (EpicStore, URL) {+ let home = FileManager.default.temporaryDirectory.appending(path: "hb-epic-switch-\(UUID().uuidString)", directoryHint: .isDirectory)+ addTeardownBlock { try? FileManager.default.removeItem(at: home) }+ let config = home.appending(path: "legendary", directoryHint: .isDirectory)+ try FileManager.default.createDirectory(at: config.appending(path: "manifests"), withIntermediateDirectories: true)+ let installed = """+ {"Garlic": {"app_name": "Garlic", "install_path": "/E/drive_c/Games/Gungeon", "platform": "Windows", "version": "1.0-w", "title": "Enter the Gungeon"},+ "GarlicDLC": {"app_name": "GarlicDLC", "install_path": "/E/drive_c/Games/Gungeon", "platform": "Windows", "version": "2", "is_dlc": true},+ "Duck": {"app_name": "Duck", "install_path": "/E/drive_c/Games/Duck", "platform": "Windows", "version": "3"}}+ """+ try Data(installed.utf8).write(to: config.appending(path: "installed.json"))+ for name in ["Garlic_Windows_1.0-w", "GarlicDLC_2", "Duck_Windows_3"] {+ try Data(name.utf8).write(to: config.appending(path: "manifests/\(name).manifest"))+ }+ return (EpicStore(paths: HighballPaths(home: home)), config)+ }++ private func installed(_ config: URL) throws -> [String: [String: Any]] {+ try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: config.appending(path: "installed.json"))) as? [String: [String: Any]])+ }++ /// What `legendary uninstall --keep-files` does to the config: the game's and its DLC's entries go.+ private func dropRecord(_ config: URL, manifestsToo: Bool = false) throws {+ var games = try installed(config)+ games["Garlic"] = nil; games["GarlicDLC"] = nil+ try JSONSerialization.data(withJSONObject: games).write(to: config.appending(path: "installed.json"))+ if manifestsToo {+ for name in ["Garlic_Windows_1.0-w", "GarlicDLC_2"] { try FileManager.default.removeItem(at: config.appending(path: "manifests/\(name).manifest")) }+ }+ }++ func testAKeptRecordGoesBackWithItsDLCAndManifestsAndNoNetwork() throws {+ let (store, config) = try legendaryHome()+ try store.saveRecord("Garlic")+ XCTAssertEqual(store.switchesInFlight(), ["Garlic"], "a switch in flight leaves its marker")+ try dropRecord(config, manifestsToo: true)+ XCTAssertTrue(try store.restoreSavedRecord("Garlic"))+ let games = try installed(config)+ XCTAssertEqual(Set(games.keys), ["Garlic", "GarlicDLC", "Duck"])+ XCTAssertEqual(games["Garlic"]?["version"] as? String, "1.0-w")+ XCTAssertEqual(games["GarlicDLC"]?["is_dlc"] as? Bool, true)+ for name in ["Garlic_Windows_1.0-w", "GarlicDLC_2", "Duck_Windows_3"] {+ XCTAssertEqual(try String(contentsOf: config.appending(path: "manifests/\(name).manifest"), encoding: .utf8), name)+ }+ XCTAssertEqual(store.switchesInFlight(), [], "the marker goes once the record is back")+ }++ /// The new build finished (legendary records the game again), or the record was never dropped:+ /// nothing is written over it, and the marker goes.+ func testARecordLegendaryHasIsLeftAsItIs() throws {+ let (store, config) = try legendaryHome()+ 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"]+ games["GarlicDLC"] = nil+ try JSONSerialization.data(withJSONObject: games).write(to: config.appending(path: "installed.json"))+ XCTAssertFalse(try store.restoreSavedRecord("Garlic"))+ XCTAssertEqual(try installed(config)["Garlic"]?["platform"] as? String, "Mac")+ XCTAssertNil(try installed(config)["GarlicDLC"])+ XCTAssertEqual(store.switchesInFlight(), [])+ }++ func testNothingIsKeptForAGameLegendaryHasNoRecordOf() throws {+ let (store, _) = try legendaryHome()+ XCTAssertThrowsError(try store.saveRecord("Nope"))+ XCTAssertEqual(store.switchesInFlight(), [])+ XCTAssertFalse(try store.restoreSavedRecord("Nope"))+ }++ /// A legendary still running holds its install lock: nothing is written, the marker stays for+ /// the next launch, and the undo goes through once the lock is free.+ func testTheUndoWaitsForLegendarysLock() throws {+ let (store, config) = try legendaryHome()+ try store.saveRecord("Garlic")+ try dropRecord(config)+ let fd = open(config.appending(path: "installed.json.lock").path, O_RDWR | O_CREAT, 0o644)+ XCTAssertEqual(flock(fd, LOCK_EX | LOCK_NB), 0)+ XCTAssertThrowsError(try store.restoreSavedRecord("Garlic"))+ XCTAssertNil(try installed(config)["Garlic"])+ XCTAssertEqual(store.switchesInFlight(), ["Garlic"])+ close(fd)+ XCTAssertTrue(try store.restoreSavedRecord("Garlic"))+ XCTAssertNotNil(try installed(config)["Garlic"])+ }++ func testLegendarysManifestNames() {+ XCTAssertEqual(EpicStore.manifestNames("Garlic", entry: ["version": "1.0+5", "platform": "Mac"]),+ ["Garlic_Mac_1.0+5.manifest", "Garlic_1.0+5.manifest"])+ XCTAssertEqual(EpicStore.manifestNames("A", entry: ["version": "v1:2/3"]), ["A_Windows_v123.manifest", "A_v123.manifest"])+ XCTAssertEqual(EpicStore.manifestNames("A", entry: [:]), [])+ }}diff --git a/Tests/HighballKitTests/StoreMacVersionTests.swift b/Tests/HighballKitTests/StoreMacVersionTests.swiftindex 77be7da..25c7330 100644--- a/Tests/HighballKitTests/StoreMacVersionTests.swift+++ b/Tests/HighballKitTests/StoreMacVersionTests.swift@@ -289,8 +289,6 @@ final class EpicMacBuildTests: XCTestCase {/// A switch between builds drops only legendary's record first, so it can be put back.func testABuildSwitchKeepsTheOldFilesUntilTheNewBuildIsIn() {XCTAssertEqual(EpicStore.keepFilesUninstallArguments(appName: "A"), ["uninstall", "A", "-y", "--keep-files", "--skip-uninstaller"])- XCTAssertEqual(EpicStore.importArguments(appName: "A", path: "/x/A", platform: "Windows"),- ["import", "A", "/x/A", "--platform", "Windows", "--with-dlcs", "--disable-check"])}func testTheFallBackToWindowsIsSeen() {