Skip to content

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

fix(app): see the arm64 and x86 wine servers when checking for a running game (#2339)

Raystorms326ff4ca0e70
diff --git a/app/Sources/BridgeplayApp/Model/BrandMigration.swift b/app/Sources/BridgeplayApp/Model/BrandMigration.swift
index 37057a4..5e272c0 100644
--- a/app/Sources/BridgeplayApp/Model/BrandMigration.swift
+++ b/app/Sources/BridgeplayApp/Model/BrandMigration.swift
@@ -22,7 +22,7 @@ struct BrandMigration: Sendable {
let steamUserData: URL
var steamRunning: @Sendable () -> Bool = { SteamBundle.isRunningForThisUser }
- var gameRunning: @Sendable () -> Bool = { Shell.processIsRunning(named: "wineserver") }
+ var gameRunning: @Sendable () -> Bool = { WineServer.isRunning() }
static let markerName = ".migrated-3.0.0"
static let backupFolder = "backups/migration-3.0.0"
diff --git a/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift b/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift
index f72ffdd..e4d38c5 100644
--- a/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift
+++ b/app/Sources/BridgeplayApp/Model/SteamPatchKeeper.swift
@@ -82,7 +82,7 @@ enum SteamPatchKeeper {
bundledVersion: String,
versionFile: URL = SupportPaths.deployedVersion
) -> (deployment: SteamDeployment, previouslyDeployed: Bool, steamRunning: Bool, gameRunning: Bool) {
- let gameRunning = Shell.processIsRunning(named: "wineserver")
+ let gameRunning = WineServer.isRunning()
|| RunnerInstaller.runningBuild() != nil
return (SteamBundle.deployment(bundledVersion: bundledVersion, versionFile: versionFile),
SteamBundle.deployedVersion(at: versionFile) != nil,
diff --git a/app/Sources/BridgeplayApp/Support/RestartGuard.swift b/app/Sources/BridgeplayApp/Support/RestartGuard.swift
index c6e9d9f..02842dc 100644
--- a/app/Sources/BridgeplayApp/Support/RestartGuard.swift
+++ b/app/Sources/BridgeplayApp/Support/RestartGuard.swift
@@ -4,6 +4,6 @@ import Foundation
// the main actor.
enum RestartGuard {
static func gameRunning() async -> Bool {
- await Task.detached(priority: .utility) { Shell.processIsRunning(named: "wineserver") }.value
+ await Task.detached(priority: .utility) { WineServer.isRunning() }.value
}
}
diff --git a/app/Sources/BridgeplayApp/Support/WineServer.swift b/app/Sources/BridgeplayApp/Support/WineServer.swift
new file mode 100644
index 0000000..b9b3ed8
--- /dev/null
+++ b/app/Sources/BridgeplayApp/Support/WineServer.swift
@@ -0,0 +1,14 @@
+import Foundation
+
+// Whether a game's Wine server is running. The server's name depends on the engine: the
+// arm64 engine runs wineserver-arm64, Rosetta's runs wineserver or wineserver-x86 (the names
+// PrefixTools.layout and dylib/feats/compat_run.sh pick). Match them all, not "wineserver".
+enum WineServer {
+ // An extended regex for pgrep -x, which anchors it to the whole process name.
+ static let processPattern = "wineserver(-arm64|-x86)?"
+
+ // This account's servers only. pgrep can take seconds, so callers keep it off the main actor.
+ static func isRunning(pgrep: String = "/usr/bin/pgrep") -> Bool {
+ Shell.processIsRunning(named: processPattern, pgrep: pgrep)
+ }
+}
diff --git a/app/Tests/BridgeplayAppTests/WineServerTests.swift b/app/Tests/BridgeplayAppTests/WineServerTests.swift
new file mode 100644
index 0000000..8520d2c
--- /dev/null
+++ b/app/Tests/BridgeplayAppTests/WineServerTests.swift
@@ -0,0 +1,75 @@
+import Foundation
+import Testing
+
+@testable import BridgeplayApp
+
+// Serialized: each case looks for children of this process, so cases must not overlap.
+@Suite("WineServer", .serialized)
+struct WineServerTests {
+
+ // Runs a copy of sleep under `name` as a child of this process, so a real game's server on
+ // the Mac cannot change the answer. Returns whether WineServer sees it.
+ private func sees(serverNamed name: String) throws -> Bool {
+ let dir = URL.temporaryDirectory.appending(path: "np-wineserver-\(UUID().uuidString)")
+ try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
+ defer { try? FileManager.default.removeItem(at: dir) }
+
+ // Only this process's children, whatever else the Mac runs.
+ let pgrep = dir.appending(path: "pgrep")
+ try Data("#!/bin/sh\nexec /usr/bin/pgrep -P \(getpid()) \"$@\"\n".utf8).write(to: pgrep)
+ let server = dir.appending(path: name)
+ try FileManager.default.copyItem(atPath: "/bin/sleep", toPath: server.path(percentEncoded: false))
+ for url in [pgrep, server] {
+ try FileManager.default.setAttributes(
+ [.posixPermissions: 0o755], ofItemAtPath: url.path(percentEncoded: false))
+ }
+ // A copied system binary only runs with a signature of its own.
+ let sign = try Shell.run("/usr/bin/codesign", ["-f", "-s", "-", server.path(percentEncoded: false)])
+ try #require(sign.succeeded)
+
+ let process = Process()
+ process.executableURL = server
+ process.arguments = ["30"]
+ try process.run()
+ defer { process.terminate(); process.waitUntilExit() }
+
+ return WineServer.isRunning(pgrep: pgrep.path(percentEncoded: false))
+ }
+
+ @Test("Every engine's server name counts as a running game",
+ arguments: ["wineserver-arm64", "wineserver-x86", "wineserver"])
+ func everyServerNameCounts(name: String) throws {
+ #expect(try sees(serverNamed: name))
+ }
+
+ @Test("A process that only starts with wineserver does not count",
+ arguments: ["wineserver-arm64x", "wineserver-foo", "xwineserver"])
+ func lookAlikesDoNotCount(name: String) throws {
+ let seen = try sees(serverNamed: name)
+ #expect(seen == false)
+ }
+
+ // The names come from PrefixTools.layout (and compat_run.sh). A server it can pick that the
+ // pattern misses would put the bug back, so every layout's server has to match.
+ @Test("Every server PrefixTools can pick matches the pattern",
+ arguments: [
+ (["lib/wine/aarch64-unix/wine.app/Contents/MacOS/wine", "bin/wineserver-arm64"], CompatTool.Flavor.fex),
+ (["lib/wine/x86_64-unix/wine", "bin/wineserver"], .rosetta),
+ (["lib/wine/x86_64-unix/wine", "bin/wineserver-x86"], .rosetta),
+ ])
+ func everyLayoutServerMatches(files: [String], flavor: CompatTool.Flavor) throws {
+ let fm = FileManager.default
+ let runner = fm.temporaryDirectory.appending(path: "np-server-\(UUID().uuidString)")
+ defer { try? fm.removeItem(at: runner) }
+ for path in files {
+ let file = runner.appending(path: path)
+ try fm.createDirectory(at: file.deletingLastPathComponent(), withIntermediateDirectories: true)
+ try Data().write(to: file)
+ try fm.setAttributes([.posixPermissions: 0o755], ofItemAtPath: file.path(percentEncoded: false))
+ }
+
+ let name = PrefixTools.layout(runner: runner, flavor: flavor).server.lastPathComponent
+ let regex = try NSRegularExpression(pattern: "^(\(WineServer.processPattern))$")
+ #expect(regex.firstMatch(in: name, range: NSRange(name.startIndex..., in: name)) != nil, "\(name)")
+ }
+}
diff --git a/scripts/np-test-game.sh b/scripts/np-test-game.sh
index 8759ca3..c211df4 100755
--- a/scripts/np-test-game.sh
+++ b/scripts/np-test-game.sh
@@ -208,8 +208,9 @@ restore_fix
stop_game() {
local ws=""
ws="$(sed -n 's/^WINESERVER=//p' "$(latest_of "$RUNLOG")" 2>/dev/null | tail -1)"
- [ -x "$ws" ] || ws="$(ls -d "$NP"/engines/*/Contents/SharedSupport/CrossOver/bin/wineserver 2>/dev/null | head -1)"
- [ -x "$ws" ] || ws="$(ls "$NP"/runners/*/CrossOver/bin/wineserver "$NP"/runners/current/bin/wineserver 2>/dev/null | head -1)"
+ # The server's name depends on the engine, as in WineServer.swift: wineserver-arm64, wineserver or wineserver-x86.
+ [ -x "$ws" ] || ws="$(ls -d "$NP"/engines/*/Contents/SharedSupport/CrossOver/bin/wineserver{-arm64,,-x86} 2>/dev/null | head -1)"
+ [ -x "$ws" ] || ws="$(ls "$NP"/runners/*/CrossOver/bin/wineserver{-arm64,,-x86} "$NP"/runners/current/bin/wineserver{-arm64,,-x86} 2>/dev/null | head -1)"
if [ -x "$ws" ] && [ -d "$PFX" ]; then
WINEPREFIX="$PFX" "$ws" -k 2>/dev/null || true
sleep 2