Keep secondary-library prefixes and Steam tools on private storage

This commit is contained in:
xXJsonDeruloXx committed 2026-10-01 16:11:19 -04:00
1 parent 647ab46659
commit 68ce64dea6
5 files changed
+188 -8

No files matched your search

@@ -5,6 +5,7 @@ import com.droiddeck.launcher.R
import com.droiddeck.launcher.runtime.LinuxRuntime
import com.droiddeck.launcher.session.GameStorage
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SecondaryLibrary
import java.io.File
/**
@@ -84,7 +85,11 @@ object Library {
/** Proton keeps each game's prefix below compatdata/<appid>/pfx in a Steam library. */
fun protonPrefix(context: Context, appId: Long, preferredLibrary: File? = null): File? {
val ids = listOf(appId.toString(), java.lang.Integer.toString(appId.toInt())).distinct()
val roots = (listOfNotNull(preferredLibrary) + steamLibraries(context).map { it.first })
val secondary = GameStorage.effective(context)?.let { File(it.path) }
fun prefixRoot(root: File): File = if (secondary != null && root.canonicalPath == secondary.canonicalPath)
SecondaryLibrary.privateRoot(context.filesDir, root).takeIf { File(it, "steamapps/compatdata").isDirectory } ?: root
else root
val roots = (listOfNotNull(preferredLibrary) + steamLibraries(context).map { it.first }).map(::prefixRoot)
.distinctBy { runCatching { it.canonicalPath }.getOrDefault(it.absolutePath) }
return roots.asSequence()
.flatMap { root -> ids.asSequence().map { id -> File(root, "steamapps/compatdata/$id/pfx") } }
@@ -0,0 +1,94 @@
package com.droiddeck.launcher.session
import java.io.File
import java.nio.file.Files
import java.nio.file.LinkOption.NOFOLLOW_LINKS
import java.nio.file.StandardCopyOption.COPY_ATTRIBUTES
import java.security.MessageDigest
import java.util.Locale
/** Android shared storage cannot create the links in Wine prefixes or native Steam depots. */
object SecondaryLibrary {
private val bootstrapTools = linkedMapOf(
"4185400" to "SteamLinuxRuntime_4-arm64",
"4427310" to "Proton Experimental (ARM64)",
"4628740" to "Proton 11.0 (ARM64)",
)
fun privateRoot(files: File, library: File): File {
val digest = MessageDigest.getInstance("SHA-256").digest(library.canonicalPath.toByteArray())
.joinToString("") { "%02x".format(it.toInt() and 255) }
return File(files, "steam-libraries/$digest")
}
/** Parent binds come first; the more specific private directories override them in PRoot. */
fun binds(files: File, library: File): List<String> {
val private = privateRoot(files, library)
val apps = File(library, "steamapps")
val tools = LinkedHashMap(bootstrapTools)
// Include other installed Proton/runtime versions, without routing ordinary game depots.
apps.listFiles()?.filter { it.name.matches(Regex("appmanifest_\\d+\\.acf")) }?.forEach { manifest ->
val text = manifest.readText()
val id = Regex("\"appid\"\\s+\"(\\d+)\"").find(text)?.groupValues?.get(1)
val dir = Regex("\"installdir\"\\s+\"([^\"]+)\"").find(text)?.groupValues?.get(1)
if (id != null && dir != null && File(dir).name == dir &&
(dir.startsWith("Proton ") || dir.startsWith("SteamLinuxRuntime"))) tools[id] = dir
}
val overrides = ArrayList<Pair<File, String>>()
for (name in listOf("compatdata", "shadercache")) {
val target = File(private, "steamapps/$name")
migrate(File(apps, name), target)
overrides.add(target to "steamapps/$name")
}
for ((id, dir) in tools) {
for (relative in listOf("steamapps/common/$dir", "steamapps/downloading/$id")) {
val target = File(private, relative)
migrate(File(library, relative), target)
// FUSE discarded the executable bits. Steam will restore the depot's exact modes
// on verification; its launchers must already be executable when importing it.
target.walkTopDown().filter { it.isFile }.forEach { it.setExecutable(true, true) }
overrides.add(target to relative)
// Steam normalizes depot install directories to lowercase on Android's
// case-insensitive shared storage. PRoot binds are case-sensitive, so both
// spellings must reach the same private directory during the commit.
val lower = relative.lowercase(Locale.ROOT)
if (lower != relative) overrides.add(target to lower)
}
}
return listOf("/mnt/droiddeck-sd", "/mnt/bannerlator-sd").flatMap { guest ->
listOf("${library.path}:$guest") + overrides.map { (host, relative) -> "${host.path}:$guest/$relative" }
}
}
/** Copy once into a staging directory; retain the source and never merge over a live prefix. */
private fun migrate(source: File, target: File) {
val migrated = File(target.parentFile, ".${target.name}.migrated")
if (target.isDirectory) {
if (!migrated.isFile) migrated.writeText(source.canonicalPath)
return
}
check(target.parentFile!!.isDirectory || target.parentFile!!.mkdirs()) { "cannot create ${target.parent}" }
if (!source.isDirectory || migrated.isFile) {
check(target.mkdirs()) { "cannot create $target" }
if (!migrated.isFile) migrated.writeText(source.canonicalPath)
return
}
val stage = File(target.parentFile, "${target.name}.migrating")
check(!stage.exists() || stage.deleteRecursively()) { "cannot clear $stage" }
check(stage.mkdirs()) { "cannot create $stage" }
try {
Files.walk(source.toPath()).use { paths ->
paths.forEach { path ->
val destination = stage.toPath().resolve(source.toPath().relativize(path))
if (Files.isDirectory(path, NOFOLLOW_LINKS)) Files.createDirectories(destination)
else Files.copy(path, destination, NOFOLLOW_LINKS, COPY_ATTRIBUTES)
}
}
check(stage.renameTo(target)) { "cannot finish migration to $target" }
migrated.writeText(source.canonicalPath)
} catch (e: Exception) {
stage.deleteRecursively()
throw e
}
}
}
@@ -736,11 +736,14 @@ class SessionService : Service() {
val problem = GameStorage.prepare(library.path)
if (problem == null) {
File(LinuxRuntime.rootDir(this), "mnt/droiddeck-sd").mkdirs()
binds.add("${library.path}:/mnt/droiddeck-sd")
// Links, prefixes and Steam entries made before the rename still name the old path.
File(LinuxRuntime.rootDir(this), "mnt/bannerlator-sd").mkdirs()
binds.add("${library.path}:/mnt/bannerlator-sd")
Log.i(TAG, "game storage: ${library.path} -> /mnt/droiddeck-sd (\"${library.label}\")")
try {
binds.addAll(SecondaryLibrary.binds(filesDir, File(library.path)))
Log.i(TAG, "game storage: ${library.path} -> /mnt/droiddeck-sd (\"${library.label}\"); prefixes and native tools private")
} catch (e: Exception) {
Log.w(TAG, "game storage: private directories could not be prepared; internal only this session", e)
}
} else {
Log.w(TAG, "game storage: $problem; internal only this session")
}
@@ -0,0 +1,76 @@
package com.droiddeck.launcher.session
import java.io.File
import java.nio.file.Files
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class SecondaryLibraryTest {
@get:Rule val tmp = TemporaryFolder()
@Test fun gamesStaySharedButPrefixesAndNativeDownloadsArePrivate() {
val files = tmp.newFolder("files")
val library = tmp.newFolder("card")
File(library, "steamapps/common/Game").mkdirs()
val binds = SecondaryLibrary.binds(files, library)
val private = SecondaryLibrary.privateRoot(files, library)
for (guest in listOf("/mnt/droiddeck-sd", "/mnt/bannerlator-sd")) {
assertTrue(binds.contains("${library.path}:$guest"))
assertTrue(binds.contains("$private/steamapps/compatdata:$guest/steamapps/compatdata"))
assertTrue(binds.contains("$private/steamapps/downloading/4185400:$guest/steamapps/downloading/4185400"))
assertTrue(binds.contains("$private/steamapps/common/SteamLinuxRuntime_4-arm64:$guest/steamapps/common/steamlinuxruntime_4-arm64"))
assertFalse(binds.any { it.endsWith(":$guest/steamapps/common/Game") })
}
}
@Test fun migrationPreservesSavesLinksAndOriginalAndNeverOverwritesNewSaves() {
val files = tmp.newFolder("files")
val library = tmp.newFolder("card")
val save = File(library, "steamapps/compatdata/42/pfx/save.dat")
save.parentFile!!.mkdirs()
save.writeText("original")
Files.createSymbolicLink(File(save.parentFile, "link").toPath(), File("save.dat").toPath())
SecondaryLibrary.binds(files, library)
val copied = File(SecondaryLibrary.privateRoot(files, library), "steamapps/compatdata/42/pfx/save.dat")
assertEquals("original", copied.readText())
assertTrue(Files.isSymbolicLink(File(copied.parentFile, "link").toPath()))
copied.writeText("new save")
SecondaryLibrary.binds(files, library)
assertEquals("new save", copied.readText())
assertEquals("original", save.readText())
}
@Test fun changingLibraryUsesADifferentPrivatePrefixDirectory() {
val files = tmp.newFolder("files")
assertNotEquals(SecondaryLibrary.privateRoot(files, tmp.newFolder("one")),
SecondaryLibrary.privateRoot(files, tmp.newFolder("two")))
}
@Test fun installedNativeToolVersionsAlsoGetPrivateStaging() {
val files = tmp.newFolder("files")
val library = tmp.newFolder("card")
val manifest = File(library, "steamapps/appmanifest_123.acf")
manifest.parentFile!!.mkdirs()
manifest.writeText("\"AppState\" { \"appid\" \"123\" \"installdir\" \"Proton 99.0 (ARM64)\" }")
assertTrue(SecondaryLibrary.binds(files, library).any {
it.endsWith(":/mnt/droiddeck-sd/steamapps/downloading/123")
})
}
@Test fun deletingAStagedDepotDoesNotRestoreTheOldSharedDownload() {
val files = tmp.newFolder("files")
val library = tmp.newFolder("card")
val stale = File(library, "steamapps/downloading/4185400/stale")
stale.parentFile!!.mkdirs()
stale.writeText("old download")
SecondaryLibrary.binds(files, library)
val target = File(SecondaryLibrary.privateRoot(files, library), "steamapps/downloading/4185400")
assertTrue(File(target, "stale").isFile)
target.deleteRecursively()
SecondaryLibrary.binds(files, library)
assertFalse(File(target, "stale").exists())
assertTrue(stale.isFile)
}
}