mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 07:46:01 +00:00
* fix: a Steam target whose file is gone goes back to the app's exe A shortcut Target edited in Steam is kept across the client's restart on purpose, but it was kept even after the file it names disappeared. When the app's own exe already equalled the recorded proposal, nothing replaced the dead target: Steam started `System/launcher.exe`, Wine answered `err:steam:run_process Failed to create process ...: 2`, and the game never ran (#694, after #1644 and #2022 removed the two real causes). The merge now treats a target that is not a file as no edit at all and writes the listing's exe and StartIn. The check runs in the guest, where the shortcut's paths live. Fixes #694 * fix: keep a Steam target whose card is not mounted - The app recreates /mnt/droiddeck-sd and /mnt/bannerlator-sd before binding the card, so the mountpoint outlives a removed card. A target directly under one had an existing parent, so the missing-file repair overwrote the edited Exe and Start In and kept them overwritten. - The repair now needs the target's storage to be there: a card mountpoint that lists nothing is a card that is not in the device, and the edit is kept for when it returns. Extra Steam libraries under /mnt/droiddeck-lib/<id> get the same check. - test_a_steam_target_under_a_bare_card_mountpoint_is_kept fails without the check and asserts both fields; test_a_steam_target_under_a_mounted_card_goes_back_to_the_apps keeps the repair. * fix: repair a Steam target removed with its folder - A missing target is gone when its storage is mounted and either its folder is still there or it lives in a tree the app always has (internal store installs, the internal Steam library). An update that removes the whole game folder now repairs the shortcut. - Store volumes and added game folders are removable mounts too: /mnt/droiddeck-stores/<id> and /root/Games/<name> are checked for content before a target under them is treated as gone, so an unplugged volume keeps the edited Exe and Start In. - Tests: internal folder removed repairs both fields; a bare store mountpoint and a card mountpoint keep both; a mounted card still repairs. 84 tests pass. * fix: classify a Steam target by its resolved path - A target such as /root/Games/Stores/../../../mnt/droiddeck-sd/Other.exe matched the internal prefix and was repaired while the card was out. internal_target and removable_mount now normalize the path before choosing the storage class, so the decision follows the file the target resolves to; the kept text is unchanged. - test_a_steam_target_is_classified_by_its_resolved_path fails before the normalization (the edit is replaced) and keeps both fields after it. 85 tests pass.
426 lines
16 KiB
Python
Executable File
426 lines
16 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Import added games while Steam is closed, preserving user-created shortcuts.
|
|
|
|
Usage: droiddeck-steam-shortcuts <steam_root> <added-games.json>
|
|
"""
|
|
import importlib.machinery
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
import re
|
|
import shutil
|
|
import struct
|
|
import sys
|
|
|
|
TAG = "droiddeck-app"
|
|
|
|
|
|
def account_dir(steam_root):
|
|
users = os.path.join(steam_root, "config", "loginusers.vdf")
|
|
recent, best = None, None
|
|
try:
|
|
with open(users, encoding="utf-8", errors="replace") as f:
|
|
text = f.read()
|
|
for block in re.finditer(r'"(\d{5,})"\s*\{([^}]*)\}', text):
|
|
stamp = re.search(r'"timestamp"\s*"(\d+)"', block.group(2))
|
|
rank = (bool(re.search(r'"MostRecent"\s*"1"', block.group(2))),
|
|
bool(re.search(r'"AutoLogin"\s*"1"', block.group(2))),
|
|
int(stamp.group(1)) if stamp else 0)
|
|
if any(rank) and (best is None or rank >= best):
|
|
recent, best = int(block.group(1)), rank
|
|
except OSError:
|
|
pass
|
|
userdata = os.path.join(steam_root, "userdata")
|
|
if recent is not None:
|
|
acct = recent - 76561197960265728
|
|
return os.path.join(userdata, str(acct))
|
|
try:
|
|
dirs = [d for d in os.listdir(userdata) if d.isdigit() and d != "0"]
|
|
except OSError:
|
|
dirs = []
|
|
return os.path.join(userdata, dirs[0]) if len(dirs) == 1 else None
|
|
|
|
|
|
|
|
def parse(data):
|
|
pos = [0]
|
|
|
|
def cstr():
|
|
end = data.index(b"\x00", pos[0])
|
|
s = data[pos[0]:end].decode("utf-8", "replace")
|
|
pos[0] = end + 1
|
|
return s
|
|
|
|
def node():
|
|
out = {}
|
|
while True:
|
|
t = data[pos[0]]
|
|
pos[0] += 1
|
|
if t == 0x08:
|
|
return out
|
|
key = cstr()
|
|
if t == 0x00:
|
|
out[key] = node()
|
|
elif t == 0x01:
|
|
out[key] = cstr()
|
|
elif t == 0x02:
|
|
out[key] = struct.unpack_from("<i", data, pos[0])[0]
|
|
pos[0] += 4
|
|
else:
|
|
raise ValueError("unknown vdf type 0x%02x" % t)
|
|
|
|
t = data[pos[0]]
|
|
pos[0] += 1
|
|
key = cstr()
|
|
return {key: node()}
|
|
|
|
|
|
def write(obj):
|
|
out = bytearray()
|
|
|
|
def emit(key, value):
|
|
if isinstance(value, dict):
|
|
out.append(0x00)
|
|
out.extend(key.encode() + b"\x00")
|
|
for k, v in value.items():
|
|
emit(k, v)
|
|
out.append(0x08)
|
|
elif isinstance(value, int):
|
|
out.append(0x02)
|
|
out.extend(key.encode() + b"\x00" + struct.pack("<i", value))
|
|
else:
|
|
out.append(0x01)
|
|
out.extend(key.encode() + b"\x00" + str(value).encode() + b"\x00")
|
|
|
|
for k, v in obj.items():
|
|
emit(k, v)
|
|
out.append(0x08)
|
|
return bytes(out)
|
|
|
|
|
|
def shortcut(game, launch_options=None):
|
|
appid = int(game["appid"]) & 0xFFFFFFFF
|
|
return {
|
|
"appid": struct.unpack("<i", struct.pack("<I", appid))[0],
|
|
"AppName": game["name"],
|
|
"Exe": '"%s"' % game["exe"],
|
|
"StartDir": '"%s"' % game["dir"],
|
|
"icon": (game.get("art") or {}).get("icon", "") or "",
|
|
"ShortcutPath": "",
|
|
"LaunchOptions": launch_options if launch_options is not None else (game.get("launch") or ""),
|
|
"IsHidden": 0,
|
|
"AllowDesktopConfig": 1,
|
|
"AllowOverlay": 1,
|
|
"OpenVR": 0,
|
|
"Devkit": 0,
|
|
"DevkitGameID": "",
|
|
"DevkitOverrideAppID": 0,
|
|
"LastPlayTime": 0,
|
|
"FlatpakAppID": "",
|
|
"tags": {"0": TAG},
|
|
}
|
|
|
|
|
|
RECORD = ".droiddeck-shortcuts.json"
|
|
AWAY_LIMIT = 256
|
|
FIELDS = ("AppName", "Exe", "StartDir", "icon", "LaunchOptions")
|
|
FOLLOWED = ("StartDir", "LaunchOptions")
|
|
# Guest mountpoints whose contents live on storage the user can unplug. The app creates
|
|
# the mountpoint before binding it, so it outlives a removed card; libraries, store
|
|
# volumes and added game folders get one mountpoint each under these families.
|
|
REMOVABLE_ROOTS = ("/mnt/droiddeck-sd", "/mnt/bannerlator-sd")
|
|
REMOVABLE_FAMILIES = ("/mnt/droiddeck-lib", "/mnt/droiddeck-stores", "/root/Games")
|
|
# Guest trees the app always has, whatever storage is in the device. A target that is
|
|
# missing from one of these is gone even when its whole folder was removed.
|
|
INTERNAL_ROOTS = ("/root/Games/Stores", "/root/.local/share/Steam")
|
|
|
|
|
|
def unquoted(value):
|
|
return value.strip().strip('"') if isinstance(value, str) else ""
|
|
|
|
|
|
def read_record(path):
|
|
try:
|
|
with open(path, encoding="utf-8") as f:
|
|
record = json.load(f)
|
|
except (OSError, ValueError):
|
|
record = {}
|
|
if not isinstance(record, dict):
|
|
record = {}
|
|
games, away = record.get("games"), record.get("away")
|
|
return {
|
|
"games": {k: v for k, v in games.items() if isinstance(v, dict)} if isinstance(games, dict) else {},
|
|
"away": {k: v for k, v in away.items() if isinstance(v, dict) and isinstance(v.get("entry"), dict)} if isinstance(away, dict) else {},
|
|
}
|
|
|
|
|
|
def under(path, root):
|
|
return path == root or path.startswith(root + "/")
|
|
|
|
|
|
def internal_target(target):
|
|
path = os.path.normpath(target)
|
|
return any(under(path, root) for root in INTERNAL_ROOTS)
|
|
|
|
|
|
def removable_mount(target):
|
|
"""The guest mountpoint holding target, when it is storage the user can unplug."""
|
|
path = os.path.normpath(target)
|
|
for root in REMOVABLE_ROOTS:
|
|
if under(path, root):
|
|
return root
|
|
for family in REMOVABLE_FAMILIES:
|
|
prefix = family + "/"
|
|
if path.startswith(prefix):
|
|
return prefix + path[len(prefix):].split("/", 1)[0]
|
|
return None
|
|
|
|
|
|
def storage_mounted(target):
|
|
"""Whether the storage holding target is there this session.
|
|
|
|
The app creates a guest mountpoint before binding storage to it, so a mountpoint that
|
|
lists nothing is one a removed card or volume left behind. A target under it may work
|
|
again when that storage returns, so its absence is not proof the file is gone.
|
|
"""
|
|
if internal_target(target):
|
|
return True
|
|
mount = removable_mount(target)
|
|
if mount is None:
|
|
return True
|
|
try:
|
|
return bool(os.listdir(mount))
|
|
except OSError:
|
|
return False
|
|
|
|
|
|
def merge(fresh, current, state, seen):
|
|
state = state if isinstance(state, dict) else {}
|
|
rev = state.get("rev") if isinstance(state.get("rev"), int) else 0
|
|
if current is None:
|
|
return fresh, rev
|
|
base = state.get("app") if isinstance(state.get("app"), dict) else {field: current.get(field) for field in FIELDS}
|
|
before = state.get("steam") if isinstance(state.get("steam"), dict) else {"Exe": current.get("Exe")}
|
|
if unquoted(current.get("Exe")) != unquoted(before.get("Exe")):
|
|
rev += 1
|
|
entry = dict(current)
|
|
entry["appid"] = fresh["appid"]
|
|
exe = unquoted(fresh["Exe"])
|
|
target = unquoted(current.get("Exe"))
|
|
# A target Steam still points at after the file is gone starts nothing, so it is not
|
|
# an edit to keep: the app's own exe replaces it (#694: a hand-typed Target outlived
|
|
# the game update that removed the file, and every launch died before the game ran).
|
|
# Only when its storage is mounted and it is missing from a tree the app always has or
|
|
# from a folder that is still there. An absent folder under a mountpoint can equally be
|
|
# unplugged storage, and that target may work again next session.
|
|
gone = (bool(target) and not os.path.isfile(target) and storage_mounted(target) and
|
|
(internal_target(target) or os.path.isdir(os.path.dirname(target))))
|
|
if exe != target and (gone or exe != unquoted(base.get("Exe")) or seen == rev):
|
|
entry["Exe"], entry["StartDir"] = fresh["Exe"], fresh["StartDir"]
|
|
for field in ("AppName", "icon"):
|
|
if current.get(field) == base.get(field):
|
|
entry[field] = fresh[field]
|
|
if isinstance(state.get("app"), dict):
|
|
for field in FOLLOWED:
|
|
was = base.get(field, "" if field == "LaunchOptions" else current.get(field))
|
|
if fresh[field] != was and current.get(field) == was:
|
|
entry[field] = fresh[field]
|
|
tags = dict(current["tags"]) if isinstance(current.get("tags"), dict) else {}
|
|
if TAG not in tags.values():
|
|
slot = 0
|
|
while str(slot) in tags:
|
|
slot += 1
|
|
tags[str(slot)] = TAG
|
|
entry["tags"] = tags
|
|
for key, value in fresh.items():
|
|
entry.setdefault(key, value)
|
|
return entry, rev
|
|
|
|
|
|
def reconcile(existing, wanted, seen, record):
|
|
games, away = record["games"], dict(record["away"])
|
|
kept, ours, placed = [], [], {}
|
|
claimed = {(unquoted(fresh["Exe"]), fresh["AppName"]) for fresh in wanted.values()}
|
|
for value in existing.values():
|
|
if not isinstance(value, dict):
|
|
continue
|
|
tags = value.get("tags")
|
|
if not isinstance(tags, dict) or TAG not in tags.values():
|
|
if (unquoted(value.get("Exe")), value.get("AppName")) not in claimed:
|
|
kept.append(value)
|
|
continue
|
|
appid = value.get("appid")
|
|
if not isinstance(appid, int) or appid in placed:
|
|
continue
|
|
key = str(appid & 0xFFFFFFFF)
|
|
fresh = wanted.get(appid)
|
|
if fresh is None:
|
|
away.pop(key, None)
|
|
away[key] = {"entry": value, "state": games.get(key)}
|
|
continue
|
|
entry, rev = merge(fresh, value, games.get(key), seen.get(appid, 0))
|
|
placed[appid] = (len(ours), rev)
|
|
ours.append(entry)
|
|
for appid, fresh in wanted.items():
|
|
if appid in placed:
|
|
continue
|
|
key = str(appid & 0xFFFFFFFF)
|
|
stashed = away.get(key)
|
|
if stashed:
|
|
entry, rev = merge(fresh, stashed["entry"], stashed.get("state"), seen.get(appid, 0))
|
|
else:
|
|
entry, rev = merge(fresh, None, games.get(key), seen.get(appid, 0))
|
|
placed[appid] = (len(ours), rev)
|
|
ours.append(entry)
|
|
for appid in placed:
|
|
away.pop(str(appid & 0xFFFFFFFF), None)
|
|
while len(away) > AWAY_LIMIT:
|
|
away.pop(next(iter(away)))
|
|
state = {}
|
|
for appid, (index, rev) in placed.items():
|
|
entry = ours[index]
|
|
state[str(appid & 0xFFFFFFFF)] = {
|
|
"app": {field: wanted[appid][field] for field in FIELDS},
|
|
"steam": {field: entry.get(field, "") for field in ("AppName", "Exe", "StartDir", "LaunchOptions")},
|
|
"rev": rev,
|
|
}
|
|
return kept, ours, {"games": state, "away": away}
|
|
|
|
|
|
def save_record(path, record):
|
|
tmp = path + ".droiddeck"
|
|
with open(tmp, "w", encoding="utf-8") as f:
|
|
json.dump(record, f)
|
|
os.replace(tmp, path)
|
|
|
|
|
|
ART_MANIFEST = ".droiddeck-art.json"
|
|
ART_SLOTS = (("p", "p"), ("header", ""), ("hero", "_hero"), ("logo", "_logo"))
|
|
|
|
|
|
def install_art(acct, games):
|
|
grid = os.path.join(acct, "config", "grid")
|
|
os.makedirs(grid, exist_ok=True)
|
|
manifest_path = os.path.join(grid, ART_MANIFEST)
|
|
try:
|
|
with open(manifest_path, encoding="utf-8") as f:
|
|
manifest = json.load(f)
|
|
except (OSError, ValueError):
|
|
manifest = {}
|
|
wanted = {}
|
|
copied = 0
|
|
for game in games:
|
|
art = game.get("art") or {}
|
|
appid = str(int(game["appid"]) & 0xFFFFFFFF)
|
|
for key, suffix in ART_SLOTS:
|
|
src = art.get(key)
|
|
if not src or not os.path.isfile(src):
|
|
continue
|
|
ext = os.path.splitext(src)[1].lower() or ".png"
|
|
if ext == ".jpeg":
|
|
ext = ".jpg"
|
|
name = appid + suffix + ext
|
|
dst = os.path.join(grid, name)
|
|
size = os.path.getsize(src)
|
|
if os.path.exists(dst) and name not in manifest:
|
|
continue
|
|
wanted[name] = size
|
|
if os.path.exists(dst) and manifest.get(name) == size:
|
|
continue
|
|
try:
|
|
shutil.copyfile(src, dst + ".tmp")
|
|
os.replace(dst + ".tmp", dst)
|
|
copied += 1
|
|
except OSError as e:
|
|
print("droiddeck-steam-shortcuts: could not copy art %s (%s)" % (name, e))
|
|
for name in list(manifest):
|
|
if name not in wanted:
|
|
try:
|
|
os.remove(os.path.join(grid, name))
|
|
except OSError:
|
|
pass
|
|
with open(manifest_path, "w", encoding="utf-8") as f:
|
|
json.dump(wanted, f)
|
|
if copied:
|
|
print("droiddeck-steam-shortcuts: %d piece(s) of art placed" % copied)
|
|
|
|
|
|
def main():
|
|
if len(sys.argv) < 3:
|
|
print(__doc__)
|
|
return 2
|
|
steam_root, listing = sys.argv[1], sys.argv[2]
|
|
try:
|
|
with open(listing, encoding="utf-8") as f:
|
|
games = json.load(f)
|
|
except (OSError, ValueError) as e:
|
|
print("droiddeck-steam-shortcuts: no list (%s)" % e)
|
|
return 0
|
|
acct = account_dir(steam_root)
|
|
if not acct:
|
|
print("droiddeck-steam-shortcuts: no signed-in account yet; nothing written")
|
|
return 0
|
|
config = os.path.join(acct, "config")
|
|
os.makedirs(config, exist_ok=True)
|
|
path = os.path.join(config, "shortcuts.vdf")
|
|
existing = {}
|
|
if os.path.isfile(path):
|
|
try:
|
|
with open(path, "rb") as f:
|
|
existing = parse(f.read()).get("shortcuts", {})
|
|
except (ValueError, IndexError, struct.error) as e:
|
|
print("droiddeck-steam-shortcuts: could not read the existing file (%s); keeping it unchanged" % e)
|
|
return 1
|
|
helper = os.path.join(os.path.dirname(os.path.abspath(__file__)), "droiddeck-steam-games")
|
|
loader = importlib.machinery.SourceFileLoader("steam_games", helper)
|
|
spec = importlib.util.spec_from_loader(loader.name, loader)
|
|
routing = importlib.util.module_from_spec(spec)
|
|
loader.exec_module(routing)
|
|
games, routes = routing.route(steam_root, acct, games)
|
|
wanted, seen = {}, {}
|
|
for game in games:
|
|
if game.get("exe") and game.get("name"):
|
|
entry = shortcut(game)
|
|
if entry["appid"] not in wanted:
|
|
wanted[entry["appid"]] = entry
|
|
seen[entry["appid"]] = game["seen"] if isinstance(game.get("seen"), int) else 0
|
|
record_path = os.path.join(config, RECORD)
|
|
kept, ours, record = reconcile(existing, wanted, seen, read_record(record_path))
|
|
entries = {str(i): v for i, v in enumerate(kept + ours)}
|
|
tmp = path + ".droiddeck"
|
|
with open(tmp, "wb") as f:
|
|
f.write(write({"shortcuts": entries}))
|
|
os.replace(tmp, path)
|
|
save_record(record_path, record)
|
|
print("droiddeck-steam-shortcuts: %d added game(s) written, %d of the client's own kept" % (len(ours), len(kept)))
|
|
install_art(acct, [g for g in games if g.get("exe") and g.get("name")])
|
|
try:
|
|
compat_path = "/usr/local/bin/steam-compatibility"
|
|
spec = importlib.util.spec_from_file_location("compat", compat_path, loader=importlib.machinery.SourceFileLoader("compat", compat_path))
|
|
compat = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(compat)
|
|
cfg = os.path.join(steam_root, "config", "config.vdf")
|
|
if os.path.isfile(cfg) and (ours or routes):
|
|
tools = os.path.join(steam_root, "compatibilitytools.d")
|
|
x64_tool = os.environ.get("DD_COMPAT_TOOL")
|
|
own = {}
|
|
if x64_tool:
|
|
protect = {compat.first_key(os.path.join(tools, e, "compatibilitytool.vdf"), ["compatibilitytools", "compat_tools"]) or e: e
|
|
for e in (os.listdir(tools) if os.path.isdir(tools) else [])}
|
|
own = {"default": x64_tool,
|
|
"directory": os.path.join(os.path.dirname(os.path.abspath(steam_root)), "droiddeck-compat"),
|
|
"linux_tool": "droiddeck-direct"}
|
|
else:
|
|
protect = compat.adopt_extras(tools)
|
|
owned = [str(int(g["appid"]) & 0xFFFFFFFF) for g in games if g.get("exe") and not g.get("native")]
|
|
used = compat.register_default(cfg, apps=owned + [str(appid) for appid in set(routes.values())],
|
|
protect=protect, owned=owned, **own)
|
|
print("droiddeck-steam-shortcuts: mapped them to %s" % used)
|
|
except Exception as e:
|
|
print("droiddeck-steam-shortcuts: compat mapping skipped (%s)" % e)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|