Skip to content

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

Load Microsoft's VC++ runtime when a prefix has it, accept newer runtime in Winetricks

matthewwynne6bf5cae9434d
diff --git a/app/Sources/NotProtonApp/Model/PrefixTools.swift b/app/Sources/NotProtonApp/Model/PrefixTools.swift
index 83f516a..9570410 100644
--- a/app/Sources/NotProtonApp/Model/PrefixTools.swift
+++ b/app/Sources/NotProtonApp/Model/PrefixTools.swift
@@ -251,6 +251,18 @@ enum PrefixTools {
static let winetricksSHA256 = "431f82fc74000e6c864409f1d8fb495d696c03928808e3e8acffc45179312a7b"
static let winetricksLogName = "winetricks.log"
+ // Wine truncates vc_redist's 1638 (newer runtime installed) to 102; Winetricks only accepts 236
+ static let winetricksWine = #"""
+ #!/bin/sh
+ "$NOTPROTON_WINE" "$@"
+ status=$?
+ case "$status:${1##*[\\/]}" in
+ 102:[Vv][Cc]*[Rr][Ee][Dd][Ii][Ss][Tt]*.[Ee][Xx][Ee]) exit 236 ;;
+ esac
+ exit "$status"
+
+ """#
+
static func winetricksVerbs(_ text: String) -> [String] {
text.split(whereSeparator: \.isWhitespace).map(String.init)
}
@@ -289,10 +301,20 @@ enum PrefixTools {
throw StepFailure(step: step, detail: String(localized: "\(prefix.title) is running. Quit the game first."))
}
let loader = try readyLoader(step: step, prefix: prefix, runner: runner, flavor: flavor)
+ let wrapper = script.deletingLastPathComponent().appending(path: "notproton-wine")
+ do {
+ try Data(winetricksWine.utf8).write(to: wrapper, options: .atomic)
+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: wrapper.path(percentEncoded: false))
+ } catch {
+ throw StepFailure(step: step, detail: error.localizedDescription)
+ }
// cabextract comes from the runner's bin, which environment() puts on PATH.
var environment = environment(prefix: prefix, runner: runner, flavor: flavor)
- environment["WINE"] = loader.path(percentEncoded: false)
+ environment["WINE"] = wrapper.path(percentEncoded: false)
+ environment["NOTPROTON_WINE"] = loader.path(percentEncoded: false)
+ // Winetricks reads the architecture from WINE_BIN, which would otherwise be the wrapper.
+ environment["WINE_BIN"] = loader.path(percentEncoded: false)
let log = prefix.root.appending(path: winetricksLogName)
let status = try Shell.logged(
diff --git a/app/Tests/NotProtonAppTests/PrefixToolsTests.swift b/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
index da841f0..e209893 100644
--- a/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
+++ b/app/Tests/NotProtonAppTests/PrefixToolsTests.swift
@@ -232,6 +232,40 @@ struct PrefixToolsTests {
atPath: samplePrefix().root.appending(path: PrefixTools.winetricksLogName).path(percentEncoded: false)))
}
+ @Test("Winetricks accepts a Visual C++ installer that finds a newer runtime already installed")
+ func winetricksAcceptsNewerVisualCpp() throws {
+ let dir = FileManager.default.temporaryDirectory.appending(path: "np-winetricks-\(UUID().uuidString)")
+ defer { try? FileManager.default.removeItem(at: dir) }
+ let library = SteamLibrary(root: dir.appending(path: "library"))
+ let prefix = WinePrefix(appID: "2075800", name: "Game", library: library, lastUsed: nil)
+ let runner = dir.appending(path: "runner")
+ let unix = runner.appending(path: "lib/wine/x86_64-unix")
+ for folder in [prefix.pfx, unix, dir.appending(path: "cache")] {
+ try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
+ }
+ func executable(_ url: URL, _ body: String) throws {
+ try Data(body.utf8).write(to: url)
+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: url.path(percentEncoded: false))
+ }
+ // Wine passes on only the low byte of 1638 (ERROR_PRODUCT_VERSION).
+ try executable(unix.appending(path: "wine"), "#!/bin/sh\ncase \"$1\" in *.exe) exit 102 ;; esac\nexit 102\n")
+ let script = dir.appending(path: "cache/winetricks")
+ try executable(script, """
+ #!/bin/sh
+ "$WINE" vc_redist.x86.exe /q; echo "x86 $?"
+ "$WINE" 'C:\\redist\\VC_redist.x64.exe' /q; echo "x64 $?"
+ "$WINE" regedit.exe /S x.reg; echo "regedit $?"
+ echo "bin $WINE_BIN"
+ """)
+
+ let log = try PrefixTools.winetricks(["vcrun2022"], in: prefix, script: script, runner: runner)
+ let output = try String(contentsOf: log, encoding: .utf8)
+ #expect(output.contains("x86 236"))
+ #expect(output.contains("x64 236"))
+ #expect(output.contains("regedit 102"), "only installers have the code rewritten")
+ #expect(output.contains("bin \(unix.appending(path: "wine").path(percentEncoded: false))"))
+ }
+
@Test("Every kind offered by the file picker is a kind that can be run")
func pickerTypesMatchWhatRuns() {
let extensions = PrefixTools.runnableTypes.compactMap(\.preferredFilenameExtension)
diff --git a/dylib/feats/compat_run.sh b/dylib/feats/compat_run.sh
index 61b3998..ce44478 100755
--- a/dylib/feats/compat_run.sh
+++ b/dylib/feats/compat_run.sh
@@ -1033,6 +1033,18 @@ link_fonts() {
done
}
+# Wine loads its own VC++ runtime over Microsoft's, which breaks some games
+native_vc_runtime() {
+ for name in concrt140 msvcp140 msvcp140_1 msvcp140_2 msvcp140_atomic_wait msvcp140_codecvt_ids \
+ vcamp140 vccorlib140 vcomp140 vcruntime140 vcruntime140_1; do
+ dll="$WINEPREFIX/drive_c/windows/system32/$name.dll"
+ [ -f "$dll" ] || continue
+ if [ "$(dd if="$dll" bs=1 skip=64 count=16 2>/dev/null)" != "Wine builtin DLL" ]; then
+ printf '%s=n,b;' "$name"
+ fi
+ done
+}
+
if [ -n "$STEAM_COMPAT_DATA_PATH" ]; then
export WINEPREFIX="$STEAM_COMPAT_DATA_PATH/pfx"
stage_step="prefix lock"
@@ -1243,7 +1255,8 @@ if [ -d "$bridge_src" ] && [ -n "$WINEPREFIX" ]; then
install_legacy_steam_dll
export WINEDLLPATH="$prefix_steam:$WINEDLLPATH"
# If the same DLL appears twice in WINEDLLOVERRIDES, the last entry wins.
- export WINEDLLOVERRIDES="${WINEDLLOVERRIDES:+$WINEDLLOVERRIDES;}steamclient=n;steamclient64=n;lsteamclient=b"
+ vc_overrides=$(native_vc_runtime)
+ export WINEDLLOVERRIDES="${vc_overrides}${WINEDLLOVERRIDES:+$WINEDLLOVERRIDES;}steamclient=n;steamclient64=n;lsteamclient=b"
native_client="$STEAM_COMPAT_CLIENT_INSTALL_PATH"
if [ -z "$native_client" ]; then
native_client="$HOME/Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS"
diff --git a/dylib/tests/envcheck.sh b/dylib/tests/envcheck.sh
index ce9c974..32cb948 100755
--- a/dylib/tests/envcheck.sh
+++ b/dylib/tests/envcheck.sh
@@ -35,6 +35,31 @@ is "user value first" "winhttp=n,b;steamclient=n;steamclient64=n;lsteamclient=b"
WINEDLLOVERRIDES="lsteamclient=n"
eval "$overrides_line"
is "trio outranks user" "lsteamclient=n;steamclient=n;steamclient64=n;lsteamclient=b" "$WINEDLLOVERRIDES"
+vc_overrides="vcruntime140_1=n,b;"
+WINEDLLOVERRIDES="vcruntime140_1=b"
+eval "$overrides_line"
+is "user outranks the native runtime" \
+ "vcruntime140_1=n,b;vcruntime140_1=b;steamclient=n;steamclient64=n;lsteamclient=b" "$WINEDLLOVERRIDES"
+vc_overrides=""
+
+echo "== Microsoft's Visual C++ runtime loads when it is installed =="
+vc_fn=$(sed -n '/^native_vc_runtime() {/,/^}/p' "$SRC")
+[ -n "$vc_fn" ] || { echo "FAIL: native_vc_runtime not found"; exit 1; }
+eval "$vc_fn"
+vc_work=$(mktemp -d)
+WINEPREFIX="$vc_work/pfx"
+sys32="$WINEPREFIX/drive_c/windows/system32"
+mkdir -p "$sys32"
+pe() { printf 'MZ%062d%s' 0 "$2" > "$sys32/$1.dll"; }
+is "empty prefix overrides nothing" "" "$(native_vc_runtime)"
+pe vcruntime140 "Wine builtin DLL"
+pe msvcp140_2 "Wine builtin DLL"
+is "Wine's own copies are left alone" "" "$(native_vc_runtime)"
+pe msvcp140_2 "This program cannot be run in DOS mode."
+pe vcruntime140_1 "This program cannot be run in DOS mode."
+: > "$sys32/msvcp140.dll"
+is "Microsoft's copies load first" "msvcp140=n,b;msvcp140_2=n,b;vcruntime140_1=n,b;" "$(native_vc_runtime)"
+rm -rf "$vc_work"
echo "== Apple GPTK initialization and launcher environment =="
work=$(mktemp -d)