mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
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:
1 parent
6d1a0d9fd0
commit
b3ebd54d87
6 files changed
+83
-23
No files matched your search
@@ -888,13 +888,15 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
}
|
||||
}
|
||||
|
||||
private fun launchGame(game: Library.SteamGame, returnHome: Boolean = false): Boolean {
|
||||
private fun launchGame(game: Library.SteamGame, returnHome: Boolean = false, onHandled: () -> Unit = {}): Boolean {
|
||||
if (!SessionPrefs.soundtracksIngame(this)) com.droiddeck.launcher.playback.SoundtrackController.pause(this)
|
||||
if (pkgStage != null) {
|
||||
onHandled()
|
||||
android.widget.Toast.makeText(this, R.string.main_wait_install_game, android.widget.Toast.LENGTH_SHORT).show()
|
||||
return false
|
||||
}
|
||||
if (com.droiddeck.launcher.session.NoSteamLaunch.enabled(this, game)) return launchWithoutSteam(game, returnHome)
|
||||
if (com.droiddeck.launcher.session.NoSteamLaunch.enabled(this, game)) return launchWithoutSteam(game, returnHome, onHandled)
|
||||
onHandled()
|
||||
if (SessionState.running) {
|
||||
if (SessionState.mode != SessionService.MODE_STEAM || SessionState.stopRequested) {
|
||||
android.widget.Toast.makeText(this, R.string.game_link_session_busy, android.widget.Toast.LENGTH_LONG).show()
|
||||
@@ -938,12 +940,13 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
.apply { if (returnHome) putExtra(SessionActivity.EXTRA_LINK_TASK, task) }
|
||||
}
|
||||
|
||||
private fun launchWithoutSteam(game: Library.SteamGame, returnHome: Boolean): Boolean {
|
||||
private fun launchWithoutSteam(game: Library.SteamGame, returnHome: Boolean, onHandled: () -> Unit = {}): Boolean {
|
||||
val app = applicationContext
|
||||
Thread({
|
||||
val added = if (game.library == Library.ADDED) runCatching { com.droiddeck.launcher.session.NoSteamLaunch.addedGame(app, game) }.getOrNull() else null
|
||||
val added = if (com.droiddeck.launcher.session.NoSteamLaunch.needsAddedGame(game)) runCatching { com.droiddeck.launcher.session.NoSteamLaunch.addedGame(app, game) }.getOrNull() else null
|
||||
val command = com.droiddeck.launcher.session.NoSteamLaunch.command(game, added)
|
||||
ui.post {
|
||||
onHandled()
|
||||
if (isDestroyed) return@post
|
||||
if (command == null) {
|
||||
android.widget.Toast.makeText(this, R.string.game_link_failed, android.widget.Toast.LENGTH_LONG).show()
|
||||
@@ -2069,8 +2072,8 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet
|
||||
com.droiddeck.launcher.frontend.AutoComponents.beforeLaunch(this, g, onBlocked = done) {
|
||||
if (request != gameLaunchRequest.get() || isFinishing || isDestroyed) { done(); return@beforeLaunch }
|
||||
com.droiddeck.launcher.stores.StoreLaunch.prepare(this, g) {
|
||||
done()
|
||||
if (request == gameLaunchRequest.get() && !isFinishing && !isDestroyed) launchGame(g)
|
||||
if (request == gameLaunchRequest.get() && !isFinishing && !isDestroyed) launchGame(g, onHandled = done)
|
||||
else done()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
@@ -49,9 +49,14 @@ object NoSteamLaunch {
|
||||
return com.droiddeck.launcher.core.GameResolution.resolve(choice, output)
|
||||
}
|
||||
|
||||
fun needsAddedGame(game: Library.SteamGame): Boolean = game.library == Library.ADDED && !viaBattleNet(game)
|
||||
|
||||
private fun viaBattleNet(game: Library.SteamGame): Boolean =
|
||||
game.library == Library.ADDED && game.source == com.droiddeck.launcher.stores.Store.BATTLENET.id
|
||||
|
||||
fun command(game: Library.SteamGame, added: AddedGames.Game?): Command? = when {
|
||||
game.library == Library.EPIC_LAUNCHER -> Command(EPIC, listOf("run"), game.name)
|
||||
game.library == Library.ADDED && game.source == com.droiddeck.launcher.stores.Store.BATTLENET.id ->
|
||||
viaBattleNet(game) ->
|
||||
game.storeId?.let(com.droiddeck.launcher.runtime.BattleNet::title)?.let { Command(BATTLENET, listOf("launch", it.code), game.name) }
|
||||
game.library == Library.ADDED -> added?.let { a -> a.steamAppId?.let { Command(PROTON_RUN, listOf("--app", it.toString()), a.name) } ?: addedCommand(a) }
|
||||
game.appId > 0 -> Command(PROTON_RUN, listOf("--app", game.appId.toString()), game.name)
|
||||
@@ -70,7 +75,7 @@ object NoSteamLaunch {
|
||||
|
||||
fun addedGame(context: Context, game: Library.SteamGame): AddedGames.Game? {
|
||||
val id = game.appId.toLong() and 0xFFFFFFFFL
|
||||
val one = game.gameFiles?.let { AddedGames.single(context, it.path) }
|
||||
val one = game.gameFiles?.let { AddedGames.single(context, it.path, reuse = true) }
|
||||
return one?.takeIf { settingsId(it) == id } ?: AddedGames.scan(context).firstOrNull { settingsId(it) == id }
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,38 @@ class AddedGamesCacheTest {
|
||||
assertEquals("Bastion", AddedGamesCache(file).name("named"))
|
||||
}
|
||||
|
||||
@Test fun aRewrittenManifestIsReadAgain() {
|
||||
val folder = tmp.newFolder("Game")
|
||||
val first = File(folder, "First.exe").apply { writeText("first") }
|
||||
val second = File(folder, "Second.exe").apply { writeText("second") }
|
||||
val manifest = File(folder, "MicrosoftGame.config").apply { writeText("<Executable Name=\"First.exe\"/>") }
|
||||
val files = tmp.newFolder("files")
|
||||
val context = object : android.content.ContextWrapper(app) {
|
||||
override fun getFilesDir() = files
|
||||
}
|
||||
AddedGames.scan(context)
|
||||
assertEquals(first, AddedGames.rank(folder).exes.first())
|
||||
val modified = folder.lastModified()
|
||||
manifest.writeText("<Executable Name=\"Second.exe\"/>")
|
||||
folder.setLastModified(modified)
|
||||
assertEquals(second, AddedGames.rank(folder, reuse = true).exes.first())
|
||||
}
|
||||
|
||||
@Test fun aDeclaredExecutableThatArrivesLaterIsPicked() {
|
||||
val folder = tmp.newFolder("Game")
|
||||
val guessed = File(folder, "Game.exe").apply { writeText("game") }
|
||||
val inner = File(folder, "Content").apply { mkdirs() }
|
||||
File(folder, "MicrosoftGame.config").writeText("<Executable Name=\"Content\\Late.exe\"/>")
|
||||
val files = tmp.newFolder("files")
|
||||
val context = object : android.content.ContextWrapper(app) {
|
||||
override fun getFilesDir() = files
|
||||
}
|
||||
AddedGames.scan(context)
|
||||
assertEquals(guessed, AddedGames.rank(folder).exes.first())
|
||||
val late = File(inner, "Late.exe").apply { writeText("late") }
|
||||
assertEquals(late, AddedGames.rank(folder, reuse = true).exes.first())
|
||||
}
|
||||
|
||||
@Test fun aRankingAScanReusedIsNotHandedToAFreshLookup() {
|
||||
val folder = tmp.newFolder("Game")
|
||||
val walked = File(folder, "Game.exe").apply { writeText("game") }
|
||||
|
||||
@@ -70,6 +70,13 @@ class NoSteamLaunchTest {
|
||||
assertEquals(NoSteamLaunch.EPIC to listOf("run"), epic.program to epic.args)
|
||||
}
|
||||
|
||||
@Test fun onlyAnAddedGameStartedFromItsOwnFilesHasItsFolderRead() {
|
||||
assertTrue(NoSteamLaunch.needsAddedGame(shortcut))
|
||||
assertFalse(NoSteamLaunch.needsAddedGame(Library.SteamGame(shortcut.appId, shortcut.name, null, Library.ADDED, source = com.droiddeck.launcher.stores.Store.BATTLENET.id)))
|
||||
assertFalse(NoSteamLaunch.needsAddedGame(steamGame))
|
||||
assertFalse(NoSteamLaunch.needsAddedGame(launcher))
|
||||
}
|
||||
|
||||
@Test fun anAddedGameOwnedOnSteamUsesItsSteamIdForSettingsAndLaunch() {
|
||||
val routed = AddedGames.Game(
|
||||
File("/x/Stardew Valley"), "Stardew Valley", File("/x/Stardew Valley/Stardew Valley.exe"),
|
||||
|
||||
Reference in new issue
Block a user