Start a launcher-run game without scanning its folder and keep Play on Starting until it launches (#2586)

* Start a launcher-run game without scanning its folder and keep Play on Starting until it launches

* Create the shared added-games cache under one lock

* Read a rewritten game manifest again instead of reusing the saved ranking

* Tie a saved ranking to the declared executables that exist
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-13 18:47:15 -05:00
1 parent 6d1a0d9fd0
commit b3ebd54d87
6 files changed
+83 -23

No files matched your search

@@ -100,12 +100,13 @@ object AddedGames {
fun candidates(folder: File, reuse: Boolean = false): List<File> = rank(folder, reuse).exes
fun rank(folder: File, reuse: Boolean = false): GameExePicker.Ranking {
val key = "${folder.path}|${folder.lastModified()}"
val declared = declaredExes(folder)
val stamp = declared.fold(0L) { hash, exe -> hash * 31 + exe.path.hashCode() }
val key = "${folder.path}|${folder.lastModified()}|$stamp"
val now = System.currentTimeMillis()
ranked[key]?.takeIf { now - it.first < RANK_FRESH_MS }?.let { return it.second }
if (reuse) kept?.rank(folder)?.let { return it }
if (reuse) kept?.rank(folder, stamp)?.let { return it }
val guessed = GameExePicker.rank(folder) { !skipExe(it.name) }
val declared = declaredExes(folder)
val preferred = declared.map { it.path }.toSet()
val result = if (declared.isEmpty()) guessed else GameExePicker.Ranking(
declared + guessed.exes.filter { it.path !in preferred },
@@ -115,7 +116,7 @@ object AddedGames {
)
if (ranked.size > 256) ranked.clear()
ranked[key] = now to result
kept?.keepRank(folder, result)
kept?.keepRank(folder, result, stamp)
return result
}
@@ -200,8 +201,7 @@ object AddedGames {
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 }
val cache = cache(context)
try {
scanAll(context)
} finally {
@@ -209,6 +209,13 @@ object AddedGames {
}
}
private val keeping = Any()
private fun cache(context: Context): AddedGamesCache = synchronized(keeping) {
val file = File(context.filesDir, AddedGamesCache.FILE)
kept?.takeIf { it.file == file } ?: AddedGamesCache(file).also { kept = it }
}
private fun scanAll(context: Context): List<Game> {
val out = ArrayList<Game>()
GamesFolderMigration.run(context)
@@ -241,11 +248,17 @@ object AddedGames {
return out.distinctBy { it.folder.canonicalPath }
}
fun single(context: Context, folderPath: String): Game? {
fun single(context: Context, folderPath: String, reuse: Boolean = false): Game? {
val folder = File(folderPath)
if (!folder.isDirectory || folderPath in SessionPrefs.removedAddedGames(context)) return null
val out = ArrayList<Game>(1)
scanGame(context, folder, out, single = AddedExes.list(context).firstOrNull { it.folder == folderPath })
val entry = AddedExes.list(context).firstOrNull { it.folder == folderPath }
val cache = if (reuse) cache(context) else null
try {
scanGame(context, folder, out, single = entry, reuse = reuse)
} finally {
cache?.save()
}
return out.firstOrNull()
}
@@ -7,7 +7,7 @@ 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 Rank(val modified: Long, val declared: Long, val at: Long, val ranking: GameExePicker.Ranking)
private class Name(val name: String, val at: Long)
private val ranks = ConcurrentHashMap<String, Rank>()
@@ -22,7 +22,7 @@ internal class AddedGamesCache(val file: File) {
val exes = o.getJSONArray("exes")
val scores = o.getJSONArray("scores")
require(exes.length() > 0 && exes.length() == scores.length())
ranks[path] = Rank(o.getLong("modified"), o.getLong("at"), GameExePicker.Ranking(
ranks[path] = Rank(o.getLong("modified"), o.optLong("declared"), o.getLong("at"), GameExePicker.Ranking(
List(exes.length()) { File(exes.getString(it)) }, List(scores.length()) { scores.getInt(it) }, o.optBoolean("uncertain"),
))
}
@@ -32,19 +32,19 @@ internal class AddedGamesCache(val file: File) {
}
}
fun rank(folder: File): GameExePicker.Ranking? {
fun rank(folder: File, declared: Long = 0): 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 }
return kept.ranking.takeIf { kept.modified == folder.lastModified() && kept.declared == declared && age in 0 until KEEP_MS && top.isFile }
}
fun keepRank(folder: File, ranking: GameExePicker.Ranking) {
fun keepRank(folder: File, ranking: GameExePicker.Ranking, declared: Long = 0) {
if (ranking.exes.isEmpty()) {
if (ranks.remove(folder.path) != null) changed = true
return
}
ranks[folder.path] = Rank(folder.lastModified(), System.currentTimeMillis(), ranking)
ranks[folder.path] = Rank(folder.lastModified(), declared, System.currentTimeMillis(), ranking)
changed = true
}
@@ -62,7 +62,7 @@ internal class AddedGamesCache(val file: File) {
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)
if (r.at >= oldest) keptRanks.put(path, JSONObject().put("modified", r.modified).put("declared", r.declared).put("at", r.at)
.put("exes", JSONArray(r.ranking.exes.map { it.path })).put("scores", JSONArray(r.ranking.scores))
.put("uncertain", r.ranking.uncertain))
}