From 4e25611f70d3c284de37d94544a7412284658ff4 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Wed, 21 Oct 2026 09:28:14 -0400 Subject: [PATCH] perf: games skip Steam's install-script evaluator once it has finished The client runs a game's install scripts through Proton at every launch, a whole Wine start before the game's own. When the evaluator already exited cleanly for the same scripts in this prefix and every process they name is still marked as run, it is not started again. Moonlighter launches went from 10.5 to 7.7 s. --- .../overlay/usr/local/bin/droiddeck-game-env | 119 +++++++++++++++++- tools/tests/test_game_environment.py | 68 ++++++++++ 2 files changed, 182 insertions(+), 5 deletions(-) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index 827ad60e85..a38d77d8c5 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -1,4 +1,5 @@ #!/usr/bin/python3 +import hashlib import json import os import re @@ -308,17 +309,118 @@ def mark_install_scripts(command, env): starts - on first launch the shared ones may still be downloading, and on ARM64 Proton the VC++ ones stop with an error.""" if not any(arg.lower().replace("\\", "/").endswith("iscriptevaluator.exe") for arg in command): - return + return [] root = env.get("BL_X64_STEAM_ROOT") or env.get("STEAM_COMPAT_CLIENT_INSTALL_PATH") or str(Path.home() / ".local/share/Steam") scripts = [str(Path(root) / arg.replace("\\", "/")) for arg in command if EVALUATOR_SCRIPT.search(arg)] scripts = [path for path in scripts if os.path.isfile(path)] if not scripts: - return + return [] try: subprocess.run([SEED_REDISTS, root], env=dict(env, DROIDDECK_SEED_SCRIPTS="\n".join(scripts)), stdout=sys.stderr, timeout=60, check=False) except (OSError, subprocess.SubprocessError) as error: print("droiddeck-game-env: install scripts not marked (%s)" % error, file=sys.stderr) + return scripts + + +INSTALL_STAMP = ".droiddeck-install-scripts" +VDF_TOKEN = re.compile(r'"((?:[^"\\]|\\.)*)"|([{}])') + + +def read_vdf(text): + tokens = iter(VDF_TOKEN.findall(text)) + + def block(): + items = [] + for word, brace in tokens: + if brace == "}": + return items + if brace: + raise ValueError("a block without a name") + value, opens = next(tokens, ("", "}")) + if opens == "}": + raise ValueError("a name without a value") + items.append((re.sub(r"\\(.)", r"\1", word), block() if opens else re.sub(r"\\(.)", r"\1", value))) + return items + + return block() + + +def run_keys(items, appid=""): + appid = next((value for name, value in items if name.lower() == "appid" and isinstance(value, str)), appid) + found = set() + for name, value in items: + if not isinstance(value, list): + continue + if any(isinstance(v, str) and re.fullmatch(r"process [0-9]+", n.lower()) for n, v in value): + key = next((v for n, v in value if n.lower() == "hasrunkey" and isinstance(v, str)), + "HKEY_LOCAL_MACHINE\\Software\\Valve\\Steam\\Apps\\" + appid) + found.add((key.lower(), name.lower())) + found |= run_keys(value, appid) + return found + + +def registry_values(path): + found, section = set(), None + with open(path, encoding="utf-8", errors="replace") as reg: + for line in reg: + if line.startswith("["): + section = line[1:line.find("]")].replace("\\\\", "\\").lower() if "]" in line else None + elif section and line.startswith('"') and '"=' in line: + found.add((section, line[1:line.index('"=')].replace("\\\\", "\\").lower())) + return found + + +def has_run(key, value, values): + path = key[len("hkey_local_machine\\"):] if key.startswith("hkey_local_machine\\") else None + if path is None: + return False + views = [path] + if path.startswith("software\\"): + views.append("software\\wow6432node\\" + path[len("software\\"):]) + return any((view, value) in values for view in views) + + +def scripts_digest(scripts): + digest = hashlib.sha256() + for path in sorted(scripts): + digest.update(os.path.basename(path).encode() + b"\0") + digest.update(Path(path).read_bytes()) + return digest.hexdigest() + + +def install_scripts_done(scripts, env): + prefix = Path(env.get("STEAM_COMPAT_DATA_PATH", "/nonexistent")) / "pfx" + try: + if (prefix / INSTALL_STAMP).read_text().strip() != scripts_digest(scripts): + return False + keys = set() + for path in scripts: + keys |= run_keys(read_vdf(Path(path).read_text(encoding="utf-8", errors="replace"))) + values = registry_values(prefix / "system.reg") + except (OSError, ValueError): + return False + return bool(keys) and all(has_run(key, value, values) for key, value in keys) + + +def record_install_scripts(digest, env): + prefix = Path(env.get("STEAM_COMPAT_DATA_PATH", "/nonexistent")) / "pfx" + try: + if prefix.is_dir(): + staged = prefix / (INSTALL_STAMP + ".tmp") + staged.write_text(digest + "\n") + staged.replace(prefix / INSTALL_STAMP) + except OSError as error: + print("droiddeck-game-env: install scripts not recorded (%s)" % error, file=sys.stderr) + + +def start(launch, launch_env, digest): + if digest: + code = subprocess.run(launch, env=launch_env, check=False).returncode + if code == 0: + record_install_scripts(digest, launch_env) + return code + return os.execvpe(launch[0], launch, launch_env) def main(): @@ -327,7 +429,14 @@ def main(): return 2 env = game_gpu(os.environ.copy()) mic_enabled = env.get("BL_MIC_ENABLED") - mark_install_scripts(command, env) + scripts = mark_install_scripts(command, env) + if scripts and install_scripts_done(scripts, env): + print("droiddeck-game-env: install scripts already run in this prefix; evaluator skipped", file=sys.stderr) + return 0 + try: + digest = scripts_digest(scripts) if scripts else None + except OSError: + digest = None app_id = game_id(env) if app_id: waiting = len(command) > 1 and command[1] == "waitforexitandrun" @@ -376,12 +485,12 @@ def main(): launch, launch_env = select_sync(command, env, sync) if launch[0] != command[0]: try: - os.execvpe(launch[0], launch, launch_env) + return start(launch, launch_env, digest) 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) - os.execvpe(launch[0], launch, launch_env) + return start(launch, launch_env, digest) if __name__ == "__main__": diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index 70c5f7cbf5..c5c308c8cc 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -4,6 +4,7 @@ import os import shutil from pathlib import Path import runpy +import shlex import subprocess import sys import tempfile @@ -94,6 +95,73 @@ class GameEnvironmentTest(unittest.TestCase): self.assertEqual(calls[0][0][1], str(root)) self.assertEqual(calls[0][1]["DROIDDECK_SEED_SCRIPTS"], str(script)) + SCRIPT = ('"evaluatorscript" { "0" { "appid" "228980" "compat_installscript" { "run process" { ' + '"x64 14.28.29334.0" { "hasrunkey" "HKEY_LOCAL_MACHINE\\\\Software\\\\Valve\\\\Steam\\\\Apps\\\\CommonRedist\\\\vcredist\\\\2019" ' + '"process 1" "%INSTALLDIR%\\\\x64.cmd" "requirement_os" { "Is64BitWindows" "1" } } ' + '"Setup" { "process 1" "%INSTALLDIR%\\\\setup.exe" } } } } }') + REDIST = "[Software\\\\Wow6432Node\\\\Valve\\\\Steam\\\\Apps\\\\CommonRedist\\\\vcredist\\\\2019] 1\n\"x64 14.28.29334.0\"=dword:00000001\n" + SETUP = "[Software\\\\Valve\\\\Steam\\\\Apps\\\\228980] 1\n\"Setup\"=dword:00000001\n" + + def test_install_script_run_keys_default_to_the_app_key(self): + keys = MODULE["run_keys"](MODULE["read_vdf"](self.SCRIPT)) + self.assertEqual(keys, { + ("hkey_local_machine\\software\\valve\\steam\\apps\\commonredist\\vcredist\\2019", "x64 14.28.29334.0"), + ("hkey_local_machine\\software\\valve\\steam\\apps\\228980", "setup"), + }) + with self.assertRaises(ValueError): + MODULE["read_vdf"]('"a" { "b" }') + + def test_install_scripts_count_as_done_only_when_recorded_and_still_marked(self): + with tempfile.TemporaryDirectory() as tmp: + script = Path(tmp) / "evaluatorscript_1.vdf" + script.write_text(self.SCRIPT) + prefix = Path(tmp) / "compat/pfx" + env = {"STEAM_COMPAT_DATA_PATH": str(prefix.parent)} + done = MODULE["install_scripts_done"] + self.assertFalse(done([str(script)], env)) + prefix.mkdir(parents=True) + MODULE["record_install_scripts"](MODULE["scripts_digest"]([str(script)]), env) + self.assertFalse(done([str(script)], env)) + (prefix / "system.reg").write_text(self.REDIST) + self.assertFalse(done([str(script)], env)) + (prefix / "system.reg").write_text(self.REDIST + self.SETUP) + self.assertTrue(done([str(script)], env)) + script.write_text(self.SCRIPT.replace("x64.cmd", "x64-update.cmd")) + self.assertFalse(done([str(script)], env)) + script.write_text('"InstallScript" { "Registry" { "HKEY_LOCAL_MACHINE\\\\Software\\\\Game" { "string" { "Path" "%INSTALLDIR%" } } } }') + MODULE["record_install_scripts"](MODULE["scripts_digest"]([str(script)]), env) + self.assertFalse(done([str(script)], env)) + + def test_the_evaluator_is_skipped_once_it_finished_the_same_scripts(self): + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + (root / "legacycompat").mkdir() + evaluator = root / "legacycompat/iscriptevaluator.exe" + evaluator.write_bytes(b"evaluator") + script = root / "legacycompat/evaluatorscript_1.vdf" + prefix = root / "compatdata/1/pfx" + prefix.mkdir(parents=True) + (prefix / "system.reg").write_text(self.REDIST + self.SETUP) + runs = root / "runs" + proton = root / "proton" + proton.write_text("#!/bin/sh\necho run >> %s\nexit ${EXIT_CODE:-0}\n" % shlex.quote(str(runs))) + proton.chmod(0o755) + + def evaluate(exit_code="0"): + script.write_text(self.SCRIPT if exit_code != "changed" else self.SCRIPT.replace("setup.exe", "setup-update.exe")) + env = dict(os.environ, HOME=tmp, STEAM_COMPAT_CLIENT_INSTALL_PATH=str(root), + STEAM_COMPAT_DATA_PATH=str(prefix.parent), EXIT_CODE="0" if exit_code == "changed" else exit_code) + result = subprocess.run([sys.executable, str(BIN / "droiddeck-game-env"), str(proton), "run", + str(evaluator), "legacycompat\\evaluatorscript_1.vdf"], + env=env, text=True, capture_output=True) + return result.returncode, len(runs.read_text().splitlines()) if runs.exists() else 0 + + self.assertEqual(evaluate("3"), (3, 1)) + self.assertFalse((prefix / ".droiddeck-install-scripts").exists()) + self.assertEqual(evaluate(), (0, 2)) + self.assertEqual(evaluate(), (0, 2)) + self.assertEqual(evaluate("changed"), (0, 3)) + def test_profile_precedence_and_unset(self): env = {"KEEP": "inherited", "REMOVE": "inherited", "CUSTOM": "launch option"} config = {"version": 1, "shared": {"CUSTOM": "shared"}, "games": {"42": {"REMOVE": None, "CUSTOM": "game", "EMPTY": ""}}}