From 2a97ecf163089d54f16805da5e0ff7db62de1864 Mon Sep 17 00:00:00 2001 From: maxjivi05 Date: Thu, 26 Nov 2026 22:35:21 -0500 Subject: [PATCH] Apply Game cores changed during a session to the next game launch The game cores list reached the guest only in the session environment, so a change made while the session ran was ignored until the next session, and a launch kept the old cores. The app now publishes the current choice to the guest with the other game settings, and the launch wrapper reads it at each launch, falling back to the session value when the file is missing. --- .../launcher/session/GameEnvironmentStore.kt | 5 ++ .../launcher/session/SessionPrefs.kt | 2 + .../session/GameEnvironmentStoreTest.kt | 12 ++++ .../overlay/usr/local/bin/steam-compatibility | 8 +++ tools/tests/test_game_cpus_live.py | 57 +++++++++++++++++++ 5 files changed, 84 insertions(+) create mode 100644 tools/tests/test_game_cpus_live.py diff --git a/app/src/main/java/com/droiddeck/launcher/session/GameEnvironmentStore.kt b/app/src/main/java/com/droiddeck/launcher/session/GameEnvironmentStore.kt index d4bdce833e..1bb5666e1c 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/GameEnvironmentStore.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/GameEnvironmentStore.kt @@ -13,6 +13,7 @@ object GameEnvironmentStore { private const val TAG = "GameEnvironmentStore" private const val SETTINGS = "game-environment.json" private const val GUEST_FILE = "root/.config/droiddeck/game-environment.json" + private const val GAME_CORES_FILE = "root/.config/droiddeck/game-cores" const val DXVK_CONFIG = "dxvkConfig" const val DXVK_CONFIG_GAMES = "dxvkConfigGames" const val GAME_DEBUG_LOGS = "gameDebugLogs" @@ -138,6 +139,10 @@ object GameEnvironmentStore { gameDebugLogsOverride ?: SessionPrefs.gameDebugLogsEffective(context), ) write(File(LinuxRuntime.rootDir(context), GUEST_FILE), json.toString()) + write( + File(LinuxRuntime.rootDir(context), GAME_CORES_FILE), + com.droiddeck.launcher.core.CpuCores.restrictionOrEmpty(SessionPrefs.gameCpus(context)) + "\n", + ) } private fun write(file: File, text: String) { diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt index b5794eff94..d8d5521d4a 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionPrefs.kt @@ -959,6 +959,8 @@ object SessionPrefs { fun setGameCpus(context: Context, list: String) { prefs(context).edit().putString("gameCpus", list).apply() + runCatching { GameEnvironmentStore.publish(context) } + .onFailure { android.util.Log.e("GameEnvironment", "Could not update game environment", it) } } fun noXalia(context: Context): Boolean = prefs(context).getBoolean("noXalia", true) diff --git a/app/src/test/java/com/droiddeck/launcher/session/GameEnvironmentStoreTest.kt b/app/src/test/java/com/droiddeck/launcher/session/GameEnvironmentStoreTest.kt index 273d91d1d9..7d303b7d82 100644 --- a/app/src/test/java/com/droiddeck/launcher/session/GameEnvironmentStoreTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/session/GameEnvironmentStoreTest.kt @@ -36,9 +36,21 @@ class GameEnvironmentStoreTest { context.getSharedPreferences("session", Context.MODE_PRIVATE).edit() .remove("logs") .remove("gameDebugLogsEnabled") + .remove("gameCpus") .commit() } + @Test fun gameCoresChangedInSettingsReachTheGuestAtOnce() { + val live = File(LinuxRuntime.rootDir(context), "root/.config/droiddeck/game-cores") + val all = com.droiddeck.launcher.core.CpuCores.all + + SessionPrefs.setGameCpus(context, all.last().toString()) + assertEquals("${all.last()}\n", live.readText()) + + SessionPrefs.setGameCpus(context, com.droiddeck.launcher.core.CpuCores.format(all.toSet())) + assertEquals("\n", live.readText()) + } + @Test fun gameDebugLogsAreOffByDefault() { assertFalse(SessionPrefs.gameDebugLogsEnabled(context)) diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index 3662eab545..b54e692517 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -227,6 +227,14 @@ dd_components() { DD_GAME_CPUS_SETUP = """ dd_game_cpus() { + local live="${HOME:-/root}/.config/droiddeck/game-cores" cpus + if [ -f "$live" ] && cpus=$(<"$live") 2>/dev/null; then + case $cpus in *[!0-9,-]*) cpus=${DD_GAME_CPUS:-} ;; esac + if [ -z "$cpus" ] && [ -n "${DD_CLIENT_CPUS:-}" ]; then + cpus=$(nproc --all 2>/dev/null) && cpus=0-$((cpus - 1)) || cpus= + fi + if [ -n "$cpus" ]; then export DD_GAME_CPUS=$cpus; else unset DD_GAME_CPUS; fi + fi [ -n "${DD_GAME_CPUS:-}" ] || return 0 command -v taskset >/dev/null 2>&1 || return 0 DD_TASKSET="taskset -c $DD_GAME_CPUS" diff --git a/tools/tests/test_game_cpus_live.py b/tools/tests/test_game_cpus_live.py new file mode 100644 index 0000000000..d7128ca661 --- /dev/null +++ b/tools/tests/test_game_cpus_live.py @@ -0,0 +1,57 @@ +import os +from pathlib import Path +import runpy +import subprocess +import tempfile +import unittest + +BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" +COMPAT = runpy.run_path(str(BIN / "steam-compatibility")) + + +class LiveGameCpusTest(unittest.TestCase): + def setUp(self): + self.temp = tempfile.TemporaryDirectory() + self.addCleanup(self.temp.cleanup) + base = Path(self.temp.name) + self.home = base / "home" + (self.home / ".config/droiddeck").mkdir(parents=True) + tools = base / "tools" + tools.mkdir() + for name, body in (("taskset", "exit 0"), ("nproc", "echo 8")): + tool = tools / name + tool.write_text("#!/bin/sh\n" + body + "\n") + tool.chmod(0o755) + self.path = str(tools) + os.pathsep + os.environ.get("PATH", "") + + def run_setup(self, live=None, **extra): + if live is not None: + (self.home / ".config/droiddeck/game-cores").write_text(live) + script = COMPAT["DD_GAME_CPUS_SETUP"] + '\ndd_game_cpus\necho "taskset=${DD_TASKSET:-}"\necho "cpus=${DD_GAME_CPUS-unset}"\n' + env = {key: value for key, value in os.environ.items() if not key.startswith("DD_")} + env.update(HOME=str(self.home), PATH=self.path, **extra) + result = subprocess.run(["bash", "-c", script], env=env, capture_output=True, text=True, check=True) + return result.stdout.splitlines() + + def test_a_change_made_during_the_session_wins_over_the_session_value(self): + self.assertEqual(self.run_setup("6,7\n", DD_GAME_CPUS="3,4,5,6,7"), + ["taskset=taskset -c 6,7", "cpus=6,7"]) + + def test_every_core_chosen_during_the_session_drops_the_session_pin(self): + self.assertEqual(self.run_setup("\n", DD_GAME_CPUS="3,4,5,6,7"), ["taskset=", "cpus=unset"]) + + def test_every_core_with_a_pinned_client_still_frees_the_game_from_the_client_cores(self): + self.assertEqual(self.run_setup("\n", DD_GAME_CPUS="0-7", DD_CLIENT_CPUS="0"), + ["taskset=taskset -c 0-7", "cpus=0-7"]) + + def test_without_the_live_file_the_session_value_is_used(self): + self.assertEqual(self.run_setup(DD_GAME_CPUS="3,4,5,6,7"), + ["taskset=taskset -c 3,4,5,6,7", "cpus=3,4,5,6,7"]) + self.assertEqual(self.run_setup(), ["taskset=", "cpus=unset"]) + + def test_an_unreadable_value_keeps_the_session_value(self): + self.assertEqual(self.run_setup("3;rm\n", DD_GAME_CPUS="4,5"), ["taskset=taskset -c 4,5", "cpus=4,5"]) + + +if __name__ == "__main__": + unittest.main()