mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 13:26:02 +00:00
feat(agent): launch and quit store games, list the library, and run Xbox and cloud save commands from droiddeckctl (#2860)
This commit is contained in:
1 parent
786166ba1a
commit
1456c0529d
6 files changed
+255
-8
No files matched your search
@@ -76,6 +76,9 @@ class AgentBridgeProvider : ContentProvider() {
|
||||
"stop" -> stop(context)
|
||||
"resume" -> resume(context)
|
||||
"launch" -> launch(context, request)
|
||||
"library" -> AgentStores.library(context, request)
|
||||
"xodus" -> AgentStores.xodus(context, request)
|
||||
"cloud" -> AgentStores.cloud(context, request)
|
||||
"focus" -> guest(context, JSONObject().put("kind", "focus"), 15_000)
|
||||
"quit" -> guest(context, JSONObject().put("kind", "quit").put("appId", request.opt("appId"))
|
||||
.put("timeout", request.optDouble("timeout", 15.0)), timeoutMs(request, 15.0, 30_000))
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.droiddeck.launcher.agent
|
||||
|
||||
import android.content.Context
|
||||
import com.droiddeck.launcher.frontend.LibraryCache
|
||||
import com.droiddeck.launcher.stores.CloudSaves
|
||||
import com.droiddeck.launcher.stores.Store
|
||||
import com.droiddeck.launcher.stores.xbox.Xodus
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.InputStream
|
||||
import java.util.concurrent.TimeUnit
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
|
||||
internal object AgentStores {
|
||||
private const val OUTPUT_CAP = 160 * 1024
|
||||
|
||||
fun library(context: Context, request: JSONObject): JSONObject {
|
||||
val query = request.optString("query").trim()
|
||||
val source = request.optString("source").trim()
|
||||
val games = JSONArray()
|
||||
LibraryCache.load(context)
|
||||
.filter { (query.isEmpty() || it.name.contains(query, ignoreCase = true)) && (source.isEmpty() || it.source.equals(source, ignoreCase = true)) }
|
||||
.forEach {
|
||||
games.put(JSONObject().put("id", it.gameIdString).put("name", it.name).put("source", it.source)
|
||||
.put("storeId", it.storeId ?: JSONObject.NULL).put("folder", it.gameFiles?.path ?: JSONObject.NULL))
|
||||
}
|
||||
return JSONObject().put("ok", true).put("command", "library").put("games", games)
|
||||
}
|
||||
|
||||
fun xodus(context: Context, request: JSONObject): JSONObject {
|
||||
AgentAccess.requireCommands(context)
|
||||
val list = request.optJSONArray("args")
|
||||
val args = if (list == null) emptyList() else List(list.length()) { list.optString(it) }
|
||||
if (args.isEmpty() || args.any { it.isEmpty() }) throw AgentException("INVALID_REQUEST", "xodus needs its arguments")
|
||||
val timeoutMs = (request.optDouble("timeout", 120.0) * 1000).toLong().coerceIn(1_000L, 900_000L)
|
||||
return Xodus.start(context.applicationContext, args).use { child ->
|
||||
val out = Tail(child.process.inputStream)
|
||||
val err = Tail(child.process.errorStream)
|
||||
val finished = child.process.waitFor(timeoutMs, TimeUnit.MILLISECONDS)
|
||||
if (!finished) child.close()
|
||||
JSONObject().put("ok", true).put("command", "xodus").put("timedOut", !finished)
|
||||
.put("exit", if (finished) child.process.exitValue() else JSONObject.NULL)
|
||||
.put("stdout", out.text()).put("stderr", err.text())
|
||||
}
|
||||
}
|
||||
|
||||
fun cloud(context: Context, request: JSONObject): JSONObject {
|
||||
AgentAccess.requireCommands(context)
|
||||
val store = Store.byId(request.optString("store"))?.takeIf(CloudSaves::supports)
|
||||
?: throw AgentException("INVALID_REQUEST", "store must be one with cloud saves")
|
||||
val id = request.optString("id").ifEmpty { throw AgentException("INVALID_REQUEST", "cloud needs a game id") }
|
||||
val force = request.optBoolean("force")
|
||||
val result = when (val direction = request.optString("direction")) {
|
||||
"down" -> CloudSaves.download(context.applicationContext, store, id, force)
|
||||
"up" -> CloudSaves.upload(context.applicationContext, store, id, force)
|
||||
else -> throw AgentException("INVALID_REQUEST", "direction '$direction' is not down or up")
|
||||
}
|
||||
return JSONObject().put("ok", true).put("command", "cloud").put("result", result.result).put("files", result.files)
|
||||
.put("bytes", result.bytes).put("reason", result.reason).put("conflicts", result.conflicts).put("detail", result.detail)
|
||||
}
|
||||
|
||||
private class Tail(stream: InputStream) {
|
||||
private val kept = ByteArrayOutputStream()
|
||||
private var dropped = false
|
||||
private val reader = Thread({
|
||||
val buffer = ByteArray(8192)
|
||||
stream.use { input ->
|
||||
while (true) {
|
||||
val n = runCatching { input.read(buffer) }.getOrDefault(-1)
|
||||
if (n < 0) break
|
||||
synchronized(kept) {
|
||||
kept.write(buffer, 0, n)
|
||||
if (kept.size() > OUTPUT_CAP * 2) {
|
||||
val bytes = kept.toByteArray()
|
||||
kept.reset()
|
||||
kept.write(bytes, bytes.size - OUTPUT_CAP, OUTPUT_CAP)
|
||||
dropped = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, "agent-xodus-output").apply { isDaemon = true; start() }
|
||||
|
||||
fun text(): String {
|
||||
reader.join(2_000)
|
||||
val bytes = synchronized(kept) { kept.toByteArray() }
|
||||
val tail = if (bytes.size > OUTPUT_CAP) bytes.copyOfRange(bytes.size - OUTPUT_CAP, bytes.size) else bytes
|
||||
return (if (dropped || bytes.size > OUTPUT_CAP) "...\n" else "") + String(tail, Charsets.UTF_8)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -708,10 +708,10 @@ internal object EnhancedSessionLogShare {
|
||||
}
|
||||
val logs = LinuxRuntime.logDir(context)
|
||||
for ((name, root) in listOf(SessionLogShare.STORES_DIR to File(logs, SessionLogShare.STORES_DIR), SessionPaths.TOOLS_DIR to File(logs, SessionPaths.TOOLS_DIR))) {
|
||||
val scan = directFiles(root, remaining(maxFiles, out.size))
|
||||
val scan = directFiles(root, STORE_LOG_SCAN)
|
||||
truncated = truncated || scan.truncated
|
||||
partial = partial || scan.partial
|
||||
scan.files.asSequence().filter { recent(it) }
|
||||
scan.files.filter { recent(it) }.sortedByDescending { it.lastModified() }.take(remaining(maxFiles, out.size))
|
||||
.forEach { add(it, root, "$name/${it.name}", name) }
|
||||
}
|
||||
if (legacy) {
|
||||
@@ -965,6 +965,7 @@ internal object EnhancedSessionLogShare {
|
||||
"events.jsonl", "memory.csv", "capture.json", "crash.log",
|
||||
)
|
||||
private const val RECENT_MILLIS = 7L * 24 * 60 * 60 * 1000
|
||||
private const val STORE_LOG_SCAN = 512
|
||||
private const val COPY_BUFFER_BYTES = 64 * 1024
|
||||
private const val TRACE_SCAN_BYTES = 16L * 1024 * 1024
|
||||
private const val LEGACY_LOG_MAX_BYTES = 8L * 1024 * 1024
|
||||
|
||||
Reference in new issue
Block a user