"""Passive reader for the local Steam installation. Reads only files the Steam client has already written: appcache/librarycache/* -- every app in the client's library appcache/appinfo.vdf -- names, genres, tags, scores (binary VDF v29) appcache/../config/libraryfolders.vdf steamapps/appmanifest_*.acf -- installed apps userdata//config/localconfig.vdf -- playtime No Steam session, credentials or network access are required. """ import os import re import struct GENRES = { 1: "Action", 2: "Strategy", 3: "RPG", 4: "Casual", 5: "Sports", 6: "Racing", 7: "Simulation", 8: "Accounting", 9: "Racing", 18: "Sports", 23: "Indie", 25: "Adventure", 28: "Simulation", 29: "Massively Multiplayer", 37: "Free To Play", 70: "Early Access", 71: "Animation & Modeling", 72: "Design & Illustration", 73: "Violent", 74: "Gore", } SKIP_GENRE_IDS = {30, 73, 74} DEFAULT_ROOTS = ( r"C:\Program Files (x86)\Steam", r"C:\Program Files\Steam", ) COVER_ORDER = ( "library_600x900.jpg", "library_capsule.jpg", "header.jpg", "library_header.jpg", "library_hero.jpg", "logo.png", ) def find_cover(appid, lib_cache): """Find a cover in both Steam librarycache layouts. Newer clients store art in hashed subdirectories: //library_capsule.jpg Older clients use flat names: /library_600x900.jpg """ if not lib_cache: return None app_dir = os.path.join(lib_cache, str(appid)) if not os.path.isdir(app_dir): return None try: dirs = [app_dir] for sub in os.listdir(app_dir): sub_dir = os.path.join(app_dir, sub) if os.path.isdir(sub_dir): dirs.append(sub_dir) # Prefer the best cover type across every layout, not the first # subdirectory that happens to contain any image. for name in COVER_ORDER: for directory in dirs: path = os.path.join(directory, name) if os.path.isfile(path): return path except OSError: return None return None def find_steam_root(): try: import winreg with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Valve\Steam") as k: path = winreg.QueryValueEx(k, "SteamPath")[0] if path and os.path.isdir(path): return os.path.normpath(path) except Exception: pass for path in DEFAULT_ROOTS: if os.path.isdir(path): return path raise SystemExit("Steam not found; pass --steam-root explicitly") def _read_text(path): with open(path, encoding="utf-8-sig", errors="replace") as f: return f.read() def read_users(root): path = os.path.join(root, "config", "loginusers.vdf") if not os.path.isfile(path): return [] text = _read_text(path) users = [] for m in re.finditer(r'"(?P\d{17})"\s*\{(?P[^}]*)\}', text): body = m.group("body") users.append({ "steamid": m.group("id"), "account": (re.search(r'"AccountName"\s*"([^"]*)"', body) or [None, None])[1], "persona": (re.search(r'"PersonaName"\s*"([^"]*)"', body) or [None, None])[1], "most_recent": (re.search(r'"MostRecent"\s*"(\d)"', body) or [None, "0"])[1] == "1", }) return users def account_id(steamid64): return int(steamid64) - 76561197960265728 def library_roots(root): roots = [root] seen = {os.path.normcase(root)} for vdf in (os.path.join(root, "config", "libraryfolders.vdf"), os.path.join(root, "steamapps", "libraryfolders.vdf")): if not os.path.isfile(vdf): continue for m in re.finditer(r'"path"\s*"((?:[^"\\]|\\.)*)"', _read_text(vdf)): path = m.group(1).replace("\\\\", "\\") key = os.path.normcase(path) if os.path.isdir(path) and key not in seen: seen.add(key) roots.append(path) return roots def installed_apps(roots): out = {} for root in roots: apps_dir = os.path.join(root, "steamapps") if not os.path.isdir(apps_dir): continue for fn in os.listdir(apps_dir): m = re.match(r"appmanifest_(\d+)\.acf$", fn) if not m: continue text = _read_text(os.path.join(apps_dir, fn)) appid = int(m.group(1)) out[appid] = { "name": (re.search(r'"name"\s*"((?:[^"\\]|\\.)*)"', text) or [None, None])[1], "size_bytes": int((re.search(r'"SizeOnDisk"\s*"(\d+)"', text) or [None, 0])[1]), "last_played": (re.search(r'"LastPlayed"\s*"(\d+)"', text) or [None, "0"])[1], "library_path": root, } return out def library_appids(root): cache = os.path.join(root, "appcache", "librarycache") if not os.path.isdir(cache): return [] return sorted(int(n) for n in os.listdir(cache) if n.isdigit()) def _appinfo_strings(data, offset): count = struct.unpack_from(" len(data): break appid = struct.unpack_from("