mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
feat: suggest Steam libraries found on internal storage and cards (#2023)
* feat: suggest Steam libraries found on internal storage and cards * fix: add found card libraries by their readable path and drop superseded scan results
This commit is contained in:
1 parent
bc5edb81db
commit
5fe3021e5e
5 files changed
+153
-13
No files matched your search
@@ -3,6 +3,7 @@ package com.droiddeck.launcher.session
|
||||
import android.content.Context
|
||||
import android.os.storage.StorageManager
|
||||
import com.droiddeck.launcher.R
|
||||
import com.droiddeck.launcher.files.StorageRoots
|
||||
import com.droiddeck.launcher.frontend.AddedGames
|
||||
import java.io.File
|
||||
import org.json.JSONArray
|
||||
@@ -49,15 +50,12 @@ object SteamLibraries {
|
||||
class Refused(val reason: String) : AddResult()
|
||||
}
|
||||
|
||||
fun add(context: Context, path: String, label: String = defaultLabel(context, File(path))): AddResult {
|
||||
fun add(context: Context, picked: String, label: String? = null): AddResult {
|
||||
val entries = list(context)
|
||||
if (entries.size >= MAX) return AddResult.Refused(context.getString(R.string.libraries_full, MAX))
|
||||
val path = innerLibrary(File(picked))?.path ?: picked
|
||||
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))
|
||||
if (taken(context, entries).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) }
|
||||
@@ -67,13 +65,70 @@ object SteamLibraries {
|
||||
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 })
|
||||
val entry = Entry(next.toString(), path, clean(label ?: defaultLabel(context, File(path))).ifEmpty { File(path).name })
|
||||
save(context, current + entry)
|
||||
SessionPrefs.setSteamLibraryNextId(context, next + 1)
|
||||
return AddResult.Added(entry)
|
||||
}
|
||||
}
|
||||
|
||||
private fun taken(context: Context, entries: List<Entry>): List<String> = entries.map { canonical(it.host) } + listOfNotNull(
|
||||
GameStorage.effective(context)?.let { canonical(File(it.path)) },
|
||||
canonical(SessionPrefs.steamRoot(context, "arm64")),
|
||||
)
|
||||
|
||||
class Found(val path: String, val shown: String, val label: String, val games: Int)
|
||||
|
||||
/** Reads storage; call off the main thread. */
|
||||
fun found(context: Context): List<Found> {
|
||||
val entries = list(context)
|
||||
val taken = taken(context, entries) + AddedGames.roots(context).map { canonical(it.host) }
|
||||
val hits = ArrayList<Pair<File, Int>>()
|
||||
for (root in StorageRoots.list(context)) {
|
||||
if (!root.readable) continue
|
||||
for (dir in candidates(rawView(root.dir), SCAN_DEPTH)) {
|
||||
val games = gameCount(dir)
|
||||
if (games > 0 && File(dir, "steamapps").canWrite() && taken.none { overlaps(it, canonical(dir)) }) hits += dir to games
|
||||
}
|
||||
}
|
||||
val picked = ArrayList<Pair<File, Int>>()
|
||||
for (hit in hits.sortedByDescending { it.second }) {
|
||||
if (picked.none { overlaps(canonical(it.first), canonical(hit.first)) }) picked += hit
|
||||
}
|
||||
return picked.map { (dir, games) ->
|
||||
val shown = File(storagePath(dir.path))
|
||||
Found(dir.path, shown.path, defaultLabel(context, shown), games)
|
||||
}
|
||||
}
|
||||
|
||||
/** The FUSE view under /storage hides files other apps wrote unless DroidDeck has all files access; the raw card mount does not. */
|
||||
private fun rawView(dir: File): File {
|
||||
if (!dir.path.startsWith("/storage/") || dir.path.startsWith("/storage/emulated")) return dir
|
||||
val raw = File("/mnt/media_rw/" + dir.path.removePrefix("/storage/"))
|
||||
return if (raw.canRead() && raw.list() != null) raw else dir
|
||||
}
|
||||
|
||||
internal fun innerLibrary(picked: File): File? {
|
||||
if (gameCount(picked) > 0) return null
|
||||
return candidates(picked, 1).filter { it != picked && gameCount(it) > 0 }.singleOrNull()
|
||||
}
|
||||
|
||||
internal fun candidates(root: File, depth: Int): List<File> {
|
||||
val out = arrayListOf(root)
|
||||
var level = listOf(root)
|
||||
repeat(depth) {
|
||||
level = level.flatMap { dir ->
|
||||
dir.listFiles { f -> f.isDirectory && !f.name.startsWith(".") && f.name != "steamapps" && !(dir == root && f.name == "Android") }
|
||||
.orEmpty().sortedBy { it.name }
|
||||
}.take(SCAN_LIMIT - out.size)
|
||||
out += level
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
internal fun gameCount(library: File): Int =
|
||||
File(library, "steamapps").list()?.count { it.startsWith("appmanifest_") && it.endsWith(".acf") } ?: 0
|
||||
|
||||
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 }) }
|
||||
@@ -84,17 +139,27 @@ object SteamLibraries {
|
||||
synchronized(lock) { save(context, list(context).filterNot { it.id == id }) }
|
||||
}
|
||||
|
||||
fun defaultLabel(context: Context, folder: File): String {
|
||||
fun defaultLabel(context: Context, picked: File): String {
|
||||
val folder = File(storagePath(picked.path))
|
||||
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) }
|
||||
val parent = folder.parentFile
|
||||
val named = if (folder.name.equals("Steam", true) && parent != null && !volumeRoot(parent)) parent else folder
|
||||
val name = named.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))
|
||||
}
|
||||
|
||||
private fun volumeRoot(dir: File): Boolean = dir.parent in setOf("/storage", "/mnt/media_rw", "/storage/emulated") || dir.parent == null
|
||||
|
||||
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)
|
||||
private fun storagePath(path: String): String = if (path.startsWith("/mnt/media_rw/")) "/storage/" + path.removePrefix("/mnt/media_rw/") else path
|
||||
|
||||
private fun canonical(file: File): String = storagePath(runCatching { file.canonicalPath }.getOrDefault(file.absolutePath))
|
||||
|
||||
private const val SCAN_DEPTH = 2
|
||||
private const val SCAN_LIMIT = 4000
|
||||
}
|
||||
Reference in new issue
Block a user