Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

10.4 KB
Raw Blame
#!/usr/bin/env bash
# Enable D3D11 rendering for Steam games: wires DXMT + the patched winemac.
#
# Prerequisite: create-steam-bottle.sh. Verified components are downloaded into
# ~/.battlenet-macos/steam-support/ (dxmt-x64/{d3d11,dxgi,d3d10core,winemetal}.dll
# + winemetal.so, winemac-patched.so, winecoreaudio-patched.so, mmdevapi-x64.dll).
# See docs/STEAM-GAMES.md for how to build the artifacts (based on
# notpop/steam-on-m1-wine plus three fixes).
#
# Architecture (verified 2026-08-27, M4 Pro / macOS 26.5, Unity title rendering in-game):
#   - Bundle x86_64 builtins = DXMT (for games) / i386 builtins = untouched vanilla
#     (protects the 32-bit steam.exe composer)
#   - winemetal: bundle builtin + a "placeholder" copy in system32 (without it,
#     wine can't find the builtin by name: c0000135 -> the game's
#     "Failed to initialize graphics")
#   - system32 gets marker-stripped vanilla d3d dlls (native, Steam client only)
#   - Registry: global d3d11/d3d10core/dxgi/winemetal=builtin (DXMT);
#     steam.exe / steamwebhelper* / steamservice get per-app native (vanilla):
#     the modern Steam CEF conflicts with DXMT builtins and restart-loops, so the
#     split is mandatory
#   - winemac-patched.so: -fvisibility=default (DXMT dlsyms macdrv APIs) +
#     WINE_NO_DOCK_ICON support (single Dock icon)
#   - winecoreaudio-patched.so + mmdevapi-x64.dll: patches/steam-wine-audio.patch, so a game's
#     5.1/7.1 output is folded to stereo instead of losing centre and back (#93)
#   - winemac-patched.so also lists low fullscreen modes (640x480 and up) that macOS does not
#     mark "safe", so GoldSrc games switch to them instead of staying black (#153)
#   - wow64cpu-x64.dll: lretq back to 32-bit code under Rosetta 2 (#153)
#   - opengl32-i386.dll + opengl32-unix.so: 32-bit immediate-mode OpenGL calls cross to 64-bit code
#     in batches (patches/opengl32-wow64-batch.patch), and hl.exe/svencoop.exe run without
#     GL_ARB_multitexture (scripts/gl-app-defaults.sh): Counter-Strike 1.6 frame rate (#153)
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
if [ "${1:-}" = renderer ]; then
  shift
  exec python3 "$ROOT/scripts/steam-renderer.py" "$@"
fi
BASE="${SOJU_BASE:-$HOME/.battlenet-macos}"
export WINEPREFIX="${WINEPREFIX:-$BASE/steam-bottle}"
source "$ROOT/scripts/steam-runtime.sh"
SUP="$BASE/steam-support/prebuilt"
python3 - "$ROOT/resources/steam-support.json" <<'PYVERSION'
import json, platform, sys
m=json.load(open(sys.argv[1]))
version=lambda s: tuple(map(int,s.split('.')))
if version(platform.mac_ver()[0]) < version(m['minimum_macos']):
    sys.exit('This Steam component build requires macOS '+m['minimum_macos']+' or later.')
PYVERSION
python3 "$ROOT/scripts/fetch-steam-support.py"
# Voice lines in Windows Media Audio need GStreamer (#94); a failed download only costs those voices.
python3 "$ROOT/scripts/fetch-gstreamer.py" >/dev/null || echo "GStreamer could not be downloaded; Steam games with WMA voices stay silent until soju update retries"
B="$WINEPREFIX/drive_c/windows"
[ -d "$B/system32" ] || { echo "Install Steam first: soju steam-install"; exit 1; }
unset DYLD_FALLBACK_LIBRARY_PATH WINEDLLOVERRIDES WINEMSYNC ROSETTA_ADVERTISE_AVX
unset CX_ACTIVE_GRAPHICS_BACKEND CX_GRAPHICS_BACKEND CX_APPLEGPTK_LIBD3DSHARED_PATH WINE_SIMULATE_WRITECOPY
export WINEDEBUG=-all
VERSION=$(cat "$SUP/.soju-support-version")
TARGET="$BASE/steam-runtime"
if [ "${1:-}" != --repair ] && soju_steam_runtime_ready && \
   [ "$STEAM_WINE_ROOT" = "$TARGET" ] && [ -f "$TARGET/.soju-runtime" ] && \
   [ "$(cat "$WINEPREFIX/.soju-steam-games" 2>/dev/null || true)" = "$VERSION" ] && \
   [ "$(cat "$TARGET/.soju-runtime")" = "$VERSION" ]; then
  echo "Steam rendering is ready ($VERSION)."
  exit 0
