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.

6.3 KB
Raw Blame
#!/usr/bin/env bash
# Xbox PC / Microsoft Store through the Xodus backend: sign-in, downloads and play.
#   xbox.sh install | prepare | login | logout [--device] | status | download <ProductId> | run <ProductId> | kill
#           | location [<folder>|--default]
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BASE="${SOJU_BASE:-$HOME/.battlenet-macos}"
export SOJU_BASE="$BASE"
XODUS_CLI="${SOJU_XODUS_BIN:-$BASE/xodus/bin/xodus-cli}"
XBOX_HOME="$BASE/xbox-bottle"
# Tokens go to the macOS Keychain ("Xodus Service"); keep backend logs quiet so
# nothing account-related reaches Soju's log files.
export XODUS_LOG=error

need_backend() {
  if [ -n "${SOJU_XODUS_BIN:-}" ]; then
    [ -x "$XODUS_CLI" ] || { echo "SOJU_XODUS_BIN must point to an xodus-cli build with the Nexus commands." >&2; exit 1; }
    return
  fi
  python3 "$ROOT/scripts/fetch-xodus-backend.py" --check || {
    echo "Set up Xbox PC in Stores first (or run: soju xbox-install)." >&2; exit 1; }
}

case "${1:-}" in
  install)
    if [ -z "${SOJU_XODUS_BIN:-}" ]; then python3 "$ROOT/scripts/fetch-xodus-backend.py"; fi
    need_backend
    mkdir -p "$XBOX_HOME/drive_c/XboxGames" "$XBOX_HOME/drive_c/ProgramData/Soju"
    marker="$XBOX_HOME/drive_c/ProgramData/Soju/xbox.json"
    if [ ! -f "$marker" ]; then
      printf '{"schema":1,"backend":"%s","created":"%s"}\n' \
        "$(python3 -c 'import json,sys;print(json.load(open(sys.argv[1]))["version"])' "$ROOT/resources/xodus-backend.json")" \
        "$(date -u +%Y-%m-%dT%H:%M:%SZ)" > "$marker"
    fi
    # Games run on the CX engine with Apple's D3DMetal; without them only downloads work for now.
    if [ -x "${ENGINE:-$BASE/cx26-engine}/bin/wine" ] && [ -f "${ENGINE:-$BASE/cx26-engine}/lib/external/libd3dshared.dylib" ]; then
      python3 "$ROOT/scripts/xbox-prefix.py"
    else
      if [ -x "${ENGINE:-$BASE/cx26-engine}/bin/wine" ]; then
        echo "Playing Xbox PC games needs Apple's Game Porting Toolkit: add it in Stores (or: soju gptk)."
      else
        echo "Playing Xbox PC games needs the CX engine: set up Xbox PC again in Stores (or: soju install)."
      fi
    fi
    echo "Xbox PC is ready. Sign in from Soju > Accounts (or: soju xbox-login)."
    python3 "$ROOT/scripts/xbox_locations.py" show | head -1 ;;
  location)
    case "${2:-}" in
      '') exec python3 "$ROOT/scripts/xbox_locations.py" show ;;
      --default) exec python3 "$ROOT/scripts/xbox_locations.py" default ;;
      *) exec python3 "$ROOT/scripts/xbox_locations.py" set "$2" ;;
    esac ;;
  prepare)
    exec python3 "$ROOT/scripts/xbox-prefix.py" ;;
  run)
    need_backend
    exec python3 "$ROOT/scripts/xbox-run.py" "${2:?Choose a game (Store product ID)}" ;;
  login)
    need_backend
    echo "Opening the Microsoft sign-in page. Finish signing in there, then this window closes."
    echo "If Microsoft shows a notice about your account and then stays on \"Please wait\", close the window and sign in again."
    # Cancel in Soju sends TERM here: close the sign-in window too, so nothing is stored afterwards.
    # Some backend lines name the account; keep e-mail addresses out of the log.
    "$XODUS_CLI" login > >(sed -E 's/[^[:space:]]+@[^[:space:]]+/<account>/g') 2>&1 &
    login_pid=$!
    trap 'kill -TERM "$login_pid" 2>/dev/null; wait "$login_pid" 2>/dev/null; exit 143' TERM INT HUP
    # A closed sign-in window ends login with an error; the stored-account check below reports it.
    wait "$login_pid" || true
    trap - TERM INT HUP
    "$XODUS_CLI" account --json | python3 -c 'import json,sys
v=json.load(sys.stdin)
sys.exit(0 if v.get("stored") else 1)' || { echo "Sign-in was not completed." >&2; exit 1; }
    echo "Signed in."
    # Xbox Live needs a profile (gamertag) on the account; Microsoft accounts made
    # outside Xbox have none until someone signs in at xbox.com once.
    # Only stderr is kept: stdout carries the account name.
    profile_err="$("$XODUS_CLI" profile --json 2>&1 >/dev/null || true)"
    case "$profile_err" in
      *"unavailable (xsts)"*) echo "This Microsoft account has no Xbox profile yet. Sign in once at xbox.com and choose a gamertag; downloads need it." ;;
    esac ;;
  logout)
    # Signing out must work even when the backend is gone or its recipe changed:
    # any xodus-cli that still runs clears the Keychain entry, and Soju's own
    # copy of the library is removed either way.
    status=0
    if [ -x "$XODUS_CLI" ] && "$XODUS_CLI" --version >/dev/null 2>&1; then
      if [ "${2:-}" = "--device" ]; then "$XODUS_CLI" logout --device || status=$?; else "$XODUS_CLI" logout || status=$?; fi
    else
      echo "The Xbox PC backend is not installed; removing Soju's copy of the library only."
    fi
    rm -f "$BASE/account-libraries/xbox.json" "$BASE"/library-artwork/xbox-*.jpg
    if [ "$status" != 0 ]; then
      echo "soju: The Xbox library was removed from Soju, but the Microsoft sign-in is still in the Keychain. Delete \"Xodus Service\" in Keychain Access, or sign out again." >&2
      exit 2
    fi
    echo "Signed out. Downloaded games are kept." ;;
  status)
    need_backend
    # Only whether an account is stored; the account name is never printed.
    "$XODUS_CLI" account --json | python3 -c 'import json,sys
print("Signed in" if json.load(sys.stdin).get("stored") else "Not signed in")' ;;
  download)
    need_backend
    exec python3 "$ROOT/scripts/xbox-download.py" "${2:?Choose a game (Store product ID)}" ;;
  kill)
    # Downloads and games record their pid; only a live xbox-download.py or
    # xbox-run.py with that pid is signalled. SIGTERM lets the download keep its
    # checkpoint, and lets xbox-run.py stop the game and copy its saves back.
    stopped=0
    for f in "$BASE"/run/xbox-*.pid; do
      [ -f "$f" ] || continue
      p="$(cat "$f" 2>/dev/null)"
      case "$p" in ''|*[!0-9]*) continue ;; esac
      cmd="$(ps -p "$p" -o command= 2>/dev/null || true)"
      [ -n "$cmd" ] || { rm -f "$f"; continue; }
      case "$cmd" in
        *xbox-download.py*) kill -TERM "$p" && echo "Download stopped. Download again to resume." && stopped=1 ;;
        *xbox-run.py*) kill -TERM "$p" && echo "Game stop requested." && stopped=1 ;;
      esac
    done
    [ "$stopped" = 1 ] || echo "No Xbox download or game is running." ;;
  *) echo "Usage: xbox.sh install|prepare|login|logout [--device]|status|download <ProductId>|run <ProductId>|kill|location [<folder>|--default]" >&2; exit 64 ;;
esac