mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 22:05:59 +00:00
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:
1 parent
c5577478ca
commit
e30e722f8b
16 files changed
+358
-9
No files matched your search
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user