fi
# Check before downloading/replacing Wine or editing the prefix.
python3 "$ROOT/scripts/steam-session.py" idle "$STEAM_WINE_ROOT"
soju_ensure_steam_runtime "$ROOT"
SOURCE="$STEAM_WINE_ROOT"
NEW=$(mktemp -d "$BASE/.steam-runtime.XXXXXX")
BACKUP=""; BACKUP_READY=0
rollback(){
  status=$?
  if [ "$status" != 0 ] && [ "$BACKUP_READY" = 1 ]; then
    "$NEW/bin/wineserver" -k 2>/dev/null || true
    "$NEW/bin/wineserver" -w 2>/dev/null || true
    cp "$BACKUP/user.reg" "$WINEPREFIX/user.reg"
    for D in d3d11 dxgi d3d10core winemetal; do
      if [ -f "$BACKUP/$D.dll" ]; then cp "$BACKUP/$D.dll" "$B/system32/$D.dll"
      else rm -f "$B/system32/$D.dll"; fi
    done
    if [ -f "$BACKUP/renderer-version" ]; then cp "$BACKUP/renderer-version" "$WINEPREFIX/.soju-steam-games"
    else rm -f "$WINEPREFIX/.soju-steam-games"; fi
    echo "Setup failed; the previous prefix configuration was restored."
  fi
  rm -rf "$NEW"
}
trap rollback EXIT
printf 'Preparing the Soju Steam runtime (existing Wine app is preserved)\n'
ditto "$SOURCE" "$NEW"
WAPP="$NEW"
WLIB="$WAPP/lib/wine"
WINE="$WAPP/bin/wine"

echo "==> 1/5 Backing up vanilla dlls (first run only)"
for D in d3d11 dxgi d3d10core; do
  [ -f "$WLIB/x86_64-windows/$D.dll.vanilla" ] || cp -f "$WLIB/x86_64-windows/$D.dll" "$WLIB/x86_64-windows/$D.dll.vanilla"
done

echo "==> 2/5 Wiring the private runtime: x86_64=DXMT, winemetal (builtin+unix), patched winemac"
cp -f "$SUP/dxmt-x64/"{d3d11.dll,dxgi.dll,d3d10core.dll,winemetal.dll} "$WLIB/x86_64-windows/"
cp -f "$SUP/dxmt-x64/winemetal.so" "$WLIB/x86_64-unix/"
[ -f "$WLIB/x86_64-unix/winemac.so.vanilla" ] || cp "$WLIB/x86_64-unix/winemac.so" "$WLIB/x86_64-unix/winemac.so.vanilla"
cp -f "$SUP/winemac-patched.so" "$WLIB/x86_64-unix/winemac.so"
if [ -f "$SUP/winecoreaudio-patched.so" ]; then
  cp -f "$SUP/winecoreaudio-patched.so" "$WLIB/x86_64-unix/winecoreaudio.so"
  cp -f "$SUP/mmdevapi-x64.dll" "$WLIB/x86_64-windows/mmdevapi.dll"
fi
# 32-bit games (#153): wow64cpu that leaves 64-bit code with lretq under Rosetta 2. With Wine's
# ljmp a 32-bit thread could reach the next system call still in 32-bit mode and crash reading
# 0x4ecd (Counter-Strike 1.6, Sven Co-op on map load). Older support packages have none.
if [ -f "$SUP/wow64cpu-x64.dll" ]; then
  [ -f "$WLIB/x86_64-windows/wow64cpu.dll.vanilla" ] || cp -f "$WLIB/x86_64-windows/wow64cpu.dll" "$WLIB/x86_64-windows/wow64cpu.dll.vanilla"
  cp -f "$SUP/wow64cpu-x64.dll" "$WLIB/x86_64-windows/wow64cpu.dll"
fi
# 32-bit OpenGL games (#153): opengl32 that sends immediate-mode calls to the Unix side in batches.
# The i386 dll and the Unix library go together. They replace files by a new inode (rm, then cp):
# Rosetta refused a library rewritten in place ("Attachment of code signature supplement failed").
if [ -f "$SUP/opengl32-i386.dll" ] && [ -f "$SUP/opengl32-unix.so" ]; then
  [ -f "$WLIB/i386-windows/opengl32.dll.vanilla" ] || cp -f "$WLIB/i386-windows/opengl32.dll" "$WLIB/i386-windows/opengl32.dll.vanilla"
  [ -f "$WLIB/x86_64-unix/opengl32.so.vanilla" ] || cp -f "$WLIB/x86_64-unix/opengl32.so" "$WLIB/x86_64-unix/opengl32.so.vanilla"
  rm -f "$WLIB/i386-windows/opengl32.dll" "$WLIB/x86_64-unix/opengl32.so"
  cp "$SUP/opengl32-i386.dll" "$WLIB/i386-windows/opengl32.dll"
  cp "$SUP/opengl32-unix.so" "$WLIB/x86_64-unix/opengl32.so"
