feat: manage several Steam libraries beside internal and the card (#1967)

* feat: manage several Steam libraries beside internal and the card

* fix: serialize Steam library registry edits, refuse folders inside a Games root, refresh games after changes

* fix: rename refreshes games, added x86-64 libraries join redist seeding and direct mapping
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-11-01 17:35:05 -05:00
1 parent c5577478ca
commit e30e722f8b
16 files changed
+358 -9

No files matched your search

@@ -79,6 +79,7 @@ import com.droiddeck.launcher.session.SessionArtifacts
import com.droiddeck.launcher.session.SessionPhase
import com.droiddeck.launcher.session.SessionState
import com.droiddeck.launcher.session.GameStorage
import com.droiddeck.launcher.session.SteamLibraries
import com.droiddeck.launcher.input.SecondScreenDisplay
import com.droiddeck.launcher.input.SecondScreenDisplays
@@ -494,6 +495,20 @@ class FrontEndActivity : ComponentActivity() {
private val pickGameStorage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data)?.let { path -> setGameStorage(path, GameStorage.labelFor(this, path)) }
}
private var steamLibraries by mutableStateOf<List<SteamLibraries.Entry>>(emptyList())
private val pickSteamLibrary = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { r ->
val path = if (r.resultCode == RESULT_OK) InAppFilePicker.pickedPath(r.data) else null
if (path != null) Thread({
val result = SteamLibraries.add(this, path)
ui.post {
steamLibraries = SteamLibraries.list(this)
refresh()
if (result is SteamLibraries.AddResult.Refused) {
android.widget.Toast.makeText(this, getString(R.string.main_storage_not_usable, result.reason), android.widget.Toast.LENGTH_LONG).show()
}
}
}, "steam-library-add").start()
}
private var romsDir by mutableStateOf<String?>(null)
private var steamGames by mutableStateOf<List<Library.SteamGame>>(emptyList())
private var steamGamesX64 by mutableStateOf(false)
@@ -2117,6 +2132,7 @@ class FrontEndActivity : ComponentActivity() {
mic = if (mode == SessionService.MODE_STEAM) mic else null,
gameStorage = if (mode == SessionService.MODE_STEAM) gameStorage else null,
storageOptions = storageOptions,
steamLibraries = steamLibraries,
storageDiagnostics = mode == SessionService.MODE_STEAM && storageDiagnostics,
fexPreset = if (mode == SessionService.MODE_STEAM) fexPreset else null,
forceSsbs = forceSsbs,
@@ -2181,6 +2197,19 @@ class FrontEndActivity : ComponentActivity() {
onPickGameStorageFolder = {
pickGameStorage.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.main_pick_game_storage), gameStorage.ifEmpty { null }))
},
onAddSteamLibrary = {
pickSteamLibrary.launch(InAppFilePicker.buildDirIntent(this, getString(R.string.libraries_pick), null))
},
onRenameSteamLibrary = { id, label ->
SteamLibraries.rename(this, id, label)
steamLibraries = SteamLibraries.list(this)
refresh()
},
onRemoveSteamLibrary = { id ->
SteamLibraries.remove(this, id)
steamLibraries = SteamLibraries.list(this)
refresh()
},
onStorageDiagnostics = { on ->
SessionPrefs.setStorageDiagnosticsEnabled(this, on)
storageDiagnostics = on
@@ -2415,6 +2444,7 @@ class FrontEndActivity : ComponentActivity() {
mic = SessionPrefs.micEnabled(this)
refreshWifiDiscovery()
gameStorage = SessionPrefs.gameStorage(this)
steamLibraries = SteamLibraries.list(this)
storageDiagnostics = SessionPrefs.storageDiagnosticsEnabled(this)
settingsMode = mode
Thread({
@@ -6,6 +6,7 @@ import com.droiddeck.launcher.runtime.LinuxRuntime
import org.json.JSONObject
import com.droiddeck.launcher.session.GameStorage
import com.droiddeck.launcher.session.SessionPrefs
import com.droiddeck.launcher.session.SteamLibraries
import com.droiddeck.launcher.stores.Store
import com.droiddeck.launcher.stores.StoreGameSidecar
import com.droiddeck.launcher.stores.StoreInstallRoot
@@ -69,6 +70,10 @@ object AddedGames {
if (path.startsWith("$roms/")) return "/root/ROMs/" + path.removePrefix("$roms/")
}
StoreInstallRoot.externalGuestPath(context, host)?.let { return it }
for (lib in SteamLibraries.list(context)) {
if (path == lib.path) return lib.guest
if (path.startsWith("${lib.path}/")) return lib.guest + "/" + path.removePrefix("${lib.path}/")
}
GameStorage.effective(context)?.let { lib ->
if (path.startsWith("${lib.path}/")) return "$LIBRARY/" + path.removePrefix("${lib.path}/")
}
@@ -83,6 +88,7 @@ object AddedGames {
if (!guest.startsWith("/") || guest.split('/').any { it == ".." }) return null
val mapped = roots(context).map { it.host to it.guest } + StoreInstallRoot.externalRoots(context) +
listOfNotNull(GameStorage.effective(context)?.let { File(it.path) to LIBRARY }) +
SteamLibraries.list(context).map { it.host to it.guest } +
(android.os.Environment.getExternalStorageDirectory() to "/root/Storage")
for ((host, root) in mapped.sortedByDescending { it.second.length }) {
if (guest == root) return host
@@ -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.SteamLibraries
import com.droiddeck.launcher.session.SecondaryLibrary
import java.io.File
@@ -88,11 +89,13 @@ object Library {
internal fun steamLibraries(context: Context, arch: String = SessionPrefs.activeSteamClientArch(context)): List<Pair<File, String>> {
val root = SessionPrefs.steamRoot(context, arch)
val card = GameStorage.effective(context)
if (arch == "x86_64") return listOfNotNull(root to "internal", card?.let { File(it.path, GameStorage.X64_CARD_DIR) to it.label })
val added = SteamLibraries.list(context).filter { it.host.isDirectory }
if (arch == "x86_64") return listOfNotNull(root to "internal", card?.let { File(it.path, GameStorage.X64_CARD_DIR) to it.label }) +
added.map { it.x64Host to it.label }
return listOfNotNull(
root to "internal",
card?.let { File(it.path) to it.label },
) + GameStorage.gamesFolderLibraries(context).map { it.host to it.label }
) + added.map { it.host to it.label } + GameStorage.gamesFolderLibraries(context).map { it.host to it.label }
}
fun protonPrefix(
@@ -62,12 +62,14 @@ object GameStorage {
fun gamesFolderLibraries(context: Context): List<Library> {
if (SessionPrefs.gameStorage(context).isNotEmpty()) return emptyList()
val card = effective(context)?.let { canonical(File(it.path)) }
val added = SteamLibraries.list(context).map { canonical(it.host) }
val sm = context.getSystemService(StorageManager::class.java)
return AddedGames.roots(context).mapNotNull { root ->
val host = canonical(root.host)
if (!hasInstalledGames(root.host)) return@mapNotNull null
if ('"' in root.guest || '\\' in root.guest || !root.host.isDirectory || !root.host.canWrite()) return@mapNotNull null
if (card != null && (host == card || host.startsWith("$card/") || card.startsWith("$host/"))) return@mapNotNull null
if (added.any { SteamLibraries.overlaps(it, host) }) return@mapNotNull null
val volume = try { sm?.getStorageVolume(root.host)?.getDescription(context) } catch (e: Exception) { null }
val name = root.host.name.ifEmpty { context.getString(R.string.gstore_folder) }
val label = if (volume.isNullOrBlank()) name else context.getString(R.string.gstore_folder_on_volume, name, volume)
@@ -1422,4 +1422,16 @@ object SessionPrefs {
fun setGameStorage(context: Context, path: String, label: String) {
prefs(context).edit().putString("gameStorage", path).putString("gameStorageLabel", label).apply()
}
fun steamLibraries(context: Context): String = prefs(context).getString("steamLibraries", "") ?: ""
fun setSteamLibraries(context: Context, json: String) {
prefs(context).edit().putString("steamLibraries", json).apply()
}
fun steamLibraryNextId(context: Context): Int = prefs(context).getInt("steamLibraryNextId", 1)
fun setSteamLibraryNextId(context: Context, id: Int) {
prefs(context).edit().putInt("steamLibraryNextId", id).apply()
}
}
@@ -1107,6 +1107,42 @@ class SessionService : Service() {
}
}
}
val addedLibraries = ArrayList<SteamLibraries.Entry>()
val cardPath = storageDiagnosticLibrary?.let { library?.path }?.let { runCatching { File(it).canonicalPath }.getOrDefault(it) }
for (entry in SteamLibraries.list(this)) {
if (cardPath != null && SteamLibraries.overlaps(cardPath, runCatching { entry.host.canonicalPath }.getOrDefault(entry.path))) {
Log.w(TAG, "added steam library: ${entry.path} overlaps the card library; not a library this session")
continue
}
if (!entry.host.isDirectory) {
Log.w(TAG, "added steam library: ${entry.path} is not attached; not a library this session")
continue
}
val problem = GameStorage.prepare(this, entry.path)
if (problem != null) {
Log.w(TAG, "added steam library: $problem")
continue
}
try {
val room = if (ProotFastPath.enabled(this)) {
ProotFastPath.MAX_BINDS - LinuxRuntime.binds(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds).size
} else Int.MAX_VALUE
val content = SecondaryLibrary.contentPath(entry.host)
val extra = SecondaryLibrary.binds(filesDir, entry.host, listOf(entry.guest), content = content)
if (extra.size > room) {
Log.w(TAG, "added steam library: ${entry.path} needs ${extra.size} binds, $room left for the fast path; not a library this session")
continue
}
File(LinuxRuntime.rootDir(this), entry.guest.removePrefix("/")).mkdirs()
binds.addAll(extra)
libraries.add(entry.guest to entry.label)
steamLibraryMounts = steamLibraryMounts + (entry.guest to content)
addedLibraries.add(entry)
Log.i(TAG, "added steam library: ${content.path} -> ${entry.guest} (\"${entry.label}\")")
} catch (e: Exception) {
Log.w(TAG, "added steam library: ${entry.path} private directories could not be prepared", e)
}
}
val x64Libraries = org.json.JSONArray()
val x64Here = SessionPrefs.steamClientArch(this) == "x86_64" ||
(SessionState.mode == MODE_DESKTOP &&
@@ -1132,6 +1168,27 @@ class SessionService : Service() {
Log.w(TAG, "x86-64 steam library: $x64 private directories could not be prepared", e)
}
}
if (x64Here) for (entry in addedLibraries) {
val problem = GameStorage.prepare(this, entry.x64Host.path)
if (problem != null) {
Log.w(TAG, "x86-64 steam library: $problem")
continue
}
try {
val room = if (ProotFastPath.enabled(this)) {
ProotFastPath.MAX_BINDS - LinuxRuntime.binds(this, sessionRoot, runtimeDir, Environment.getExternalStorageDirectory(), binds).size
} else Int.MAX_VALUE
val extra = SecondaryLibrary.binds(filesDir, entry.x64Host, listOf(entry.x64Guest)).drop(1)
if (extra.size > room) {
Log.w(TAG, "x86-64 steam library: ${entry.x64Host} needs ${extra.size} binds, $room left for the fast path; not a library this session")
continue
}
binds.addAll(extra)
x64Libraries.put(org.json.JSONObject().put("path", entry.x64Guest).put("label", entry.label))
} catch (e: Exception) {
Log.w(TAG, "x86-64 steam library: ${entry.x64Host} private directories could not be prepared", e)
}
}
try {
val listing = File(filesDir, "session/steam-libraries-x64.json").apply { parentFile?.mkdirs() }
listing.writeText(x64Libraries.toString())
@@ -0,0 +1,100 @@
package com.droiddeck.launcher.session
import android.content.Context
import android.os.storage.StorageManager
import com.droiddeck.launcher.R
import com.droiddeck.launcher.frontend.AddedGames
import java.io.File
import org.json.JSONArray
import org.json.JSONObject
/** Steam libraries the user added beside the internal one and the card library, each a folder of its own. */
object SteamLibraries {
const val GUEST_ROOT = "/mnt/droiddeck-lib"
// Each library costs a bind per redirected tool and prefix folder; the PRoot fast path holds 256 in all.
const val MAX = 4
private val lock = Any()
class Entry(val id: String, val path: String, val label: String) {
val host: File get() = File(path)
val guest: String get() = "$GUEST_ROOT/$id"
val x64Host: File get() = File(path, GameStorage.X64_CARD_DIR)
val x64Guest: String get() = "$guest/${GameStorage.X64_CARD_DIR}"
}
fun list(context: Context): List<Entry> = parse(SessionPrefs.steamLibraries(context))
internal fun parse(json: String): List<Entry> {
if (json.isBlank()) return emptyList()
val array = runCatching { JSONArray(json) }.getOrNull() ?: return emptyList()
return (0 until array.length()).mapNotNull { i ->
val o = array.optJSONObject(i) ?: return@mapNotNull null
val id = o.optString("id")
val path = o.optString("path")
if (!id.matches(Regex("[0-9]{1,6}")) || path.isEmpty()) null else Entry(id, path, clean(o.optString("label")).ifEmpty { File(path).name })
}.distinctBy { it.id }
}
private fun save(context: Context, entries: List<Entry>) {
val array = JSONArray()
for (e in entries) array.put(JSONObject().put("id", e.id).put("path", e.path).put("label", e.label))
SessionPrefs.setSteamLibraries(context, array.toString())
}
/** Steam's libraryfolders.vdf stores labels between quotes with no escaping DroidDeck relies on. */
internal fun clean(label: String): String = label.replace('"', ' ').replace('\\', ' ').replace(Regex("\\s+"), " ").trim()
sealed class AddResult {
class Added(val entry: Entry) : AddResult()
class Refused(val reason: String) : AddResult()
}
fun add(context: Context, path: String, label: String = defaultLabel(context, File(path))): AddResult {
val entries = list(context)
if (entries.size >= MAX) return AddResult.Refused(context.getString(R.string.libraries_full, MAX))
val host = canonical(File(path))
val taken = entries.map { canonical(it.host) } + listOfNotNull(
GameStorage.effective(context)?.let { canonical(File(it.path)) },
canonical(SessionPrefs.steamRoot(context, "arm64")),
)
if (taken.any { overlaps(it, host) }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
// Adding a Games folder itself is fine; a child of one would make it overlap and hide the rest of that folder.
if (AddedGames.roots(context).any { host.startsWith(canonical(it.host) + "/") }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
GameStorage.prepare(context, path)?.let { return AddResult.Refused(it) }
synchronized(lock) {
val current = list(context)
if (current.size >= MAX) return AddResult.Refused(context.getString(R.string.libraries_full, MAX))
if (current.any { overlaps(canonical(it.host), host) }) return AddResult.Refused(context.getString(R.string.libraries_overlap))
// A removed library's mount stays registered with Steam until its next session, so ids are never reused.
val next = maxOf(SessionPrefs.steamLibraryNextId(context), (current.maxOfOrNull { it.id.toInt() } ?: 0) + 1)
val entry = Entry(next.toString(), path, clean(label).ifEmpty { File(path).name })
save(context, current + entry)
SessionPrefs.setSteamLibraryNextId(context, next + 1)
return AddResult.Added(entry)
}
}
fun rename(context: Context, id: String, label: String) {
val name = clean(label).takeIf { it.isNotEmpty() } ?: return
synchronized(lock) { save(context, list(context).map { if (it.id == id) Entry(it.id, it.path, name) else it }) }
}
/** Forgets the library; its folder and games stay where they are. */
fun remove(context: Context, id: String) {
synchronized(lock) { save(context, list(context).filterNot { it.id == id }) }
}
fun defaultLabel(context: Context, folder: File): String {
val volume = try {
context.getSystemService(StorageManager::class.java)?.getStorageVolume(folder)?.getDescription(context)
} catch (e: Exception) {
null
}
val name = folder.name.ifEmpty { context.getString(R.string.gstore_folder) }
return clean(if (volume.isNullOrBlank()) name else context.getString(R.string.gstore_folder_on_volume, name, volume))
}
internal fun overlaps(a: String, b: String): Boolean = a == b || a.startsWith("$b/") || b.startsWith("$a/")
private fun canonical(file: File): String = runCatching { file.canonicalPath }.getOrDefault(file.absolutePath)
}
@@ -74,6 +74,7 @@ class ModeSettings(
val mic: Boolean?,
val gameStorage: String? = null,
val storageOptions: List<com.droiddeck.launcher.session.GameStorage.Option> = emptyList(),
val steamLibraries: List<com.droiddeck.launcher.session.SteamLibraries.Entry> = emptyList(),
val storageDiagnostics: Boolean = false,
val fexPreset: String? = null,
val forceSsbs: Boolean = false,
@@ -124,6 +125,9 @@ class ModeSettingsActions(
val onGameStorage: (path: String, label: String) -> Unit = { _, _ -> },
val onPickGameStorageFolder: () -> Unit = {},
val onStorageDiagnostics: (Boolean) -> Unit = {},
val onAddSteamLibrary: () -> Unit = {},
val onRenameSteamLibrary: (id: String, label: String) -> Unit = { _, _ -> },
val onRemoveSteamLibrary: (id: String) -> Unit = {},
val onFexPreset: (String) -> Unit = {},
val onForceSsbs: (Boolean) -> Unit = {},
val onSyncBackend: (String) -> Unit = {},
@@ -402,6 +406,34 @@ fun ModeSheetRows(s: ModeSettings, a: ModeSettingsActions, tab: String, extras:
onChange = a.onStorageDiagnostics,
)
}
if (s.gameStorage != null) SettingsGroup(stringResource(R.string.libraries_title), detail = stringResource(R.string.libraries_applies_next)) {
var renaming by remember { mutableStateOf<com.droiddeck.launcher.session.SteamLibraries.Entry?>(null) }
for (library in s.steamLibraries) {
val attached = library.host.isDirectory
SettingsRow(library.label, if (attached) library.path else stringResource(R.string.libraries_missing, library.path)) {
Row(horizontalArrangement = androidx.compose.foundation.layout.Arrangement.spacedBy(6.dp)) {
SecondaryButton(stringResource(R.string.libraries_rename), compact = true) { renaming = library }
SecondaryButton(stringResource(R.string.edit_remove), compact = true) { a.onRemoveSteamLibrary(library.id) }
}
}
}
SettingsRow(
stringResource(R.string.libraries_add),
if (s.steamLibraries.size < com.droiddeck.launcher.session.SteamLibraries.MAX) stringResource(R.string.libraries_hint)
else stringResource(R.string.libraries_full, com.droiddeck.launcher.session.SteamLibraries.MAX),
) {
SecondaryButton(
stringResource(R.string.mode_choose_folder),
enabled = s.steamLibraries.size < com.droiddeck.launcher.session.SteamLibraries.MAX,
onClick = a.onAddSteamLibrary,
)
}
renaming?.let { library ->
TextEntryDialog(stringResource(R.string.libraries_rename_title), library.label, onDismiss = { renaming = null }) { name ->
a.onRenameSteamLibrary(library.id, name)
}
}
}
}
if (tab == SheetTab.STEAM && steam) {
extras.account(host)