mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-24 23:36:04 +00:00
Stores: an Epic game signs in however it is launched
Metalstorm started from the Steam client's own Play button came up as a guest: the exchange code was only minted when a launch began in the app. The compat tool now runs droiddeck-store-launch on every real launch. It finds the store launcher among Steam's arguments and, for an Epic game, asks the app through the session's stores/req and stores/resp folders, waiting up to five seconds. The app refreshes the token, mints the code and writes it into the game's own folder; the code never crosses the channel or the log. No answer and the game starts with its offline identity; a code file older than five minutes is dropped first. Every attempt logs epic launch id=... code=yes|no reason=...; a sign-in that ran out dims the Epic chip and brings back the sign-in card.
This commit is contained in:
1 parent
64d5f92391
commit
78e4cb8369
13 files changed
+406
-18
No files matched your search
@@ -60,6 +60,8 @@ object SessionFiles {
|
||||
"usr/local/bin/droiddeck-desktop-bookmarks" to "usr/local/bin/droiddeck-desktop-bookmarks",
|
||||
"usr/local/bin/droiddeck-steam-shim" to "usr/local/bin/droiddeck-steam-shim",
|
||||
"usr/local/bin/droiddeck-steam-shortcuts" to "usr/local/bin/droiddeck-steam-shortcuts",
|
||||
// Asks the app for an Epic game's sign-in code at launch, from the compat tool (StoreLaunchRequests).
|
||||
"usr/local/bin/droiddeck-store-launch" to "usr/local/bin/droiddeck-store-launch",
|
||||
"usr/local/bin/droiddeck-steam-games" to "usr/local/bin/droiddeck-steam-games",
|
||||
"usr/local/bin/droiddeck-pad-defaults" to "usr/local/bin/droiddeck-pad-defaults",
|
||||
// Flatpak: the bwrap stand-in, the store's helper and setup, and the front end's launcher.
|
||||
|
||||
@@ -325,6 +325,8 @@ class SessionService : Service() {
|
||||
killStragglers()
|
||||
SessionFiles.stage(this, root)
|
||||
com.droiddeck.launcher.agent.AgentGuest.reset(this)
|
||||
// The compat tool asks for an Epic game's sign-in code at every launch, the client's Play button included.
|
||||
com.droiddeck.launcher.stores.StoreLaunchRequests.start(this)
|
||||
|
||||
val sessionDir = openSessionFolder()
|
||||
val sessionLog = File(sessionDir, "session.log")
|
||||
@@ -1414,6 +1416,7 @@ class SessionService : Service() {
|
||||
pipTask = false
|
||||
SessionState.guestPid = -1
|
||||
com.droiddeck.launcher.agent.AgentGuest.stop()
|
||||
com.droiddeck.launcher.stores.StoreLaunchRequests.stop()
|
||||
runCatching { com.droiddeck.launcher.agent.AgentEnv.endSession(this) }
|
||||
.onFailure { Log.w(TAG, "clearing the agent's session environment", it) }
|
||||
releaseLocks()
|
||||
|
||||
@@ -74,22 +74,46 @@ object StoreLaunch {
|
||||
if (store != Store.EPIC) { onReady(); return }
|
||||
val app = context.applicationContext
|
||||
Thread({
|
||||
try {
|
||||
val game = StoreInstallRoot.gameFolders(app).firstOrNull { folder -> StoreGameSidecar.read(folder)?.let { it.store == store && it.id == id } == true }
|
||||
if (game != null) {
|
||||
val code = StoresState.backend(Store.EPIC)?.let { (it as? EpicLaunchSupport)?.exchangeCode(app) }
|
||||
val file = File(game, EPIC_CODE)
|
||||
if (code != null) { file.writeText(code); Log.i(TAG, "epic: exchange code ready for $id") }
|
||||
else { file.delete(); Log.w(TAG, "epic: no exchange code; launching offline") }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "epic prepare: ${e.message}")
|
||||
}
|
||||
epicCode(app, id)
|
||||
StoresState.post(onReady)
|
||||
}, "stores-launch-prep").start()
|
||||
}
|
||||
|
||||
/** The same, for a game the Games tab launches: a store's game by its [Library.SteamGame.source]. */
|
||||
/** What [epicCode] did, for the guest's request and the log: whether a code was written, and why not. */
|
||||
class CodeResult(val written: Boolean, val reason: String)
|
||||
|
||||
/**
|
||||
* Mints an Epic exchange code for the installed game [id] and writes it as [EPIC_CODE] into the
|
||||
* very folder its sidecar and launcher are in, where the launcher .bat reads and deletes it.
|
||||
* Blocking (network). Logs `epic launch id=<id> code=yes|no reason=...` - never the code. Every
|
||||
* launch path comes here: the Games tab and Stores before they launch, and the compat tool for
|
||||
* any launch, the Steam client's own Play button included ([StoreLaunchRequests]).
|
||||
*/
|
||||
fun epicCode(context: Context, id: String): CodeResult {
|
||||
val app = context.applicationContext
|
||||
val result = try {
|
||||
val folder = StoreInstallRoot.gameFolders(app).firstOrNull { f -> StoreGameSidecar.read(f)?.let { it.store == Store.EPIC && it.id == id } == true }
|
||||
if (folder == null) CodeResult(false, "not-installed")
|
||||
else {
|
||||
val file = File(folder, EPIC_CODE)
|
||||
file.delete()
|
||||
val support = StoresState.backend(Store.EPIC) as? EpicLaunchSupport
|
||||
val code = support?.exchangeCode(app)
|
||||
when {
|
||||
support == null -> CodeResult(false, "no-backend")
|
||||
code != null -> { file.writeText(code); CodeResult(true, "ok") }
|
||||
StoreAccounts.signedInAs(app, Store.EPIC) == null -> CodeResult(false, "signed-out")
|
||||
support.signInExpired(app) -> { StoresState.markSignInExpired(Store.EPIC); CodeResult(false, "sign-in-expired") }
|
||||
else -> CodeResult(false, "exchange-failed")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
CodeResult(false, "error-" + e.javaClass.simpleName)
|
||||
}
|
||||
Log.i(TAG, "epic launch id=$id code=${if (result.written) "yes" else "no"} reason=${result.reason}")
|
||||
return result
|
||||
}
|
||||
|
||||
fun prepare(context: Context, game: Library.SteamGame, onReady: () -> Unit) {
|
||||
val store = Store.byId(game.source)
|
||||
val id = game.storeId
|
||||
@@ -101,4 +125,6 @@ object StoreLaunch {
|
||||
/** What the Epic backend adds for launches: a short-lived exchange code for the signed-in account. */
|
||||
interface EpicLaunchSupport {
|
||||
fun exchangeCode(context: Context): String?
|
||||
/** True when the stored sign-in can no longer be refreshed: the user has to sign in again. */
|
||||
fun signInExpired(context: Context): Boolean
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.droiddeck.launcher.stores
|
||||
|
||||
import android.content.Context
|
||||
import android.os.FileObserver
|
||||
import android.util.Log
|
||||
import com.droiddeck.launcher.runtime.LinuxRuntime
|
||||
import java.io.File
|
||||
import org.json.JSONObject
|
||||
|
||||
/**
|
||||
* The app's half of droiddeck-store-launch: the compat tool asks, before Proton starts a store
|
||||
* game, for what the store needs at launch, through files under the session root's `stores/`
|
||||
* (`req/<id>.json` in, `resp/<id>.json` out, both written tmp + rename). Today that is an Epic
|
||||
* game's exchange code, which [StoreLaunch.epicCode] writes into the game's own folder; the answer
|
||||
* says only whether it did and why not. Watched while a session runs.
|
||||
*/
|
||||
object StoreLaunchRequests {
|
||||
private const val TAG = "StoreLaunchRequests"
|
||||
private val lock = Any()
|
||||
private var observer: FileObserver? = null
|
||||
|
||||
fun dir(context: Context) = File(LinuxRuntime.sessionRoot(context), "stores")
|
||||
|
||||
/** Before each session: nothing of the last one's requests; then watch for new ones. */
|
||||
fun start(context: Context) {
|
||||
val app = context.applicationContext
|
||||
synchronized(lock) {
|
||||
observer?.stopWatching()
|
||||
val root = dir(app)
|
||||
root.deleteRecursively()
|
||||
val req = File(root, "req").apply { mkdirs() }
|
||||
File(root, "resp").mkdirs()
|
||||
@Suppress("DEPRECATION")
|
||||
observer = object : FileObserver(req.path, MOVED_TO or CLOSE_WRITE) {
|
||||
override fun onEvent(event: Int, path: String?) {
|
||||
if (path == null || !path.endsWith(".json")) return
|
||||
val file = File(req, path)
|
||||
Thread({ answer(app, file) }, "store-launch-request").start()
|
||||
}
|
||||
}.also { it.startWatching() }
|
||||
}
|
||||
}
|
||||
|
||||
fun stop() {
|
||||
synchronized(lock) {
|
||||
observer?.stopWatching()
|
||||
observer = null
|
||||
}
|
||||
}
|
||||
|
||||
private fun answer(app: Context, file: File) {
|
||||
val request = try {
|
||||
JSONObject(file.readText()).also { file.delete() }
|
||||
} catch (e: Exception) {
|
||||
return // withdrawn already, or not complete yet (the rename brings it again)
|
||||
}
|
||||
val reply = when (request.optString("op")) {
|
||||
"epic-code" -> {
|
||||
val id = request.optString("id")
|
||||
if (id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-id")
|
||||
else StoreLaunch.epicCode(app, id).let { JSONObject().put("ok", true).put("code", it.written).put("reason", it.reason) }
|
||||
}
|
||||
else -> JSONObject().put("ok", false).put("reason", "unknown-op")
|
||||
}
|
||||
try {
|
||||
val resp = File(dir(app), "resp").apply { mkdirs() }
|
||||
val tmp = File(resp, file.name + ".tmp")
|
||||
tmp.writeText(reply.toString())
|
||||
tmp.renameTo(File(resp, file.name))
|
||||
} catch (e: Exception) {
|
||||
Log.w(TAG, "could not answer ${file.name}: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -118,6 +118,11 @@ object StoresState {
|
||||
|
||||
fun isSignedIn(store: Store): Boolean = accounts.containsKey(store)
|
||||
|
||||
/** Stores whose sign-in has run out (a launch could not get a code): their chip dims until the user signs in again. */
|
||||
val expired = mutableStateMapOf<Store, Boolean>()
|
||||
|
||||
fun markSignInExpired(store: Store) = post { expired[store] = true }
|
||||
|
||||
/** Installs that stopped before they finished; their Install button reads "Resume install". */
|
||||
var unfinished by mutableStateOf<List<StoreInstallRoot.Unfinished>>(emptyList())
|
||||
internal set
|
||||
@@ -149,6 +154,7 @@ object StoresState {
|
||||
}
|
||||
|
||||
fun signIn(context: Context, store: Store) {
|
||||
expired.remove(store)
|
||||
backends[store]?.signIn(context) ?: logLine("${store.label}: this build has no sign-in for it yet")
|
||||
}
|
||||
|
||||
|
||||
@@ -95,7 +95,8 @@ public final class EpicAuthClient {
|
||||
public static String getExchangeCode(String accessToken) {
|
||||
try {
|
||||
String resp = getRequest(EXCHANGE_URL, accessToken);
|
||||
if (resp == null) return null;
|
||||
// getRequest has logged the HTTP status; the code itself is never logged.
|
||||
if (resp == null) { Log.i(TAG, "exchange code: no answer"); return null; }
|
||||
return new JSONObject(resp).optString("code", null);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "exchange code failed: " + e.getClass().getSimpleName());
|
||||
|
||||
@@ -97,6 +97,9 @@ object EpicBackend : StoreBackend, EpicLaunchSupport {
|
||||
return EpicAuthClient.getExchangeCode(token)
|
||||
}
|
||||
|
||||
/** The access token is past its time and the refresh just failed: only a new sign-in helps. */
|
||||
override fun signInExpired(context: Context): Boolean = EpicCredentialStore.expired(context)
|
||||
|
||||
private class InstallJob(val app: Context, val item: CatalogItem, val namespace: String, val catalogItemId: String, val folder: File) : DownloadQueue.DownloadJob {
|
||||
private val cancelled = AtomicBoolean(false)
|
||||
// On a card the in-flight chunks go to the app's cache instead: the card's write rate paces
|
||||
|
||||
@@ -36,6 +36,12 @@ object EpicCredentialStore {
|
||||
|
||||
/** A valid access token, refreshed when within five minutes of expiry; null when signed out or the refresh failed. */
|
||||
@JvmStatic
|
||||
/** The stored access token is past its expiry (a refresh, if one was tried, did not replace it). */
|
||||
fun expired(context: Context): Boolean {
|
||||
val creds = load(context) ?: return false
|
||||
return creds.expiresAt in 1 until System.currentTimeMillis()
|
||||
}
|
||||
|
||||
fun getValidAccessToken(context: Context): String? {
|
||||
val creds = load(context) ?: return null
|
||||
if (creds.expiresAt - System.currentTimeMillis() < 5L * 60L * 1000L && creds.refreshToken != null) {
|
||||
|
||||
@@ -163,7 +163,8 @@ internal fun StoresPage(s: FrontEndState, a: FrontEndActions, modifier: Modifier
|
||||
when {
|
||||
chip == DOWNLOADS -> StoresDownloadsPane(s, a)
|
||||
store == null -> {}
|
||||
!StoresState.isSignedIn(store) -> Rise(1) { SignInCard(store) }
|
||||
// Signed out, or a sign-in that ran out (a launch could not get its code): sign in again.
|
||||
!StoresState.isSignedIn(store) || StoresState.expired[store] == true -> Rise(1) { SignInCard(store) }
|
||||
// One focus group, so the pad walks the page's own controls - back, the hero's
|
||||
// actions, the cards - and reaches the rail only with Left from them.
|
||||
openGame != null -> Column(Modifier.fillMaxWidth().focusGroup()) { StoreGameDetail(store, openGame!!, s, a, onBack = { closeGame() }) }
|
||||
@@ -191,9 +192,9 @@ private fun StoreChips(selected: String, active: Int, onPick: (String) -> Unit,
|
||||
label = store.shortLabel, on = selected == store.id, key = store.id, modifier = Modifier.weight(1f),
|
||||
lead = {
|
||||
val c = sourceColours(store.id)
|
||||
Box(Modifier.size(12.dp).clip(CircleShape).background(c.dot).alpha(if (StoresState.isSignedIn(store)) 1f else 0.35f))
|
||||
Box(Modifier.size(12.dp).clip(CircleShape).background(c.dot).alpha(if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) 1f else 0.35f))
|
||||
},
|
||||
description = store.label + if (StoresState.isSignedIn(store)) "" else " " + stringResource(R.string.stores_chip_signed_out),
|
||||
description = store.label + if (StoresState.isSignedIn(store) && StoresState.expired[store] != true) "" else " " + stringResource(R.string.stores_chip_signed_out),
|
||||
) { onPick(store.id) }
|
||||
}
|
||||
StoreChip(
|
||||
|
||||
Reference in new issue
Block a user