Skip to content

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

A Steam install that finishes while its page is open turns the button into Play (highball#286). In NorseGaud's recording on 0.10.15, Steam finished installing Age of Empires II HD and the game's page kept offering Install until something else refreshed the library about a minute later. The install watcher only covered the environment's own steamapps, so a game installed into a second library folder (a drive added with Steam's Add Storage) was never heard, and a watch left on a steamapps that Steam recreated hears nothing either. The Steam client watch, which already polls every 8 seconds while Steam runs, only re-read the owned games, never the install records. Now the watcher covers the steamapps of every library folder, and the client watch compares a fingerprint of the install records (each appmanifest's path and date across the library folders) and re-reads the installed games when it changes, through a Steam-only refresh that does not spawn legendary. Steam replaces an appmanifest with a new file on each write (on this Mac every record's creation date equals its last change), so a watched folder does hear installs; which of the two gaps caused the reported case is not known, since the recording does not show the library chosen. New test.

Gauthier Piarrette1f6d7ae6d14e
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index aa52405..e1d49f5 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -562,6 +562,17 @@ final class AppState {
steamOwnedRefresh()
}
+ @ObservationIgnored private var steamInstallSignatures: [String: String] = [:]
+
+ /// The client watch's backstop for the install watcher: a running Steam's install records
+ /// changed, so the installed games are read again whether or not the watcher heard it
+ /// (highball#286, a finished install whose page kept offering Install).
+ func steamInstallsMayHaveChanged(_ bottle: String, signature: String) {
+ guard steamInstallSignatures[bottle] != signature else { return }
+ steamInstallSignatures[bottle] = signature
+ refreshSteamInstalls()
+ }
+
/// Hands an owned Steam game to its bottle's Steam, whose own dialog asks where to put it:
/// a running client takes the steam:// URL; otherwise Steam starts as its pin does (with
/// the sync its window needs) and receives it as an argument. Nothing is automated.
@@ -835,25 +846,34 @@ final class AppState {
/// finishing in an open Steam window), so the library follows without a relaunch.
@ObservationIgnored private var installWatcher: DirectoryWatcher?
- /// The directories whose entries change when a game is installed or removed: Steam's
- /// steamapps (appmanifest files) and the folder Epic installs land in.
+ /// The directories whose entries change when a game is installed or removed: the steamapps
+ /// of each Steam library folder (appmanifest files) and the folder Epic installs land in.
private func installDirectories(of bottle: Bottle) -> [URL] {
- var dirs = [bottle.driveC.appending(path: "Games")]
- if let root = SteamLibrary.steamRoot(of: bottle) { dirs.append(root.appending(path: "steamapps")) }
- return dirs
+ [bottle.driveC.appending(path: "Games")] + SteamLibrary.steamappsDirectories(of: bottle)
}
/// The cheap part of `refresh`: installed games only. Runs on every install-record change,
/// so it must not spawn per call beyond legendary's install list, and never touches the
/// directories it watches.
func refreshInstalledGames() {
- // Re-arm first: a steamapps that did not exist at the last full refresh (Steam was
- // installed after launch) is only watched from here on, and a replaced one is reopened.
- installWatcher?.watch(bottles.flatMap(installDirectories) + MacSteam.steamappsDirectories())
+ refreshSteamInstalls()
let mac = MacSteam.installedGames()
if mac != macSteamGames { macSteamGames = mac; rebuildLibrary() }
+ refreshEpicInstalls()
+ }
+
+ /// The Windows Steam part of `refreshInstalledGames`, without legendary, so the client watch
+ /// can run it every few seconds of a long download for the price of a few directory reads.
+ func refreshSteamInstalls() {
+ // Re-arm first: a steamapps that did not exist at the last full refresh (Steam was
+ // installed after launch, or a library folder was added) is only watched from here on,
+ // and a replaced one is reopened.
+ installWatcher?.watch(bottles.flatMap(installDirectories) + MacSteam.steamappsDirectories())
let games = Dictionary(bottles.map { ($0.name, SteamLibrary.games(in: $0)) }, uniquingKeysWith: { a, _ in a })
if games != gamesByBottle { gamesByBottle = games; rebuildLibrary() }
+ }
+
+ private func refreshEpicInstalls() {
guard epicSignedIn, !epicFetchInFlight else { return }
epicFetchInFlight = true
Task.detached { [store = epicStore] in
@@ -1951,6 +1971,9 @@ final class AppState {
// The owned library fills in as the client loads it after sign-in (highball#199).
let signature = SteamOwnedLibrary.signature(steamRoot: root)
await MainActor.run { [self] in self.steamLibraryMayHaveChanged(bottle.name, signature: signature) }
+ // An install starting or finishing where the directory watcher can't hear it (highball#286).
+ let installs = SteamLibrary.installSignature(of: bottle)
+ await MainActor.run { [self] in self.steamInstallsMayHaveChanged(bottle.name, signature: installs) }
// Steam can park a launch on a dialog nobody sees (issue #74).
if let waiting = SteamGameAction.pending(steamRoot: root) {
await MainActor.run { [self] in self.steamIsWaitingForAnAnswer(waiting, in: bottle) }
diff --git a/Sources/HighballKit/SteamLibrary.swift b/Sources/HighballKit/SteamLibrary.swift
index 5c140f0..8d1b1d4 100644
--- a/Sources/HighballKit/SteamLibrary.swift
+++ b/Sources/HighballKit/SteamLibrary.swift
@@ -106,6 +106,42 @@ public enum SteamLibrary {
return folders
}
+ /// The `steamapps` folder of each library folder, the environment's own first, symlinks
+ /// resolved: where Steam writes its install records. The library's watcher covers all of
+ /// them, since an install into a second library folder (an external drive) used to reach
+ /// the game's page only with the next full refresh (highball#286).
+ public static func steamappsDirectories(of bottle: Bottle) -> [URL] {
+ guard let root = steamRoot(of: bottle) else { return [] }
+ return steamappsDirectories(steamRoot: root, bottleURL: bottle.url)
+ }
+
+ static func steamappsDirectories(steamRoot root: URL, bottleURL: URL) -> [URL] {
+ libraryFolders(steamRoot: root, bottleURL: bottleURL).map { $0.appending(path: "steamapps").resolvingSymlinksInPath() }
+ }
+
+ /// A cheap fingerprint of the install records: each appmanifest's path and modification date
+ /// across the library folders. Steam writes a record as an install starts, advances and
+ /// finishes, so the Steam client watch compares this every few seconds and catches what the
+ /// directory watcher cannot hear, such as a library folder added while the app runs or a
+ /// steamapps Steam recreated under a watch. In highball#286 an Age of Empires II HD install
+ /// finished in Steam while its page kept offering Install.
+ public static func installSignature(of bottle: Bottle) -> String {
+ guard let root = steamRoot(of: bottle) else { return "" }
+ return installSignature(steamRoot: root, bottleURL: bottle.url)
+ }
+
+ static func installSignature(steamRoot root: URL, bottleURL: URL) -> String {
+ var records: [String] = []
+ for steamapps in steamappsDirectories(steamRoot: root, bottleURL: bottleURL) {
+ let entries = (try? FileManager.default.contentsOfDirectory(at: steamapps, includingPropertiesForKeys: [.contentModificationDateKey])) ?? []
+ for url in entries where url.lastPathComponent.hasPrefix("appmanifest_") && url.pathExtension == "acf" {
+ let date = (try? url.resourceValues(forKeys: [.contentModificationDateKey]))?.contentModificationDate
+ records.append("\(url.path) \(date?.timeIntervalSince1970 ?? 0)")
+ }
+ }
+ return records.sorted().joined(separator: "\n")
+ }
+
/// The `"path"` values of a libraryfolders.vdf, their doubled backslashes made single.
static func libraryPaths(in text: String) -> [String] {
text.matches(of: #/"path"\s+"([^"]*)"/#).map { String($0.1).replacingOccurrences(of: "\\\\", with: "\\") }
diff --git a/Tests/HighballKitTests/SteamLibraryFoldersTests.swift b/Tests/HighballKitTests/SteamLibraryFoldersTests.swift
index beb4307..fec03cf 100644
--- a/Tests/HighballKitTests/SteamLibraryFoldersTests.swift
+++ b/Tests/HighballKitTests/SteamLibraryFoldersTests.swift
@@ -90,4 +90,36 @@ final class SteamLibraryFoldersTests: XCTestCase {
XCTAssertNil(SteamLibrary.unixURL(windowsPath: "not a windows path", bottleURL: bottle))
XCTAssertEqual(SteamLibrary.libraryPaths(in: #""path" "D:\\SteamLibrary""#), ["D:\\SteamLibrary"])
}
+
+ func testTheInstallSignatureFollowsRecordsInEveryLibraryFolder() throws {
+ // highball#286: an install finished in Steam and its page kept offering Install. The
+ // watcher covered only the environment's own steamapps, and the client watch now compares
+ // this signature, so a record written in any library folder counts.
+ let bottle = tmp.appending(path: "bottle", directoryHint: .isDirectory)
+ let external = tmp.appending(path: "Disk/SteamLibrary", directoryHint: .isDirectory)
+ let steam = bottle.appending(path: "drive_c/Program Files (x86)/Steam", directoryHint: .isDirectory)
+ try write("", to: steam.appending(path: "steam.exe"))
+ try write(manifest(400, "Portal", dir: "Portal"), to: steam.appending(path: "steamapps/appmanifest_400.acf"))
+ try FileManager.default.createDirectory(at: external.appending(path: "steamapps"), withIntermediateDirectories: true)
+ let dosdevices = bottle.appending(path: "dosdevices", directoryHint: .isDirectory)
+ try FileManager.default.createDirectory(at: dosdevices, withIntermediateDirectories: true)
+ try FileManager.default.createSymbolicLink(atPath: dosdevices.appending(path: "d:").path, withDestinationPath: tmp.appending(path: "Disk").path)
+ try write("\"libraryfolders\"\n{\n\t\"1\"\n\t{\n\t\t\"path\"\t\t\"D:\\\\SteamLibrary\"\n\t}\n}\n",
+ to: steam.appending(path: "steamapps/libraryfolders.vdf"))
+
+ XCTAssertEqual(SteamLibrary.steamappsDirectories(steamRoot: steam, bottleURL: bottle).map(\.path),
+ [steam.appending(path: "steamapps").resolvingSymlinksInPath().path,
+ external.appending(path: "steamapps").resolvingSymlinksInPath().path],
+ "the watcher gets the environment's own steamapps and the second library's")
+ let before = SteamLibrary.installSignature(steamRoot: steam, bottleURL: bottle)
+ XCTAssertEqual(SteamLibrary.installSignature(steamRoot: steam, bottleURL: bottle), before, "nothing written, nothing changed")
+ let record = external.appending(path: "steamapps/appmanifest_221380.acf")
+ try write(manifest(221380, "Age of Empires II (2013)", dir: "Age2HD"), to: record)
+ try FileManager.default.setAttributes([.modificationDate: Date(timeIntervalSince1970: 1_000)], ofItemAtPath: record.path)
+ let started = SteamLibrary.installSignature(steamRoot: steam, bottleURL: bottle)
+ XCTAssertNotEqual(started, before, "a record appearing in the second library folder changes the signature")
+ try FileManager.default.setAttributes([.modificationDate: Date(timeIntervalSince1970: 2_000)], ofItemAtPath: record.path)
+ XCTAssertNotEqual(SteamLibrary.installSignature(steamRoot: steam, bottleURL: bottle), started,
+ "a rewritten record, the install finishing, changes it again")
+ }
}