294 lines
9.1 KiB
Python
294 lines
9.1 KiB
Python
"""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/<id>/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:
|
|
<appid>/<hash>/library_capsule.jpg
|
|
Older clients use flat names:
|
|
<appid>/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<id>\d{17})"\s*\{(?P<body>[^}]*)\}', 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("<I", data, offset)[0]
|
|
pos = offset + 4
|
|
strings = []
|
|
for _ in range(count):
|
|
end = data.index(b"\x00", pos)
|
|
strings.append(data[pos:end].decode("utf-8", "replace"))
|
|
pos = end + 1
|
|
return strings
|
|
|
|
|
|
def _appinfo_kv(data, off, strings):
|
|
out = {}
|
|
while True:
|
|
kind = data[off]
|
|
off += 1
|
|
if kind == 0x08:
|
|
return out, off
|
|
idx = struct.unpack_from("<I", data, off)[0]
|
|
off += 4
|
|
key = strings[idx] if idx < len(strings) else None
|
|
if kind == 0x00:
|
|
value, off = _appinfo_kv(data, off, strings)
|
|
elif kind == 0x01:
|
|
end = data.index(b"\x00", off)
|
|
value = data[off:end].decode("utf-8", "replace")
|
|
off = end + 1
|
|
elif kind == 0x02:
|
|
value = struct.unpack_from("<i", data, off)[0]; off += 4
|
|
elif kind == 0x03:
|
|
value = struct.unpack_from("<f", data, off)[0]; off += 4
|
|
elif kind == 0x04:
|
|
value = struct.unpack_from("<I", data, off)[0]; off += 4
|
|
elif kind == 0x05:
|
|
value = struct.unpack_from("<I", data, off)[0]; off += 4
|
|
elif kind == 0x06:
|
|
value = struct.unpack_from("<I", data, off)[0]; off += 4
|
|
elif kind == 0x07:
|
|
value = struct.unpack_from("<Q", data, off)[0]; off += 8
|
|
elif kind == 0x0A:
|
|
value = struct.unpack_from("<q", data, off)[0]; off += 8
|
|
else:
|
|
raise ValueError(f"unknown binary VDF type {kind:#x}")
|
|
out[key] = value
|
|
|
|
|
|
def read_appinfo(root):
|
|
"""Return {appid: {name, type, genres, tags, metacritic, review_pct, release}}."""
|
|
path = os.path.join(root, "appcache", "appinfo.vdf")
|
|
if not os.path.isfile(path):
|
|
return {}
|
|
with open(path, "rb") as f:
|
|
data = f.read()
|
|
magic = struct.unpack_from("<I", data, 0)[0]
|
|
if magic != 0x07564429:
|
|
raise SystemExit(f"Unsupported appinfo.vdf version: {magic:#x} (only v29 is implemented)")
|
|
|
|
table_offset = struct.unpack_from("<Q", data, 8)[0]
|
|
strings = _appinfo_strings(data, table_offset)
|
|
|
|
apps = {}
|
|
off = 16
|
|
while True:
|
|
if off + 8 > len(data):
|
|
break
|
|
appid = struct.unpack_from("<I", data, off)[0]
|
|
if appid == 0:
|
|
break
|
|
size = struct.unpack_from("<I", data, off + 4)[0]
|
|
kv_offset = off + 68
|
|
try:
|
|
kv, _ = _appinfo_kv(data, kv_offset, strings)
|
|
info = kv.get("appinfo", {})
|
|
common = info.get("common", {})
|
|
apps[appid] = {
|
|
"name": common.get("name") or info.get("name"),
|
|
"type": common.get("type"),
|
|
"genres": common.get("genres") or {},
|
|
"tags": common.get("store_tags") or {},
|
|
"metacritic": common.get("metacritic_score"),
|
|
"review_pct": common.get("review_percentage"),
|
|
"release": common.get("steam_release_date"),
|
|
}
|
|
except Exception:
|
|
pass
|
|
off += 8 + size
|
|
return apps
|
|
|
|
|
|
def read_playtime(root, steamid):
|
|
"""Return {appid: {minutes, last_played}} from localconfig.vdf."""
|
|
path = os.path.join(root, "userdata", str(account_id(steamid)), "config", "localconfig.vdf")
|
|
if not os.path.isfile(path):
|
|
return {}
|
|
text = _read_text(path)
|
|
start = text.find('"apps"')
|
|
if start < 0:
|
|
return {}
|
|
text = text[start:]
|
|
|
|
out = {}
|
|
for m in re.finditer(r'"(\d+)"\s*\n\s*\{', text):
|
|
appid = int(m.group(1))
|
|
depth = 0
|
|
end = m.end() - 1
|
|
while end < len(text):
|
|
if text[end] == "{":
|
|
depth += 1
|
|
elif text[end] == "}":
|
|
depth -= 1
|
|
if depth == 0:
|
|
break
|
|
end += 1
|
|
body = text[m.end():end]
|
|
last = re.search(r'"LastPlayed"\s*"(\d+)"', body)
|
|
play = re.search(r'"Playtime"\s*"(\d+)"', body)
|
|
if last or play:
|
|
out[appid] = {
|
|
"minutes": int(play.group(1)) if play else 0,
|
|
"last_played": int(last.group(1)) if last else 0,
|
|
}
|
|
return out
|