Merge origin/main into feat/malibridge-v25

This commit is contained in:
DroidDeck Merge committed 2026-11-01 19:59:31 +08:00
commit a2a5f84711
132 files changed
+7212 -156

No files matched your search

@@ -1,7 +1,15 @@
#!/usr/bin/python3
import json
import os
import re
import shutil
import signal
import socket
import subprocess
import sys
import tempfile
import time
import urllib.parse
os.environ.setdefault("FLATPAK_BWRAP", "/usr/local/bin/droiddeck-bwrap")
@@ -11,6 +19,22 @@ gi.require_version("Flatpak", "1.0")
from gi.repository import Flatpak, Gio, GLib
REMOTE = "flathub"
SYSTEM_BUS = "/run/dbus/system_bus_socket"
BUS_COMMANDS = ("install", "install-bundle", "uninstall", "update", "prune")
BUS_CONFIG = """<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<type>system</type>
<listen>unix:path=%s</listen>
<auth>EXTERNAL</auth>
<policy context="default">
<allow user="*"/>
<allow own="*"/>
<allow send_destination="*"/>
<allow receive_sender="*"/>
</policy>
</busconfig>
"""
def prepare_rootfs():
@@ -33,6 +57,116 @@ def prepare_rootfs():
emit(e="warning", message="could not prepare %s: %s" % (target, err))
def bus_reachable(path):
if isinstance(path, tuple):
try:
socket.create_connection(path, timeout=1).close()
return True
except (OSError, ValueError):
return False
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
sock.connect(path)
return True
except OSError:
return False
finally:
sock.close()
def system_bus_paths():
address = os.environ.get("DBUS_SYSTEM_BUS_ADDRESS", "")
if not address:
return [SYSTEM_BUS]
paths = []
for entry in filter(None, address.split(";")):
found = None
kind, _, params = entry.partition(":")
values = dict(part.partition("=")[::2] for part in params.split(","))
if kind == "unix":
if values.get("path"):
found = urllib.parse.unquote_to_bytes(values["path"])
elif values.get("abstract"):
found = b"\0" + urllib.parse.unquote_to_bytes(values["abstract"])
elif kind == "tcp":
port = urllib.parse.unquote(values.get("port", ""))
digits = port.lstrip("0")
if not re.fullmatch(r"[0-9]+", port, re.ASCII) or not 0 < len(digits) <= 5 or int(digits) > 65535:
continue
found = (urllib.parse.unquote(values.get("host", "localhost")), int(digits))
if found is None:
return None
paths.append(found)
return paths
def system_bus_reachable():
paths = system_bus_paths()
return paths is None or any(bus_reachable(path) for path in paths)
def stop_system_bus(bus):
if not bus:
return
proc, work, conf, path = bus
if proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
remove_bus_files(work, conf, path)
def remove_bus_files(work, conf, path):
for name in (path, conf):
try:
os.unlink(name)
except OSError:
pass
try:
os.rmdir(work)
except OSError:
pass
def private_system_bus():
if system_bus_reachable():
return None
daemon = shutil.which("dbus-daemon")
if not daemon:
emit(e="warning", message="no system bus and no dbus-daemon to start one")
return None
try:
work = tempfile.mkdtemp(prefix="droiddeck-flatpak-bus-")
except OSError as err:
emit(e="warning", message="could not start a private system bus: %s" % err)
return None
conf = os.path.join(work, "bus.conf")
path = os.path.join(work, "bus")
try:
with open(conf, "w") as f:
f.write(BUS_CONFIG % path)
proc = subprocess.Popen([daemon, "--config-file=" + conf, "--nofork", "--nopidfile"],
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except OSError as err:
remove_bus_files(work, conf, path)
emit(e="warning", message="could not start a private system bus: %s" % err)
return None
bus = (proc, work, conf, path)
for _ in range(50):
if bus_reachable(path) or proc.poll() is not None:
break
time.sleep(0.1)
if not bus_reachable(path):
stop_system_bus(bus)
emit(e="warning", message="the private system bus did not start")
return None
os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = "unix:path=" + path
return bus
def emit(**kw):
sys.stdout.write(json.dumps(kw) + "\n")
sys.stdout.flush()
@@ -257,6 +391,16 @@ def main():
cmd, args = sys.argv[1], sys.argv[2:]
if cmd in ("install", "install-bundle", "update"):
prepare_rootfs()
bus = private_system_bus() if cmd in BUS_COMMANDS else None
if bus:
signal.signal(signal.SIGTERM, lambda signum, frame: sys.exit(128 + signum))
try:
return command(cmd, args)
finally:
stop_system_bus(bus)
def command(cmd, args):
if cmd == "install" and args:
return install(args[0])
if cmd == "install-bundle" and args:
@@ -4,6 +4,7 @@ import json
import os
import re
import runpy
import signal
import subprocess
import sys
import tempfile
@@ -142,6 +143,12 @@ def protonfixes_off(game):
return {"PROTONFIXES_DISABLE": "1"}, [], "umu-protonfixes off (its ALSA audio and VC++ installer steps fail here)"
@fix
def little_nightmares_threads(game):
if game.stem == "littlenightmares":
return {"PROTONFIXES_DISABLE": "1"}, [], "Little Nightmares: umu-protonfixes off (its -onethread puts the game and renderer on one thread)"
@fix
def caravan_sandwitch_feature_level(game):
if "caravansandwitch.exe" in game.files or game.stem == "caravansandwitch-win64-shipping":
@@ -743,7 +750,202 @@ def directaudio_settings(env):
return env
def start(launch, launch_env, digest):
DIRT_GAMES = {"dirt3_game": "dirt3", "showdown": "dirtshowdown", "showdown_avx": "dirtshowdown"}
DIRT_WS2 = "/usr/local/lib/droiddeck-dirt/ws2_32.dll"
DIRT_MARKER = b"droiddeck-dirt-ws2_32"
DIRT_FIXED = "showdown_avx.exe".encode("utf-16-le")
DIRT_SAVED = ".droiddeck-original"
def dirt_telemetry_on(prefix, game):
"""Whether the game's UDP/motion output is on, as In-game stats or another listener sets it ("dbox" is its default)."""
try:
folders = [f for f in (Path(prefix) / "pfx/drive_c/users/steamuser/Documents/My Games").iterdir()
if f.name.lower().replace(" ", "") == game]
except OSError:
return False
for folder in folders:
try:
text = (folder / "hardwaresettings/hardware_settings_config.xml").read_text(encoding="latin-1")
except OSError:
continue
for tag in ("udp", "motion"):
element = re.search(r"<%s\s[^>]*>" % tag, text)
if element:
attrs = dict(re.findall(r'\s(\w+)="([^"]*)"', element.group(0)))
return attrs.get("enabled") == "true" and attrs.get("ip", "").lower() not in ("", "dbox")
return False
def proton_ws2(proton):
return Path(proton).parent / "files/lib/wine/i386-windows/ws2_32.dll"
def proton_has_dirt_fix(proton):
try:
return DIRT_FIXED in proton_ws2(proton).read_bytes()
except OSError:
return False
WINE_DLL_DIRS = ("/lib/wine/i386-windows", "/lib/wine/x86_64-windows", "/lib/wine/aarch64-windows", "/lib/wine",
"/lib/wine/fakedlls", "/lib64/wine", "/lib64/wine/fakedlls", "/lib64/wine/x86_64-windows")
def is_wine_dll_link(path):
"""A link into a Wine DLL folder: the rule Proton uses to tell its own DLLs in a prefix (file_is_wine_builtin_dll)."""
try:
return os.path.islink(path) and os.path.dirname(os.readlink(path)).endswith(WINE_DLL_DIRS)
except OSError:
return False
def prefix_ws2(prefix):
return Path(prefix) / "pfx/drive_c/windows/syswow64/ws2_32.dll"
def is_dirt_stand_in(path):
try:
return not path.is_symlink() and DIRT_MARKER in path.read_bytes()
except OSError:
return False
def dirt_ws2_wanted(command, env):
"""The prefix to give the ws2_32 stand-in: a DiRT 3 / Showdown launch with telemetry on and a Proton without the fix."""
prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/")
if len(command) < 3 or not prefix or not os.path.isfile(DIRT_WS2):
return None
game = DIRT_GAMES.get(os.path.splitext(os.path.basename(command[2]))[0].lower())
if not game or not dirt_telemetry_on(prefix, game) or proton_has_dirt_fix(command[0]):
return None
return prefix if os.path.lexists(prefix_ws2(prefix)) else None
def swap_dirt_ws2(prefix):
"""Puts the stand-in where the prefix's syswow64/ws2_32.dll is; that file is kept beside it."""
target = prefix_ws2(prefix)
saved = target.with_name(target.name + DIRT_SAVED)
data = Path(DIRT_WS2).read_bytes()
if not is_dirt_stand_in(target):
if os.path.lexists(saved):
os.unlink(saved)
os.replace(target, saved)
staged = target.with_name(target.name + ".droiddeck-tmp")
staged.write_bytes(data)
os.replace(staged, target)
def restore_dirt_ws2(prefix, proton):
"""Puts a Wine syswow64/ws2_32.dll back if the stand-in is still there; true when it changed something.
That is the prefix's own one, unless it was a link to a Wine DLL folder and the game ran on another Proton since
the swap: Proton then relinked the prefix's other DLLs to itself but left the stand-in, so a link to the running
Proton's ws2_32.dll goes back instead. Anything else (a native file, a link to a mod's DLL) goes back as it was."""
target = prefix_ws2(prefix)
saved = target.with_name(target.name + DIRT_SAVED)
own = proton_ws2(proton)
had_saved = os.path.lexists(saved)
if had_saved:
if os.path.lexists(target) and not is_dirt_stand_in(target):
os.unlink(saved)
return False
if not own.is_file() or not is_wine_dll_link(saved) or os.path.realpath(saved) == os.path.realpath(own):
os.replace(saved, target)
return True
os.unlink(saved)
if own.is_file() and (is_dirt_stand_in(target) or (had_saved and not os.path.lexists(target))):
link = target.with_name(target.name + ".droiddeck-tmp")
if os.path.lexists(link):
os.unlink(link)
os.symlink(own, link)
os.replace(link, target)
return True
return False
EGO_RESTRICTIONS = "system/hardware_settings_restrictions.xml"
EGO_CORE_RULE = re.compile(r'[ \t]*<restrict_if name="cores" op="(int_[a-z]+)" value="(\d+)">.*?</restrict_if>[ \t]*\r?\n', re.S)
EGO_OPS = {"int_eq": int.__eq__, "int_ne": int.__ne__, "int_ge": int.__ge__, "int_gt": int.__gt__,
"int_le": int.__le__, "int_lt": int.__lt__}
def windows_core_count(env):
count = (env.get("WINE_CPU_TOPOLOGY") or "").split(":", 1)[0]
return int(count) if count.isascii() and count.isdigit() and int(count) > 0 else None
def ego_core_plan(command, env):
"""Codemasters EGO games (DiRT 3, DiRT Showdown, GRID, F1 ...) pick their thread plan by core count from
system/hardware_settings_restrictions.xml. A count with no plan (5 when a phone's slow cores are left out) puts
every worker on one core, and DiRT 3 then stops on a loading screen. Adds a plan for the count Windows sees,
copied from the nearest smaller one; the file as it was is kept beside it. Returns a note, or None."""
count = windows_core_count(env)
if not count or len(command) < 3:
return None
folder = Path(command[2]).parent
path = folder / EGO_RESTRICTIONS
try:
raw = path.read_bytes()
except OSError:
return None
text = raw.decode("latin-1")
start, end = text.find("<cpu>"), text.find("</cpu>")
if start < 0 or end < start:
return None
rules = list(EGO_CORE_RULE.finditer(text, start, end))
if not rules or any(EGO_OPS.get(rule.group(1), lambda a, b: True)(count, int(rule.group(2))) for rule in rules):
return None
smaller = [rule for rule in rules if rule.group(1) == "int_eq" and int(rule.group(2)) < count]
if not smaller:
return None
source = max(smaller, key=lambda rule: int(rule.group(2)))
worker_map = re.search(r'workerMapFile="([^"]+)"', source.group(0))
if worker_map and not (folder / worker_map.group(1)).is_file():
return None
plan = source.group(0).replace('value="%s"' % source.group(2), 'value="%d"' % count, 1)
saved = path.with_name(path.name + DIRT_SAVED)
if not os.path.lexists(saved):
saved.write_bytes(raw)
handle, staged = tempfile.mkstemp(prefix=path.name + ".", suffix=".droiddeck-tmp", dir=str(path.parent))
try:
with os.fdopen(handle, "wb") as out:
out.write((text[:source.end()] + plan + text[source.end():]).encode("latin-1"))
os.replace(staged, path)
except BaseException:
if os.path.lexists(staged):
os.unlink(staged)
raise
return "%s had no thread plan for %d cores; added a copy of its %s-core plan" % (EGO_RESTRICTIONS, count, source.group(2))
def wait_forwarding_signals(process):
def forward(signum, frame):
try:
process.send_signal(signum)
except OSError:
pass
previous = {signum: signal.signal(signum, forward) for signum in (signal.SIGTERM, signal.SIGINT, signal.SIGHUP)}
try:
code = process.wait()
finally:
for signum, handler in previous.items():
signal.signal(signum, handler)
return 128 - code if code < 0 else code
def start(launch, launch_env, digest, after=None):
if after:
process = subprocess.Popen(launch, env=launch_env)
try:
return wait_forwarding_signals(process)
finally:
try:
after()
except OSError as error:
print("droiddeck-game-env: DiRT telemetry fix: prefix ws2_32 not put back (%s); the next launch does it" % error,
file=sys.stderr)
if digest:
code = subprocess.run(launch, env=launch_env, check=False).returncode
if code == 0:
@@ -768,6 +970,7 @@ def main():
except OSError:
digest = None
app_id = game_id(env)
dirt_prefix = None
if app_id:
gl_default = gl_vsync(env)
waiting = len(command) > 1 and command[1] == "waitforexitandrun"
@@ -787,6 +990,10 @@ def main():
if components:
own = fixes.get("WINEDLLOVERRIDES")
fixes["WINEDLLOVERRIDES"] = components + ";" + own if own else components
dirt_prefix = dirt_ws2_wanted(command, env)
if dirt_prefix:
own = fixes.get("WINEDLLOVERRIDES")
fixes["WINEDLLOVERRIDES"] = own + ";ws2_32=n,b" if own else "ws2_32=n,b"
path = Path.home() / ".config/droiddeck/game-environment.json"
config = {}
try:
@@ -822,6 +1029,13 @@ def main():
env = force_ssbs(command, env)
game_cores(env)
cpu_topology(env, command)
if app_id and len(command) > 2 and command[1] == "waitforexitandrun":
try:
note = ego_core_plan(command, env)
except OSError as error:
note = "no thread plan added for this core count (%s)" % error
if note:
print("droiddeck-game-env: " + note, file=sys.stderr)
try:
xbox = runpy.run_path(str(Path(__file__).with_name("droiddeck-xbox")))
except Exception as error:
@@ -834,16 +1048,45 @@ def main():
return 1
if xbox_result is not None:
return xbox_result
after = None
prefix = env.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/")
if app_id and prefix and len(command) > 1 and command[1] == "waitforexitandrun":
try:
if dirt_prefix:
after = lambda: restore_dirt_ws2(dirt_prefix, command[0])
swap_dirt_ws2(dirt_prefix)
print("droiddeck-game-env: DiRT telemetry fix: ws2_32 stand-in in the prefix until the game exits",
file=sys.stderr)
elif restore_dirt_ws2(prefix, command[0]):
print("droiddeck-game-env: DiRT telemetry fix: put the prefix's own ws2_32 back", file=sys.stderr)
except OSError as error:
print("droiddeck-game-env: DiRT telemetry fix not applied (%s)" % error, file=sys.stderr)
if after:
after = None
try:
restore_dirt_ws2(dirt_prefix, command[0])
except OSError as undo:
print("droiddeck-game-env: DiRT telemetry fix: prefix ws2_32 not put back (%s); Wine uses its own copy "
"and the next launch puts the file back" % undo, file=sys.stderr)
sync = load_sync()
launch, launch_env = select_sync(command, env, sync)
if launch[0] != command[0]:
try:
return start(launch, launch_env, digest)
return start(launch, launch_env, digest, after)
except OSError as error:
print("droiddeck-game-env: %s could not start (%s); stock launch" % (launch[0], error), file=sys.stderr)
launch, launch_env = command, without_pack(launch_env, env)
record_stock(sync, launch, launch_env)
return start(launch, launch_env, digest)
try:
return start(launch, launch_env, digest, after)
except OSError:
if after:
try:
after()
except OSError as error:
print("droiddeck-game-env: DiRT telemetry fix: prefix ws2_32 not put back (%s); the next launch does it" % error,
file=sys.stderr)
raise
if __name__ == "__main__":
@@ -623,7 +623,7 @@ case $mode in
fi
if [ "${BL_ZRAM:-0}" = 1 ]; then
step "zRAM compression: on"
[ "${BL_ZRAM_CLIENT:-1}" = 1 ] || step "zRAM: Steam and the hidden Epic Games Launcher are not compressed while games run (turned off in Performance)"
[ "${BL_ZRAM_CLIENT:-1}" = 1 ] || step "zRAM: Steam, the hidden Epic Games Launcher and Battle.net are not compressed while games run (turned off in Performance)"
_bl_ram_cap=0
case ${BL_RAM_CAP_MB:-} in
''|0) ;;
@@ -633,7 +633,7 @@ case $mode in
(
bl_zram_scan() {
_bl_game=0 _bl_pids=() _bl_others=() _bl_apps=
local _d _c _args _name _a _id _fex
local _d _c _args _name _a _id _fex _lower _bnet=() _bnet_game=0
for _d in /proc/[0-9]*; do
[ "${_d##*/}" != "$BASHPID" ] || continue
_args=()
@@ -658,14 +658,34 @@ case $mode in
;;
steam|steamwebhelper) _bl_pids+=("${_d##*/}") ;;
*)
_lower=${_name,,}
if [ "$_fex" = 1 ] && grep -qz '^DROIDDECK_EPIC_HIDDEN=1$' "$_d/environ" 2>/dev/null; then
_bl_pids+=("${_d##*/}")
elif [[ $_lower == [a-z]:[\\/]'program files (x86)'[\\/]battle.net[\\/]* ||
$_lower == [a-z]:[\\/]programdata[\\/]battle.net[\\/]* ]]; then
_bnet+=("${_d##*/}")
else
_bl_others+=("${_d##*/}")
if [[ $_lower == [a-z]:[\\/]* ]]; then
case ${_lower##*[\\/]} in
agent.exe|conhost.exe|explorer.exe|lsass.exe|plugplay.exe|rpcss.exe|services.exe|start.exe|\
steam.exe|svchost.exe|tabtip.exe|winedevice.exe|rundll32.exe|wineboot.exe|winemenubuilder.exe) ;;
*)
for _a in "${_args[@]:1}"; do
[ "${_a,,}" = -uid ] && _bnet_game=1
done
;;
esac
fi
fi
;;
esac
done
if [ "$_bnet_game" = 1 ]; then
_bl_pids+=("${_bnet[@]}")
else
_bl_others+=("${_bnet[@]}")
fi
}
bl_zram_usage() {
local _p _k _v _u _rss=0 _swap=0
@@ -20,6 +20,7 @@ LIBRARY = "/mnt/droiddeck-sd"
LEGACY_LIBRARY = "/mnt/bannerlator-sd"
PARKED = ".droiddeck-parked"
BASE_ID = 76561197960265728
SPACEWAR = 480
def read_json(path, default):
@@ -86,14 +87,14 @@ def identify(game):
folder = Path(game["folder"])
steamapps = folder.parent.parent
for library in ([steamapps] if folder.parent.name == "common" and steamapps.name == "steamapps" else []) + [folder.parent / WINNATIVE]:
matches = manifest_ids(library, folder.name)
matches = [appid for appid in manifest_ids(library, folder.name) if appid != SPACEWAR]
if matches:
return matches[0] if len(matches) == 1 else None
ids = set()
for parent in {folder, Path(game["exe"]).parent}:
try:
appid = int((parent / "steam_appid.txt").read_text().strip())
if 0 < appid < 0x80000000:
if 0 < appid < 0x80000000 and appid != SPACEWAR:
ids.add(appid)
except (OSError, ValueError):
pass
@@ -107,7 +108,7 @@ def identify(game):
ids = {int(item["id"]) for item in items
if key(item.get("name", "")) == key(game["name"]) and key(game["name"])}
if ids:
return next(iter(ids)) if len(ids) == 1 else None
return next(iter(ids)) if len(ids) == 1 and SPACEWAR not in ids else None
return None
except (OSError, ValueError, KeyError):
return None
@@ -423,20 +424,20 @@ def seed_install(steam_root, game, appid):
return True
def release(steam_root, game):
"""Undoes an import made before store games were skipped. Only our symlink and manifest go;
the store's folder stays where it is."""
def release(steam_root, game, stale, reason):
"""Undoes the imports of the game's folder as the Steam apps [stale] picks. Only our symlink and
manifest go; the folder stays where it is."""
steamapps = Path(steam_root) / "steamapps"
sources = {game.get("folder", ""), legacy_path(game.get("folder", ""))}
for target in sorted((steamapps / "common").glob("DroidDeck-*")):
appid = target.name.removeprefix("DroidDeck-")
if not (appid.isdigit() and target.is_symlink() and os.readlink(target) in sources):
if not (appid.isdigit() and target.is_symlink() and os.readlink(target) in sources and stale(int(appid))):
continue
for manifest in (steamapps / f"appmanifest_{appid}.acf", steamapps / PARKED / f"appmanifest_{appid}.acf"):
if manifest.is_file() and ours(manifest, appid):
manifest.unlink()
target.unlink()
print(f"droiddeck-steam-games: {game.get('name', appid)} came from {game['store']}; it is a shortcut again, not Steam app {appid}")
print(f"droiddeck-steam-games: {game.get('name', appid)} {reason}; it is no longer Steam app {appid}")
def route(steam_root, acct, games):
@@ -457,7 +458,7 @@ def route(steam_root, acct, games):
for game in games:
if store_game(game):
try:
release(steam_root, game)
release(steam_root, game, lambda old: True, f"came from {game['store']}")
except OSError as error:
print(f"droiddeck-steam-games: store import not undone ({error})")
shortcuts.append(game)
@@ -469,9 +470,14 @@ def route(steam_root, acct, games):
appid = candidates.get(folder, candidates.get(legacy_path(folder)))
stamp = source_stamp(game)
recorded = sources.get(folder, sources.get(legacy_path(folder)))
confirmed = (isinstance(appid, int) and 0 < appid < 0x80000000
and owned.get(str(appid)) is True and stamp is not None
and recorded in (stamp, [stamp[0], legacy_path(stamp[1])] + stamp[2:]))
current = stamp is not None and recorded in (stamp, [stamp[0], legacy_path(stamp[1])] + stamp[2:])
named = isinstance(appid, int) and 0 < appid < 0x80000000 and appid != SPACEWAR
try:
release(steam_root, game, lambda old: old == SPACEWAR or (current and named and old != appid),
"names another Steam app now")
except OSError as error:
print(f"droiddeck-steam-games: earlier import not undone ({error})")
confirmed = named and current and owned.get(str(appid)) is True
if confirmed:
try:
if seed_install(steam_root, game, appid):
@@ -480,7 +486,7 @@ def route(steam_root, acct, games):
print(f"droiddeck-steam-games: {game.get('name', appid)} stays a shortcut this session: it could not be linked as Steam app {appid}")
except OSError as error:
print(f"droiddeck-steam-games: import unavailable ({error})")
elif isinstance(appid, int) and 0 < appid < 0x80000000 and seeded(steamapps, game, appid):
elif named and seeded(steamapps, game, appid):
routes[str(game["appid"])] = appid
continue
shortcuts.append(game)