feat: add persistent per-game compatibility profiles (#449)

* feat(profiles): add persistent per-game compatibility profiles

* fix(profiles): show Windows component overrides

Include per-game Windows component selections in the resolved profile summary so the session overlay marks those games as custom and shows the selected components.

* fix(session): detect Proton games across runtime paths

* fix(profiles): select the active Gamescope game

* fix(profiles): improve per-game reset feedback

* fix(profiles): align session components with game overrides

* fix(session): prefer selected Steam game during refresh

* fix(session): resume profile following when reopening games

* chore: keep local dev testing outside the repository

* test(session): remove local dev package path

* fix(session): preserve profiles when refresh fails

* fix(session): reject empty game profile reset scope

* perf(session): cache drawer inventory by refresh revision

* fix(profiles): align game presets and profile reset behavior

* fix(profiles): preserve originals and normalize shortcut ids

* fix(profiles): keep editor scope stable after rebase

* fix(tests): host Compose tests in both build variants

* fix(profiles): recover interrupted component selection saves

---------

Co-authored-by: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com>
This commit is contained in:
Juan MartíandKurt Himebauch authored and GitHub committed 2026-10-11 17:00:28 -04:00
1 parent 78fcb60fd5
commit 435beb7f19
46 files changed
+3501 -150

No files matched your search

@@ -6,6 +6,7 @@ import android.content.Context
import android.util.Log
import com.droiddeck.launcher.core.Downloader
import com.droiddeck.launcher.core.FileUtils
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.gpu.GpuInfo
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.github.luben.zstd.ZstdInputStream
@@ -79,7 +80,14 @@ object ComponentsManager {
private const val PLUGIN_DATA = "root/homebrew/data/decky-components-manager"
data class Proton(val id: String, val name: String, val dir: File, val guestPath: String, val version: String, val valve: Boolean)
data class Component(val detected: String, val detail: String, val inUse: String, val activeFile: String?, val queued: String?)
data class Component(
val detected: String,
val detail: String,
val inUse: String,
val selected: String,
val activeFile: String?,
val queued: String?,
)
data class Original(val comp: String, val protonVersion: String, val label: String, val size: Long)
data class Package(val file: String, val comp: String, val version: String, val description: String, val size: Long)
data class CatalogItem(val file: String, val comp: String, val release: String, val url: String, val size: Long, val digest: String)
@@ -129,23 +137,121 @@ object ComponentsManager {
// ------------------------------------------------------------------ state
private fun loadState(context: Context): JSONObject =
runCatching { JSONObject(stateFile(context).readText()) }.getOrElse { JSONObject() }.apply {
private fun pendingStateFile(context: Context) = File(dataDir(context), "state.pending.json")
private fun loadState(context: Context): JSONObject {
recoverState(context)
return runCatching { JSONObject(stateFile(context).readText()) }.getOrElse { JSONObject() }.apply {
if (!has("active")) put("active", JSONObject())
if (!has("queued")) put("queued", JSONObject())
if (!has("profiles")) put("profiles", JSONObject())
}
}
private fun writeStateFile(file: File, text: String) {
val tmp = File(file.parentFile, file.name + ".tmp")
try {
writeSynced(tmp, text)
check(tmp.renameTo(file)) { "Could not save component selections: ${file.name}" }
} finally {
tmp.delete()
}
}
private fun writeSynced(file: File, text: String) {
FileOutputStream(file).use {
it.write(text.toByteArray(Charsets.UTF_8))
it.fd.sync()
}
}
private fun recoverState(context: Context) {
val pending = pendingStateFile(context)
if (!pending.exists()) return
val transaction = JSONObject(pending.readText())
val state = transaction.getJSONObject("state")
val expected = transaction.getString("launchSha256")
val published = File(launchDir(context), "desired.tsv")
val staged = File(launchDir(context), "desired.tsv.tmp")
// The launch-table rename is the commit point. Never read stale app selections after it.
if (!staged.exists() && published.isFile && Hashes.sha256(published) == expected) {
writeStateFile(stateFile(context), state.toString(2))
Log.i(TAG, "recovered published component selections")
} else {
Log.i(TAG, "discarded unpublished component selections")
}
check(pending.delete()) { "Could not finish component selection recovery" }
if (staged.exists()) check(staged.delete()) { "Could not remove unpublished component selections" }
}
private fun saveState(context: Context, state: JSONObject) {
val f = stateFile(context)
val tmp = File(f.parentFile, f.name + ".tmp")
tmp.writeText(state.toString(2))
tmp.renameTo(f)
// Every change of choice reaches the launch wrappers at once.
runCatching { syncLaunchState(context, state) }.onFailure { Log.w(TAG, "launch state", it) }
val prepared = prepareLaunchState(context, state)
val dir = launchDir(context)
val tmp = File(dir, "desired.tsv.tmp")
try {
writeSynced(tmp, prepared.text)
writeStateFile(pendingStateFile(context), JSONObject()
.put("state", state)
.put("launchSha256", Hashes.sha256(tmp))
.toString())
check(tmp.renameTo(File(dir, "desired.tsv"))) { "Could not publish component selections" }
writeStateFile(stateFile(context), state.toString(2))
check(pendingStateFile(context).delete()) { "Could not finish saving component selections" }
} finally {
if (!pendingStateFile(context).exists()) tmp.delete()
}
File(dir, "store").listFiles()?.filter { it.isDirectory && it.name !in prepared.keep }
?.forEach { it.deleteRecursively() }
}
private fun JSONObject.sub(key: String): JSONObject = optJSONObject(key) ?: JSONObject().also { put(key, it) }
// Keep launch-time restore rows after the last per-game selection is removed.
private fun rememberProfiledComponent(state: JSONObject, comp: String) {
val profiled = state.optJSONArray("profiledComponents")?.let { values ->
(0 until values.length()).mapNotNull { values.optString(it).takeIf(COMPONENTS::contains) }
}.orEmpty().toMutableSet()
if (profiled.add(comp)) state.put("profiledComponents", JSONArray(profiled.sorted()))
}
fun gameComponents(context: Context, scope: String): Map<String, String> = synchronized(lock) {
val profile = loadState(context).sub("profiles").optJSONObject(scope) ?: return emptyMap()
COMPONENTS.mapNotNull { comp -> profile.optString(comp).takeIf(String::isNotEmpty)?.let { comp to it } }.toMap()
}
fun setGameComponent(context: Context, scope: String, comp: String, file: String?) = synchronized(lock) {
require(scope.matches(Regex("[1-9][0-9]{0,9}")) && scope.toLong() <= 4294967295L)
require(comp in COMPONENTS)
val state = loadState(context)
val profiles = state.sub("profiles")
val profile = profiles.optJSONObject(scope) ?: JSONObject().also { profiles.put(scope, it) }
if (file == null) {
if (profile.optString(comp).isNotEmpty()) rememberProfiledComponent(state, comp)
profile.remove(comp)
if (profile.length() == 0) profiles.remove(scope)
} else {
val name = safeName(file)
val wcp = File(packagesDir(context), name)
require(packageInfo(context, wcp).comp == comp)
for (proton in protons(context)) ensureOriginal(context, proton, comp, state)
rememberProfiledComponent(state, comp)
profile.put(comp, name)
}
saveState(context, state)
}
fun clearGameComponents(context: Context, scope: String) = synchronized(lock) {
require(GameEnvironment.validScope(scope))
val state = loadState(context)
val profiles = state.sub("profiles")
profiles.optJSONObject(scope)?.let { profile ->
COMPONENTS.filter { profile.optString(it).isNotEmpty() }
.forEach { rememberProfiledComponent(state, it) }
}
profiles.remove(scope)
saveState(context, state)
}
/** Takes over the Decky plugin's packages, originals and swap records, once. */
private fun migratePlugin(context: Context) {
val marker = File(dataDir(context), ".plugin-imported")
@@ -220,24 +326,33 @@ object ComponentsManager {
* Is anything running on this Proton right now - a game, or the client's own start-up runs,
* whose Wine processes can outlive them? Swaps wait while it is. Read from the command lines.
*/
fun inUse(context: Context, proton: Proton): Boolean = anyProcess(context, proton) { needle, text -> text.contains(needle + "proton ") || text.contains(needle + "files/bin") }
fun inUse(context: Context, proton: Proton): Boolean =
anyProcess(context, proton) { paths, text -> processUsesProton(paths, text, gameOnly = false) }
/**
* Is a game running on this Proton? Only a launch runs proton with the verb waitforexitandrun
* (Proton's own script stays up until the game exits); the client's start-up runs do not.
*/
fun gameRunning(context: Context, proton: Proton): Boolean = anyProcess(context, proton) { needle, text -> text.contains(needle + "proton waitforexitandrun") }
fun gameRunning(context: Context, proton: Proton): Boolean =
anyProcess(context, proton) { paths, text -> processUsesProton(paths, text, gameOnly = true) }
private fun anyProcess(context: Context, proton: Proton, match: (String, String) -> Boolean): Boolean {
val needles = (listOf(proton.guestPath) + EsyncPacks.distPathsFor(root(context), proton.guestPath))
.map { it.trimEnd('/') + "/" }
internal fun processUsesProton(paths: List<String>, text: String, gameOnly: Boolean): Boolean =
paths.any { path ->
val needle = path.trimEnd('/') + "/"
if (gameOnly) text.contains(needle + "proton waitforexitandrun")
else text.contains(needle + "proton ") || text.contains(needle + "files/bin")
}
private fun anyProcess(context: Context, proton: Proton, match: (List<String>, String) -> Boolean): Boolean {
val guestPaths = listOf(proton.guestPath) + EsyncPacks.distPathsFor(root(context), proton.guestPath)
val paths = (guestPaths + guestPaths.map { host(context, it).absolutePath } + proton.dir.absolutePath).distinct()
val procs = File("/proc").listFiles() ?: return false
for (p in procs) {
if (!p.name.all(Char::isDigit)) continue
val cmd = runCatching { File(p, "cmdline").readBytes() }.getOrNull() ?: continue
if (cmd.isEmpty()) continue
val text = String(cmd).replace('\u0000', ' ')
if (needles.any { match(it, text) }) return true
if (match(paths, text)) return true
}
return false
}
@@ -261,7 +376,7 @@ object ComponentsManager {
private fun compFiles(dir: File, comp: String): List<String> {
if (comp == "fex") return FEX_FILES.filter { File(dir, it).isFile }
val base = File(dir, COMP_DIR.getValue(comp))
return base.walkTopDown().filter { it.isFile }.map { it.relativeTo(dir).path }.sorted().toList()
return base.walkTopDown().filter { it.isFile }.map { it.relativeTo(dir).invariantSeparatorsPath }.sorted().toList()
}
private fun owned(comp: String, rel: String) = comp == "fex" || PE_ARCHES.any { "/$it/" in rel }
@@ -352,7 +467,7 @@ object ComponentsManager {
tar.closeArchiveEntry()
}
}
tmp.renameTo(dest)
check(tmp.renameTo(dest)) { "Could not save original bundle ${dest.name}" }
}
// ------------------------------------------------------------------ originals
@@ -380,8 +495,18 @@ object ComponentsManager {
if (dest.isFile) return
val active = state.sub("active").optJSONObject(p.id)?.optJSONObject(comp)
if (active != null && active.optString("protonVersion") == p.version) return
val published = File(launchDir(context), "desired.tsv")
if (published.isFile) {
val mayHaveOverride = published.useLines { rows ->
rows.any { row ->
val fields = row.split('\t')
fields.size == 7 && fields[0] != "*" && fields[1] == p.dir.name &&
fields[2] == p.version && fields[3] == comp
}
}
check(!mayHaveOverride) { context.getString(R.string.cmgr_original_missing) }
}
val rels = compFiles(p.dir, comp)
if (rels.isEmpty()) return
val current = if (comp == "fex") fexVersion(p.dir) else readVersionFile(File(p.dir, "${COMP_DIR.getValue(comp)}/version"))
val profile = JSONObject()
.put("type", TYPE_OF.getValue(comp))
@@ -400,7 +525,7 @@ object ComponentsManager {
for (p in protons(context)) for (comp in COMPONENTS) {
runCatching { ensureOriginal(context, p, comp, state) }.onFailure { Log.w(TAG, "original $comp of ${p.name}", it) }
}
runCatching { syncLaunchState(context, state) }.onFailure { Log.w(TAG, "launch state", it) }
runCatching { saveState(context, state) }.onFailure { Log.w(TAG, "launch state", it) }
}
// ------------------------------------------------------------------ view
@@ -431,9 +556,14 @@ object ComponentsManager {
else context.getString(R.string.cmgr_changed_outside)
}
}
val selectedLabel = when {
active == null || active.optString("protonVersion") != p.version ->
context.getString(R.string.comp_tag_original)
else -> originalLabel(context, active) ?: active.optString("label", active.optString("file"))
}
val q = state.sub("queued").optJSONObject(p.id)?.optJSONObject(comp)
Component(
detected.ifEmpty { context.getString(R.string.cmgr_not_present) }, detail, inUseLabel,
detected.ifEmpty { context.getString(R.string.cmgr_not_present) }, detail, inUseLabel, selectedLabel,
active?.optString("file")?.takeIf { active.optString("protonVersion") == p.version },
q?.let { originalLabel(context, it) ?: it.optString("label") },
)
@@ -455,64 +585,111 @@ object ComponentsManager {
// ------------------------------------------------------------------ launch-time enforcement
private data class PreparedLaunchState(val text: String, val keep: Set<String>)
/**
* Writes what each Proton should be using for the launch wrappers: desired.tsv, one line per
* file (tool dir, Proton build, component, file, sha256, source copy), and an unpacked copy of
* every package or older-build original in use to copy from. Rebuilt whole after every change, so
* a restored or deleted choice simply stops being enforced. The Proton's own current-build
* originals are not listed: the wrappers also give Steam's probe runs their own prefix, which is
* what overwrote files in the first place.
* Stages what each Proton should use for the launch wrappers. General rows use scope `*`;
* per-game rows use the game's compatdata id and replace General for that component. Deletion
* rows remove managed DLLs absent from the chosen package, so switching games cannot leave files
* from the previous game's component set behind.
*/
private fun syncLaunchState(context: Context, state: JSONObject) {
private fun prepareLaunchState(context: Context, state: JSONObject): PreparedLaunchState {
val dir = launchDir(context)
val store = File(dir, "store").apply { mkdirs() }
val all = protons(context)
val lines = StringBuilder()
val keep = HashSet<String>()
val active = state.sub("active")
for (pid in active.keys()) {
val p = all.firstOrNull { it.id == pid } ?: continue
val comps = active.optJSONObject(pid) ?: continue
for (comp in comps.keys()) {
val a = comps.optJSONObject(comp) ?: continue
if (a.optString("protonVersion") != p.version) continue
val file = a.optString("file")
val (key, wcp) = if (file.startsWith("original ")) {
val v = file.removePrefix("original ")
"original-$pid-${safeName(v)}-$comp" to File(originalsDir(context), "$pid/${safeName(v)}/$comp.wcp")
} else safeName(file.removeSuffix(".wcp")) to File(packagesDir(context), file)
if (!wcp.isFile) continue
val unpacked = File(store, key)
if (!File(unpacked, ".complete").isFile) {
unpacked.deleteRecursively()
readWcp(wcp) { tar ->
while (true) {
val e = tar.nextTarEntry ?: break
val rel = normalize(e.name)
if (e.isDirectory || !rel.startsWith("files/") || rel.split('/').any { it == ".." }) continue
val out = File(unpacked, rel)
out.parentFile?.mkdirs()
FileOutputStream(out).use { tar.copyTo(it) }
}
fun unpack(key: String, wcp: File, comp: String): File {
check(wcp.isFile) { context.getString(R.string.cmgr_not_stored) }
val unpacked = File(store, key)
if (!File(unpacked, ".complete").isFile) {
check(packageInfo(context, wcp).comp == comp) { "Wrong component in ${wcp.name}" }
unpacked.deleteRecursively()
check(unpacked.mkdirs()) { "Could not stage component bundle ${wcp.name}" }
readWcp(wcp) { tar ->
while (true) {
val e = tar.nextTarEntry ?: break
val rel = normalize(e.name)
if (e.isDirectory || !rel.startsWith("files/") || rel.split('/').any { it == ".." }) continue
val out = File(unpacked, rel)
out.parentFile?.mkdirs()
FileOutputStream(out).use { tar.copyTo(it) }
}
File(unpacked, ".complete").writeText("1\n")
}
keep += key
val files = a.optJSONObject("files") ?: continue
for (rel in files.keys()) {
val src = File(unpacked, rel)
if (!src.isFile) continue
val guestSrc = "/" + src.absolutePath.removePrefix(root(context).absolutePath.trimEnd('/')).trimStart('/')
lines.append(p.dir.name).append('\t').append(p.version).append('\t').append(comp).append('\t')
.append(rel).append('\t').append(files.optString(rel)).append('\t').append(guestSrc).append('\n')
}
File(unpacked, ".complete").writeText("1\n")
}
keep += key
return unpacked
}
fun managed(comp: String): Set<String> = if (comp == "fex") FEX_FILES.toSet() else buildSet {
for (arch in PE_ARCHES) for (dll in COMP_DLLS.getValue(comp)) add("${COMP_DIR.getValue(comp)}/$arch/$dll")
}
fun append(scope: String, p: Proton, comp: String, key: String, wcp: File) {
val unpacked = unpack(key, wcp, comp)
val files = unpacked.let { base ->
File(base, "files").walkTopDown().filter { it.isFile }.map { it.relativeTo(base).invariantSeparatorsPath }
.filter { owned(comp, it) }.associateWith { File(base, it) }
}
for (rel in managed(comp) - files.keys) {
lines.append(scope).append('\t').append(p.dir.name).append('\t').append(p.version).append('\t').append(comp)
.append('\t').append(rel).append("\t-\t-\n")
}
for ((rel, src) in files) {
val guestSrc = "/" + src.relativeTo(root(context)).invariantSeparatorsPath
lines.append(scope).append('\t').append(p.dir.name).append('\t').append(p.version).append('\t').append(comp)
.append('\t').append(rel).append('\t').append(Hashes.sha256(src)).append('\t').append(guestSrc).append('\n')
}
}
store.listFiles()?.filter { it.isDirectory && it.name !in keep }?.forEach { it.deleteRecursively() }
val list = File(dir, "desired.tsv")
val tmp = File(dir, "desired.tsv.tmp")
tmp.writeText(lines.toString())
tmp.renameTo(list)
fun choice(p: Proton, comp: String, entry: JSONObject): Pair<String, File>? {
val file = entry.optString("file")
return if (file.startsWith("original ")) {
val version = safeName(file.removePrefix("original "))
"original-${p.id}-$version-$comp" to File(originalsDir(context), "${p.id}/$version/$comp.wcp")
} else {
safeName(file.removeSuffix(".wcp")) to File(packagesDir(context), file)
}.takeIf { it.second.isFile }
}
val profiles = state.sub("profiles")
val profiledComponents = buildSet {
state.optJSONArray("profiledComponents")?.let { stored ->
for (index in 0 until stored.length()) {
stored.optString(index).takeIf(COMPONENTS::contains)?.let(::add)
}
}
for (scope in profiles.keys()) {
val profile = profiles.optJSONObject(scope) ?: continue
COMPONENTS.filterTo(this) { profile.optString(it).isNotEmpty() }
}
}
val active = state.sub("active")
for (p in all) for (comp in COMPONENTS) {
val entry = active.optJSONObject(p.id)?.optJSONObject(comp)
val selected = entry?.takeIf { it.optString("protonVersion") == p.version }?.let { choice(p, comp, it) }
if (selected != null) append("*", p, comp, selected.first, selected.second)
else if (comp in profiledComponents) {
ensureOriginal(context, p, comp, state)
val original = originalFile(context, p, comp)
check(original.isFile) { context.getString(R.string.cmgr_original_missing) }
append("*", p, comp, "original-${p.id}-${safeName(p.version)}-$comp", original)
}
}
for (scope in profiles.keys()) {
val profile = profiles.optJSONObject(scope) ?: continue
for (comp in COMPONENTS) {
val file = profile.optString(comp)
if (file.isEmpty()) continue
val wcp = File(packagesDir(context), file)
if (!wcp.isFile) continue
val key = safeName(file.removeSuffix(".wcp"))
for (p in all) append(scope, p, comp, key, wcp)
}
}
return PreparedLaunchState(lines.toString(), keep)
}
// ------------------------------------------------------------------ actions
@@ -615,6 +792,11 @@ object ComponentsManager {
val comps = active.optJSONObject(pid) ?: continue
for (comp in comps.keys()) check(comps.optJSONObject(comp)?.optString("file") != name) { context.getString(R.string.cmgr_package_in_use, name, pid) }
}
val profiles = state.sub("profiles")
for (scope in profiles.keys()) {
val profile = profiles.optJSONObject(scope) ?: continue
check(COMPONENTS.none { profile.optString(it) == name }) { context.getString(R.string.cmgr_package_in_use, name, scope) }
}
val f = File(packagesDir(context), name)
check(f.isFile) { context.getString(R.string.cmgr_package_not_stored, name) }
f.delete()
@@ -13,6 +13,7 @@ object GameEnvironmentStore {
private const val GUEST_FILE = "root/.config/droiddeck/game-environment.json"
/** The published file's extra DXVK options (droiddeck-game-env reads it; the app's own file has none). */
const val DXVK_CONFIG = "dxvkConfig"
const val DXVK_CONFIG_GAMES = "dxvkConfigGames"
@Synchronized
fun read(context: Context): GameEnvironment.Config {
@@ -57,6 +58,13 @@ object GameEnvironmentStore {
publish(context, config)
}
@Synchronized
fun setGameFexPreset(context: Context, scope: String, preset: String?): GameEnvironment.Config {
val config = GameEnvironment.withGameFexPreset(read(context), scope, preset)
save(context, config)
return config
}
@Synchronized
fun publish(context: Context, config: GameEnvironment.Config = read(context)) {
val resolved = config.copy(shared = GameEnvironment.defaults(SessionPrefs.fexPreset(context), SessionPrefs.forceSsbs(context)) + config.shared)
@@ -68,6 +76,21 @@ object GameEnvironmentStore {
TextureFiltering.autoLodBias(SessionState.upscaleRatio),
)
if (dxvk.isNotEmpty()) json.put(DXVK_CONFIG, dxvk)
val autoBias = TextureFiltering.autoLodBias(SessionState.upscaleRatio)
val generalAnisotropy = SessionPrefs.textureAnisotropy(context)
val generalLodBias = SessionPrefs.textureLodBias(context)
val gameDxvk = JSONObject()
SessionPrefs.gameTextureFiltering(context).forEach { (scope, choice) ->
gameDxvk.put(
scope,
TextureFiltering.dxvkOptions(
choice.anisotropy ?: generalAnisotropy,
choice.lodBias ?: generalLodBias,
autoBias,
),
)
}
if (gameDxvk.length() > 0) json.put(DXVK_CONFIG_GAMES, gameDxvk)
write(File(LinuxRuntime.rootDir(context), GUEST_FILE), json.toString())
}
@@ -0,0 +1,14 @@
package com.droiddeck.launcher.session
internal class GameProfileFollowState {
var followsSteam = true
private set
fun selectManually() {
followsSteam = false
}
fun resumeFollowingSteam() {
followsSteam = true
}
}
@@ -0,0 +1,16 @@
package com.droiddeck.launcher.session
internal class GameProfileInventoryCache<T : Any> {
private val lock = Any()
private var revision: Long? = null
private var value: List<T>? = null
// Called only by the refresh worker; the UI invalidates through request revisions.
fun get(requestedRevision: Long, load: () -> List<T>): List<T> = synchronized(lock) {
value?.takeIf { revision == requestedRevision } ?: load().toList().also {
value = it
revision = requestedRevision
}
}
}
@@ -0,0 +1,31 @@
package com.droiddeck.launcher.session
import android.content.Context
import com.droiddeck.launcher.core.GameEnvironment
object GameProfileManager {
@Synchronized
fun reset(context: Context, scope: String) {
require(scope.isNotEmpty() && GameEnvironment.validScope(scope))
val failures = mutableListOf<Pair<String, Throwable>>()
fun clear(name: String, action: () -> Unit) {
runCatching(action).onFailure { failures += name to it }
}
clear("environment") {
GameEnvironmentStore.save(
context,
GameEnvironmentStore.read(context).withEntries(scope, emptyMap()),
)
}
clear("Proton") { ProtonDefault.requestGame(context, scope, null) }
clear("component packages") { ComponentsManager.clearGameComponents(context, scope) }
clear("texture filtering") { SessionPrefs.clearGameTextureFiltering(context, scope) }
clear("Windows components") { WinComponents.setPicks(context, scope, emptyList()) }
if (failures.isNotEmpty()) {
val names = failures.joinToString { it.first }
throw IllegalStateException("Could not clear: $names", failures.first().second)
}
}
}
@@ -0,0 +1,13 @@
package com.droiddeck.launcher.session
internal data class GameProfileRefreshState<T>(
val data: T? = null,
val loaded: Boolean = false,
val failed: Boolean = false,
) {
fun succeeded(data: T) = GameProfileRefreshState(data, loaded = true)
fun failed() = copy(failed = true)
fun retrying() = copy(failed = false)
}
@@ -0,0 +1,33 @@
package com.droiddeck.launcher.session
internal class LatestRequestCoordinator<T> {
data class Request<T>(val generation: Long, val value: T)
private val lock = Any()
private var generation = 0L
private var running = false
private var latest: Request<T>? = null
private var pending: Request<T>? = null
fun submit(value: T): Boolean = synchronized(lock) {
if (latest?.value != value) generation++
pending = Request(generation, value).also { latest = it }
if (running) {
false
} else {
running = true
true
}
}
fun takeLatest(): Request<T>? = synchronized(lock) {
pending.also {
pending = null
if (it == null) running = false
}
}
fun isLatest(request: Request<T>): Boolean = synchronized(lock) {
request.generation == latest?.generation
}
}
@@ -2,6 +2,7 @@ package com.droiddeck.launcher.session
import android.content.Context
import android.util.Log
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONObject
import java.io.File
@@ -9,10 +10,12 @@ import java.io.File
object ProtonDefault {
private const val TAG = "ProtonDefault"
private const val DIR = "root/.local/share/droiddeck-compat"
private const val GAMES = "games.json"
data class Tool(val name: String, val display: String, val dir: String, val valve: Boolean)
data class Request(val seq: Long, val valve: Boolean, val dir: String)
data class State(val default: String?, val applied: Long, val live: Boolean, val pending: String?, val tools: List<Tool>)
data class GameChoice(val valve: Boolean, val dir: String)
enum class Outcome { LIVE, NEXT_START, NOT_RUNNABLE }
@@ -60,6 +63,33 @@ object ProtonDefault {
return protons.firstOrNull { it.valve == pick.first && it.dir.name == pick.second }?.id
}
internal fun parseGames(text: String?): Map<String, GameChoice?> {
val root = runCatching { JSONObject(text ?: "") }.getOrNull() ?: return emptyMap()
if (root.optInt("version", 0) != 1) return emptyMap()
val games = root.optJSONObject("games") ?: return emptyMap()
return buildMap {
games.keys().forEach { id ->
if (!GameEnvironment.validScope(id)) return@forEach
if (games.isNull(id)) {
put(id, null)
} else {
val value = games.optJSONObject(id) ?: return@forEach
val gameDir = value.optString("dir")
val valve = value.opt("valve")
if (gameDir.isNotEmpty() && valve is Boolean) put(id, GameChoice(valve, gameDir))
}
}
}
}
fun gameChoice(context: Context, scope: String): GameChoice? =
parseGames(read(File(dir(context), GAMES)))[scope]
fun gameSelectedId(context: Context, scope: String, protons: List<ComponentsManager.Proton>): String? {
val pick = gameChoice(context, scope) ?: return null
return protons.firstOrNull { it.valve == pick.valve && it.dir.name == pick.dir }?.id
}
internal fun nextSeq(now: Long, state: State, previous: Request?): Long =
maxOf(now, state.applied + 1, (previous?.seq ?: 0) + 1)
@@ -83,4 +113,33 @@ object ProtonDefault {
val tool = state.tools.firstOrNull { it.valve == proton.valve && it.dir == proton.dir.name }
return if (SessionState.running && state.live && (tool == null || state.pending != tool.name)) Outcome.LIVE else Outcome.NEXT_START
}
@Synchronized
fun requestGame(context: Context, scope: String, proton: ComponentsManager.Proton?): Outcome {
require(GameEnvironment.validScope(scope))
val d = dir(context)
val choices = parseGames(read(File(d, GAMES))).toMutableMap()
choices[scope] = proton?.let { GameChoice(it.valve, it.dir.name) }
val games = JSONObject()
choices.forEach { (id, choice) ->
games.put(id, choice?.let { JSONObject().put("valve", it.valve).put("dir", it.dir) } ?: JSONObject.NULL)
}
val staged = File(d, "$GAMES.staged")
try {
d.mkdirs()
staged.writeText(JSONObject().put("version", 1).put("games", games).toString(1) + "\n")
if (!staged.renameTo(File(d, GAMES))) throw java.io.IOException("rename failed")
} catch (e: Exception) {
staged.delete()
Log.w(TAG, "could not write the per-game Proton request", e)
throw e
}
val state = state(context)
val tool = proton?.let { selected -> state.tools.firstOrNull { it.valve == selected.valve && it.dir == selected.dir.name } }
return if (SessionState.running && state.live && (proton == null || tool != null && state.pending != tool.name)) {
Outcome.LIVE
} else {
Outcome.NEXT_START
}
}
}
@@ -0,0 +1,70 @@
package com.droiddeck.launcher.session
import com.droiddeck.launcher.core.FexPreset
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.frontend.Library
data class ProfileValue(val value: String, val inherited: Boolean)
data class SelectedGameProfile(
val appId: Long,
val name: String,
val fexPreset: ProfileValue,
val proton: ProfileValue,
val protonId: String?,
val components: Map<String, ProfileValue>,
val componentFiles: Map<String, String>,
val environmentOverrides: Int,
val windowsComponentNames: List<String>,
) {
val hasOverrides: Boolean
get() = !fexPreset.inherited ||
!proton.inherited ||
components.values.any { !it.inherited } ||
environmentOverrides > 0 ||
windowsComponentNames.isNotEmpty()
companion object {
fun resolve(
selectedAppId: Long,
games: List<Library.SteamGame>,
generalFexPreset: String,
environment: GameEnvironment.Config,
snapshot: ComponentsManager.Snapshot,
generalProtonId: String?,
gameProtonChoice: ProtonDefault.GameChoice?,
gameProtonId: String?,
gameComponents: Map<String, String>,
gameWindowsComponents: List<String>,
): SelectedGameProfile? {
val game = games.firstOrNull { it.profileKey == selectedAppId.toString() } ?: return null
val fexOverride = GameEnvironment.gameFexPreset(environment, game.profileKey)
val selectedProtonId = if (gameProtonChoice == null) generalProtonId else gameProtonId
val selectedProton = snapshot.protons.firstOrNull { it.proton.id == selectedProtonId }
val packages = snapshot.packages.associateBy { it.file }
val components = ComponentsManager.COMPONENTS.associateWith { component ->
val override = gameComponents[component]
ProfileValue(
value = override?.let { packages[it]?.version ?: it }
?: selectedProton?.components?.get(component)?.selected.orEmpty(),
inherited = override == null,
)
}
val otherEnvironment = environment.games[game.profileKey].orEmpty().keys - FexPreset.environmentKeys
return SelectedGameProfile(
appId = selectedAppId,
name = game.name,
fexPreset = ProfileValue(fexOverride ?: generalFexPreset, fexOverride == null),
proton = ProfileValue(
selectedProton?.proton?.name ?: gameProtonChoice?.dir.orEmpty(),
gameProtonChoice == null,
),
protonId = selectedProtonId,
components = components,
componentFiles = gameComponents,
environmentOverrides = otherEnvironment.size,
windowsComponentNames = gameWindowsComponents.map(WinComponentNames::of),
)
}
}
}
@@ -4,12 +4,15 @@ import android.content.Context
import android.content.pm.ActivityInfo
import androidx.annotation.StringRes
import com.droiddeck.launcher.R
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.core.TextureFiltering
import com.droiddeck.launcher.gpu.ScreenEffects
import org.json.JSONObject
/** The in-session switches: the HUD and how the on-screen controls decide to appear. */
object SessionPrefs {
data class GameTextureFiltering(val anisotropy: Int? = null, val lodBias: String? = null)
const val SUSPEND_AUTO = "auto"
const val SUSPEND_NATIVE = "native"
const val SUSPEND_MANUAL = "manual"
@@ -868,6 +871,74 @@ object SessionPrefs {
publishGameEnvironment(context)
}
fun gameTextureFiltering(context: Context, scope: String): GameTextureFiltering {
require(GameEnvironment.validScope(scope))
return gameTextureFiltering(context)[scope] ?: GameTextureFiltering()
}
fun gameTextureFiltering(context: Context): Map<String, GameTextureFiltering> {
val json = runCatching {
JSONObject(prefs(context).getString("gameTextureFiltering", null) ?: "{}")
}.getOrElse { JSONObject() }
return buildMap {
json.keys().forEach { scope ->
if (!GameEnvironment.validScope(scope)) return@forEach
val entry = json.optJSONObject(scope) ?: return@forEach
val anisotropy = entry.optInt("anisotropy").takeIf {
entry.has("anisotropy") && it in TextureFiltering.ANISOTROPY
}
val lodBias = entry.optString("lodBias").takeIf {
entry.has("lodBias") && it in TextureFiltering.LOD_BIAS
}
if (anisotropy != null || lodBias != null) {
put(scope, GameTextureFiltering(anisotropy, lodBias))
}
}
}
}
fun setGameTextureAnisotropy(context: Context, scope: String, value: Int?) {
require(value == null || value in TextureFiltering.ANISOTROPY)
updateGameTextureFiltering(context, scope) { it.copy(anisotropy = value) }
}
fun setGameTextureLodBias(context: Context, scope: String, choice: String?) {
require(choice == null || choice in TextureFiltering.LOD_BIAS)
updateGameTextureFiltering(context, scope) { it.copy(lodBias = choice) }
}
fun clearGameTextureFiltering(context: Context, scope: String) {
require(GameEnvironment.validScope(scope))
val choices = gameTextureFiltering(context).toMutableMap()
if (choices.remove(scope) == null) return
saveGameTextureFiltering(context, choices)
publishGameEnvironment(context)
}
private fun updateGameTextureFiltering(
context: Context,
scope: String,
change: (GameTextureFiltering) -> GameTextureFiltering,
) {
require(GameEnvironment.validScope(scope))
val choices = gameTextureFiltering(context).toMutableMap()
val next = change(choices[scope] ?: GameTextureFiltering())
if (next.anisotropy == null && next.lodBias == null) choices.remove(scope) else choices[scope] = next
saveGameTextureFiltering(context, choices)
publishGameEnvironment(context)
}
private fun saveGameTextureFiltering(context: Context, choices: Map<String, GameTextureFiltering>) {
val json = JSONObject()
choices.forEach { (id, choice) ->
json.put(id, JSONObject().apply {
choice.anisotropy?.let { put("anisotropy", it) }
choice.lodBias?.let { put("lodBias", it) }
})
}
prefs(context).edit().putString("gameTextureFiltering", json.toString()).apply()
}
/** Hands the change to the next game launch (GameEnvironmentStore); the running game keeps its own. */
private fun publishGameEnvironment(context: Context) {
runCatching { GameEnvironmentStore.publish(context) }