fi
# MoltenVK 1.4.2 with Soju's patch for undescribed vertex inputs (scripts/build-moltenvk.sh), for
# games that draw with Vulkan and for DXVK. Older support packages have none: the runtime's stays.
if [ -f "$SUP/libMoltenVK.dylib" ]; then
  [ -f "$WAPP/lib/libMoltenVK.dylib.vanilla" ] || cp -f "$WAPP/lib/libMoltenVK.dylib" "$WAPP/lib/libMoltenVK.dylib.vanilla"
  cp -f "$SUP/libMoltenVK.dylib" "$WAPP/lib/libMoltenVK.dylib"
fi
# i386 stays vanilla (protects the 32-bit steam.exe composer)

BACKUP="$BASE/steam-support/prefix-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP"
# Keep this backup and the two before it; every run used to leave one more behind.
ls -d "$BASE"/steam-support/prefix-backup-* 2>/dev/null | sort | awk -v keep=3 '{a[NR]=$0} END{for(i=1;i<=NR-keep;i++) print a[i]}' |
  while IFS= read -r old; do case "$old" in "$BASE"/steam-support/prefix-backup-2*) rm -rf "$old" ;; esac; done
cp "$WINEPREFIX/user.reg" "$BACKUP/user.reg"
for D in d3d11 dxgi d3d10core winemetal; do
  [ ! -f "$B/system32/$D.dll" ] || cp "$B/system32/$D.dll" "$BACKUP/$D.dll"
done
[ ! -f "$WINEPREFIX/.soju-steam-games" ] || cp "$WINEPREFIX/.soju-steam-games" "$BACKUP/renderer-version"
BACKUP_READY=1
echo "Recovery copy: $BACKUP"

echo "==> 3/5 Prefix: winemetal placeholder + marker-stripped vanilla natives for Steam"
cp -f "$WLIB/x86_64-windows/winemetal.dll" "$B/system32/winemetal.dll"
for D in d3d11 dxgi d3d10core; do
  cp -f "$WLIB/x86_64-windows/$D.dll.vanilla" "$B/system32/$D.dll"
  /usr/bin/python3 - "$B/system32/$D.dll" <<'EOF'
import sys
p = sys.argv[1]; d = open(p,'rb').read()
m = b'Wine builtin DLL'
if m in d: open(p,'wb').write(d.replace(m, b'Xine builtin DLL', 1))
EOF
done

echo "==> 4/5 Registry: global=builtin (DXMT), Steam processes per-app=native (vanilla)"
for DLL in d3d11 d3d10core dxgi winemetal; do
  "$WINE" reg add "HKCU\\Software\\Wine\\DllOverrides" /v "$DLL" /t REG_SZ /d builtin /f >/dev/null 2>&1
done
for APP in steam.exe steamwebhelper.exe steamwebhelper_real.exe steamservice.exe; do
  for DLL in d3d11 d3d10core dxgi; do
    "$WINE" reg add "HKCU\\Software\\Wine\\AppDefaults\\$APP\\DllOverrides" /v "$DLL" /t REG_SZ /d native /f >/dev/null 2>&1
  done
  "$WINE" reg add "HKCU\\Software\\Wine\\AppDefaults\\$APP\\DllOverrides" /v winemetal /t REG_SZ /d disabled /f >/dev/null 2>&1
done

# GoldSrc games draw several times faster without multitexture on Apple's OpenGL (#153).
bash "$ROOT/scripts/gl-app-defaults.sh" "$WINE"

echo "==> 5/5 Removing the fullscreen-forcing AppCompat token (allows windowed mode)"
# The reg adds above left a wineserver up with a dirty HKCU; it rewrites
# user.reg when it exits, which would undo an edit made now. Wait for it.
"$WAPP/bin/wineserver" -w 2>/dev/null || true
/usr/bin/python3 - "$WINEPREFIX/user.reg" <<'EOF'
import sys
p = sys.argv[1]
d = open(p, encoding='utf-8', errors='surrogateescape').read()
n = d.replace('DISABLEDXMAXIMIZEDWINDOWEDMODE', '')
if n != d: open(p, 'w', encoding='utf-8', errors='surrogateescape').write(n)
EOF

"$WINE" --version >/dev/null
printf '%s\n' "$VERSION" > "$NEW/.soju-runtime"
printf '%s\n' "$VERSION" > "$WINEPREFIX/.soju-steam-games"
[ ! -d "$TARGET.previous" ] || rm -rf "$TARGET.previous"
[ ! -d "$TARGET" ] || mv "$TARGET" "$TARGET.previous"
if ! mv "$NEW" "$TARGET"; then
  [ ! -d "$TARGET.previous" ] || mv "$TARGET.previous" "$TARGET"
  exit 1
fi
BACKUP_READY=0
echo "Done. Launch with scripts/play.sh steam, to pin windowed mode, use the"
echo "in-game setting or the Steam launch option '-screen-fullscreen 0' (Unity titles)."