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.

32.1 KB
Raw Blame
#!/usr/bin/env python3
"""Diablo II: Resurrected in VR (offline): set up and play D2R VR Unleashed.

  soju vr setup [--refresh]   copy the Battle.net bottle and install the mod there
  soju vr play                start D2R in VR (offline characters only)
  soju vr status              what is installed (JSON with --json)
  soju vr remove              delete the VR copy (your Battle.net bottle is untouched)

The mod goes into an APFS copy of the Battle.net bottle (d2r-vr/bottle): the
copy takes almost no extra disk space at first, and the bottle Battle.net uses
is never modified. D2RLoader starts D2R with -mod, which keeps it offline.

Every download is pinned by size and SHA-256 in resources/d2r-vr.json. The VR
runtime (OXRSys + the wineopenxr bridge) is Soju's own build of pinned sources
(scripts/build-vr-runtime.sh). The mod's own installer runs under Windows
Python inside the copy, with the dependencies passed in already checked.

Three things the mod needs on Soju's engine, all inside the copy:
- the game folder's d3d12.dll is renamed, so D3D12 loads from system32, where
  ReShade watches for it (Wine already uses its builtin d3d12 either way);
- in VR the windowed resolution is fixed at 1920x1080, the size the game first
  creates its swapchain at, so it never resizes it (on the Mac screen it is the
  player's own setting);
- NVIDIA DLSS goes through Apple's MetalFX bridge like normal D2R, with a small
  nvngx.dll in front (tools/ngx-unwrap) that hands the bridge the real D3D12
  objects instead of ReShade's wrappers;
- one jump in ReShade64.dll skips work on a resource D3DMetal already destroyed
  (resources/d2r-vr.json, reshade.patch).
"""
import argparse
import hashlib
import json
import os
from pathlib import Path, PurePosixPath
import shutil
import subprocess
import sys
import tempfile
import zipfile

ROOT = Path(__file__).resolve().parents[1]
MANIFEST = ROOT / 'resources/d2r-vr.json'
BASE = Path(os.environ.get('SOJU_BASE', Path.home() / '.battlenet-macos'))
ENGINE = Path(os.environ.get('ENGINE', BASE / 'cx26-engine'))
SOURCE = BASE / 'bottle'
VR = BASE / 'd2r-vr'
BOTTLE = VR / 'bottle'
GAME_REL = 'drive_c/Program Files (x86)/Diablo II Resurrected'
SAVES_REL = 'drive_c/users/crossover/Saved Games/Diablo II Resurrected'
STATE = VR / 'state.json'
LIMIT = 256 * 1024 * 1024


class VRError(Exception):
    pass


def say(text):
    print(text, flush=True)


def digest(path):
    h = hashlib.sha256()
    with Path(path).open('rb') as f:
        for block in iter(lambda: f.read(1024 * 1024), b''):
            h.update(block)
    return h.hexdigest()


def manifest():
    return json.loads(MANIFEST.read_text())


def recipe(m):
    return hashlib.sha256(json.dumps(m, sort_keys=True).encode()).hexdigest()


def read_raw(path):
    """Text with its line endings as they are (Path.read_text has no newline= before Python 3.13,
    and the app runs this with macOS's /usr/bin/python3, 3.9)."""
    with open(path, encoding='utf-8', newline='') as f:
        return f.read()


def write_raw(path, text):
    with open(path, 'w', encoding='utf-8', newline='') as f:
        f.write(text)


