mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 04:55:44 +00:00
fix(session): Game cores changed during a session apply to the next launch (#3314)
This commit is contained in:
1 parent
281a64f6c5
commit
5f088283af
5 files changed
+137
-2
No files matched your search
@@ -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"
|
||||
@@ -108,10 +109,12 @@ object GameEnvironmentStore {
|
||||
@Synchronized
|
||||
fun publish(
|
||||
context: Context,
|
||||
config: GameEnvironment.Config = read(context),
|
||||
config: GameEnvironment.Config? = null,
|
||||
gameDebugLogsOverride: Boolean? = null,
|
||||
) {
|
||||
val resolved = config.copy(shared = GameEnvironment.defaults(SessionPrefs.fexPreset(context), SessionPrefs.forceSsbs(context)) + config.shared)
|
||||
publishGameCores(context)
|
||||
val stored = config ?: read(context)
|
||||
val resolved = stored.copy(shared = GameEnvironment.defaults(SessionPrefs.fexPreset(context), SessionPrefs.forceSsbs(context)) + stored.shared)
|
||||
val json = encode(resolved)
|
||||
val dxvk = TextureFiltering.dxvkOptions(
|
||||
SessionPrefs.textureAnisotropy(context), SessionPrefs.textureLodBias(context),
|
||||
@@ -140,6 +143,26 @@ object GameEnvironmentStore {
|
||||
write(File(LinuxRuntime.rootDir(context), GUEST_FILE), json.toString())
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun publishGameCores(context: Context) {
|
||||
val file = File(LinuxRuntime.rootDir(context), GAME_CORES_FILE)
|
||||
val pending = File(file.path + ".new")
|
||||
try {
|
||||
check(file.parentFile!!.isDirectory || file.parentFile!!.mkdirs())
|
||||
java.io.FileOutputStream(pending).use {
|
||||
it.write((com.droiddeck.launcher.core.CpuCores.restrictionOrEmpty(SessionPrefs.gameCpus(context)) + "\n").toByteArray(Charsets.UTF_8))
|
||||
it.fd.sync()
|
||||
}
|
||||
java.nio.file.Files.move(
|
||||
pending.toPath(), file.toPath(),
|
||||
java.nio.file.StandardCopyOption.ATOMIC_MOVE, java.nio.file.StandardCopyOption.REPLACE_EXISTING,
|
||||
)
|
||||
} catch (error: Exception) {
|
||||
Log.e(TAG, "Could not update game cores", error)
|
||||
if (!file.delete() && file.exists()) Log.e(TAG, "Could not remove outdated game cores")
|
||||
}
|
||||
}
|
||||
|
||||
private fun write(file: File, text: String) {
|
||||
check(file.parentFile!!.isDirectory || file.parentFile!!.mkdirs())
|
||||
val atomic = AtomicFile(file)
|
||||
|
||||
@@ -1008,6 +1008,7 @@ object SessionPrefs {
|
||||
|
||||
fun setGameCpus(context: Context, list: String) {
|
||||
prefs(context).edit().putString("gameCpus", list).apply()
|
||||
GameEnvironmentStore.publishGameCores(context)
|
||||
}
|
||||
|
||||
fun noXalia(context: Context): Boolean = prefs(context).getBoolean("noXalia", true)
|
||||
|
||||
@@ -36,7 +36,43 @@ class GameEnvironmentStoreTest {
|
||||
context.getSharedPreferences("session", Context.MODE_PRIVATE).edit()
|
||||
.remove("logs")
|
||||
.remove("gameDebugLogsEnabled")
|
||||
.remove("gameCpus")
|
||||
.commit()
|
||||
listOf("game-environment.json.new", "game-cores.new").forEach { File(guest.parentFile, it).delete() }
|
||||
}
|
||||
|
||||
@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())
|
||||
}
|
||||
|
||||
@Config(sdk = [34])
|
||||
@Test fun gameCoresStillReachTheGuestWhenTheRestOfTheEnvironmentCannotBeWritten() {
|
||||
val dir = File(LinuxRuntime.rootDir(context), "root/.config/droiddeck")
|
||||
val last = com.droiddeck.launcher.core.CpuCores.all.last()
|
||||
assertTrue(File(dir, "game-environment.json.new").mkdirs())
|
||||
|
||||
context.getSharedPreferences("session", Context.MODE_PRIVATE).edit().putString("gameCpus", last.toString()).commit()
|
||||
|
||||
assertTrue(runCatching { GameEnvironmentStore.publish(context) }.isFailure)
|
||||
assertEquals("$last\n", File(dir, "game-cores").readText())
|
||||
}
|
||||
|
||||
@Test fun gameCoresThatCannotBeWrittenLeaveNoOlderValueForTheGuest() {
|
||||
val dir = File(LinuxRuntime.rootDir(context), "root/.config/droiddeck")
|
||||
val all = com.droiddeck.launcher.core.CpuCores.all
|
||||
SessionPrefs.setGameCpus(context, all.last().toString())
|
||||
assertTrue(File(dir, "game-cores.new").mkdirs())
|
||||
|
||||
SessionPrefs.setGameCpus(context, all.first().toString())
|
||||
|
||||
assertFalse(File(dir, "game-cores").exists())
|
||||
}
|
||||
|
||||
@Test fun gameDebugLogsAreOffByDefault() {
|
||||
|
||||
@@ -227,6 +227,14 @@ dd_components() {
|
||||
|
||||
DD_GAME_CPUS_SETUP = """
|
||||
dd_game_cpus() {
|
||||
local live=/root/.config/droiddeck/game-cores cpus rest=
|
||||
if [ -f "$live" ] && { IFS= read -r cpus && ! IFS= read -r rest; } <"$live" 2>/dev/null && [ -z "$rest" ] && [ "$(wc -c <"$live" 2>/dev/null)" = "$((${#cpus} + 1))" ]; then
|
||||
[[ -z $cpus || $cpus =~ ^[0-9]+(-[0-9]+)?(,[0-9]+(-[0-9]+)?)*$ ]] || cpus=${DD_GAME_CPUS:-}
|
||||
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"
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
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.live = base / "game-cores"
|
||||
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.live.write_bytes(live.encode())
|
||||
setup = COMPAT["DD_GAME_CPUS_SETUP"].replace("/root/.config/droiddeck/game-cores", str(self.live))
|
||||
script = 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(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_a_file_caught_mid_write_keeps_the_session_value(self):
|
||||
for partial in ("", "6,7", "6"):
|
||||
self.assertEqual(self.run_setup(partial, DD_GAME_CPUS="4,5"), ["taskset=taskset -c 4,5", "cpus=4,5"], partial)
|
||||
|
||||
def test_an_unreadable_value_keeps_the_session_value(self):
|
||||
for bad in ("3;rm\n", "6-\n", "-3\n", "3,,4\n", "3,\n", "3\n4\n", "3\n4", "\n3\n", "3\0,4\n", "\0\n"):
|
||||
self.assertEqual(self.run_setup(bad, DD_GAME_CPUS="4,5"), ["taskset=taskset -c 4,5", "cpus=4,5"], bad)
|
||||
|
||||
def test_the_file_is_found_whatever_home_the_client_runs_with(self):
|
||||
self.assertEqual(self.run_setup("6,7\n", DD_GAME_CPUS="3,4,5,6,7", HOME="/root/.droiddeck-x64"),
|
||||
["taskset=taskset -c 6,7", "cpus=6,7"])
|
||||
self.assertIn("live=/root/.config/droiddeck/game-cores ", COMPAT["DD_GAME_CPUS_SETUP"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user