steam-backlog-enforcer/_installer_import/steam_installer/map_installed.py
Krzysztof kuhy Rudnicki c1b6fdd16f 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 <noreply@anthropic.com>
2026-11-22 14:57:48 +01:00

126 lines
4.3 KiB
Python

"""`--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