def fetch(entry, name, label):
    """Download entry['url'] into the cache unless a checked copy is there."""
    cache = VR / 'downloads'
    cache.mkdir(parents=True, exist_ok=True)
    dest = cache / name
    if dest.is_file() and dest.stat().st_size == entry['size'] and digest(dest) == entry['sha256']:
        return dest
    if not entry.get('sha256') or not entry.get('size'):
        raise VRError(label + ' is not published yet for this Soju version.')
    say('Downloading ' + label + ' (' + str(max(1, entry['size'] // 1000000)) + ' MB)…')
    part = dest.with_suffix(dest.suffix + '.part')
    got = subprocess.run(['/usr/bin/curl', '--fail', '--silent', '--show-error', '--location', '--proto', '=https',
                          '--proto-redir', '=https', '--retry', '3', '--connect-timeout', '20', '--max-time', '1800',
                          entry['url'], '--output', str(part)], capture_output=True, text=True)
    if got.returncode:
        part.unlink(missing_ok=True)
        host = entry['url'].split('/')[2]
        # Seen 2026-11-25: an internet provider's security filter (Spectrum) answers d2rloader.net
        # with its own warning page, so the TLS connection fails ("wrong version number").
        raise VRError('Could not download ' + label + ' from ' + host + ': ' + (got.stderr.strip() or 'curl failed')
                      + '. If your network or internet provider filters websites, allow ' + host + ' and run setup again.')
    if part.stat().st_size != entry['size'] or digest(part) != entry['sha256']:
        part.unlink(missing_ok=True)
        raise VRError(label + ' did not match its pinned checksum; nothing was installed.')
    part.replace(dest)
    return dest


def safe_extract_zip(archive, target):
    target.mkdir(parents=True, exist_ok=True)
    with zipfile.ZipFile(archive) as z:
        for info in z.infolist():
            path = PurePosixPath(info.filename.replace('\\', '/'))
            if path.is_absolute() or '..' in path.parts:
                raise VRError('Unsafe path in ' + archive.name + ': ' + info.filename)
            out = target.joinpath(*path.parts)
            if info.is_dir():
                out.mkdir(parents=True, exist_ok=True)
                continue
            out.parent.mkdir(parents=True, exist_ok=True)
            with z.open(info) as src, out.open('wb') as dst:
                shutil.copyfileobj(src, dst)


def safe_extract_tar(archive, target):
    import tarfile
    target.mkdir(parents=True, exist_ok=True)
    with tarfile.open(archive) as t:
        for member in t.getmembers():
            path = PurePosixPath(member.name)
            if path.is_absolute() or '..' in path.parts or not (member.isfile() or member.isdir()):
                raise VRError('Unexpected entry in the VR runtime: ' + member.name)
        if hasattr(tarfile, 'data_filter'):
            t.extractall(target, filter='data')
        else:   # macOS's /usr/bin/python3 3.9: members were checked above
            t.extractall(target)


def winpath(path):
    return 'Z:' + str(Path(path).resolve()).replace('/', '\\')


def wine_env(prefix):
    env = dict(os.environ)
    env.update({
        'WINEPREFIX': str(prefix),
        'WINEDEBUG': os.environ.get('SOJU_VR_WINEDEBUG', 'fixme-all'),
        'WINEMSYNC': '1',
        'ROSETTA_ADVERTISE_AVX': '1',
        'WINE_SIMULATE_WRITECOPY': '1',
        'CX_ACTIVE_GRAPHICS_BACKEND': 'd3dmetal',
        'CX_GRAPHICS_BACKEND': 'd3dmetal',
        'CX_APPLEGPTK_LIBD3DSHARED_PATH': str(ENGINE / 'lib/external/libd3dshared.dylib'),
        'DYLD_FALLBACK_LIBRARY_PATH': str(ENGINE / 'lib') + ':/usr/lib',
    })
    env.pop('XR_RUNTIME_JSON', None)
    return env


def wine(prefix, args, **kwargs):
    return subprocess.run([str(ENGINE / 'bin/wine')] + args, env=wine_env(prefix), **kwargs)


def game_build(bottle):
    info = bottle / GAME_REL / '.build.info'
    try:
        text = info.read_text(errors='replace')
    except OSError:
        return None
    for line in reversed(text.splitlines()[1:]):
        for field in line.split('|'):
            parts = field.split('.')
            if len(parts) == 3 and all(p.isdigit() for p in parts):
                return field
    return None


SHELLS = {'bash', 'zsh', '-zsh', 'sh', 'grep', 'pgrep', 'ps', 'python3', 'python', 'osascript'}


def running(names):
    """Wine processes whose command line names one of these executables.

    A shell or script that only mentions the name (pgrep -f would match it)
    does not count."""
    out = subprocess.run(['/bin/ps', '-axo', 'args='], capture_output=True, text=True).stdout
    found = []
    for line in out.splitlines():
        first = line.strip().split(' ', 1)[0].replace('\\', '/').rsplit('/', 1)[-1]
        if first.lower() in SHELLS:
            continue
        if any(name.lower() in line.lower() for name in names):
            found.append(line)
    return found


def stop_bottle(prefix):
    subprocess.run([str(ENGINE / 'bin/wineserver'), '-k'], env=wine_env(prefix),
                   stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)


def patch_reshade(dll, spec):
    data = bytearray(dll.read_bytes())
    current = hashlib.sha256(data).hexdigest()
    patch = spec['patch']
    if current == patch['patched_sha256']:
        return False
    if current != spec['dll_sha256']:
        raise VRError('ReShade64.dll is not the pinned ReShade 6.8.0 build; not patching it.')
    at = patch['file_offset']
    old, new = bytes.fromhex(patch['old']), bytes.fromhex(patch['new'])
    if bytes(data[at:at + len(old)]) != old:
        raise VRError('ReShade64.dll does not have the expected code at the patch site.')
    data[at:at + len(new)] = new
    if hashlib.sha256(data).hexdigest() != patch['patched_sha256']:
        raise VRError('Patched ReShade64.dll did not come out as expected.')
    tmp = dll.with_suffix('.dll.soju-tmp')
    tmp.write_bytes(bytes(data))
    tmp.replace(dll)
    return True


def isolate_user_folders(bottle):
    """Turn the copy's Documents/Downloads/... links to the Mac folders into plain folders.

    Started from Soju.app, D2R writing its logs to Documents makes macOS ask whether
    Soju may use the Documents folder, and the game waits on that prompt. The copy
    keeps its own logs instead; the Mac folders are left alone.
    """
    users = bottle / 'drive_c/users'
    changed = []
    if not users.is_dir():
        return changed
    for user in users.iterdir():
        if user.is_symlink() or not user.is_dir():
            continue
        for link in user.iterdir():
            if not link.is_symlink():
                continue
            target = os.path.realpath(link)
            if target == str(bottle.resolve()) or target.startswith(str(bottle.resolve()) + os.sep):
                continue
            link.unlink()
            link.mkdir(mode=0o700)
            changed.append(link.name)
    return changed


def fix_settings(saves, flat=False, dlss=False):
    """In VR: windowed 1920x1080 (above). On the Mac screen the player's own resolution stays.
    NVIDIA DLSS stays as chosen when the MetalFX bridge is ready, else it is turned off."""
    settings = saves / 'Settings.json'
    data = json.loads(settings.read_text(encoding='utf-8-sig'))
    want = {} if flat else {'Screen Resolution (Windowed)': '1920x1080', 'Window Mode': 0}
    if not dlss:
        want['NVIDIA DLSS'] = 0
    if all(str(data.get(k)) == str(v) for k, v in want.items()):
        return
    data.update(want)
    settings.write_text(json.dumps(data, indent=4), encoding='utf-8')


def apple_shim(path):
    try:
        return path.is_file() and b'D3DMetalDLLsBase' in path.read_bytes()
    except OSError:
        return False


def dlss_bridge(m):
    """NVIDIA DLSS in the copy through Apple's NGX shim (as scripts/gptk-dlss.sh does for normal
    games), with Soju's nvngx.dll in front of it. The modded game runs under ReShade, whose
    D3D12 wrappers crash the shim when they reach it directly (hang at the title, 2026-11-25).
    True when ready; False on an engine without Apple's shim, where DLSS stays off."""
    win, unix = ENGINE / 'lib/wine/x86_64-windows', ENGINE / 'lib/wine/x86_64-unix'
    shim = next((p for p in (win / 'nvngx-on-metalfx.dll', win / 'nvngx.dll') if apple_shim(p)), None)
    unwrap = VR / 'dlss/nvngx.dll'
    spec = m.get('ngx_unwrap') or {}
    if (shim is None or not (ENGINE / 'lib/external/libd3dshared.dylib').is_file() or not unwrap.is_file()
            or digest(unwrap) != spec.get('sha256')):
        return False
    system32 = BOTTLE / 'drive_c/windows/system32'
    try:
        # The shim under its own name, loaded by the unwrapper. Wine finds its unixlib by that
        # name, so the engine gets the same copy and link next to its nvngx.dll.
        for target in (win / 'nvngx_metalfx.dll', system32 / 'nvngx_metalfx.dll'):
            if not target.is_file() or digest(target) != digest(shim):
                shutil.copy2(shim, target)
        link = unix / 'nvngx_metalfx.so'
        if (os.readlink(link) if link.is_symlink() else None) != '../../external/libd3dshared.dylib':
            link.unlink(missing_ok=True)
            link.symlink_to('../../external/libd3dshared.dylib')
        if not (system32 / 'nvngx.dll').is_file() or digest(system32 / 'nvngx.dll') != spec['sha256']:
            shutil.copy2(unwrap, system32 / 'nvngx.dll')
    except OSError:
        return False
    reg = BOTTLE / 'system.reg'
    try:
        has_key = '[Software\\\\NVIDIA Corporation\\\\Global\\\\NGXCore]' in reg.read_text(errors='replace')
    except OSError:
        return False
    if not has_key:
        wine(BOTTLE, ['reg', 'add', 'HKLM\\SOFTWARE\\NVIDIA Corporation\\Global\\NGXCore', '/v', 'FullPath',
                      '/t', 'REG_SZ', '/d', 'C:\\Windows\\System32', '/f'], capture_output=True)
    return True


def write_runtime_routing(m):
    runtime = VR / 'runtime'
    openxr = BOTTLE / 'drive_c/openxr'
    openxr.mkdir(parents=True, exist_ok=True)
    shutil.copy2(runtime / 'wine/x86_64-windows/wineopxdms.dll', openxr / 'wineopxdms.dll')
    (openxr / 'wineopxdms.json').write_text(json.dumps({
        'file_format_version': '1.0.0',
        'runtime': {'name': 'Soju wineopenxr (D3DMetal)', 'library_path': 'C:\\openxr\\wineopxdms.dll'},
    }, indent=2) + '\n')
    # The bridge applies these to its own (macOS) side when it loads; the
    # Windows OpenXR loader keeps reading the registry below.
    (VR / 'logs').mkdir(parents=True, exist_ok=True)
    (VR / 'bridge.env').write_text('XR_RUNTIME_JSON=' + str(runtime / 'runtime/oxrsys-runtime.json') + '\n'
                                   'OXR_BRIDGE_LOG=' + str(VR / 'logs/bridge.log') + '\n')
    wine(BOTTLE, ['reg', 'add', 'HKLM\\Software\\Khronos\\OpenXR\\1', '/v', 'ActiveRuntime', '/t', 'REG_SZ',
                  '/d', 'C:\\openxr\\wineopxdms.json', '/f'],
         stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True)


def builds(m):
    """The D2R builds setup accepts: the one the mod release was verified on, then the ones
    Soju checked it on since (manifest "extra_builds", each with what was checked)."""
    return [m['game_build']] + [b['build'] for b in m.get('extra_builds', [])]


def allow_installer_build(mod, build):
    """The mod's own installer (0.3.31) refuses every Battle.net build but 3.3.93847 before it
    installs anything. For a build listed in extra_builds, add it to that list in the extracted copy."""
    gate = mod / 'steamvr/game_install.py'
    text = read_raw(gate)
    old = "BATTLENET_VERSIONS = ('3.3.93847',)"
    if build and build not in text and old in text:
        write_raw(gate, text.replace(old, "BATTLENET_VERSIONS = ('" + build + "', '3.3.93847',)"))


def quiet_loader_warning(game, build):
    """D2RLoader 1.3.1 stops before the game on a D2R it has not been tested with ("This D2R version
    hasn't been fully tested yet", OK to go on), a window nobody in a headset can press. Its
    "Don't remind me again" choice is kept per major.minor version in d2rloader/data/loader-state.json;
    set it for a build listed in extra_builds."""
    parts = str(build or '').split('.')
    if len(parts) < 2:
        return
    version = parts[0] + '.' + parts[1] + '.0'
    state_file = game / 'd2rloader/data/loader-state.json'
    try:
        state = json.loads(state_file.read_text())
    except (OSError, ValueError):
        state = {}
    if not isinstance(state, dict):
        state = {}
    ignored = state.get('ignored_unsupported_versions')
    if not isinstance(ignored, list):
        ignored = []
    if version not in ignored:
        state['ignored_unsupported_versions'] = ignored + [version]
        state_file.parent.mkdir(parents=True, exist_ok=True)
        state_file.write_text(json.dumps(state, indent=2) + '\n')


def setup(refresh=False):
    m = manifest()
    if not (ENGINE / 'bin/wine').is_file():
        raise VRError('The Soju engine is not installed. Run: soju install')
    if not (SOURCE / GAME_REL / 'D2R.exe').is_file() and not (BOTTLE / GAME_REL / 'D2R.exe').is_file():
        raise VRError('Diablo II: Resurrected is not installed in the Battle.net bottle.')
    if running(['D2R.exe', 'D2RLoader.exe']):
        raise VRError('Close Diablo II: Resurrected first.')

    kept = VR / 'saves-kept'
    if refresh and BOTTLE.exists():
        # Check the new source first: a D2R update the mod does not support
        # must not cost the copy that still works.
        build = game_build(SOURCE)
        if build not in builds(m):
            raise VRError('Battle.net now has D2R ' + str(build) + '; D2R VR Unleashed supports '
                          + ', '.join(builds(m)) + '. Your current VR copy was kept.')
        if running(['Battle.net.exe']):
            raise VRError('Close Battle.net first, so the copy is consistent.')
        stop_bottle(BOTTLE)
        # Offline characters made in VR (and the VR video settings) live only
        # in the copy: keep them across the new copy.
        saves = BOTTLE / SAVES_REL
        if saves.is_dir() and not kept.exists():
            saves.rename(kept)
        say('Removing the old VR copy…')
        shutil.rmtree(BOTTLE)
        STATE.unlink(missing_ok=True)
    if not BOTTLE.exists():
        build = game_build(SOURCE)
        if build not in builds(m):
            raise VRError('D2R VR Unleashed supports D2R ' + ', '.join(builds(m)) + '; this install is '
                          + str(build) + '.')
        if running(['Battle.net.exe']):
            raise VRError('Close Battle.net first, so the copy is consistent.')
        say('Copying the Battle.net bottle (APFS copy, shares disk space until files change)…')
        VR.mkdir(parents=True, exist_ok=True)
        source = SOURCE.resolve()
        result = subprocess.run(['/bin/cp', '-c', '-R', str(source), str(BOTTLE)], capture_output=True, text=True)
        if result.returncode:
            shutil.rmtree(BOTTLE, ignore_errors=True)
            raise VRError('Could not make an APFS copy of the bottle (the bottle must be on an APFS volume): '
                          + result.stderr.strip())
        if kept.is_dir():
            saves = BOTTLE / SAVES_REL
            shutil.rmtree(saves, ignore_errors=True)
            saves.parent.mkdir(parents=True, exist_ok=True)
            kept.rename(saves)
            say('Kept the saves and settings of the previous VR copy.')
    elif game_build(BOTTLE) not in builds(m):
        raise VRError('The VR copy has D2R ' + str(game_build(BOTTLE)) + '; D2R VR Unleashed supports '
                      + ', '.join(builds(m)) + '.')

    state = {}
    try:
        state = json.loads(STATE.read_text())
    except (OSError, ValueError):
        pass
    if state.get('recipe') == recipe(m) and (BOTTLE / GAME_REL / 'D2RLoader.exe').is_file():
        say('D2R VR is already set up. Play with: soju vr play')
        return 0

    runtime_archive = fetch(m['runtime'], 'soju-vr-runtime.tar.xz', 'the VR runtime')
    python_zip = fetch(m['python'], 'python-embed-amd64.zip', 'Windows Python (python.org)')
    mod_zip = fetch(m['mod'], 'd2r-pcvr-windows.zip', m['mod']['name'])
    patched_plugin = fetch(m['mod_patch'], 'd2rl-pcvr-soju.dll', 'the VR character creation fix')
    ngx_unwrap = fetch(m['ngx_unwrap'], 'soju-nvngx.dll', 'the DLSS fix for the VR copy')
    loader_zip = fetch(m['loader'], m['loader']['file'], 'D2RLoader (d2rloader.net)')
    reshade_exe = fetch(m['reshade'], 'ReShade_Setup_Addon.exe', 'ReShade (reshade.me)')

    for name in ('runtime', 'python', 'mod', 'deps'):
        shutil.rmtree(VR / name, ignore_errors=True)
    safe_extract_tar(runtime_archive, VR / 'runtime')
    safe_extract_zip(python_zip, VR / 'python')
    safe_extract_zip(mod_zip, VR / 'mod')
    mod = VR / 'mod' / m['mod']['folder']
    allow_installer_build(mod, game_build(BOTTLE))
    stage_mod_patch(mod, patched_plugin, m['mod_patch'])
    deps = VR / 'deps'
    (deps / 'reshade-6.8').mkdir(parents=True)
    shutil.copy2(loader_zip, deps / m['loader']['file'])
    with zipfile.ZipFile(reshade_exe) as z:
        dll = z.read(m['reshade']['member'])
    if hashlib.sha256(dll).hexdigest() != m['reshade']['dll_sha256']:
        raise VRError('ReShade64.dll inside the ReShade setup did not match its pin.')
    (deps / 'reshade-6.8/ReShade64.dll').write_bytes(dll)
    # The embeddable Python only imports from its ._pth paths; add the mod's.
    (VR / 'python/python312._pth').write_text('python312.zip\r\n.\r\n' + winpath(mod / 'steamvr') + '\r\n')

    game = BOTTLE / GAME_REL
    off = game / 'd3d12.dll.soju-vr-off'
    if off.exists() and not (game / 'd3d12.dll').exists():
        off.rename(game / 'd3d12.dll')   # the installer checks the stock game first
    stop_bottle(BOTTLE)
    say('Installing D2R VR Unleashed into the VR copy…')
    args = [winpath(VR / 'python/python.exe'), winpath(mod / 'steamvr/install.py'),
            '--game', 'C:\\Program Files (x86)\\Diablo II Resurrected',
            '--saves', 'C:\\users\\crossover\\Saved Games\\Diablo II Resurrected',
            '--deps', winpath(deps), '--plugin', winpath(mod / 'bin/d2rl-pcvr.dll'),
            '--openxr-loader', winpath(mod / 'bin/openxr_loader.dll'), '--casclib', winpath(mod / 'bin/CascLib.dll'),
            '--backup-dir', winpath(VR / 'backups'), '--store', 'battlenet', '--experimental-battlenet']
    if (game / 'D2RLoader.exe').is_file():
        args.append('--preserve-settings')
    result = wine(BOTTLE, args, capture_output=True, text=True)
    log = VR / 'logs/install.log'
    log.parent.mkdir(parents=True, exist_ok=True)
    log.write_text(result.stdout + result.stderr)
    if result.returncode or 'Installed offline VR mod' not in result.stdout:
        tail = [line for line in (result.stdout + result.stderr).splitlines() if line.strip()][-3:]
        raise VRError('The mod installer stopped: ' + ' | '.join(tail) + ' (log: ' + str(log) + ')')

    shutil.copytree(mod / 'soju-source', game / 'mods' / m['mod']['mod_name'] / 'soju-source', dirs_exist_ok=True)
    patch_reshade(game / 'mods' / m['mod']['mod_name'] / 'reshade/ReShade64.dll', m['reshade'])
    if (game / 'd3d12.dll').exists():
        (game / 'd3d12.dll').rename(off)
    if game_build(BOTTLE) != m['game_build']:
        quiet_loader_warning(game, game_build(BOTTLE))
    (VR / 'dlss').mkdir(exist_ok=True)
    shutil.copy2(ngx_unwrap, VR / 'dlss/nvngx.dll')
    try:
        fix_settings(BOTTLE / SAVES_REL, dlss=dlss_bridge(m))
    except (OSError, ValueError):
        pass   # no Settings.json before the game's first start; play applies them again
    isolate_user_folders(BOTTLE)
    write_runtime_routing(m)
    stop_bottle(BOTTLE)
    STATE.write_text(json.dumps({'recipe': recipe(m), 'game_build': game_build(BOTTLE),
                                 'mod': m['mod']['name']}, indent=2) + '\n')
    say('D2R VR is set up. Play with: soju vr play')
    say('Connect the OXRSys headset/client or Simulator before soju vr play; tracking must be ready before the title screen.')
    say('Create an OFFLINE character in VR: stick-right + A selects New; stick + A chooses a class and types on the VR keyboard.')
    say('If D2R says you have not been online for 30 days: play D2R online once from Battle.net, '
        'then run: soju vr setup --refresh (VR characters are kept).')
    return 0


def status():
    m = manifest()
    state = {}
    try:
        state = json.loads(STATE.read_text())
    except (OSError, ValueError):
        pass
    return {
        'installed': BOTTLE.is_dir() and (BOTTLE / GAME_REL / 'D2RLoader.exe').is_file(),
        'current': state.get('recipe') == recipe(m),
        'game_build': game_build(BOTTLE) if BOTTLE.is_dir() else None,
        'supported_build': ', '.join(builds(m)),
        'source_build': game_build(SOURCE),
        'source_supported': game_build(SOURCE) in builds(m),
        'mod': m['mod']['name'],
        'path': str(VR),
    }


def stage_mod_patch(mod, plugin, spec):
    """Verify before replacing the extracted plugin; the installer backs up installed files."""
    if plugin.stat().st_size != spec['size'] or digest(plugin) != spec['sha256']:
        raise VRError('The VR character creation DLL does not match its pin.')
    source_patch = ROOT / spec['patch']
    if digest(source_patch) != spec['patch_sha256']:
        raise VRError('The VR character creation source patch does not match its pin.')
    shutil.copy2(plugin, mod / 'bin/d2rl-pcvr.dll')
    notice = mod / 'soju-source'
    notice.mkdir(exist_ok=True)
    shutil.copy2(source_patch, notice / source_patch.name)
    shutil.copy2(source_patch.parent / 'LICENSE', notice / 'LICENSE')
    (notice / 'SOURCE.txt').write_text(
        'D2R-PCVR modifications under MPL-2.0.\n'
        'Source: ' + spec['source_url'] + '\n'
        'Base commit: ' + spec['base_commit'] + '\n'
        'Apply the accompanying patch to this base.\n')


FLAT_PITCH, VR_PITCH = '-85', '-30'
VIEWS = {'classic': 1, 'third': 2, 'first': 3}   # the mod's flat views, F1 - F3


def set_ini(ini, want):
    """Set key = value in [section]s of an ini file the mod or ReShade reads, keeping its line
    endings; a key missing from its section is added at the section's end, a missing section at the end."""
    try:
        text = read_raw(ini)
    except OSError:
        return
    end = '\r\n' if '\r\n' in text else '\n'
    left = {sec: dict(keys) for sec, keys in want.items()}
    out, section = [], ''

    def flush():
        for key, value in left.pop(section, {}).items():
            out.append(key + '=' + value + end if ini.name == 'ReShade.ini' else key + ' = ' + value + end)

    for line in text.splitlines(keepends=True):
        stripped = line.strip()
        if stripped.startswith('['):
            if out and not out[-1].strip():
                blank = out.pop()
                flush()
                out.append(blank)
            else:
                flush()
            section = stripped
        elif section in left and '=' in stripped:
            key = stripped.split('=', 1)[0].strip()
            if key in left[section]:
                value = left[section].pop(key)
                line = (key + '=' + value if ini.name == 'ReShade.ini' else key + ' = ' + value) + end
        out.append(line)
    flush()
    for sec, keys in left.items():
        if keys:
            if out and out[-1].strip():
                out.append(end)
            out.append(sec + end)
            for key, value in keys.items():
                out.append((key + '=' + value if ini.name == 'ReShade.ini' else key + ' = ' + value) + end)
    new = ''.join(out)
    if new != text:
        write_raw(ini, new)


def set_mode(game, flat, view='first'):
    """The mod plays to the headset ([mode] platform = 1) or to the Mac screen (platform = 0), in
    the view flat_view names. Mac keyboards send F1 - F3 as brightness keys unless fn is held, so
    the view is chosen before the game starts. On the Mac screen in first or third person the mouse
    turns the view and W A S D walk ([input] mouse_look, off in the mod's own settings, where F9
    would turn it on); in the headset and in the game's own camera it stays off."""
    mod = game / 'mods' / 'D2R-PCVR'
    mode = {'platform': '0' if flat else '1'}
    if flat:
        mode['flat_view'] = str(VIEWS.get(view, 3))
    look = '1' if flat and view != 'classic' else '0'
    # [camera] pitch: the view's tilt from the game's own steep camera, more negative looking higher.
    # -30 (the mod's) suits a headset, where the head lifts it further; on the Mac screen it left first
    # person looking down at the hero's shoulders, and -85 looks ahead (checked 2026-11-25).
    pitch = FLAT_PITCH if flat else VR_PITCH
    set_ini(mod / 'd2rloader/plugins/d2r_pcvr.ini',
            {'[mode]': mode, '[input]': {'mouse_look': look}, '[camera]': {'pitch': pitch}})
    # ReShade's first-run line ("Press 'Home' to start the tutorial") otherwise stays on screen.
    set_ini(mod / 'reshade/ReShade.ini', {'[OVERLAY]': {'TutorialProgress': '4'}})


def play_env(flat, dlss=False):
    env = wine_env(BOTTLE)
    if dlss:
        env['D3DM_ENABLE_METALFX'] = '1'
        env['WINEDLLOVERRIDES'] = 'nvngx=n'   # Soju's nvngx.dll in system32, not the engine's shim
    if not flat:
        env['WINEDLLPATH'] = str(VR / 'runtime/wine')
        env['OXR_BRIDGE_ENV_FILE'] = str(VR / 'bridge.env')
    # flat: the OpenXR runtime cannot load, so the mod leaves VR off and the game
    # uses its normal screen, where the mouse can pick a class on character creation.
    # In VR, stick + A use D2R's native controller character picker.
    return env


def play(extra, flat=False, view='first'):
    info = status()
    if not info['installed'] or not info['current']:
        raise VRError('D2R VR is not set up for this Soju version. Run: soju vr setup')
    if running(['D2R.exe', 'D2RLoader.exe']):
        raise VRError('Diablo II: Resurrected is already running.')
    isolate_user_folders(BOTTLE)   # copies set up by 1.8.0/1.8.1 still have the links
    dlss = dlss_bridge(manifest())
    env = play_env(flat, dlss)
    set_mode(BOTTLE / GAME_REL, flat, view)
    try:
        fix_settings(BOTTLE / SAVES_REL, flat, dlss)
    except (OSError, ValueError):
        pass
    if flat:
        label = {'first': 'first person', 'third': 'third person', 'classic': "the game's own camera"}[view]
        say('Opening the VR copy on the Mac screen in ' + label + ': mouse look, W A S D walk. '
            'In the game fn+F1 / fn+F2 / fn+F3 switch between the game camera, third and first person.')
    if not flat:
        say('Connect the OXRSys headset/Simulator before the title screen. Use stick + A for offline character creation.')
        say('Use the VR name keyboard: stick + A types, X erases, Y changes case; select Create + A. B goes back.')
    os.chdir(BOTTLE / GAME_REL)
    # -mod keeps D2R offline: no Battle.net game servers.
    os.execve(str(ENGINE / 'bin/wine'), [str(ENGINE / 'bin/wine'), 'D2RLoader.exe', '-mod',
                                         manifest()['mod']['mod_name']] + extra, env)


def remove(yes):
    if not VR.exists():
        say('D2R VR is not set up.')
        return 0
    if not yes:
        raise VRError('This deletes ' + str(VR) + ' (offline characters made in VR included). '
                      'Run again with --yes to confirm.')
    if running(['D2R.exe', 'D2RLoader.exe']):
        raise VRError('Close Diablo II: Resurrected first.')
    if BOTTLE.exists():
        stop_bottle(BOTTLE)
    shutil.rmtree(VR)
    say('Removed ' + str(VR) + '. Your Battle.net bottle was not changed.')
    return 0


def main(argv):
    parser = argparse.ArgumentParser(prog='soju vr')
    sub = parser.add_subparsers(dest='command', required=True)
    p = sub.add_parser('setup')
    p.add_argument('--refresh', action='store_true', help='copy the Battle.net bottle again (after a game update)')
    p = sub.add_parser('play')
    p.add_argument('--flat', action='store_true',
                   help='play the VR copy on the Mac screen: mouse look and W A S D, no headset')
    p.add_argument('--view', choices=sorted(VIEWS), default='first',
                   help='with --flat: first or third person, or classic (the game\'s own camera)')
    p = sub.add_parser('status')
    p.add_argument('--json', action='store_true')
    p = sub.add_parser('remove')
    p.add_argument('--yes', action='store_true')
    args, extra = parser.parse_known_args(argv)
    try:
        if args.command == 'setup':
            return setup(args.refresh)
        if args.command == 'play':
            return play(extra, args.flat, args.view)
        if args.command == 'status':
            info = status()
            if args.json:
                print(json.dumps(info))
            else:
                say('D2R VR: ' + ('ready' if info['installed'] and info['current'] else 'not set up'))
                say('  game build in the VR copy: ' + str(info['game_build']) + ' (supported '
                    + info['supported_build'] + ')')
                say('  Battle.net bottle build: ' + str(info['source_build']))
            return 0
        if args.command == 'remove':
            return remove(args.yes)
    except VRError as error:
        print('soju vr: ' + str(error), file=sys.stderr)
        return 1
    except subprocess.CalledProcessError as error:
        print('soju vr: a step failed (' + ' '.join(map(str, error.cmd[:2])) + ')', file=sys.stderr)
        return 1
    return 0


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))