feat(steam): adopt game folders found in added libraries (#2317)

* feat: adopt game folders in added Steam libraries

* Share the appmanifest builder between steam-library and steam-games

* steam: load the shared compatibility module once per run

* Guard unreadable game folders during library adoption

---------

Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
MaxsTechReviewandKurt Himebauch authored and GitHub committed 2026-11-17 11:25:04 -05:00
1 parent c31f19a9f0
commit b88b5f6b1d
6 files changed
+262 -4

No files matched your search

@@ -1,9 +1,11 @@
#!/usr/bin/env python3
import ctypes as C
import functools
import json
import os
from pathlib import Path
import re
import runpy
import shlex
import subprocess
import sys
@@ -445,18 +447,22 @@ def park(steamapps):
print(f"droiddeck-steam-games: {appid} is not here this session; its install is set aside until it is")
@functools.lru_cache(maxsize=1)
def compatibility():
return runpy.run_path(os.path.join(os.path.dirname(os.path.abspath(__file__)), "steam-compatibility"))
def seed_install(steam_root, game, appid):
steamapps = Path(steam_root) / "steamapps"
common = steamapps / "common"
name = f"DroidDeck-{appid}"
target = common / name
source = Path(game["folder"])
title = game["name"].replace("\\", "\\\\").replace('"', '\\"').replace("\n", " ").replace("\r", " ")
minimal = ('"AppState"\n{\n' + f' "appid" "{appid}"\n "Universe" "1"\n "name" "{title}"\n'
+ f' "StateFlags" "4"\n "installdir" "{name}"\n' + '}\n')
manifest_text = compatibility()["manifest_text"]
minimal = manifest_text(appid, game["name"], name)
legacy = minimal.replace(' "Universe" "1"\n', '')
# Steam checks every file of an import with no build before updating it, from scratch each time it is interrupted.
bare = minimal[:-2] + ' "AutoUpdateBehavior" "1"\n}\n'
bare = manifest_text(appid, game["name"], name, True)
build = installed_build(source, appid, name)
if build is None:
content = bare
@@ -2,8 +2,10 @@
import json
import os
import re
import runpy
import sys
BIN_DIR = os.path.dirname(os.path.abspath(__file__))
LIBRARIES = (("/mnt/droiddeck-sd", os.environ.get("BL_LIBRARY_LABEL", "SD Card")),)
MAIN_LABEL = "Internal Storage"
@@ -50,6 +52,77 @@ def register(steam_root, library, label):
return len(apps)
def installdirs(libraries):
found = set()
for library in libraries:
steamapps = os.path.join(library, "steamapps")
try:
names = os.listdir(steamapps)
except OSError:
continue
for name in names:
if not re.fullmatch(r"appmanifest_\d+\.acf", name):
continue
try:
with open(os.path.join(steamapps, name), encoding="utf-8", errors="replace") as f:
m = re.search(r'"installdir"\s+"([^"]*)"', f.read(), re.I)
except OSError:
continue
if m:
found.add(m.group(1))
return found
def populated(path):
try:
with os.scandir(path) as entries:
return next(entries, None) is not None
except OSError:
return False
def adoptable(library, known):
common = os.path.join(library, "steamapps", "common")
try:
names = sorted(os.listdir(common))
except OSError:
return []
return [n for n in names if n not in known and not n.startswith(".")
and os.path.isdir(os.path.join(common, n)) and populated(os.path.join(common, n))]
def adopt(steam_root, library):
compat = runpy.run_path(os.path.join(BIN_DIR, "steam-compatibility"))
known = compat["libraries"](steam_root)
folders = adoptable(library, installdirs(known + [library]))
if not folders:
return 0
entries = compat["appinfo_entries"](os.path.join(steam_root, "appcache", "appinfo.vdf"))
by_dir = {}
for app, info in entries.items():
if app not in compat["NOT_GAMES"] and info["type"] in compat["PLAYABLE_TYPES"] and info["installdir"]:
by_dir.setdefault(info["installdir"], []).append((app, info))
steamapps = os.path.join(library, "steamapps")
adopted = 0
for folder in folders:
matches = by_dir.get(folder, [])
games = [m for m in matches if m[1]["type"] == "game"] or matches
if len(games) != 1:
continue
app, info = games[0]
if any(os.path.isfile(os.path.join(root, "steamapps", "appmanifest_%s.acf" % app))
for root in known + [library]):
continue
try:
with open(os.path.join(steamapps, "appmanifest_%s.acf" % app), "x", encoding="utf-8") as f:
f.write(compat["manifest_text"](app, info["name"] or folder, folder, True))
except OSError:
continue
adopted += 1
print("droiddeck-steam-library: adopted %s as app %s in %s" % (folder, app, library))
return adopted
RETIRED = ("/mnt/bannerlator",)
RENAMED = (("/mnt/bannerlator-sd", "/mnt/droiddeck-sd"),)
@@ -176,6 +249,7 @@ def main():
managed = libraries is not None
for library, label in libraries if managed else LIBRARIES:
if os.path.isdir(os.path.join(library, "steamapps")):
adopt(steam_root, library)
register(steam_root, library, label)
relabel(steam_root, library, label)
else:
@@ -893,7 +893,10 @@ def appinfo_entries(path, wanted=None):
deck = common.get("steam_deck_compatibility")
configuration = deck.get("configuration") if isinstance(deck, dict) else None
recommended = configuration.get("recommended_runtime", "") if isinstance(configuration, dict) else ""
config = info.get("config") if isinstance(info.get("config"), dict) else {}
entry = {
"name": str(common.get("name", "")),
"installdir": str(config.get("installdir", "")),
"oslist": {part.strip().lower() for part in str(common.get("oslist", "")).split(",") if part.strip()},
"osarch": str(common.get("osarch", "")).lower(),
"type": str(common.get("type", "")).lower(),
@@ -960,6 +963,13 @@ def native_targets(steam_root, apps):
return targets
def manifest_text(appid, name, installdir, bare=False):
title = name.replace("\\", "\\\\").replace('"', '\\"').replace("\n", " ").replace("\r", " ")
text = ('"AppState"\n{\n' + f' "appid" "{appid}"\n "Universe" "1"\n "name" "{title}"\n'
+ f' "StateFlags" "4"\n "installdir" "{installdir}"\n' + '}\n')
return text[:-2] + ' "AutoUpdateBehavior" "1"\n}\n' if bare else text
def app_manifest(steam_root, app):
for library in libraries(steam_root):
try: