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.

30.4 KB
Raw Blame
#!/usr/bin/env python3
"""Read installed games locally. No account APIs, tokens, telemetry or network.

Launches are reconstructed from current manifests, never from a cached command.
All launcher arguments are passed as an argv array without a shell.
"""
import argparse
import json
import os
from pathlib import Path, PureWindowsPath
import re
import sqlite3
import sys
sys.path.insert(0, str(Path(__file__).resolve().parent))
from game_artwork import executable_icon
from library_services import saved_accounts
import xbox_locations
import didim_games
from soju_anticheat import note as anticheat_note, denied as anticheat_denied
from urllib.parse import quote

FOLDERS = {'steam': 'steam-bottle', 'epic': 'epic-bottle',
           'battlenet': 'bottle', 'gog': 'gog-bottle', 'xbox': 'xbox-bottle'}
# Steam games that stop without Apple GPTK (soju steam-gptk on): Overwatch 2, and
# Diablo II: Resurrected, whose loader needs GPTK's libd3dshared. Order of the Sinking Star
# asks for Direct3D feature level 12_0 and shows an error window under the Direct3D 11 setup (#171).
STEAM_GPTK_GAMES = {'2357570', '2536520', '499170'}
# Any other Steam game whose store page asks for DirectX 12 at minimum gets the same treatment: in the
# Direct3D 11 setup such games close within a minute (Order of the Sinking Star said so in a window, #171;
# Resident Evil 4, Dead Island 2 and Hogwarts Legacy closed at once in the play counts, 2026-11-23).
# The answer is kept per game in steam-dx12.json, so the store is asked once.
STEAM_STORE = 'https://store.steampowered.com/api/appdetails?appids={}&filters=basic,pc_requirements'


def needs_dx12(base, uid, fetch=None):
    """True when Steam's store lists DirectX 12 under the minimum requirements of this game."""
    cache = Path(base) / 'steam-dx12.json'
    try: known = json.loads(cache.read_text())
    except (OSError, ValueError): known = {}
    if uid in known: return bool(known[uid])
    try:
        if fetch is None:
            import urllib.request
            req = urllib.request.Request(STEAM_STORE.format(uid), headers={'user-agent': 'soju'})
            with urllib.request.urlopen(req, timeout=5) as res: body = res.read().decode('utf-8', 'replace')
        else:
            body = fetch(uid)
        data = json.loads(body).get(uid, {})
        if not data.get('success'): return False
        info = data.get('data') if isinstance(data.get('data'), dict) else {}
        req = info.get('pc_requirements') or {}
        minimum = req.get('minimum', '') if isinstance(req, dict) else ''
        found = re.search(r'DirectX:?\s*(?:</strong>)?\s*(?:Version\s*)?(\d+)', minimum, re.I)
        answer = bool(found and int(found.group(1)) >= 12)
    except Exception:
        return False   # offline or the store did not answer: start the game as before, ask again next time
    known[uid] = answer
    try:
        cache.write_text(json.dumps(known)); os.chmod(cache, 0o600)
    except OSError: pass
    return answer

# Installed by Steam itself, not games: Steamworks Common Redistributables.
STEAM_TOOLS = {'228980'}
CLIENTS = {
    'steam': 'Program Files (x86)/Steam/steam.exe',
    'epic': 'Program Files/Epic Games/Launcher/Portal/Binaries/Win64/EpicGamesLauncher.exe',
    'battlenet': 'Program Files (x86)/Battle.net/Battle.net.exe',
    'gog': 'Program Files/GOG Galaxy/GalaxyClient.exe',
    # Xbox PC has no Windows client: `xbox.sh install` writes this marker.
    'xbox': 'ProgramData/Soju/xbox.json',
}
XBOX_ENGINE = 'Set up Xbox PC again in Stores: it installs the engine Xbox PC games run on.'
XBOX_GPTK = "Add Apple's Game Porting Toolkit in Stores before playing."
# Product UIDs differ from the Battle.net launch command's product codes.
BNET = {
    'osi': ('Diablo II: Resurrected', 'OSI'),
    'diablo3': ('Diablo III', 'D3'), 'fen': ('Diablo IV', 'Fen'),
    'wow': ('World of Warcraft', 'WoW'),
    's2': ('StarCraft II', 'S2'), 's1': ('StarCraft: Remastered', 'S1'),
    'w3': ('Warcraft III', 'W3'), 'hs_beta': ('Hearthstone', 'WTCG'),
    'heroes': ('Heroes of the Storm', 'Hero'), 'prometheus': ('Overwatch 2', 'Pro'),
    # Without its code Play only opened Battle.net (the game then needs Battle.net's own Play button).
    # ANBS seen 2026-11-26: a cold Battle.net logged LaunchBinary: uid=anbs 13 s after signing in.
    'anbs': ('Diablo Immortal', 'ANBS'),
}
MAX_METADATA = 16 * 1024 * 1024


