steam-backlog-enforcer/steam_backlog_enforcer/tests/_appinfo_home.py
Krzysztof kuhy Rudnicki be4426f3d0
Some checks are pending
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Tool need: native games need no Proton tools
C12: DUSK (519860) is a native Linux game with no CompatToolMapping entry,
so the old rule kept Proton Experimental, SLR 4.0 and the redistributables
for good. A game now also counts as covered when Valve runs it natively:
no mapping of kuhy's (or an empty one), linux in appinfo's common/oslist,
and no Valve tool pick (its own config/app_mappings or app 891390's). Any
doubt still keeps them: an unreadable or old-format appinfo.vdf, a game
missing from it, no 891390 pick list, or a native game forced onto another
tool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 16:52:39 +01:00

74 lines
2.6 KiB
Python

"""Fixture ``appcache/appinfo.vdf`` files in Steam's binary v29 layout.
Mirrors what :mod:`steam_backlog_enforcer._appinfo_cache` reads: a 16-byte
head (magic, universe, string-table offset), one record per app (appid, size,
60-byte header, binary key-values keyed by string-table index), appid 0, then
the string table. A Python ``int`` becomes a 32-bit int value (type 2).
"""
from __future__ import annotations
import struct
from typing import TYPE_CHECKING
from steam_backlog_enforcer._appinfo_cache import MAGIC, STEAM_PLAY_APP
if TYPE_CHECKING:
from pathlib import Path
type Node = str | int | dict[str, Node]
def _encode(node: dict[str, Node], strings: list[str]) -> bytes:
out = b""
for key, value in node.items():
if key not in strings:
strings.append(key)
index = struct.pack("<I", strings.index(key))
if isinstance(value, dict):
out += b"\x00" + index + _encode(value, strings)
elif isinstance(value, int):
out += b"\x02" + index + struct.pack("<i", value)
else:
out += b"\x01" + index + value.encode() + b"\0"
return out + b"\x08"
def appinfo_bytes(apps: dict[int, dict[str, Node]], *, magic: int = MAGIC) -> bytes:
"""The binary cache holding *apps* (appid -> its ``appinfo`` block)."""
strings: list[str] = []
body = b""
for app_id, block in apps.items():
data = b"\0" * 60 + _encode({"appinfo": block}, strings)
body += struct.pack("<II", app_id, len(data)) + data
body += struct.pack("<I", 0)
table = struct.pack("<I", len(strings)) + b"".join(
s.encode() + b"\0" for s in strings
)
return struct.pack("<IIq", magic, 1, 16 + len(body)) + body + table
def game(oslist: str, **config: Node) -> dict[str, Node]:
"""A game's appinfo block with *oslist* and an optional ``config``."""
return {"common": {"name": "G", "oslist": oslist}, "config": dict(config)}
def steam_play(mapped: tuple[int, ...] = ()) -> dict[str, Node]:
"""App 891390's block; *mapped* games get a Valve tool pick."""
mappings: dict[str, Node] = {
str(a): {"appid": str(a), "tool": "proton-experimental"} for a in mapped
}
return {"extended": {"app_mappings": mappings}}
def write_appinfo(
root: Path, apps: dict[int, dict[str, Node]], *, steam_play_app: bool = True
) -> Path:
"""Write ``<root>/appcache/appinfo.vdf``; 891390 is added unless told not."""
if steam_play_app:
apps = {STEAM_PLAY_APP: steam_play()} | apps
path = root / "appcache" / "appinfo.vdf"
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(appinfo_bytes(apps))
return path