#!/bin/bash
# Fix for c0000005 crashes of old DirectDraw games (Runaway) under CrossOver:
# blocks loading of the Steam overlay (gameoverlayrenderer.dll) for the game process only.
# The overlay keeps working for all other games in the bottle.

set -euo pipefail

BOTTLE="Steam"
GAME_DIR="/Users/<YOUR_PROFILE_NAME>/Library/Application Support/CrossOver/Bottles/Steam/drive_c/Program Files (x86)/Steam/steamapps/common/Runaway A Road Adventure"
GAME_EXE="Runaway.exe"

CX_BIN=""
for app in "/Applications/CrossOver.app" "/Applications/CrossOver Preview.app"; do
  if [ -x "$app/Contents/SharedSupport/CrossOver/bin/wine" ]; then
    CX_BIN="$app/Contents/SharedSupport/CrossOver/bin/wine"
    break
  fi
done
if [ -z "$CX_BIN" ]; then
  echo "Error: CrossOver not found in /Applications" >&2
  exit 1
fi

if [ ! -f "$GAME_DIR/$GAME_EXE" ]; then
  echo "Error: $GAME_EXE not found in: $GAME_DIR" >&2
  exit 1
fi

REG_FILE="$(mktemp /tmp/overlay_override.XXXXXX.reg)"
cat > "$REG_FILE" <<EOF
REGEDIT4

[HKEY_CURRENT_USER\Software\Wine\AppDefaults\\${GAME_EXE}\DllOverrides]
"gameoverlayrenderer"=""
EOF

CX_BOTTLE="$BOTTLE" "$CX_BIN" --bottle "$BOTTLE" regedit "$REG_FILE"
rm -f "$REG_FILE"

echo "Registry check:"
CX_BOTTLE="$BOTTLE" "$CX_BIN" --bottle "$BOTTLE" reg query "HKCU\\Software\\Wine\\AppDefaults\\${GAME_EXE}\\DllOverrides"
echo "Done: Steam overlay disabled for $GAME_EXE (bottle: $BOTTLE)."