def read_bytes(path):
    with path.open('rb') as stream:
        data = stream.read(MAX_METADATA + 1)
    if len(data) > MAX_METADATA:
        raise ValueError('metadata is too large')
    return data


def read_json(path):
    value = json.loads(read_bytes(path).decode('utf-8-sig'))
    if not isinstance(value, dict):
        raise ValueError('expected a metadata object')
    return value


def case_path(root, parts):
    """Resolve Windows' case-insensitive filenames on a case-sensitive volume."""
    for part in parts:
        if part in ('', '.'):
            continue
        if part == '..' or '\x00' in part:
            raise ValueError('invalid path component')
        target = root / part
        if not target.exists() and root.is_dir():
            target = next((p for p in root.iterdir() if p.name.casefold() == part.casefold()), target)
        root = target
    return root


def windows_path(prefix, text):
    if not isinstance(text, str) or not text or '\x00' in text:
        raise ValueError('missing Windows path')
    p = PureWindowsPath(text)
    if not re.fullmatch(r'[A-Za-z]:', p.drive) or not p.root:
        raise ValueError('expected an absolute mapped Windows drive')
    drive = p.drive[0].lower()
    root = prefix / 'drive_c' if drive == 'c' else prefix / 'dosdevices' / (drive + ':')
    return case_path(root, p.parts[1:])


def vdf(data):
    # Steam KeyValues: quoted strings, escaped quotes/backslashes, comments, objects.
    tokens = re.findall(r'"(?:\\.|[^"\\])*"|[{}]|//[^\n]*|[^\s{}"]+', data)
    tokens = [t for t in tokens if not t.startswith('//')]
    at = 0
    def value(t):
        if t.startswith('"'):
            return re.sub(r'\\(["\\])', r'\1', t[1:-1])
        return t
    def obj(depth=0, nested=False):
        nonlocal at
        if depth > 32:
            raise ValueError('KeyValues nesting too deep')
        result = {}
        while at < len(tokens):
            key = tokens[at]; at += 1
            if key == '}':
                if not nested: raise ValueError('unexpected closing brace')
                return result
            if key == '{' or at == len(tokens): raise ValueError('invalid KeyValues')
            item = tokens[at]; at += 1
            if item == '}': raise ValueError('missing value')
            result[value(key)] = obj(depth + 1, True) if item == '{' else value(item)
        if nested: raise ValueError('unclosed KeyValues object')
        return result
    return obj()


def protobuf(data):
    """Bounded wire reader for Agent's local ProductDb (no generated dependency)."""
    at = 0
    def integer():
        nonlocal at
        n = 0
        for shift in range(0, 70, 7):
            if at >= len(data): raise ValueError('truncated protobuf')
            c = data[at]; at += 1; n |= (c & 127) << shift
            if c < 128: return n
        raise ValueError('invalid varint')
    result = {}
    while at < len(data):
        tag = integer(); field, wire = tag >> 3, tag & 7
        if not field: raise ValueError('invalid protobuf field')
        if wire == 0: item = integer()
        elif wire in (1, 2, 5):
            size = integer() if wire == 2 else (8 if wire == 1 else 4)
            if size > len(data) - at: raise ValueError('truncated protobuf value')
            item = data[at:at + size]; at += size
        else: raise ValueError('unsupported protobuf wire type')
        result.setdefault(field, []).append(item)
    return result


