Skip to content

Run Windows games on Apple Silicon — free, open source, with an open compatibility database that tells you what actually works.

Merge main into feat/game-sizes

Gabe23c7ab5cba73
diff --cc Sources/HighballApp/AppState.swift
index f05e152,b1537c7..eb4bbcf
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@@ -1278,7 -1277,10 +1281,8 @@@ final class AppState
await MainActor.run { [weak self] in
guard let self else { return }
self.epicFetchInFlight = false
+ defer { self.offerKeptEpicSaves() }
- let installs = EpicStore.installMap(installed)
- guard installs != self.epicInstalls else { return }
- self.epicInstalls = installs
+ guard self.applyEpicInstalls(installed) else { return }
self.forgetProgramExecutables()
self.rebuildLibrary()
}
@@@ -3507,18 -3517,15 +3520,19 @@@
epicFetchInFlight = true
epicLoading = epicOwned.isEmpty
Task.detached { [store = epicStore] in
- let owned = (try? store.ownedGames()) ?? []
- let installed = (try? store.installedGames()) ?? []
+ // nil when legendary could not answer (offline, Epic down): the list shown, the one
+ // saved last time, stays, rather than the library emptying.
+ let owned = try? store.ownedGames()
+ let installed = try? store.installedGames()
await MainActor.run { [weak self] in
- self?.epicOwned = owned.sorted { $0.app_title < $1.app_title }
- self?.epicInstalls = EpicStore.installMap(installed)
- self?.epicLoading = false
- self?.epicFetchInFlight = false
- self?.rebuildLibrary() // Epic results arrive after refresh(); fold them in
- self?.offerKeptEpicSaves()
+ guard let self else { return }
+ if let owned { self.epicOwned = owned.sorted { $0.app_title < $1.app_title } }
+ if let installed { _ = self.applyEpicInstalls(installed) }
+ self.epicLoading = false
+ self.epicFetchInFlight = false
+ self.rebuildLibrary() // Epic results arrive after refresh(); fold them in
+ if owned != nil { self.saveLibraryCache() }
++ self.offerKeptEpicSaves()
}
}
}
@@@ -3595,18 -3616,168 +3623,180 @@@
}
}
+ /// legendary's install records by app name (installed size, save folder), for the game page.
+ @ObservationIgnored var epicInstallRecords: [String: EpicStore.InstalledGame] = [:]
+
+ /// Keeps legendary's records and the install map read from them. Whether the library changed.
+ private func applyEpicInstalls(_ installed: [EpicStore.InstalledGame]) -> Bool {
+ let installs = EpicStore.installMap(installed)
+ epicInstallRecords = Dictionary(installed.map { ($0.app_name, $0) }, uniquingKeysWith: { a, _ in a })
+ guard installs != epicInstalls else { return false }
+ epicInstalls = installs
+ return true
+ }
+
+ /// An Epic game the player is asked about before it is removed: nothing tells the files the
+ /// game wrote in its install folder from legendary's (no manifest, and installed before
+ /// Highball kept a history), so Keep can't copy them. Nothing is removed until they agree.
+ struct EpicRemovalWithoutCopy {
+ let app: String
+ let title: String
+ let bottle: Bottle
+ }
+ var epicRemovalWithoutCopy: EpicRemovalWithoutCopy?
+
+ /// The player agreed to remove the Epic game without a copy of its install folder.
+ func epicRemoveWithoutCopy() {
+ guard let pending = epicRemovalWithoutCopy else { return }
+ epicRemovalWithoutCopy = nil
+ appendLog("\(pending.title): removing it without a copy of what it wrote in its folder, as asked")
+ epicUninstall(pending.app, title: pending.title, in: pending.bottle, deleteSaves: false, agreed: true)
+ }
+
+ /// Removes an Epic game through legendary, so its library stays right. Keep save data (the
+ /// default) first copies, as kept backups, the game's Epic save folder and every file the game
+ /// wrote in its install folder (EpicSaves.gameFiles), and stops if a copy fails; a later
+ /// install is offered them back. When nothing tells the game's files apart, the player is
+ /// asked first (epicRemovalWithoutCopy); `agreed` is their go-ahead. `deleteSaves` copies
+ /// nothing and also deletes the save folder when it lies inside the environment
+ /// (EpicSaves.removeSaves). What follows the uninstall waits for legendary's record of the
+ /// game to be gone, not the folder: legendary leaves the folder when the game wrote files in it.
+ private func epicUninstall(_ app: String, title: String, in bottle: Bottle, deleteSaves: Bool, agreed: Bool = false) {
+ let store = epicStore
+ let install = epicInstalls[app].map { URL(fileURLWithPath: $0, isDirectory: true) }
+ // Resolving the save folder runs legendary's sync-saves (moving nothing), which a player
+ // who turned Sync saves with Epic off didn't ask for.
+ let resolve = !epicCloudSavesOff.contains(app)
+ runBusy(String(format: L("Removing %@"), title),
+ done: DoneState(title: String(format: L("%@ removed"), title), ctaTitle: nil, cta: nil),
+ stop: .cancelTask(label: L("Stop"))) { [self] in
+ let saves = EpicSaves(store: store), prefix = bottle.url
+ let log: @Sendable (String) -> Void = { line in Task { @MainActor in self.appendLog(line) } }
+ var saveFolder: URL?
+ var kept: [KeptSaves.Entry] = []
+ if let install, EpicStore.isInstalled(path: install.path, inDriveC: bottle.driveC) {
+ stage = L("Finding the game's saves")
+ saveFolder = await Task.detached { saves.uninstallSaveFolder(app, install: install, prefix: prefix, resolve: resolve, onLine: log) }.value
+ try checkStopped()
+ if !deleteSaves {
+ let gameFiles = await Task.detached { saves.gameFiles(app, install: install, prefix: prefix) }.value
+ try checkStopped()
+ if gameFiles == nil, !agreed {
+ epicRemovalWithoutCopy = EpicRemovalWithoutCopy(app: app, title: title, bottle: bottle)
+ busyDoneOverride = .handedOff // the question takes over; nothing was removed
+ return
+ }
+ // These would go with the game, so they are copied before anything runs, and a
+ // failed copy stops the removal. Never pruned.
+ stage = L("Keeping a copy of the game's saves")
+ kept = try await Task.detached {
+ try saves.backUpForUninstall(app, install: install, prefix: prefix, saveFolder: saveFolder, gameFiles: gameFiles)
+ }.value
+ try checkStopped() // a detached copy runs on through a Stop: nothing is removed after one
+ for entry in kept {
+ let (files, bytes) = SaveBackups.contents(of: entry.backup)
+ let what = entry.name == EpicSaves.gameFolderName ? "the game wrote in its folder" : "in its save folder"
+ appendLog("\(title): \(files) file\(files == 1 ? "" : "s") \(what) (\(ByteCountFormatter.string(fromByteCount: bytes, countStyle: .file))) copied to \(entry.backup.path)")
+ }
+ }
+ }
+ try checkStopped()
+ appendLog("\(title): asking the Epic tools to uninstall it")
+ _ = try await Task.detached { try store.uninstall(app, onLine: log) }.value
+ // legendary's records unreadable: the folder vanishing is all there is to go by.
+ let uninstalled = store.isRecorded(app).map { !$0 } ?? install.map { !FileManager.default.fileExists(atPath: $0.path) } ?? false
+ if install != nil, !uninstalled {
+ appendLog("\(title): the Epic tools still list it as installed, so no saves were kept or deleted")
+ }
+ if let install, uninstalled {
+ EpicSaves.forgetHistory(app: app, driveC: bottle.driveC)
+ if deleteSaves, let saveFolder {
+ if saves.removeSaves(app, folder: saveFolder, install: install, prefix: prefix) {
+ appendLog("\(title): saves at \(saveFolder.path) deleted, as asked")
+ } else if FileManager.default.fileExists(atPath: saveFolder.path) {
+ appendLog("\(title): saves at \(saveFolder.path) left in place: they are outside this environment, or hold other things Highball keeps")
+ }
+ } else if deleteSaves {
+ appendLog("\(title): Highball found no save folder for it to delete, so no saves outside the game's folder were deleted")
+ }
+ // Recorded once legendary's record is gone, so this install is never offered its own saves.
+ saves.keep(kept, of: app)
+ // What legendary didn't install keeps its folder, which a reinstall there can't use.
+ if FileManager.default.fileExists(atPath: install.path) {
+ // legendary's own records, read locally and off the main actor; when they can't
+ // be read, nothing tells whether another game is inside, so the folder stays.
+ let prefix = bottle.url
+ let removed = await Task.detached { () -> Bool? in
+ guard let records = store.installRecords() else { return nil }
+ let others = records.values.filter { !$0.isEmpty }.map { URL(fileURLWithPath: $0) }
+ return saves.removeLeftover(install, prefix: prefix, stillInstalled: others)
+ }.value
+ switch removed {
+ case true?: appendLog("\(title): removed the folder the Epic tools left at \(install.path)")
+ case false?: appendLog("\(title): the Epic tools left \(install.path), with files they didn't install; Highball leaves it, as it holds more than this game")
+ case nil: appendLog("\(title): the Epic tools left \(install.path); Highball couldn't read their install records, so it leaves the folder")
+ }
+ }
+ }
+ await MainActor.run { self.refresh() }
+ }
+ }
+
+ /// Saves an uninstall kept, offered to a new install of the game (EpicSaves.keep).
+ struct EpicSavesToPutBack {
+ let app: String
+ let title: String
+ let install: URL
+ let bottle: Bottle
+ let kept: KeptSaves
+ }
+ var epicSavesToPutBack: EpicSavesToPutBack?
+
+ /// Installs whose put-back question was answered Not now, as GOG's (gogPutBackLater).
+ private var epicPutBackLater: Set<String> = []
+
+ /// Offers, once per install, to put back saves an uninstall kept of the same game. Only for
+ /// an install Highball made (EpicSaves.history), and while the game isn't running.
+ private func offerKeptEpicSaves() {
+ guard epicSavesToPutBack == nil else { return }
+ let all = EpicSaves(store: epicStore).keptSaves() // one read, not one per installed game
+ guard !all.isEmpty else { return }
+ for (app, path) in epicInstalls.sorted(by: { $0.key < $1.key }) {
+ let install = URL(fileURLWithPath: path, isDirectory: true)
+ guard let bottle = bottles.first(where: { EpicStore.isInstalled(path: path, inDriveC: $0.driveC) }),
+ let kept = KeptSavesFile.toOffer(all[app], install: install),
+ !epicPutBackLater.contains(install.standardizedFileURL.path),
+ EpicSaves.history(app: app, driveC: bottle.driveC).firstSetup != nil,
+ !runningSessions.contains(where: { $0.libraryID == "epic:\(app)" }) else { continue }
+ let title = epicOwned.first { $0.app_name == app }?.app_title ?? app
+ epicSavesToPutBack = EpicSavesToPutBack(app: app, title: title, install: install, bottle: bottle, kept: kept)
+ return
+ }
+ }
+
+ /// The answer to the put-back question, as GOG's: Not now asks again the next time Highball
+ /// starts, the other two answer that install for good.
+ func epicPutBackSaves(_ answer: PutBackAnswer) {
+ guard let pending = epicSavesToPutBack else { return }
+ epicSavesToPutBack = nil
+ let saves = EpicSaves(store: epicStore)
+ if answer == .notNow {
+ epicPutBackLater.insert(pending.install.standardizedFileURL.path)
+ } else {
+ saves.markAsked(pending.app, install: pending.install)
+ }
+ guard answer == .putBack else { offerKeptEpicSaves(); return }
+ Task {
+ do {
+ let count = try await Task.detached { try saves.putBack(pending.kept, app: pending.app, install: pending.install, prefix: pending.bottle.url) }.value
+ appendLog("\(pending.title): \(count) saved file\(count == 1 ? "" : "s") put back from \(pending.kept.date.formatted()), what was there copied to save-backups first")
+ } catch {
+ fail(HighballError.failed(String(format: L("Highball could not put back all of %@'s saves: %@"), pending.title, error.localizedDescription)))
+ }
+ offerKeptEpicSaves()
+ }
+ }
+
/// Quitting the app stops an Epic download the way Stop does, so it never runs on alone,
/// holding legendary's install lock with nothing on screen to stop it (highball#323).
func stopEpicInstall() { epicInstallControl?.stop() }
@@@ -4251,149 -4438,24 +4552,158 @@@
AchievementToast.shared.show(sample)
}
+ // MARK: Sizes
+
+ /// What the store said a download takes, by item id and platform, kept on disk for a week
+ /// (download-sizes.json): asking gogdl or legendary takes seconds, and a page opened again
+ /// shows the size at once.
+ private var downloadEstimates: [String: DownloadEstimate] = [:]
+ private var downloadEstimatesFetched: [String: Date] = [:]
+ private var downloadEstimatesLoaded = false
+ private var downloadSizesFile: URL { paths.home.appending(path: "download-sizes.json") }
+ private struct SavedEstimate: Codable { var download: Int64?; var disk: Int64?; var fetched: Date }
+
+ private func loadDownloadEstimates() {
+ guard !downloadEstimatesLoaded else { return }
+ downloadEstimatesLoaded = true
+ let decoder = JSONDecoder(); decoder.dateDecodingStrategy = .iso8601
+ let saved = (try? decoder.decode([String: SavedEstimate].self, from: Data(contentsOf: downloadSizesFile))) ?? [:]
+ let fresh = Date().addingTimeInterval(-7 * 86_400)
+ for (key, value) in saved where value.fetched > fresh {
+ downloadEstimates[key] = DownloadEstimate(download: value.download, disk: value.disk)
+ downloadEstimatesFetched[key] = value.fetched
+ }
+ }
+
+ private func saveDownloadEstimates() {
+ let saved = Dictionary(uniqueKeysWithValues: downloadEstimates.map { key, value in
+ (key, SavedEstimate(download: value.download, disk: value.disk, fetched: downloadEstimatesFetched[key] ?? Date()))
+ })
+ let encoder = JSONEncoder(); encoder.dateEncodingStrategy = .iso8601; encoder.outputFormatting = [.sortedKeys]
+ try? encoder.encode(saved).write(to: downloadSizesFile, options: .atomic)
+ }
+
+ /// The download and installed size of a game's Windows or Mac build before it is installed:
+ /// GOG's and Epic's tools ask the store, Steam's comes from its client's data on the Mac.
+ /// nil when there is no saying (signed out, offline, no such build, Steam not set up).
+ func downloadEstimate(for item: LibraryItem, mac: Bool, refresh: Bool = false) async -> DownloadEstimate? {
+ let key = Self.estimateKey(item, mac: mac)
+ loadDownloadEstimates()
+ if !refresh, let cached = downloadEstimates[key] { return cached }
+ let estimate: DownloadEstimate?
+ switch item.source {
+ case .gog:
+ guard let id = item.gogID else { return nil }
+ let store = gogStore
+ estimate = await Task.detached {
+ (try? store.info(id, platform: mac ? "osx" : "windows")).map { DownloadEstimate(download: $0.downloadSize, disk: $0.diskSize) }
+ }.value
+ case .epic:
+ guard let app = item.epicAppName else { return nil }
+ let store = epicStore
+ estimate = await Task.detached { try? store.downloadEstimate(app, platform: mac ? "Mac" : "Windows") }.value
+ case .steam:
+ // Steam's own data in the environment's client: no request, no Steam window.
+ guard let appID = item.steamAppID,
+ let root = (bottles.first { $0.name == item.bottleName } ?? defaultBottle).flatMap(SteamLibrary.steamRoot(of:)) else { return nil }
+ estimate = await Task.detached { () -> DownloadEstimate? in
+ guard let app = (try? SteamAppInfo.read(root.appending(path: "appcache/appinfo.vdf"), only: [appID]))?[appID] else { return nil }
+ return SteamDownloadSize.estimate(appID: appID, app: app, packages: SteamPackageInfo.read(steamRoot: root), mac: mac)
+ }.value
+ default:
+ return nil
+ }
+ // An answer with neither size (GOG's info without them) says nothing: never kept as the size.
+ let answer = estimate.flatMap { $0.download != nil || $0.disk != nil ? $0 : nil }
+ if let answer { downloadEstimates[key] = answer; downloadEstimatesFetched[key] = Date(); saveDownloadEstimates() }
+ return answer ?? (refresh ? downloadEstimates[key] : nil)
+ }
+
+ private static func estimateKey(_ item: LibraryItem, mac: Bool) -> String { "\(item.id)|\(mac ? "mac" : "windows")" }
+
+ /// The size saved from an earlier check, at once, with no store asked.
+ func savedEstimate(for item: LibraryItem, mac: Bool) -> DownloadEstimate? {
+ loadDownloadEstimates()
+ return downloadEstimates[Self.estimateKey(item, mac: mac)]
+ }
+
+ /// A saved size older than a day: worth a quiet second look when its page opens (an update
+ /// can change it).
+ func estimateIsStale(for item: LibraryItem, mac: Bool) -> Bool {
+ (downloadEstimatesFetched[Self.estimateKey(item, mac: mac)] ?? .distantPast) < Date().addingTimeInterval(-86_400)
+ }
+
+ /// A native Mac version the store offers: Steam's (MacSteamBuild).
+ func offersMacVersion(_ item: LibraryItem) -> Bool {
+ item.installedOnMac || (item.source == .steam && macSteamBuild(for: item) != nil)
+ }
+
+ /// An installed game's folder and saves as they are on disk now.
+ struct InstalledSizes: Equatable {
+ /// The Windows build's folder (or a program's); nil for Steam, whose own record the page shows.
+ var game: Int64?
+ /// The Mac build's size: Steam for Mac's record.
+ var macGame: Int64?
+ /// The saves Highball knows the place of: GOG's and Epic's cloud-save folders, Steam
+ /// Cloud's own copy for Steam. nil when there is no such place or nothing in it yet.
+ var saves: Int64?
+ }
+
+ func installedSizes(for item: LibraryItem) async -> InstalledSizes {
+ let bottle = bottles.first { $0.name == item.bottleName }
+ switch item.source {
+ case .steam:
+ // The Windows build's size is Steam's own record (LibraryItem.sizeOnDisk); Steam for
+ // Mac's install has its own record too.
+ let mac = macSteamGames.first { $0.appid == item.steamAppID }.map(\.sizeOnDisk).flatMap { $0 > 0 ? $0 : nil }
+ guard let appID = item.steamAppID, let root = bottle.flatMap(SteamLibrary.steamRoot(of:)) else { return InstalledSizes(macGame: mac) }
+ return await Task.detached {
+ InstalledSizes(macGame: mac, saves: DiskUsage.bytes(at: SteamLibrary.cloudSaveFolders(steamRoot: root, appID: appID)))
+ }.value
+ case .gog:
+ let windows = bottle.flatMap { gogInstalled(item, in: $0) }
+ let saves = GOGSaves(store: gogStore)
+ return await Task.detached {
+ var sizes = InstalledSizes(game: windows.flatMap { DiskUsage.bytes(at: $0.folder) })
+ if let windows, let bottle, let client = saves.clientID(for: windows),
+ let locations = (try? saves.locations(clientID: client)) ?? nil {
+ sizes.saves = DiskUsage.bytes(at: locations.compactMap { GOGSaves.resolve($0.template, game: windows, driveC: bottle.driveC) })
+ }
+ return sizes
+ }.value
+ case .epic:
+ guard let app = item.epicAppName else { return InstalledSizes() }
+ let known = epicInstallRecords[app], store = epicStore
+ return await Task.detached {
+ guard let record = known ?? (try? store.installedGames())?.first(where: { $0.app_name == app }) else { return InstalledSizes() }
+ let size = record.install_size ?? record.install_path.flatMap { DiskUsage.bytes(at: URL(fileURLWithPath: $0)) }
+ return InstalledSizes(game: size, saves: record.save_path.flatMap { DiskUsage.bytes(at: URL(fileURLWithPath: $0)) })
+ }.value
+ default:
+ // A program added by hand, a Battle.net game: the folder it lives in.
+ let folder = programFolder(for: item)
+ return await Task.detached { InstalledSizes(game: folder.flatMap { DiskUsage.bytes(at: $0) }) }.value
+ }
+ }
+
- /// The game's cloud-save folders in this environment, empty when it has none or GOG cannot say.
- private func gogSaveLocations(_ game: GOGStore.InstalledGame, in bottle: Bottle) async -> [(GOGSaves.Location, URL)] {
+ /// The game's cloud-save locations. Returns an empty array for a game without cloud saves
+ /// (no client id, or no configuration at GOG), and nil when GOG couldn't be reached. Both
+ /// lookups can need GOG: an offline install's client id comes from its build manifest.
+ private func gogCloudLocations(_ game: GOGStore.InstalledGame) async -> [GOGSaves.Location]? {
let saves = GOGSaves(store: gogStore)
- // Both can reach GOG (an offline install's client id comes from its build manifest).
- let locations = await Task.detached { () -> [GOGSaves.Location]? in
- guard let client = saves.clientID(for: game) else { return nil }
- return (try? saves.locations(clientID: client)) ?? nil
+ return await Task.detached { () -> [GOGSaves.Location]? in
+ do {
+ guard let client = try saves.lookUpClientID(for: game) else { return [] }
+ return try saves.locations(clientID: client) ?? []
+ } catch { return nil }
}.value
- return (locations ?? []).compactMap { location in
+ }
+
+ /// The game's cloud-save folders in this environment: empty for a game without cloud saves,
+ /// nil when GOG couldn't be reached.
+ private func gogSaveLocations(_ game: GOGStore.InstalledGame, in bottle: Bottle) async -> [(GOGSaves.Location, URL)]? {
+ guard let locations = await gogCloudLocations(game) else { return nil }
+ return locations.compactMap { location in
guard let folder = GOGSaves.resolve(location.template, game: game, driveC: bottle.driveC) else {
appendLog("\(game.title): cloud saves at \(location.template) have no folder here, skipping that one")
return nil
diff --cc Sources/HighballKit/EpicStore.swift
index d78b43e,7527f28..5043809
--- a/Sources/HighballKit/EpicStore.swift
+++ b/Sources/HighballKit/EpicStore.swift
@@@ -159,9 -154,7 +160,9 @@@ public struct EpicStore: Sendable
public struct InstalledGame: Codable, Sendable {
public let app_name: String
public let install_path: String?
+ /// legendary's record of the installed size, in bytes.
+ public var install_size: Int64? = nil
- /// Where the game keeps the saves legendary syncs, once it has synced them.
+ /// Where the game keeps the saves legendary syncs, once it has resolved it (EpicSaves).
public var save_path: String? = nil
}