Skip to content

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

Cold-start Steam when the running client cannot serve a launch

Gauthier Piarrette1f5b3a0fe002
diff --git a/Sources/HighballApp/AppState.swift b/Sources/HighballApp/AppState.swift
index d5bf998..6a64625 100644
--- a/Sources/HighballApp/AppState.swift
+++ b/Sources/HighballApp/AppState.swift
@@ -742,6 +742,11 @@ final class AppState {
runBusy("Starting \(game.name)", expected: L("a cold Steam client can take a couple of minutes"), showLogSheet: false,
done: DoneState(title: L("Running"), ctaTitle: nil, cta: nil)) { [self] in
let runner = WineRunner(paths: paths, engine: engine, bottle: bottle)
+ // A running client would serve the launch with its own environment; when that is
+ // not the game's, cold-start first (never under a running game, see SteamRestart).
+ if let why = try await runner.restartSteamIfMismatched(renderer: renderer) {
+ await MainActor.run { self.appendLog("Restarting Steam before \(game.name): \(why).") }
+ }
let launch = Task {
try await runner.start(steam, arguments: ["-silent", "-applaunch", String(game.appid)] + extraArgs, renderer: renderer) { line in
Task { @MainActor in self.appendLog(line) }
diff --git a/Sources/HighballKit/SteamRestart.swift b/Sources/HighballKit/SteamRestart.swift
new file mode 100644
index 0000000..f8a5a12
--- /dev/null
+++ b/Sources/HighballKit/SteamRestart.swift
@@ -0,0 +1,65 @@
+import Foundation
+
+/// Whether a running Steam client can serve a launch, or has to be cold-started first.
+///
+/// `steam -applaunch` only forwards to a client that is already running, and the game then
+/// inherits that client's environment, not the launch's: a Steam window opened from the tile
+/// runs with sync off (its CEF workaround) and with whatever renderer the bottle had at the
+/// time, so a game launched from the library afterwards would run without msync, or under the
+/// wrong renderer, while its log header claims otherwise (UX plan 0.6, issue #54).
+public enum SteamRestart {
+ /// Pure: why the running client (environment `live`) cannot serve a launch that needs
+ /// `wanted`, in words for the log, or nil when it can. Only the renderer overlay path and
+ /// the sync mode matter; everything else in the environment is per-launch noise.
+ public static func reason(live: [String: String], wanted: [String: String], wantedRenderer: String) -> String? {
+ var reasons: [String] = []
+ if live["WINEDLLPATH_PREPEND"] != wanted["WINEDLLPATH_PREPEND"] {
+ reasons.append("it runs with a different renderer than \(wantedRenderer)")
+ }
+ let liveSync = SyncMode(environment: live), wantedSync = SyncMode(environment: wanted)
+ if liveSync != wantedSync {
+ reasons.append("it runs with sync \(liveSync.rawValue) and the game wants \(wantedSync.rawValue)")
+ }
+ return reasons.isEmpty ? nil : reasons.joined(separator: "; ")
+ }
+}
+
+extension WineRunner {
+ /// The environment of the bottle's running Steam client, nil when none runs (or the
+ /// kernel would not show it, which it does for Wine's processes).
+ public func runningSteamEnvironment() -> [String: String]? {
+ for pid in ProcessTable.processes(ofPrefix: bottle.url) {
+ guard let info = ProcessTable.commandLineAndEnvironment(of: pid),
+ let first = info.arguments.first, Self.isSteamExecutable(first) else { continue }
+ return info.environment
+ }
+ return nil
+ }
+
+ /// Whether a Steam game is running in the bottle: a prefix process working inside a
+ /// steamapps/common folder. Sessions the app started are a subset; a game launched from
+ /// the Steam window itself counts too.
+ public func steamGameIsRunning() -> Bool {
+ ProcessTable.processes(ofPrefix: bottle.url).contains { pid in
+ guard let cwd = ProcessTable.workingDirectory(of: pid) else { return false }
+ return cwd.range(of: "/steamapps/common/", options: .caseInsensitive) != nil
+ }
+ }
+
+ /// Cold-starts the bottle when its running Steam client could not serve a launch with
+ /// `renderer` (see `SteamRestart`), so the launch that follows starts a fresh client with
+ /// the right environment. Never while a game runs in the bottle: a wrong environment
+ /// beats killing someone's game. Returns the reason for the log, nil when nothing was done.
+ public func restartSteamIfMismatched(renderer: Renderer?) async throws -> String? {
+ guard let live = runningSteamEnvironment() else { return nil }
+ let wanted = try bottle.environment(engine: engine, renderer: renderer)
+ guard let why = SteamRestart.reason(live: live, wanted: wanted,
+ wantedRenderer: (renderer ?? bottle.settings.renderer).rawValue),
+ !steamGameIsRunning() else { return nil }
+ try kill()
+ // `kill` waits for the server; give the client's own exit a moment too, so the launch
+ // that follows does not forward to a client on its way out.
+ for _ in 0..<50 where runningSteamEnvironment() != nil { try await Task.sleep(for: .milliseconds(100)) }
+ return why
+ }
+}
diff --git a/Tests/HighballKitTests/SteamRestartTests.swift b/Tests/HighballKitTests/SteamRestartTests.swift
new file mode 100644
index 0000000..856452c
--- /dev/null
+++ b/Tests/HighballKitTests/SteamRestartTests.swift
@@ -0,0 +1,39 @@
+import XCTest
+@testable import HighballKit
+
+final class SteamRestartTests: XCTestCase {
+ private let dxvk = ["WINEDLLPATH_PREPEND": "/e/dxvk/wine", "WINEMSYNC": "1", "WINEESYNC": "0"]
+
+ func testSameEnvironmentNeedsNoRestart() {
+ XCTAssertNil(SteamRestart.reason(live: dxvk, wanted: dxvk, wantedRenderer: "dxvk"))
+ }
+
+ func testRendererOverlayDifferenceNamesTheRenderer() {
+ var live = dxvk; live["WINEDLLPATH_PREPEND"] = "/e/dxmt/wine"
+ let why = SteamRestart.reason(live: live, wanted: dxvk, wantedRenderer: "dxvk")
+ XCTAssertEqual(why, "it runs with a different renderer than dxvk")
+ }
+
+ func testWined3dClientVersusOverlayLaunchIsARendererDifference() {
+ var live = dxvk; live["WINEDLLPATH_PREPEND"] = nil
+ XCTAssertNotNil(SteamRestart.reason(live: live, wanted: dxvk, wantedRenderer: "dxvk"))
+ XCTAssertNil(SteamRestart.reason(live: live, wanted: live, wantedRenderer: "wined3d"))
+ }
+
+ func testSyncDifferenceNamesBothModes() {
+ var live = dxvk; live["WINEMSYNC"] = "0"
+ let why = SteamRestart.reason(live: live, wanted: dxvk, wantedRenderer: "dxvk")
+ XCTAssertEqual(why, "it runs with sync none and the game wants msync")
+ }
+
+ func testBothDifferencesAreListed() {
+ let live = ["WINEDLLPATH_PREPEND": "/e/dxmt/wine", "WINEMSYNC": "0", "WINEESYNC": "0"]
+ let why = SteamRestart.reason(live: live, wanted: dxvk, wantedRenderer: "dxvk") ?? ""
+ XCTAssertTrue(why.contains("renderer") && why.contains("sync none"), why)
+ }
+
+ func testUnrelatedVariablesAreIgnored() {
+ var live = dxvk; live["DXVK_LOG_PATH"] = "C:\\other"; live["STEAM_COMPAT"] = "1"
+ XCTAssertNil(SteamRestart.reason(live: live, wanted: dxvk, wantedRenderer: "dxvk"))
+ }
+}