Skip to content

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

Offer prefix rebuild choices when launching incompatible games

Logan381559b518ea
diff --git a/Makefile b/Makefile
index 40ac529..d7741d7 100644
--- a/Makefile
+++ b/Makefile
@@ -543,7 +543,7 @@ fonts-install:
@rsync -a --delete $(FONT_FILES) build/fonts/ "$(SUPPORT_DIR)/fonts/"
@echo "==> Installed: $(SUPPORT_DIR)/fonts"
-deploy: $(TARGET) dylib-install sigdb-install fonts-install helpers-install
+deploy: $(TARGET) dylib-install sigdb-install fonts-install helpers-install prefix-command-install
@if [ ! -d "$(STEAM_APP)" ]; then \
echo "==> deploy: $(STEAM_APP) not found"; exit 1; fi
$(call install_atomically,$(TARGET),$(DEPLOY_DST))
@@ -609,6 +609,12 @@ APP_VERSION := $(shell sed -n 's/^\#define NOTPROTON_VERSION "\(.*\)"/\1/p' dyli
APP_SIGN_ID ?= -
+.PHONY: prefix-command-install
+prefix-command-install: app
+ @mkdir -p "$(SUPPORT_DIR)"
+ @printf '%s' "$(CURDIR)/$(APP_BUNDLE)/Contents/MacOS/NotProtonApp" > "$(SUPPORT_DIR)/app-executable.new"
+ @mv -f "$(SUPPORT_DIR)/app-executable.new" "$(SUPPORT_DIR)/app-executable"
+
ICONGEN := $(OUT_DIR)/icongen
ICON_DOC := $(OUT_DIR)/NotProton.icon
ICON_DIR := $(OUT_DIR)/icon
diff --git a/app/Sources/NotProtonApp/Model/PrefixRebuild.swift b/app/Sources/NotProtonApp/Model/PrefixRebuild.swift
index 36cb57f..b33eba9 100644
--- a/app/Sources/NotProtonApp/Model/PrefixRebuild.swift
+++ b/app/Sources/NotProtonApp/Model/PrefixRebuild.swift
@@ -11,12 +11,10 @@ extension PrefixTools {
now: Date = .now,
keepBackup: Bool = true
) throws -> URL? {
- let kept = try recreate(
+ try rebuild(
prefix, runner: SupportPaths.clonedRoot(forBuild: tool.build, runners: runners),
- flavor: tool.tool.flavor, now: now, keepBackup: keepBackup
+ flavor: tool.tool.flavor, now: now, keepBackup: keepBackup, record: tool
)
- try writeBuildRecord(tool, for: prefix)
- return kept
}
@discardableResult
@@ -26,6 +24,13 @@ extension PrefixTools {
flavor: CompatTool.Flavor = .fex,
now: Date = .now,
keepBackup: Bool = true
+ ) throws -> URL? {
+ try rebuild(prefix, runner: runner, flavor: flavor, now: now, keepBackup: keepBackup)
+ }
+
+ private static func rebuild(
+ _ prefix: WinePrefix, runner: URL, flavor: CompatTool.Flavor,
+ now: Date, keepBackup: Bool, record: InstalledTool? = nil
) throws -> URL? {
guard let claim = PrefixStore.claim(prefix) else {
throw StepFailure(
@@ -89,6 +94,7 @@ extension PrefixTools {
if parked { try? fm.moveItem(at: previous, to: prefix.pfx) }
throw error
}
+ if let record { try writeBuildRecord(record, for: prefix) }
guard parked else { return nil }
guard keepBackup else {
try? fm.removeItem(at: previous)
diff --git a/app/Sources/NotProtonApp/Model/SteamInstaller.swift b/app/Sources/NotProtonApp/Model/SteamInstaller.swift
index 85d03c5..efcd570 100644
--- a/app/Sources/NotProtonApp/Model/SteamInstaller.swift
+++ b/app/Sources/NotProtonApp/Model/SteamInstaller.swift
@@ -196,6 +196,9 @@ enum SteamInstaller {
throw StepFailure(step: step, detail: String(localized: "These files did not update: \(remaining.joined(separator: ", "))."))
}
+ if let executable = Bundle.main.executableURL {
+ try RebuildPrefixCommand.recordExecutable(executable, in: support)
+ }
report(.finished)
return InstallOutcome(
stoppedClient: stopped,
diff --git a/app/Sources/NotProtonApp/Support/Launch.swift b/app/Sources/NotProtonApp/Support/Launch.swift
index 945ec62..f5aa427 100644
--- a/app/Sources/NotProtonApp/Support/Launch.swift
+++ b/app/Sources/NotProtonApp/Support/Launch.swift
@@ -3,9 +3,68 @@ import Foundation
@main
enum Launch {
static func main() {
+ if CommandLine.arguments.dropFirst().first == RebuildPrefixCommand.flag {
+ do {
+ try RebuildPrefixCommand.run(Array(CommandLine.arguments.dropFirst(2)))
+ exit(0)
+ } catch {
+ FileHandle.standardError.write(Data("\(error.localizedDescription)\n".utf8))
+ exit(1)
+ }
+ }
if CommandLine.arguments.dropFirst().contains(RemoveEverythingCommand.flag) {
RemoveEverythingCommand.run()
}
NotProtonApp.main()
}
}
+
+enum RebuildPrefixCommand {
+ static let flag = "--rebuild-prefix"
+ static let executableRecord = "app-executable"
+
+ struct Request {
+ let prefix: WinePrefix
+ let tool: InstalledTool
+ let keepBackup: Bool
+ }
+
+ static func request(_ arguments: [String], tools: [InstalledTool]) throws -> Request {
+ func refused(_ detail: String) -> StepFailure {
+ StepFailure(step: "Rebuild prefix", detail: detail)
+ }
+ guard arguments.count == 5,
+ ["keep-backup", "discard-backup"].contains(arguments[4]),
+ arguments[0].hasPrefix("/")
+ else { throw refused("Invalid rebuild arguments.") }
+ let root = URL(filePath: arguments[0]).standardizedFileURL
+ let appID = root.lastPathComponent
+ let compatdata = root.deletingLastPathComponent()
+ let steamapps = compatdata.deletingLastPathComponent()
+ guard !appID.isEmpty, appID.utf8.allSatisfy({ (48...57).contains($0) }),
+ compatdata.lastPathComponent == "compatdata", steamapps.lastPathComponent == "steamapps",
+ FileManager.default.fileExists(atPath: root.appending(path: "pfx").path(percentEncoded: false))
+ else { throw refused("The game prefix could not be found under steamapps/compatdata.") }
+ guard let tool = tools.first(where: {
+ $0.name == arguments[1] && $0.build == arguments[2] && $0.tool.flavor.rawValue == arguments[3]
+ }) else { throw refused("The selected compatibility tool is unavailable. Set it up in NotProton first.") }
+ let library = SteamLibrary(root: steamapps.deletingLastPathComponent())
+ return Request(
+ prefix: WinePrefix(appID: appID, name: PrefixStore.appName(appID: appID, in: library),
+ library: library, lastUsed: nil),
+ tool: tool, keepBackup: arguments[4] == "keep-backup"
+ )
+ }
+
+ static func run(_ arguments: [String]) throws {
+ let lock = try DeploymentContent.acquireInstallationLock()
+ defer { lock.release() }
+ let request = try request(arguments, tools: CompatToolList.installed())
+ try PrefixTools.recreate(request.prefix, as: request.tool, keepBackup: request.keepBackup)
+ }
+
+ static func recordExecutable(_ executable: URL, in support: URL) throws {
+ try atomicReplace(support.appending(path: executableRecord),
+ with: Data(executable.path(percentEncoded: false).utf8), step: "Record NotProton app")
+ }
+}
diff --git a/app/Tests/NotProtonAppTests/PrefixToolsTests.swift b/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
index cbf6650..363d63f 100644
--- a/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
+++ b/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
@@ -353,7 +353,10 @@ struct PrefixToolsTests {
#expect(helper.upperBound == gate.lowerBound)
#expect(gate.upperBound < alert.lowerBound)
- #expect(source.ranges(of: "osascript").count == 1, "an alert bypasses show_alert")
+ #expect(source.ranges(of: "osascript").count == 2)
+ let rebuild = try #require(source.range(of: "rebuild_prefix() {"))
+ let prompt = try #require(source.range(of: "choice=$(osascript"))
+ #expect(source[rebuild.upperBound..<prompt.lowerBound].contains(#"[ "$verb" != run ] || exit 1"#))
let calls = source.ranges(of: " show_alert \"")
#expect(!calls.isEmpty)
@@ -365,6 +368,52 @@ struct PrefixToolsTests {
}
}
+ @Test("Steam rebuild requests use the exact selected tool and library", arguments: [true, false])
+ func steamRebuildRequest(keepBackup: Bool) throws {
+ let (dir, _, prefix) = try usedPrefixAndRunner()
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let selected = InstalledTool(tool: CompatTool(name: "chosen", flavor: .rosetta, display: "Chosen"), build: "chosen-build")
+ let other = InstalledTool(tool: CompatTool(name: "old", flavor: .fex, display: "Old"), build: "old-build")
+ let args = [prefix.root.path(percentEncoded: false), selected.name, selected.build,
+ selected.tool.flavor.rawValue, keepBackup ? "keep-backup" : "discard-backup"]
+ let request = try RebuildPrefixCommand.request(args, tools: [other, selected])
+ #expect(request.prefix.root == prefix.root)
+ #expect(request.tool == selected)
+ #expect(request.keepBackup == keepBackup)
+ for index in 0..<args.count {
+ var invalid = args
+ invalid[index] = "invalid"
+ #expect(throws: StepFailure.self) { try RebuildPrefixCommand.request(invalid, tools: [selected]) }
+ }
+ #expect(throws: StepFailure.self) { try RebuildPrefixCommand.request([], tools: [selected]) }
+ #expect(throws: StepFailure.self) { try RebuildPrefixCommand.request(args + ["extra"], tools: [selected]) }
+ #expect(throws: StepFailure.self) { try RebuildPrefixCommand.request(args, tools: []) }
+ }
+
+ @Test("A selected-tool rebuild preserves saves and records the selected build", arguments: [true, false])
+ func steamRebuildSelectedTool(keepBackup: Bool) throws {
+ let (dir, runner, prefix) = try usedPrefixAndRunner()
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let tool = InstalledTool(tool: CompatTool(name: "chosen", flavor: .rosetta, display: "Chosen"), build: "chosen-build")
+ let runners = dir.appending(path: "runners")
+ let target = SupportPaths.clonedRoot(forBuild: tool.build, runners: runners)
+ try FileManager.default.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)
+ try FileManager.default.moveItem(at: runner, to: target)
+ let claim = try #require(PrefixStore.claim(prefix))
+ #expect(throws: StepFailure.self) {
+ try PrefixTools.recreate(prefix, as: tool, runners: runners, keepBackup: keepBackup)
+ }
+ #expect(PrefixTools.buildRecord(of: prefix) == nil)
+ #expect(PrefixStore.backups(of: prefix).isEmpty)
+ claim.release()
+ let backup = try PrefixTools.recreate(prefix, as: tool, runners: runners, keepBackup: keepBackup)
+ #expect((backup != nil) == keepBackup)
+ #expect(PrefixStore.backups(of: prefix).count == (keepBackup ? 1 : 0))
+ #expect(PrefixTools.buildRecord(of: prefix)?.build == tool.build)
+ let save = prefix.pfx.appending(path: "drive_c/users/steamuser/AppData/Roaming/game/save.dat")
+ #expect(try String(contentsOf: save, encoding: .utf8) == "save")
+ }
+
@Test("The Rosetta flavor takes the x86_64 tree on a runner that has both")
func rosettaFlavorTakesX86() throws {
let fm = FileManager.default
diff --git a/dylib/feats/compat_run.sh b/dylib/feats/compat_run.sh
index 1c00ebe..418e0d9 100755
--- a/dylib/feats/compat_run.sh
+++ b/dylib/feats/compat_run.sh
@@ -312,6 +312,45 @@ alert_safe() {
printf '%s' "$1" | tr -d '"\\'
}
+rebuild_prefix() {
+ # Install-script helpers must never prompt or rebuild behind the player's back.
+ [ "$verb" != run ] || exit 1
+ [ "${np_rebuilt:-0}" != 1 ] || {
+ show_alert "Prefix rebuild failed" "The rebuilt prefix still does not match the selected compatibility tool. Check notproton-run.log."
+ exit 1
+ }
+ exec 8>&- 9>&-
+ choice=$(osascript - "$1" "$np_display" <<'APPLESCRIPT'
+on run argv
+ set explanation to (item 1 of argv) & return & return & "Rebuild with " & (item 2 of argv) & "? Saved games will be preserved. A backup also keeps a copy of the original prefix."
+ set answer to display dialog explanation with title "This game needs its prefix rebuilt" buttons {"Cancel", "Rebuild Prefix", "Backup & Rebuild Prefix"} default button "Backup & Rebuild Prefix" cancel button "Cancel" with icon caution
+ return button returned of answer
+end run
+APPLESCRIPT
+ ) || exit 1
+ case "$choice" in
+ 'Backup & Rebuild Prefix') backup=keep-backup ;;
+ 'Rebuild Prefix') backup=discard-backup ;;
+ *) exit 1 ;;
+ esac
+ executable=$(cat "$np_support/app-executable" 2>/dev/null) || executable=""
+ if [ ! -x "$executable" ]; then
+ show_alert "NotProton app unavailable" "Open NotProton and install or repair it again, then relaunch the game."
+ exit 1
+ fi
+ if ! "$executable" --rebuild-prefix "$STEAM_COMPAT_DATA_PATH" "${np_tool_dir##*/}" \
+ "$np_build" "$np_flavor" "$backup" >> "$log" 2>&1; then
+ show_alert "Prefix rebuild failed" "The game was not launched. Check notproton-run.log for details. Quit any running game or Wine tool before trying again."
+ exit 1
+ fi
+ # A competing launch or rebuild may have run while the dialog was open.
+ # Retake the normal launch lock and check both mismatches before touching Wine.
+ prepare_prefix_directory
+ np_rebuilt=1
+ note_other_build
+ refuse_foreign_prefix
+}
+
refuse_foreign_prefix() {
case "${wine_unix##*/}" in
aarch64-unix) want=aa64 ;;
@@ -320,8 +359,7 @@ refuse_foreign_prefix() {
have=$(prefix_machine) || return 0
[ "$have" = "$want" ] && return 0
echo "=== prefix ntdll is $have and this compatibility tool wants $want, rebuild the prefix in NotProton ===" >> "$log" 2>&1 || true
- show_alert "This game needs its prefix rebuilt" "This game originally ran under $(tool_name "$have"), but $(tool_name "$want") is present now. The prefix needs to be rebuilt in NotProton in order to run the game. You will not lose game saves by rebuilding the prefix."
- exit 1
+ rebuild_prefix "This game originally ran under $(tool_name "$have"), but the selected compatibility tool uses $(tool_name "$want")."
}
last_wine_build() {
diff --git a/dylib/tests/buildcheck.sh b/dylib/tests/buildcheck.sh
index 7738a57..9c3e0c6 100644
--- a/dylib/tests/buildcheck.sh
+++ b/dylib/tests/buildcheck.sh
@@ -48,6 +48,8 @@ launch() {
log="$data/log"
# shellcheck disable=SC2329 # a switch must not raise a dialog
show_alert() { printf 'alert: %s\n' "$2"; }
+ # shellcheck disable=SC2329
+ rebuild_prefix() { show_alert "rebuild" "$1"; exit 1; }
eval "$functions"
note_other_build
claim_prefix
@@ -114,6 +116,93 @@ data=$(mktemp -d "$work/compatdata.XXXXXX")
)
is "an open alert does not keep the prefix locked" "free" "$(cat "$data/during-alert" 2>/dev/null)"
+echo "== rebuild choices =="
+arch_functions="$(extract prefix_machine)
+$(extract tool_name)
+$(extract refuse_foreign_prefix)"
+arch_data="$work/arch/pfx/drive_c/windows/system32"
+mkdir -p "$arch_data"
+dd if=/dev/zero bs=1 count=60 of="$arch_data/ntdll.dll" 2>/dev/null
+# DOS header points at a PE header with the x86_64 machine word.
+printf '\100\000\000\000PE\000\000\144\206' >> "$arch_data/ntdll.dll"
+arch_case() {
+ (
+ WINEPREFIX="$work/arch/pfx" wine_unix="$1" log=/dev/null
+ # shellcheck disable=SC2329
+ rebuild_prefix() { printf 'rebuild: %s\n' "$1"; }
+ eval "$arch_functions"
+ refuse_foreign_prefix
+ )
+}
+is "an architecture mismatch offers rebuilding" 'rebuild: This game originally ran under the Rosetta build of CrossOver, but the selected compatibility tool uses the FEX build of CrossOver.' "$(arch_case aarch64-unix)"
+is "a matching architecture needs no rebuild" "" "$(arch_case x86_64-unix)"
+rebuild=$(extract rebuild_prefix)
+mkdir -p "$work/app with spaces"
+cat > "$work/app with spaces/NotProtonApp" <<'SH'
+#!/bin/sh
+printf '%s\n' "$@" > "$TEST_DATA/arguments"
+/usr/bin/lockf -s -t 0 "$TEST_DATA/.notproton-prefix.lock" true || exit 66
+exit "$TEST_STATUS"
+SH
+chmod +x "$work/app with spaces/NotProtonApp"
+printf '%s' "$work/app with spaces/NotProtonApp" > "$np_support/app-executable"
+
+rebuild_case() {
+ choice=$1 command_status=$2 launch_verb=$3
+ data=$(mktemp -d "$work/compat data.XXXXXX")
+ (
+ export TEST_DATA="$data" TEST_STATUS="$command_status"
+ STEAM_COMPAT_DATA_PATH=$data np_tool_dir=/tools/chosen np_build=selected-build np_flavor=rosetta
+ np_display='Selected "tool" with \ and a newline
+in its name'
+ verb=$launch_verb log="$data/log"
+ # shellcheck disable=SC2329
+ osascript() { cat > "$data/prompt"; [ "$choice" != cancel ] || return 1; printf '%s\n' "$choice"; }
+ # shellcheck disable=SC2329
+ show_alert() { echo "$1" >> "$data/alerts"; }
+ # shellcheck disable=SC2329
+ prepare_prefix_directory() { exec 8>> "$data/.notproton-prefix.lock"; /usr/bin/lockf -s -t 0 8; echo locked >> "$data/checks"; }
+ # shellcheck disable=SC2329
+ note_other_build() { echo build >> "$data/checks"; }
+ # shellcheck disable=SC2329
+ refuse_foreign_prefix() { echo arch >> "$data/checks"; }
+ eval "$rebuild"
+ prepare_prefix_directory
+ : > "$data/checks"
+ rebuild_prefix 'Previous "tool" with \ and a newline
+in its name'
+ echo launched >> "$data/checks"
+ ) && status=0 || status=$?
+}
+rebuild_case 'Backup & Rebuild Prefix' 0 waitforexitandrun
+is "backup rebuild succeeds with lock released for the app" 0 "$status"
+is "selected tool and backup choice reach the app as arguments" "--rebuild-prefix
+$data
+chosen
+selected-build
+rosetta
+keep-backup" "$(cat "$data/arguments")"
+is "launch retakes its lock and rechecks both mismatches" "locked
+build
+arch
+launched" "$(cat "$data/checks")"
+rebuild_case 'Rebuild Prefix' 0 waitforexitandrun
+is "rebuild without backup succeeds" 0 "$status"
+is "rebuild without backup passes discard-backup" discard-backup "$(tail -1 "$data/arguments")"
+rebuild_case cancel 0 waitforexitandrun
+is "cancel stops the launch" 1 "$status"
+is "cancel never calls the app" no "$([ -f "$data/arguments" ] && echo yes || echo no)"
+rebuild_case 'Rebuild Prefix' 7 waitforexitandrun
+is "a failed rebuild stops the launch" 1 "$status"
+is "a failed rebuild never continues launch preparation" "" "$(cat "$data/checks")"
+rebuild_case 'Rebuild Prefix' 0 run
+is "install helpers refuse silently" 1 "$status"
+is "install helpers never prompt" no "$([ -f "$data/prompt" ] && echo yes || echo no)"
+printf '%s' "$work/missing" > "$np_support/app-executable"
+rebuild_case 'Rebuild Prefix' 0 waitforexitandrun
+is "a moved or missing app stops the launch" 1 "$status"
+is "a missing app explains how to repair" 'NotProton app unavailable' "$(cat "$data/alerts")"
+
if [ "$fails" -eq 0 ]; then
echo "==> buildcheck: all assertions hold"
else