mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
Share installed games between the arm64 and x86-64 Steam clients
This commit is contained in:
1 parent
8d407aa1e8
commit
c866bb90db
8 files changed
+486
-14
No files matched your search
@@ -396,6 +396,7 @@ case $mode in
|
||||
steam_root=$XDG_DATA_HOME/Steam
|
||||
if [ -f "${BL_X64_HOME:-$HOME/.droiddeck-x64}/.local/share/Steam/local.vdf" ]; then
|
||||
droiddeck-steam-x64 --sync-login || true
|
||||
droiddeck-steam-x64 --share || true
|
||||
fi
|
||||
steam_client=("$steam_root/steamrtarm64/steam")
|
||||
_bl_srt_shim=$HOME/.droiddeck-steamrt-shim
|
||||
|
||||
+266
@@ -0,0 +1,266 @@
|
||||
#!/usr/bin/python3
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import runpy
|
||||
import sys
|
||||
|
||||
NOT_GAMES = frozenset((
|
||||
"228980", "1070560", "1391110", "1628350", "1493710", "3127680",
|
||||
"4183110", "4185400", "4427310", "4628740",
|
||||
))
|
||||
TOOL_DIR = re.compile(r"(steam ?linux ?runtime.*|proton.*|steamworks shared|fex|steamvr)", re.I)
|
||||
AUTOMATIC = ("", "droiddeck-direct", "droiddeck-fex", "droiddeck-proton-arm64", "droiddeck-proton-11-arm64")
|
||||
STATE = ".droiddeck-shared.json"
|
||||
LABELS = {"arm64": "ARM64 client games", "x86-64": "x86-64 client games"}
|
||||
|
||||
|
||||
def say(message):
|
||||
print("droiddeck-steam-share: " + message)
|
||||
|
||||
|
||||
def read(path):
|
||||
try:
|
||||
with open(path, encoding="utf-8", errors="surrogateescape") as f:
|
||||
return f.read()
|
||||
except OSError:
|
||||
return None
|
||||
|
||||
|
||||
def write(path, text):
|
||||
staged = path + ".droiddeck-new"
|
||||
with open(staged, "w", encoding="utf-8", errors="surrogateescape") as f:
|
||||
f.write(text)
|
||||
os.replace(staged, path)
|
||||
|
||||
|
||||
def field(text, key):
|
||||
m = re.search(r'"%s"\s+"([^"]*)"' % re.escape(key), text or "", re.I)
|
||||
return m.group(1) if m else None
|
||||
|
||||
|
||||
def number(text, key):
|
||||
value = field(text, key)
|
||||
return int(value) if value and value.isdigit() else 0
|
||||
|
||||
|
||||
def same(a, b):
|
||||
return os.path.realpath(a) == os.path.realpath(b)
|
||||
|
||||
|
||||
def libraries(root, skip):
|
||||
found = [root] + [p.replace("\\\\", "\\") for p in
|
||||
re.findall(r'"path"\s+"((?:[^"\\]|\\.)*)"', read(os.path.join(root, "steamapps", "libraryfolders.vdf")) or "")]
|
||||
return [p for p in dict.fromkeys(found) if not any(same(p, s) for s in skip)]
|
||||
|
||||
|
||||
def installs(libs):
|
||||
games = {}
|
||||
for library in libs:
|
||||
steamapps = os.path.join(library, "steamapps")
|
||||
try:
|
||||
names = sorted(os.listdir(steamapps))
|
||||
except OSError:
|
||||
continue
|
||||
for name in names:
|
||||
m = re.fullmatch(r"appmanifest_(\d+)\.acf", name)
|
||||
if not m or m.group(1) in NOT_GAMES or m.group(1) in games:
|
||||
continue
|
||||
text = read(os.path.join(steamapps, name))
|
||||
folder = field(text, "installdir")
|
||||
if (not folder or folder in (".", "..") or "/" in folder or TOOL_DIR.fullmatch(folder)
|
||||
or not number(text, "StateFlags") & 4
|
||||
or not os.path.isdir(os.path.join(steamapps, "common", folder))):
|
||||
continue
|
||||
games[m.group(1)] = (steamapps, text, folder)
|
||||
return games
|
||||
|
||||
|
||||
def link(path, target):
|
||||
if os.path.islink(path):
|
||||
if os.readlink(path) == target:
|
||||
return True
|
||||
os.unlink(path)
|
||||
elif os.path.lexists(path):
|
||||
return False
|
||||
os.symlink(target, path)
|
||||
return True
|
||||
|
||||
|
||||
def unlink(path):
|
||||
if os.path.islink(path):
|
||||
os.unlink(path)
|
||||
|
||||
|
||||
def emptied(src, folder):
|
||||
path = os.path.join(src, "common", folder)
|
||||
return not os.path.isdir(path) or not os.listdir(path)
|
||||
|
||||
|
||||
def sync(view, source, own):
|
||||
steamapps = os.path.join(view, "steamapps")
|
||||
for sub in ("common", "compatdata"):
|
||||
os.makedirs(os.path.join(steamapps, sub), exist_ok=True)
|
||||
state = {}
|
||||
try:
|
||||
state = json.loads(read(os.path.join(view, STATE)) or "{}")
|
||||
except ValueError:
|
||||
pass
|
||||
managed = {app: tuple(v) for app, v in state.get("shared", {}).items() if isinstance(v, list) and len(v) == 2}
|
||||
hidden = state.get("hidden", {})
|
||||
shared, back = {}, []
|
||||
for app, (src, folder) in managed.items():
|
||||
if app in source and app not in own:
|
||||
continue
|
||||
manifest = os.path.join(steamapps, "appmanifest_%s.acf" % app)
|
||||
if app not in own and not os.path.isfile(manifest) and emptied(src, folder) and os.path.isfile(os.path.join(src, "appmanifest_%s.acf" % app)):
|
||||
os.unlink(os.path.join(src, "appmanifest_%s.acf" % app))
|
||||
unlink(os.path.join(steamapps, "common", folder))
|
||||
unlink(os.path.join(steamapps, "compatdata", app))
|
||||
if os.path.isfile(manifest):
|
||||
os.unlink(manifest)
|
||||
for app, (src, text, folder) in sorted(source.items(), key=lambda item: int(item[0])):
|
||||
if app in own:
|
||||
continue
|
||||
stamp = number(text, "LastUpdated")
|
||||
if hidden.get(app) == stamp:
|
||||
continue
|
||||
hidden.pop(app, None)
|
||||
manifest = os.path.join(steamapps, "appmanifest_%s.acf" % app)
|
||||
mine = read(manifest)
|
||||
if app in managed and mine is None:
|
||||
unlink(os.path.join(steamapps, "common", folder))
|
||||
unlink(os.path.join(steamapps, "compatdata", app))
|
||||
if emptied(src, folder):
|
||||
os.unlink(os.path.join(src, "appmanifest_%s.acf" % app))
|
||||
else:
|
||||
hidden[app] = stamp
|
||||
continue
|
||||
if not link(os.path.join(steamapps, "common", folder), os.path.join(src, "common", folder)):
|
||||
continue
|
||||
if os.path.isdir(os.path.join(src, "compatdata", app)):
|
||||
link(os.path.join(steamapps, "compatdata", app), os.path.join(src, "compatdata", app))
|
||||
if mine is not None and number(mine, "LastUpdated") > stamp:
|
||||
write(os.path.join(src, "appmanifest_%s.acf" % app), mine)
|
||||
back.append(app)
|
||||
elif mine != text:
|
||||
write(manifest, text)
|
||||
shared[app] = [src, folder]
|
||||
state = {"shared": shared, "hidden": hidden}
|
||||
write(os.path.join(view, STATE), json.dumps(state, indent=1, sort_keys=True) + "\n")
|
||||
return shared, back
|
||||
|
||||
|
||||
def tools(root):
|
||||
names = set()
|
||||
folder = os.path.join(root, "compatibilitytools.d")
|
||||
try:
|
||||
entries = os.listdir(folder)
|
||||
except OSError:
|
||||
return names
|
||||
for entry in entries:
|
||||
text = read(os.path.join(folder, entry, "compatibilitytool.vdf")) or ""
|
||||
m = re.search(r'"compat_tools"\s*\{\s*"([^"]+)"', text)
|
||||
if m:
|
||||
names.add(m.group(1))
|
||||
return names
|
||||
|
||||
|
||||
def mapping_block(text):
|
||||
m = re.search(r'"CompatToolMapping"\s*\{', text)
|
||||
if not m:
|
||||
return None
|
||||
depth, k = 1, m.end()
|
||||
while depth and k < len(text):
|
||||
depth += {"{": 1, "}": -1}.get(text[k], 0)
|
||||
k += 1
|
||||
return m.end(), k - 1
|
||||
|
||||
|
||||
def entries(text):
|
||||
span = mapping_block(text)
|
||||
found = {}
|
||||
if not span:
|
||||
return found
|
||||
inner = text[span[0]:span[1]]
|
||||
for m in re.finditer(r'"(\d+)"\s*\{([^{}]*)\}', inner):
|
||||
name = re.search(r'"name"\s*"([^"]*)"', m.group(2))
|
||||
found.setdefault(m.group(1), name.group(1) if name else "")
|
||||
return found
|
||||
|
||||
|
||||
def set_tools(config, wanted):
|
||||
text = read(config)
|
||||
if text is None or not wanted:
|
||||
return []
|
||||
span = mapping_block(text)
|
||||
if not span:
|
||||
return []
|
||||
inner = text[span[0]:span[1]]
|
||||
changed = []
|
||||
for app, tool in sorted(wanted.items(), key=lambda item: int(item[0])):
|
||||
m = re.search(r'"%s"\s*\{([^{}]*)\}' % app, inner)
|
||||
if m:
|
||||
name = re.search(r'"name"\s*"([^"]*)"', m.group(1))
|
||||
name = name.group(1) if name else ""
|
||||
if name == tool or not (name in AUTOMATIC or name.startswith("proton_")):
|
||||
continue
|
||||
inner = inner[:m.start()] + inner[m.end():]
|
||||
inner = ('\n\t\t\t\t\t"%s"\n\t\t\t\t\t{\n\t\t\t\t\t\t"name"\t\t"%s"\n\t\t\t\t\t\t"config"\t\t""\n'
|
||||
'\t\t\t\t\t\t"priority"\t\t"250"\n\t\t\t\t\t}' % (app, tool)) + inner
|
||||
changed.append(app)
|
||||
if changed:
|
||||
write(config, text[:span[0]] + inner + text[span[1]:])
|
||||
return changed
|
||||
|
||||
|
||||
def translate(name, here, available):
|
||||
if name == "droiddeck-fex" and here == "x86-64":
|
||||
return "droiddeck-direct"
|
||||
if name == "droiddeck-direct" and here == "arm64":
|
||||
return "droiddeck-fex"
|
||||
return name if name in available else None
|
||||
|
||||
|
||||
def library_helper():
|
||||
sibling = os.path.join(os.path.dirname(os.path.realpath(__file__)), "droiddeck-steam-library")
|
||||
return runpy.run_path(sibling if os.path.isfile(sibling) else "/usr/local/bin/droiddeck-steam-library")
|
||||
|
||||
|
||||
def main(argv):
|
||||
if len(argv) != 5 or argv[0] not in LABELS:
|
||||
say("usage: droiddeck-steam-share arm64|x86-64 ARM_STEAM X64_STEAM X64_CARD SHARED_DIR")
|
||||
return 2
|
||||
here, arm, x64, x64_card, shared_dir = argv
|
||||
views = {name: os.path.join(shared_dir, name) for name in LABELS}
|
||||
roots = {"arm64": arm, "x86-64": x64}
|
||||
other = "x86-64" if here == "arm64" else "arm64"
|
||||
skip = list(views.values()) + [x64, x64_card]
|
||||
arm_libs = libraries(arm, skip)
|
||||
x64_libs = [x64] + [p for p in libraries(x64, list(views.values()) + [arm]) if not same(p, x64)]
|
||||
games = {"arm64": installs(arm_libs), "x86-64": installs(x64_libs)}
|
||||
view = views[other]
|
||||
shared, back = sync(view, games[other], games[here])
|
||||
helper = library_helper()
|
||||
if not any(re.fullmatch(r"appmanifest_\d+\.acf", n) for n in os.listdir(os.path.join(view, "steamapps"))):
|
||||
helper["forget"](roots[here], view)
|
||||
return 0
|
||||
helper["register"](roots[here], view, LABELS[other])
|
||||
helper["relabel"](roots[here], view, LABELS[other])
|
||||
picks = entries(read(os.path.join(roots[other], "config", "config.vdf")) or "")
|
||||
available = tools(roots[here]) | {"droiddeck-direct" if here == "x86-64" else "droiddeck-fex"}
|
||||
wanted = {}
|
||||
for app in shared:
|
||||
tool = translate(picks.get(app) or picks.get("0", ""), here, available)
|
||||
if tool:
|
||||
wanted[app] = tool
|
||||
changed = set_tools(os.path.join(roots[here], "config", "config.vdf"), wanted)
|
||||
say("%d game(s) from the %s client%s%s" % (
|
||||
len(shared), other,
|
||||
"; tools set for %s" % " ".join(changed) if changed else "",
|
||||
"; manifests sent back for %s" % " ".join(back) if back else ""))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
@@ -87,7 +87,7 @@ x64_map_direct() {
|
||||
[ -f "$STEAM_X64/config/config.vdf" ] || return 0
|
||||
if [ "$mode" = write ] && pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1; then return 0; fi
|
||||
python3 - "$mode" "$STEAM_X64/config/config.vdf" "$STEAM_X64/appcache/appinfo.vdf" "$X64_HOME/direct-auto.json" \
|
||||
"$X64_HOME/direct-sniper" "$STEAM_X64/steamapps:/mnt/droiddeck-sd/x86-64/steamapps" "$(x64_default_tool)" \
|
||||
"$X64_HOME/direct-sniper" "$STEAM_X64/steamapps:/mnt/droiddeck-sd/x86-64/steamapps:$X64_HOME/shared/arm64/steamapps" "$(x64_default_tool)" \
|
||||
"$REAL_HOME/.bl-compat-pending" ${BL_X64_DIRECT_APPS:-550} \
|
||||
<<'PY' || { [ "$mode" = live ] || step "could not map titles to DroidDeck direct"; }
|
||||
# Steam does not swap an installed build when the tool changes: a title installed for Windows stays on Proton.
|
||||
@@ -610,6 +610,7 @@ RUN
|
||||
printf '"InstallConfigStore"\n{\n\t"Software"\n\t{\n\t\t"Valve"\n\t\t{\n\t\t\t"Steam"\n\t\t\t{\n\t\t\t\t"CompatToolMapping"\n\t\t\t\t{\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n' \
|
||||
> "$STEAM_X64/config/config.vdf"
|
||||
fi
|
||||
x64_share x86-64
|
||||
x64_map_direct
|
||||
# Each wrapper execs droiddeck-x64-arm-proton through an arm64 binary, which FEX hands straight to the CPU.
|
||||
arm_tools=$ARM_STEAM/compatibilitytools.d
|
||||
@@ -659,9 +660,9 @@ VDF
|
||||
# Each client keeps its own libraries; written only while no x86 client runs, as it rewrites these on exit.
|
||||
if ! pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1; then
|
||||
listing_ok=0
|
||||
if python3 - "$STEAM_X64" "${BL_X64_STEAM_LIBRARIES:-}" "$STEAM_X64/config/libraryfolders.vdf" "$STEAM_X64/steamapps/libraryfolders.vdf" <<'PY'; then
|
||||
if BL_X64_SHARED=$X64_HOME/shared/arm64 python3 - "$STEAM_X64" "${BL_X64_STEAM_LIBRARIES:-}" "$STEAM_X64/config/libraryfolders.vdf" "$STEAM_X64/steamapps/libraryfolders.vdf" <<'PY'; then
|
||||
import json, os, re, sys
|
||||
keep = {os.path.realpath(sys.argv[1])}
|
||||
keep = {os.path.realpath(sys.argv[1]), os.path.realpath(os.environ["BL_X64_SHARED"])}
|
||||
# Without the session's list nothing is known about the card: leave the folders as they are.
|
||||
if not sys.argv[2]:
|
||||
sys.exit(3)
|
||||
@@ -911,6 +912,14 @@ x64_exec() {
|
||||
exec "${BL_FEX:-/usr/local/bin/FEX}" "$STEAM_X64/steamrt64/steam" "$@"
|
||||
}
|
||||
|
||||
x64_share() {
|
||||
pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1 && return 0
|
||||
pgrep -f 'steamrtarm64/steam( |$)' >/dev/null 2>&1 && return 0
|
||||
[ -d "$STEAM_X64/steamapps" ] && [ -d "$ARM_STEAM/steamapps" ] || return 0
|
||||
droiddeck-steam-share "$1" "$ARM_STEAM" "$STEAM_X64" /mnt/droiddeck-sd/x86-64 "$X64_HOME/shared" ||
|
||||
step "could not share the games between the clients"
|
||||
}
|
||||
|
||||
# Steam renews its token every few days and refuses old copies: copy the newest every start, never while a client runs.
|
||||
x64_sync_login() {
|
||||
pgrep -f 'steamrt64/steam( |$)' >/dev/null 2>&1 && return 0
|
||||
@@ -1027,6 +1036,7 @@ PY
|
||||
|
||||
case "${1:-}" in
|
||||
--sync-login) x64_sync_login; exit 0 ;;
|
||||
--share) x64_share arm64; exit 0 ;;
|
||||
--prepare) x64_prepare; exit 0 ;;
|
||||
--exec) shift; x64_exec "$@" ;;
|
||||
--gpu) x64_gpu; exit $? ;;
|
||||
|
||||
Reference in new issue
Block a user