Skip to content

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

Follow the launchers' install records while they run

Gauthier Piarrette474551e555b6
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index 6a64625..b11c369 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -247,6 +247,36 @@ final class AppState {
private var prunedLogsThisRun = false
+ /// Re-reads the launchers' install records when they change (a download starting or
+ /// 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.
+ 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
+ }
+
+ /// 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() {
+ let games = Dictionary(bottles.map { ($0.name, SteamLibrary.games(in: $0)) }, uniquingKeysWith: { a, _ in a })
+ if games != gamesByBottle { gamesByBottle = games; rebuildLibrary() }
+ guard epicSignedIn, !epicFetchInFlight else { return }
+ Task.detached { [store = epicStore] in
+ let installed = (try? store.installedGames()) ?? []
+ await MainActor.run { [weak self] in
+ let installs = Dictionary(uniqueKeysWithValues: installed.compactMap { g in g.install_path.map { (g.app_name, $0) } })
+ guard let self, installs != self.epicInstalls else { return }
+ self.epicInstalls = installs
+ self.rebuildLibrary()
+ }
+ }
+ }
+
func refresh() {
if !prunedLogsThisRun {
prunedLogsThisRun = true
@@ -265,6 +295,8 @@ final class AppState {
if selectedBottle == nil { selectedBottle = bottles.first?.name }
// Never trap on duplicate names (a Finder-duplicated bottle crashed the app at launch — issue #13).
gamesByBottle = Dictionary(bottles.map { ($0.name, SteamLibrary.games(in: $0)) }, uniquingKeysWith: { a, _ in a })
+ if installWatcher == nil { installWatcher = DirectoryWatcher { [weak self] in self?.refreshInstalledGames() } }
+ installWatcher?.watch(bottles.flatMap(installDirectories))
libraryPlays = libraryStore.load()
rebuildLibrary()
epicRefresh()
diff --git a/Sources/HighballKit/DirectoryWatcher.swift b/Sources/HighballKit/DirectoryWatcher.swift
new file mode 100644
index 0000000..86d0b1a
--- /dev/null
+++ b/Sources/HighballKit/DirectoryWatcher.swift
@@ -0,0 +1,51 @@
+import Foundation
+
+/// Watches directories for entries appearing, disappearing or being renamed and reports once
+/// per burst, on the main queue, after a quiet period.
+///
+/// The launchers write their install records as files (Steam's `appmanifest_<appid>.acf`,
+/// Epic's `Manifests/*.item`), so a download that starts or finishes while a launcher window
+/// is open shows up in the library without relaunching the app (UX plan 0.6).
+public final class DirectoryWatcher {
+ private var sources: [String: DispatchSourceFileSystemObject] = [:]
+ private var pending: DispatchWorkItem?
+ private let quiet: TimeInterval
+ private let onChange: () -> Void
+
+ public init(quiet: TimeInterval = 2, onChange: @escaping () -> Void) {
+ self.quiet = quiet
+ self.onChange = onChange
+ }
+
+ deinit { for source in sources.values { source.cancel() } }
+
+ /// The directories currently watched (the ones that existed at the last `watch`).
+ public var watched: Set<String> { Set(sources.keys) }
+
+ /// Replaces the watched set. A directory that does not exist yet is skipped and picked
+ /// up by a later call; one already watched keeps its source, so calling this on every
+ /// refresh is cheap and does not lose events.
+ public func watch(_ directories: [URL]) {
+ let wanted = Set(directories.map(\.path))
+ for (path, source) in sources where !wanted.contains(path) {
+ source.cancel()
+ sources[path] = nil
+ }
+ for path in wanted where sources[path] == nil {
+ let fd = open(path, O_EVTONLY)
+ guard fd >= 0 else { continue }
+ let source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: fd, eventMask: [.write, .rename, .delete], queue: .main)
+ source.setEventHandler { [weak self] in self?.schedule() }
+ source.setCancelHandler { close(fd) }
+ source.resume()
+ sources[path] = source
+ }
+ }
+
+ private func schedule() {
+ pending?.cancel()
+ let item = DispatchWorkItem { [weak self] in self?.onChange() }
+ pending = item
+ DispatchQueue.main.asyncAfter(deadline: .now() + quiet, execute: item)
+ }
+}
diff --git a/Tests/HighballKitTests/DirectoryWatcherTests.swift b/Tests/HighballKitTests/DirectoryWatcherTests.swift
new file mode 100644
index 0000000..ec6fed8
--- /dev/null
+++ b/Tests/HighballKitTests/DirectoryWatcherTests.swift
@@ -0,0 +1,31 @@
+import XCTest
+@testable import HighballKit
+
+final class DirectoryWatcherTests: XCTestCase {
+ func testNewEntryReportsOnceAfterTheQuietPeriod() throws {
+ let dir = FileManager.default.temporaryDirectory.appending(path: "watch-\(UUID().uuidString)")
+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let changed = expectation(description: "change reported")
+ changed.assertForOverFulfill = true
+ let watcher = DirectoryWatcher(quiet: 0.3) { changed.fulfill() }
+ watcher.watch([dir])
+ XCTAssertEqual(watcher.watched, [dir.path])
+ // Two writes inside one quiet period: one report.
+ FileManager.default.createFile(atPath: dir.appending(path: "appmanifest_1.acf").path, contents: Data("a".utf8))
+ FileManager.default.createFile(atPath: dir.appending(path: "appmanifest_2.acf").path, contents: Data("b".utf8))
+ wait(for: [changed], timeout: 5)
+ withExtendedLifetime(watcher) {}
+ }
+
+ func testMissingDirectoryIsSkippedAndDroppedDirectoriesAreReleased() throws {
+ let dir = FileManager.default.temporaryDirectory.appending(path: "watch-\(UUID().uuidString)")
+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let watcher = DirectoryWatcher(quiet: 0.1) {}
+ watcher.watch([dir, dir.appending(path: "missing")])
+ XCTAssertEqual(watcher.watched, [dir.path])
+ watcher.watch([])
+ XCTAssertTrue(watcher.watched.isEmpty)
+ }
+}