Skip to content

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

fix(launch): keep drive S: on quick relaunches, and count a starting game as running (#1950)

Raystormsb6e6ecd990a1
diff --git a/app/Sources/BridgeplayApp/Model/RunnerInstaller.swift b/app/Sources/BridgeplayApp/Model/RunnerInstaller.swift
index 49e4fb8..f9e6d45 100644
--- a/app/Sources/BridgeplayApp/Model/RunnerInstaller.swift
+++ b/app/Sources/BridgeplayApp/Model/RunnerInstaller.swift
@@ -261,9 +261,53 @@ enum RunnerInstaller {
static func runningBuild(
in runners: URL = SupportPaths.runners,
- running: (URL) -> Bool = { RunnerInstaller.isRunning(from: $0) }
+ running: (URL) -> Bool = { RunnerInstaller.isRunning(from: $0) },
+ launching: () -> String? = { RunnerInstaller.launchingBuild() }
) -> String? {
RunnerStore.clonedBuilds(in: runners).first { running(SupportPaths.runnerRoot(forBuild: $0, runners: runners)) }
+ ?? launching()
+ }
+
+ // Steam keeps a tool's run script going from Play until the game exits. Early in a launch
+ // Wine renames its processes and only the script shows a game is starting. ps prints the
+ // arguments of this user's processes only.
+ static func launchingBuild(
+ tools: [URL] = SupportPaths.Steam.ourTools(), ps: String = "/bin/ps"
+ ) -> String? {
+ guard !tools.isEmpty,
+ let result = try? Shell.run(ps, ["-axww", "-o", "args="]), result.succeeded
+ else { return nil }
+ // Steam starts the script as <tool>/./run.
+ let lines = result.stdout.split(separator: "\n").map { line in
+ var line = line.trimmingCharacters(in: .whitespaces)
+ while line.contains("/./") { line = line.replacingOccurrences(of: "/./", with: "/") }
+ return line
+ }
+ for tool in tools {
+ let root = tool.standardizedFileURL.path(percentEncoded: false)
+ let scripts = Set([root, realpath(root, nil).map { defer { free($0) }; return String(cString: $0) }]
+ .compactMap { $0 }
+ .map { ($0.hasSuffix("/") ? $0 : $0 + "/") + "run" })
+ // Only the script being run counts: as the command itself or as a shell's first
+ // argument, not a path some other process (tail, an editor) was given.
+ func runs(_ command: Substring) -> Bool {
+ scripts.contains { command == $0 || command.hasPrefix($0 + " ") }
+ }
+ let launching = lines.contains { line in
+ let line = Substring(line)
+ if runs(line) { return true }
+ guard let space = line.firstIndex(of: " "),
+ ["sh", "bash", "dash", "zsh"].contains(line[..<space].split(separator: "/").last ?? "")
+ else { return false }
+ return runs(line[line.index(after: space)...])
+ }
+ if launching {
+ let build = (try? String(contentsOf: tool.appending(path: "build"), encoding: .utf8))?
+ .trimmingCharacters(in: .whitespacesAndNewlines)
+ return build.flatMap { $0.isEmpty ? nil : $0 } ?? tool.lastPathComponent
+ }
+ }
+ return nil
}
static func hasClone(forBuild build: String, runners: URL = SupportPaths.runners) -> Bool {
diff --git a/app/Sources/BridgeplayApp/Model/SteamInstaller.swift b/app/Sources/BridgeplayApp/Model/SteamInstaller.swift
index 62bac87..95a7de1 100644
--- a/app/Sources/BridgeplayApp/Model/SteamInstaller.swift
+++ b/app/Sources/BridgeplayApp/Model/SteamInstaller.swift
@@ -95,6 +95,7 @@ enum SteamInstaller {
lockFolder: URL = SupportPaths.applicationSupport,
modifications: SteamModifications = .shared,
runnerIsRunning: @Sendable (URL) -> Bool = { RunnerInstaller.isRunning(from: $0) },
+ launchingBuild: (URL) -> String? = { RunnerInstaller.launchingBuild(tools: SupportPaths.Steam.ourTools(in: $0)) },
verifyRunner: (RunnerBuild, URL) throws -> Void = RunnerInstaller.verifyClone,
patchRunner: (RunnerBuild, URL, URL) throws -> Void = { build, root, bridge in
_ = try NtdllPatcher.stage(build: build, runnerRoot: root, bridge: bridge)
@@ -142,6 +143,9 @@ enum SteamInstaller {
for id in RunnerStore.clonedBuilds(in: runners) where runnerIsRunning(SupportPaths.runnerRoot(forBuild: id, runners: runners)) {
throw StepFailure(step: step, detail: String(localized: "A game or Wine tool is running on build \(id). Quit it before updating Bridgeplay."))
}
+ if let id = launchingBuild(compatTools) {
+ throw StepFailure(step: step, detail: String(localized: "A game or Wine tool is running on build \(id). Quit it before updating Bridgeplay."))
+ }
}
try refuseRunningRunners()
let builds = RunnerStore.installedBuilds(in: runners)
diff --git a/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift b/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift
index 446e413..a56762e 100644
--- a/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift
+++ b/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift
@@ -73,7 +73,7 @@ enum SteamPatchKeeper {
versionFile: URL = SupportPaths.deployedVersion
) -> (deployment: SteamDeployment, previouslyDeployed: Bool, steamRunning: Bool, gameRunning: Bool) {
let gameRunning = Shell.processIsRunning(named: "wineserver")
- || RunnerStore.clonedBuilds().contains { RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0)) }
+ || RunnerInstaller.runningBuild() != nil
return (SteamBundle.deployment(bundledVersion: bundledVersion, versionFile: versionFile),
SteamBundle.deployedVersion(at: versionFile) != nil,
SteamBundle.isRunning,
diff --git a/app/Sources/BridgeplayApp/SetupAssistant.swift b/app/Sources/BridgeplayApp/SetupAssistant.swift
index 88b8321..8b585f9 100644
--- a/app/Sources/BridgeplayApp/SetupAssistant.swift
+++ b/app/Sources/BridgeplayApp/SetupAssistant.swift
@@ -422,9 +422,7 @@ final class SetupAssistant {
_ 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)) }
- },
+ runningBuild: () -> String? = { RunnerInstaller.runningBuild() },
stop: () throws -> Bool = { try SteamBundle.stopClient(step: defaultStep) },
write: (String) throws -> Void = { try SteamDefaultTool.set($0) }
) throws {
diff --git a/app/Sources/BridgeplayApp/Support/InstallIntoSteamCommand.swift b/app/Sources/BridgeplayApp/Support/InstallIntoSteamCommand.swift
index e993a02..5123de1 100644
--- a/app/Sources/BridgeplayApp/Support/InstallIntoSteamCommand.swift
+++ b/app/Sources/BridgeplayApp/Support/InstallIntoSteamCommand.swift
@@ -16,9 +16,7 @@ enum InstallIntoSteamCommand {
throw StepFailure(step: SteamInstaller.step,
detail: "Installation is blocked. Use the Bridgeplay app that installed this build.")
}
- if RunnerStore.clonedBuilds().contains(where: {
- RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0))
- }) {
+ if RunnerInstaller.runningBuild() != nil {
throw StepFailure(step: SteamInstaller.step,
detail: "A game or Wine tool is running. Quit it before updating Bridgeplay.")
}
diff --git a/app/Sources/BridgeplayApp/Support/PostUpdateRefresh.swift b/app/Sources/BridgeplayApp/Support/PostUpdateRefresh.swift
index 485f56e..abdb22c 100644
--- a/app/Sources/BridgeplayApp/Support/PostUpdateRefresh.swift
+++ b/app/Sources/BridgeplayApp/Support/PostUpdateRefresh.swift
@@ -59,9 +59,7 @@ enum PostUpdateRefresh {
// pgrep and ps can take seconds, so neither runs on the main actor.
let (steamRunning, workloadRunning) = await Task.detached(priority: .utility) {
(SteamBundle.isRunning,
- RunnerStore.clonedBuilds().contains {
- RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0))
- })
+ RunnerInstaller.runningBuild() != nil)
}.value
let decision = decide(
steam: snapshot.steam,
diff --git a/app/Sources/BridgeplayApp/Support/SetupWinecxCommand.swift b/app/Sources/BridgeplayApp/Support/SetupWinecxCommand.swift
index f101402..029a0fd 100644
--- a/app/Sources/BridgeplayApp/Support/SetupWinecxCommand.swift
+++ b/app/Sources/BridgeplayApp/Support/SetupWinecxCommand.swift
@@ -21,9 +21,7 @@ enum SetupWinecxCommand {
throw StepFailure(step: FreeEngineWinecx.step,
detail: "Installation is blocked. Use the Bridgeplay app that installed this build.")
}
- if RunnerStore.clonedBuilds().contains(where: {
- RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0))
- }) {
+ if RunnerInstaller.runningBuild() != nil {
throw StepFailure(step: FreeEngineWinecx.step,
detail: "A game or Wine tool is running. Quit it before setting WineCX up.")
}
diff --git a/app/Sources/BridgeplayApp/Support/UpdateEverything.swift b/app/Sources/BridgeplayApp/Support/UpdateEverything.swift
index cc3f36c..ef7555f 100644
--- a/app/Sources/BridgeplayApp/Support/UpdateEverything.swift
+++ b/app/Sources/BridgeplayApp/Support/UpdateEverything.swift
@@ -43,9 +43,7 @@ extension SystemStatus {
// pgrep and ps can take seconds, so neither runs on the main actor.
let busy = await Task.detached(priority: .utility) {
- SteamBundle.isRunning || RunnerStore.clonedBuilds().contains {
- RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0))
- }
+ SteamBundle.isRunning || RunnerInstaller.runningBuild() != nil
}.value
if snapshot.steamRunning || busy {
AppLog.note("update: \(plan) waiting for Steam and games to close")
diff --git a/app/Sources/BridgeplayApp/SystemStatus.swift b/app/Sources/BridgeplayApp/SystemStatus.swift
index 4c27f07..ad9718f 100644
--- a/app/Sources/BridgeplayApp/SystemStatus.swift
+++ b/app/Sources/BridgeplayApp/SystemStatus.swift
@@ -602,9 +602,7 @@ final class SystemStatus {
private func requireInstallableContent() async throws {
try await requireUnblockedContent()
let running = await Task.detached(priority: .utility) {
- RunnerStore.clonedBuilds().contains {
- RunnerInstaller.isRunning(from: SupportPaths.runnerRoot(forBuild: $0))
- }
+ RunnerInstaller.runningBuild() != nil
}.value
guard !running else {
throw StepFailure(step: SteamInstaller.step, detail: String(localized: "A game or Wine tool is running. Quit it before updating Bridgeplay."))
diff --git a/app/Tests/BridgeplayAppTests/RunnerSwitchTests.swift b/app/Tests/BridgeplayAppTests/RunnerSwitchTests.swift
index 0f34654..ebbaebe 100644
--- a/app/Tests/BridgeplayAppTests/RunnerSwitchTests.swift
+++ b/app/Tests/BridgeplayAppTests/RunnerSwitchTests.swift
@@ -745,8 +745,52 @@ struct RunnerRemovalTests {
let path = ps.path(percentEncoded: false)
#expect(RunnerInstaller.isRunning(from: link, ps: path))
- #expect(RunnerInstaller.runningBuild(in: runners) { RunnerInstaller.isRunning(from: $0, ps: path) } == "27")
- #expect(RunnerInstaller.runningBuild(in: runners) { _ in false } == nil)
+ #expect(RunnerInstaller.runningBuild(in: runners, running: { RunnerInstaller.isRunning(from: $0, ps: path) }, launching: { nil }) == "27")
+ #expect(RunnerInstaller.runningBuild(in: runners, running: { _ in false }, launching: { nil }) == nil)
+ }
+
+ @Test("A launch reads as running from its run script, before any Wine process shows up")
+ func readsALaunchFromItsRunScript() throws {
+ let fm = FileManager.default
+ let dir = URL(filePath: try scratchDirectory("runner-launching").path(percentEncoded: false)).resolvingSymlinksInPath()
+ defer { try? fm.removeItem(at: dir) }
+ let tools = dir.appending(path: "compatibilitytools.d")
+ let current = tools.appending(path: "bridgeplay-proton-winecx-11.17")
+ let legacy = tools.appending(path: "notproton-winecx-11.1")
+ for tool in [current, legacy] { try fm.createDirectory(at: tool, withIntermediateDirectories: true) }
+ try "winecx-11.17\n".write(to: current.appending(path: "build"), atomically: true, encoding: .utf8)
+ func ps(_ lines: [String]) throws -> String {
+ let ps = dir.appending(path: "ps-\(UUID().uuidString)")
+ let body = lines.map { "echo '\($0)'" }.joined(separator: "\n")
+ try "#!/bin/sh\n\(body)\n".write(to: ps, atomically: true, encoding: .utf8)
+ try fm.setAttributes([.posixPermissions: 0o755], ofItemAtPath: ps.path(percentEncoded: false))
+ return ps.path(percentEncoded: false)
+ }
+ let script = current.appending(path: "run").path(percentEncoded: false)
+ let launching = try ps(["/sbin/launchd", "/bin/sh \(script) waitforexitandrun /G/Game.exe"])
+ // What Steam runs.
+ let dotted = try ps(["/bin/sh \(current.path(percentEncoded: false))/./run waitforexitandrun /G/Game.exe"])
+ #expect(RunnerInstaller.launchingBuild(tools: [current], ps: dotted) == "winecx-11.17")
+
+ #expect(RunnerInstaller.launchingBuild(tools: [current, legacy], ps: launching) == "winecx-11.17")
+ #expect(RunnerInstaller.launchingBuild(tools: [legacy], ps: launching) == nil)
+ #expect(RunnerInstaller.launchingBuild(tools: [], ps: launching) == nil)
+ // A tool without a build file is named by its folder.
+ let old = try ps(["/bin/sh \(legacy.appending(path: "run").path(percentEncoded: false))"])
+ #expect(RunnerInstaller.launchingBuild(tools: [current, legacy], ps: old) == "notproton-winecx-11.1")
+ #expect(RunnerInstaller.launchingBuild(tools: [current], ps: try ps(["\(script) waitforexitandrun /G/Game.exe"])) == "winecx-11.17")
+ // Nothing running, a longer name, a file next to run, a process only naming the script,
+ // and ps failing all read as no launch.
+ let idle = try ps(["/sbin/launchd", "/bin/sh \(script)x", "/bin/cat \(script).log", "/bin/sh /x\(script)",
+ "/usr/bin/tail -f \(script)", "/usr/bin/vim \(script)", "/bin/sh -c cat \(script)"])
+ #expect(RunnerInstaller.launchingBuild(tools: [current, legacy], ps: idle) == nil)
+ #expect(RunnerInstaller.launchingBuild(tools: [current], ps: "/nonexistent/ps") == nil)
+
+ let runners = dir.appending(path: "runners")
+ try fm.createDirectory(at: runners.appending(path: "crossover-27"), withIntermediateDirectories: true)
+ #expect(RunnerInstaller.runningBuild(in: runners, running: { _ in false }, launching: { "winecx-11.17" }) == "winecx-11.17")
+ #expect(RunnerInstaller.runningBuild(in: runners, running: { _ in true }, launching: { "winecx-11.17" }) == "27")
+ #expect(RunnerInstaller.runningBuild(in: runners, running: { _ in false }, launching: { nil }) == nil)
}
@Test("Any build can be removed, and its tools and staged copies go with it")
diff --git a/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift b/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift
index 13201fc..3bca69a 100644
--- a/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift
+++ b/app/Tests/BridgeplayAppTests/SteamInstallerTests.swift
@@ -112,6 +112,7 @@ struct SteamInstallerTests {
recordBuild: Bool = true,
refusedResets: [String] = [],
runnerIsRunning: (@Sendable (URL) -> Bool)? = nil,
+ launchingBuild: (URL) -> String? = { _ in nil },
stopClient: SteamInstaller.ClientStopper? = nil,
verifyRunner: (RunnerBuild, URL) throws -> Void = { _, _ in },
register: SteamInstaller.BundleRegistrar? = nil,
@@ -133,6 +134,7 @@ struct SteamInstallerTests {
lockFolder: fixture.work,
modifications: fixture.modifications,
runnerIsRunning: runnerIsRunning ?? { _ in running },
+ launchingBuild: launchingBuild,
verifyRunner: verifyRunner,
patchRunner: { _, _, _ in },
stopClient: stopClient ?? { _, onStopping in
@@ -787,6 +789,21 @@ struct SteamInstallerTests {
#expect(!FileManager.default.fileExists(atPath: fixture.deployedDylib.path))
}
+ @Test("A game still launching, before Wine shows up, blocks installation before Steam is asked to quit")
+ func refusesLaunchingGame() async throws {
+ let work = try scratchDirectory("install-launching")
+ defer { try? FileManager.default.removeItem(at: work) }
+ let fixture = try await self.fixture(into: work)
+ let calls = Calls()
+ let tools = fixture.work.appending(path: "compatibilitytools.d")
+ let error = try #require(throws: StepFailure.self) {
+ try install(fixture, calls: calls, launchingBuild: { $0 == tools ? "winecx-11.17" : nil })
+ }
+ #expect(error.detail.contains("winecx-11.17"))
+ #expect(calls.stops == 0, "Steam was asked to quit under a starting game")
+ #expect(!FileManager.default.fileExists(atPath: fixture.deployedDylib.path))
+ }
+
@Test("Wine running from an unsupported clone also blocks installation")
func refusesRunningOrphan() async throws {
let work = try scratchDirectory("install-orphan")
diff --git a/dylib/feats/compat_run.sh b/dylib/feats/compat_run.sh
index 26cf77c..80d4e2c 100755
--- a/dylib/feats/compat_run.sh
+++ b/dylib/feats/compat_run.sh
@@ -2390,18 +2390,26 @@ map_game_drive() {
drive="$WINEPREFIX/dosdevices/s:"
[ -d "$WINEPREFIX/dosdevices" ] && [ ! -L "$WINEPREFIX/dosdevices" ] || return 0
library=""
+ listed=""
set -f
old_ifs=$IFS
IFS=:
for path in $STEAM_COMPAT_LIBRARY_PATHS; do
path=${path%/}
[ -n "$path" ] || continue
+ listed=1
case "$STEAM_COMPAT_INSTALL_PATH/" in
"$path"/*) if [ "${#path}" -gt "${#library}" ]; then library=$path; fi ;;
esac
done
IFS=$old_ifs
set +f
+ # Steam sometimes passes no libraries on a quick relaunch. That says nothing about where
+ # the game is, so S: stays as it is.
+ if [ -z "$listed" ]; then
+ echo "=== Steam passed no library paths, drive S: left as it is ===" >> "$log" 2>&1 || true
+ return 0
+ fi
if [ -z "$library" ]; then
unmap_game_drive
return 0
diff --git a/dylib/tests/gamedrivecheck.sh b/dylib/tests/gamedrivecheck.sh
index 35ab6b6..83d3d35 100644
--- a/dylib/tests/gamedrivecheck.sh
+++ b/dylib/tests/gamedrivecheck.sh
@@ -79,6 +79,20 @@ is "the record is gone" no "$([ -e "$compat/notproton-game-drive" ] && echo yes
is "wine restarts to drop S:" "wineserver -k wineserver -w " "$(restarted)"
is "the launch continues" reached "$(tail -1 "$work/out")"
+echo "== no library paths from Steam leave S: alone"
+run "$lib/steamapps/common/Some Game" "$libs"
+for none in "" ":" "::"; do
+ run "$lib/steamapps/common/Some Game" "$none"
+ is "S: still points at the library root ($none)" "$lib" "$(target)"
+ is "the mapping is still recorded ($none)" "$lib" "$(cat "$compat/notproton-game-drive")"
+ is "wine is not restarted ($none)" "" "$(restarted)"
+ is "the launch continues ($none)" reached "$(tail -1 "$work/out")"
+done
+rm -f "$drive" "$compat/notproton-game-drive"
+run "$lib/steamapps/common/Some Game" ""
+is "no S: is made without library paths" none "$(target)"
+is "wine is not restarted without library paths" "" "$(restarted)"
+
echo "== a game outside every library gets no S:"
run "$work/loose/Some Game" "$libs"
is "no S:" none "$(target)"