mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 23:02:19 +01:00
Read app info from Steam's cache, drop steamcmd
Write the 15-key manifest from scratch when none exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
ed03bf76e7
commit
e2fc518624
@ -1,12 +1,10 @@
|
||||
"""Look up app metadata via steamcmd's app_info_print (anonymous, no login needed)."""
|
||||
"""App metadata (name, installdir, depots, build) from Steam's local app cache."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
import subprocess
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
from steam_installer import vdf
|
||||
from steam_installer import appinfo_cache
|
||||
|
||||
|
||||
# depotdownloader is never given -branch, so it always fetches this one.
|
||||
@ -41,56 +39,37 @@ class DepotPlan:
|
||||
# depot id -> the manifest gid the public branch ships, for every depot in
|
||||
# the app's list. A cached depot manifest matching it proves the build.
|
||||
manifests: dict[str, str] = field(default_factory=dict)
|
||||
# shared depot id -> the app it is installed under (its `depotfromapp`).
|
||||
# A manifest written from scratch records these as SharedDepots.
|
||||
shared_owner: dict[str, str] = field(default_factory=dict)
|
||||
|
||||
@property
|
||||
def all_expected(self) -> set[str]:
|
||||
return self.required | self.shared
|
||||
|
||||
|
||||
def _fetch_app_blocks(appids: list[int]) -> dict[int, dict]:
|
||||
"""App info for every id in `appids`, from a single anonymous steamcmd call.
|
||||
def _cached_blocks(appids: list[int]) -> dict[int, dict]:
|
||||
"""App info for those of `appids` Steam's local cache holds.
|
||||
|
||||
One call for the lot: each steamcmd start costs seconds of login, and a game
|
||||
can list dozens of DLC.
|
||||
Read from `appcache/appinfo.vdf` (see :mod:`appinfo_cache`), not from
|
||||
steamcmd: this tool no longer runs steamcmd at all.
|
||||
"""
|
||||
prints = [arg for appid in appids for arg in ("+app_info_print", str(appid))]
|
||||
result = subprocess.run(
|
||||
[
|
||||
"steamcmd",
|
||||
# Without this, a Linux steamcmd resolves a Windows-only app's depot
|
||||
# list against the host platform and can come back empty.
|
||||
"+@sSteamCmdForcePlatformType",
|
||||
"windows",
|
||||
"+login",
|
||||
"anonymous",
|
||||
*prints,
|
||||
"+quit",
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
# The exit code says nothing useful; a missing block is caught below.
|
||||
check=False,
|
||||
timeout=60 + 5 * len(appids),
|
||||
)
|
||||
output = result.stdout
|
||||
# app_info_print emits `"<appid>"\n{ ... }` inside a stream of steamcmd's own
|
||||
# log lines; each block runs from its own header to the next one.
|
||||
starts = {}
|
||||
for appid in appids:
|
||||
match = re.search(rf'^"{appid}"\s*$', output, re.MULTILINE)
|
||||
if not match:
|
||||
raise AppInfoError(
|
||||
f"steamcmd did not return app info for {appid}:\n{output}"
|
||||
)
|
||||
starts[appid] = match.start()
|
||||
ordered = sorted(starts.values()) + [len(output)]
|
||||
blocks: dict[int, dict] = {}
|
||||
for appid, start in starts.items():
|
||||
end = ordered[ordered.index(start) + 1]
|
||||
app_block = vdf.parse(output[start:end]).get(str(appid), {})
|
||||
if not isinstance(app_block, dict):
|
||||
raise AppInfoError(f"unexpected app_info_print structure for {appid}")
|
||||
blocks[appid] = app_block
|
||||
try:
|
||||
apps = appinfo_cache.load()
|
||||
except appinfo_cache.CacheError as exc:
|
||||
raise AppInfoError(str(exc)) from exc
|
||||
return {appid: apps[appid] for appid in appids if appid in apps}
|
||||
|
||||
|
||||
def _fetch_app_blocks(appids: list[int]) -> dict[int, dict]:
|
||||
"""App info for every id in `appids`; a missing one is an error with the fix."""
|
||||
blocks = _cached_blocks(appids)
|
||||
missing = [str(appid) for appid in appids if appid not in blocks]
|
||||
if missing:
|
||||
raise AppInfoError(
|
||||
f"app {', '.join(missing)} is not in Steam's local app cache: "
|
||||
f"{appinfo_cache.FIX}"
|
||||
)
|
||||
return blocks
|
||||
|
||||
|
||||
@ -188,7 +167,10 @@ def get_dlc_plans(
|
||||
game_depots = app_block.get("depots", {})
|
||||
game_depot_ids = set(game_depots) if isinstance(game_depots, dict) else set()
|
||||
plans: dict[int, dict[str, str]] = {}
|
||||
for dlc_id, dlc_block in _fetch_app_blocks(dlc_ids).items():
|
||||
# A DLC the cache lacks is not owned: Steam caches app info for every app
|
||||
# the account owns (1828 of 7102 listed DLC ids on 2026-11-22), and
|
||||
# depotdownloader would refuse an unowned one anyway.
|
||||
for dlc_id, dlc_block in _cached_blocks(dlc_ids).items():
|
||||
depots = dlc_block.get("depots", {})
|
||||
if not isinstance(depots, dict):
|
||||
continue
|
||||
@ -206,12 +188,15 @@ def _plan_from_depots(
|
||||
"""The depots of one app's `depots` section that an (os, language) install gets."""
|
||||
required: set[str] = set()
|
||||
shared: set[str] = set()
|
||||
shared_owner: dict[str, str] = {}
|
||||
for depot_id, entry in depots.items():
|
||||
if not depot_id.isdigit() or not isinstance(entry, dict):
|
||||
continue
|
||||
# A shared depot is installed once under its owning app, not here.
|
||||
if "depotfromapp" in entry or "sharedinstall" in entry:
|
||||
shared.add(depot_id)
|
||||
if isinstance(entry.get("depotfromapp"), str):
|
||||
shared_owner[depot_id] = entry["depotfromapp"]
|
||||
continue
|
||||
# Another app's DLC is not part of this install -- but a DLC app's own
|
||||
# depots carry its own id as dlcappid, and are exactly what it installs.
|
||||
@ -231,4 +216,4 @@ def _plan_from_depots(
|
||||
if isinstance(depot_language, str) and depot_language != language:
|
||||
continue
|
||||
required.add(depot_id)
|
||||
return DepotPlan(required=required, shared=shared)
|
||||
return DepotPlan(required=required, shared=shared, shared_owner=shared_owner)
|
||||
|
||||
115
_installer_import/steam_installer/appinfo_cache.py
Normal file
115
_installer_import/steam_installer/appinfo_cache.py
Normal file
@ -0,0 +1,115 @@
|
||||
"""Read app info from the Steam client's own cache, `appcache/appinfo.vdf`.
|
||||
|
||||
This replaced `steamcmd +app_info_print`. On 2026-11-22 all 11 compared
|
||||
fields for app 1201270 (name, installdir, build id, change number, depot
|
||||
gids, ...) matched steamcmd's answer exactly. The cache is only as fresh as
|
||||
Steam's last run, which is why callers check its gids against what
|
||||
depotdownloader actually fetched.
|
||||
|
||||
Layout (magic 0x07564429): u32 magic, u32 universe, i64 offset of a string
|
||||
table; then per app: u32 appid, u32 size, `size` bytes made of a 60-byte
|
||||
header and binary key-values whose keys are u32 indexes into the string
|
||||
table. Appid 0 ends the list. The table is a u32 count followed by
|
||||
NUL-terminated strings.
|
||||
|
||||
Every scalar comes back as a string, the way `app_info_print` printed it:
|
||||
the rest of the tool compares `== "1"` and `!= str(appid)`, and an int there
|
||||
would silently change which depots count.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
from pathlib import Path
|
||||
|
||||
from steam_installer.library import find_steam_root
|
||||
|
||||
MAGIC = 0x07564429
|
||||
_HEADER = 60
|
||||
FIX = "start Steam once, let it sync, rerun"
|
||||
|
||||
# Fixed-width value types: KV type byte -> struct format.
|
||||
_SCALARS = {2: "<i", 3: "<f", 4: "<i", 6: "<i", 7: "<Q", 10: "<q"}
|
||||
|
||||
_memo: dict[Path, tuple[tuple[int, int], dict[int, dict]]] = {}
|
||||
|
||||
|
||||
class CacheError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def default_path() -> Path:
|
||||
return find_steam_root() / "appcache" / "appinfo.vdf"
|
||||
|
||||
|
||||
def load(path: Path | None = None) -> dict[int, dict]:
|
||||
"""Every app in the cache, appid -> its `appinfo` block.
|
||||
|
||||
Parsed once per file version: four lookups in one run would otherwise
|
||||
parse the ~100 MB file four times.
|
||||
"""
|
||||
path = path or default_path()
|
||||
try:
|
||||
stat = path.stat()
|
||||
except OSError as exc:
|
||||
raise CacheError(f"cannot read Steam's app cache {path}: {FIX}") from exc
|
||||
key = (stat.st_mtime_ns, stat.st_size)
|
||||
cached = _memo.get(path)
|
||||
if cached is not None and cached[0] == key:
|
||||
return cached[1]
|
||||
apps = _parse(path.read_bytes())
|
||||
_memo[path] = (key, apps)
|
||||
return apps
|
||||
|
||||
|
||||
def _parse(buf: bytes) -> dict[int, dict]:
|
||||
magic, _universe, table_offset = struct.unpack_from("<IIq", buf, 0)
|
||||
if magic != MAGIC:
|
||||
raise CacheError(f"unsupported appinfo.vdf format {magic:#010x}")
|
||||
strings = _string_table(buf, table_offset)
|
||||
apps: dict[int, dict] = {}
|
||||
pos = 16
|
||||
while True:
|
||||
appid, size = struct.unpack_from("<II", buf, pos)
|
||||
if appid == 0:
|
||||
return apps
|
||||
data, _ = _kv(buf, pos + 8 + _HEADER, strings)
|
||||
inner = data.get("appinfo")
|
||||
apps[appid] = inner if isinstance(inner, dict) else data
|
||||
pos += 8 + size
|
||||
|
||||
|
||||
def _string_table(buf: bytes, offset: int) -> list[str]:
|
||||
(count,) = struct.unpack_from("<I", buf, offset)
|
||||
pos = offset + 4
|
||||
out: list[str] = []
|
||||
for _ in range(count):
|
||||
end = buf.index(b"\0", pos)
|
||||
out.append(buf[pos:end].decode("utf-8", "replace"))
|
||||
pos = end + 1
|
||||
return out
|
||||
|
||||
|
||||
def _kv(buf: bytes, pos: int, strings: list[str]) -> tuple[dict, int]:
|
||||
out: dict = {}
|
||||
while True:
|
||||
kind = buf[pos]
|
||||
pos += 1
|
||||
if kind == 8:
|
||||
return out, pos
|
||||
(index,) = struct.unpack_from("<I", buf, pos)
|
||||
pos += 4
|
||||
key = strings[index]
|
||||
if kind == 0:
|
||||
out[key], pos = _kv(buf, pos, strings)
|
||||
elif kind == 1:
|
||||
end = buf.index(b"\0", pos)
|
||||
out[key] = buf[pos:end].decode("utf-8", "replace")
|
||||
pos = end + 1
|
||||
elif kind in _SCALARS:
|
||||
fmt = _SCALARS[kind]
|
||||
(value,) = struct.unpack_from(fmt, buf, pos)
|
||||
out[key] = str(value)
|
||||
pos += struct.calcsize(fmt)
|
||||
else:
|
||||
raise CacheError(f"unsupported value type {kind} in appinfo.vdf")
|
||||
@ -70,17 +70,6 @@ def _build_parser() -> argparse.ArgumentParser:
|
||||
"steamcmd leaves behind, and clear them away."
|
||||
),
|
||||
)
|
||||
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."
|
||||
),
|
||||
)
|
||||
parser.add_argument(
|
||||
"--verbose",
|
||||
action="store_true",
|
||||
@ -176,7 +165,7 @@ def _run(args: argparse.Namespace, log: runlog.RunLog) -> int:
|
||||
return _repair_ghosts(log)
|
||||
|
||||
try:
|
||||
deps.require(skip_steamcmd=args.skip_steamcmd)
|
||||
deps.require()
|
||||
resolved = target.resolve_target(
|
||||
args.target, args.appid, interactive=interactive
|
||||
)
|
||||
|
||||
@ -8,7 +8,6 @@ from pathlib import Path
|
||||
|
||||
TOOL_HINTS = {
|
||||
"depotdownloader": "Run ./install.sh, which fetches it from the SteamRE release.",
|
||||
"steamcmd": "Run ./install.sh, or: yay -S steamcmd",
|
||||
}
|
||||
|
||||
|
||||
@ -32,17 +31,19 @@ def depotdownloader_bin() -> str:
|
||||
return "depotdownloader"
|
||||
|
||||
|
||||
def missing_tools(*, skip_steamcmd: bool) -> list[str]:
|
||||
"""Which required binaries are not on PATH, in the order they'd be used."""
|
||||
def missing_tools() -> list[str]:
|
||||
"""Which required binaries are not on PATH, in the order they'd be used.
|
||||
|
||||
steamcmd is not one of them: app info comes from Steam's own cache and
|
||||
the manifest is written by this tool.
|
||||
"""
|
||||
needed = ["depotdownloader"]
|
||||
if not skip_steamcmd:
|
||||
needed.append("steamcmd")
|
||||
resolved = {"depotdownloader": depotdownloader_bin()}
|
||||
return [tool for tool in needed if shutil.which(resolved.get(tool, tool)) is None]
|
||||
|
||||
|
||||
def require(*, skip_steamcmd: bool) -> None:
|
||||
missing = missing_tools(skip_steamcmd=skip_steamcmd)
|
||||
def require() -> None:
|
||||
missing = missing_tools()
|
||||
if missing:
|
||||
details = "; ".join(
|
||||
f"`{tool}` not found -- {TOOL_HINTS[tool]}" for tool in missing
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
"""Hand-finish the appmanifest, but only when every precondition really holds.
|
||||
|
||||
Split out of cli.py so the entry point stays under the repo's 250-line cap.
|
||||
The logic is unchanged: it patches a manifest steamcmd or Steam already
|
||||
wrote, and refuses rather than fabricating one.
|
||||
It patches the manifest Steam (or the backlog enforcer) already wrote, and
|
||||
writes one from scratch when none exists -- steamcmd, which used to create
|
||||
it, is no longer run. Either way every precondition is checked first.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@ -21,32 +22,22 @@ def finish(
|
||||
target: Path,
|
||||
depot_plan: appinfo.DepotPlan,
|
||||
expected_build_id: str | None,
|
||||
skipped_steamcmd: bool,
|
||||
) -> int:
|
||||
logger = log.logger
|
||||
manifest_path = library.steamapps / f"appmanifest_{metadata.appid}.acf"
|
||||
|
||||
if not manifest_path.is_file():
|
||||
# Reachable two ways, so don't blame steamcmd unconditionally: under
|
||||
# --skip-steamcmd it never runs at all, and nothing else in this tool
|
||||
# creates a manifest from scratch.
|
||||
logger.error(
|
||||
"No %s exists, so there is nothing to finish. This tool only ever "
|
||||
"patches a manifest Steam or steamcmd already wrote; it never "
|
||||
"fabricates one. %s",
|
||||
manifest_path,
|
||||
(
|
||||
"Re-run without --skip-steamcmd so steamcmd creates it (it writes "
|
||||
"one even when it crashes)."
|
||||
if skipped_steamcmd
|
||||
else "steamcmd writes one even when it crashes, so its absence "
|
||||
"means steamcmd never ran far enough."
|
||||
),
|
||||
seed = None
|
||||
if manifest_path.is_file():
|
||||
app_state = recognize.read_app_state(manifest_path)
|
||||
else:
|
||||
# Nothing on disk to patch. Build the manifest in memory and write it
|
||||
# only once every check below has passed: a StateFlags=4 file left
|
||||
# behind by a refused run would make Steam fetch the whole game.
|
||||
logger.info("No %s yet -- writing one from scratch.", manifest_path.name)
|
||||
seed = manifest_write.seed_app_state(
|
||||
metadata.appid, metadata.name, target.name, depot_plan.shared_owner
|
||||
)
|
||||
logger.info("Run log (send this): %s", log.path)
|
||||
return 1
|
||||
|
||||
app_state = recognize.read_app_state(manifest_path)
|
||||
app_state = seed
|
||||
problems = finish_check.check_ready_to_finish(
|
||||
install_dir=target,
|
||||
app_state=app_state,
|
||||
@ -77,6 +68,7 @@ def finish(
|
||||
install_dir=target,
|
||||
now_unix=int(time.time()),
|
||||
dlc_depots=depot_plan.dlc,
|
||||
seed=seed,
|
||||
# Proven depot by depot, so it outranks whatever the manifest says.
|
||||
build_id=(
|
||||
expected_build_id
|
||||
|
||||
@ -66,8 +66,10 @@ def check_ready_to_finish(
|
||||
)
|
||||
if stale:
|
||||
problems.append(
|
||||
f"depot(s) {', '.join(stale)} hold a manifest the public branch no "
|
||||
"longer ships -- stale content"
|
||||
f"depot(s) {', '.join(stale)} hold a manifest other than the public "
|
||||
"gid in Steam's local app cache -- stale content. depotdownloader "
|
||||
"always fetches the current build, so the cache is the stale side: "
|
||||
"start Steam once, let it sync, rerun"
|
||||
)
|
||||
if build_proven(cached, required_depots, expected_manifests):
|
||||
# The manifest's buildid only ever stood in for this proof. A stub
|
||||
|
||||
@ -15,7 +15,6 @@ from steam_installer import (
|
||||
manifest_write,
|
||||
recognize,
|
||||
runlog,
|
||||
steamcmd_validate,
|
||||
steamctl,
|
||||
takeover,
|
||||
)
|
||||
@ -149,50 +148,6 @@ def _download_and_recognize(
|
||||
):
|
||||
logger.debug("depot %s -> manifest %s", depot_id, mid)
|
||||
|
||||
if not args.skip_steamcmd:
|
||||
logger.info("Reconciling with steamcmd (validate)...")
|
||||
log.section("steamcmd app_update validate")
|
||||
result = steamcmd_validate.run_steamcmd_validate(
|
||||
metadata.appid, username, logger
|
||||
)
|
||||
log.file_contents("Manifest after steamcmd", manifest_path)
|
||||
|
||||
# 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, install_target
|
||||
)
|
||||
):
|
||||
# The manifest being valid does not mean steamcmd is why. It can
|
||||
# already have been correct on arrival while steamcmd failed to even
|
||||
# log in -- reporting that as "steamcmd finished cleanly" credits a
|
||||
# step that did nothing and hides a real authentication failure.
|
||||
if result.returncode == 0:
|
||||
logger.info("steamcmd finished cleanly -- Steam should show Play.")
|
||||
else:
|
||||
logger.warning(
|
||||
"steamcmd exited %d, but %s is already a complete install "
|
||||
"-- leaving it as is. Steam should show Play.",
|
||||
result.returncode,
|
||||
manifest_path.name,
|
||||
)
|
||||
logger.info("Run log: %s", log.path)
|
||||
return 0
|
||||
|
||||
if result.crashed_on_install_script:
|
||||
logger.info(
|
||||
"steamcmd hit the known install-script crash; finishing the manifest manually..."
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
"steamcmd exited %d but the manifest doesn't reflect a real install; "
|
||||
"checking whether it is safe to finish it manually...",
|
||||
result.returncode,
|
||||
)
|
||||
|
||||
return finish(
|
||||
log,
|
||||
library,
|
||||
@ -200,5 +155,4 @@ def _download_and_recognize(
|
||||
install_target,
|
||||
depot_plan,
|
||||
expected_build_id,
|
||||
skipped_steamcmd=args.skip_steamcmd,
|
||||
)
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
"""Every write this tool makes to an appmanifest, and only from verified values.
|
||||
|
||||
Split out of recognize.py (250-line cap). When steamcmd's validate cannot
|
||||
finish the job (see :mod:`steamcmd_validate`), the manifest is finished by hand
|
||||
Split out of recognize.py (250-line cap). The manifest is finished by hand
|
||||
using ONLY values already verified: the exact manifest IDs from
|
||||
depotdownloader's own local cache (never guessed), and the real on-disk size
|
||||
measured directly.
|
||||
depotdownloader's own local cache (never guessed), the build those prove, and
|
||||
the real on-disk size measured directly. When no manifest exists it is
|
||||
written from scratch with the 15 keys Steam accepted on 2026-11-22.
|
||||
|
||||
Do NOT leave the download bookkeeping for Steam to tidy up on its next launch.
|
||||
An earlier version did, on the belief that Steam self-corrects; it does not. A
|
||||
@ -26,16 +26,15 @@ from steam_installer.library import SteamLibrary
|
||||
def ensure_installdir_correct(
|
||||
library: SteamLibrary, appid: int, install_dir: Path
|
||||
) -> None:
|
||||
"""Patch a stale `installdir` field BEFORE running steamcmd, not just after.
|
||||
"""Patch a stale `installdir` field before anything else, not just after.
|
||||
|
||||
Confirmed necessary: a prior, unrelated Steam GUI glitch left this app's
|
||||
manifest pointing at a different folder name. steamcmd's own validate run
|
||||
didn't correct it despite a clean exit, and relaunching the Steam client
|
||||
read that same stale field and wiped the correctly-downloaded folder as
|
||||
manifest pointing at a different folder name, and relaunching the Steam
|
||||
client read that stale field and wiped the correctly-downloaded folder as
|
||||
"orphaned" while starting a fresh download into the wrong one. Fixing it
|
||||
proactively, before steamcmd ever runs, closes that window entirely.
|
||||
Does nothing if no manifest exists yet -- steamcmd's own app_update writes
|
||||
one in that case, and it derives installdir from the app's metadata.
|
||||
first, as soon as Steam is closed, closes that window entirely. Does
|
||||
nothing if no manifest exists yet: the one written from scratch takes
|
||||
installdir from the real install folder.
|
||||
"""
|
||||
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
|
||||
if not manifest_path.is_file():
|
||||
@ -66,6 +65,39 @@ def measure_content_size(install_dir: Path) -> int:
|
||||
return total
|
||||
|
||||
|
||||
def seed_app_state(
|
||||
appid: int, name: str, installdir: str, shared_owner: dict[str, str]
|
||||
) -> dict:
|
||||
"""The skeleton of a from-scratch manifest, in the key order Steam writes.
|
||||
|
||||
These 15 keys are the proven minimum: Steam logged "Fully Installed,App
|
||||
Running", queued no download and added LastOwner, UserConfig and the rest
|
||||
itself. Placeholders here are overwritten by `finish_manifest_manually`.
|
||||
SharedDepots is only added for a game that has some (fixture-tested only).
|
||||
"""
|
||||
state: dict = {
|
||||
"appid": str(appid),
|
||||
"universe": "1",
|
||||
"name": name,
|
||||
"StateFlags": "4",
|
||||
"installdir": installdir,
|
||||
"LastUpdated": "0",
|
||||
"SizeOnDisk": "0",
|
||||
"StagingSize": "0",
|
||||
"buildid": "0",
|
||||
"UpdateResult": "0",
|
||||
# All four at 0: counters that disagree made Steam download a game again.
|
||||
"BytesToDownload": "0",
|
||||
"BytesDownloaded": "0",
|
||||
"BytesToStage": "0",
|
||||
"BytesStaged": "0",
|
||||
"InstalledDepots": {},
|
||||
}
|
||||
if shared_owner:
|
||||
state["SharedDepots"] = dict(shared_owner)
|
||||
return state
|
||||
|
||||
|
||||
def finish_manifest_manually(
|
||||
library: SteamLibrary,
|
||||
appid: int,
|
||||
@ -73,26 +105,27 @@ def finish_manifest_manually(
|
||||
now_unix: int,
|
||||
dlc_depots: dict[str, str] | None = None,
|
||||
build_id: str | None = None,
|
||||
seed: dict | None = None,
|
||||
) -> Path:
|
||||
"""Patch appmanifest_<appid>.acf to StateFlags=4 using only verified-known values.
|
||||
|
||||
Requires steamcmd to have already written a partial manifest (even a crashed
|
||||
run does) -- that's where buildid and SharedDepots come from. This never
|
||||
fabricates a manifest from nothing.
|
||||
Patches the manifest on disk, or -- when there is none -- writes `seed`
|
||||
(from :func:`seed_app_state`) instead. With neither it refuses.
|
||||
|
||||
`dlc_depots` maps a DLC depot to its DLC app id. Steam's own manifests
|
||||
record each DLC depot with that `dlcappid`, so it is written the same way.
|
||||
`build_id`, when given, is a build the caller proved from the cached depot
|
||||
manifests; it replaces the one Steam or steamcmd recorded.
|
||||
manifests; it replaces the one Steam recorded.
|
||||
"""
|
||||
manifest_path = library.steamapps / f"appmanifest_{appid}.acf"
|
||||
if not manifest_path.is_file():
|
||||
existed = manifest_path.is_file()
|
||||
if not existed and seed is None:
|
||||
raise recognize.RecognizeError(
|
||||
f"No existing {manifest_path.name} to finish -- run steamcmd's "
|
||||
"app_update at least once first (even a crashed run writes one)."
|
||||
f"No existing {manifest_path.name} to finish and no seed to write"
|
||||
)
|
||||
data = vdf.parse(manifest_path.read_text())
|
||||
app_state = data.get("AppState", {})
|
||||
app_state = (
|
||||
vdf.parse(manifest_path.read_text()).get("AppState", {}) if existed else seed
|
||||
)
|
||||
if not isinstance(app_state, dict):
|
||||
raise recognize.RecognizeError(
|
||||
f"{manifest_path} is not a valid AppState manifest"
|
||||
@ -105,7 +138,7 @@ def finish_manifest_manually(
|
||||
)
|
||||
|
||||
size_on_disk = measure_content_size(install_dir)
|
||||
# "0" means "no real build installed yet" (steamcmd/Steam use it as a
|
||||
# "0" means "no real build installed yet" (Steam uses it as a
|
||||
# sentinel) -- a bare `or` treats that non-empty string as truthy and
|
||||
# never falls through to TargetBuildID, leaving a stale/wrong buildid.
|
||||
existing_buildid = app_state.get("buildid")
|
||||
@ -145,7 +178,8 @@ def finish_manifest_manually(
|
||||
app_state.pop("DlcDownloads", None)
|
||||
settle_download_counters(app_state)
|
||||
|
||||
backup_once(manifest_path)
|
||||
if existed:
|
||||
backup_once(manifest_path)
|
||||
write_atomic(manifest_path, vdf.dumps("AppState", app_state))
|
||||
return manifest_path
|
||||
|
||||
|
||||
@ -1,79 +0,0 @@
|
||||
"""Reconcile an install with Steam's own bookkeeping via `steamcmd ... validate`.
|
||||
|
||||
Split out of recognize.py (250-line cap). `app_update <id> validate` hash-checks
|
||||
existing files against the depot manifest and only fetches real deltas --
|
||||
confirmed live against a real 90+ GB install, going from ~24% to ~41% credited
|
||||
in one pass without redownloading from scratch.
|
||||
|
||||
Some titles ship a post-install script (installscript.vdf) that steamcmd runs
|
||||
through a private, headless Proton setup. On at least one real title this hit a
|
||||
confirmed steamcmd bug -- a stack-overflow segfault in the closed-source
|
||||
steamclient.so error-handling path after the script evaluator reports failure
|
||||
(the same stack frame repeating dozens of times). There's no known fix; the
|
||||
caller detects that crash and finishes the manifest by hand instead
|
||||
(:mod:`manifest_write`).
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import signal
|
||||
from dataclasses import dataclass
|
||||
|
||||
from steam_installer import runlog
|
||||
|
||||
|
||||
@dataclass
|
||||
class ValidateResult:
|
||||
completed: bool
|
||||
crashed_on_install_script: bool
|
||||
returncode: int
|
||||
output: str = ""
|
||||
|
||||
|
||||
def run_steamcmd_validate(
|
||||
appid: int,
|
||||
username: str,
|
||||
logger: logging.Logger,
|
||||
) -> ValidateResult:
|
||||
"""Run steamcmd's app_update ... validate.
|
||||
|
||||
Takes no install dir on purpose. `force_install_dir` sets a Steam *library
|
||||
root*, not an install directory: pointing it at the game folder made
|
||||
steamcmd look for manifests in `<game>/steamapps/`, find none ("Loaded 0
|
||||
apps from install folder"), create that stray directory, and then fail the
|
||||
app with "Invalid platform" because it had no depot context for it. The app
|
||||
already lives in a registered library, so letting steamcmd use the real one
|
||||
is both correct and sufficient.
|
||||
|
||||
Output is teed through a pty (see runlog) rather than captured: steamcmd
|
||||
prompts for a password and possibly a Steam Guard code on stdin, and prompt
|
||||
text written to a captured/buffered pipe never reaches the terminal -- the
|
||||
process looks hung with no visible request for input.
|
||||
|
||||
The confirmed steamcmd install-script bug (see module docstring) kills
|
||||
the process with SIGSEGV -- Python reports that as returncode -11 on
|
||||
POSIX. Checking the signal directly is more reliable than grepping
|
||||
captured text for a message string.
|
||||
"""
|
||||
cmd = [
|
||||
"steamcmd",
|
||||
# A Linux steamcmd refuses a Windows-only app with "Invalid platform"
|
||||
# unless told which platform to resolve depots for. Must precede +login.
|
||||
"+@sSteamCmdForcePlatformType",
|
||||
"windows",
|
||||
"+login",
|
||||
username,
|
||||
"+app_update",
|
||||
str(appid),
|
||||
"validate",
|
||||
"+quit",
|
||||
]
|
||||
returncode, output = runlog.run_logged(cmd, logger, timeout=3600)
|
||||
crashed = returncode == -signal.SIGSEGV
|
||||
return ValidateResult(
|
||||
completed=returncode == 0,
|
||||
crashed_on_install_script=crashed,
|
||||
returncode=returncode,
|
||||
output=output,
|
||||
)
|
||||
49
_installer_import/tests/_appinfo_blob.py
Normal file
49
_installer_import/tests/_appinfo_blob.py
Normal file
@ -0,0 +1,49 @@
|
||||
"""Build a binary appinfo.vdf (magic 0x07564429) for tests.
|
||||
|
||||
The writer mirrors what Steam stores: a header, one entry per app (appid,
|
||||
size, 60 header bytes, binary key-values whose keys index a string table),
|
||||
a zero appid terminator, then the string table itself.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import struct
|
||||
|
||||
MAGIC = 0x07564429
|
||||
|
||||
|
||||
def _kv(data: dict, strings: dict[str, int]) -> bytes:
|
||||
out = bytearray()
|
||||
for key, value in data.items():
|
||||
index = strings.setdefault(key, len(strings))
|
||||
if isinstance(value, dict):
|
||||
out += b"\x00" + struct.pack("<I", index) + _kv(value, strings)
|
||||
elif isinstance(value, int):
|
||||
out += b"\x02" + struct.pack("<I", index) + struct.pack("<i", value)
|
||||
elif isinstance(value, float):
|
||||
out += b"\x03" + struct.pack("<I", index) + struct.pack("<f", value)
|
||||
elif isinstance(value, tuple):
|
||||
kind, raw = value # (7, u64) / (10, i64): the wide integer types
|
||||
fmt = "<Q" if kind == 7 else "<q"
|
||||
out += bytes([kind]) + struct.pack("<I", index) + struct.pack(fmt, raw)
|
||||
else:
|
||||
out += b"\x01" + struct.pack("<I", index) + value.encode() + b"\x00"
|
||||
return bytes(out) + b"\x08"
|
||||
|
||||
|
||||
def encode(apps: dict[int, dict], change_number: int = 41062472) -> bytes:
|
||||
"""Serialize `{appid: appinfo-dict}`; each dict is wrapped in "appinfo"."""
|
||||
strings: dict[str, int] = {}
|
||||
body = bytearray()
|
||||
for appid, data in apps.items():
|
||||
kv = _kv({"appinfo": data}, strings)
|
||||
header = struct.pack("<IIQ20sI20s", 2, 1795000000, 0, b"\0" * 20,
|
||||
change_number, b"\0" * 20)
|
||||
payload = header + kv
|
||||
body += struct.pack("<II", appid, len(payload)) + payload
|
||||
body += struct.pack("<I", 0)
|
||||
table_offset = 16 + len(body)
|
||||
table = struct.pack("<I", len(strings)) + b"".join(
|
||||
key.encode() + b"\x00" for key in strings
|
||||
)
|
||||
return struct.pack("<IIq", MAGIC, 1, table_offset) + bytes(body) + table
|
||||
@ -1,21 +1,9 @@
|
||||
"""Stubs shared by the CLI flow tests (plain functions, not fixtures)."""
|
||||
|
||||
from steam_installer import depotdl, steamcmd_validate
|
||||
from steam_installer import depotdl
|
||||
|
||||
ARGV = ["--appid", "1771300", "--username", "u", "--language", "english"]
|
||||
|
||||
|
||||
def _stub_download(monkeypatch, returncode=0):
|
||||
monkeypatch.setattr(depotdl, "download", lambda **kw: returncode)
|
||||
|
||||
|
||||
def _stub_steamcmd(monkeypatch, returncode, crashed=False):
|
||||
monkeypatch.setattr(
|
||||
steamcmd_validate,
|
||||
"run_steamcmd_validate",
|
||||
lambda appid, username, logger: steamcmd_validate.ValidateResult(
|
||||
completed=returncode == 0,
|
||||
crashed_on_install_script=crashed,
|
||||
returncode=returncode,
|
||||
),
|
||||
)
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
import pytest
|
||||
from _appinfo_blob import encode
|
||||
|
||||
from steam_installer import appinfo
|
||||
|
||||
@ -127,114 +128,77 @@ def test_get_metadata_rejects_malformed_common_section(monkeypatch):
|
||||
appinfo.get_metadata(1)
|
||||
|
||||
|
||||
class _FakeCompleted:
|
||||
def __init__(self, stdout):
|
||||
self.stdout = stdout
|
||||
self.returncode = 0
|
||||
|
||||
|
||||
# What steamcmd actually emits: its own log lines, then the VDF block.
|
||||
STEAMCMD_OUTPUT = """Redirecting stderr to '/home/u/.steam/logs/stderr.txt'
|
||||
Logging directory: '/home/u/.steam/logs'
|
||||
"@sSteamCmdForcePlatformType" = "windows"
|
||||
Logging in user 'anonymous' to Steam Public...OK
|
||||
"1771300"
|
||||
{
|
||||
\t"common"
|
||||
\t{
|
||||
\t\t"name"\t\t"Kingdom Come: Deliverance II"
|
||||
\t\t"installdir"\t\t"KingdomComeDeliverance2"
|
||||
\t}
|
||||
\t"depots"
|
||||
\t{
|
||||
\t\t"1771303"
|
||||
\t\t{
|
||||
\t\t\t"config"
|
||||
\t\t\t{
|
||||
\t\t\t\t"oslist"\t\t"windows"
|
||||
\t\t\t}
|
||||
\t\t}
|
||||
\t\t"branches"
|
||||
\t\t{
|
||||
\t\t\t"public"
|
||||
\t\t\t{
|
||||
\t\t\t\t"buildid"\t\t"23914554"
|
||||
\t\t\t}
|
||||
\t\t}
|
||||
\t}
|
||||
# The block steamcmd's app_info_print used to return for KCD2, now as Steam
|
||||
# keeps it in its binary cache. The differential tests below pin that the
|
||||
# cache gives the same answers the steamcmd text did.
|
||||
KCD2 = {
|
||||
"common": {"name": "Kingdom Come: Deliverance II", "installdir": "KingdomComeDeliverance2"},
|
||||
"depots": {
|
||||
"1771303": {"config": {"oslist": "windows"}},
|
||||
"branches": {"public": {"buildid": 23914554}},
|
||||
},
|
||||
}
|
||||
"""
|
||||
|
||||
|
||||
def test_fetch_app_block_parses_real_steamcmd_output(monkeypatch):
|
||||
seen = {}
|
||||
@pytest.fixture
|
||||
def cache(tmp_path, monkeypatch):
|
||||
"""Point appinfo at a fixture cache; returns a writer for its apps."""
|
||||
path = tmp_path / "appinfo.vdf"
|
||||
monkeypatch.setattr(appinfo.appinfo_cache, "default_path", lambda: path)
|
||||
|
||||
def fake_run(cmd, **kwargs):
|
||||
seen["cmd"] = cmd
|
||||
return _FakeCompleted(STEAMCMD_OUTPUT)
|
||||
def write(apps):
|
||||
path.write_bytes(encode(apps))
|
||||
|
||||
monkeypatch.setattr(appinfo.subprocess, "run", fake_run)
|
||||
return write
|
||||
|
||||
|
||||
def test_metadata_comes_from_steams_cache(cache):
|
||||
cache({1771300: KCD2})
|
||||
metadata = appinfo.get_metadata(1771300)
|
||||
|
||||
assert metadata.name == "Kingdom Come: Deliverance II"
|
||||
assert metadata.installdir == "KingdomComeDeliverance2"
|
||||
# The platform flag must precede +login or a Windows-only app comes back empty.
|
||||
cmd = seen["cmd"]
|
||||
assert cmd.index("+@sSteamCmdForcePlatformType") < cmd.index("+login")
|
||||
assert cmd[cmd.index("+@sSteamCmdForcePlatformType") + 1] == "windows"
|
||||
|
||||
|
||||
def test_get_build_id_through_the_real_parser(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
appinfo.subprocess, "run", lambda cmd, **kw: _FakeCompleted(STEAMCMD_OUTPUT)
|
||||
)
|
||||
def test_get_build_id_through_the_real_parser(cache):
|
||||
# Stored as an int32 in the binary cache; still a string like steamcmd's.
|
||||
cache({1771300: KCD2})
|
||||
assert appinfo.get_build_id(1771300) == "23914554"
|
||||
|
||||
|
||||
def test_fetch_app_block_raises_when_appid_header_absent(monkeypatch):
|
||||
"""steamcmd prints its usual log noise even for an unknown app; without the
|
||||
`"<appid>"` header there is no app info to parse."""
|
||||
monkeypatch.setattr(
|
||||
appinfo.subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: _FakeCompleted("Logging in user 'anonymous'...OK\n"),
|
||||
)
|
||||
with pytest.raises(appinfo.AppInfoError, match="did not return app info"):
|
||||
def test_app_missing_from_the_cache_fails_with_the_fix(cache):
|
||||
cache({1771300: KCD2})
|
||||
with pytest.raises(appinfo.AppInfoError, match="start Steam once, let it sync, rerun"):
|
||||
appinfo.get_metadata(999999999)
|
||||
|
||||
|
||||
def test_fetch_app_block_raises_when_block_is_not_a_mapping(monkeypatch):
|
||||
"""Defensive guard against a shape vdf.parse cannot currently produce (it
|
||||
always yields a dict for a `{ ... }` block). Exercised by injecting a parser
|
||||
that returns a scalar, so the guard stays correct if vdf ever changes."""
|
||||
def test_unreadable_cache_fails_with_the_fix(tmp_path, monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
appinfo.subprocess,
|
||||
"run",
|
||||
lambda cmd, **kw: _FakeCompleted('"1771300"\n{\n}\n'),
|
||||
appinfo.appinfo_cache, "default_path", lambda: tmp_path / "missing.vdf"
|
||||
)
|
||||
monkeypatch.setattr(appinfo.vdf, "parse", lambda text: {"1771300": "scalar"})
|
||||
with pytest.raises(appinfo.AppInfoError, match="start Steam once"):
|
||||
appinfo.get_metadata(1)
|
||||
|
||||
with pytest.raises(appinfo.AppInfoError, match="unexpected app_info_print"):
|
||||
appinfo.get_metadata(1771300)
|
||||
|
||||
def test_dlc_missing_from_the_cache_counts_as_not_owned(cache):
|
||||
"""Steam caches app info for what the account owns; on 2026-11-22 only 1828
|
||||
of 7102 listed DLC ids were in the cache. Refusing on a missing one would
|
||||
block installs that worked with steamcmd."""
|
||||
game = {**KCD2, "extended": {"listofdlc": "500,600"}}
|
||||
dlc = {"depots": {"501": {"manifests": {"public": {"gid": "9"}}}}}
|
||||
cache({1771300: game, 500: dlc})
|
||||
assert appinfo.get_dlc_plans(1771300) == {500: {"501": "9"}}
|
||||
|
||||
|
||||
def test_get_build_id_handles_non_dict_sections(monkeypatch):
|
||||
for text in (
|
||||
'"1"\n{\n\t"depots"\t\t"scalar"\n}\n',
|
||||
'"1"\n{\n\t"depots"\n\t{\n\t\t"branches"\t\t"scalar"\n\t}\n}\n',
|
||||
'"1"\n{\n\t"depots"\n\t{\n\t\t"branches"\n\t\t{\n\t\t\t"public"\t\t"x"\n\t\t}\n\t}\n}\n',
|
||||
for block in (
|
||||
{"depots": "scalar"},
|
||||
{"depots": {"branches": "scalar"}},
|
||||
{"depots": {"branches": {"public": "x"}}},
|
||||
):
|
||||
monkeypatch.setattr(
|
||||
appinfo.subprocess, "run", lambda cmd, _t=text, **kw: _FakeCompleted(_t)
|
||||
)
|
||||
_app_block(monkeypatch, block)
|
||||
assert appinfo.get_build_id(1) is None
|
||||
|
||||
|
||||
def test_get_build_id_returns_none_when_buildid_not_a_string(monkeypatch):
|
||||
text = '"1"\n{\n\t"depots"\n\t{\n\t\t"branches"\n\t\t{\n\t\t\t"public"\n\t\t\t{\n\t\t\t\t"buildid"\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n'
|
||||
monkeypatch.setattr(
|
||||
appinfo.subprocess, "run", lambda cmd, **kw: _FakeCompleted(text)
|
||||
)
|
||||
_app_block(monkeypatch, {"depots": {"branches": {"public": {"buildid": {}}}}})
|
||||
assert appinfo.get_build_id(1) is None
|
||||
|
||||
|
||||
81
_installer_import/tests/test_appinfo_cache.py
Normal file
81
_installer_import/tests/test_appinfo_cache.py
Normal file
@ -0,0 +1,81 @@
|
||||
"""Steam's local binary app-info cache, read without steamcmd."""
|
||||
|
||||
import pytest
|
||||
from steam_installer import appinfo_cache
|
||||
|
||||
from _appinfo_blob import encode
|
||||
|
||||
|
||||
def _write(tmp_path, apps):
|
||||
path = tmp_path / "appinfo.vdf"
|
||||
path.write_bytes(encode(apps))
|
||||
return path
|
||||
|
||||
|
||||
def test_reads_every_app_and_unwraps_appinfo(tmp_path):
|
||||
path = _write(tmp_path, {10: {"common": {"name": "A"}}, 20: {"common": {}}})
|
||||
apps = appinfo_cache.load(path)
|
||||
assert set(apps) == {10, 20}
|
||||
assert apps[10] == {"common": {"name": "A"}}
|
||||
|
||||
|
||||
def test_scalars_come_back_as_strings_like_app_info_print(tmp_path):
|
||||
"""The rest of the tool compares `== "1"` and `!= str(appid)`; an int here
|
||||
would silently drop a DLC app's own depots."""
|
||||
data = {"depots": {"5": {"dlcappid": 7, "optional": 1, "f": 1.5,
|
||||
"gid": (7, 2**63 + 5), "neg": (10, -3)}}}
|
||||
apps = appinfo_cache.load(_write(tmp_path, {7: data}))
|
||||
assert apps[7]["depots"]["5"] == {
|
||||
"dlcappid": "7", "optional": "1", "f": "1.5",
|
||||
"gid": str(2**63 + 5), "neg": "-3",
|
||||
}
|
||||
|
||||
|
||||
def test_parse_is_cached_until_the_file_changes(tmp_path, monkeypatch):
|
||||
path = _write(tmp_path, {1: {"common": {"name": "old"}}})
|
||||
assert appinfo_cache.load(path)[1]["common"]["name"] == "old"
|
||||
calls = []
|
||||
real = appinfo_cache._parse
|
||||
monkeypatch.setattr(appinfo_cache, "_parse", lambda b: calls.append(1) or real(b))
|
||||
appinfo_cache.load(path)
|
||||
assert calls == []
|
||||
path.write_bytes(encode({1: {"common": {"name": "newer"}}}))
|
||||
assert appinfo_cache.load(path)[1]["common"]["name"] == "newer"
|
||||
assert calls == [1]
|
||||
|
||||
|
||||
def test_wrong_magic_is_refused(tmp_path):
|
||||
path = tmp_path / "appinfo.vdf"
|
||||
path.write_bytes(b"\x28\x44\x56\x07" + b"\0" * 20)
|
||||
with pytest.raises(appinfo_cache.CacheError, match="unsupported"):
|
||||
appinfo_cache.load(path)
|
||||
|
||||
|
||||
def test_unknown_value_type_is_refused(tmp_path):
|
||||
blob = bytearray(encode({1: {"common": {"name": "A"}}}))
|
||||
blob[16 + 8 + 60] = 9 # first KV type byte
|
||||
path = tmp_path / "appinfo.vdf"
|
||||
path.write_bytes(bytes(blob))
|
||||
with pytest.raises(appinfo_cache.CacheError, match="value type 9"):
|
||||
appinfo_cache.load(path)
|
||||
|
||||
|
||||
def test_missing_file_names_the_fix(tmp_path):
|
||||
with pytest.raises(appinfo_cache.CacheError, match="start Steam once"):
|
||||
appinfo_cache.load(tmp_path / "nope.vdf")
|
||||
|
||||
|
||||
def test_entry_without_appinfo_wrapper_is_taken_as_is(tmp_path):
|
||||
import _appinfo_blob
|
||||
|
||||
blob = _appinfo_blob.encode({3: {"x": "y"}})
|
||||
# Rename the "appinfo" wrapper key in the string table.
|
||||
blob = blob.replace(b"appinfo\x00", b"appinfX\x00")
|
||||
path = tmp_path / "appinfo.vdf"
|
||||
path.write_bytes(blob)
|
||||
assert appinfo_cache.load(path)[3] == {"appinfX": {"x": "y"}}
|
||||
|
||||
|
||||
def test_default_path_is_under_the_steam_root(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(appinfo_cache, "find_steam_root", lambda: tmp_path)
|
||||
assert appinfo_cache.default_path() == tmp_path / "appcache" / "appinfo.vdf"
|
||||
@ -1,7 +1,7 @@
|
||||
"""End-to-end-ish tests for the CLI's decision logic.
|
||||
|
||||
The bug that motivated all of this lived here, not in the manifest writer:
|
||||
steamcmd died instantly having done nothing, and main() still wrote
|
||||
a step died instantly having done nothing, and main() still wrote
|
||||
StateFlags=4, printed "launch Steam and confirm Play works" and returned 0.
|
||||
These tests pin the fail-closed behaviour.
|
||||
"""
|
||||
@ -13,21 +13,19 @@ from steam_installer import (
|
||||
depotdl,
|
||||
manifest_write,
|
||||
recognize,
|
||||
steamcmd_validate,
|
||||
vdf,
|
||||
)
|
||||
|
||||
from _cli_helpers import ARGV, _stub_download, _stub_steamcmd
|
||||
from _cli_helpers import ARGV, _stub_download
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("cli_logs")
|
||||
|
||||
|
||||
def test_dead_steamcmd_run_still_finishes_when_content_is_present(steam, monkeypatch):
|
||||
"""steamcmd failing is survivable -- the content is what matters -- but the
|
||||
manifest must come out consistent, not merely flagged installed."""
|
||||
def test_downloaded_content_finishes_the_manifest(steam, monkeypatch):
|
||||
"""The content is what matters, and the manifest must come out consistent,
|
||||
not merely flagged installed."""
|
||||
library, install_dir = steam
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
|
||||
@ -42,7 +40,6 @@ def test_refuses_when_a_required_depot_never_downloaded(steam, monkeypatch):
|
||||
app installed would be a fabricated success."""
|
||||
library, install_dir = steam
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
monkeypatch.setattr(
|
||||
appinfo,
|
||||
"get_depot_plan",
|
||||
@ -58,7 +55,6 @@ def test_refuses_when_a_required_depot_never_downloaded(steam, monkeypatch):
|
||||
def test_refuses_when_depotdownloader_fails(steam, monkeypatch):
|
||||
library, _ = steam
|
||||
_stub_download(monkeypatch, returncode=1)
|
||||
_stub_steamcmd(monkeypatch, returncode=0)
|
||||
|
||||
assert cli.main(ARGV) == 1
|
||||
app_state = recognize.read_app_state(library.steamapps / "appmanifest_1771300.acf")
|
||||
@ -79,79 +75,6 @@ def test_declining_to_close_steam_aborts_before_downloading(steam, monkeypatch):
|
||||
assert called == []
|
||||
|
||||
|
||||
def test_clean_steamcmd_run_short_circuits(steam, monkeypatch):
|
||||
"""If steamcmd genuinely finished the job, don't hand-patch anything."""
|
||||
library, install_dir = steam
|
||||
_stub_download(monkeypatch)
|
||||
|
||||
def fake_validate(appid, username, logger):
|
||||
(library.steamapps / "appmanifest_1771300.acf").write_text(
|
||||
vdf.dumps(
|
||||
"AppState",
|
||||
{
|
||||
"StateFlags": "4",
|
||||
"installdir": "KingdomComeDeliverance2",
|
||||
"InstalledDepots": {"1771303": {"manifest": "1"}},
|
||||
"BytesToDownload": "1",
|
||||
"BytesDownloaded": "1",
|
||||
},
|
||||
)
|
||||
)
|
||||
return steamcmd_validate.ValidateResult(True, False, 0)
|
||||
|
||||
monkeypatch.setattr(steamcmd_validate, "run_steamcmd_validate", fake_validate)
|
||||
called = []
|
||||
monkeypatch.setattr(
|
||||
manifest_write,
|
||||
"finish_manifest_manually",
|
||||
lambda **kw: called.append(1),
|
||||
)
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
assert called == []
|
||||
|
||||
|
||||
def test_a_complete_install_after_a_failed_steamcmd_is_not_credited_to_it(
|
||||
steam, monkeypatch, capsys
|
||||
):
|
||||
"""A valid manifest does not mean steamcmd is why it is valid.
|
||||
|
||||
steamcmd can fail to even log in while the install was already complete on
|
||||
arrival. Reporting that as "finished cleanly" credits a step that did
|
||||
nothing and hides a real authentication failure, so the non-zero exit has
|
||||
to say so out loud.
|
||||
"""
|
||||
library, _ = steam
|
||||
_stub_download(monkeypatch)
|
||||
|
||||
def fake_validate(appid, username, logger):
|
||||
(library.steamapps / "appmanifest_1771300.acf").write_text(
|
||||
vdf.dumps(
|
||||
"AppState",
|
||||
{
|
||||
"StateFlags": "4",
|
||||
"installdir": "KingdomComeDeliverance2",
|
||||
"InstalledDepots": {"1771303": {"manifest": "1"}},
|
||||
"BytesToDownload": "1",
|
||||
"BytesDownloaded": "1",
|
||||
},
|
||||
)
|
||||
)
|
||||
return steamcmd_validate.ValidateResult(True, False, 5)
|
||||
|
||||
monkeypatch.setattr(steamcmd_validate, "run_steamcmd_validate", fake_validate)
|
||||
called = []
|
||||
monkeypatch.setattr(
|
||||
manifest_write, "finish_manifest_manually", lambda **kw: called.append(1)
|
||||
)
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
out = capsys.readouterr().out
|
||||
assert "steamcmd exited 5" in out
|
||||
assert "finished cleanly" not in out
|
||||
assert called == []
|
||||
|
||||
|
||||
def test_user_declining_confirmation_aborts(steam, monkeypatch):
|
||||
monkeypatch.setattr("builtins.input", lambda *a: "n")
|
||||
called = []
|
||||
|
||||
@ -11,36 +11,87 @@ from steam_installer import (
|
||||
vdf,
|
||||
)
|
||||
|
||||
from _cli_helpers import ARGV, _stub_download, _stub_steamcmd
|
||||
from _cli_helpers import ARGV, _stub_download
|
||||
|
||||
pytestmark = pytest.mark.usefixtures("cli_logs")
|
||||
|
||||
|
||||
def test_missing_manifest_under_skip_steamcmd_does_not_blame_steamcmd(
|
||||
steam, monkeypatch, capsys
|
||||
):
|
||||
"""--skip-steamcmd never runs steamcmd, so "steamcmd never ran far enough"
|
||||
would send the user chasing the wrong thing."""
|
||||
library, _ = steam
|
||||
(library.steamapps / "appmanifest_1771300.acf").unlink()
|
||||
# The 15 keys Steam accepted on 2026-11-22 (A Space for the Unbound): it
|
||||
# logged "Fully Installed,App Running", queued no download, and added its own
|
||||
# LastOwner, UserConfig, MountedConfig, ... on exit.
|
||||
FROM_SCRATCH_KEYS = [
|
||||
"appid", "universe", "name", "StateFlags", "installdir", "LastUpdated",
|
||||
"SizeOnDisk", "StagingSize", "buildid", "UpdateResult", "BytesToDownload",
|
||||
"BytesDownloaded", "BytesToStage", "BytesStaged", "InstalledDepots",
|
||||
]
|
||||
|
||||
|
||||
def _proven_plan(monkeypatch, install_dir, shared_owner=None):
|
||||
(gid,) = recognize.discover_cached_depot_manifests(install_dir).values()
|
||||
monkeypatch.setattr(
|
||||
appinfo,
|
||||
"get_depot_plan",
|
||||
lambda appid, language: appinfo.DepotPlan(
|
||||
{"1771303"}, set(shared_owner or ()), manifests={"1771303": gid},
|
||||
shared_owner=dict(shared_owner or {}),
|
||||
),
|
||||
)
|
||||
return gid
|
||||
|
||||
|
||||
def test_missing_manifest_is_written_from_scratch(steam, monkeypatch):
|
||||
"""No steamcmd any more, so nothing else creates the manifest."""
|
||||
library, install_dir = steam
|
||||
manifest = library.steamapps / "appmanifest_1771300.acf"
|
||||
manifest.unlink()
|
||||
gid = _proven_plan(monkeypatch, install_dir)
|
||||
_stub_download(monkeypatch)
|
||||
|
||||
assert cli.main([*ARGV, "--skip-steamcmd"]) == 1
|
||||
assert cli.main(ARGV) == 0
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert "never fabricates one" in out
|
||||
assert "Re-run without --skip-steamcmd" in out
|
||||
assert "steamcmd never ran far enough" not in out
|
||||
app_state = vdf.parse(manifest.read_text())["AppState"]
|
||||
assert list(app_state) == FROM_SCRATCH_KEYS
|
||||
assert app_state["appid"] == "1771300"
|
||||
assert app_state["universe"] == "1"
|
||||
assert app_state["name"] == "KCD2"
|
||||
assert app_state["StateFlags"] == "4"
|
||||
assert app_state["installdir"] == "KingdomComeDeliverance2"
|
||||
assert app_state["SizeOnDisk"] == "1000"
|
||||
assert app_state["buildid"] == "23914554"
|
||||
for key in ("StagingSize", "UpdateResult", "BytesToDownload",
|
||||
"BytesDownloaded", "BytesToStage", "BytesStaged"):
|
||||
assert app_state[key] == "0", key
|
||||
assert app_state["InstalledDepots"] == {"1771303": {"manifest": gid, "size": "0"}}
|
||||
# Nothing to back up: there was no manifest before.
|
||||
assert not manifest.with_suffix(".acf.bak").exists()
|
||||
|
||||
|
||||
def test_missing_manifest_with_steamcmd_blames_steamcmd(steam, monkeypatch, capsys):
|
||||
library, _ = steam
|
||||
(library.steamapps / "appmanifest_1771300.acf").unlink()
|
||||
def test_a_shared_depot_is_recorded_under_its_owner_from_scratch(steam, monkeypatch):
|
||||
"""Fixture-only: no shared-depot game was tested live. Steam records a
|
||||
depot installed under another app as SharedDepots {depot: owner}."""
|
||||
library, install_dir = steam
|
||||
manifest = library.steamapps / "appmanifest_1771300.acf"
|
||||
manifest.unlink()
|
||||
_proven_plan(monkeypatch, install_dir, shared_owner={"228989": "228980"})
|
||||
_stub_download(monkeypatch)
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
app_state = vdf.parse(manifest.read_text())["AppState"]
|
||||
assert app_state["SharedDepots"] == {"228989": "228980"}
|
||||
assert "228989" not in app_state["InstalledDepots"]
|
||||
|
||||
|
||||
def test_unproven_content_writes_no_manifest_from_scratch(steam, monkeypatch, capsys):
|
||||
"""Fail closed: without a proven build there is no buildid to write, and a
|
||||
half-true StateFlags=4 manifest is what made Steam redownload games."""
|
||||
library, _ = steam
|
||||
manifest = library.steamapps / "appmanifest_1771300.acf"
|
||||
manifest.unlink()
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
|
||||
assert cli.main(ARGV) == 1
|
||||
assert "steamcmd never ran far enough" in capsys.readouterr().out
|
||||
assert not manifest.exists()
|
||||
assert "no usable buildid" in capsys.readouterr().out
|
||||
|
||||
|
||||
def test_language_defaults_to_english_when_prompt_is_empty(steam, monkeypatch):
|
||||
@ -56,21 +107,12 @@ def test_language_defaults_to_english_when_prompt_is_empty(steam, monkeypatch):
|
||||
),
|
||||
)
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
|
||||
cli.main(["--appid", "1771300", "--username", "u"])
|
||||
|
||||
assert seen["language"] == "english"
|
||||
|
||||
|
||||
def test_reports_the_known_install_script_crash(steam, monkeypatch, capsys):
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=-11, crashed=True)
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
assert "known install-script crash" in capsys.readouterr().out
|
||||
|
||||
|
||||
def test_adopts_staged_content_then_clears_it(steam, monkeypatch, capsys):
|
||||
"""Steam's staging tree is adopted into the install dir before downloading,
|
||||
and only cleared once the install is finished -- never discarded up front."""
|
||||
@ -79,7 +121,6 @@ def test_adopts_staged_content_then_clears_it(steam, monkeypatch, capsys):
|
||||
staged.mkdir(parents=True)
|
||||
(staged / "big.pak").write_bytes(b"x" * 2_000_000)
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
|
||||
@ -96,7 +137,6 @@ def test_fails_when_written_manifest_does_not_read_back_as_installed(
|
||||
"""Claiming success without re-reading the write is the same fail-open
|
||||
mistake in a different place."""
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
monkeypatch.setattr(
|
||||
recognize, "manifest_reflects_real_install", lambda *a, **kw: False
|
||||
)
|
||||
@ -116,7 +156,7 @@ def test_module_is_runnable_as_a_script(coverage_subprocess_env):
|
||||
env=coverage_subprocess_env,
|
||||
)
|
||||
assert result.returncode == 0
|
||||
assert "--skip-steamcmd" in result.stdout
|
||||
assert "--skip-steamcmd" not in result.stdout
|
||||
|
||||
|
||||
def test_a_stub_manifest_is_finished_with_the_proven_build(steam, monkeypatch):
|
||||
@ -138,7 +178,7 @@ def test_a_stub_manifest_is_finished_with_the_proven_build(steam, monkeypatch):
|
||||
)
|
||||
_stub_download(monkeypatch)
|
||||
|
||||
assert cli.main([*ARGV, "--skip-steamcmd"]) == 0
|
||||
assert cli.main(ARGV) == 0
|
||||
app_state = vdf.parse(manifest.read_text())["AppState"]
|
||||
assert (app_state["StateFlags"], app_state["buildid"]) == ("4", "23914554")
|
||||
|
||||
@ -156,5 +196,5 @@ def test_the_install_folder_is_corrected_even_when_the_download_fails(
|
||||
manifest.write_text(vdf.dumps("AppState", state))
|
||||
_stub_download(monkeypatch, returncode=2)
|
||||
|
||||
assert cli.main([*ARGV, "--skip-steamcmd"]) == 1
|
||||
assert cli.main(ARGV) == 1
|
||||
assert recognize.read_app_state(manifest)["installdir"] == "KingdomComeDeliverance2"
|
||||
|
||||
@ -5,27 +5,26 @@ import pytest
|
||||
from steam_installer import deps
|
||||
|
||||
|
||||
def test_nothing_missing_when_both_tools_are_on_path(monkeypatch):
|
||||
def test_nothing_missing_when_depotdownloader_is_on_path(monkeypatch):
|
||||
monkeypatch.setattr(deps.shutil, "which", lambda name: f"/usr/bin/{name}")
|
||||
assert deps.missing_tools(skip_steamcmd=False) == []
|
||||
deps.require(skip_steamcmd=False)
|
||||
assert deps.missing_tools() == []
|
||||
deps.require()
|
||||
|
||||
|
||||
def test_steamcmd_is_not_required_when_it_is_skipped(monkeypatch):
|
||||
def test_steamcmd_is_never_required(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
deps.shutil, "which", lambda name: None if name == "steamcmd" else "/usr/bin/dd"
|
||||
)
|
||||
assert deps.missing_tools(skip_steamcmd=True) == []
|
||||
assert deps.missing_tools(skip_steamcmd=False) == ["steamcmd"]
|
||||
assert deps.missing_tools() == []
|
||||
|
||||
|
||||
def test_missing_tool_raises_with_the_command_that_fixes_it(monkeypatch):
|
||||
monkeypatch.setattr(deps.shutil, "which", lambda name: None)
|
||||
|
||||
with pytest.raises(deps.DependencyError) as excinfo:
|
||||
deps.require(skip_steamcmd=False)
|
||||
deps.require()
|
||||
|
||||
message = str(excinfo.value)
|
||||
assert "depotdownloader" in message
|
||||
assert "steamcmd" in message
|
||||
assert "steamcmd" not in message
|
||||
assert "./install.sh" in message
|
||||
|
||||
@ -6,10 +6,9 @@ own app id; 2060310 is listed in the game's own depots; 2441600 has none.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
from _cli_helpers import ARGV, _stub_download, _stub_steamcmd
|
||||
from _cli_helpers import ARGV, _stub_download
|
||||
|
||||
from steam_installer import appinfo, cli, depotdl, deps, dlc, recognize, runlog, vdf
|
||||
|
||||
@ -42,7 +41,7 @@ DLC_BLOCKS = {
|
||||
def _app_info(monkeypatch, game=GAME, dlc_blocks=DLC_BLOCKS):
|
||||
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game)
|
||||
monkeypatch.setattr(
|
||||
appinfo, "_fetch_app_blocks", lambda ids: {i: dlc_blocks[i] for i in ids}
|
||||
appinfo, "_cached_blocks", lambda ids: {i: dlc_blocks[i] for i in ids}
|
||||
)
|
||||
|
||||
|
||||
@ -54,7 +53,7 @@ def test_only_dlc_with_depots_of_its_own_needs_a_run(monkeypatch):
|
||||
@pytest.mark.parametrize("game", [{}, {"extended": "scalar"}, {"extended": {}}])
|
||||
def test_a_game_without_dlc_needs_no_lookup(monkeypatch, game):
|
||||
monkeypatch.setattr(appinfo, "_fetch_app_block", lambda appid: game)
|
||||
monkeypatch.setattr(appinfo, "_fetch_app_blocks", lambda ids: pytest.fail())
|
||||
monkeypatch.setattr(appinfo, "_cached_blocks", lambda ids: pytest.fail())
|
||||
assert appinfo.get_dlc_plans(1) == {}
|
||||
|
||||
|
||||
@ -70,25 +69,6 @@ def test_depot_plan_tags_the_games_own_dlc_depots(monkeypatch):
|
||||
assert plan.dlc == {"2060310": "2060310"}
|
||||
|
||||
|
||||
def test_app_info_for_many_apps_comes_from_one_steamcmd_call(monkeypatch):
|
||||
output = (
|
||||
'Loading...\n"2060310"\n{\n\t"common"\n\t{\n\t\t"name"\t"A"\n\t}\n}\n'
|
||||
'"2138330"\n{\n\t"common"\n\t{\n\t\t"name"\t"B"\n\t}\n}\nUnloading\n'
|
||||
)
|
||||
calls = []
|
||||
|
||||
def fake_run(cmd, **kwargs):
|
||||
calls.append(cmd)
|
||||
return subprocess.CompletedProcess(cmd, 0, stdout=output, stderr="")
|
||||
|
||||
monkeypatch.setattr(appinfo.subprocess, "run", fake_run)
|
||||
blocks = appinfo._fetch_app_blocks([2138330, 2060310])
|
||||
assert blocks[2060310]["common"]["name"] == "A"
|
||||
assert blocks[2138330]["common"]["name"] == "B"
|
||||
assert len(calls) == 1
|
||||
assert calls[0].count("+app_info_print") == 2
|
||||
|
||||
|
||||
def _logged(monkeypatch, returncode, output=""):
|
||||
monkeypatch.setattr(
|
||||
depotdl.runlog, "run_logged", lambda cmd, logger: (returncode, output)
|
||||
@ -163,7 +143,6 @@ def test_downloaded_dlc_is_required_and_credited_to_its_app(steam, monkeypatch):
|
||||
}
|
||||
manifest.write_text(vdf.dumps("AppState", state))
|
||||
_stub_download(monkeypatch)
|
||||
_stub_steamcmd(monkeypatch, returncode=8)
|
||||
monkeypatch.setattr(dlc, "download_owned", lambda *a: _dlc_plan("100389"))
|
||||
|
||||
assert cli.main(ARGV) == 0
|
||||
@ -197,7 +176,7 @@ def test_a_stub_with_own_app_dlc_finishes_with_the_proven_build(steam, monkeypat
|
||||
_stub_download(monkeypatch)
|
||||
monkeypatch.setattr(dlc, "download_owned", lambda *a: _dlc_plan("100389"))
|
||||
|
||||
assert cli.main([*ARGV, "--skip-steamcmd"]) == 0
|
||||
assert cli.main(ARGV) == 0
|
||||
assert recognize.read_app_state(manifest)["buildid"] == "23914554"
|
||||
|
||||
|
||||
|
||||
@ -193,6 +193,9 @@ def test_a_stale_depot_manifest_is_refused(tmp_path, monkeypatch):
|
||||
expected_manifests={"1086941": "8253106406752983615"},
|
||||
)
|
||||
assert any("1086941" in p and "stale" in p for p in problems)
|
||||
# The cache is the side that lags: depotdownloader always fetches the
|
||||
# current build, so the fix is to let Steam refresh its cache.
|
||||
assert any("start Steam once, let it sync, rerun" in p for p in problems)
|
||||
|
||||
|
||||
def test_a_proven_build_still_needs_a_buildid_to_record(tmp_path, monkeypatch):
|
||||
|
||||
@ -15,7 +15,7 @@ def test_finish_manifest_manually_requires_existing_manifest(tmp_path):
|
||||
library = SteamLibrary(tmp_path / "Steam")
|
||||
install_dir = tmp_path / "Steam" / "steamapps" / "common" / "Game"
|
||||
install_dir.mkdir(parents=True)
|
||||
with pytest.raises(RecognizeError, match="run steamcmd"):
|
||||
with pytest.raises(RecognizeError, match="no seed to write"):
|
||||
finish_manifest_manually(library, 1771300, install_dir, now_unix=1000)
|
||||
|
||||
|
||||
|
||||
@ -1,47 +0,0 @@
|
||||
import logging
|
||||
import signal
|
||||
|
||||
from steam_installer.steamcmd_validate import run_steamcmd_validate
|
||||
|
||||
|
||||
def test_run_steamcmd_validate_argv_and_result(monkeypatch):
|
||||
seen = {}
|
||||
|
||||
def fake_run_logged(cmd, logger, timeout=None):
|
||||
seen["cmd"] = cmd
|
||||
seen["timeout"] = timeout
|
||||
return 0, "all good"
|
||||
|
||||
monkeypatch.setattr(
|
||||
"steam_installer.steamcmd_validate.runlog.run_logged", fake_run_logged
|
||||
)
|
||||
|
||||
result = run_steamcmd_validate(1771300, "krzys2000", logging.getLogger("t"))
|
||||
|
||||
cmd = seen["cmd"]
|
||||
# The platform flag must precede +login, and no force_install_dir may appear:
|
||||
# it sets a LIBRARY ROOT and pointing it at the game dir caused the original
|
||||
# "Invalid platform" failure.
|
||||
assert cmd.index("+@sSteamCmdForcePlatformType") < cmd.index("+login")
|
||||
assert "force_install_dir" not in " ".join(cmd)
|
||||
assert cmd[cmd.index("+app_update") + 1 : cmd.index("+app_update") + 3] == [
|
||||
"1771300",
|
||||
"validate",
|
||||
]
|
||||
assert result.completed and result.returncode == 0
|
||||
assert result.output == "all good"
|
||||
assert not result.crashed_on_install_script
|
||||
|
||||
|
||||
def test_run_steamcmd_validate_detects_the_install_script_segfault(monkeypatch):
|
||||
"""The known steamcmd bug kills the process with SIGSEGV; checking the
|
||||
signal beats grepping captured text for a message string."""
|
||||
monkeypatch.setattr(
|
||||
"steam_installer.steamcmd_validate.runlog.run_logged",
|
||||
lambda cmd, logger, timeout=None: (-signal.SIGSEGV, ""),
|
||||
)
|
||||
|
||||
result = run_steamcmd_validate(1, "u", logging.getLogger("t"))
|
||||
|
||||
assert result.crashed_on_install_script
|
||||
assert not result.completed
|
||||
Loading…
Reference in New Issue
Block a user