Skip to content

Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit

fix(setup): quit Steam before making WineCX its default

Raystorme03fd56b2704
diff --git a/app/Sources/BridgeplayApp/Model/SteamDefaultTool.swift b/app/Sources/BridgeplayApp/Model/SteamDefaultTool.swift
index 84d8b61..f267a32 100644
--- a/app/Sources/BridgeplayApp/Model/SteamDefaultTool.swift
+++ b/app/Sources/BridgeplayApp/Model/SteamDefaultTool.swift
@@ -38,12 +38,7 @@ enum SteamDefaultTool {
steamRunning: () -> Bool = { SteamBundle.isRunningForThisUser }
) throws {
guard !steamRunning() else { throw SteamRunning() }
- guard let text = try? String(contentsOf: file, encoding: .utf8) else {
- throw StepFailure(step: step, detail: "Steam has no config.vdf yet. Open Steam once, log in, quit it and run this again.")
- }
- guard let result = updated(text, tool: tool) else {
- throw StepFailure(step: step, detail: "config.vdf has no InstallConfigStore > Software > Valve > Steam section.")
- }
+ let (text, result) = try planned(tool, in: file)
guard result != text else { return }
// A default that is already one of ours is not the user's, so the backup taken when
// Bridgeplay first replaced theirs is kept.
@@ -55,6 +50,24 @@ enum SteamDefaultTool {
try result.write(to: file, atomically: true, encoding: .utf8)
}
+ // Whether set() would write, so a caller can leave Steam open when it would not. A new
+ // Steam may write config.vdf only when it quits, so a missing one counts as a change.
+ static func needsChange(to tool: String, in file: URL = config) throws -> Bool {
+ guard FileManager.default.fileExists(atPath: file.path(percentEncoded: false)) else { return true }
+ let (text, result) = try planned(tool, in: file)
+ return result != text
+ }
+
+ private static func planned(_ tool: String, in file: URL) throws -> (text: String, result: String) {
+ guard let text = try? String(contentsOf: file, encoding: .utf8) else {
+ throw StepFailure(step: step, detail: "Steam has no config.vdf yet. Open Steam once, log in, quit it and run this again.")
+ }
+ guard let result = updated(text, tool: tool) else {
+ throw StepFailure(step: step, detail: "config.vdf has no InstallConfigStore > Software > Valve > Steam section.")
+ }
+ return (text, result)
+ }
+
// Puts back what set() replaced when the default is still one of Bridgeplay's tools: the
// backed up entry, or no "0" entry at all when there is no backup or there was none.
// A default the user has changed since is left alone. Returns whether it wrote.
diff --git a/app/Sources/BridgeplayApp/SetupAssistant.swift b/app/Sources/BridgeplayApp/SetupAssistant.swift
index 785ae10..f5a37d3 100644
--- a/app/Sources/BridgeplayApp/SetupAssistant.swift
+++ b/app/Sources/BridgeplayApp/SetupAssistant.swift
@@ -67,10 +67,12 @@ final class SetupAssistant {
var metalHUD = GameDefaults.isOn(.metalHUD)
var highResolution = GameDefaults.isOn(.highResolution)
+ nonisolated static let defaultStep = String(localized: "Make WineCX Steam's default")
+
static let plannedTasks = [
SetupTask(id: "steam", title: String(localized: "Add Bridgeplay to Steam")),
SetupTask(id: "engine", title: String(localized: "Set up the WineCX engine")),
- SetupTask(id: "default", title: String(localized: "Make WineCX Steam's default")),
+ SetupTask(id: "default", title: defaultStep),
]
// Set by the debug-only --setup-preview window: actions pretend instead of installing,
@@ -314,10 +316,10 @@ final class SetupAssistant {
set("engine", .done)
set("default", .running)
- // pgrep and the config.vdf write stay off the main actor.
+ // Quitting Steam and the config.vdf write stay off the main actor.
do {
if let tool = FreeEngineWinecx.current.runner.tools.first?.name {
- try await Task.detached(priority: .userInitiated) { try SteamDefaultTool.set(tool) }.value
+ try await Task.detached(priority: .userInitiated) { try Self.makeDefault(tool) }.value
}
set("default", .done)
} catch is SteamDefaultTool.SteamRunning {
@@ -332,6 +334,31 @@ final class SetupAssistant {
step = .done
}
+ // Steam rewrites config.vdf when it quits, so this account's Steam is quit first, and
+ // only when the default changes. The "steam" task only quits it when it has files to
+ // replace. A Steam open in another account does not write this account's config.vdf.
+ nonisolated static func makeDefault(
+ _ tool: String,
+ needsChange: (String) throws -> Bool = { try SteamDefaultTool.needsChange(to: $0) },
+ steamOpen: () -> Bool = { SteamBundle.isRunningForThisUser },
+ runningBuild: () -> String? = {
+ RunnerStore.clonedBuilds().first { RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0)) }
+ },
+ stop: () throws -> Bool = { try SteamBundle.stopClient(step: defaultStep) },
+ write: (String) throws -> Void = { try SteamDefaultTool.set($0) }
+ ) throws {
+ guard try needsChange(tool) else { return }
+ // Quitting Steam can take a running game with it.
+ if steamOpen(), let build = runningBuild() {
+ throw StepFailure(step: defaultStep,
+ detail: String(localized: "A game or Wine tool is still running on build \(build). Quit it first."))
+ }
+ do {
+ if try stop() { AppLog.note("setup: quit Steam to set its default compatibility tool") }
+ } catch is SteamBundle.OpenInAnotherAccount {}
+ try write(tool)
+ }
+
var notStarted: Bool { tasks.allSatisfy { $0.state == .waiting } }
var failed: Bool { tasks.contains { if case .failed = $0.state { true } else { false } } }
diff --git a/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift b/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift
index 1b45297..0ddf155 100644
--- a/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift
+++ b/app/Tests/BridgeplayAppTests/SetupAssistantTests.swift
@@ -74,6 +74,158 @@ struct SetupAssistantTests {
defaults.set(true, forKey: SetupAssistant.finishedKey)
#expect(!SetupAssistant.shouldShow(nil, defaults: defaults))
}
+
+ // MARK: Making WineCX Steam's default
+
+ // Steam in this account, another account, or both. quits: whether it obeys steam://exit.
+ private final class FakeSteam: @unchecked Sendable {
+ private let lock = NSLock()
+ private var here: Bool
+ private let elsewhere: Bool
+ private let quits: Bool
+ private var asked = false
+
+ init(here: Bool, elsewhere: Bool = false, quits: Bool = true) {
+ self.here = here
+ self.elsewhere = elsewhere
+ self.quits = quits
+ }
+
+ var runningHere: Bool { lock.withLock { here } }
+ var askedToQuit: Bool { lock.withLock { asked } }
+
+ var client: SteamBundle.Client {
+ SteamBundle.Client(
+ runningAnywhere: { self.lock.withLock { self.here || self.elsewhere } },
+ runningHere: { self.runningHere },
+ askToQuit: { self.lock.withLock { self.asked = true; if self.quits { self.here = false } } },
+ requestQuit: {},
+ stuckExiting: { _ in false },
+ kill: {},
+ sweep: { _ in },
+ pause: {},
+ attempts: 2,
+ escalationAttempts: 1)
+ }
+ }
+
+ private static let config = """
+ "InstallConfigStore"
+ {
+ \t"Software"
+ \t{
+ \t\t"Valve"
+ \t\t{
+ \t\t\t"Steam"
+ \t\t\t{
+ \t\t\t}
+ \t\t}
+ \t}
+ }
+ """
+
+ private static let tool = "bridgeplay-proton-winecx-11.17"
+
+ // Runs the setup step against a temporary config.vdf; returns its text afterwards.
+ private func makeDefault(_ steam: FakeSteam, config: String? = config, runningBuild: String? = nil) throws -> String {
+ let dir = try folder()
+ let file = dir.appending(path: "config.vdf")
+ try config?.write(to: file, atomically: true, encoding: .utf8)
+ defer { try? FileManager.default.removeItem(at: dir) }
+ do {
+ try SetupAssistant.makeDefault(
+ Self.tool,
+ needsChange: { try SteamDefaultTool.needsChange(to: $0, in: file) },
+ steamOpen: { steam.runningHere },
+ runningBuild: { runningBuild },
+ stop: { try SteamBundle.stopClient(app: URL(filePath: "/tmp/Steam.app"), helpers: [], step: "Test", client: steam.client) },
+ write: { try SteamDefaultTool.set($0, in: file, backup: dir.appending(path: "backup.json"),
+ steamRunning: { steam.runningHere }) })
+ } catch {
+ #expect((try? String(contentsOf: file, encoding: .utf8)) == config)
+ throw error
+ }
+ return try String(contentsOf: file, encoding: .utf8)
+ }
+
+ @Test("Setup quits this account's Steam, then writes the default")
+ func quitsSteamThenWritesDefault() throws {
+ let steam = FakeSteam(here: true)
+ let result = try makeDefault(steam)
+ #expect(steam.askedToQuit)
+ #expect(SteamDefaultTool.current(in: result)?.name == Self.tool)
+ }
+
+ @Test("With Steam closed, nothing is asked to quit and the default is written")
+ func closedSteamIsLeftAlone() throws {
+ let steam = FakeSteam(here: false)
+ let result = try makeDefault(steam)
+ #expect(!steam.askedToQuit)
+ #expect(SteamDefaultTool.current(in: result)?.name == Self.tool)
+ }
+
+ @Test("Steam open only in another account is left open, and this account's default is written")
+ func steamInAnotherAccount() throws {
+ let steam = FakeSteam(here: false, elsewhere: true)
+ let result = try makeDefault(steam)
+ #expect(!steam.askedToQuit)
+ #expect(SteamDefaultTool.current(in: result)?.name == Self.tool)
+ }
+
+ @Test("Steam open in both accounts: this account's is quit and its default is written")
+ func steamInBothAccounts() throws {
+ let steam = FakeSteam(here: true, elsewhere: true)
+ let result = try makeDefault(steam)
+ #expect(steam.askedToQuit)
+ #expect(SteamDefaultTool.current(in: result)?.name == Self.tool)
+ }
+
+ @Test("Without a config.vdf yet, Steam is still quit, since it may write one as it quits")
+ func noConfigYet() throws {
+ let steam = FakeSteam(here: true)
+ let error = #expect(throws: StepFailure.self) { try makeDefault(steam, config: nil) }
+ #expect(error?.detail.contains("no config.vdf") == true)
+ #expect(steam.askedToQuit)
+ }
+
+ @Test("A config.vdf without a Steam section fails without quitting Steam")
+ func brokenConfig() throws {
+ let steam = FakeSteam(here: true)
+ let error = #expect(throws: StepFailure.self) { try makeDefault(steam, config: "\"InstallConfigStore\"\n{\n}\n") }
+ #expect(error?.detail.contains("no InstallConfigStore") == true)
+ #expect(!steam.askedToQuit)
+ }
+
+ @Test("With Steam closed, a running Wine tool does not stop the default being written")
+ func wineToolWithSteamClosed() throws {
+ let steam = FakeSteam(here: false)
+ let result = try makeDefault(steam, runningBuild: "winecx-11.17")
+ #expect(SteamDefaultTool.current(in: result)?.name == Self.tool)
+ }
+
+ @Test("Steam is left open when WineCX is already its default")
+ func alreadyDefault() throws {
+ let steam = FakeSteam(here: true)
+ let config = try #require(SteamDefaultTool.updated(Self.config, tool: Self.tool))
+ #expect(try makeDefault(steam, config: config) == config)
+ #expect(!steam.askedToQuit)
+ }
+
+ @Test("A running game stops setup before Steam is quit, and nothing is written")
+ func runningGame() throws {
+ let steam = FakeSteam(here: true)
+ let error = #expect(throws: StepFailure.self) { try makeDefault(steam, runningBuild: "winecx-11.17") }
+ #expect(error?.detail.contains("winecx-11.17") == true)
+ #expect(!steam.askedToQuit)
+ }
+
+ @Test("Steam that does not quit stops setup with that reason and writes nothing")
+ func steamDoesNotQuit() throws {
+ let steam = FakeSteam(here: true, quits: false)
+ let error = #expect(throws: StepFailure.self) { try makeDefault(steam) }
+ #expect(error?.detail.hasPrefix("Steam did not quit.") == true)
+ #expect(steam.runningHere)
+ }
}
@Suite("Setup checks")