feat: DroidDeck Proton (Auto) assembles a per-game Proton from recipes

This commit is contained in:
xXJsonDeruloXx committed 2026-10-08 15:47:27 -04:00
1 parent 134fa167fe
commit c5d5ac904e
12 files changed
+1148 -32

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
{
"version": 1,
"games": {}
}
@@ -483,20 +483,7 @@ object ComponentsManager {
} 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) }
}
}
File(unpacked, ".complete").writeText("1\n")
}
unpack(wcp, unpacked)
keep += key
val files = a.optJSONObject("files") ?: continue
for (rel in files.keys()) {
@@ -515,6 +502,34 @@ object ComponentsManager {
tmp.renameTo(list)
}
/**
* A package's files/ unpacked into [dir], with `.complete` written last; a directory that already
* has `.complete` is left as it is. The launch wrappers and droiddeck-recipe read only complete ones.
*/
internal fun unpack(wcp: File, dir: File) {
if (File(dir, ".complete").isFile) return
dir.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(dir, rel)
out.parentFile?.mkdirs()
FileOutputStream(out).use { tar.copyTo(it) }
}
}
File(dir, "files").mkdirs()
File(dir, ".complete").writeText("1\n")
}
/** The stored package file for a catalog file name (present or not). */
internal fun packageFile(context: Context, file: String): File = File(packagesDir(context), safeName(file))
/** A stored package's details, or null when it is missing or unreadable. */
internal fun storedPackage(context: Context, file: String): Package? =
packageFile(context, file).takeIf { it.isFile }?.let { runCatching { packageInfo(context, it) }.getOrNull() }
// ------------------------------------------------------------------ actions
/** Applies a package now, or queues it when a game is running on that Proton. Returns what happened. */
@@ -737,7 +752,7 @@ object ComponentsManager {
forcedPackageWanted(active?.optString("file"), active?.optString("protonVersion") == p.version)
}
if (targets.isEmpty()) continue
val missing = missingPackage(context, rule, lookup, fetch)
val missing = missingPackage(context, rule.comp, rule.file, rule.release, lookup, fetch)
if (missing != null) {
lines.add(missing)
continue
@@ -747,18 +762,20 @@ object ComponentsManager {
return lines.joinToString("; ").ifEmpty { null }
}
/** Null when [rule]'s package is stored and readable. Otherwise the log line, after a fetch. */
private fun missingPackage(
/** Null when [file]'s package is stored and readable. Otherwise the log line, after a fetch. */
internal fun missingPackage(
context: Context,
rule: ForcedPackage,
comp: String,
file: String,
release: String,
lookup: (file: String, release: String) -> CatalogItem?,
fetch: (CatalogItem) -> Unit,
): String? {
val wcp = File(packagesDir(context), safeName(rule.file))
val wcp = packageFile(context, file)
if (wcp.isFile && runCatching { packageInfo(context, wcp) }.isSuccess) return null
wcp.delete()
val item = lookup(rule.file, rule.release)
?: return "${rule.file} is not in the Nightlies listing; ${LABEL.getValue(rule.comp)} stays on the Proton's copy"
val item = lookup(file, release)
?: return "$file is not in the Nightlies listing; ${LABEL.getValue(comp)} stays on the Proton's copy"
fetch(item)
return null
}
@@ -0,0 +1,157 @@
package com.droiddeck.launcher.session
import android.content.Context
import android.util.AtomicFile
import com.droiddeck.launcher.core.GameEnvironment
import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONObject
import java.io.File
/**
* The recipes behind "DroidDeck Proton (Auto)": per game, a DXVK, VKD3D-Proton and/or FEX package
* from the Nightlies "-Linux" releases and a few environment variables.
*
* The app keeps the recipes ([OVERRIDE] in its files, else the bundled [ASSET]), makes sure every
* package they name is stored (the same download Components uses), unpacks each into the runtime's
* recipe store, and publishes [GUEST_FILE] with those paths. droiddeck-recipe reads that file when
* the Auto tool launches a game and starts Proton from a tree assembled for it; nothing is swapped
* into any Proton, so every other tool stays as it shipped.
*
* A recipe may only set tuning variables: the same rule droiddeck-recipe applies, kept here so a
* bad file is refused before it is published.
*/
object DdProtonRecipes {
private const val TAG = "DdProtonRecipes"
const val ASSET = "recipes/default.json"
const val OVERRIDE = "recipes.json"
private const val GUEST_FILE = "root/.config/droiddeck/recipes.json"
private const val STORE = "root/.local/share/droiddeck-recipes/store"
private val ENV_PREFIXES = listOf("DXVK_", "VKD3D_", "FEX_", "MESA_", "PROTON_")
private val ENV_EXACT = setOf("mesa_glthread", "WINEDLLOVERRIDES")
private val ENV_DENIED_PARTS = listOf("PATH", "_DIR", "_FILE", "LIBRARY", "ROOTFS", "THUNK", "PRELOAD")
private val DLL_OVERRIDE = Regex("[A-Za-z0-9_.*-]+(?:,[A-Za-z0-9_.*-]+)*=(?:n|b|n,b|b,n|d)?")
data class PackageRef(val comp: String, val file: String, val release: String)
data class Recipe(val id: String, val packages: List<PackageRef>, val env: Map<String, String>, val note: String)
fun envAllowed(name: String, value: String): Boolean {
if (!GameEnvironment.validName(name) || name.startsWith("LD_")) return false
if (ENV_DENIED_PARTS.any { it in name.uppercase() }) return false
if (name !in ENV_EXACT && ENV_PREFIXES.none { name.startsWith(it) }) return false
if (!GameEnvironment.validValue(value)) return false
if (name == "WINEDLLOVERRIDES") return value.split(';').filter { it.isNotEmpty() }.all { DLL_OVERRIDE.matches(it) }
return true
}
/** The recipes in [text]; throws IllegalArgumentException naming the first thing wrong. */
fun parse(text: String): List<Recipe> {
val json = JSONObject(text)
require(json.optInt("version") == 1) { "recipes: version must be 1" }
val games = json.optJSONObject("games") ?: return emptyList()
return games.keys().asSequence().sorted().map { id ->
require(id.isNotEmpty() && GameEnvironment.validScope(id)) { "recipes: $id is not a Steam app or shortcut id" }
val game = games.getJSONObject(id)
val packages = ComponentsManager.COMPONENTS.mapNotNull { comp ->
val ref = game.optJSONObject(comp) ?: return@mapNotNull null
val file = ref.optString("file")
val release = ref.optString("release")
require(file.endsWith(".wcp") && ComponentsManager.safeName(file) == file) { "recipes: $id: $comp file '$file'" }
require(release.isNotEmpty() && ComponentsManager.safeName(release) == release) { "recipes: $id: $comp release '$release'" }
PackageRef(comp, file, release)
}
val env = buildMap {
game.optJSONObject("env")?.let { vars ->
vars.keys().forEach { name ->
val value = vars.get(name)
require(value is String && envAllowed(name, value)) { "recipes: $id: $name is not a variable a recipe may set" }
put(name, value)
}
}
}
Recipe(id, packages, env, game.optString("note").take(200))
}.toList()
}
/** The recipe store directory a package unpacks into. */
internal fun storeKey(file: String) = ComponentsManager.safeName(file.removeSuffix(".wcp"))
/**
* The guest file: each game's components as unpacked store paths. A component whose package is
* not in [available] (file name -> guest path) is left out, so that game runs on the depot's copy.
*/
internal fun publishJson(recipes: List<Recipe>, available: Map<String, String>): JSONObject {
val games = JSONObject()
for (recipe in recipes) {
val game = JSONObject()
for (ref in recipe.packages) available[ref.file]?.let { game.put(ref.comp, it) }
if (recipe.env.isNotEmpty()) game.put("env", JSONObject(recipe.env))
if (recipe.note.isNotEmpty()) game.put("note", recipe.note)
games.put(recipe.id, game)
}
return JSONObject().put("version", 1).put("games", games)
}
private fun source(context: Context): String? {
val override = File(context.filesDir, OVERRIDE)
if (override.isFile) return override.readText()
return runCatching { context.assets.open(ASSET).use { it.readBytes().decodeToString() } }.getOrNull()
}
/**
* Fetches and unpacks what the recipes need, then publishes them; returns a line for the session
* log. A package that cannot be fetched is left out of the published file and named in the line.
*/
fun ensure(context: Context): String = ensure(
context,
{ file, release -> ComponentsManager.catalogItem(context, file, release) },
{ ComponentsManager.download(context, it) {} },
)
internal fun ensure(
context: Context,
lookup: (file: String, release: String) -> ComponentsManager.CatalogItem?,
fetch: (ComponentsManager.CatalogItem) -> Unit,
kind: (file: String) -> String? = { ComponentsManager.storedPackage(context, it)?.comp },
unpack: (file: String, dir: File) -> Unit = { file, dir -> ComponentsManager.unpack(ComponentsManager.packageFile(context, file), dir) },
): String {
val text = source(context)
val recipes = if (text == null) emptyList() else parse(text)
val root = LinuxRuntime.rootDir(context)
val store = File(root, STORE).apply { mkdirs() }
val available = LinkedHashMap<String, String>()
val problems = ArrayList<String>()
for (ref in recipes.flatMap { it.packages }.distinctBy { it.file }) {
val fetched = runCatching { ComponentsManager.missingPackage(context, ref.comp, ref.file, ref.release, lookup, fetch) }
val missing = fetched.getOrElse { "${ref.file}: ${it.message}" }
if (missing != null) { problems += missing; continue }
when (kind(ref.file)) {
ref.comp -> Unit
null -> { problems += "${ref.file} could not be read"; continue }
else -> { problems += "${ref.file} is not a ${ref.comp} package"; continue }
}
val dir = File(store, storeKey(ref.file))
runCatching { unpack(ref.file, dir) }
.onFailure { dir.deleteRecursively(); problems += "${ref.file}: ${it.message}" }
.onSuccess { available[ref.file] = "/" + dir.absolutePath.removePrefix(root.absolutePath.trimEnd('/')).trimStart('/') }
}
val keep = available.keys.map { storeKey(it) }.toSet()
store.listFiles()?.filter { it.isDirectory && it.name !in keep }?.forEach { it.deleteRecursively() }
write(File(root, GUEST_FILE), publishJson(recipes, available).toString())
val line = "$TAG: ${recipes.size} recipe(s), ${available.size} package(s) ready"
return if (problems.isEmpty()) line else "$line; " + problems.joinToString("; ")
}
private fun write(file: File, text: String) {
check(file.parentFile!!.isDirectory || file.parentFile!!.mkdirs())
val atomic = AtomicFile(file)
val output = atomic.startWrite()
try {
output.write(text.toByteArray(Charsets.UTF_8))
atomic.finishWrite(output)
} catch (error: Exception) {
atomic.failWrite(output)
throw error
}
}
}
@@ -36,6 +36,7 @@ object SessionFiles {
"usr/local/bin/droiddeck-wincomponents" to "usr/local/bin/droiddeck-wincomponents",
"usr/local/bin/droiddeck-msi-install" to "usr/local/bin/droiddeck-msi-install",
"usr/local/bin/droiddeck-esync" to "usr/local/bin/droiddeck-esync",
"usr/local/bin/droiddeck-recipe" to "usr/local/bin/droiddeck-recipe",
"usr/local/bin/droiddeck-steam-compat" to "usr/local/bin/droiddeck-steam-compat",
"usr/local/bin/droiddeck-fex" to "usr/local/bin/droiddeck-fex",
"libblsession.so" to "usr/local/lib/libblsession.so",
@@ -343,6 +343,11 @@ class SessionService : Service() {
runCatching { ComponentsManager.ensureForcedPackages(this) }
.onSuccess { if (it != null) Log.i(TAG, it) }
.onFailure { Log.w(TAG, "forced components", it) }
// DroidDeck Proton (Auto)'s recipes: their packages fetched and unpacked, the file
// droiddeck-recipe reads published. Only that tool reads it; a failure leaves it stock.
runCatching { DdProtonRecipes.ensure(this) }
.onSuccess { Log.i(TAG, it) }
.onFailure { Log.w(TAG, "DroidDeck Proton (Auto) recipes", it) }
}
addClientEnvironment(guest, steamHere)
// Where the fast path's description of proot's view goes, once the binds are known.
@@ -0,0 +1,138 @@
package com.droiddeck.launcher.session
import android.content.Context
import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.io.File
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], manifest = Config.NONE)
class DdProtonRecipesTest {
private val dxvk = "dxvk-2.4-linux.wcp"
private val vkd3d = "vkd3d-proton-2.14-linux.wcp"
private lateinit var context: Context
@Before fun setUp() {
context = RuntimeEnvironment.getApplication()
File(context.filesDir, "components").deleteRecursively()
File(context.filesDir, DdProtonRecipes.OVERRIDE).delete()
LinuxRuntime.rootDir(context).deleteRecursively()
}
@Test fun recipesParseIntoPackagesEnvAndNote() {
val recipes = DdProtonRecipes.parse(
"""{"version":1,"games":{"42":{"dxvk":{"file":"$dxvk","release":"Dxvk-Linux"},
"env":{"DXVK_HUD":"version","WINEDLLOVERRIDES":"xinput1_3=n,b;libsentry=d"},"note":"test"},
"4294967295":{"fex":{"file":"fex-2607-linux.wcp","release":"FexCore-Linux"}}}}""",
)
assertEquals(listOf("42", "4294967295"), recipes.map { it.id })
assertEquals(listOf(DdProtonRecipes.PackageRef("dxvk", dxvk, "Dxvk-Linux")), recipes[0].packages)
assertEquals(mapOf("DXVK_HUD" to "version", "WINEDLLOVERRIDES" to "xinput1_3=n,b;libsentry=d"), recipes[0].env)
assertEquals("test", recipes[0].note)
assertEquals("fex", recipes[1].packages.single().comp)
}
@Test fun anythingOutsideTheFormatIsRefused() {
val bad = listOf(
"""{"version":2,"games":{}}""",
"""{"version":1,"games":{"0":{}}}""",
"""{"version":1,"games":{"steam":{}}}""",
"""{"version":1,"games":{"42":{"dxvk":{"file":"../x.wcp","release":"Dxvk-Linux"}}}}""",
"""{"version":1,"games":{"42":{"dxvk":{"file":"dxvk.tar","release":"Dxvk-Linux"}}}}""",
"""{"version":1,"games":{"42":{"dxvk":{"file":"$dxvk","release":""}}}}""",
"""{"version":1,"games":{"42":{"env":{"LD_PRELOAD":"/x.so"}}}}""",
"""{"version":1,"games":{"42":{"env":{"DXVK_HUD":1}}}}""",
)
for (text in bad) {
assertThrows(text, Exception::class.java) { DdProtonRecipes.parse(text) }
}
}
@Test fun onlyTuningVariablesAreAllowed() {
for (name in listOf("DXVK_HUD", "VKD3D_CONFIG", "FEX_TSOENABLED", "MESA_SHADER_CACHE_MAX_SIZE", "PROTON_USE_WINED3D", "mesa_glthread")) {
assertTrue(name, DdProtonRecipes.envAllowed(name, "1"))
}
for (name in listOf("LD_PRELOAD", "LD_LIBRARY_PATH", "WINEDLLPATH", "PATH", "HOME", "PROTON_LOG_DIR", "DXVK_CONFIG_FILE",
"VKD3D_SHADER_CACHE_PATH", "MESA_SHADER_CACHE_DIR", "FEX_ROOTFS", "FEX_THUNKHOSTLIBS", "CUSTOM", "BAD-NAME")) {
assertFalse(name, DdProtonRecipes.envAllowed(name, "1"))
}
assertTrue(DdProtonRecipes.envAllowed("WINEDLLOVERRIDES", "a=n;b=b,n;c=d;d="))
for (value in listOf("../evil.dll=n", "a=native", "a", "a=n;b=x")) {
assertFalse(value, DdProtonRecipes.envAllowed("WINEDLLOVERRIDES", value))
}
assertFalse(DdProtonRecipes.envAllowed("DXVK_HUD", "a\u0000b"))
}
@Test fun thePublishedFileNamesStorePathsAndLeavesOutWhatIsMissing() {
val recipes = DdProtonRecipes.parse(
"""{"version":1,"games":{"42":{"dxvk":{"file":"$dxvk","release":"Dxvk-Linux"},
"vkd3d":{"file":"$vkd3d","release":"Vkd3d-proton-Linux"},"env":{"DXVK_HUD":"version"},"note":"n"}}}""",
)
val json = DdProtonRecipes.publishJson(recipes, mapOf(dxvk to "/root/.local/share/droiddeck-recipes/store/dxvk-2.4-linux"))
assertEquals(1, json.getInt("version"))
val game = json.getJSONObject("games").getJSONObject("42")
assertEquals("/root/.local/share/droiddeck-recipes/store/dxvk-2.4-linux", game.getString("dxvk"))
assertFalse(game.has("vkd3d"))
assertEquals("version", game.getJSONObject("env").getString("DXVK_HUD"))
assertEquals("n", game.getString("note"))
}
@Test fun ensureFetchesUnpacksAndPublishes() {
File(context.filesDir, DdProtonRecipes.OVERRIDE).writeText(
"""{"version":1,"games":{"42":{"dxvk":{"file":"$dxvk","release":"Dxvk-Linux"},
"vkd3d":{"file":"$vkd3d","release":"Vkd3d-proton-Linux"}}}}""",
)
val fetched = mutableListOf<String>()
val line = DdProtonRecipes.ensure(
context,
{ file, release -> if (file == dxvk) item(file, release) else null },
{ fetched += it.file },
kind = { file -> if (file == dxvk) "dxvk" else null },
unpack = ::fakeUnpack,
)
assertEquals(listOf(dxvk), fetched)
assertTrue(line, line.startsWith("DdProtonRecipes: 1 recipe(s), 1 package(s) ready; $vkd3d is not in the Nightlies listing"))
val root = LinuxRuntime.rootDir(context)
val store = File(root, "root/.local/share/droiddeck-recipes/store/dxvk-2.4-linux")
assertTrue(File(store, ".complete").isFile)
assertEquals("d3d11", File(store, "files/lib/wine/dxvk/aarch64-windows/d3d11.dll").readText())
val published = JSONObject(File(root, "root/.config/droiddeck/recipes.json").readText())
val game = published.getJSONObject("games").getJSONObject("42")
assertEquals("/root/.local/share/droiddeck-recipes/store/dxvk-2.4-linux", game.getString("dxvk"))
assertFalse(game.has("vkd3d"))
// A recipe that stops naming a package drops its unpacked copy; a package of the wrong kind is refused.
File(context.filesDir, DdProtonRecipes.OVERRIDE).writeText(
"""{"version":1,"games":{"42":{"vkd3d":{"file":"$dxvk","release":"Dxvk-Linux"}}}}""",
)
val second = DdProtonRecipes.ensure(context, { f, r -> item(f, r) }, {}, kind = { "dxvk" }, unpack = ::fakeUnpack)
assertTrue(second, second.endsWith("$dxvk is not a vkd3d package"))
assertFalse(store.exists())
assertFalse(JSONObject(File(root, "root/.config/droiddeck/recipes.json").readText())
.getJSONObject("games").getJSONObject("42").has("vkd3d"))
}
@Test fun noRecipesPublishesAnEmptyFile() {
val line = DdProtonRecipes.ensure(context, { _, _ -> null }, {}, kind = { null }, unpack = ::fakeUnpack)
assertEquals("DdProtonRecipes: 0 recipe(s), 0 package(s) ready", line)
val published = JSONObject(File(LinuxRuntime.rootDir(context), "root/.config/droiddeck/recipes.json").readText())
assertEquals(0, published.getJSONObject("games").length())
}
private fun item(file: String, release: String) =
ComponentsManager.CatalogItem(file, "dxvk", release, "https://example.invalid/$file", 1, "sha256:00")
/** Stands in for ComponentsManager.unpack: zstd-jni has no host library for JVM tests. */
private fun fakeUnpack(file: String, dir: File) {
File(dir, "files/lib/wine/dxvk/aarch64-windows").mkdirs()
File(dir, "files/lib/wine/dxvk/aarch64-windows/d3d11.dll").writeText("d3d11")
File(dir, ".complete").writeText("1\n")
}
}