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.

Add soju-reaper: kill windowless zombie game processes, tear down idle bottles

mikey92b8512a819577
diff --git a/install.sh b/install.sh
index 3c9afb0d937..e2d01aedde5 100644
--- a/install.sh
+++ b/install.sh
@@ -113,6 +113,9 @@ fi
# ---------- 4. App bundle ----------
say "[4/4] Creating Battle.net.app"
+REAPER="$BASE/soju-reaper.sh"
+[ -f "$REAPER" ] || curl -fsSL "https://raw.githubusercontent.com/$REPO/main/scripts/soju-reaper.sh" -o "$REAPER" 2>/dev/null || true
+chmod +x "$REAPER" 2>/dev/null || true
APP="$HOME/Applications/Battle.net.app"
mkdir -p "$APP/Contents/MacOS"
cat > "$APP/Contents/MacOS/launcher" <<EOF
@@ -122,6 +125,8 @@ export CX_APPLEGPTK_LIBD3DSHARED_PATH="$ENGINE/lib/external/libd3dshared.dylib"
export DYLD_FALLBACK_LIBRARY_PATH="$ENGINE/lib:/usr/lib"
BN="\$WINEPREFIX/drive_c/Program Files (x86)/Battle.net"
for v in "\$BN"/Battle.net.[0-9]*; do [ -d "\$v" ] && cp -f "\$BN/Battle.net.exe" "\$v/Battle.net.exe" 2>/dev/null; done
+# Reap zombie game processes so quitting the game really quits it
+pgrep -f "soju-reaper.sh \$WINEPREFIX" >/dev/null 2>&1 || { [ -x "$REAPER" ] && ( "$REAPER" "\$WINEPREFIX" "$ENGINE/bin/wineserver" >/dev/null 2>&1 & ); }
exec "$ENGINE/bin/wine" "C:\\\\Program Files (x86)\\\\Battle.net\\\\Battle.net Launcher.exe" --disable-gpu-compositing
EOF
chmod +x "$APP/Contents/MacOS/launcher"
diff --git a/scripts/play.sh b/scripts/play.sh
index 8a6a66eecf4..d4abcd0de00 100755
--- a/scripts/play.sh
+++ b/scripts/play.sh
@@ -40,13 +40,23 @@ if [[ -f "$BN/Battle.net.exe" ]]; then
done
fi
+REAPER="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/soju-reaper.sh"
+start_reaper(){
+ # Watchdog: reaps game processes that outlive their windows, and shuts the
+ # prefix down once everything is closed — so "quit" really means quit.
+ pgrep -f "soju-reaper.sh $WINEPREFIX" >/dev/null 2>&1 && return 0
+ [ -x "$REAPER" ] && ( "$REAPER" "$WINEPREFIX" "$ENGINE/bin/wineserver" >/dev/null 2>&1 & )
+}
+
case "$MODE" in
battlenet) # Battle.net launcher (log in, then Play for online)
+ start_reaper
exec "$ENGINE/bin/wine" \
"C:\\Program Files (x86)\\Battle.net\\Battle.net Launcher.exe" \
--disable-gpu-compositing
;;
d2r) # Launch the game directly (offline / previous session)
+ start_reaper
cd "$WINEPREFIX/drive_c/Program Files (x86)/Diablo II Resurrected"
exec "$ENGINE/bin/wine" "D2R.exe" "${@:2}"
;;
@@ -105,6 +115,7 @@ case "$MODE" in
"$WINESTABLE" "${STEAM_CMD[@]}"
;;
kill) # Stop everything in the Battle.net bottle
+ pkill -f "soju-reaper.sh" 2>/dev/null || true
"$ENGINE/bin/wineserver" -k 2>/dev/null || true
;;
steam-kill) # Stop everything in the Steam bottle — note this bottle runs on
diff --git a/scripts/soju-reaper.sh b/scripts/soju-reaper.sh
new file mode 100755
index 00000000000..87ca18518ad
--- /dev/null
+++ b/scripts/soju-reaper.sh
@@ -0,0 +1,89 @@
+#!/usr/bin/env bash
+# soju-reaper — cleans up game processes that outlive their windows.
+#
+# Why: under Wine, quitting D2R sometimes leaves D2R.exe alive with no window
+# (a shutdown thread misses a wakeup). The user sees a dead Dock icon and a
+# process holding gigabytes of RAM. This watchdog kills a game process once it
+# has gone windowless for two consecutive checks, and when the bottle is fully
+# idle (no game, no launcher window) it shuts the whole prefix down so nothing
+# lingers after "quit".
+#
+# Window detection uses CGWindowListCopyWindowInfo via python3+ctypes — no
+# Accessibility permission needed, nothing to install.
+#
+# Usage: soju-reaper.sh <WINEPREFIX> <wineserver-path> (backgrounded by play.sh)
+set -u
+
+PREFIX="${1:?usage: soju-reaper.sh <WINEPREFIX> <wineserver>}"
+WINESERVER="${2:?}"
+INTERVAL=20
+GAME_RE='D2R\.exe|BlizzardError\.exe'
+UI_RE='Battle\.net\.exe.*--from-launcher|Battle\.net Launcher\.exe'
+
+window_pids() {
+ /usr/bin/python3 - <<'PY'
+import ctypes
+cg = ctypes.CDLL("/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics")
+cf = ctypes.CDLL("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation")
+cg.CGWindowListCopyWindowInfo.restype = ctypes.c_void_p
+cg.CGWindowListCopyWindowInfo.argtypes = [ctypes.c_uint32, ctypes.c_uint32]
+cf.CFArrayGetCount.restype = ctypes.c_long
+cf.CFArrayGetCount.argtypes = [ctypes.c_void_p]
+cf.CFArrayGetValueAtIndex.restype = ctypes.c_void_p
+cf.CFArrayGetValueAtIndex.argtypes = [ctypes.c_void_p, ctypes.c_long]
+cf.CFDictionaryGetValue.restype = ctypes.c_void_p
+cf.CFDictionaryGetValue.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
+cf.CFStringCreateWithCString.restype = ctypes.c_void_p
+cf.CFStringCreateWithCString.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_uint32]
+cf.CFNumberGetValue.restype = ctypes.c_bool
+cf.CFNumberGetValue.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]
+key = cf.CFStringCreateWithCString(None, b"kCGWindowOwnerPID", 0x08000100)
+arr = cg.CGWindowListCopyWindowInfo(1, 0) # kCGWindowListOptionOnScreenOnly
+pids = set()
+for i in range(cf.CFArrayGetCount(arr)):
+ num = cf.CFDictionaryGetValue(cf.CFArrayGetValueAtIndex(arr, i), key)
+ if num:
+ v = ctypes.c_int(0)
+ if cf.CFNumberGetValue(num, 9, ctypes.byref(v)):
+ pids.add(v.value)
+print(" ".join(map(str, sorted(pids))))
+PY
+}
+
+declare -A STRIKES
+IDLE_STRIKES=0
+
+while true; do
+ sleep "$INTERVAL"
+
+ GAME_PIDS=$(pgrep -f "$GAME_RE" 2>/dev/null || true)
+ UI_PIDS=$(pgrep -f "$UI_RE" 2>/dev/null || true)
+
+ # Bottle fully gone? Nothing left to do.
+ if [ -z "$GAME_PIDS" ] && [ -z "$UI_PIDS" ]; then
+ IDLE_STRIKES=$((IDLE_STRIKES + 1))
+ # Two idle checks in a row: tear down leftover services (Agent, winedevice…)
+ if [ "$IDLE_STRIKES" -ge 2 ]; then
+ WINEPREFIX="$PREFIX" "$WINESERVER" -k 2>/dev/null || true
+ exit 0
+ fi
+ continue
+ fi
+ IDLE_STRIKES=0
+
+ [ -z "$GAME_PIDS" ] && continue
+ WINS=" $(window_pids) "
+
+ for pid in $GAME_PIDS; do
+ if [[ "$WINS" == *" $pid "* ]]; then
+ STRIKES[$pid]=0
+ else
+ STRIKES[$pid]=$(( ${STRIKES[$pid]:-0} + 1 ))
+ # Windowless on two consecutive checks (>=40s): it's a zombie, reap it.
+ if [ "${STRIKES[$pid]}" -ge 2 ]; then
+ kill -9 "$pid" 2>/dev/null || true
+ unset "STRIKES[$pid]"
+ fi
+ fi
+ done
+done