fix: a session starts from the last added-games listing and refreshes it in the background

This commit is contained in:
xXJsonDeruloXx committed 2026-10-11 10:08:10 -04:00
1 parent dfc0b95557
commit e079f9602f
4 files changed
+46 -8

No files matched your search

@@ -490,8 +490,10 @@ object AddedGames {
}
/** The list the session hands the runtime's shortcuts writer; one file per session start. */
fun listingFile(context: Context): File = File(context.filesDir, "session/added-games.json")
fun writeListing(context: Context, games: List<Game>): File {
val file = File(context.filesDir, "session/added-games.json").apply { parentFile?.mkdirs() }
val file = listingFile(context).apply { parentFile?.mkdirs() }
val json = StringBuilder("[")
games.forEachIndexed { i, g ->
if (i > 0) json.append(',')
@@ -508,7 +510,12 @@ object AddedGames {
if (pieces.isNotEmpty()) json.append(",\"art\":{").append(pieces.joinToString(",") { (k, v) -> quote(k) + ":" + quote(v) }).append('}')
json.append('}')
}
file.writeText(json.append(']').toString())
val staged = File(file.path + ".tmp")
staged.writeText(json.append(']').toString())
if (!staged.renameTo(file)) {
file.writeText(staged.readText())
staged.delete()
}
return file
}
@@ -31,6 +31,28 @@ object AddedGamesPublisher {
if (now) work.run() else worker.execute(work)
}
fun refreshDuringSession(context: Context) {
val app = context.applicationContext
worker.execute {
try {
val before = AddedGames.listingFile(app).takeIf { it.isFile }?.readText().orEmpty()
val games = AddedGames.scan(app)
AddedGames.writeListing(app, games)
LibraryCache.save(app, Library.launchableGames(app, added = games))
val fresh = games.filter { !before.contains(org.json.JSONObject.quote(it.guestExe)) }
if (fresh.isEmpty()) return@execute
Log.i(TAG, "session refresh: ${fresh.size} game(s) not in the listing the session started with")
for (attempt in 0 until 36) {
if (com.droiddeck.launcher.stores.SteamLiveShortcuts.sync(app, fresh) { true }) break
if (!com.droiddeck.launcher.session.SessionState.running) break
Thread.sleep(5_000)
}
} catch (e: Exception) {
Log.w(TAG, "session refresh: ${e.message}")
}
}
}
/** The listing and the library cache from one scan of the added games. Blocking. */
fun republish(context: Context) {
val games = AddedGames.scan(context)
@@ -385,12 +385,19 @@ class SessionService : Service() {
// The user's own games, for the runtime's shortcuts writer to put in the client's library
// before the client starts (see frontend/AddedGames and droiddeck-steam-shortcuts).
if (steamHere) {
val added = com.droiddeck.launcher.frontend.AddedGames.scan(this)
val listing = com.droiddeck.launcher.frontend.AddedGames.writeListing(this, added)
val cached = com.droiddeck.launcher.frontend.AddedGames.listingFile(this)
val listing = if (cached.isFile) {
com.droiddeck.launcher.frontend.AddedGamesPublisher.refreshDuringSession(this)
Log.i(TAG, "added games: the last listing, refreshed in the background")
cached
} else {
val added = com.droiddeck.launcher.frontend.AddedGames.scan(this)
if (added.isNotEmpty()) Log.i(TAG, "added games: " + added.joinToString { "${it.name} (${it.exe.name})" })
com.droiddeck.launcher.frontend.AddedGames.writeListing(this, added)
}
guest.add("BL_ADDED_GAMES=" + listing.path)
if (OfflineMode.enabled(this)) guest.add("BL_STEAM_OFFLINE=1")
if (com.droiddeck.launcher.gpu.Lossless.owned(this)) guest.add("BL_LOSSLESS_OWNED=1")
if (added.isNotEmpty()) Log.i(TAG, "added games: " + added.joinToString { "${it.name} (${it.exe.name})" })
}
// Where the guest leaves a request for another session (the desktop's Steam launchers).
guest.add("BL_LAUNCH_DIR=" + sessionRoot.path)
@@ -37,9 +37,10 @@ object SteamLiveShortcuts {
private const val COMPAT_TOOL = "droiddeck-proton-arm64"
/** Adds every game in [games] that [include] takes (store games by default) and the client does not list yet. Blocking; worker thread. */
fun sync(context: Context, games: List<AddedGames.Game>, include: (AddedGames.Game) -> Boolean = { it.source != Library.ADDED }) {
fun sync(context: Context, games: List<AddedGames.Game>, include: (AddedGames.Game) -> Boolean = { it.source != Library.ADDED }): Boolean {
val wanted = games.filter(include)
if (wanted.isEmpty() || !clientRunning()) return
if (wanted.isEmpty()) return true
if (!clientRunning()) return false
val list = JSONArray()
for (g in wanted) list.put(JSONObject().put("name", g.name).put("exe", g.guestExe).put("dir", g.guestDir))
val js = """
@@ -59,9 +60,10 @@ object SteamLiveShortcuts {
return added;
})()
""".trimIndent()
val result = evaluate(context, js) ?: return
val result = evaluate(context, js) ?: return false
Log.i(TAG, "live shortcuts: $result added")
if (result != "0") StoresState.logLine("Steam: $result shortcut(s) added to the running client")
return true
}
/** Removes the shortcut with [appId] from the running client. Blocking; worker thread. */