Added games: one scan at a time, and keep what it found across launches

Every Steam session scans the added games before the Linux side starts,
walking each game folder for its exe and reading the exe's version info.
On an AYANEO Pocket FIT Elite with 40 games on an exFAT SD card that walk
took 10 s with a warm cache and up to 155 s after Android had dropped it,
and the front end's art pass and library load ran the same scan at the
same time, so the session waited on all of them.

Scans now run one at a time, so a waiting scan reuses what the one before
it just found. Each game folder's exe ranking and identified name are kept
in added-games-scan.json; a scan reuses a ranking while the folder's
modification time is unchanged, its first exe still exists and it is under
a week old, and walks the folder again otherwise. The exe picker, single
game refreshes and adding a folder still walk the folder fresh.
This commit is contained in:
K11MCH1 authored and Kurt Himebauch committed 2026-11-05 13:59:59 -05:00
1 parent fcbd45453f
commit 93923e125d
3 files changed
+197 -12

No files matched your search

@@ -99,10 +99,11 @@ object AddedGames {
fun candidates(folder: File): List<File> = rank(folder).exes
fun rank(folder: File): GameExePicker.Ranking {
fun rank(folder: File, reuse: Boolean = false): GameExePicker.Ranking {
val key = "${folder.path}|${folder.lastModified()}"
val now = System.currentTimeMillis()
ranked[key]?.takeIf { now - it.first < RANK_FRESH_MS }?.let { return it.second }
if (reuse) kept?.rank(folder)?.let { ranked[key] = now to it; return it }
val guessed = GameExePicker.rank(folder) { !skipExe(it.name) }
val declared = declaredExes(folder)
val preferred = declared.map { it.path }.toSet()
@@ -114,23 +115,27 @@ object AddedGames {
)
if (ranked.size > 256) ranked.clear()
ranked[key] = now to result
kept?.keepRank(folder, result)
return result
}
private val ranked = java.util.concurrent.ConcurrentHashMap<String, Pair<Long, GameExePicker.Ranking>>()
private const val RANK_FRESH_MS = 60_000L
@Volatile private var kept: AddedGamesCache? = null
private val identified = java.util.concurrent.ConcurrentHashMap<String, String>()
internal fun identifiedName(folder: File, exe: File): String? {
val key = "${exe.path}|${exe.length()}|${exe.lastModified()}|${folder.lastModified()}"
identified[key]?.let { return it.ifEmpty { null } }
kept?.name(key)?.let { identified[key] = it; return it.ifEmpty { null } }
val id = runCatching { GameIdentifier.identify(exe) }.getOrNull()
val found = id?.name?.takeIf {
id.nameSource in setOf(GameIdentifier.Source.STEAM_MANIFEST_ACF, GameIdentifier.Source.GOG_INFO, GameIdentifier.Source.PE_VERSION)
}
val name = found?.let { if (id?.nameSource == GameIdentifier.Source.PE_VERSION) betterSpelling(underscoresToSpaces(it), folder.name) else it }
identified[key] = name.orEmpty()
kept?.keepName(key, name.orEmpty())
return name
}
@@ -172,17 +177,17 @@ object AddedGames {
"launcher", "game", "engine", "system", "data", "app",
)
internal fun groupGames(folder: File): List<File>? {
internal fun groupGames(folder: File, reuse: Boolean = false): List<File>? {
val files = folder.listFiles() ?: return null
if (files.any { it.isFile && it.name.endsWith(".exe", ignoreCase = true) && !skipExe(it.name) }) return null
if (declaredExes(folder).isNotEmpty()) return null
val dirs = files.filter { it.isDirectory && !it.name.startsWith(".") && !isSteamLibraryFolder(it.name) }.sortedBy { it.name.lowercase() }
if (dirs.any { it.name.lowercase() in GAME_PARTS || GameExePicker.auxiliaryDir(it) }) return null
return dirs.filter { rank(it).exes.isNotEmpty() }.takeIf { it.size >= 2 }
return dirs.filter { rank(it, reuse).exes.isNotEmpty() }.takeIf { it.size >= 2 }
}
private fun expand(folder: File, entries: Set<String>): List<File> =
if (folder.path in entries) listOf(folder) else groupGames(folder) ?: listOf(folder)
private fun expand(folder: File, entries: Set<String>, reuse: Boolean = false): List<File> =
if (folder.path in entries) listOf(folder) else groupGames(folder, reuse) ?: listOf(folder)
private fun entryFolders(context: Context): Set<String> = AddedExes.list(context).map { it.folder }.toSet()
@@ -192,7 +197,19 @@ object AddedGames {
internal fun isSteamLibraryFolder(name: String): Boolean =
name.equals("steamapps", ignoreCase = true) || name == com.droiddeck.launcher.session.GameStorage.X64_CARD_DIR
fun scan(context: Context): List<Game> {
private val scanning = Any()
fun scan(context: Context): List<Game> = synchronized(scanning) {
val file = File(context.filesDir, AddedGamesCache.FILE)
val cache = kept?.takeIf { it.file == file } ?: AddedGamesCache(file).also { kept = it }
try {
scanAll(context)
} finally {
cache.save()
}
}
private fun scanAll(context: Context): List<Game> {
val out = ArrayList<Game>()
GamesFolderMigration.run(context)
val folders = listOfNotNull(
@@ -208,16 +225,16 @@ object AddedGames {
if (folder.name.lowercase() in steamInstalls || isSteamLibraryFolder(folder.name)) continue
if (folder.path in removed) continue
if (folder.name == "Games" && StoreInstallRoot.roots(context).any { it.absolutePath == folder.absolutePath }) continue
for (game in expand(folder, entries)) if (game.path !in removed) scanGame(context, game, out)
for (game in expand(folder, entries, reuse = true)) if (game.path !in removed) scanGame(context, game, out, reuse = true)
}
}
for (entry in AddedExes.list(context)) {
val folder = File(entry.folder)
if (folder.path in removed || !folder.isDirectory) continue
scanGame(context, folder, out, single = entry)
scanGame(context, folder, out, single = entry, reuse = true)
}
for (folder in libraryDepotInstalls(context)) if (folder.path !in removed) scanGame(context, folder, out)
for (folder in StoreInstallRoot.gameFolders(context)) if (StoreInstallRoot.isFinished(folder)) scanGame(context, folder, out)
for (folder in libraryDepotInstalls(context)) if (folder.path !in removed) scanGame(context, folder, out, reuse = true)
for (folder in StoreInstallRoot.gameFolders(context)) if (StoreInstallRoot.isFinished(folder)) scanGame(context, folder, out, reuse = true)
for (game in com.droiddeck.launcher.runtime.BattleNet.games(context)) {
if (game.folder.path !in removed) com.droiddeck.launcher.runtime.BattleNet.shortcut(context, game)?.let(out::add)
}
@@ -398,11 +415,11 @@ object AddedGames {
private val INSTALL_DIR = Regex("\"installdir\"\\s*\"([^\"]*)\"", RegexOption.IGNORE_CASE)
private fun scanGame(context: Context, folder: File, out: MutableList<Game>, single: AddedExes.Entry? = null) {
private fun scanGame(context: Context, folder: File, out: MutableList<Game>, single: AddedExes.Entry? = null, reuse: Boolean = false) {
run {
val sidecar = StoreGameSidecar.read(folder)
sidecar?.takeIf { it.id.isNotEmpty() }?.let { SessionPrefs.followStoreGame(context, "${it.store.id}:${it.id}", folder.path) { old -> !File(old).exists() } }
val ranking = rank(folder)
val ranking = rank(folder, reuse)
val candidates = ranking.exes
val picked = SessionPrefs.addedGameExe(context, folder.path)
val chosen = picked.takeIf { it.isNotEmpty() }?.let { File(it) }?.takeIf { it.isFile }
@@ -0,0 +1,85 @@
package com.droiddeck.launcher.frontend
import android.util.AtomicFile
import org.json.JSONArray
import org.json.JSONObject
import java.io.File
import java.util.concurrent.ConcurrentHashMap
internal class AddedGamesCache(val file: File) {
private class Rank(val modified: Long, val at: Long, val ranking: GameExePicker.Ranking)
private class Name(val name: String, val at: Long)
private val ranks = ConcurrentHashMap<String, Rank>()
private val names = ConcurrentHashMap<String, Name>()
@Volatile private var changed = false
init {
val json = runCatching { JSONObject(AtomicFile(file).readFully().toString(Charsets.UTF_8)) }.getOrNull()
json?.optJSONObject("ranks")?.let { all ->
for (path in all.keys()) {
val o = all.optJSONObject(path) ?: continue
val exes = o.optJSONArray("exes") ?: continue
val scores = o.optJSONArray("scores") ?: continue
if (exes.length() == 0 || exes.length() != scores.length()) continue
ranks[path] = Rank(o.optLong("modified"), o.optLong("at"), GameExePicker.Ranking(
List(exes.length()) { File(exes.getString(it)) }, List(scores.length()) { scores.getInt(it) }, o.optBoolean("uncertain"),
))
}
}
json?.optJSONObject("names")?.let { all ->
for (key in all.keys()) all.optJSONObject(key)?.let { names[key] = Name(it.optString("name"), it.optLong("at")) }
}
}
fun rank(folder: File): GameExePicker.Ranking? {
val kept = ranks[folder.path] ?: return null
val age = System.currentTimeMillis() - kept.at
val top = kept.ranking.exes.first()
return kept.ranking.takeIf { kept.modified == folder.lastModified() && age in 0 until KEEP_MS && top.isFile }
}
fun keepRank(folder: File, ranking: GameExePicker.Ranking) {
if (ranking.exes.isEmpty()) {
if (ranks.remove(folder.path) != null) changed = true
return
}
ranks[folder.path] = Rank(folder.lastModified(), System.currentTimeMillis(), ranking)
changed = true
}
fun name(key: String): String? = names[key]?.name
fun keepName(key: String, name: String) {
names[key] = Name(name, System.currentTimeMillis())
changed = true
}
@Synchronized
fun save() {
if (!changed) return
changed = false
val oldest = System.currentTimeMillis() - KEEP_MS
val keptRanks = JSONObject()
ranks.forEach { (path, r) ->
if (r.at >= oldest) keptRanks.put(path, JSONObject().put("modified", r.modified).put("at", r.at)
.put("exes", JSONArray(r.ranking.exes.map { it.path })).put("scores", JSONArray(r.ranking.scores))
.put("uncertain", r.ranking.uncertain))
}
val keptNames = JSONObject()
names.forEach { (key, n) -> if (n.at >= oldest) keptNames.put(key, JSONObject().put("name", n.name).put("at", n.at)) }
val atomic = AtomicFile(file)
val out = atomic.startWrite()
try {
out.write(JSONObject().put("ranks", keptRanks).put("names", keptNames).toString().toByteArray())
atomic.finishWrite(out)
} catch (e: Exception) {
atomic.failWrite(out)
}
}
companion object {
const val FILE = "added-games-scan.json"
const val KEEP_MS = 7L * 24 * 3600 * 1000
}
}