def first_file(paths):
    return next((str(p) for p in paths if p.is_file()), None)


class Library:
    def __init__(self, base):
        self.base = Path(base).expanduser().absolute()
        self.folders = dict(FOLDERS)
        # soju steam-gptk on: Steam lives in its own bottle (scripts/steam-gptk.sh).
        if (self.base / 'steam-gptk').is_file() and \
                (self.base / 'steam-gptk-bottle/drive_c' / CLIENTS['steam']).is_file():
            self.folders['steam'] = 'steam-gptk-bottle'
        self.games = {}
        self.warnings = []

    def warn(self, platform, message):
        text = platform + ': ' + message
        if text not in self.warnings: self.warnings.append(text)

    @staticmethod
    def blocked(platform, uid, title, location):
        """Why Play cannot work here, for a game whose anti-cheat its makers keep off Wine (or None)."""
        found = anticheat_denied(title, uid if platform == 'steam' else None, location)
        if not found: return None
        return (title.strip() + ' uses ' + found + ', and its makers do not let it run outside Windows. '
                'It does not start on a Mac, in Soju or with any other Windows layer.')

    def add(self, platform, uid, title, location, arguments, artwork=None, ready=True):
        if not isinstance(uid, str) or not re.fullmatch(r'[A-Za-z0-9_.:-]{1,200}', uid):
            raise ValueError('invalid game identifier')
        if not isinstance(title, str) or not title.strip(): raise ValueError('missing game name')
        unplugged = location.is_symlink() and not location.exists()
        if not unplugged and not location.is_dir(): return
        key = platform + ':' + uid
        issue = None
        prefix = self.base / self.folders[platform]
        if not (prefix / 'drive_c' / CLIENTS[platform]).is_file():
            issue = 'Install this platform in Stores, then try again.'
        elif unplugged:
            target = Path(os.readlink(location)).parts
            drive = target[2] if len(target) > 2 and target[1] == 'Volumes' else 'its drive'
            issue = 'This game is on ' + drive + '. Connect the drive, then try again.'
        elif not ready: issue = 'Finish the installation or update in the official launcher.'
        elif not arguments: issue = 'Open the official launcher to play this title.'
        # A Steam game that needs Apple GPTK while Steam is in its plain mode: Play switches the mode itself.
        gptk = issue is None and platform == 'steam' and uid in STEAM_GPTK_GAMES and self.folders['steam'] == 'steam-bottle'
        self.games[key] = dict(id=key, platform=platform, title=title.strip()[:300],
                               install_path=str(location), artwork=artwork,
                               issue=issue, arguments=arguments,
                               note=None if unplugged else anticheat_note(location), gptk=gptk,
                               blocked=self.blocked(platform, uid, title, None if unplugged else location))
        if location.is_symlink():
            # Moved to another drive with scripts/game-storage.py: say where it is.
            target = Path(os.readlink(location)).parts
            if len(target) > 2 and target[1] == 'Volumes':
                self.games[key].update(volume=target[2], available=not unplugged)

    def steam(self):
        prefix = self.base / self.folders['steam']
        client = prefix / 'drive_c/Program Files (x86)/Steam'
        roots = {client}
        for f in [client / 'steamapps/libraryfolders.vdf', client / 'config/libraryfolders.vdf']:
            if not f.is_file(): continue
            try:
                for key, item in vdf(read_bytes(f).decode('utf-8-sig'))['libraryfolders'].items():
                    if key.isdigit():
                        roots.add(windows_path(prefix, item['path'] if isinstance(item, dict) else item))
            except (OSError, ValueError, KeyError, TypeError, AttributeError):
                self.warn('steam', 'Some library folders could not be read. Open Steam and refresh.')
        for root in sorted(roots):
            if root != client and not root.is_dir():
                self.warn('steam', 'A library drive is unavailable. Reconnect it and refresh.')
            for f in sorted((root / 'steamapps').glob('appmanifest_*.acf')):
                try:
                    item = vdf(read_bytes(f).decode('utf-8-sig'))['AppState']
                    uid = item['appid']
                    if not uid.isdigit() or f.stem != 'appmanifest_' + uid:
                        raise ValueError('mismatched Steam app ID')
                    if uid in STEAM_TOOLS: continue  # installed with Steam itself, not a game
                    location = case_path(root / 'steamapps/common', PureWindowsPath(item['installdir']).parts)
                    if PureWindowsPath(item['installdir']).drive: raise ValueError('invalid relative path')
                    artroot = client / 'appcache/librarycache'
                    art = first_file([artroot / uid / 'library_600x900.jpg',
                                      artroot / (uid + '_library_600x900.jpg'),
                                      artroot / uid / 'header.jpg', artroot / (uid + '_header.jpg')])
                    ready = bool(int(item.get('StateFlags', '0')) & 4)
                    self.add('steam', uid, item['name'], location, ['steam://rungameid/' + uid], art, ready)
                except (OSError, ValueError, KeyError, TypeError, AttributeError):
                    self.warn('steam', 'An installation record could not be read. Refresh after Steam finishes updating.')

    def epic(self):
        prefix = self.base / self.folders['epic']
        manifests = prefix / 'drive_c/ProgramData/Epic/EpicGamesLauncher/Data/Manifests'
        # Epic's launcher moved installs to its EOSH service in 2026-11 and, on that first start, dropped the
        # record of games it did not find there ("EOSH reconciliation: removing installation not reported by
        # EOSH"); the game's files stay. Soju keeps a copy of each game record it has seen, so such a game stays
        # in the library while its folder is there; Play then makes Epic find it again ("Install" into the same
        # folder: Epic checks the files and downloads nothing). A record Epic is rebuilding sits in Pending.
        known = self.base / 'epic-known'
        seen = {}
        for f in sorted(manifests.glob('*.item')) + sorted((manifests / 'Pending').glob('*.item')):
            try: seen.setdefault(read_json(f)['AppName'], (f, f.parent.name == 'Pending'))
            except (OSError, ValueError, KeyError, TypeError, AttributeError):
                self.warn('epic', 'An installation record could not be read. Refresh after Epic finishes updating.')
        for f in sorted(known.glob('*.item')) if known.is_dir() else []:
            try: seen.setdefault(read_json(f)['AppName'], (f, 'known'))
            except (OSError, ValueError, KeyError, TypeError): pass
        for uid, (f, origin) in sorted(seen.items()):
            try:
                item = read_json(f)
                uid = item['AppName']
                if item.get('MainGameAppName') not in (None, '', uid): continue  # DLC
                # Epic's own parts (EOSH, its overlay) have records too: only games go in the library.
                if 'games' not in (item.get('AppCategories') or ['games']): continue
                if origin == 'known' and not (windows_path(prefix, item['InstallLocation']) / '.egstore').is_dir(): continue
                if origin is False and re.fullmatch(r'[0-9a-f]{32}', uid):
                    try:
                        known.mkdir(parents=True, exist_ok=True)
                        (known / (uid + '.item')).write_text(f.read_text(encoding='utf-8-sig'), encoding='utf-8')
                    except OSError: pass
                location = windows_path(prefix, item['InstallLocation'])
                namespace, catalog = item.get('CatalogNamespace'), item.get('CatalogItemId')
                parts = [namespace, catalog, uid] if namespace and catalog else [uid]
                if any(not isinstance(p, str) for p in parts): raise ValueError('invalid Epic ID')
                uri = 'com.epicgames.launcher://apps/' + quote(':'.join(parts), safe='')
                uri += '?action=launch&silent=true'
                art = first_file(sorted(location.glob('*.ico')))
                ready = not item.get('bIsIncompleteInstall', False) or origin == 'known'
                executable = item.get('LaunchExecutable')
                if executable:
                    relative = PureWindowsPath(executable)
                    if relative.drive or relative.root: raise ValueError('invalid executable path')
                    exe = case_path(location, relative.parts)
                    ready = ready and exe.is_file()
                    art = art or executable_icon(exe, self.base / 'library-artwork')
                self.add('epic', uid, item['DisplayName'], location, [uri], art, ready)
            except (OSError, ValueError, KeyError, TypeError, AttributeError):
                self.warn('epic', 'An installation record could not be read. Refresh after Epic finishes updating.')

    def battlenet(self):
        prefix = self.base / self.folders['battlenet']
        path = prefix / 'drive_c/ProgramData/Battle.net/Agent/product.db'
        if not path.is_file(): return
        records = protobuf(read_bytes(path)).get(1, [])
        for record in records:
            try:
                fields = protobuf(record)
                uid = fields[1][0].decode('utf-8')
                if uid in ('agent', 'battle.net', 'bna'): continue
                settings = protobuf(fields[3][0])
                location = windows_path(prefix, settings[1][0].decode('utf-8'))
                # An Agent record can outlive an uninstall or partial download.
                # A game moved to another drive (scripts/game-storage.py) stays listed while unplugged.
                unplugged = location.is_symlink() and not location.exists()
                # Diablo Immortal has no .build.info; Battle.net leaves .product.db in every finished install.
                if not unplugged and not any((location / mark).is_file() for mark in ('.build.info', '.product.db')): continue
                title, code = BNET.get(uid.casefold(), (location.name, None))
                arguments = ['--exec=launch ' + code] if code else []
                art = first_file(sorted(location.glob('*.ico')))
                if not art:
                    executables = [p for p in location.glob('*.exe') if not any(word in p.name.lower() for word in ('launcher', 'uninstall', 'error', 'crash'))]
                    executables.sort(key=lambda p: p.stat().st_size, reverse=True)
                    # A game whose only exe at the top is its launcher (Diablo Immortal) takes the icon from that.
                    executables = executables or sorted(location.glob('*Launcher.exe'))
                    for exe in executables[:4]:
                        art = executable_icon(exe, self.base / 'library-artwork')
                        if art: break
                self.add('battlenet', uid, title, location, arguments, art)
            except (OSError, ValueError, KeyError, IndexError, TypeError, AttributeError):
                self.warn('battlenet', 'An installation record could not be read. Refresh after Battle.net finishes updating.')

    def gog(self):
        prefix = self.base / self.folders['gog']
        cdrive = prefix / 'drive_c'
        entries = {}
        database = cdrive / 'ProgramData/GOG.com/Galaxy/storage/galaxy-2.0.db'
        if database.is_file():
            try:
                db = sqlite3.connect(database.as_uri() + '?mode=ro', uri=True, timeout=1)
                try:
                    db.execute('PRAGMA query_only=ON')
                    rows = db.execute('SELECT i.productId, i.installationPath, p.name FROM InstalledBaseProducts i LEFT JOIN Products p ON p.id = i.productId').fetchall()
                    for uid, path, name in rows:
                        entries[str(uid)] = (windows_path(prefix, path), path, name)
                finally: db.close()
            except (sqlite3.Error, OSError, ValueError):
                self.warn('gog', 'Galaxy installation records are unavailable; checking standard game folders.')
        for pattern in ['GOG Games/*/goggame-*.info', 'Program Files/GOG Galaxy/Games/*/goggame-*.info',
                        'Program Files (x86)/GOG Galaxy/Games/*/goggame-*.info']:
            for f in cdrive.glob(pattern):
                uid = f.stem.removeprefix('goggame-')
                path = 'C:\\' + str(f.parent.relative_to(cdrive)).replace('/', '\\')
                known = entries.get(uid)
                if not known or not (known[0] / f.name).is_file():
                    entries[uid] = (f.parent, path, (known[2] if known else None) or f.parent.name)
        for uid, (location, winpath, title) in entries.items():
            try:
                if not uid.isdigit(): raise ValueError('invalid GOG ID')
                info = location / ('goggame-' + uid + '.info')
                if not info.is_file(): continue
                item = read_json(info)
                if str(item.get('gameId')) != uid: raise ValueError('mismatched GOG ID')
                if str(item.get('rootGameId', uid)) != uid: continue  # DLC
                title = item.get('name') or title or location.name
                art = first_file([location / ('goggame-' + uid + '.ico')])
                args = ['/command=runGame', '/gameId=' + uid, '/path=' + winpath]
                self.add('gog', uid, title, location, args, art)
            except (OSError, ValueError, KeyError, TypeError):
                self.warn('gog', 'An installation record could not be read. Verify this game in Galaxy.')

    def xbox(self):
        # Downloads may be on the Mac or on external drives (xbox_locations.py); a game whose
        # drive is unplugged stays listed as downloaded but not available.
        locations = xbox_locations
        locations.BASE, locations.DEFAULT = self.base, self.base / self.folders['xbox'] / 'drive_c/XboxGames'
        locations.STATE = self.base / 'xbox-locations.json'
        notes = []
        found = locations.scan(warnings=notes)
        for note in notes: self.warn('xbox', note)
        index = locations.load()['games']
        for uid, (location, kind) in sorted(found.items()):
            try:
                art = first_file([self.base / 'library-artwork' / ('xbox-' + uid + '.jpg')])
                if kind == 'missing':
                    title = index.get(uid, {}).get('title') or uid
                    issue = locations.not_connected(location)
                    self.games['xbox:' + uid] = dict(id='xbox:' + uid, platform='xbox', title=title.strip()[:300],
                                                     install_path=str(location / uid), artwork=art, issue=issue,
                                                     arguments=['run', uid], downloaded=True, available=False,
                                                     volume=locations.volume_name(location))
                    continue
                if kind == 'complete':
                    item = read_json(location / '.soju-xbox-install.json')
                    if item.get('schema') != 1 or item.get('product_id') != uid: raise ValueError('mismatched Xbox ID')
                    title, ready = item.get('title') or uid, True
                else:
                    title, ready = uid, False
                self.add('xbox', uid, title, location, ['run', uid], art, ready)
                game = self.games['xbox:' + uid]
                if not ready:
                    game['issue'] = 'This download is not finished. Download again to resume it.'
                elif game['issue'] is None:
                    # The prefix itself is prepared on the first Play; the engine must exist.
                    engine = self.base / 'cx26-engine'
                    if not os.access(engine / 'bin/wine', os.X_OK): game['issue'] = XBOX_ENGINE
                    elif not (engine / 'lib/external/libd3dshared.dylib').is_file(): game['issue'] = XBOX_GPTK
                game['downloaded'] = ready
                game['available'] = True
                game['volume'] = locations.volume_name(location.parent)
            except (OSError, ValueError, KeyError, TypeError, AttributeError):
                self.warn('xbox', 'A download record could not be read. Download the game again to repair it.')

    def didim(self):
        # Native Mac games from Didim Labs: no Windows client or engine involved.
        try: found = didim_games.installed(self.base)
        except (OSError, ValueError, KeyError, TypeError):
            self.warn('didim', 'A Didim Labs game record could not be read. Install the game again.'); return
        for item in found:
            key = 'didim:' + item['uid']
            self.games[key] = dict(id=key, platform='didim', title=item['title'].strip()[:300],
                                   install_path=item['folder'], artwork=item['artwork'], issue=None,
                                   arguments=[item['app']])

    def scan(self):
        self.games = {}; self.warnings = []
        for platform in FOLDERS:
            try: getattr(self, platform)()
            except (OSError, ValueError, KeyError, TypeError, sqlite3.Error):
                self.warn(platform, 'Installation records are temporarily unavailable. Open the launcher and refresh.')
        self.didim()
        accounts, owned, warnings = saved_accounts(self.base)
        self.warnings.extend(warnings)
        for game in owned:
            if game['id'] in self.games:
                self.games[game['id']]['owned'] = True
                # A partial download shows under its Store name.
                if self.games[game['id']]['title'] == game['id'].split(':', 1)[1]:
                    self.games[game['id']]['title'] = game['title']
                if game.get('artwork') and not self.games[game['id']]['artwork']:
                    self.games[game['id']]['artwork'] = game['artwork']
            else:
                self.games[game['id']] = dict(game, arguments=[])
        return {'accounts': accounts, 'games': [{k: v for k, v in g.items() if k != 'arguments'}
                          for g in sorted(self.games.values(), key=lambda g: (g['title'].casefold(), g['id']))],
                'warnings': self.warnings, 'xbox_location': self.xbox_location()}

    def xbox_location(self):
        try: return xbox_locations.describe()
        except (OSError, ValueError, KeyError, TypeError): return None

    def command(self, game_id, install=False):
        self.scan()
        game = self.games.get(game_id)
        if game is None: raise ValueError('This game is no longer installed. Refresh the library.')
        root = Path(__file__).resolve().parent
        if game['platform'] == 'didim':
            if install: raise ValueError('This game is installed. Use Play instead.')
            # -W keeps Soju's task alive until the game quits, so Stop and Running work.
            return ['/usr/bin/open', '-W', '-n', game['arguments'][0]]
        if game['platform'] == 'xbox':
            # A game on an unplugged drive is never downloaded again onto the Mac.
            if game.get('available') is False: raise ValueError(game['issue'])
            if install:
                if game.get('downloaded'): raise ValueError('This game is already downloaded. Use Play instead.')
                if game.get('owned') is not True and not game['install_path']: raise ValueError('This game is not in your Xbox library.')
                if game['issue'] and not game['install_path']: raise ValueError(game['issue'])
                if not (self.base / self.folders['xbox'] / 'drive_c' / CLIENTS['xbox']).is_file():
                    raise ValueError('Set up Xbox PC in Stores first.')
                return ['/bin/bash', str(root / 'xbox.sh'), 'download', game_id.split(':', 1)[1]]
            if not game.get('downloaded'): raise ValueError(game['issue'] or 'This game is owned but not downloaded. Download it first.')
            if game['issue']: raise ValueError(game['issue'])
            return ['/bin/bash', str(root / 'xbox.sh')] + game['arguments']
        if install:
            if game['install_path']: raise ValueError('This game is installed. Use Play instead.')
            client = self.base / self.folders[game['platform']] / 'drive_c' / CLIENTS[game['platform']]
            if not client.is_file(): raise ValueError('Install this launcher in Stores first.')
            args = ['steam://install/' + game_id.split(':', 1)[1]] if game['platform'] == 'steam' else []
            return ['/bin/bash', str(Path(__file__).resolve().parent / 'play.sh'), game['platform']] + args
        if not game['install_path']: raise ValueError('This game is owned but not installed. Open its launcher to install it.')
        if game['issue']: raise ValueError(game['issue'])
        if game.get('gptk') or (game['platform'] == 'steam' and self.folders['steam'] == 'steam-bottle'
                                and needs_dx12(self.base, game_id.split(':', 1)[1])):
            return ['/bin/bash', str(root / 'steam-gptk.sh'), 'play'] + game['arguments']
        return ['/bin/bash', str(root / 'play.sh'), game['platform']] + game['arguments']


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('action', choices=['list', 'launch', 'diagnose', 'install'], nargs='?', default='list')
    parser.add_argument('game_id', nargs='?')
    parser.add_argument('--base', default=os.environ.get('SOJU_BASE', str(Path.home() / '.battlenet-macos')))
    parser.add_argument('--dry-run', action='store_true', help='Print launch argv without starting anything')
    args = parser.parse_args()
    lib = Library(args.base)
    try:
        if args.action == 'list':
            print(json.dumps(lib.scan(), ensure_ascii=False)); return
        if not args.game_id: parser.error('choose a game ID from the list')
        if args.action == 'diagnose':
            lib.scan()
            game = lib.games.get(args.game_id)
            if not game: raise ValueError('This game is no longer installed. Refresh the library.')
            print(game['title'] + ' (' + game['platform'] + ')', flush=True)
            print(game['issue'] or 'Installation found. Checking the platform environment…', flush=True)
            if game['platform'] == 'didim':
                print('Native Mac game. Files: ' + game['install_path'], flush=True); return
            command = ['/bin/bash', str(Path(__file__).resolve().parent / 'doctor.sh'), game['platform']]
        else: command = lib.command(args.game_id, install=args.action == 'install')
        if args.dry_run: print(json.dumps(command)); return
        env = dict(os.environ, SOJU_BASE=str(lib.base))
        for key in ['WINEPREFIX', 'ENGINE']: env.pop(key, None)
        os.execve(command[0], command, env)
    except (OSError, ValueError) as error:
        print('soju: ' + str(error), file=sys.stderr); sys.exit(1)


if __name__ == '__main__':
    main()