From c1b6fdd16f07e91313dc3c2ec821965a1aef15c4 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 22 Nov 2026 14:57:48 +0100 Subject: [PATCH] Map safe games to a FOSS Proton Install the pinned proton-cachyos-native build (signature and sha256 checked, .INSTALL never run), decide per game whether it is safe, write the CompatToolMapping as text, add --map-installed and --valve-proton, and drop steamcmd from install.sh. Co-Authored-By: Claude Opus 5.5 --- _installer_import/.pre-commit-config.yaml | 7 +- _installer_import/README.md | 92 ++++++-- _installer_import/install.sh | 43 +++- _installer_import/pyproject.toml | 4 +- _installer_import/run.sh | 5 +- _installer_import/scripts/deploy.sh | 2 +- .../steam_installer/anticheat.py | 57 +++++ _installer_import/steam_installer/appinfo.py | 1 - _installer_import/steam_installer/cli.py | 15 +- .../steam_installer/compat_apply.py | 74 ++++++ .../steam_installer/compat_decide.py | 108 +++++++++ .../steam_installer/compat_map.py | 208 +++++++++++++++++ _installer_import/steam_installer/config.py | 5 +- .../data/cachyos_signing_key.asc | 53 +++++ .../data/compat_overrides.json | 1 + .../steam_installer/data/proton-native.lock | 7 + .../steam_installer/finish_check.py | 2 +- _installer_import/steam_installer/flow.py | 17 +- .../steam_installer/map_installed.py | 125 +++++++++++ .../steam_installer/proton_native.py | 173 ++++++++++++++ .../steam_installer/proton_pkg.py | 208 +++++++++++++++++ .../steam_installer/recognize.py | 4 +- _installer_import/steam_installer/runlog.py | 12 +- _installer_import/steam_installer/target.py | 4 +- _installer_import/steam_installer/vdf.py | 4 +- _installer_import/tests/_proton_fixture.py | 121 ++++++++++ _installer_import/tests/conftest.py | 31 +++ _installer_import/tests/test_appinfo.py | 7 + _installer_import/tests/test_appinfo_cache.py | 4 +- _installer_import/tests/test_cli_finish.py | 4 +- _installer_import/tests/test_compat_decide.py | 164 ++++++++++++++ _installer_import/tests/test_compat_map.py | 198 ++++++++++++++++ _installer_import/tests/test_install_sh.py | 45 ++++ _installer_import/tests/test_map_installed.py | 197 ++++++++++++++++ _installer_import/tests/test_proton_native.py | 211 ++++++++++++++++++ .../tests/test_proton_native_cli.py | 58 +++++ _installer_import/tests/test_proton_pkg.py | 98 ++++++++ 37 files changed, 2303 insertions(+), 66 deletions(-) create mode 100644 _installer_import/steam_installer/anticheat.py create mode 100644 _installer_import/steam_installer/compat_apply.py create mode 100644 _installer_import/steam_installer/compat_decide.py create mode 100644 _installer_import/steam_installer/compat_map.py create mode 100644 _installer_import/steam_installer/data/cachyos_signing_key.asc create mode 100644 _installer_import/steam_installer/data/compat_overrides.json create mode 100644 _installer_import/steam_installer/data/proton-native.lock create mode 100644 _installer_import/steam_installer/map_installed.py create mode 100644 _installer_import/steam_installer/proton_native.py create mode 100644 _installer_import/steam_installer/proton_pkg.py create mode 100644 _installer_import/tests/_proton_fixture.py create mode 100644 _installer_import/tests/test_compat_decide.py create mode 100644 _installer_import/tests/test_compat_map.py create mode 100644 _installer_import/tests/test_install_sh.py create mode 100644 _installer_import/tests/test_map_installed.py create mode 100644 _installer_import/tests/test_proton_native.py create mode 100644 _installer_import/tests/test_proton_native_cli.py create mode 100644 _installer_import/tests/test_proton_pkg.py diff --git a/_installer_import/.pre-commit-config.yaml b/_installer_import/.pre-commit-config.yaml index ce3b75d..4cf791d 100644 --- a/_installer_import/.pre-commit-config.yaml +++ b/_installer_import/.pre-commit-config.yaml @@ -51,7 +51,9 @@ repos: types: [text] # Every dependency on its ecosystem's newest STABLE release, exact-pinned - # so the question is mechanically answerable at all. + # so the question is mechanically answerable at all. That includes the + # native Proton build, pinned in proton-native.lock and checked against + # cachyos.db by the shared gate's pacman-repo adapter. # # `files:` is what keeps this cheap: a commit touching only source makes # ZERO network requests, and manifest commits mostly hit a 6h disk cache. @@ -71,7 +73,8 @@ repos: |package\.json|package-lock\.json|pnpm-lock\.yaml |Cargo\.(toml|lock)|go\.(mod|sum) |\.fvmrc|\.nvmrc|\.python-version - |\.github/workflows/.*\.ya?ml)$ + |\.github/workflows/.*\.ya?ml + |steam_installer/data/proton-native\.lock)$ - id: repo-contract name: repo contract (CLAUDE.md Commands + test_changed) diff --git a/_installer_import/README.md b/_installer_import/README.md index c8eeab9..e7b6bd2 100644 --- a/_installer_import/README.md +++ b/_installer_import/README.md @@ -8,9 +8,11 @@ a real install so **Play** works. ./run.sh BeamNG.drive ``` -That is the whole thing. `run.sh` installs its own dependencies (steamcmd via -`yay`, DepotDownloader from the SteamRE release, and a local `.venv`) on first -use, then works out what you meant. All four of these install the same game: +That is the whole thing. `run.sh` installs its own dependencies (`libibus` via +pacman, DepotDownloader from the SteamRE release, a local `.venv` and the native +Proton described below) on first use, then works out what you meant. It never +runs `steamcmd`: app info comes from Steam's own cache and the tool writes the +manifest itself. All four of these install the same game: ```bash ./run.sh 284160 # an AppID @@ -25,8 +27,49 @@ name shows a numbered picker, and in a non-interactive run it lists the candidates and exits 2 rather than guessing. Useful flags: `--dry-run` (resolve and report, change nothing), `--yes` -(no prompts), `--repair-ghosts` (see below), `--username` (remembered after the -first run, in `~/.config/steam-game-installer/config`; never a password). +(no prompts), `--repair-ghosts` (see below), `--valve-proton` (keep this game on +Valve's Proton for this run), `--map-installed` (see below), `--username` +(remembered after the first run, in `~/.config/steam-game-installer/config`; +never a password). + +## Games run on a FOSS Proton + +A plain Windows game is mapped to `proton-cachyos-native`, a Proton that +CachyOS builds from source against the host's own libraries. Steam then runs +it with no Steam Linux Runtime container, and Proton Experimental, Steam Linux +Runtime 4.0 and the Steamworks Redistributables are not needed while only such +games are installed (steam-backlog-enforcer removes them then). + +- **The tool.** `install.sh` downloads the exact build pinned in + `steam_installer/data/proton-native.lock` from the CachyOS mirror. It checks + the package's `.sig` against the CachyOS key pinned in + `steam_installer/data/cachyos_signing_key.asc` (fingerprint and sources in + `proton_pkg.py`), and its sha256 against the lock. Then it extracts only the + tool folder into `compatibilitytools.d/` and writes a `VERSION` stamp; the + package's `.INSTALL` is never run. A copy already on disk is adopted when + every file matches the package's `.MTREE`. An update is swapped in by rename, + only while Steam and every game are closed. The dependency-freshness gate + checks the lock against `cachyos.db`. +- **Which games.** Only a game proven safe: Windows-only (the app, every depot, + every launch option), no shared depots, no owned DLC depot, not in + AreWeAntiCheatYet's `games.json` (fetched every run, last good copy cached; + no list means nothing is mapped), and not in + `steam_installer/data/compat_overrides.json` (`{"": ""}` for a + game that breaks on the native build). Anything else keeps Valve's default, + so it runs exactly as before. The run prints every reason. +- **The mapping.** With Steam closed, the game gets a `CompatToolMapping` + entry in `config/config.vdf` (`"name" "proton-cachyos-native"`, priority + 250). The file is edited as text, and none of its other lines is ever logged. + An entry Valve chose (priority 100) is replaced; one you picked is kept. +- **Games already installed.** `./run.sh --map-installed` prints the decision + for every installed game and writes nothing; add `--apply` to close Steam and + write the safe ones. `--appids 730,1201270` decides any cached app instead. + +Proven live on 2026-11-22 with A Space for the Unbound (1201270). Not tested +yet, so they stay on Valve's default or are fixture-tested only: a game with +shared depots (its from-scratch manifest records `SharedDepots`), an owned DLC +depot, a native Linux game, an anti-cheat game, a global default mapping and a +package update. **Steam gets closed for you.** Steam owns `appmanifest_.acf` while it runs and rewrites it from its own in-memory state, so anything written @@ -76,7 +119,8 @@ password nobody was there to type. ## Ghost installs -`steamcmd +login anonymous +app_update ` has no licence for the app. +Older versions of this tool ran steamcmd. `steamcmd +login anonymous +app_update +` has no licence for the app. It does not say so. It transfers nothing, prints `Success! App '...' fully installed.`, exits 0, and writes a manifest claiming `StateFlags=4` with `SizeOnDisk=0` and an empty `InstalledDepots`. @@ -111,17 +155,21 @@ manifest verbatim before and after every write, the depot cache listing, expected-vs-actual depots, and whether Steam was running. Send `latest.log` when something goes wrong. `--verbose` mirrors all of it to the console. -Subprocess output is teed through a pty rather than captured, because steamcmd -only prompts for a password / Steam Guard code when it believes it is on a -terminal — with a plain pipe the prompt never appears and the run looks hung. +Subprocess output is teed through a pty rather than captured, because +depotdownloader only prompts for a password / Steam Guard code when it believes +it is on a terminal — with a plain pipe the prompt never appears and the run looks hung. ## How it works -1. `depotdownloader` pulls the content (fast path). +1. App info (name, installdir, depots, build) is read from Steam's own cache, + `appcache/appinfo.vdf`. An app missing from it fails with the fix: start + Steam once, let it sync, rerun. A DLC missing from it counts as not owned. +2. `depotdownloader` pulls the content (fast path). Then each owned DLC with depots of its own gets a run of its own. -2. `steamcmd app_update validate` reconciles Steam's own bookkeeping. -3. If that fails or leaves the manifest incomplete, the manifest is finished by - hand — but only when every precondition below holds. +3. The manifest is finished by hand, or written from scratch with the 15 keys + Steam accepted when none exists, but only when every precondition below + holds. Steam adds `LastOwner`, `UserConfig` and the rest itself. +4. The game is mapped to the native Proton when it is safe (see above). ## Fail-closed preconditions @@ -132,25 +180,23 @@ The tool refuses to mark an app installed, and exits non-zero, unless: (required = this app's depots for the chosen language and OS, excluding DLC, optional and shared depots — KCD2 lists 12 depots but an english/windows install only ever fetches 3); -- the manifest carries a real `buildid` matching the current public build; +- every required depot holds the public gid from Steam's cache, which proves + the build (a differing gid means the cache is stale: start Steam, let it + sync, rerun), or else the manifest carries a matching real `buildid`; - no Steam client is running (it is closed first, and restarted afterwards); - the manifest reads back as a real install *after* being written. This matters: an earlier version wrote `StateFlags=4` and reported success after -a steamcmd run that had done nothing at all. +a steamcmd run that had done nothing at all. A from-scratch manifest is built in +memory and only written once every check has passed. ## Failure modes this encodes Each of these cost a real debugging session; the tests pin them. -**`force_install_dir` is a library root, not an install directory.** Pointing it -at the game folder made steamcmd look for manifests in `/steamapps/`, find -none (`Loaded 0 apps from install folder`), create that stray directory, and then -fail the app with `Invalid platform` — it had no depot context for the app. -Dropped entirely: the app already lives in a registered library. - -**A Linux steamcmd needs `+@sSteamCmdForcePlatformType windows`** (before -`+login`) for a Windows-only game, or it resolves depots against the host OS. +**A wrong `installdir` makes Steam wipe the game.** Steam treated the correctly +downloaded folder as orphaned, deleted it and started a fresh download into the +folder its manifest named. The field is fixed first, as soon as Steam is closed. **Steam rejects an internally inconsistent manifest.** A file that says `StateFlags=4` while `BytesDownloaded` (22 GB) still differs from diff --git a/_installer_import/install.sh b/_installer_import/install.sh index 2ae395e..ae43f40 100755 --- a/_installer_import/install.sh +++ b/_installer_import/install.sh @@ -3,13 +3,20 @@ # ============================================================================ # Install everything this tool shells out to, then set up the dev environment. # -# steamcmd AUR package, via yay +# libibus the one runtime library of proton-cachyos-native's 63 +# package dependencies a plain Arch desktop may lack (pacman) # depotdownloader official self-contained release from SteamRE/DepotDownloader # .venv local virtualenv with the package installed editable +# native Proton proton-cachyos-native, pinned in +# steam_installer/data/proton-native.lock, signature- and +# sha256-checked, into Steam's compatibilitytools.d +# +# steamcmd is not needed any more: app info comes from Steam's own cache and +# the manifest is written by the tool itself. # # Idempotent: every step no-ops when its target is already in place, so run.sh -# can call it on every run. `yay` shells out to `sudo pacman`, so installing -# steamcmd may prompt for a password -- that is the one interactive step. +# can call it on every run. Installing libibus runs `sudo -n pacman`; when sudo +# would prompt, it stops and prints the one command to type. # ============================================================================ set -euo pipefail @@ -45,19 +52,29 @@ die() { exit 1 } -ensure_steamcmd() { - if command -v steamcmd >/dev/null 2>&1; then +ensure_libibus() { + if pacman -Q libibus >/dev/null 2>&1; then return 0 fi if [[ "$CHECK_ONLY" -eq 1 ]]; then - echo "missing: steamcmd -- run: yay -S steamcmd" >&2 + echo "missing: libibus -- run: sudo pacman -S --needed libibus" >&2 return 1 fi - command -v yay >/dev/null 2>&1 || - die "steamcmd is missing and yay is not installed. Install it, then: yay -S steamcmd" - echo "Installing steamcmd (yay may ask for your sudo password)..." - yay -S --needed --noconfirm --answerdiff None --answeredit None steamcmd - command -v steamcmd >/dev/null 2>&1 || die "steamcmd still not on PATH after install" + sudo -n pacman -S --needed --noconfirm libibus || + die "libibus is missing and sudo needs a password. Run: sudo pacman -S --needed libibus" +} + +# Never runs the package's .INSTALL; see steam_installer/proton_native.py. +ensure_native_proton() { + local python="${VENV}/bin/python" + if [[ "$CHECK_ONLY" -eq 1 ]]; then + if [[ -x "$python" ]] && ! "$python" -m steam_installer.proton_native --check; then + echo "missing: proton-cachyos-native at the pinned version -- run ./install.sh" >&2 + return 1 + fi + return 0 + fi + "$python" -m steam_installer.proton_native || die "native Proton install failed" } # Build from source only if the published binary can't be fetched. The upstream @@ -137,15 +154,17 @@ setup_dev_env() { main() { local missing=0 - ensure_steamcmd || missing=1 + ensure_libibus || missing=1 ensure_depotdownloader || missing=1 if [[ "$CHECK_ONLY" -eq 1 ]]; then + ensure_native_proton || missing=1 [[ "$missing" -eq 0 ]] && echo "All dependencies present." exit "$missing" fi setup_dev_env + ensure_native_proton echo "All dependencies present." : >"$STAMP" } diff --git a/_installer_import/pyproject.toml b/_installer_import/pyproject.toml index 2e02155..697f19a 100644 --- a/_installer_import/pyproject.toml +++ b/_installer_import/pyproject.toml @@ -1,11 +1,11 @@ [project] name = "steam-game-installer" version = "0.1.0" -description = "Fast Steam game installs: depotdownloader for speed, steamcmd for recognition." +description = "Fast Steam game installs: depotdownloader for speed, a hand-written manifest for recognition, a FOSS Proton to run them." requires-python = ">=3.14" # kuhy's shared "Created by kuhy" credit, shown at the end of --help. dependencies = [ - "kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.0#subdirectory=kuhy_credit", + "kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.2#subdirectory=kuhy_credit", ] [project.scripts] diff --git a/_installer_import/run.sh b/_installer_import/run.sh index 27ff431..8d3aa73 100755 --- a/_installer_import/run.sh +++ b/_installer_import/run.sh @@ -20,6 +20,7 @@ readonly SCRIPT_DIR readonly VENV_PY="${SCRIPT_DIR}/.venv/bin/python" readonly STAMP="${SCRIPT_DIR}/.venv/.deps-ok" readonly INSTALLER="${SCRIPT_DIR}/install.sh" +readonly PROTON_LOCK="${SCRIPT_DIR}/steam_installer/data/proton-native.lock" SKIP_DEPS="${SGI_SKIP_DEPS:-0}" DEPS_ONLY=0 @@ -52,7 +53,9 @@ deps_are_current() { [[ -f "$STAMP" ]] || return 1 [[ "$STAMP" -nt "$INSTALLER" ]] || return 1 [[ "$STAMP" -nt "${SCRIPT_DIR}/pyproject.toml" ]] || return 1 - "$VENV_PY" -c 'import steam_installer' >/dev/null 2>&1 + [[ "$STAMP" -nt "$PROTON_LOCK" ]] || return 1 + # A bumped pin or a deleted tool folder: the stamp alone would miss both. + "$VENV_PY" -m steam_installer.proton_native --check >/dev/null 2>&1 } main() { diff --git a/_installer_import/scripts/deploy.sh b/_installer_import/scripts/deploy.sh index a5a19a1..ed075f1 100755 --- a/_installer_import/scripts/deploy.sh +++ b/_installer_import/scripts/deploy.sh @@ -3,7 +3,7 @@ # ============================================================================ # Deploy origin/main to where this tool runs: kuhy's checkout # (~/src/steam-game-installer, used through ./run.sh). Refuses a dirty tree, -# fast-forwards it, reruns ./run.sh --deps-only (steamcmd, depotdownloader, +# fast-forwards it, reruns ./run.sh --deps-only (libibus, depotdownloader, the native Proton, # editable .venv), then starts the CLI as the health check. Nothing is # published elsewhere: no package, no service, no release. # Idempotent; non-zero unless the checkout is at origin/main and the CLI starts. diff --git a/_installer_import/steam_installer/anticheat.py b/_installer_import/steam_installer/anticheat.py new file mode 100644 index 0000000..dc4eb1a --- /dev/null +++ b/_installer_import/steam_installer/anticheat.py @@ -0,0 +1,57 @@ +"""Steam app ids that use anti-cheat, from AreWeAntiCheatYet's `games.json`. + +Fetched on every run; the last good copy is kept so a flaky network does not +change any decision. With neither a fetch nor a copy the answer is "unknown", +and an unknown list maps nothing. Every listed game counts, whatever its +status: kuhy's library has 54 such games (CS2, Garry's Mod, TF2, ...), and VAC +runs inside Valve's client, so those stay on Valve's own setup. +""" + +from __future__ import annotations + +import json +import os +import urllib.request +from pathlib import Path + +URL = ( + "https://raw.githubusercontent.com/AreWeAntiCheatYet/AreWeAntiCheatYet/" + "HEAD/games.json" +) +CACHE = Path.home() / ".cache" / "steam-game-installer" / "anticheat-games.json" + + +def _download() -> bytes: + with urllib.request.urlopen(URL, timeout=20) as response: + return response.read() + + +def _ids(raw: bytes | str) -> set[int]: + """Steam ids in a games.json; ValueError/TypeError when it is not one.""" + games = json.loads(raw) + if not isinstance(games, list): + raise TypeError("games.json is not a list") + ids: set[int] = set() + for game in games: + stores = game.get("storeIds") if isinstance(game, dict) else None + steam = stores.get("steam") if isinstance(stores, dict) else None + if isinstance(steam, str) and steam.isdigit(): + ids.add(int(steam)) + return ids + + +def steam_ids(cache: Path = CACHE) -> set[int] | None: + """Anti-cheat Steam app ids, or None when no list can be had.""" + try: + raw = _download() + ids = _ids(raw) + except (OSError, ValueError, TypeError): + try: + return _ids(cache.read_bytes()) + except (OSError, ValueError, TypeError): + return None + cache.parent.mkdir(parents=True, exist_ok=True) + tmp = cache.with_name(f"{cache.name}.tmp{os.getpid()}") + tmp.write_bytes(raw) + os.replace(tmp, cache) + return ids diff --git a/_installer_import/steam_installer/appinfo.py b/_installer_import/steam_installer/appinfo.py index c53e961..bd7f014 100644 --- a/_installer_import/steam_installer/appinfo.py +++ b/_installer_import/steam_installer/appinfo.py @@ -6,7 +6,6 @@ from dataclasses import dataclass, field from steam_installer import appinfo_cache - # depotdownloader is never given -branch, so it always fetches this one. PUBLIC_BRANCH = "public" diff --git a/_installer_import/steam_installer/cli.py b/_installer_import/steam_installer/cli.py index 8275ce0..8712ae8 100644 --- a/_installer_import/steam_installer/cli.py +++ b/_installer_import/steam_installer/cli.py @@ -13,6 +13,7 @@ from steam_installer import ( deps, flow, ghost, + map_installed, resolve, runlog, takeover, @@ -70,6 +71,7 @@ def _build_parser() -> argparse.ArgumentParser: "steamcmd leaves behind, and clear them away." ), ) + map_installed.add_arguments(parser) parser.add_argument( "--verbose", action="store_true", @@ -82,8 +84,11 @@ def main(argv: list[str] | None = None) -> int: parser = _build_parser() args = parser.parse_args(argv) - if not args.repair_ghosts and args.appid is None and args.target is None: - parser.error("provide an AppID, a Steam URL, a game name, or --repair-ghosts") + if not (args.repair_ghosts or args.map_installed) and not (args.appid or args.target): + parser.error( + "provide an AppID, a Steam URL, a game name, --repair-ghosts or --map-installed" + ) + map_installed.check_arguments(parser, args) literal = target.parse_literal(args.target) if args.target else None log = runlog.setup(args.appid or (literal[0] if literal else None), args.verbose) @@ -163,6 +168,8 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: if args.repair_ghosts: return _repair_ghosts(log) + if args.map_installed: + return map_installed.run(args, log, find_steam_root()) try: deps.require() @@ -184,7 +191,8 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: resolved.source, ) - libraries = list_libraries(find_steam_root()) + steam_root = find_steam_root() + libraries = list_libraries(steam_root) install_target = pick_install_target(libraries, metadata.installdir) library = next(lib for lib in libraries if lib.common == install_target.parent) report = ghost.inspect(library, metadata.appid) @@ -223,6 +231,7 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int: return flow.install( args=args, log=log, + steam_root=steam_root, library=library, metadata=metadata, install_target=install_target, diff --git a/_installer_import/steam_installer/compat_apply.py b/_installer_import/steam_installer/compat_apply.py new file mode 100644 index 0000000..9e271a2 --- /dev/null +++ b/_installer_import/steam_installer/compat_apply.py @@ -0,0 +1,74 @@ +"""Run the per-game decision and, when it says so, write the mapping. + +Shared by the install flow (right after the manifest is finished, while Steam +is still closed) and by `--map-installed`. The decision rules live in +:mod:`compat_decide`, the config.vdf edit in :mod:`compat_map`. +""" + +from __future__ import annotations + +import logging +from pathlib import Path + +from steam_installer import anticheat, appinfo, compat_decide, compat_map, proton_pkg + +VALVE_PROTON = "--valve-proton" +TOOL_DIR = "compatibilitytools.d/proton-cachyos-native" + + +class Context: + """What every decision in one run shares: fetched once, not per game.""" + + def __init__(self, steam_root: Path, extra_overrides: dict[str, str] | None = None): + self.steam_root = steam_root + self.config = compat_map.config_path(steam_root) + self.anticheat = anticheat.steam_ids() + self.overrides = {**compat_decide.load_overrides(), **(extra_overrides or {})} + self.tool_ready = compat_decide.tool_ready( + steam_root / TOOL_DIR, proton_pkg.read_lock().version + ) + + def decide(self, appid: int, block: dict, owned_dlc: bool) -> compat_decide.Decision: + return compat_decide.decide( + appid, + block, + anticheat=self.anticheat, + overrides=self.overrides, + owned_dlc=owned_dlc, + tool_ready=self.tool_ready, + ) + + +def after_install( + steam_root: Path, + appid: int, + owned_dlc: bool, + valve_proton: bool, + logger: logging.Logger, +) -> None: + """Map a freshly installed game, or say why it keeps Valve's default. + + Never fails the install: a game left unmapped still runs on Valve's + Proton, exactly as before this tool knew about compat tools. + """ + extra = {str(appid): VALVE_PROTON} if valve_proton else {} + try: + context = Context(steam_root, extra) + decision = context.decide(appid, appinfo._fetch_app_block(appid), owned_dlc) + if not decision.map: + logger.info("Compat tool: %s", decision.describe()) + return + outcome = compat_map.map_app(context.config, appid) + except (appinfo.AppInfoError, compat_map.CompatMapError, OSError, ValueError) as exc: + logger.warning("Compat tool not set (%s); the game stays on Valve's default.", exc) + return + report(logger, appid, outcome) + + +def report(logger: logging.Logger, appid: int, outcome: compat_map.Outcome) -> None: + if outcome.action == "kept": + logger.info("Compat tool for %s kept: %s", appid, outcome.detail) + elif outcome.action == "already": + logger.info("Compat tool for %s: already mapped to %s.", appid, compat_map.TOOL) + else: + logger.info("Compat tool for %s: mapped to %s.", appid, compat_map.TOOL) diff --git a/_installer_import/steam_installer/compat_decide.py b/_installer_import/steam_installer/compat_decide.py new file mode 100644 index 0000000..d366c05 --- /dev/null +++ b/_installer_import/steam_installer/compat_decide.py @@ -0,0 +1,108 @@ +"""Decide per game whether Steam may run it on the native Proton. + +Only a game this can prove safe is mapped: Windows-only (the app, every depot +and every launch option), no shared depots, no owned DLC depot, not in the +anti-cheat list, not overridden. Anything else keeps Valve's default, so +Valve's tools come back while that game is installed -- every game stays +playable. When unsure, the answer is no. +""" + +from __future__ import annotations + +import json +from dataclasses import dataclass, field +from pathlib import Path + +OVERRIDES = Path(__file__).parent / "data" / "compat_overrides.json" + + +@dataclass +class Decision: + appid: int + reasons: list[str] = field(default_factory=list) + + @property + def map(self) -> bool: + return not self.reasons + + def describe(self) -> str: + return "map" if self.map else f"Valve default: {', '.join(self.reasons)}" + + +def load_overrides(path: Path = OVERRIDES) -> dict[str, str]: + """`{appid: reason}` for games that break on the native build.""" + data = json.loads(path.read_text()) + if not isinstance(data, dict) or not all( + key.isdigit() and isinstance(reason, str) for key, reason in data.items() + ): + raise ValueError(f"{path.name}: compat_overrides must map app ids to reasons") + return data + + +def tool_ready(tool_dir: Path, pinned_version: str) -> bool: + """The tool is there and is the exact pinned, verified build.""" + stamp = tool_dir / "VERSION" + return ( + (tool_dir / "toolmanifest.vdf").is_file() + and stamp.is_file() + and stamp.read_text().strip() == pinned_version + ) + + +def _oses(value: object) -> set[str]: + return {os for os in value.split(",") if os} if isinstance(value, str) else set() + + +def _dicts(section: object) -> list[dict]: + if not isinstance(section, dict): + return [] + return [entry for entry in section.values() if isinstance(entry, dict)] + + +def _config_oslist(entry: dict) -> set[str]: + config = entry.get("config") + return _oses(config.get("oslist")) if isinstance(config, dict) else set() + + +def _platform_reasons(block: dict) -> list[str]: + common = block.get("common") + app_oses = _oses(common.get("oslist")) if isinstance(common, dict) else set() + depots = _dicts(block.get("depots")) + config = block.get("config") + launches = _dicts(config.get("launch")) if isinstance(config, dict) else [] + oses = app_oses.union(*(_config_oslist(e) for e in depots + launches)) + reasons = [] + if not app_oses: + reasons.append("no OS list") + if "linux" in oses: + reasons.append("native") + if oses - {"windows", "linux"}: + reasons.append("not Windows-only") + if any("depotfromapp" in d or "sharedinstall" in d for d in depots): + reasons.append("shared depots") + return reasons + + +def decide( + appid: int, + block: dict, + *, + anticheat: set[int] | None, + overrides: dict[str, str], + owned_dlc: bool, + tool_ready: bool, +) -> Decision: + """Every reason `appid` must keep Valve's default; none means map it.""" + reasons: list[str] = [] + if anticheat is None: + reasons.append("anti-cheat list unavailable") + elif appid in anticheat: + reasons.append("anti-cheat") + reasons += _platform_reasons(block) + if owned_dlc: + reasons.append("owned DLC") + if str(appid) in overrides: + reasons.append(f"override: {overrides[str(appid)]}") + if not tool_ready: + reasons.append("native Proton not installed") + return Decision(appid, reasons) diff --git a/_installer_import/steam_installer/compat_map.py b/_installer_import/steam_installer/compat_map.py new file mode 100644 index 0000000..f87c351 --- /dev/null +++ b/_installer_import/steam_installer/compat_map.py @@ -0,0 +1,208 @@ +"""Map one game to the native Proton in Steam's `config/config.vdf`. + +The entry goes under InstallConfigStore/Software/Valve/Steam/CompatToolMapping, +the same shape Steam writes for a per-game choice in its own UI: + + "" { "name" "proton-cachyos-native" "config" "" "priority" "250" } + +Steam itself records Valve's per-game defaults at priority 100. On 2026-11-22 +Steam honoured a 250 entry: `compat_log.txt` named the native tool as the only +tool. A global default was never tested, so only per-game entries are written. + +The file is edited as text, never round-tripped through a parser: every other +byte stays as Steam wrote it. It also holds private values (tokens, account +ids), so no line of it is ever logged or put in an error message -- errors +carry line numbers only. Steam rewrites the file from memory on exit, so a +write while it runs would be lost; it is refused. +""" + +from __future__ import annotations + +import os +import re +import tempfile +from dataclasses import dataclass, field +from pathlib import Path + +from steam_installer import recognize + +TOOL = "proton-cachyos-native" +PRIORITY = "250" +VALVE_PRIORITY = "100" +_STEAM = ("installconfigstore", "software", "valve", "steam") +_MAPPING = (*_STEAM, "compattoolmapping") +# A quoted string (escapes allowed, newlines too), a brace, or anything else. +_TOKEN = re.compile(r'"(?:[^"\\]|\\.)*"|[{}]|[^\s"{}]+') + + +class CompatMapError(Exception): + pass + + +@dataclass +class Outcome: + action: str # "mapped", "already" or "kept" + detail: str = "" + + +@dataclass +class _Entry: + open_line: int # the `{` after the appid key + close_line: int = -1 + values: dict[str, str] = field(default_factory=dict) + + +@dataclass +class _Layout: + lines: list[str] + steam_open: int = -1 + mapping_open: int = -1 + entry: _Entry | None = None + + +def config_path(steam_root: Path) -> Path: + return steam_root / "config" / "config.vdf" + + +def current(config: Path, appid: int) -> tuple[str, str] | None: + """The (name, priority) the game is mapped to, or None when unmapped.""" + entry = _scan(_read(config), str(appid)).entry + if entry is None: + return None + return entry.values.get("name", ""), entry.values.get("priority", "") + + +def map_app(config: Path, appid: int) -> Outcome: + """Point `appid` at the native Proton unless kuhy chose another tool.""" + pids = recognize.steam_client_pids() + if pids: + raise CompatMapError( + "Steam is running and would overwrite config.vdf on exit -- close it first" + ) + layout = _scan(_read(config), str(appid)) + lines = layout.lines + entry = layout.entry + if entry is not None: + name = entry.values.get("name", "") + priority = entry.values.get("priority", "") + if name == TOOL: + return Outcome("already") + if name and priority != VALVE_PRIORITY: + return Outcome("kept", f"already mapped to {name} (priority {priority})") + key_line = entry.open_line - 1 + if lines[key_line].strip() != f'"{appid}"' or lines[entry.close_line].strip() != "}": + raise CompatMapError(f"config.vdf line {key_line + 1}: unexpected layout") + indent = _indent(lines[entry.open_line]) + lines[key_line : entry.close_line + 1] = _entry(appid, indent) + _write(config, lines) + return Outcome("mapped", f"replaced Valve's choice {name or '(empty)'}") + if layout.mapping_open >= 0: + indent = _indent(lines[layout.mapping_open]) + "\t" + lines[layout.mapping_open + 1 : layout.mapping_open + 1] = _entry(appid, indent) + else: + one = _indent(lines[layout.steam_open]) + "\t" + block = [f'{one}"CompatToolMapping"\n', f"{one}{{\n"] + block += [*_entry(appid, one + "\t"), f"{one}}}\n"] + lines[layout.steam_open + 1 : layout.steam_open + 1] = block + _write(config, lines) + return Outcome("mapped") + + +def _read(config: Path) -> list[str]: + try: + return config.read_text(encoding="utf-8").splitlines(keepends=True) + except OSError as exc: + raise CompatMapError(f"cannot read {config}") from exc + + +def _tokens(lines: list[str]) -> list[tuple[str, int, bool]]: + """(text, line index, is_quoted) for every token; quoted values may span lines.""" + text = "".join(lines) + starts = [0] + for line in lines: + starts.append(starts[-1] + len(line)) + out: list[tuple[str, int, bool]] = [] + line_no = 0 + for match in _TOKEN.finditer(text): + while starts[line_no + 1] <= match.start(): + line_no += 1 + token = match.group(0) + if token[0] == '"': + out.append((token[1:-1], line_no, True)) + elif token in "{}": + out.append((token, line_no, False)) + else: + raise CompatMapError(f"config.vdf line {line_no + 1}: unexpected layout") + return out + + +def _scan(lines: list[str], appid: str) -> _Layout: + """Find the Steam block, its mapping block and the app's entry.""" + layout = _Layout(lines) + stack: list[str] = [] + pending: str | None = None + for text, index, quoted in _tokens(lines): + in_entry = layout.entry is not None and tuple(stack) == (*_MAPPING, appid) + if quoted: + if pending is None: + pending = text + continue + if in_entry: + layout.entry.values[pending.lower()] = text + pending = None + elif text == "{": + if pending is None: + raise CompatMapError(f"config.vdf line {index + 1}: block without a key") + stack.append(pending.lower()) + pending = None + _note_open(layout, tuple(stack), appid, index) + else: + if not stack or pending is not None: + raise CompatMapError(f"config.vdf line {index + 1}: unbalanced brace") + if in_entry: + layout.entry.close_line = index + stack.pop() + if layout.steam_open < 0: + raise CompatMapError("config.vdf has no InstallConfigStore/Software/Valve/Steam") + return layout + + +def _note_open(layout: _Layout, path: tuple[str, ...], appid: str, index: int) -> None: + if path in (_STEAM, _MAPPING, (*_MAPPING, appid)) and layout.lines[index].strip() != "{": + raise CompatMapError(f"config.vdf line {index + 1}: unexpected layout") + if path == _STEAM: + layout.steam_open = index + elif path == _MAPPING: + layout.mapping_open = index + elif path == (*_MAPPING, appid): + layout.entry = _Entry(open_line=index) + + +def _indent(line: str) -> str: + return line[: len(line) - len(line.lstrip())] + + +def _entry(appid: int, indent: str) -> list[str]: + inner = indent + "\t" + return [ + f'{indent}"{appid}"\n', + f"{indent}{{\n", + f'{inner}"name"\t\t"{TOOL}"\n', + f'{inner}"config"\t\t""\n', + f'{inner}"priority"\t\t"{PRIORITY}"\n', + f"{indent}}}\n", + ] + + +def _write(config: Path, lines: list[str]) -> None: + """Replace the file atomically, keeping its permissions.""" + mode = config.stat().st_mode & 0o777 + fd, tmp = tempfile.mkstemp(dir=config.parent, prefix=".config.vdf.") + try: + with os.fdopen(fd, "w", encoding="utf-8") as handle: + handle.write("".join(lines)) + os.chmod(tmp, mode) + os.replace(tmp, config) + except BaseException: + Path(tmp).unlink(missing_ok=True) + raise diff --git a/_installer_import/steam_installer/config.py b/_installer_import/steam_installer/config.py index a86be4d..03b6233 100644 --- a/_installer_import/steam_installer/config.py +++ b/_installer_import/steam_installer/config.py @@ -1,9 +1,8 @@ """Remember the Steam username so every run doesn't ask for it again. Only the username is ever persisted. Passwords and Steam Guard codes stay -where they belong: with steamcmd's own cached login token (prompted through -the pty in `runlog.run_logged`) and depotdownloader's `-remember-password`. -Writing either of those to a dotfile would be a downgrade in security over +where they belong: with depotdownloader's `-remember-password` login token +(prompted through the pty in `runlog.run_logged`). Writing them to a dotfile would be a downgrade in security over what Steam's own tooling already does. """ diff --git a/_installer_import/steam_installer/data/cachyos_signing_key.asc b/_installer_import/steam_installer/data/cachyos_signing_key.asc new file mode 100644 index 0000000..90b0e88 --- /dev/null +++ b/_installer_import/steam_installer/data/cachyos_signing_key.asc @@ -0,0 +1,53 @@ +-----BEGIN PGP PUBLIC KEY BLOCK----- +Comment: Hostname: +Version: Hockeypuck 2.2 + +xsDNBGERxNwBDAD1aEVN8rdWBlV2sOwkiW1ddO44xKjdumwuGhoXP1pUzK4SkNUb +IrQ1cenckdxzZsS8cbXTvEteC4K8tTYxobZpgzAPhKZNECsyCyDf2DEYv7bS75gy +sdl0bh+mOXPjvOe9IK3eFPQWWFd0erYyf9SHjlZ0utOg3qZ2JuVupGOH8W3taKth +ijR98RzUqP5rPoMPKcrOdBUiIREHx0y6SwdyX0c2XKm52T3cP8O0MPsvZLbRwGU0 +DTyr7knzuGhR9tLxsy4PqKz3tSUEGM7PhFcpEoRFvV/4Al8/OpRaPj3zRn6RbL1D +9ZAH9OblgHkQlK6MPnLOygoaAvjreMHK3ykjlRugUtnRBmQHhH+o3iXb8Ep5GXjZ +XJZazy5ZSTbuJ8+DcjuC/G459ePnQ658od2Sxz5OBRmK3a1taAtgszHio1EHb8Db +OZ+Za5E46VOHM/8sl8toGbK4JOqpgdQ3YqIH4R915WqCYnMvfTQbCiHLYzWHrgAA +awBu3kaV2MagPnkAEQEAAc0bQ2FjaHlPUyA8YWRtaW5AY2FjaHlvcy5vcmc+wsEO +BBMBCAA4AhsDBQsJCAcCBhUKCQgLAgQWAgMBAh4BAheAFiEEiC3P5I4gUdSOJWKr +87YHSI2zWkcFAmTUdqcACgkQ87YHSI2zWkc2ygwAknD+O2LQeHp1tEYwplF9Rthq +uPN64IxC/8nAc+L92fSOkVJKUfRh0PJcCCZgpTBAa7UV++GsT9SZ8jRtUPDfAy/R +0TSwKkEC6b1291xNdV3O83CtkSJRTP39nVCefXrBUpIwmkQJuytBF1qypTuw4jom +orcbmE40O7uNDe0FpZOJkUxT1sVJqj+y2h4mJsDuEAYKVQa4809yDwlQOYlLg6i3 +wEBut7PPh8bEFWycNElKseh3dpQfvFVWMcUBafpQq7Q9zVFDW3nCUkFBMErcLb0y +VARQizrO3GqmWSU0zmgs8ruc3HnYjIfLIU68JRDsLdFgzIstEasnOskdUYywdvYw +BeGpCQX2ZFcTosLrZc1SRAk/XKioVXeHA92dcQacBClzKCmbat0CcNng5phu9fom +xGbNijvV5NWmQITw7j2sco0nLtfx2MiyRlR8s6pgrZ/VgfGq5K2gM306MgshgyMW +oIELm7aUTkbY+QH4v0y+U/KUNeHetDZhsMLwZq1DwsEUBBMBCAA+FiEEiC3P5I4g +UdSOJWKr87YHSI2zWkcFAmERxNwCGwMFCQPCZwAFCwkIBwIGFQoJCAsCBBYCAwEC +HgECF4AACgkQ87YHSI2zWkcB8QwA5+lumQa7SItzfd5v7HJhDU9IEE6gvxhs9wPC +UxcZmbCVj4AoP0yLXshbKpbwfpXQN+CmlnNr+U9TmUe0q6f6L7qdqIAfDGOy2BOX +Xn/x1h5u7jeppUYpSiB4oZW2A6zHuWp1RyGSiPFO0W+k5awA8nBNnZpnCdZIjT2R +OWCpSOpGgR8V35joq/tUxje/A1W7oaHuWnuSIz8nEqHQc9xTXbU4tFab24cxRxV+ +3U1wJ10jC0VoBPX2B7KhutgEIqwq9V2XOfRUAKO2u5IcKKwYFhjHa4O951rm+YoV +L2fj3BNeQwwwufofEHat4GhVXhvw+oZli3PMZ/9PZBf40/T5I4m+t+4nkpyyl3ng +Z8iW55gGKkcjgEmGBurmB+KuRbSBG6gQrYIfVvil+QFRVFh9yQJNKX7lzZpBv6M2 +tc1kC8N0jV2pTjlTwjDhn5xm3qC00FHLuvp6kNNDpnkYxYhVYR5PVLE1TOyPqL4L +J3I93lNfpef8p6jjBEJXi6C8PL+mzsDNBGERxNwBDAC1RE2JFOjAMzqg3f5JsTa5 +1WgApe2zlS/QjD+qr2LbC0CvQK3WO/MseqnEBfnXlUrL+tCzQXG79+kJk5xHsdaI +bblTqdX3xNbT0/+sJkLsUWbfLh1lTgs+NnltsCtg1cIKm/eOBEWyVfnrkKI+uTlQ +DDGcHW9XGaxLaIMi8FgaitMtrvH3AdajEpvpvdAAUbeIUL/hFs6VTSs74xJRxy0J +1eZYlZXo2YQ8EuZCAtyaQuvoDKijybxqj+FKPRiL920UI2emBYL0cwMRq1XylZST +7xXEzrEVgQ+xEUTBOUgmbRmIJVsSE/pDy7127dwPVpTglWiwfqntgdujrl6HM2b0 +a6lV5/ZoeYL5ucu6bku0ewbkoEKhqTe1JSVYWbStW5hwGFBxCFxGpKQMMsDltmst +8Z0Ic1Bhes00gd7LAC0AK8OkWFs4sIbHinNBVXQH/5MINF2ngm9tP9kym17Nr5Jt +erbxOOyNYl+ZOq6HFsrMXMZ1Cx+d4diqK2GnSeCZIvMAEQEAAcLA/AQYAQgAJhYh +BIgtz+SOIFHUjiViq/O2B0iNs1pHBQJhEcTcAhsMBQkDwmcAAAoJEPO2B0iNs1pH +mFMMAOuyEJ93J4X5H+Jnf8/pXg2PFVvp0se7weCFha1d9umGSTrQEjivm55z1X4R ++Jt6D/cL3wN1WnayyNEtVzTD1CiuMDNO4a+qyBmE3/tJRWyIXoZ/wdNu38rRNO/S +dNbDWj0dCl6HCokLKSlfpP7996zymNeVX7yCw7T56jb2mTILTszyE0xYPVAKb8cR +Zt9gUTivxQ3SPw5xVe1OXcF0Dkwx2EAAm7Alo6DMCZlqTii8Xc6KYcaSRHNdlmkk +W3RcTMkNja5mRFhzygklh9ViXqQs+9gZB+VHDqB2iNKbHJwO40aLRo/bta1h9P0H +DD8q8mZWkZpGBlzul0x5UMedCeYudk9tOMwFCKRrhZeAyzMRn/+FXUw7oFiWX6RX +9ujq0O+Yna6WsWe+YN/vmT0KloTzRb0yqeDc37zc850+woYy+mNpV2+Tq6hBDajk ++BohvFyI7T4vvs45e+GwN2fTS6oqVsc9TfO7Q8JTi5y9UqByEbNCbqY/IeIwaapR +IqDYsg== +=N+41 +-----END PGP PUBLIC KEY BLOCK----- diff --git a/_installer_import/steam_installer/data/compat_overrides.json b/_installer_import/steam_installer/data/compat_overrides.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/_installer_import/steam_installer/data/compat_overrides.json @@ -0,0 +1 @@ +{} diff --git a/_installer_import/steam_installer/data/proton-native.lock b/_installer_import/steam_installer/data/proton-native.lock new file mode 100644 index 0000000..9c7a349 --- /dev/null +++ b/_installer_import/steam_installer/data/proton-native.lock @@ -0,0 +1,7 @@ +# pacman-repo lock: the exact proton-cachyos-native build steam-game-installer +# installs. The dependency-freshness gate (kuhyx/utils, pacman-repo adapter) +# compares it with cachyos.db; bump version and sha256 together. +name=proton-cachyos-native +repo=https://mirror.cachyos.org/repo/x86_64/cachyos/cachyos.db +version=1:11.0.20261015-1 +sha256=25bf302f90e8e01118555b997b873c3cd06a33b5647e79d049a59e29794b43e0 diff --git a/_installer_import/steam_installer/finish_check.py b/_installer_import/steam_installer/finish_check.py index a455c02..fbcb644 100644 --- a/_installer_import/steam_installer/finish_check.py +++ b/_installer_import/steam_installer/finish_check.py @@ -87,7 +87,7 @@ def check_ready_to_finish( if not (buildid and buildid != "0") and not (target and target != "0"): problems.append( "manifest has no usable buildid (both buildid and TargetBuildID are 0) " - "-- steamcmd never recorded a real build" + "-- no real build was ever recorded" ) resolved = buildid if buildid and buildid != "0" else target if expected_build_id is None: diff --git a/_installer_import/steam_installer/flow.py b/_installer_import/steam_installer/flow.py index fb27e8d..848b6fd 100644 --- a/_installer_import/steam_installer/flow.py +++ b/_installer_import/steam_installer/flow.py @@ -10,6 +10,7 @@ from pathlib import Path from steam_installer import ( appinfo, + compat_apply, depotdl, dlc, manifest_write, @@ -50,6 +51,7 @@ def install( *, args, log: runlog.RunLog, + steam_root: Path, library: SteamLibrary, metadata: appinfo.AppMetadata, install_target: Path, @@ -64,9 +66,22 @@ def install( if steam_was_running is None: return 1 try: - return _download_and_recognize( + returncode = _download_and_recognize( args, log, library, metadata, install_target, staged, username, language ) + if returncode == 0: + # Still inside the window where Steam is closed: config.vdf written + # under a running client is lost when it exits. + cached = recognize.discover_cached_depot_manifests(install_target) + dlc_depots = appinfo.get_depot_plan(metadata.appid, language).dlc + compat_apply.after_install( + steam_root, + metadata.appid, + owned_dlc=bool(set(dlc_depots) & set(cached)), + valve_proton=args.valve_proton, + logger=logger, + ) + return returncode finally: if steam_was_running: steamctl.start(logger) diff --git a/_installer_import/steam_installer/map_installed.py b/_installer_import/steam_installer/map_installed.py new file mode 100644 index 0000000..8ed5731 --- /dev/null +++ b/_installer_import/steam_installer/map_installed.py @@ -0,0 +1,125 @@ +"""`--map-installed`: run the compat decision over every installed game. + + ./run.sh --map-installed # dry run: print, write nothing + ./run.sh --map-installed --apply # close Steam, map the safe ones + ./run.sh --map-installed --appids 730,10 # decide any cached app (read-only) + +The dry run only reads, so it works with Steam open. `--apply` closes Steam +first (it rewrites config.vdf on exit) and restarts it afterwards. +""" + +from __future__ import annotations + +import argparse +from pathlib import Path + +from steam_installer import ( + appinfo, + compat_apply, + compat_map, + flow, + recognize, + runlog, + steamctl, +) +from steam_installer.library import SteamLibrary, list_libraries + +# Steam's own compat tools and runtimes are apps too, but not games. +TOOL_TYPES = {"tool", "config"} + + +def add_arguments(parser: argparse.ArgumentParser) -> None: + parser.add_argument( + "--valve-proton", + action="store_true", + help="Keep this game on Valve's default Proton for this run.", + ) + parser.add_argument( + "--map-installed", + action="store_true", + help="Decide every installed game's compat tool. Dry run unless --apply.", + ) + parser.add_argument( + "--apply", action="store_true", help="With --map-installed: write the mappings." + ) + parser.add_argument( + "--appids", + default=None, + help="With --map-installed: comma-separated app ids to decide instead.", + ) + + +def check_arguments(parser: argparse.ArgumentParser, args: argparse.Namespace) -> None: + if args.map_installed and args.valve_proton: + parser.error("--valve-proton goes with an install, not --map-installed") + + +def _installed(libraries: list[SteamLibrary]) -> dict[int, dict]: + """appid -> AppState for every manifest in every library.""" + found: dict[int, dict] = {} + for library in libraries: + for manifest in sorted(library.steamapps.glob("appmanifest_*.acf")): + appid = manifest.stem.removeprefix("appmanifest_") + if appid.isdigit(): + found[int(appid)] = recognize.read_app_state(manifest) + return found + + +def _owned_dlc(app_state: dict) -> bool: + depots = app_state.get("InstalledDepots") + entries = depots.values() if isinstance(depots, dict) else [] + return any(isinstance(entry, dict) and "dlcappid" in entry for entry in entries) + + +def _is_tool(block: dict) -> bool: + common = block.get("common") + kind = common.get("type", "") if isinstance(common, dict) else "" + return isinstance(kind, str) and kind.lower() in TOOL_TYPES + + +def run(args: argparse.Namespace, log: runlog.RunLog, steam_root: Path) -> int: + logger = log.logger + installed = _installed(list_libraries(steam_root)) + if args.appids: + appids = [int(x) for x in args.appids.split(",") if x.strip().isdigit()] + else: + appids = sorted(installed) + context = compat_apply.Context(steam_root) + blocks = appinfo._cached_blocks(appids) + to_map: list[int] = [] + for appid in appids: + block = blocks.get(appid) + if block is None: + logger.info("%s: not in Steam's app cache -- skipped", appid) + continue + if _is_tool(block) and not args.appids: + continue + name = block.get("common", {}).get("name", "?") + decision = context.decide(appid, block, _owned_dlc(installed.get(appid, {}))) + line = f"{appid} {name}: {decision.describe()}" + if decision.map: + to_map.append(appid) + current = compat_map.current(context.config, appid) + if current and current[0] == compat_map.TOOL: + line += " -- already mapped" + logger.info("%s", line) + if not args.apply: + logger.info("Dry run: nothing written. Rerun with --apply to write.") + return 0 + return _apply(args, logger, context.config, to_map) + + +def _apply(args, logger, config, to_map: list[int]) -> int: + was_running = flow._prepare_steam(args, logger) + if was_running is None: + return 1 + try: + for appid in to_map: + compat_apply.report(logger, appid, compat_map.map_app(config, appid)) + except compat_map.CompatMapError as exc: + logger.error("Mapping stopped: %s", exc) + return 1 + finally: + if was_running: + steamctl.start(logger) + return 0 diff --git a/_installer_import/steam_installer/proton_native.py b/_installer_import/steam_installer/proton_native.py new file mode 100644 index 0000000..c8e42d1 --- /dev/null +++ b/_installer_import/steam_installer/proton_native.py @@ -0,0 +1,173 @@ +"""Install the pinned proton-cachyos-native build into Steam's compatibilitytools.d. + + python -m steam_installer.proton_native # what install.sh runs + python -m steam_installer.proton_native --check # exit 0 when in place + +The package comes straight from the CachyOS mirror: the `proton-cachyos-native` +entry of `cachyos.db`, its signature and sha256 checked (see :mod:`proton_pkg`), +and only `usr/share/steam/compatibilitytools.d/proton-cachyos-native/` taken +out of it. Its `.INSTALL` is never run. It is a WoW64 build that needs no +lib32 packages, and unlike the `-slr` variant its toolmanifest requires no +Steam Linux Runtime -- which is the point. + +A `VERSION` stamp equal to the lock means there is nothing to do and no +network is touched. Otherwise the new build is extracted next to the old one +and swapped in by rename -- never while Steam or a game from the tool runs, +since a game would lose its files mid-run. A copy already on disk with no +stamp is adopted as is when every file matches the package's `.MTREE`. + +`SGI_PROTON_REPO` points the fetch at another copy of the repo db (a local +test mirror); the pinned key and sha256 still decide what is accepted. +""" + +from __future__ import annotations + +import dataclasses +import os +import shutil +import subprocess +import sys +import tempfile +import urllib.request +from pathlib import Path + +from steam_installer import proton_pkg, recognize +from steam_installer.library import find_steam_root +from steam_installer.proton_pkg import FINGERPRINT, KEY, Lock, PkgError + +TOOL_NAME = "proton-cachyos-native" + + +def in_use(tool: Path) -> list[int]: + """Steam client pids plus every process running from the tool folder.""" + pids = list(recognize.steam_client_pids()) + root = tool.resolve() + for entry in recognize.PROC_DIR.iterdir(): + if not entry.name.isdigit(): + continue + try: + exe = (entry / "exe").resolve(strict=True) + except OSError: + continue + if exe.is_relative_to(root): + pids.append(int(entry.name)) + return pids + + +def _download(url: str, dest: Path) -> None: + with urllib.request.urlopen(url, timeout=60) as response, dest.open("wb") as out: + shutil.copyfileobj(response, out, 1 << 20) + + +def _fetch_verified(lock: Lock, work: Path, key: Path, fingerprint: str) -> Path: + """Download the pinned package and prove it, or raise PkgError.""" + db = work / "repo.db" + _download(lock.repo, db) + entry = proton_pkg.find_in_db(db, lock.name) + if entry.version != lock.version: + raise PkgError( + f"the repo serves {lock.name} {entry.version} but the lock pins " + f"{lock.version}: bump steam_installer/data/proton-native.lock " + "(version and sha256)" + ) + if entry.sha256 != lock.sha256: + raise PkgError("the repo's sha256 for the pinned version differs from the lock") + package = work / entry.filename + url = proton_pkg.package_url(lock, entry.filename) + _download(url, package) + sig = work / f"{entry.filename}.sig" + _download(f"{url}.sig", sig) + home = work / "gnupg" + home.mkdir(mode=0o700) + try: + proton_pkg.verify_signature(package, sig, key, fingerprint, home) + finally: + subprocess.run( + ["gpgconf", "--homedir", str(home), "--kill", "all"], + check=False, capture_output=True, + ) + if proton_pkg.sha256_file(package) != lock.sha256: + raise PkgError("the downloaded package's sha256 differs from the pinned sha256") + return package + + +def _stamp(tool: Path, version: str) -> None: + (tool / "VERSION").write_text(version + "\n") + + +def _swap(package: Path, tools_dir: Path, tool: Path, version: str) -> None: + new = tools_dir / f".{TOOL_NAME}.new" + old = tools_dir / f".{TOOL_NAME}.old" + for leftover in (new, old): + shutil.rmtree(leftover, ignore_errors=True) + new.mkdir() + proton_pkg.extract(package, new) + if tool.exists(): + tool.rename(old) + new.rename(tool) + shutil.rmtree(old, ignore_errors=True) + _stamp(tool, version) + + +def ensure( + tools_dir: Path, lock: Lock, *, key: Path = KEY, fingerprint: str = FINGERPRINT +) -> str: + """Make `tools_dir/proton-cachyos-native` the pinned build; return what happened.""" + tool = tools_dir / TOOL_NAME + stamp = tool / "VERSION" + current = stamp.read_text().strip() if stamp.is_file() else None + if current == lock.version: + return f"current: {lock.version}" + tools_dir.mkdir(parents=True, exist_ok=True) + work = Path(tempfile.mkdtemp(dir=tools_dir, prefix=".proton-download.")) + try: + package = _fetch_verified(lock, work, key, fingerprint) + checks = f"signature by {fingerprint} and sha256 both passed" + if not tool.exists(): + verb = f"installed: {lock.version}" + elif current is not None: + verb = f"updated: {current} -> {lock.version}" + else: + entries = proton_pkg.read_mtree(package) + mismatch = proton_pkg.first_mismatch(tool, entries) + if mismatch is None: + _stamp(tool, lock.version) + count = sum(entry.kind != "dir" for entry in entries.values()) + return f"adopted: all {count} files match .MTREE ({checks})" + verb = f"replaced: mismatch in {mismatch}" + busy = in_use(tool) if tool.exists() else [] + if busy: + return ( + f"deferred: Steam or a game is running (pid {', '.join(map(str, busy))}); " + "rerun with Steam closed" + ) + _swap(package, tools_dir, tool, lock.version) + return f"{verb} ({checks})" + finally: + shutil.rmtree(work, ignore_errors=True) + + +def main(argv: list[str] | None = None) -> int: + """Install the pinned build; with `--check`, only say whether it is in place.""" + lock = proton_pkg.read_lock() + if os.environ.get("SGI_PROTON_REPO"): + lock = dataclasses.replace(lock, repo=os.environ["SGI_PROTON_REPO"]) + try: + tools_dir = find_steam_root() / "compatibilitytools.d" + except FileNotFoundError: + print("native Proton skipped: no Steam installation") + return 0 + if argv and argv[0] == "--check": + stamp = tools_dir / TOOL_NAME / "VERSION" + return 0 if stamp.is_file() and stamp.read_text().strip() == lock.version else 1 + try: + status = ensure(tools_dir, lock) + except (PkgError, OSError) as exc: + print(f"error: native Proton: {exc}", file=sys.stderr) + return 1 + print(f"native Proton {status}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main(sys.argv[1:])) diff --git a/_installer_import/steam_installer/proton_pkg.py b/_installer_import/steam_installer/proton_pkg.py new file mode 100644 index 0000000..74ab6e8 --- /dev/null +++ b/_installer_import/steam_installer/proton_pkg.py @@ -0,0 +1,208 @@ +"""The CachyOS package formats and the two checks every download must pass. + +The repo database is not signed, so nothing in it is trusted on its own: +- the package's detached `.sig` must verify against the CachyOS key pinned in + `data/cachyos_signing_key.asc`, by primary-key fingerprint, in a throwaway + gpg home that holds that key and nothing else; +- the package's sha256 must equal both the db entry and the pinned lock. +Either failing stops the install before anything is extracted. + +Pinned key: 882DCFE48E2051D48E2562ABF3B607488DB35A47 ("CachyOS +"). The .sig names its own issuer, so it could not vouch +for the pin. Taken on 2026-11-22 from two other sources instead: +https://keyserver.ubuntu.com/pks/lookup?op=get&search=0x882DCFE48E2051D48E2562ABF3B607488DB35A47 +(the key file) and the long id F3B607488DB35A47 that CachyOS's own installer +imports, https://github.com/CachyOS/cachyos-repo-add-script/blob/5b27dc1eff276e104077cfaf75798b4c290eaef4/cachyos-repo.sh#L156 +""" + +from __future__ import annotations + +import gzip +import hashlib +import re +import subprocess +import tarfile +from dataclasses import dataclass +from pathlib import Path +from urllib.parse import quote + +DATA = Path(__file__).parent / "data" +LOCK = DATA / "proton-native.lock" +KEY = DATA / "cachyos_signing_key.asc" +FINGERPRINT = "882DCFE48E2051D48E2562ABF3B607488DB35A47" +PREFIX = "usr/share/steam/compatibilitytools.d/proton-cachyos-native/" + + +class PkgError(Exception): + pass + + +@dataclass(frozen=True) +class Lock: + name: str + repo: str # URL of the .db + version: str + sha256: str + + +@dataclass(frozen=True) +class DbEntry: + filename: str + version: str + sha256: str + + +@dataclass(frozen=True) +class MtreeEntry: + kind: str # "file", "link" or "dir" + sha256: str = "" + link: str = "" + + +def read_lock(path: Path = LOCK) -> Lock: + fields = {} + for line in path.read_text().splitlines(): + if line.strip() and not line.lstrip().startswith("#"): + key, _, value = line.partition("=") + fields[key.strip()] = value.strip() + try: + return Lock(fields["name"], fields["repo"], fields["version"], fields["sha256"]) + except KeyError as exc: + raise PkgError(f"{path.name} has no {exc.args[0]}=") from exc + + +def package_url(lock: Lock, filename: str) -> str: + """The package sits next to the db; the epoch colon must be escaped.""" + return f"{lock.repo.rsplit('/', 1)[0]}/{quote(filename)}" + + +def find_in_db(db: Path, name: str) -> DbEntry: + """The db entry whose %NAME% is exactly `name` (not `-v3`).""" + with tarfile.open(db, "r:*") as tar: + for member in tar: + if not member.name.endswith("/desc"): + continue + fields = _desc(tar.extractfile(member).read().decode()) + if fields.get("NAME") == name: + return DbEntry(fields["FILENAME"], fields["VERSION"], fields["SHA256SUM"]) + raise PkgError(f"the repo database lists no {name}") + + +def _desc(text: str) -> dict[str, str]: + fields: dict[str, str] = {} + for block in text.strip().split("\n\n"): + head, _, value = block.partition("\n") + fields[head.strip("%")] = value.strip() + return fields + + +def sha256_file(path: Path) -> str: + digest = hashlib.sha256() + with path.open("rb") as handle: + for chunk in iter(lambda: handle.read(1 << 20), b""): + digest.update(chunk) + return digest.hexdigest() + + +def _gpg(home: Path, *args: str) -> subprocess.CompletedProcess: + return subprocess.run( + ["gpg", "--homedir", str(home), "--batch", "--no-tty", *args], + capture_output=True, text=True, check=False, timeout=60, + ) + + +def key_fingerprints(key: Path, home: Path) -> list[str]: + """Primary-key fingerprints in `key`, imported into the empty `home`.""" + _gpg(home, "--import", str(key)) + listing = _gpg(home, "--with-colons", "--fingerprint").stdout.splitlines() + out, after_pub = [], False + for line in listing: + if line.startswith("pub"): + after_pub = True + elif line.startswith("fpr") and after_pub: + out.append(line.split(":")[9]) + after_pub = False + return out + + +def verify_signature(package: Path, sig: Path, key: Path, fingerprint: str, home: Path) -> None: + """Fail unless `sig` is a good signature by the pinned primary key.""" + if fingerprint not in key_fingerprints(key, home): + raise PkgError("the pinned key file does not hold the pinned fingerprint") + result = _gpg(home, "--status-fd", "1", "--verify", str(sig), str(package)) + signers = [ + line.split()[-1] + for line in result.stdout.splitlines() + if line.startswith("[GNUPG:] VALIDSIG ") + ] + if result.returncode != 0 or signers != [fingerprint]: + raise PkgError(f"package signature does not verify against {fingerprint}") + + +def _unescape(path: str) -> str: + return re.sub(r"\\([0-7]{3})", lambda m: chr(int(m.group(1), 8)), path) + + +def read_mtree(package: Path) -> dict[str, MtreeEntry]: + """The package's own file list for the tool folder, relative to it.""" + with tarfile.open(package, "r:zst") as tar: + for member in tar: + if member.name == ".MTREE": + text = gzip.decompress(tar.extractfile(member).read()).decode() + break + else: + raise PkgError("package has no .MTREE") + entries: dict[str, MtreeEntry] = {} + defaults: dict[str, str] = {} + for line in text.splitlines(): + if not line or line.startswith("#"): + continue + path, *pairs = line.split(" ") + values = dict(pair.partition("=")[::2] for pair in pairs) + if path == "/set": + defaults.update(values) + continue + path = _unescape(path.removeprefix("./")) + if not path.startswith(PREFIX) or path == PREFIX: + continue + values = {**defaults, **values} + entries[path[len(PREFIX):]] = MtreeEntry( + values.get("type", "file"), values.get("sha256digest", ""), values.get("link", "") + ) + return entries + + +def first_mismatch(folder: Path, entries: dict[str, MtreeEntry]) -> str | None: + """The first path where `folder` differs from the package, or None. + + Files the package does not list count as a difference (a stray + user_settings.py changes how Proton runs), except Python's own + `__pycache__`, which running protonfixes creates. + """ + for rel, entry in sorted(entries.items()): + path = folder / rel + if entry.kind == "link": + if not path.is_symlink() or str(path.readlink()) != entry.link: + return rel + elif entry.kind == "file" and ( + path.is_symlink() or not path.is_file() or sha256_file(path) != entry.sha256 + ): + return rel + for path in sorted(folder.rglob("*")): + if path.is_dir() and not path.is_symlink(): + continue + rel = str(path.relative_to(folder)) + if rel not in entries and "__pycache__" not in path.parts and rel != "VERSION": + return rel + return None + + +def extract(package: Path, dest: Path) -> None: + """Unpack only the tool folder into `dest`; scripts are never run.""" + with tarfile.open(package, "r:zst") as tar: + members = [] + for member in tar.getmembers(): + if member.name.startswith(PREFIX) and member.name != PREFIX: + member.name = member.name[len(PREFIX):] + members.append(member) + tar.extractall(dest, members=members, filter="data") diff --git a/_installer_import/steam_installer/recognize.py b/_installer_import/steam_installer/recognize.py index 25f6df6..a2a9a51 100644 --- a/_installer_import/steam_installer/recognize.py +++ b/_installer_import/steam_installer/recognize.py @@ -1,7 +1,7 @@ """Read-only questions about whether Steam will see an install as real. -Split on 2026-09-25 (250-line cap): steamcmd's validate run now lives in -:mod:`steamcmd_validate`, every manifest write in :mod:`manifest_write`, and +Split on 2026-09-25 (250-line cap): every manifest write lives in +:mod:`manifest_write`, and the preconditions for writing in :mod:`finish_check`. What stays here is the shared vocabulary they all read: depotdownloader's cache, Steam's staging tree and client process, and whether a manifest's bookkeeping says "done". diff --git a/_installer_import/steam_installer/runlog.py b/_installer_import/steam_installer/runlog.py index d3741de..ebcd38d 100644 --- a/_installer_import/steam_installer/runlog.py +++ b/_installer_import/steam_installer/runlog.py @@ -8,10 +8,10 @@ log records the manifest *verbatim, before and after* every write, alongside the full output of every subprocess. Subprocess output is teed through a pty rather than captured with -`capture_output=True`. steamcmd prompts for a password (and possibly a Steam -Guard code) on stdin; prompt text written to a captured/buffered pipe never +`capture_output=True`. depotdownloader prompts for a password (and possibly a +Steam Guard code) on stdin; prompt text written to a captured/buffered pipe never reaches the terminal, so the process looks hung with no visible request for -input. A pty keeps steamcmd in line-buffered interactive mode and lets us +input. A pty keeps it in line-buffered interactive mode and lets us mirror every byte to both the terminal and the log. """ @@ -110,7 +110,7 @@ def run_logged( """Run `cmd` on a pty, mirroring output to both the terminal and the log. Returns (returncode, combined_output). stdin stays attached to the real - terminal so interactive prompts (steamcmd's password / Steam Guard) work + terminal so interactive prompts (depotdownloader's password / Steam Guard) work exactly as they do without logging. """ logger.debug("$ %s", shlex.join(cmd)) @@ -139,7 +139,7 @@ def run_logged( except subprocess.TimeoutExpired as exc: # Record what the child managed to say before it hung, then re-raise. # Losing it here defeats the whole point of the log in precisely the - # case where it matters most -- a steamcmd hang at the 3600s timeout. + # case where it matters most -- a download hung until its timeout. output = _log_output(logger, chunks, started, returncode=None) process.wait() exc.output = output.encode() @@ -177,7 +177,7 @@ def _read_pty(fd: int, size: int) -> bytes: def _stdin_for_child(slave_fd: int) -> int: """The child's stdin: the real terminal when there is one, else the pty. - steamcmd needs a readable stdin to prompt for a password, but `sys.stdin` + depotdownloader needs a readable stdin to prompt for a password, but `sys.stdin` is not always a real file descriptor -- under pytest's capture, systemd, or a piped invocation it has no fileno(). Falling back to the pty's slave end keeps the child on a terminal-like stdin instead of crashing. diff --git a/_installer_import/steam_installer/target.py b/_installer_import/steam_installer/target.py index 6d956ba..3bf6ec2 100644 --- a/_installer_import/steam_installer/target.py +++ b/_installer_import/steam_installer/target.py @@ -8,8 +8,8 @@ Four accepted forms, all resolving to the same app: BeamNG.drive BeamNG -Whatever the input, the appid is always confirmed through steamcmd's own -`app_info_print` before anything acts on it, so the name and installdir used +Whatever the input, the appid is always confirmed against Steam's own app +cache (`appcache/appinfo.vdf`) before anything acts on it, so the name and installdir used downstream come from Steam rather than from a search index that ranks "Portal 2" above "Portal". """ diff --git a/_installer_import/steam_installer/vdf.py b/_installer_import/steam_installer/vdf.py index b73bec3..850680b 100644 --- a/_installer_import/steam_installer/vdf.py +++ b/_installer_import/steam_installer/vdf.py @@ -1,7 +1,7 @@ """Minimal reader/writer for Valve's KeyValues (VDF) text format. -Steam uses this format for appmanifest_*.acf, libraryfolders.vdf, and -steamcmd's `app_info_print` output. Only the plain-text variant is +Steam uses this format for appmanifest_*.acf and libraryfolders.vdf. +(Its binary cousin, appcache/appinfo.vdf, is read by :mod:`appinfo_cache`.) Only the plain-text variant is supported (no binary VDF) -- that covers every file this project touches. """ diff --git a/_installer_import/tests/_proton_fixture.py b/_installer_import/tests/_proton_fixture.py new file mode 100644 index 0000000..dec074c --- /dev/null +++ b/_installer_import/tests/_proton_fixture.py @@ -0,0 +1,121 @@ +"""A tiny fake CachyOS repo: a signed proton package, its db and the signing key. + +Built for real (zstd tars, a gzip .MTREE, a gpg detached signature from a +throwaway ed25519 key) so the fetcher's checks run against the same formats +the real mirror serves. +""" + +from __future__ import annotations + +import gzip +import hashlib +import io +import subprocess +import tarfile +from dataclasses import dataclass +from pathlib import Path + +PREFIX = "usr/share/steam/compatibilitytools.d/proton-cachyos-native/" +VERSION = "1:11.0.20261015-1" +FILENAME = f"proton-cachyos-native-{VERSION}-x86_64.pkg.tar.zst" +FILES = { + "toolmanifest.vdf": b'"manifest"\n{\n "commandline" "/proton %verb%"\n}\n', + "proton": b"#!/usr/bin/env python3\n", + "files/bin/wine": b"\x7fELF wine", + "user settings.py": b"# a name with a space\n", +} +LINKS = {"files/bin/msidb": "wine"} + + +@dataclass +class Repo: + root: Path # served as the mirror: holds cachyos.db, the package, the .sig + key: Path + fingerprint: str + sha256: str + gpg_home: Path + + @property + def db_url(self) -> str: + return (self.root / "cachyos.db").as_uri() + + @property + def package(self) -> Path: + return self.root / FILENAME + + +def _mtree() -> bytes: + lines = ["#mtree", "/set type=file uid=0 gid=0 mode=644"] + lines.append("./.INSTALL time=1.0 size=3 sha256digest=00") + lines.append(f"./{PREFIX.rstrip('/')} time=1.0 mode=755 type=dir") + for name, data in FILES.items(): + digest = hashlib.sha256(data).hexdigest() + path = (PREFIX + name).replace(" ", "\\040") + lines.append(f"./{path} time=1.0 size={len(data)} sha256digest={digest}") + for name, target in LINKS.items(): + lines.append(f"./{PREFIX}{name} time=1.0 mode=777 type=link link={target}") + return gzip.compress(("\n".join(lines) + "\n").encode()) + + +def _add(tar: tarfile.TarFile, name: str, data: bytes) -> None: + info = tarfile.TarInfo(name) + info.size = len(data) + tar.addfile(info, io.BytesIO(data)) + + +def build_package(path: Path, files: dict[str, bytes] = FILES) -> None: + with tarfile.open(path, "w:zst") as tar: + _add(tar, ".INSTALL", b"rm -rf /\n") # never run, never extracted + _add(tar, ".MTREE", _mtree()) + _add(tar, "usr/bin/stray", b"outside the tool folder") + for name, data in files.items(): + _add(tar, PREFIX + name, data) + for name, target in LINKS.items(): + info = tarfile.TarInfo(PREFIX + name) + info.type = tarfile.SYMTYPE + info.linkname = target + tar.addfile(info) + + +def build_db(path: Path, version: str, sha256: str, name: str = "proton-cachyos-native") -> None: + desc = ( + f"%FILENAME%\n{name}-{version}-x86_64.pkg.tar.zst\n\n%NAME%\n{name}\n\n" + f"%VERSION%\n{version}\n\n%SHA256SUM%\n{sha256}\n\n" + ).encode() + other = b"%NAME%\nproton-cachyos-native-v3\n\n%VERSION%\n9:9-9\n\n" + with tarfile.open(path, "w:zst") as tar: + _add(tar, "proton-cachyos-native-v3-9:9-9/desc", other) + _add(tar, f"{name}-{version}/desc", desc) + + +def gpg(home: Path, *args: str) -> None: + subprocess.run( + ["gpg", "--homedir", str(home), "--batch", "--passphrase", "", *args], + check=True, + capture_output=True, + ) + + +def build_repo(tmp: Path) -> Repo: + root = tmp / "mirror" + root.mkdir() + home = tmp / "signer" + home.mkdir(mode=0o700) + gpg(home, "--quick-gen-key", "Fake CachyOS ", "ed25519", "sign", "never") + listing = subprocess.run( + ["gpg", "--homedir", str(home), "--with-colons", "--fingerprint"], + check=True, capture_output=True, text=True, + ).stdout + fingerprint = next(line.split(":")[9] for line in listing.splitlines() if line.startswith("fpr")) + key = tmp / "key.asc" + gpg(home, "--armor", "--output", str(key), "--export", fingerprint) + package = root / FILENAME + build_package(package) + gpg(home, "--output", f"{package}.sig", "--detach-sign", str(package)) + sha256 = hashlib.sha256(package.read_bytes()).hexdigest() + build_db(root / "cachyos.db", VERSION, sha256) + return Repo(root, key, fingerprint, sha256, home) + + +def stop_agent(home: Path) -> None: + subprocess.run(["gpgconf", "--homedir", str(home), "--kill", "all"], check=False) diff --git a/_installer_import/tests/conftest.py b/_installer_import/tests/conftest.py index 0956df6..79abb43 100644 --- a/_installer_import/tests/conftest.py +++ b/_installer_import/tests/conftest.py @@ -194,3 +194,34 @@ def steam(tmp_path, monkeypatch): # Prompting is tty-gated now, and pytest's stdin is not a tty. monkeypatch.setattr(cli, "_is_interactive", lambda args: True) return library, install_dir + + +@pytest.fixture(autouse=True) +def _no_live_compat_side_effects(request, tmp_path, monkeypatch): + """No test may fetch the anti-cheat list or read the real app cache. + + The modules that test those seams themselves (with their own fakes) are + exempt from the part they cover. + """ + from steam_installer import anticheat, appinfo_cache + + def offline(): + raise OSError("tests are offline") + + module = request.node.module.__name__ + if not request.node.name.startswith("test_anticheat_download"): + monkeypatch.setattr(anticheat, "_download", offline) + monkeypatch.setattr(anticheat, "CACHE", tmp_path / "no-anticheat-cache.json") + if module != "test_appinfo_cache": + monkeypatch.setattr( + appinfo_cache, "default_path", lambda: tmp_path / "no-appinfo.vdf" + ) + # The live Steam client is never seen, stopped or started by a test: an + # unstubbed --apply run once shut down and relaunched kuhy's real Steam. + from steam_installer import steamctl + + if module in ("test_steamctl", "test_recognize"): + return # these fake subprocess and /proc themselves + monkeypatch.setattr(recognize, "steam_client_pids", list) + monkeypatch.setattr(steamctl, "stop", lambda *a, **k: pytest.fail("real steam stop")) + monkeypatch.setattr(steamctl, "start", lambda *a, **k: pytest.fail("real steam start")) diff --git a/_installer_import/tests/test_appinfo.py b/_installer_import/tests/test_appinfo.py index 413114c..e509ac9 100644 --- a/_installer_import/tests/test_appinfo.py +++ b/_installer_import/tests/test_appinfo.py @@ -202,3 +202,10 @@ def test_get_build_id_handles_non_dict_sections(monkeypatch): def test_get_build_id_returns_none_when_buildid_not_a_string(monkeypatch): _app_block(monkeypatch, {"depots": {"branches": {"public": {"buildid": {}}}}}) assert appinfo.get_build_id(1) is None + + +def test_a_shared_install_without_an_owner_is_still_shared(monkeypatch): + _app_block(monkeypatch, {"depots": {"5": {"sharedinstall": "1"}, "6": {"depotfromapp": "228980"}}}) + plan = appinfo.get_depot_plan(1) + assert plan.shared == {"5", "6"} + assert plan.shared_owner == {"6": "228980"} diff --git a/_installer_import/tests/test_appinfo_cache.py b/_installer_import/tests/test_appinfo_cache.py index 18d6dc5..7e80fc1 100644 --- a/_installer_import/tests/test_appinfo_cache.py +++ b/_installer_import/tests/test_appinfo_cache.py @@ -1,10 +1,10 @@ """Steam's local binary app-info cache, read without steamcmd.""" import pytest -from steam_installer import appinfo_cache - from _appinfo_blob import encode +from steam_installer import appinfo_cache + def _write(tmp_path, apps): path = tmp_path / "appinfo.vdf" diff --git a/_installer_import/tests/test_cli_finish.py b/_installer_import/tests/test_cli_finish.py index c320d43..2942006 100644 --- a/_installer_import/tests/test_cli_finish.py +++ b/_installer_import/tests/test_cli_finish.py @@ -4,6 +4,8 @@ import subprocess import sys import pytest +from _cli_helpers import ARGV, _stub_download + from steam_installer import ( appinfo, cli, @@ -11,8 +13,6 @@ from steam_installer import ( vdf, ) -from _cli_helpers import ARGV, _stub_download - pytestmark = pytest.mark.usefixtures("cli_logs") diff --git a/_installer_import/tests/test_compat_decide.py b/_installer_import/tests/test_compat_decide.py new file mode 100644 index 0000000..1639dc9 --- /dev/null +++ b/_installer_import/tests/test_compat_decide.py @@ -0,0 +1,164 @@ +"""Which games get the native Proton, and why the others keep Valve's default.""" + +import json + +import pytest + +from steam_installer import anticheat, compat_decide + +WINDOWS_ONLY = { + "common": {"oslist": "windows"}, + "depots": {"11": {"config": {"oslist": "windows"}}, "12": {}}, + "config": {"launch": {"0": {"config": {"oslist": "windows"}}}}, +} + + +def _decide(block=WINDOWS_ONLY, **kw): + args = {"anticheat": set(), "overrides": {}, "owned_dlc": False, "tool_ready": True} + args.update(kw) + return compat_decide.decide(10, block, **args) + + +def test_a_plain_windows_game_is_mapped(): + decision = _decide() + assert decision.map + assert decision.describe() == "map" + + +@pytest.mark.parametrize( + ("block", "reason"), + [ + ({**WINDOWS_ONLY, "common": {"oslist": "windows,linux"}}, "native"), + ({**WINDOWS_ONLY, "depots": {"11": {"config": {"oslist": "linux"}}}}, "native"), + ( + {**WINDOWS_ONLY, "config": {"launch": {"0": {"config": {"oslist": "linux"}}}}}, + "native", + ), + ({**WINDOWS_ONLY, "common": {"oslist": "windows,macos"}}, "not Windows-only"), + ({**WINDOWS_ONLY, "common": {}}, "no OS list"), + ({"common": "x"}, "no OS list"), + ({**WINDOWS_ONLY, "depots": {"11": {"depotfromapp": "228980"}}}, "shared depots"), + ({**WINDOWS_ONLY, "depots": {"11": {"sharedinstall": "1"}}}, "shared depots"), + ], +) +def test_unsafe_games_keep_valves_default(block, reason): + decision = _decide(block) + assert not decision.map + assert reason in decision.reasons + + +def test_odd_shapes_are_ignored_not_crashed_on(): + block = { + "common": {"oslist": "windows"}, + "depots": {"branches": {"public": {}}, "11": "scalar", "12": {"config": "x"}}, + "config": {"launch": "scalar"}, + } + assert _decide(block).map + assert _decide({**block, "config": {"launch": {"0": "x", "1": {"config": "y"}}}}).map + assert _decide({**block, "depots": "scalar", "config": "scalar"}).map + + +def test_anti_cheat_whatever_its_status(): + assert _decide(anticheat={10}).reasons == ["anti-cheat"] + + +def test_no_anti_cheat_list_means_no_mapping(): + assert _decide(anticheat=None).reasons == ["anti-cheat list unavailable"] + + +def test_owned_dlc_is_left_on_valves_default_until_tested_live(): + assert _decide(owned_dlc=True).reasons == ["owned DLC"] + + +def test_override_and_missing_tool(): + assert _decide(overrides={"10": "crashes at intro"}).reasons == [ + "override: crashes at intro" + ] + assert _decide(tool_ready=False).reasons == ["native Proton not installed"] + + +def test_every_reason_is_reported(): + block = {**WINDOWS_ONLY, "common": {"oslist": "windows,linux"}} + decision = _decide(block, anticheat={10}) + assert decision.describe() == "Valve default: anti-cheat, native" + + +def test_overrides_file_is_tracked_and_valid(): + assert compat_decide.load_overrides() == {} + + +def test_overrides_file_must_map_ids_to_reasons(tmp_path): + path = tmp_path / "o.json" + path.write_text(json.dumps({"10": "why"})) + assert compat_decide.load_overrides(path) == {"10": "why"} + for bad in ([1], {"x": "why"}, {"10": 3}): + path.write_text(json.dumps(bad)) + with pytest.raises(ValueError, match="compat_overrides"): + compat_decide.load_overrides(path) + + +def test_tool_ready_needs_the_manifest_and_the_pinned_stamp(tmp_path): + tool = tmp_path / "proton-cachyos-native" + assert not compat_decide.tool_ready(tool, "1:2-3") + tool.mkdir() + (tool / "toolmanifest.vdf").write_text("x") + assert not compat_decide.tool_ready(tool, "1:2-3") + (tool / "VERSION").write_text("1:2-2\n") + assert not compat_decide.tool_ready(tool, "1:2-3") + (tool / "VERSION").write_text("1:2-3\n") + assert compat_decide.tool_ready(tool, "1:2-3") + + +GAMES = [ + {"name": "CS", "storeIds": {"steam": "730"}, "status": "Supported"}, + {"name": "Epic only", "storeIds": {"epic": "x"}}, + {"name": "Broken", "storeIds": "x"}, + "not a game", +] + + +def test_anticheat_list_is_fetched_and_cached(tmp_path, monkeypatch): + cache = tmp_path / "ac.json" + monkeypatch.setattr(anticheat, "_download", lambda: json.dumps(GAMES).encode()) + assert anticheat.steam_ids(cache) == {730} + assert json.loads(cache.read_text()) == GAMES + + +def test_anticheat_falls_back_to_the_last_good_copy(tmp_path, monkeypatch): + cache = tmp_path / "ac.json" + cache.write_text(json.dumps(GAMES)) + + def offline(): + raise OSError("no network") + + monkeypatch.setattr(anticheat, "_download", offline) + assert anticheat.steam_ids(cache) == {730} + + +def test_anticheat_with_no_fetch_and_no_cache_is_unknown(tmp_path, monkeypatch): + monkeypatch.setattr(anticheat, "_download", lambda: b"{not json") + assert anticheat.steam_ids(tmp_path / "ac.json") is None + (tmp_path / "ac.json").write_text('{"a": 1}') + assert anticheat.steam_ids(tmp_path / "ac.json") is None + + +def test_anticheat_download_reads_the_upstream_url(monkeypatch): + seen = {} + + class _Response: + def __enter__(self): + return self + + def __exit__(self, *a): + return False + + def read(self): + return b"[]" + + def fake_urlopen(url, timeout): + seen["url"] = url + return _Response() + + monkeypatch.setattr(anticheat.urllib.request, "urlopen", fake_urlopen) + assert anticheat._download() == b"[]" + assert "AreWeAntiCheatYet" in seen["url"] diff --git a/_installer_import/tests/test_compat_map.py b/_installer_import/tests/test_compat_map.py new file mode 100644 index 0000000..27638cd --- /dev/null +++ b/_installer_import/tests/test_compat_map.py @@ -0,0 +1,198 @@ +"""Per-game CompatToolMapping entries, inserted into config.vdf as text.""" + +import pytest + +from steam_installer import compat_map + +HEAD = '''"InstallConfigStore" +{ +\t"Software" +\t{ +\t\t"Valve" +\t\t{ +\t\t\t"Steam" +\t\t\t{ +\t\t\t\t"SecretToken"\t\t"do-not-print-me" +''' +TAIL = '''\t\t\t} +\t\t} +\t} +\t"Music" +\t{ +\t\t"CompatToolMapping"\t\t"decoy outside the Steam block" +\t} +} +''' + + +def _entry(appid, name, priority, indent="\t\t\t\t\t"): + return ( + f'{indent}"{appid}"\n{indent}{{\n' + f'{indent}\t"name"\t\t"{name}"\n{indent}\t"config"\t\t""\n' + f'{indent}\t"priority"\t\t"{priority}"\n{indent}}}\n' + ) + + +def _config(tmp_path, mapping=None): + body = HEAD + if mapping is not None: + body += '\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n' + mapping + "\t\t\t\t}\n" + path = tmp_path / "config.vdf" + path.write_text(body + TAIL) + path.chmod(0o600) + return path + + +@pytest.fixture(autouse=True) +def _steam_closed(monkeypatch): + monkeypatch.setattr(compat_map.recognize, "steam_client_pids", list) + + +def test_adds_the_block_when_there_is_none(tmp_path): + path = _config(tmp_path) + before = path.read_text() + + outcome = compat_map.map_app(path, 1201270) + + assert outcome.action == "mapped" + text = path.read_text() + assert compat_map.current(path, 1201270) == ("proton-cachyos-native", "250") + # Everything Steam wrote is still there byte for byte. + assert text.replace(_mapping_block(1201270), "") == before + assert path.stat().st_mode & 0o777 == 0o600 + + +def _mapping_block(appid): + return ( + '\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n' + + _entry(appid, "proton-cachyos-native", "250") + + "\t\t\t\t}\n" + ) + + +def test_adds_an_entry_to_an_existing_block(tmp_path): + path = _config(tmp_path, _entry(10, "proton_9", "250")) + assert compat_map.map_app(path, 20).action == "mapped" + assert compat_map.current(path, 10) == ("proton_9", "250") + assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") + + +def test_replaces_valves_own_choice(tmp_path): + path = _config(tmp_path, _entry(20, "proton_experimental", "100")) + outcome = compat_map.map_app(path, 20) + assert outcome.action == "mapped" + assert "proton_experimental" in outcome.detail + assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") + assert path.read_text().count('"20"') == 1 + + +def test_replaces_an_empty_entry(tmp_path): + path = _config(tmp_path, _entry(20, "", "250")) + assert compat_map.map_app(path, 20).action == "mapped" + assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") + + +def test_keeps_a_tool_kuhy_picked_himself(tmp_path): + path = _config(tmp_path, _entry(20, "GE-Proton10", "250")) + before = path.read_text() + outcome = compat_map.map_app(path, 20) + assert outcome.action == "kept" + assert "GE-Proton10" in outcome.detail + assert path.read_text() == before + + +def test_a_second_run_changes_nothing(tmp_path): + path = _config(tmp_path) + compat_map.map_app(path, 20) + once = path.read_text() + assert compat_map.map_app(path, 20).action == "already" + assert path.read_text() == once + + +def test_refuses_while_steam_runs(tmp_path, monkeypatch): + path = _config(tmp_path) + before = path.read_text() + monkeypatch.setattr(compat_map.recognize, "steam_client_pids", lambda: [7]) + with pytest.raises(compat_map.CompatMapError, match="Steam is running"): + compat_map.map_app(path, 20) + assert path.read_text() == before + + +@pytest.mark.parametrize( + "text", + [ + '"InstallConfigStore"\n{\n}\n', + '"InstallConfigStore"\n{\n\t"Software" { }\n}\n', + '"InstallConfigStore"\n{\n\t}\n\t}\n', + '{\n}\n', + ], +) +def test_an_unexpected_layout_is_refused_without_echoing_lines(tmp_path, text): + path = tmp_path / "config.vdf" + path.write_text(text + '"x"\t"do-not-print-me"\n') + with pytest.raises(compat_map.CompatMapError) as excinfo: + compat_map.map_app(path, 20) + assert "do-not-print-me" not in str(excinfo.value) + + +def test_missing_config_is_an_error(tmp_path): + with pytest.raises(compat_map.CompatMapError, match="cannot read"): + compat_map.current(tmp_path / "nope.vdf", 1) + + +def test_current_is_none_for_an_unmapped_app(tmp_path): + assert compat_map.current(_config(tmp_path), 20) is None + assert compat_map.current(_config(tmp_path, _entry(10, "x", "1")), 20) is None + + +def test_keys_match_case_insensitively(tmp_path): + path = _config(tmp_path) + path.write_text(path.read_text().replace('"Steam"', '"steam"')) + assert compat_map.map_app(path, 20).action == "mapped" + assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") + + +def test_config_path_is_under_the_steam_root(tmp_path): + assert compat_map.config_path(tmp_path) == tmp_path / "config" / "config.vdf" + + +def test_a_value_spanning_lines_is_read_as_one_value(tmp_path): + """Steam stores some values with embedded newlines (seen in the real file).""" + path = _config(tmp_path) + path.write_text(path.read_text().replace( + '"SecretToken"\t\t"do-not-print-me"', '"SecretToken"\t\t"line one\nline {two}"')) + assert compat_map.map_app(path, 20).action == "mapped" + assert compat_map.current(path, 20) == ("proton-cachyos-native", "250") + + +def test_an_entry_sharing_a_line_with_its_key_is_refused(tmp_path): + path = _config(tmp_path, '\t\t\t\t\t"20" {\n\t\t\t\t\t\t"name" "x"\n\t\t\t\t\t}\n') + with pytest.raises(compat_map.CompatMapError, match="unexpected layout"): + compat_map.map_app(path, 20) + + +def test_an_entry_closing_on_a_shared_line_is_refused(tmp_path): + path = _config(tmp_path, '\t\t\t\t\t"20"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name" "" }\n') + with pytest.raises(compat_map.CompatMapError, match="unexpected layout"): + compat_map.map_app(path, 20) + + +def test_unquoted_tokens_are_refused(tmp_path): + path = _config(tmp_path) + path.write_text(path.read_text().replace('"SecretToken"', "SecretToken")) + with pytest.raises(compat_map.CompatMapError, match="line 9: unexpected layout"): + compat_map.current(path, 20) + + +def test_a_failed_replace_leaves_the_file_and_no_temp(tmp_path, monkeypatch): + path = _config(tmp_path) + before = path.read_text() + + def fail(src, dst): + raise OSError("disk full") + + monkeypatch.setattr(compat_map.os, "replace", fail) + with pytest.raises(OSError, match="disk full"): + compat_map.map_app(path, 20) + assert path.read_text() == before + assert sorted(p.name for p in tmp_path.iterdir()) == ["config.vdf"] diff --git a/_installer_import/tests/test_install_sh.py b/_installer_import/tests/test_install_sh.py new file mode 100644 index 0000000..d431b1e --- /dev/null +++ b/_installer_import/tests/test_install_sh.py @@ -0,0 +1,45 @@ +"""install.sh --check-only: what a run needs, with steamcmd no longer on the list.""" + +import os +import subprocess +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent + + +def _stub(bin_dir: Path, name: str, script: str) -> None: + path = bin_dir / name + path.write_text(f"#!/bin/bash\n{script}\n") + path.chmod(0o755) + + +def _check_only(tmp_path, libibus_installed: bool): + bin_dir = tmp_path / "bin" + bin_dir.mkdir() + _stub(bin_dir, "depotdownloader", "exit 0") + _stub(bin_dir, "pacman", f'[[ "$*" == "-Q libibus" ]] && exit {0 if libibus_installed else 1}; exit 0') + env = {**os.environ, "PATH": f"{bin_dir}:/usr/bin:/bin", "HOME": str(tmp_path)} + return subprocess.run( + ["bash", str(ROOT / "install.sh"), "--check-only"], + capture_output=True, text=True, env=env, timeout=30, check=False, + ) + + +def test_missing_libibus_is_reported_and_steamcmd_is_not_needed(tmp_path): + result = _check_only(tmp_path, libibus_installed=False) + assert result.returncode == 1 + assert "missing: libibus" in result.stderr + assert "steamcmd" not in result.stdout + result.stderr + + +def test_all_present_without_steamcmd(tmp_path): + result = _check_only(tmp_path, libibus_installed=True) + assert result.returncode == 0, result.stderr + assert "All dependencies present." in result.stdout + + +def test_run_sh_rechecks_when_the_proton_pin_changes(): + """A stale stamp must not hide a lock bump or a deleted tool folder.""" + text = (ROOT / "run.sh").read_text() + assert "proton-native.lock" in text + assert "steam_installer.proton_native --check" in text diff --git a/_installer_import/tests/test_map_installed.py b/_installer_import/tests/test_map_installed.py new file mode 100644 index 0000000..6449a64 --- /dev/null +++ b/_installer_import/tests/test_map_installed.py @@ -0,0 +1,197 @@ +"""Mapping games to the native Proton: after an install, and for the whole library.""" + +import pytest +from _appinfo_blob import encode +from _cli_helpers import ARGV, _stub_download + +from steam_installer import ( + anticheat, + cli, + compat_apply, + compat_map, + proton_pkg, + recognize, + steamctl, + vdf, +) + +pytestmark = pytest.mark.usefixtures("cli_logs") + +LOCKED = proton_pkg.read_lock().version +CONFIG = '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n\t\t\t}\n\t\t}\n\t}\n}\n' +PLAIN = {"common": {"name": "Plain", "oslist": "windows", "type": "Game"}, + "depots": {"11": {"config": {"oslist": "windows"}}}} +NATIVE = {"common": {"name": "Native", "oslist": "windows,linux", "type": "Game"}} +SHARED = {"common": {"name": "Shared", "oslist": "windows", "type": "Game"}, + "depots": {"31": {"depotfromapp": "228980"}}} +CS = {"common": {"name": "CS", "oslist": "windows,linux", "type": "Game"}} +TOOL = {"common": {"name": "Proton Experimental", "type": "Tool"}} + + +@pytest.fixture +def home(tmp_path, monkeypatch): + """A Steam root with config.vdf, the stamped tool, an app cache and anti-cheat list.""" + root = tmp_path / "Steam" + (root / "config").mkdir(parents=True) + (root / "config" / "config.vdf").write_text(CONFIG) + tool = root / "compatibilitytools.d" / "proton-cachyos-native" + tool.mkdir(parents=True) + (tool / "toolmanifest.vdf").write_text("x") + (tool / "VERSION").write_text(LOCKED + "\n") + cache = tmp_path / "appinfo.vdf" + cache.write_bytes(encode({10: PLAIN, 20: NATIVE, 30: SHARED, 730: CS, 1493710: TOOL})) + monkeypatch.setattr(compat_apply.appinfo.appinfo_cache, "default_path", lambda: cache) + monkeypatch.setattr(anticheat, "_download", lambda: b'[{"storeIds": {"steam": "730"}}]') + steamapps = root / "steamapps" + steamapps.mkdir() + for appid in (10, 20, 1493710): + (steamapps / f"appmanifest_{appid}.acf").write_text( + vdf.dumps("AppState", {"appid": str(appid), "InstalledDepots": {}})) + monkeypatch.setattr(cli, "find_steam_root", lambda: root) + return root + + +def _mapped(root, appid): + return compat_map.current(root / "config" / "config.vdf", appid) + + +def test_dry_run_lists_decisions_and_writes_nothing(home, capsys): + before = (home / "config" / "config.vdf").read_bytes() + assert cli.main(["--map-installed"]) == 0 + out = capsys.readouterr().out + assert "10 Plain: map" in out + assert "20 Native: Valve default: native" in out + assert "1493710" not in out # a tool, not a game + assert "Dry run" in out + assert (home / "config" / "config.vdf").read_bytes() == before + + +def test_appids_decides_any_cached_app(home, capsys): + assert cli.main(["--map-installed", "--appids", "730,20,30,10,999"]) == 0 + out = capsys.readouterr().out + assert "730 CS: Valve default: anti-cheat, native" in out + assert "30 Shared: Valve default: shared depots" in out + assert "10 Plain: map" in out + assert "999: not in Steam's app cache" in out + + +def test_apply_maps_the_safe_ones_and_a_rerun_changes_nothing(home, capsys): + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert _mapped(home, 10) == ("proton-cachyos-native", "250") + assert _mapped(home, 20) is None + once = (home / "config" / "config.vdf").read_bytes() + capsys.readouterr() + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert "10 Plain: map -- already mapped" in capsys.readouterr().out + assert (home / "config" / "config.vdf").read_bytes() == once + + +def test_apply_closes_steam_first_and_restarts_it(home, monkeypatch): + events = [] + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [] if events else [5]) + monkeypatch.setattr(steamctl, "stop", lambda logger: events.append("stop") or True) + monkeypatch.setattr(steamctl, "start", lambda logger: events.append("start")) + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert events == ["stop", "start"] + assert _mapped(home, 10) is not None + + +def test_apply_declined_writes_nothing(home, monkeypatch): + monkeypatch.setattr(recognize, "steam_client_pids", lambda: [5]) + monkeypatch.setattr("builtins.input", lambda *a: "n") + assert cli.main(["--map-installed", "--apply"]) == 1 + assert _mapped(home, 10) is None + + +def test_a_kept_choice_and_a_write_error_are_reported(home, monkeypatch, capsys): + config = home / "config" / "config.vdf" + compat_map.map_app(config, 10) + config.write_text(config.read_text().replace("proton-cachyos-native", "GE-Proton10")) + assert cli.main(["--map-installed", "--apply", "--yes"]) == 0 + assert "kept: already mapped to GE-Proton10" in capsys.readouterr().out + config.write_text("garbage {") + assert cli.main(["--map-installed", "--apply", "--yes"]) == 1 + + +def test_owned_dlc_in_the_manifest_keeps_valves_default(home, capsys): + manifest = home / "steamapps" / "appmanifest_10.acf" + manifest.write_text(vdf.dumps("AppState", {"appid": "10", "InstalledDepots": { + "11": {"manifest": "1"}, "12": {"manifest": "2", "dlcappid": "13"}}})) + assert cli.main(["--map-installed"]) == 0 + assert "10 Plain: Valve default: owned DLC" in capsys.readouterr().out + + +def test_valve_proton_only_goes_with_an_install(home): + with pytest.raises(SystemExit): + cli.main(["--map-installed", "--valve-proton"]) + + +def test_an_install_maps_the_game_while_steam_is_closed(steam, monkeypatch, capsys, tmp_path): + library, install_dir = steam + _with_compat_home(library.path, monkeypatch, tmp_path) + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir) + assert cli.main(ARGV) == 0 + assert _mapped(library.path, 1771300) == ("proton-cachyos-native", "250") + assert "mapped to proton-cachyos-native" in capsys.readouterr().out + + +def test_valve_proton_keeps_valves_default_for_one_run(steam, monkeypatch, capsys, tmp_path): + library, install_dir = steam + _with_compat_home(library.path, monkeypatch, tmp_path) + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir) + assert cli.main([*ARGV, "--valve-proton"]) == 0 + assert _mapped(library.path, 1771300) is None + assert "Valve default: override: --valve-proton" in capsys.readouterr().out + + +def test_a_mapping_failure_leaves_the_install_on_valves_default(steam, monkeypatch, capsys, tmp_path): + """The install itself succeeded; the game still runs on Valve's Proton.""" + library, install_dir = steam + _with_compat_home(library.path, monkeypatch, tmp_path) + (library.path / "config" / "config.vdf").write_text("broken {") + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir) + assert cli.main(ARGV) == 0 + assert "stays on Valve's default" in capsys.readouterr().out + + +def test_an_owned_dlc_download_keeps_valves_default(steam, monkeypatch, capsys, tmp_path): + library, install_dir = steam + _with_compat_home(library.path, monkeypatch, tmp_path) + _stub_download(monkeypatch) + monkeypatch.setattr(compat_apply.appinfo, "_fetch_app_block", lambda appid: PLAIN) + _proven(monkeypatch, install_dir, dlc={"1771303": "9"}) + assert cli.main(ARGV) == 0 + assert "Valve default: owned DLC" in capsys.readouterr().out + + +def _with_compat_home(root, monkeypatch, tmp_path): + (root / "config").mkdir(parents=True, exist_ok=True) + (root / "config" / "config.vdf").write_text(CONFIG) + tool = root / "compatibilitytools.d" / "proton-cachyos-native" + tool.mkdir(parents=True) + (tool / "toolmanifest.vdf").write_text("x") + (tool / "VERSION").write_text(LOCKED + "\n") + monkeypatch.setattr(anticheat, "_download", lambda: b"[]") + + +def _proven(monkeypatch, install_dir, dlc=None): + (gid,) = recognize.discover_cached_depot_manifests(install_dir).values() + plan = compat_apply.appinfo.DepotPlan({"1771303"}, set(), manifests={"1771303": gid}, + dlc=dict(dlc or {})) + monkeypatch.setattr(compat_apply.appinfo, "get_depot_plan", lambda appid, language: plan) + + +def test_a_failed_write_stops_the_run(home, monkeypatch, capsys): + def boom(config, appid): + raise compat_map.CompatMapError("config.vdf line 3: unexpected layout") + + monkeypatch.setattr(compat_map, "map_app", boom) + (home / "steamapps" / "appmanifest_notanid.acf").write_text("x") + assert cli.main(["--map-installed", "--apply", "--yes"]) == 1 + assert "Mapping stopped: config.vdf line 3" in capsys.readouterr().out diff --git a/_installer_import/tests/test_proton_native.py b/_installer_import/tests/test_proton_native.py new file mode 100644 index 0000000..6272089 --- /dev/null +++ b/_installer_import/tests/test_proton_native.py @@ -0,0 +1,211 @@ +"""Fetch, verify and install the native Proton, never running its .INSTALL.""" + +import os +import shutil + +import pytest +from _proton_fixture import ( + FILES, + VERSION, + build_db, + build_package, + build_repo, + gpg, + stop_agent, +) + +from steam_installer import proton_native, proton_pkg + + +@pytest.fixture(scope="module") +def repo(tmp_path_factory): + built = build_repo(tmp_path_factory.mktemp("repo")) + yield built + stop_agent(built.gpg_home) + + +@pytest.fixture +def mirror(repo, tmp_path): + """A private copy of the fake mirror a test may tamper with.""" + root = tmp_path / "mirror" + shutil.copytree(repo.root, root) + return root + + +@pytest.fixture +def setup(repo, mirror, tmp_path, monkeypatch): + tools = tmp_path / "compatibilitytools.d" + tools.mkdir() + lock = proton_pkg.Lock( + "proton-cachyos-native", (mirror / "cachyos.db").as_uri(), VERSION, repo.sha256 + ) + monkeypatch.setattr(proton_native, "in_use", lambda tool: []) + + def run(**kw): + return proton_native.ensure( + tools, lock, key=repo.key, fingerprint=repo.fingerprint, **kw + ) + + return tools, lock, run + + +def _tree(tool): + return sorted(str(p.relative_to(tool)) for p in tool.rglob("*")) + + +def test_a_fresh_install_extracts_only_the_tool_and_stamps_it(setup): + tools, _, run = setup + status = run() + tool = tools / "proton-cachyos-native" + assert status.startswith("installed") + assert "signature" in status and "sha256" in status + assert (tool / "VERSION").read_text() == VERSION + "\n" + assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"] + assert os.readlink(tool / "files/bin/msidb") == "wine" + assert not (tools / ".INSTALL").exists() + assert not any(".INSTALL" in p or "stray" in p for p in _tree(tools)) + assert sorted(os.listdir(tools)) == ["proton-cachyos-native"] + + +def test_a_current_stamp_needs_no_network(setup, monkeypatch): + _, _, run = setup + run() + monkeypatch.setattr(proton_native, "_download", lambda url, dest: pytest.fail()) + assert run() == f"current: {VERSION}" + + +def _assert_fails_untouched(run, tools, match): + old = tools / "proton-cachyos-native" + old.mkdir(exist_ok=True) + (old / "keep").write_text("old build") + with pytest.raises(proton_pkg.PkgError, match=match): + run() + assert (old / "keep").read_text() == "old build" + assert sorted(os.listdir(tools)) == ["proton-cachyos-native"] + + +def test_a_tampered_signature_fails_closed(setup, mirror): + tools, _, run = setup + sig = next(mirror.glob("*.sig")) + data = bytearray(sig.read_bytes()) + data[-5] ^= 0xFF + sig.write_bytes(bytes(data)) + _assert_fails_untouched(run, tools, "signature") + + +def test_a_signature_from_another_key_fails_closed(setup, mirror, tmp_path): + tools, _, run = setup + other = tmp_path / "other" + other.mkdir(mode=0o700) + gpg(other, "--quick-gen-key", "Mallory ", "ed25519", "sign", "never") + package = next(mirror.glob("*.pkg.tar.zst")) + (mirror / f"{package.name}.sig").unlink() + gpg(other, "--output", f"{package}.sig", "--detach-sign", str(package)) + stop_agent(other) + _assert_fails_untouched(run, tools, "signature") + + +def test_a_package_that_differs_from_the_pinned_sha256_fails_closed(setup, mirror, repo): + tools, _, run = setup + package = next(mirror.glob("*.pkg.tar.zst")) + build_package(package, {**FILES, "proton": b"evil"}) + gpg(repo.gpg_home, "--yes", "--output", f"{package}.sig", "--detach-sign", str(package)) + _assert_fails_untouched(run, tools, "sha256") + + +def test_a_db_sha256_that_disagrees_with_the_lock_fails_closed(setup, mirror): + tools, _, run = setup + build_db(mirror / "cachyos.db", VERSION, "0" * 64) + _assert_fails_untouched(run, tools, "sha256") + + +def test_a_newer_package_in_the_repo_asks_for_a_lock_bump(setup, mirror, repo): + tools, _, run = setup + build_db(mirror / "cachyos.db", "1:11.0.20261101-1", repo.sha256) + _assert_fails_untouched(run, tools, "bump") + + +def test_a_repo_without_the_package_fails(setup, mirror, repo): + tools, _, run = setup + build_db(mirror / "cachyos.db", VERSION, repo.sha256, name="something-else") + _assert_fails_untouched(run, tools, "no proton-cachyos-native") + + +def _existing_copy(tools): + tool = tools / "proton-cachyos-native" + for name, data in FILES.items(): + (tool / name).parent.mkdir(parents=True, exist_ok=True) + (tool / name).write_bytes(data) + os.symlink("wine", tool / "files/bin/msidb") + (tool / "__pycache__").mkdir() + (tool / "__pycache__" / "x.pyc").write_bytes(b"made by running it") + return tool + + +def test_an_identical_copy_is_adopted_without_extracting(setup, monkeypatch): + tools, _, run = setup + tool = _existing_copy(tools) + monkeypatch.setattr(proton_pkg, "extract", lambda *a: pytest.fail("extracted")) + status = run() + assert status.startswith(f"adopted: all {len(FILES) + 1} files match .MTREE (signature") + assert (tool / "VERSION").read_text() == VERSION + "\n" + + +@pytest.mark.parametrize( + ("change", "where"), + [ + (lambda t: (t / "files/bin/wine").write_bytes(b"patched"), "files/bin/wine"), + (lambda t: (t / "user_settings.py").write_text("x"), "user_settings.py"), + (lambda t: (t / "proton").unlink(), "proton"), + (lambda t: (t / "files/bin/msidb").unlink() or os.symlink("x", t / "files/bin/msidb"), "msidb"), + (lambda t: (t / "files/bin/msidb").unlink(), "msidb"), + ], +) +def test_a_differing_copy_is_replaced(setup, change, where): + tools, _, run = setup + tool = _existing_copy(tools) + change(tool) + status = run() + assert status.startswith("replaced: mismatch in") and where in status + assert (tool / "files/bin/wine").read_bytes() == FILES["files/bin/wine"] + assert not (tool / "user_settings.py").exists() + assert sorted(os.listdir(tools)) == ["proton-cachyos-native"] + + +def test_replacing_is_deferred_while_steam_or_a_game_runs(setup, monkeypatch): + tools, _, run = setup + tool = _existing_copy(tools) + (tool / "proton").write_text("old") + monkeypatch.setattr(proton_native, "in_use", lambda t: [4242]) + status = run() + assert status.startswith("deferred") + assert (tool / "proton").read_text() == "old" + assert not (tool / "VERSION").exists() + + +def test_an_older_stamp_is_updated(setup): + tools, _, run = setup + tool = _existing_copy(tools) + (tool / "VERSION").write_text("1:10.0-1\n") + assert run().startswith("updated: 1:10.0-1 ->") + assert (tool / "VERSION").read_text() == VERSION + "\n" + + +def test_in_use_sees_steam_and_processes_running_from_the_tool(tmp_path, monkeypatch): + proc = tmp_path / "proc" + tool = tmp_path / "tool" + (tool / "files/bin").mkdir(parents=True) + (tool / "files/bin/wine").write_text("") + (tmp_path / "other").write_text("") + for pid, target in ( + ("10", tool / "files/bin/wine"), + ("11", tmp_path / "other"), + ("13", tmp_path / "gone"), + ): + (proc / pid).mkdir(parents=True) + os.symlink(target, proc / pid / "exe") + (proc / "self").mkdir() + (proc / "12").mkdir() + monkeypatch.setattr(proton_native.recognize, "PROC_DIR", proc) + monkeypatch.setattr(proton_native.recognize, "steam_client_pids", lambda: [99]) + assert proton_native.in_use(tool) == [99, 10] diff --git a/_installer_import/tests/test_proton_native_cli.py b/_installer_import/tests/test_proton_native_cli.py new file mode 100644 index 0000000..eae936d --- /dev/null +++ b/_installer_import/tests/test_proton_native_cli.py @@ -0,0 +1,58 @@ +"""The proton_native entry point: what install.sh and run.sh call.""" + +import subprocess +import sys + +from steam_installer import proton_native, proton_pkg + + +def test_main_reports_and_exits(monkeypatch, capsys, tmp_path): + monkeypatch.setattr(proton_native, "ensure", lambda *a, **kw: "current: x") + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + assert proton_native.main([]) == 0 + assert "current: x" in capsys.readouterr().out + + def boom(*a, **kw): + raise proton_pkg.PkgError("bad signature") + + monkeypatch.setattr(proton_native, "ensure", boom) + assert proton_native.main([]) == 1 + assert "bad signature" in capsys.readouterr().err + + +def test_check_reports_whether_the_stamp_matches_the_lock(tmp_path, monkeypatch): + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + assert proton_native.main(["--check"]) == 1 + tool = tmp_path / "compatibilitytools.d" / "proton-cachyos-native" + tool.mkdir(parents=True) + (tool / "VERSION").write_text(proton_pkg.read_lock().version + "\n") + assert proton_native.main(["--check"]) == 0 + + +def test_no_steam_installation_means_nothing_to_install(monkeypatch, capsys): + def absent(): + raise FileNotFoundError("No Steam installation found") + + monkeypatch.setattr(proton_native, "find_steam_root", absent) + assert proton_native.main([]) == 0 + assert proton_native.main(["--check"]) == 0 + assert "skipped" in capsys.readouterr().out + + +def test_the_repo_can_be_pointed_at_a_test_mirror(monkeypatch, tmp_path, capsys): + seen = {} + monkeypatch.setattr(proton_native, "find_steam_root", lambda: tmp_path) + monkeypatch.setenv("SGI_PROTON_REPO", "file:///mirror/cachyos.db") + monkeypatch.setattr(proton_native, "ensure", lambda d, lock: seen.setdefault("repo", lock.repo)) + assert proton_native.main([]) == 0 + assert seen["repo"] == "file:///mirror/cachyos.db" + + +def test_module_runs_as_a_script(tmp_path, coverage_subprocess_env): + env = {**coverage_subprocess_env, "HOME": str(tmp_path)} + result = subprocess.run( + [sys.executable, "-m", "steam_installer.proton_native", "--check"], + capture_output=True, text=True, env=env, timeout=30, check=False, + ) + assert result.returncode == 0 + assert "skipped" in result.stdout diff --git a/_installer_import/tests/test_proton_pkg.py b/_installer_import/tests/test_proton_pkg.py new file mode 100644 index 0000000..68fb12c --- /dev/null +++ b/_installer_import/tests/test_proton_pkg.py @@ -0,0 +1,98 @@ +"""The package formats and checks behind the native Proton fetcher.""" + +import hashlib +import tarfile + +import pytest +from _proton_fixture import PREFIX, build_repo, stop_agent + +from steam_installer import proton_pkg + + +@pytest.fixture(scope="module") +def repo(tmp_path_factory): + built = build_repo(tmp_path_factory.mktemp("repo")) + yield built + stop_agent(built.gpg_home) + + +def test_the_real_lock_pins_the_proven_build(): + lock = proton_pkg.read_lock() + assert lock.name == "proton-cachyos-native" + assert lock.version == "1:11.0.20261015-1" + assert lock.sha256 == "25bf302f90e8e01118555b997b873c3cd06a33b5647e79d049a59e29794b43e0" + assert lock.repo == "https://mirror.cachyos.org/repo/x86_64/cachyos/cachyos.db" + + +def test_a_lock_missing_a_key_is_refused(tmp_path): + path = tmp_path / "x.lock" + path.write_text("# c\nname=a\nrepo=b.db\n") + with pytest.raises(proton_pkg.PkgError, match="version"): + proton_pkg.read_lock(path) + + +def test_the_pinned_key_file_holds_the_pinned_fingerprint(tmp_path): + home = tmp_path / "g" + home.mkdir(mode=0o700) + try: + assert proton_pkg.key_fingerprints(proton_pkg.KEY, home) == [proton_pkg.FINGERPRINT] + finally: + stop_agent(home) + + +def test_package_urls_quote_the_epoch_colon(): + lock = proton_pkg.Lock("p", "https://m/repo/cachyos.db", "1:2-3", "x") + assert proton_pkg.package_url(lock, "p-1:2-3-x86_64.pkg.tar.zst") == ( + "https://m/repo/p-1%3A2-3-x86_64.pkg.tar.zst" + ) + + +def test_sha256_file_streams(tmp_path): + path = tmp_path / "f" + path.write_bytes(b"abc" * 100000) + assert proton_pkg.sha256_file(path) == hashlib.sha256(b"abc" * 100000).hexdigest() + + +def test_mtree_unescapes_octal_names(repo): + entries = proton_pkg.read_mtree(repo.package) + assert entries["user settings.py"].kind == "file" + assert entries["files/bin/msidb"].link == "wine" + assert ".INSTALL" not in entries and "" not in entries + assert PREFIX not in entries + + + + +def test_db_entries_other_than_desc_are_ignored(tmp_path): + db = tmp_path / "x.db" + with tarfile.open(db, "w:zst") as tar: + info = tarfile.TarInfo("p-1/files") + tar.addfile(info) + with pytest.raises(proton_pkg.PkgError, match="lists no p"): + proton_pkg.find_in_db(db, "p") + + +def test_a_key_file_without_the_pinned_fingerprint_is_refused(repo, tmp_path): + home = tmp_path / "g" + home.mkdir(mode=0o700) + try: + with pytest.raises(proton_pkg.PkgError, match="pinned key file"): + proton_pkg.verify_signature( + repo.package, repo.package, repo.key, "0" * 40, home + ) + finally: + stop_agent(home) + + +def test_a_package_without_mtree_is_refused(tmp_path): + package = tmp_path / "p.pkg.tar.zst" + with tarfile.open(package, "w:zst") as tar: + tar.addfile(tarfile.TarInfo("usr/x")) + with pytest.raises(proton_pkg.PkgError, match="no .MTREE"): + proton_pkg.read_mtree(package) + + +def test_directories_listed_in_mtree_need_no_content_check(tmp_path): + entries = {"files": proton_pkg.MtreeEntry("dir")} + (tmp_path / "files").mkdir() + assert proton_pkg.first_mismatch(tmp_path, entries) is None