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:
+147
View File
@@ -0,0 +1,147 @@
import importlib.machinery
import importlib.util
import runpy
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin"
APPINFO = runpy.run_path(str(Path(__file__).with_name("test_droiddeck_fex.py")))["AppInfoTest"].appinfo
def load(name):
loader = importlib.machinery.SourceFileLoader(name.replace("-", "_"), str(BIN / name))
spec = importlib.util.spec_from_loader(loader.name, loader)
module = importlib.util.module_from_spec(spec)
loader.exec_module(module)
return module
library = load("droiddeck-steam-library")
def record(kind, installdir, name):
return {"common": {"type": kind, "name": name}, "config": {"installdir": installdir}}
class AdoptTest(unittest.TestCase):
def setUp(self):
temp = tempfile.TemporaryDirectory()
self.addCleanup(temp.cleanup)
root = Path(temp.name)
self.steam = root / "Steam"
self.card = root / "card"
self.common = self.card / "steamapps/common"
self.common.mkdir(parents=True)
(self.steam / "appcache").mkdir(parents=True)
(self.steam / "steamapps").mkdir()
self.write_info({
10: record("Game", "Alpha", 'Alpha "Deluxe"'),
20: record("Game", "Beta", "Beta"),
30: record("Tool", "Proton Thing", "Proton Thing"),
40: record("Game", "Shared", "Shared Game"),
41: record("Demo", "Shared", "Shared Demo"),
50: record("Game", "Twin", "Twin One"),
51: record("Game", "Twin", "Twin Two"),
})
for patcher in (patch.object(library, "BIN_DIR", str(BIN)),
patch("builtins.print")):
patcher.start()
self.addCleanup(patcher.stop)
def write_info(self, apps):
(self.steam / "appcache/appinfo.vdf").write_bytes(APPINFO(apps))
def folder(self, name, files=("data.bin",)):
path = self.common / name
path.mkdir(parents=True, exist_ok=True)
for f in files:
(path / f).write_text("x")
def manifest(self, app, root=None):
return (root or self.card) / ("steamapps/appmanifest_%s.acf" % app)
def adopt(self):
return library.adopt(str(self.steam), str(self.card))
def test_matched_folder_gets_a_validating_manifest(self):
self.folder("Alpha")
self.assertEqual(self.adopt(), 1)
text = self.manifest(10).read_text()
for line in ('"appid" "10"', '"Universe" "1"', '"StateFlags" "4"', '"installdir" "Alpha"',
'"name" "Alpha \\"Deluxe\\""', '"AutoUpdateBehavior" "1"'):
self.assertIn(line, text)
self.assertNotIn("buildid", text)
self.assertNotIn("InstalledDepots", text)
def test_existing_manifest_is_untouched(self):
self.folder("Alpha")
self.manifest(10).write_text("mine")
self.assertEqual(self.adopt(), 0)
self.assertEqual(self.manifest(10).read_text(), "mine")
def test_unmatched_empty_and_hidden_folders_are_skipped(self):
self.folder("Unknown")
self.folder(".hidden")
(self.common / "Beta").mkdir()
(self.common / "file.txt").write_text("x")
self.assertEqual(self.adopt(), 0)
self.assertEqual(list((self.card / "steamapps").glob("appmanifest_*")), [])
def test_unreadable_game_folder_is_skipped_without_aborting(self):
self.folder("Unreadable")
real = library.os.scandir
def scandir(path):
if str(path).endswith("Unreadable"):
raise PermissionError(path)
return real(path)
with patch.object(library.os, "scandir", scandir):
self.assertEqual(library.adoptable(str(self.card), set()), [])
def test_tool_type_app_is_skipped(self):
self.folder("Proton Thing")
self.assertEqual(self.adopt(), 0)
self.assertFalse(self.manifest(30).exists())
def test_game_wins_over_demo_and_ambiguous_games_are_skipped(self):
self.folder("Shared")
self.folder("Twin")
self.assertEqual(self.adopt(), 1)
self.assertTrue(self.manifest(40).exists())
self.assertFalse(self.manifest(41).exists())
self.assertFalse(self.manifest(50).exists() or self.manifest(51).exists())
def test_app_installed_in_another_library_is_left_alone(self):
self.folder("Alpha")
other = self.steam / "steamapps/appmanifest_10.acf"
other.write_text('"AppState"\n{\n\t"appid"\t"10"\n\t"installdir"\t"Elsewhere"\n}\n')
self.assertEqual(self.adopt(), 0)
self.assertFalse(self.manifest(10).exists())
self.assertEqual(len(list(self.steam.glob("steamapps/appmanifest_*"))), 1)
def test_adoption_is_idempotent_and_only_touches_the_given_library(self):
self.folder("Alpha")
self.folder("Beta")
self.assertEqual(self.adopt(), 2)
first = {n: self.manifest(n).read_text() for n in (10, 20)}
self.assertEqual(self.adopt(), 0)
self.assertEqual({n: self.manifest(n).read_text() for n in (10, 20)}, first)
self.assertEqual(list(self.steam.glob("steamapps/appmanifest_*")), [])
def test_main_adopts_every_registered_library(self):
self.folder("Beta")
listing = self.steam.parent / "libraries.json"
listing.write_text('[{"path": "%s", "label": "SD Card"}]' % self.card)
with patch.dict("os.environ", {"BL_STEAM_LIBRARIES": str(listing)}), \
patch("sys.argv", ["droiddeck-steam-library", str(self.steam)]):
library.main()
self.assertTrue(self.manifest(20).exists())
self.assertIn(str(self.card), (self.steam / "steamapps/libraryfolders.vdf").read_text())
self.assertIn('"20"', (self.steam / "steamapps/libraryfolders.vdf").read_text())
if __name__ == "__main__":
unittest.main()
+10
View File
@@ -5,6 +5,7 @@ import os
from pathlib import Path
import shutil
import subprocess
import runpy
import tempfile
import unittest
import zlib
@@ -26,6 +27,15 @@ shortcuts = load('droiddeck-steam-shortcuts')
library = load('droiddeck-steam-library')
class ManifestTextTest(unittest.TestCase):
def test_minimal_and_bare_text_is_exact(self):
build = runpy.run_path(str(BIN / 'steam-compatibility'))['manifest_text']
minimal = ('"AppState"\n{\n "appid" "7"\n "Universe" "1"\n "name" "A \\"B\\" C D"\n'
' "StateFlags" "4"\n "installdir" "DroidDeck-7"\n}\n')
self.assertEqual(minimal, build(7, 'A "B"\nC\rD', 'DroidDeck-7'))
self.assertEqual(minimal[:-2] + ' "AutoUpdateBehavior" "1"\n}\n', build(7, 'A "B"\nC\rD', 'DroidDeck-7', True))
class ImportsTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()