steam-backlog-enforcer/_installer_import/steam_installer/cli.py
Krzysztof kuhy Rudnicki f9e91bba17 fix(recognize): verify manifest state instead of trusting steamcmd's exit code
Confirmed live against a real install: steamcmd's app_update can exit 0
without actually finishing -- it left installdir pointing at a stale,
unrelated folder name (from an earlier corrupted manifest state) and
InstalledDepots empty despite a "clean" exit. Relaunching the Steam
client then read that same stale field and wiped the correctly-downloaded
folder as orphaned while starting a fresh download into the wrong one --
real data loss, not a hypothetical.

- manifest_reflects_real_install() checks StateFlags/installdir/
  InstalledDepots directly instead of trusting the return code.
- ensure_installdir_correct() patches a stale installdir BEFORE running
  steamcmd, not just in the crash-fallback path after.
- run_steamcmd_validate() no longer captures stdout/stderr: steamcmd
  prompts for a password (and Steam Guard) on stdin, and prompt text
  written to a captured/buffered stdout never reached the terminal --
  the process looked hung with no visible request for input. Output
  now inherits the parent terminal, matching depotdownloader's own
  interactive login. The confirmed install-script segfault is detected
  via the SIGSEGV exit signal instead of grepping captured text.
- finish_manifest_manually() now also corrects installdir (previously
  only fixed by ensure_installdir_correct, not the crash-fallback path)
  and no longer lets a "0" buildid sentinel short-circuit past a real
  TargetBuildID via a bare `or` chain.
- cli.py gains --skip-steamcmd: go straight from depotdownloader's
  download to a direct manifest write, bypassing steamcmd's app_update
  entirely. It has now shown two separate failure modes (the segfault,
  and a silent no-op run) -- this is the same direct-write approach
  already confirmed to work standalone.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-25 15:51:20 +02:00

145 lines
5.1 KiB
Python

"""Entry point: resolve -> confirm -> fast download -> make Steam recognize it."""
from __future__ import annotations
import argparse
import shutil
import sys
import time
from steam_installer import appinfo, depotdl, recognize, resolve
from steam_installer.library import find_steam_root, list_libraries, pick_install_target
def _require_tool(name: str, install_hint: str) -> None:
if shutil.which(name) is None:
print(f"error: `{name}` not found on PATH. {install_hint}", file=sys.stderr)
sys.exit(1)
def _resolve_appid(args: argparse.Namespace) -> int:
"""Prefer an explicit --appid; fall back to search, which is best-effort
(store.steampowered.com is known to reject scripted clients on some
networks -- see resolve.py). Either way, the final confirmation uses the
real name from steamcmd's own app info, not whatever the search returned.
"""
if args.appid is not None:
return args.appid
try:
results = resolve.search(args.name)
except resolve.SearchUnavailable as exc:
print(f"Store search unavailable ({exc}).", file=sys.stderr)
print(
"Find the numeric AppID yourself (e.g. from the store URL) and pass --appid.",
file=sys.stderr,
)
sys.exit(1)
chosen = resolve.prompt_confirm(results)
return chosen.appid
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Download an owned Steam game fast, then make Steam recognize it."
)
parser.add_argument("name", nargs="?", default=None, help="Game name to search for")
parser.add_argument(
"--appid", type=int, default=None, help="Steam AppID (skips search)"
)
parser.add_argument("--username", required=True, help="Steam account username")
parser.add_argument(
"--language", default=None, help="Language depot to fetch (default: prompts)"
)
parser.add_argument(
"--max-downloads", type=int, default=depotdl.DEFAULT_MAX_DOWNLOADS
)
parser.add_argument(
"--skip-steamcmd",
action="store_true",
help=(
"Finish the manifest directly from depotdownloader's own cached data "
"instead of running steamcmd's app_update. Steamcmd's install-script "
"step has shown two separate failure modes (a confirmed segfault, "
"and a silent no-op run); this is the same direct-write approach "
"already confirmed to work."
),
)
args = parser.parse_args(argv)
if args.appid is None and args.name is None:
parser.error("provide a game name to search for, or --appid directly")
_require_tool("depotdownloader", "See ~/depotdownloader.")
if not args.skip_steamcmd:
_require_tool("steamcmd", "Run: yay -S steamcmd")
appid = _resolve_appid(args)
metadata = appinfo.get_metadata(appid)
print(f"Confirmed: {metadata.name} (appid {metadata.appid})")
answer = input("Proceed? [y/N]: ").strip().lower()
if answer != "y":
print("Cancelled.")
return 1
steam_root = find_steam_root()
libraries = list_libraries(steam_root)
target = pick_install_target(libraries, metadata.installdir)
print(f"Install target: {target}")
language = args.language
if language is None:
language = input("Language [english]: ").strip() or "english"
print("Downloading via depotdownloader (fast path)...")
depotdl.download(
appid=metadata.appid,
install_dir=target,
username=args.username,
language=language,
max_downloads=args.max_downloads,
)
library = next(lib for lib in libraries if lib.common == target.parent)
recognize.ensure_installdir_correct(library, metadata.appid, target)
if not args.skip_steamcmd:
print("Reconciling with steamcmd (validate)...")
result = recognize.run_steamcmd_validate(metadata.appid, target, args.username)
# Never trust steamcmd's exit code alone -- verify the manifest actually
# reflects a real install before believing it. A "clean" exit has
# produced a manifest steamcmd itself hadn't finished updating before.
if (
not result.crashed_on_install_script
and recognize.manifest_reflects_real_install(
library, metadata.appid, target
)
):
print("steamcmd finished cleanly -- Steam should show Play.")
return 0
if result.crashed_on_install_script:
print(
"steamcmd hit the known install-script crash; finishing the manifest manually..."
)
else:
print(
f"steamcmd exited {result.returncode} but the manifest doesn't reflect a "
"real install; finishing it manually..."
)
manifest_path = recognize.finish_manifest_manually(
library=library,
appid=metadata.appid,
install_dir=target,
now_unix=int(time.time()),
)
print(f"Wrote {manifest_path} -- launch Steam and confirm Play works.")
return 0
if __name__ == "__main__":
raise SystemExit(main())