mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-06 13:05:47 +00:00
perf(session): check the known game process first in the game exit watcher (#3306)
Check the known game process first when watching for the game to exit
This commit is contained in:
1 parent
25923966eb
commit
6877a5e8e6
3 files changed
+28
-7
No files matched your search
@@ -1727,6 +1727,7 @@ class SessionService : Service() {
|
||||
var startupWasSuspended = false
|
||||
var seen = false
|
||||
var goneSince = 0L
|
||||
var gamePid: String? = null
|
||||
while (token == gameExitWatchToken && gen == sessionGen && SessionState.running && !SessionState.stopRequested) {
|
||||
val now = android.os.SystemClock.elapsedRealtime()
|
||||
val startupSuspended = SessionState.suspended || SessionState.suspending || SessionState.phase != SessionPhase.READY
|
||||
@@ -1742,7 +1743,8 @@ class SessionService : Service() {
|
||||
lastStartupCheck = now
|
||||
}
|
||||
}
|
||||
if (steamGameRunning(gameId)) {
|
||||
gamePid = SteamGameProcess.runningPid(gameId, gamePid)
|
||||
if (gamePid != null) {
|
||||
if (!seen) {
|
||||
Log.i(TAG, "game $gameId started; tracking its exit")
|
||||
mainHandler.post {
|
||||
|
||||
@@ -55,12 +55,15 @@ object SteamGameProcess {
|
||||
return false
|
||||
}
|
||||
|
||||
fun running(gameId: String, proc: File = File("/proc")): Boolean =
|
||||
proc.listFiles()?.any { p ->
|
||||
p.name.all(Char::isDigit) &&
|
||||
runCatching { File(p, "cmdline").readBytes() }
|
||||
.getOrNull()?.let { matches(it, gameId) } == true
|
||||
} == true
|
||||
fun running(gameId: String, proc: File = File("/proc")): Boolean = runningPid(gameId, null, proc) != null
|
||||
|
||||
fun runningPid(gameId: String, known: String?, proc: File = File("/proc")): String? {
|
||||
if (known != null && runs(File(proc, known), gameId)) return known
|
||||
return proc.listFiles()?.firstOrNull { p -> p.name.all(Char::isDigit) && runs(p, gameId) }?.name
|
||||
}
|
||||
|
||||
private fun runs(process: File, gameId: String): Boolean =
|
||||
runCatching { File(process, "cmdline").readBytes() }.getOrNull()?.let { matches(it, gameId) } == true
|
||||
|
||||
// A front end that resumes a player by launching the same game again would otherwise have Steam
|
||||
// start it a second time, which Steam answers with "Game already running".
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
@@ -89,6 +91,20 @@ class SteamGameProcessTest {
|
||||
assertTrue(SteamGameProcess.running("351710", proc))
|
||||
}
|
||||
|
||||
@Test fun aKnownGameProcessIsCheckedBeforeEveryOtherProcess() {
|
||||
val proc = tmp.newFolder("proc")
|
||||
fun process(pid: String, vararg args: String) = File(proc, pid).apply { mkdirs() }.resolve("cmdline").writeBytes(cmdline(*args))
|
||||
process("100", "reaper", "SteamLaunch", "AppId=1")
|
||||
process("200", "reaper", "SteamLaunch", "AppId=1")
|
||||
assertEquals("200", SteamGameProcess.runningPid("1", "200", proc))
|
||||
File(proc, "200/cmdline").writeBytes(cmdline("bash"))
|
||||
assertEquals("100", SteamGameProcess.runningPid("1", "200", proc))
|
||||
assertEquals("100", SteamGameProcess.runningPid("1", "999", proc))
|
||||
File(proc, "100").deleteRecursively()
|
||||
assertNull(SteamGameProcess.runningPid("1", "100", proc))
|
||||
assertNull(SteamGameProcess.runningPid("2", null, proc))
|
||||
}
|
||||
|
||||
@Test fun onlyTheRunningCurrentGameCountsAsAlreadyRunning() {
|
||||
val proc = tmp.newFolder("proc-relaunch")
|
||||
File(proc, "200").apply { mkdirs() }.resolve("cmdline")
|
||||
|
||||
Reference in new issue
Block a user