mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
Fix Steam game exit detection and return
This commit is contained in:
1 parent
9eca06d92e
commit
7dc9d342e3
3 files changed
+56
-5
No files matched your search
@@ -1633,8 +1633,13 @@ class SessionService : Service() {
|
||||
if (token != gameExitWatchToken || gen != sessionGen || !SessionState.running ||
|
||||
SessionState.stopRequested || SessionState.mode != MODE_STEAM ||
|
||||
!SessionPrefs.returnAfterGame(this) || SessionState.gameId != gameId ||
|
||||
pendingSteamGameId != null || SteamGameProcess.running()) {
|
||||
Log.i(TAG, "keeping Steam open after $gameId: another game is running or queued")
|
||||
pendingSteamGameId != null) {
|
||||
return@postDelayed
|
||||
}
|
||||
|
||||
if (SteamGameProcess.running()) {
|
||||
Log.i(TAG, "another Steam game is still running after $gameId; checking again")
|
||||
returnAfterGame(gameId, gen, token)
|
||||
return@postDelayed
|
||||
}
|
||||
|
||||
|
||||
@@ -18,18 +18,39 @@ object SteamGameProcess {
|
||||
runCatching { File(p, "cmdline").readBytes() }
|
||||
.getOrNull()?.let(::matches) == true
|
||||
} == true
|
||||
private fun steamAppIdForGameId(gameId: String): String? {
|
||||
if (gameId.isEmpty() || !gameId.all(Char::isDigit)) return null
|
||||
|
||||
val numericId = gameId.toULongOrNull() ?: return null
|
||||
val appId = if (numericId > UInt.MAX_VALUE.toULong()) {
|
||||
numericId shr 32
|
||||
} else {
|
||||
numericId
|
||||
}
|
||||
|
||||
return appId.takeIf { it != 0uL }?.toString()
|
||||
}
|
||||
|
||||
fun matches(cmdline: ByteArray, gameId: String): Boolean {
|
||||
if (gameId.isEmpty() || !gameId.all(Char::isDigit)) return false
|
||||
val expectedAppId = steamAppIdForGameId(gameId) ?: return false
|
||||
val args = String(cmdline).split('\u0000')
|
||||
|
||||
for (i in args.indices) {
|
||||
if (args[i] == "SteamLaunch") {
|
||||
val appId = args.getOrNull(i + 1)
|
||||
?.takeIf { it.startsWith("AppId=") }
|
||||
?.removePrefix("AppId=")
|
||||
if (appId == gameId) return true
|
||||
|
||||
if (appId == expectedAppId) return true
|
||||
}
|
||||
|
||||
if (args[i] == "-applaunch" &&
|
||||
args.getOrNull(i + 1) == expectedAppId
|
||||
) {
|
||||
return true
|
||||
}
|
||||
if (args[i] == "-applaunch" && args.getOrNull(i + 1) == gameId) return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,31 @@ class SteamGameProcessTest {
|
||||
assertFalse(SteamGameProcess.matches(cmdline("python3", "/proton", "waitforexitandrun", "game.exe"), "976310"))
|
||||
}
|
||||
|
||||
@Test fun shortcutCGameIdsMatchTheir32BitSteamAppId() {
|
||||
val cgameId = ((976310L shl 32) or 0x02000000L).toString()
|
||||
|
||||
assertTrue(
|
||||
SteamGameProcess.matches(
|
||||
cmdline("reaper", "SteamLaunch", "AppId=976310", "--", "proton", "waitforexitandrun", "game.exe"),
|
||||
cgameId,
|
||||
)
|
||||
)
|
||||
|
||||
assertTrue(
|
||||
SteamGameProcess.matches(
|
||||
cmdline("steam", "-applaunch", "976310"),
|
||||
cgameId,
|
||||
)
|
||||
)
|
||||
|
||||
assertFalse(
|
||||
SteamGameProcess.matches(
|
||||
cmdline("reaper", "SteamLaunch", "AppId=123", "--", "proton", "waitforexitandrun", "game.exe"),
|
||||
cgameId,
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Test fun runningLooksAtEveryProcess() {
|
||||
val proc = tmp.newFolder("proc")
|
||||
fun process(pid: String, vararg args: String) = File(proc, pid).apply { mkdirs() }.resolve("cmdline").writeBytes(cmdline(*args))
|
||||
|
||||
Reference in new issue
Block a user