From 8f36eb7f548998969854d23a6af4668af3090ced Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Mon, 12 Oct 2026 16:40:06 -0400 Subject: [PATCH] Add opt-in GOG achievements through Comet (#651) --- .gitattributes | 3 + .github/workflows/build.yml | 9 +- .../com/droiddeck/launcher/SessionActivity.kt | 46 ++-- .../droiddeck/launcher/agent/AgentGuest.kt | 9 + .../session/SessionAchievementGame.kt | 11 + .../launcher/session/SessionFiles.kt | 2 +- .../launcher/session/SessionService.kt | 5 +- .../stores/AchievementNotifications.kt | 50 +++++ .../launcher/stores/StoreAchievements.kt | 5 + .../launcher/stores/StoreGameSidecar.kt | 13 +- .../launcher/stores/StoreInstalls.kt | 2 +- .../launcher/stores/StoreLaunchRequests.kt | 2 + .../droiddeck/launcher/stores/StoresState.kt | 3 +- .../droiddeck/launcher/stores/gog/GogAuth.kt | 30 ++- .../droiddeck/launcher/stores/gog/GogComet.kt | 189 ++++++++++++++++ .../ui/AchievementNotificationOverlay.kt | 96 +++++++++ .../droiddeck/launcher/ui/FrontEndSetup.kt | 23 +- .../launcher/ui/StoreAchievementsUi.kt | 6 +- .../droiddeck/launcher/ui/StoresDownloads.kt | 8 + app/src/main/res/values/strings.xml | 10 + .../session/SessionAchievementGameTest.kt | 29 +++ .../stores/AchievementNotificationsTest.kt | 55 +++++ .../launcher/stores/GogCometGenerationTest.kt | 34 +++ .../launcher/stores/StoreGameSidecarTest.kt | 10 + docs/development/comet-integration-plan.md | 159 ++++++++++++++ tools/build_local.sh | 1 + tools/comet/LICENSE | 202 ++++++++++++++++++ tools/comet/README.md | 24 +++ tools/comet/build-local.sh | 18 ++ tools/comet/build.sh | 24 +++ tools/comet/droiddeck.patch | 174 +++++++++++++++ .../usr/local/bin/droiddeck-store-launch | 75 +++++++ .../overlay/usr/local/bin/steam-compatibility | 4 +- tools/release/runtime_inputs.py | 4 +- tools/tests/test_store_launch.py | 74 ++++++- 35 files changed, 1364 insertions(+), 45 deletions(-) create mode 100644 .gitattributes create mode 100644 app/src/main/java/com/droiddeck/launcher/session/SessionAchievementGame.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/AchievementNotifications.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt create mode 100644 app/src/main/java/com/droiddeck/launcher/ui/AchievementNotificationOverlay.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/session/SessionAchievementGameTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/AchievementNotificationsTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/stores/GogCometGenerationTest.kt create mode 100644 docs/development/comet-integration-plan.md create mode 100644 tools/comet/LICENSE create mode 100644 tools/comet/README.md create mode 100644 tools/comet/build-local.sh create mode 100644 tools/comet/build.sh create mode 100644 tools/comet/droiddeck.patch diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..0e4a346586 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +# Vendored files preserve upstream license spacing and unified-diff context lines. +tools/comet/LICENSE whitespace=-blank-at-eof +tools/comet/droiddeck.patch whitespace=-blank-at-eol,-blank-at-eof diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 62fda2a5e0..2fc4823e43 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -186,7 +186,7 @@ jobs: uses: actions/cache@v4 with: path: app/src/main/assets/linuxfs - key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/build-x86-preloads.sh', 'tools/linuxfs/fex/*', 'tools/linuxfs/clipboard/*.c', 'tools/linuxfs/ssbs/*.c', 'tools/proot/fastpath/*.c', 'tools/linuxfs/overlay/usr/local/bin/*', 'tools/linuxfs/overlay/usr/bin/**', 'tools/linuxfs/desktop/*', 'tools/gamescope/release.env', 'tools/wlroots/release.env', 'tools/linuxfs/uruntime.env', 'tools/linuxfs/licenses/*', 'tools/mangoapp/*', 'tools/msitools/**', 'tools/linuxfs/debian-image.env') }} + key: preloads-${{ env.PRELOAD_CACHE_VERSION }}-${{ hashFiles('app/src/main/cpp/fakeinput_steam.cpp', 'tools/linuxfs/preload/*.c', 'tools/linuxfs/build-x86-preloads.sh', 'tools/linuxfs/fex/*', 'tools/linuxfs/clipboard/*.c', 'tools/linuxfs/ssbs/*.c', 'tools/proot/fastpath/*.c', 'tools/linuxfs/overlay/usr/local/bin/*', 'tools/linuxfs/overlay/usr/bin/**', 'tools/linuxfs/desktop/*', 'tools/gamescope/release.env', 'tools/wlroots/release.env', 'tools/linuxfs/uruntime.env', 'tools/linuxfs/licenses/*', 'tools/mangoapp/*', 'tools/msitools/**', 'tools/comet/**', 'tools/linuxfs/debian-image.env') }} - name: Build libfakeinput.so and libblsession.so (glibc aarch64) if: steps.preloads.outputs.cache-hit != 'true' @@ -450,6 +450,13 @@ jobs: restore-keys: | cxx-${{ env.NDK_VERSION }}- + - name: Build GOG Galaxy companion + if: steps.preloads.outputs.cache-hit != 'true' + run: | + rustup toolchain install 1.90.0 --profile minimal + rustup target add --toolchain 1.90.0 aarch64-unknown-linux-gnu + RUSTUP_TOOLCHAIN=1.90.0 bash tools/comet/build.sh + - name: Build id: build env: diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 6e0847bede..8d702631d2 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -562,7 +562,6 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { androidx.compose.runtime.LaunchedEffect(drawerOpen) { if (drawerOpen) { syncLossless() - refreshAchievementsInFront() padBridge?.releaseAll() androidx.compose.runtime.withFrameNanos { } val requested = sessionOverlay.requestFocus() @@ -571,6 +570,15 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { releaseDrawerDirection() } } + LaunchedEffect(drawerOpen, achievementsOpen, AgentGuest.focusRevision, StoreAchievements.revision) { + if (!drawerOpen && !achievementsOpen) return@LaunchedEffect + val found = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { + runCatching { resolveAchievementsInFront() } + .onFailure { Log.w(TAG, "achievements in front: ${it.javaClass.simpleName}") }.getOrNull() + } + achievementsInFront = found + if (found == null) achievementsOpen = false + } // The PC keyboard takes it too, for moving over the keys; the drawer opens over it. androidx.compose.runtime.LaunchedEffect(pcKeyboardOpen) { if (pcKeyboardOpen) padBridge?.releaseAll() } if (pcKeyboardOpen) com.droiddeck.launcher.ui.PcKeyboard( @@ -758,6 +766,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { achievementsInFront?.takeIf { achievementsOpen }?.let { inFront -> SessionAchievements(inFront.game, inFront.title, onClose = ::closeAchievements) } + com.droiddeck.launcher.ui.AchievementNotificationOverlay() if (SessionState.suspended) SessionPausedOverlay( title = pausedTitle(), onResume = { SessionService.resume(this@SessionActivity) }, @@ -798,35 +807,26 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { padBridge?.triggerQam() } - /** Re-reads so the drawer's count picks up the panel's fresh list. */ - private fun closeAchievements() { - achievementsOpen = false - refreshAchievementsInFront() - } + private fun closeAchievements() { achievementsOpen = false } - private fun refreshAchievementsInFront() { - if (SessionState.mode != SessionService.MODE_STEAM) { achievementsInFront = null; return } - Thread({ - val found = runCatching { resolveAchievementsInFront() }.onFailure { Log.w(TAG, "achievements in front: ${it.javaClass.simpleName}") }.getOrNull() - uiHandler.post { achievementsInFront = found } - }, "achievements-in-front").start() - } - - /** Steam's focused game (a Steam title means none), else the last store launch, else the session's own game. */ + /** A live game window only; Steam's overlay may be in front of that window. */ private fun resolveAchievementsInFront(): AchievementsInFront? { + if (!SessionState.running || SessionState.mode != SessionService.MODE_STEAM) return null + val snapshot = AgentGuest.focus(this) ?: return null val games = LibraryCache.load(this) - fun storeOf(g: Library.SteamGame) = Store.byId(g.source)?.let { st -> g.storeId?.let { st to it } } val byKey = games.mapNotNull { g -> g.profileKey.toLongOrNull()?.let { it to g } }.toMap() - val focused = runCatching { AgentGuest.activeSteamAppId(this, byKey.keys) }.getOrNull()?.let(byKey::get) - val url = SessionState.steamUrl?.substringAfter("rungameid/", "")?.takeIf { it.isNotEmpty() } - val (store, id) = (if (focused != null) storeOf(focused) - else StoreLaunchRequests.lastLaunched ?: games.firstOrNull { it.gameIdString == url }?.let(::storeOf)) - ?: return null + val windows = snapshot.optJSONArray("focusableApps")?.let { list -> List(list.length()) { list.optLong(it) } }.orEmpty() + val focusedId = snapshot.optLong("focusedApp").takeUnless { snapshot.isNull("focusedApp") } + val appId = com.droiddeck.launcher.session.SessionAchievementGame.activeAppId(focusedId, windows, byKey.keys) ?: return null + val focused = byKey[appId] ?: return null + val store = Store.byId(focused.source) ?: return null + val id = focused.storeId ?: return null val source = StoreAchievements.forSource(store.id) ?: return null val sidecar = StoreGameSidecar.find(this, store, id) ?: return null val game = StoreAchievements.Game.of(sidecar) - val saved = source.cached(this, game)?.takeIf { it.isNotEmpty() } - return AchievementsInFront(game, sidecar.title, saved?.let { l -> l.count { it.unlocked } to l.size }) + val list = source.cached(this, game) ?: (source.fetch(this, game) as? StoreAchievements.Result.Found)?.list ?: return null + if (list.isEmpty()) return null + return AchievementsInFront(game, sidecar.title, list.count { it.unlocked } to list.size) } private fun togglePcKeyboard() { diff --git a/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt b/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt index e0c0aed239..c572e943d2 100644 --- a/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt +++ b/app/src/main/java/com/droiddeck/launcher/agent/AgentGuest.kt @@ -1,6 +1,11 @@ package com.droiddeck.launcher.agent import android.content.Context +import android.os.Handler +import android.os.Looper +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.setValue import android.os.FileObserver import android.util.Log import com.droiddeck.launcher.runtime.LinuxRuntime @@ -16,6 +21,9 @@ import java.util.UUID * whatever runs does so inside the live session with its own environment. */ object AgentGuest { + private val main by lazy { Handler(Looper.getMainLooper()) } + var focusRevision by mutableIntStateOf(0) + private set private const val TAG = "AgentGuest" private const val POLL_MS = 50L private val lock = Any() @@ -70,6 +78,7 @@ object AgentGuest { if (key == lastFocusKey) return lastFocusKey = key } + main.post { focusRevision++ } SessionEvents.record("focus.changed", fields + ("seq" to snapshot.optInt("seq"))) } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionAchievementGame.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionAchievementGame.kt new file mode 100644 index 0000000000..62991bd8eb --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionAchievementGame.kt @@ -0,0 +1,11 @@ +package com.droiddeck.launcher.session + +/** An active game needs a live window; a launch URL or stale Steam base layer is insufficient. */ +internal object SessionAchievementGame { + fun activeAppId(focused: Long?, windows: List, installed: Set): Long? { + if (focused != null && focused > 0 && focused != 769L) { + return focused.takeIf { it in windows && it in installed } + } + return windows.filter { it != 769L && it in installed }.distinct().singleOrNull() + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt index 9bc9fc1955..4d2c1c3916 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionFiles.kt @@ -140,7 +140,7 @@ object SessionFiles { "usr/local/bin/gamescope" to "usr/local/bin/gamescope", "usr/local/lib/droiddeck/uruntime" to "usr/local/lib/droiddeck/uruntime", "usr/local/share/licenses/uruntime/LICENSE" to "usr/local/share/licenses/uruntime/LICENSE", - ) + mangoapp + msitools.map { it to it } + fexPreloads).filter { (asset, _) -> + ) + mangoapp + listOf("comet", "GalaxyCommunication.exe", "update-permissions.exe", "LICENSE", "version").map { "usr/local/lib/droiddeck-comet/$it" to "usr/local/lib/droiddeck-comet/$it" } + msitools.map { it to it } + fexPreloads).filter { (asset, _) -> val dir = asset.substringBeforeLast('/') runCatching { context.assets.list("linuxfs/$dir")?.contains(asset.substringAfterLast('/')) == true }.getOrDefault(false) } diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index 0622b79e28..5802b563e4 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -334,6 +334,7 @@ class SessionService : Service() { 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.gog.GogComet.begin() com.droiddeck.launcher.stores.StoreLaunchRequests.start(this) val sessionDir = openSessionFolder() @@ -1540,6 +1541,7 @@ class SessionService : Service() { } updateDownloadMonitoring() val stoppedGen = sessionGen + val stoppedSession = SessionState.sessionId SessionState.stopRequested = false SessionEvents.record("guest.exited", mapOf("status" to status)) GpuClockPin.stop(this) @@ -1586,13 +1588,14 @@ class SessionService : Service() { if (prootPid > 1 || auxiliary.isNotEmpty()) { Thread({ if (prootPid > 1 && steamClientMayRun) askSteamToExit(prootPid) + com.droiddeck.launcher.stores.gog.GogComet.end(stoppedSession) auxiliary.forEach { (pid, started) -> teardown(pid, started) } if (prootPid > 1) teardown(prootPid) killGuestLeftovers() finishSessionStop(status, stoppedGen) }, "session-teardown").start() } else { - finishSessionStop(status, stoppedGen) + Thread({ com.droiddeck.launcher.stores.gog.GogComet.end(stoppedSession); finishSessionStop(status, stoppedGen) }, "gog-session-stop").start() } } if (prootPid > 1) acquireLocks() diff --git a/app/src/main/java/com/droiddeck/launcher/stores/AchievementNotifications.kt b/app/src/main/java/com/droiddeck/launcher/stores/AchievementNotifications.kt new file mode 100644 index 0000000000..818bd0f37a --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/AchievementNotifications.kt @@ -0,0 +1,50 @@ +package com.droiddeck.launcher.stores + +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import org.json.JSONObject + +/** Session-owned unlocks; history reads and upload acknowledgements never create notifications. */ +object AchievementNotifications { + data class Unlock(val client: String, val id: String, val name: String, val description: String, val icon: String?, val game: String) + + private var session: String? = null + private val seen = HashSet() + var pending by mutableStateOf>(emptyList()) + private set + + fun begin(owner: String?) = StoresState.post { + session = owner + seen.clear() + pending = emptyList() + } + + fun end(owner: String?) = StoresState.post { + if (session == owner) { + session = null + seen.clear() + pending = emptyList() + } + } + + fun unlock(owner: String, unlock: Unlock) = StoresState.post { + if (session == owner && seen.add("${unlock.client}:${unlock.id}")) { + pending = (pending + unlock).take(16) + } + } + + fun dismiss(unlock: Unlock) { + if (pending.firstOrNull() === unlock) pending = pending.drop(1) + } + + /** Only presentation fields from the owned daemon, bounded before entering Compose. */ + fun parse(event: JSONObject, game: String): Unlock? { + val client = event.optString("client_id") + val id = event.optString("id") + val name = event.optString("name").trim() + if (!client.matches(Regex("[0-9]{1,32}")) || id.isBlank() || id.length > 256 || name.isEmpty()) return null + val icon = event.optString("icon").takeIf { it.length <= 2048 && it.startsWith("https://") } + return Unlock(client, id, name.take(256), event.optString("description").take(1024), icon, game.take(256)) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreAchievements.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreAchievements.kt index 3d79423ab9..9c1eb5fdc2 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreAchievements.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreAchievements.kt @@ -2,6 +2,8 @@ package com.droiddeck.launcher.stores import android.content.Context import android.util.Log +import androidx.compose.runtime.getValue +import androidx.compose.runtime.setValue import com.droiddeck.launcher.stores.epic.EpicAchievements import com.droiddeck.launcher.stores.gog.GogAchievements import org.json.JSONArray @@ -94,6 +96,9 @@ abstract class StoreAchievements(val source: String, val label: String) { companion object { private const val TAG = "StoreAchievements" + var revision by androidx.compose.runtime.mutableIntStateOf(0) + private set + fun invalidate() { revision++ } private val all: List by lazy { listOf(GogAchievements, EpicAchievements) } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt index 84b2bdf822..149b6c4557 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreGameSidecar.kt @@ -43,6 +43,7 @@ class StoreGameSidecar( val epic: EpicOptions = EpicOptions(), /** GOG / Epic cloud saves for this game: down before a launch, up after it (the game card's switch). */ val cloud: Boolean = true, + val achievements: Boolean = true, ) { val isInstalled: Boolean get() = state == STATE_INSTALLED @@ -53,8 +54,8 @@ class StoreGameSidecar( exe: String = this.exe, launcher: String? = this.launcher, args: List = this.args, env: Map = this.env, installVersion: String = this.installVersion, installedAt: Long = this.installedAt, cover: String? = this.cover, hero: String? = this.hero, - extra: Map = this.extra, state: String = this.state, epic: EpicOptions = this.epic, cloud: Boolean = this.cloud, - ) = StoreGameSidecar(store, id, title, exe, launcher, args, env, installVersion, installedAt, cover, hero, extra, state, epic, cloud) + extra: Map = this.extra, state: String = this.state, epic: EpicOptions = this.epic, cloud: Boolean = this.cloud, achievements: Boolean = this.achievements, + ) = StoreGameSidecar(store, id, title, exe, launcher, args, env, installVersion, installedAt, cover, hero, extra, state, epic, cloud, achievements) fun toJson(): JSONObject = JSONObject().apply { put("version", VERSION) @@ -65,6 +66,7 @@ class StoreGameSidecar( if (state != STATE_INSTALLED) put("state", state) if (store == Store.EPIC) put("epic", JSONObject().put("eos", epic.eos).put("offline", epic.offline)) if (store != Store.AMAZON) put("cloud", cloud) + if (store == Store.GOG) put("achievements", achievements) if (!launcher.isNullOrEmpty()) put("launcher", launcher) if (args.isNotEmpty()) put("args", JSONArray(args)) if (env.isNotEmpty()) put("env", JSONObject(env as Map<*, *>)) @@ -116,6 +118,12 @@ class StoreGameSidecar( return read(folder)?.takeIf { it.cloud == on } } + fun updateAchievements(folder: File, on: Boolean): StoreGameSidecar? { + val current = read(folder) ?: return null + current.copy(achievements = on).write(folder) + return read(folder)?.takeIf { it.achievements == on } + } + /** The sidecar in [folder], or null when there is none or it cannot be read. */ fun read(folder: File): StoreGameSidecar? { val f = file(folder) @@ -153,6 +161,7 @@ class StoreGameSidecar( // An "overlay" (or "v") field from an earlier build is read past: there is no overlay. epic = o.optJSONObject("epic")?.let { e -> EpicOptions(eos = e.optBoolean("eos", true), offline = e.optBoolean("offline", false)) } ?: EpicOptions(), cloud = o.optBoolean("cloud", true), + achievements = o.optBoolean("achievements", true), ) } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt index d1a6b15b89..97a6c90321 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreInstalls.kt @@ -45,7 +45,7 @@ object StoreInstalls { onStep?.invoke(0, STEPS) // The user's launch choices survive an update or a repair. val before = StoreGameSidecar.read(folder) - val finished = StoreLaunch.writeLauncher(folder, sidecar.copy(state = StoreGameSidecar.STATE_INSTALLED, epic = before?.epic ?: sidecar.epic, cloud = before?.cloud ?: sidecar.cloud)) + val finished = StoreLaunch.writeLauncher(folder, sidecar.copy(state = StoreGameSidecar.STATE_INSTALLED, epic = before?.epic ?: sidecar.epic, cloud = before?.cloud ?: sidecar.cloud, achievements = before?.achievements ?: sidecar.achievements)) finished.write(folder) if (StoreGameSidecar.read(folder)?.isInstalled != true) throw java.io.IOException("the install record could not be written in ${folder.name}") onStep?.invoke(1, STEPS) diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt index 3ef779ee99..03864d99cb 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoreLaunchRequests.kt @@ -59,6 +59,8 @@ object StoreLaunchRequests { return // withdrawn already, or not complete yet (the rename brings it again) } val reply = when (request.optString("op")) { + "gog-achievements" -> runCatching { com.droiddeck.launcher.stores.gog.GogComet.prepare(app, request.optString("id")) } + .getOrElse { JSONObject().put("ok", false).put("ready", false).put("reason", "service-unavailable") } "epic-code" -> { val id = request.optString("id") if (id.isEmpty()) JSONObject().put("ok", false).put("reason", "no-id") diff --git a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt index 20fd017120..4f1cb172c1 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt @@ -174,7 +174,8 @@ object StoresState { val app = context.applicationContext Thread({ backends[store]?.signOut(app) - StoreAccounts.clear(app, store) + if (store == Store.GOG) com.droiddeck.launcher.stores.gog.GogComet.replaceAccount { StoreAccounts.clear(app, store) } + else StoreAccounts.clear(app, store) main.post { accounts.remove(store); library.remove(store); status.remove(store); problems.remove(store) logLine("${store.label}: signed out") diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt index 56c7659b38..43191105e4 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogAuth.kt @@ -33,7 +33,12 @@ object GogAuth { fun isSignedIn(context: Context): Boolean = StoreAccounts.read(context, Store.GOG)?.optString("access_token", "")?.isNotEmpty() == true /** Saves a fresh sign-in and looks the account's name up for the chip. Blocking. */ - fun saveLogin(context: Context, accessToken: String, refreshToken: String?, userId: String?) { + fun saveLogin(context: Context, accessToken: String, refreshToken: String?, userId: String?) = GogComet.replaceAccount { + saveAccount(context, accessToken, refreshToken, userId) + } + + @Synchronized + private fun saveAccount(context: Context, accessToken: String, refreshToken: String?, userId: String?) { var username = "" runCatching { val body = StoreNet.get("https://embed.gog.com/userData.json", bearer = accessToken, userAgent = GALAXY_UA) @@ -52,6 +57,7 @@ object GogAuth { /** A usable access token, refreshed when the recorded login time says it has expired; null when signed out or the refresh failed. */ @JvmStatic + @Synchronized fun validToken(context: Context): String? { val json = StoreAccounts.read(context, Store.GOG) ?: return null val token = json.optString("access_token", "").ifEmpty { return null } @@ -63,6 +69,7 @@ object GogAuth { /** Refreshes the access token; the new one, or null on any failure. Blocking. */ @JvmStatic + @Synchronized fun refresh(context: Context): String? { val json = StoreAccounts.read(context, Store.GOG) ?: return null val refreshToken = json.optString("refresh_token", "").ifEmpty { return null } @@ -87,6 +94,27 @@ object GogAuth { } } + /** A complete companion snapshot. Never exported through the guest request channel. */ + @Synchronized + fun cometCredentials(context: Context): JSONObject? { + val access = validToken(context) ?: return null + val saved = StoreAccounts.read(context, Store.GOG) ?: return null + val refresh = saved.optString("refresh_token").ifEmpty { return null } + var user = saved.optString("user_id") + var username = saved.optString("username") + if (!user.matches(Regex("[0-9]+")) || username.isEmpty()) { + val body = StoreNet.get("https://embed.gog.com/userData.json", bearer = access, userAgent = GALAXY_UA, timeoutMs = 5_000) ?: return null + val info = runCatching { JSONObject(body) }.getOrNull() ?: return null + user = info.optString("galaxyUserId", info.optString("userId", user)) + username = info.optString("username", username) + if (!user.matches(Regex("[0-9]+")) || username.isEmpty()) return null + saved.put("user_id", user).put("username", username) + StoreAccounts.write(context, Store.GOG, saved) + } + return JSONObject().put("access_token", access).put("refresh_token", refresh) + .put("user_id", user).put("username", username) + } + class GameToken(val accessToken: String, val userId: String) /** The Galaxy refresh token exchanged for one scoped to a game's own client. Blocking. */ diff --git a/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt new file mode 100644 index 0000000000..a514b8db39 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/stores/gog/GogComet.kt @@ -0,0 +1,189 @@ +package com.droiddeck.launcher.stores.gog + +import android.content.Context +import android.os.Environment +import android.util.Log +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.setValue +import com.droiddeck.launcher.runtime.LinuxRuntime +import com.droiddeck.launcher.session.SessionEvents +import com.droiddeck.launcher.session.SessionPrefs +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.stores.Store +import com.droiddeck.launcher.stores.AchievementNotifications +import com.droiddeck.launcher.stores.StoreAchievements +import com.droiddeck.launcher.stores.StoreAccounts +import com.droiddeck.launcher.stores.StoreGameSidecar +import com.droiddeck.launcher.stores.StoreInstallRoot +import com.droiddeck.launcher.stores.StoresState +import org.json.JSONObject +import java.io.File +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit + +/** Owns one Galaxy companion; only its structured, secret-free events reach Android logs. */ +object GogComet { + private const val TAG = "GogComet" + private const val BIN = "/usr/local/lib/droiddeck-comet/comet" + private val lock = Any() + @Volatile private var process: Process? = null + private var pid = -1 + private var started: String? = null + private var account = "" + private var session: String? = null + private var accepting = false + private var generation: String? = null + @Volatile private var gamesByClient: Map = emptyMap() + var status by mutableStateOf("off") + private set + + fun enabled(context: Context) = GogPrefs.get(context).getBoolean("achievements", false) + + fun setEnabled(context: Context, on: Boolean) { + GogPrefs.get(context).edit().putBoolean("achievements", on).apply() + + } + + fun begin() = synchronized(lock) { + stopLocked() + accepting = true + generation = SessionState.sessionId + AchievementNotifications.begin(generation) + } + + /** Account replacement and launch preparation use the same lock, preventing stale restarts. */ + fun replaceAccount(change: () -> Unit) = synchronized(lock) { + stopLocked() + AchievementNotifications.begin(generation) + change() + } + + fun stop() = synchronized(lock) { + stopLocked() + } + + fun end(expected: String?) = synchronized(lock) { + if (generation == expected) { + accepting = false + stopLocked() + AchievementNotifications.end(expected) + generation = null + } + } + + private fun report(value: String) { + StoresState.post { status = value } + Log.i(TAG, value) + SessionEvents.record("gog.achievements", mapOf("status" to value)) + } + + private fun publishPolicy(context: Context): File { + val dir = File(context.filesDir, "gog-achievements").apply { mkdirs() } + val games = if (!enabled(context)) emptyMap() else StoreInstallRoot.gameFolders(context).mapNotNull { folder -> + val game = StoreGameSidecar.read(folder) ?: return@mapNotNull null + if (game.store != Store.GOG || !game.achievements) return@mapNotNull null + val client = GogPrefs.get(context).getString("client_id_${game.id}", null) + ?: runCatching { JSONObject(File(folder, "goggame-${game.id}.info").readText()).optString("clientId") }.getOrNull() + client?.takeIf { it.matches(Regex("[0-9]+")) }?.let { it to game } + }.toMap() + gamesByClient = games + val file = File(dir, "clients.json") + val tmp = File(dir, "clients.json.tmp") + tmp.writeText(org.json.JSONArray(games.keys.toList()).toString()) + check(tmp.renameTo(file)) + return file + } + + /** No arbitrary paths or credentials from the guest are accepted. */ + fun prepare(context: Context, id: String): JSONObject = synchronized(lock) { + fun reply(ready: Boolean, reason: String): JSONObject { + report(reason) + return JSONObject().put("ok", ready).put("ready", ready).put("reason", reason) + } + if (!accepting || !SessionState.running || generation != SessionState.sessionId) return reply(false, "no-session") + val folder = StoreInstallRoot.gameFolders(context).firstOrNull { f -> StoreGameSidecar.read(f)?.let { it.store == Store.GOG && it.id == id } == true } + val sidecar = folder?.let { StoreGameSidecar.read(it) } + if (sidecar?.store != Store.GOG || sidecar.id != id) return reply(false, "no-game") + val policy = publishPolicy(context) + if (!enabled(context) || !sidecar.achievements) return reply(false, "off") + val credentials = GogAuth.cometCredentials(context) ?: return reply(false, + if (StoreAccounts.isUnavailable(Store.GOG)) "credentials-unavailable" + else if (StoreAccounts.read(context, Store.GOG)?.optString("refresh_token").isNullOrEmpty()) "sign-in-needed" else "credentials-unavailable") + if (!SessionState.running || generation != SessionState.sessionId) return reply(false, "no-session") + val user = credentials.getString("user_id") + if (process?.isAlive == true && account == user && session == SessionState.sessionId && identity(pid) == started) { + return JSONObject().put("ok", true).put("ready", true).put("reason", "ready") + } + stopLocked() + if (!File(LinuxRuntime.rootDir(context), BIN.removePrefix("/")).isFile) return reply(false, "not-installed") + val data = File(context.filesDir, "gog-achievements").apply { mkdirs() } + val ready = CountDownLatch(1) + val command = LinuxRuntime.command(context, null, null, Environment.getExternalStorageDirectory(), + listOf("/usr/bin/env", "HOME=/root", "XDG_DATA_HOME=${data.path}/data", "XDG_CONFIG_HOME=${data.path}/config", "COMET_LOG=off", "COMET_ALLOWED_CLIENTS=${policy.path}", BIN, "--credentials-stdin")) + val pb = ProcessBuilder(command).redirectErrorStream(true) + pb.environment().apply { + remove("LD_PRELOAD"); remove("LD_AUDIT"); remove("LD_LIBRARY_PATH") + put("PROOT_LOADER", LinuxRuntime.prootLoader(context).path) + put("PROOT_TMP_DIR", context.cacheDir.path) + if (SessionPrefs.prootNoSeccomp(context)) put("PROOT_NO_SECCOMP", "1") + LinuxRuntime.prootLibraryPath(context).takeIf { it.isNotEmpty() }?.let { put("LD_LIBRARY_PATH", it) } + } + try { + val child = pb.start() + process = child; account = user; session = SessionState.sessionId + Thread({ + runCatching { child.inputStream.bufferedReader().useLines { lines -> lines.forEach { line -> + // Raw upstream errors/payloads can contain tokens. Never forward them. + when { + line.startsWith("DROIDDECK_COMET_READY:") -> { + val childPid = line.substringAfter(':').toIntOrNull() ?: -1 + if (childPid > 1 && process === child) { + pid = childPid; started = identity(childPid); ready.countDown() + } + } + line.startsWith("DROIDDECK_COMET_UNLOCKED:") -> if (process === child) { + val event = runCatching { JSONObject(line.substringAfter(':')) }.getOrNull() + val game = event?.optString("client_id")?.let { gamesByClient[it] } + val unlock = event?.let { AchievementNotifications.parse(it, game?.title.orEmpty()) } + val owner = session + if (game != null && unlock != null && owner != null && owner == SessionState.sessionId && SessionState.running) { + report("unlocked-locally") + AchievementNotifications.unlock(owner, unlock) + SessionEvents.record("achievement.unlocked", mapOf("store" to "gog", "gameId" to game.id, "achievementId" to unlock.id)) + } + } + line == "DROIDDECK_COMET_PENDING" -> if (process === child) report("pending-upload") + line.startsWith("DROIDDECK_COMET_UPLOADED:") -> if (process === child && line.substringAfter(':').toIntOrNull()?.let { it > 0 } == true) { report("uploaded"); StoresState.post { StoreAchievements.invalidate() } } + } + } } } + if (process === child) report("service-unavailable") + ready.countDown() + }, "gog-comet-output").start() + child.outputStream.bufferedWriter().use { it.write(credentials.toString()) } + if (!ready.await(5, TimeUnit.SECONDS) || pid <= 1 || started == null || !child.isAlive) { + stopLocked(); return reply(false, "service-unavailable") + } + reply(true, "ready") + } catch (e: Exception) { + stopLocked() + reply(false, "service-unavailable") + } + } + + private fun identity(pid: Int): String? = if (pid <= 1) null else runCatching { + File("/proc/$pid/stat").readText().substringAfterLast(") ").split(' ')[19] + }.getOrNull() + + private fun stopLocked() { + val child = process ?: return + // Signal the announced daemon, not proot; start time prevents signalling a reused PID. + if (started != null && identity(pid) == started) android.os.Process.sendSignal(pid, 2) + if (!child.waitFor(5, TimeUnit.SECONDS)) { + if (started != null && identity(pid) == started) android.os.Process.sendSignal(pid, 15) + child.destroy() + } + process = null; pid = -1; started = null; account = ""; session = null + report("stopped") + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AchievementNotificationOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/AchievementNotificationOverlay.kt new file mode 100644 index 0000000000..828c1cd996 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/AchievementNotificationOverlay.kt @@ -0,0 +1,96 @@ +package com.droiddeck.launcher.ui + +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut +import androidx.compose.animation.slideInVertically +import androidx.compose.animation.slideOutVertically +import androidx.compose.foundation.background +import androidx.compose.foundation.border +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.widthIn +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.testTag +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.text.style.TextOverflow +import androidx.compose.ui.tooling.preview.Preview +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import coil.compose.AsyncImage +import com.droiddeck.launcher.R +import com.droiddeck.launcher.session.SessionEvents +import com.droiddeck.launcher.stores.AchievementNotifications +import kotlinx.coroutines.delay + +@Composable +internal fun AchievementNotificationOverlay() { + val unlock = AchievementNotifications.pending.firstOrNull() + var visible by remember(unlock) { mutableStateOf(false) } + LaunchedEffect(unlock) { + if (unlock == null) return@LaunchedEffect + visible = true + SessionEvents.record("achievement.notification-shown", mapOf("achievementId" to unlock.id)) + delay(5_000) + visible = false + delay(Motion.ms(220).toLong()) + AchievementNotifications.dismiss(unlock) + } + Box(Modifier.fillMaxSize().padding(20.dp), contentAlignment = Alignment.BottomEnd) { + AnimatedVisibility( + visible, enter = fadeIn(Motion.tw(220)) + slideInVertically(Motion.tw(300)) { it / 2 }, + exit = fadeOut(Motion.tw(180)) + slideOutVertically(Motion.tw(220)) { it / 3 }, + ) { if (unlock != null) AchievementNotificationCard(unlock) } + } +} + +@Composable +private fun AchievementNotificationCard(unlock: AchievementNotifications.Unlock) { + val pal = LocalPalette.current + val shape = RoundedCornerShape(16.dp) + Row( + Modifier.widthIn(max = 360.dp).clip(shape).background(pal.surface.copy(alpha = 0.97f)) + .border(1.dp, pal.signal.copy(alpha = 0.65f), shape).padding(14.dp) + .testTag("achievement-notification"), + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(12.dp), + ) { + Box(Modifier.size(48.dp).clip(RoundedCornerShape(10.dp)).background(pal.surfaceVariant), contentAlignment = Alignment.Center) { + Trophy(26.dp, description = null) + if (unlock.icon != null) AsyncImage(unlock.icon, null, contentScale = ContentScale.Crop, modifier = Modifier.fillMaxSize()) + } + Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(3.dp)) { + Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(5.dp)) { + Trophy(13.dp, description = null) + Text(stringResource(R.string.achievement_unlocked).uppercase(), fontSize = 10.sp, fontWeight = FontWeight.SemiBold, letterSpacing = 0.8.sp, color = pal.signal) + } + Text(unlock.name, color = pal.onBackground, fontSize = 15.sp, fontWeight = FontWeight.Bold, maxLines = 2, overflow = TextOverflow.Ellipsis) + if (unlock.game.isNotBlank()) Text(unlock.game, color = pal.onSurfaceVariant, fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis) + } + } +} + +@Preview(widthDp = 420, heightDp = 150) +@Composable +private fun AchievementNotificationPreview() = DroidDeckTheme { + Box(Modifier.padding(20.dp)) { + AchievementNotificationCard(AchievementNotifications.Unlock("1", "first", "First Steps", "", null, "Hollow Valley")) + } +} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt index 613c96d437..5e82d96331 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/FrontEndSetup.kt @@ -495,8 +495,8 @@ internal fun LaunchSettings(s: FrontEndState, a: FrontEndActions, host: MenuHost if (controller != null) add { m -> SettingCard(stringResource(R.string.setup_card_controls), stringResource(R.string.setup_card_controls_hint), "card:controls", m, controller.onMapping) } // An Epic game's own launch choices, kept in its sidecar so a launch from the Steam client // honours them too (droiddeck-store-launch reads the same file). - // Epic's launch choices; cloud saves live in the game's Cloud saves view, so a GOG game has no card. - val cardStore = com.droiddeck.launcher.stores.Store.byId(game?.source.orEmpty())?.takeIf { it == com.droiddeck.launcher.stores.Store.EPIC } + // Store launch choices; cloud saves have their own view. + val cardStore = com.droiddeck.launcher.stores.Store.byId(game?.source.orEmpty())?.takeIf { it == com.droiddeck.launcher.stores.Store.EPIC || it == com.droiddeck.launcher.stores.Store.GOG } if (cardStore != null && game?.gameFiles != null) add { m -> Box(m) { StoreLaunchCard(host, game.gameFiles!!, cardStore) @@ -537,7 +537,14 @@ private fun StoreLaunchCard(host: MenuHost, folder: java.io.File, store: com.dro val options = sidecar?.epic ?: com.droiddeck.launcher.stores.EpicOptions() val epic = store == com.droiddeck.launcher.stores.Store.EPIC val key = "store-" + store.id - val value = buildList { + val value = if (!epic) stringResource(when { + !com.droiddeck.launcher.stores.gog.GogComet.enabled(appContext) || sidecar?.achievements == false -> R.string.gog_achievements_off + com.droiddeck.launcher.stores.gog.GogComet.status == "sign-in-needed" -> R.string.gog_achievements_signin + com.droiddeck.launcher.stores.gog.GogComet.status in listOf("service-unavailable", "credentials-unavailable", "not-installed") -> R.string.gog_achievements_unavailable + com.droiddeck.launcher.stores.gog.GogComet.status == "uploaded" -> R.string.gog_achievements_uploaded + com.droiddeck.launcher.stores.gog.GogComet.status == "pending-upload" || com.droiddeck.launcher.stores.gog.GogComet.status == "unlocked-locally" -> R.string.gog_achievements_pending + else -> R.string.gog_achievements_on + }) else buildList { if (epic && options.offline) add(stringResource(R.string.epic_card_offline)) else if (epic && options.eos) add(stringResource(R.string.epic_card_eos)) }.ifEmpty { listOf(stringResource(R.string.epic_card_none)) }.joinToString(" ยท ") @@ -556,6 +563,16 @@ private fun StoreLaunchCard(host: MenuHost, folder: java.io.File, store: com.dro host.open = if (host.open == key) null else key } AnchoredMenu(host.open == key, onDismiss = { if (host.open == key) host.open = null }, title = store.shortLabel) { first -> + if (!epic) { + MenuItem(stringResource(R.string.gog_achievements), checked = sidecar?.achievements != false, focusRequester = first, + detail = if (!com.droiddeck.launcher.stores.gog.GogComet.enabled(appContext)) stringResource(R.string.gog_achievements_enable_store) else null) { + val on = sidecar?.achievements == false + Thread({ + val written = runCatching { com.droiddeck.launcher.stores.StoreGameSidecar.updateAchievements(folder, on) }.getOrNull() + com.droiddeck.launcher.stores.StoresState.post { sidecar = written ?: sidecar } + }, "gog-game-achievements").start() + } + } if (epic) { MenuItem(stringResource(R.string.epic_eos), checked = options.eos, focusRequester = first) { set { it.copy(eos = !it.eos) } } MenuItem(stringResource(R.string.epic_offline), checked = options.offline) { set { it.copy(offline = !it.offline) } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoreAchievementsUi.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoreAchievementsUi.kt index a740e11a1d..0e87128b4f 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoreAchievementsUi.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoreAchievementsUi.kt @@ -78,7 +78,7 @@ internal class AchievementsState(val list: List?, val reason: Reaso internal fun rememberAchievements(game: StoreAchievements.Game?): AchievementsState? { val ctx = LocalContext.current.applicationContext var state by remember(game) { mutableStateOf(if (game == null) null else AchievementsState.READING) } - LaunchedEffect(game) { + LaunchedEffect(game, StoreAchievements.revision) { if (game == null) return@LaunchedEffect val source = StoreAchievements.forSource(game.source) ?: return@LaunchedEffect val cached = withContext(Dispatchers.IO) { source.cached(ctx, game) } @@ -101,8 +101,8 @@ internal fun achievementsNote(state: AchievementsState, label: String): String? } @Composable -internal fun Trophy(size: Dp = 18.dp, modifier: Modifier = Modifier) { - Icon(Icons.Filled.EmojiEvents, stringResource(R.string.achievements_all), modifier.size(size), tint = TrophyGold) +internal fun Trophy(size: Dp = 18.dp, modifier: Modifier = Modifier, description: String? = stringResource(R.string.achievements_all)) { + Icon(Icons.Filled.EmojiEvents, description, modifier.size(size), tint = TrophyGold) } @Composable diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt index cf491d772e..47b356e7da 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoresDownloads.kt @@ -553,6 +553,14 @@ internal fun storesSettingsStep(at: Rect, s: androidx.compose.runtime.StateSize on disk Uninstall the desktop? All + GOG achievements + Sync achievements for supported GOG games. Applies on the next launch. + Achievements off + Achievements enabled + Sign in to GOG again + Achievements uploaded + Achievement upload pending + Enable GOG achievements in Stores settings + Achievements unavailable; check session logs + Achievement unlocked diff --git a/app/src/test/java/com/droiddeck/launcher/session/SessionAchievementGameTest.kt b/app/src/test/java/com/droiddeck/launcher/session/SessionAchievementGameTest.kt new file mode 100644 index 0000000000..79156cbe98 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/session/SessionAchievementGameTest.kt @@ -0,0 +1,29 @@ +package com.droiddeck.launcher.session + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class SessionAchievementGameTest { + private val installed = setOf(10L, 20L) + + @Test fun exitingGameCannotRemainSelectedWithoutItsWindow() { + assertNull(SessionAchievementGame.activeAppId(10, listOf(769), installed)) + } + + @Test fun steamOverlayKeepsTheRunningGameSelected() { + assertEquals(10L, SessionAchievementGame.activeAppId(769, listOf(769, 10), installed)) + } + + @Test fun anotherForegroundAppCannotBorrowABackgroundGamesAchievements() { + assertNull(SessionAchievementGame.activeAppId(30, listOf(30, 10), installed)) + } + + @Test fun ambiguousBackgroundGamesDoNotPickAnArbitraryGame() { + assertNull(SessionAchievementGame.activeAppId(769, listOf(769, 10, 20), installed)) + } + + @Test fun theForegroundGameWinsWhenSeveralGamesAreRunning() { + assertEquals(20L, SessionAchievementGame.activeAppId(20, listOf(769, 10, 20), installed)) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/AchievementNotificationsTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/AchievementNotificationsTest.kt new file mode 100644 index 0000000000..3efda39d07 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/AchievementNotificationsTest.kt @@ -0,0 +1,55 @@ +package com.droiddeck.launcher.stores + +import android.os.Looper +import org.json.JSONObject +import org.junit.Assert.* +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.Shadows.shadowOf +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class AchievementNotificationsTest { + private val unlock = AchievementNotifications.Unlock("123", "first", "First Steps", "", null, "Hollow Valley") + private fun drain() = shadowOf(Looper.getMainLooper()).idle() + + @Before fun reset() { AchievementNotifications.begin("test"); drain() } + + @Test fun duplicatesAndOldSessionEventsDoNotQueue() { + AchievementNotifications.unlock("old", unlock) + AchievementNotifications.unlock("test", unlock) + AchievementNotifications.unlock("test", unlock.copy(name = "Repeated")) + drain() + assertEquals(listOf(unlock), AchievementNotifications.pending) + } + + @Test fun oldTeardownAndDismissCannotRemoveReplacementUnlock() { + AchievementNotifications.unlock("test", unlock); drain() + AchievementNotifications.begin("replacement") + val replacement = unlock.copy(id = "second") + AchievementNotifications.unlock("replacement", replacement) + AchievementNotifications.end("test"); drain() + AchievementNotifications.dismiss(unlock) + assertEquals(listOf(replacement), AchievementNotifications.pending) + AchievementNotifications.end("replacement"); drain() + assertTrue(AchievementNotifications.pending.isEmpty()) + } + + @Test fun burstIsBoundedAndKeepsOrder() { + repeat(30) { AchievementNotifications.unlock("test", unlock.copy(id = "$it")) }; drain() + assertEquals((0..15).map { "$it" }, AchievementNotifications.pending.map { it.id }) + AchievementNotifications.dismiss(AchievementNotifications.pending.first()) + assertEquals("1", AchievementNotifications.pending.first().id) + } + + @Test fun malformedEventsAndNonHttpsIconsAreRejected() { + assertNull(AchievementNotifications.parse(JSONObject().put("client_id", "123"), "")) + val event = JSONObject().put("client_id", "123").put("id", "first").put("name", "First Steps").put("icon", "file:///private/icon") + assertNull(AchievementNotifications.parse(event, "Game")!!.icon) + event.put("client_id", "null") + assertNull(AchievementNotifications.parse(event, "Game")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/GogCometGenerationTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/GogCometGenerationTest.kt new file mode 100644 index 0000000000..0590ec1584 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/stores/GogCometGenerationTest.kt @@ -0,0 +1,34 @@ +package com.droiddeck.launcher.stores + +import com.droiddeck.launcher.session.SessionState +import com.droiddeck.launcher.stores.gog.GogComet +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class GogCometGenerationTest { + @After fun reset() { + GogComet.end(SessionState.sessionId) + SessionState.running = false + SessionState.sessionId = null + } + + @Test fun anOldTeardownCannotDisableTheReplacementSession() { + SessionState.running = true + SessionState.sessionId = "old" + GogComet.begin() + SessionState.sessionId = "replacement" + GogComet.begin() + GogComet.end("old") + val app = RuntimeEnvironment.getApplication() + assertEquals("no-game", GogComet.prepare(app, "missing").getString("reason")) + GogComet.end("replacement") + assertEquals("no-session", GogComet.prepare(app, "missing").getString("reason")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt index 1171bce1a7..ae966ca9b6 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/StoreGameSidecarTest.kt @@ -74,6 +74,16 @@ class StoreGameSidecarTest { assertEquals(null, StoreGameSidecar.updateEpic(java.io.File(folder, "missing")) { it }) } + @Test fun gogAchievementsAreIndependentOfCloudAndSurviveEdits() { + val folder = java.io.File(org.robolectric.RuntimeEnvironment.getApplication().filesDir, "gog-achievements").apply { mkdirs() } + StoreGameSidecar(Store.GOG, "1", "Game", "game.exe", cloud = false).write(folder) + assertTrue(StoreGameSidecar.read(folder)!!.achievements) + StoreGameSidecar.updateAchievements(folder, false) + StoreGameSidecar.updateCloud(folder, true) + assertFalse(StoreGameSidecar.read(folder)!!.achievements) + assertTrue(StoreGameSidecar.read(folder)!!.cloud) + } + @Test fun anInstallUnderWayHasNoExeYetAndIsNotInstalled() { val started = StoreGameSidecar(Store.EPIC, "Metalstorm", "Metalstorm", exe = "", state = StoreGameSidecar.STATE_INSTALLING) val back = StoreGameSidecar.parse(started.toJson().toString())!! diff --git a/docs/development/comet-integration-plan.md b/docs/development/comet-integration-plan.md new file mode 100644 index 0000000000..2a90559f26 --- /dev/null +++ b/docs/development/comet-integration-plan.md @@ -0,0 +1,159 @@ +# Comet integration research and implementation plan + +Researched 2026-10-12 against DroidDeck `10ef8c4bd62b21872fc13382a808cb0f2f117b26` (0.4.0). +The research below informed the implementation on `feat/gog-comet-achievements`, rebased onto refreshed main `a2a9586b`. Controlled SDK unlock/upload has now been verified on Thor; wider gameplay coverage remains an acceptance test for users. + +## Implementation and testing status + +Implemented an opt-in GOG companion built from pinned Comet v0.3.2, credential transfer through stdin, one owned daemon per session/account, private persistent databases, per-game settings, and first-launch Galaxy service provisioning using the selected Proton. Local unlocks, pending uploads and successful uploads produce sanitized session events. The game settings card shows pending/uploaded/unavailable status for the companion in the current session; it is not a per-game achievement history. Local unlocks now show an animated DroidDeck notification with the achievement icon, name and game title. The achievement reader comes from main; upload acknowledgements refresh its progress. No Galaxy overlay is installed. The session-menu reader is available only for a live supported GOG/Epic game window with a nonempty achievement list, including while Steam's overlay is open; GOG browsing does not require Comet syncing to be enabled. Epic syncing remains a separate investigation. + +Comet's patch also uses an unconditional periodic sync tick during SDK traffic, preserves retries after failed achievement POSTs, and limits SDK clients to enabled installed GOG games. Credential errors and raw upstream protocol output are not forwarded to Android/session logs. + +Validation on AYN Thor (`192.168.0.8:38029`, Linux runtime r9): + +- ARM64 companion runs and receives credentials through proot stdin; owned readiness occurs in under a second. +- A fresh disposable prefix installs the x64 dummy service through ARM64 Proton/FEX before its first launch. +- ELDERBORN (GOG product `1732383191`) launches through the normal managed Steam shortcut. Its Galaxy SDK authentication and 27 achievement definitions can be read through Comet, and independently from GOG's gameplay API. +- Normal session stop removes the owned companion and preserves its database. +- Release APK builds; 694 JVM tests pass and all 20 store-launch tests pass. +- Linux Docker suite: 548 tests, 518 pass, 29 skipped, one failure in the unchanged x86 SIGILL fault-report test (Docker reports `si_code 01`, assertion expects `02`). This is an environment-dependent mismatch, not a green full-suite result. + +The reader updated from 0/27 to 1/27 and back after upload/reset without reopening. A temporary protocol probe unlocked locked ELDERBORN achievements after twenty seconds, repeated the SDK request, confirmed the new unlock independently through the GOG gameplay API, and cleared each tested achievement afterward. The notification appeared once over the focused game and dismissed after five seconds. A temporary per-app HTTPS rejection confirmed a local popup, pending upload, and successful retry after the rule was removed; the account records and network rule were restored. Account changes with pending data, first-ever offline launches, 32-bit titles, and wider game/Proton compatibility still need user testing. A current Steam live-shortcut API mismatch required restarting Steam after installing the test game; the normal startup shortcut worked. That existing issue is outside this change. + +## User test guide + +1. Sign into GOG and enable **GOG achievements** in Stores settings. It is off by default. Keep the game's own achievement switch enabled in its launch settings; cloud saves may be on or off. +2. Start a supported GOG game using a DroidDeck managed compatibility tool. Obtain a genuinely unearned achievement through normal gameplay. +3. Check the game's GOG profile independently. Allow at least ten seconds online and a normal game exit. A local unlock should show a five-second notification over the game. The popup confirms the local unlock; check the GOG profile or the uploaded event for server delivery. +4. Include the game/product ID, device, Proton choice, whether the prefix was new, and online/offline sequence with the session report. The `gog.achievements` events distinguish `unlocked-locally`, `pending-upload` and `uploaded`; `uploaded` is emitted only after a successful GOG POST. Do not include credentials. +5. If the game has trouble with the companion, disable its achievement switch and relaunch. Changes to these switches apply on the next game launch. + +## Recommendation and scope + +Integrate Comet as a native ARM64 Linux companion for **GOG Galaxy achievements** inside DroidDeck's existing Linux/Proton session. Start with an opt-in GOG setting and a per-game disable switch. Cover launches from Games, Stores, Android shortcuts, Steam's own Play button, and KDE when those paths use DroidDeck's managed compatibility tool. + +Comet handles the Galaxy SDK requests made by a game and uploads achievements to that game's GOG account. It does not transfer achievements between storefronts, add achievements to games without Galaxy SDK support, or implement Epic achievement sync. [Comet README](https://github.com/imLinguin/comet/blob/19b32597019ea1e64f1916010441960bcaac6e49/README.md) + +Treat the screenshot as two workstreams: + +| Request | Approach | Initial deliverable | +| --- | --- | --- | +| GOG achievements | Comet + existing GOG sign-in + Proton prefix setup | An unlock reaches the correct GOG profile from a real DroidDeck launch | +| Epic achievements | Validate the game's EOS authentication, deployment, consent and achievement calls | Evidence of an EOS unlock and the corresponding Epic profile update; fixes only where a failure is demonstrated | + +Epic Games Store achievements are linked to EOS achievements in a game-specific deployment. A generic launcher service cannot create that integration for a title. DroidDeck already supplies EOS identity/deployment arguments and a fresh exchange code. [Epic achievement setup](https://dev.epicgames.com/docs/epic-games-store/services/epic-achievements/achievements-setup), [Epic authentication guide](https://dev.epicgames.com/docs/epic-online-services/accounts-and-social/eos-epic-account-services/auth-interface/auth-guide/auth-intro) + +## What was verified + +- Local `main` fast-forwarded from `b6c1d3c0` to `10ef8c4b` and matched refreshed `origin/main`. +- Removed seven clean, remotely backed checkouts with non-force `git worktree remove`; pruned nine registrations pointing to missing directories. Kept dirty worktrees, open-PR work, credentials, captures, and uncertain checkouts. Remote branches and PRs were not changed. +- Inspected Comet release `v0.3.2` (`3d7488ee72f83b7e8462a70fd4c4638d6da71360`) and upstream main (`19b32597019ea1e64f1916010441960bcaac6e49`). Main has substantial unreleased changes, including progress-stat workarounds. Use a pinned release as the initial baseline; evaluate later changes individually. +- Downloaded `comet-aarch64-unknown-linux-gnu` and `dummy-service.zip` from v0.3.2. ELF inspection confirmed AArch64, interpreter `/lib/ld-linux-aarch64.so.1`, dependencies on `libgcc_s`, `libm`, `libc` and the loader, and required GLIBC symbols through 2.34. Binary size is 11,365,728 bytes, before APK compression. +- The current Linux runtime catalog is `linuxfs-r9`, described as Arch Linux ARM aarch64. This is separate from the Debian image used for x86 preload builds. Actual loader/library compatibility must still be checked in the installed runtime. +- Inspected Heroic main `db5272043e74336b72bc60caad14ca74701082d8`: its GOG launcher spawns Comet with `--from-heroic --username ... --quit`, then terminates it after the game. That establishes a reference integration, but its process and credential handling should not be copied wholesale. + +Downloaded artifact hashes, useful for reproducing this inspection: + +```text +comet-aarch64-unknown-linux-gnu +c8cd850a03ba66c10bf620eee2735946ddf0b5969cd38cae02c6bede6d15c5b8 +dummy-service.zip +7fc5facd1fce4d516cd3ce527e7e4dbe0bfd17649cea805e8ea7c37d4ba58d9a +``` + +These are locally measured hashes, not independently signed upstream attestations. A patched build will have different hashes. + +## Existing DroidDeck integration points + +| Source | Existing behavior | Proposed change | +| --- | --- | --- | +| `stores/gog/GogAuth.kt`, `stores/LoginFlows.kt` | Saves access token, optional refresh token/user ID, username; refreshes expired access tokens | Add a validated Comet credential snapshot, recover missing identity where possible, serialize refresh and sign-out | +| `stores/StoreAccounts.kt`, `stores/CredentialCipher.kt` | Keeps store credentials encrypted with Android Keystore | Keep these as the durable credential authority; do not export a persistent Heroic auth file | +| `stores/StoreLaunchRequests.kt` | App-side request handler under the session's `stores/req` and `stores/resp` | Handle GOG companion preparation; ordinary replies contain status, not tokens | +| `tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch` | Finds sidecars beside a launcher or ancestor of a direct `.exe`; runs before managed Proton launches | Detect GOG settings, request companion readiness and arrange prefix setup, independently of cloud saves | +| `tools/linuxfs/overlay/usr/local/bin/steam-compatibility` | Generates compatibility wrappers; waits for Proton and calls `--exited` when cloud saves are enabled | Generalize post-game handling so achievements still get lifecycle handling with cloud saves disabled | +| `session/SessionService.kt`, `session/SessionFiles.kt` | Stages session helpers, starts request observation and tears down the guest | Own the companion's session identity and cleanup; request graceful shutdown before guest teardown | +| `stores/StoreGameSidecar.kt`, `stores/gog/GogPrefs.kt` | Persist game and store settings | Add backward-compatible GOG achievement settings; keep game tokens out of sidecars | +| `app/build.gradle`, `tools/build_local.sh`, `.github/workflows/build.yml`, `tools/release/runtime_inputs.py` | Prepare, cache and attest runtime assets | Include pinned Comet binary, service tools, licenses and build inputs in both local and CI APK paths | + +Paths above are under `app/src/main/java/com/droiddeck/launcher/` unless fully qualified. + +`ComponentsManager.kt` manages FEX/DXVK/VKD3D packages. Comet should initially be an internal runtime asset rather than an additional graphics-component type. + +## Proposed architecture + +### 1. Package a pinned companion + +Build from Comet v0.3.2 with a small, reviewable patch set and its protocol submodule pinned. Put the executable and dummy-service tools under a dedicated guest directory such as `/usr/local/lib/droiddeck-comet/`. Ship the Apache-2.0 license and attribution. Add a `tools/comet/` build recipe and source/patch/hash metadata using the existing prepared-runtime conventions. + +Keep the current upstream ARM64 asset as the initial compatibility probe. The production build needs the credential and logging changes below. Ensure the Rust/toolchain baseline supports the pinned release and that CI cache keys and runtime attestation include the new inputs. Exercise cached and uncached builds, plus runtime installation into an existing app installation. + +### 2. Hand off credentials without putting them in command lines + +Comet's real CLI uses `--access-token`, `--refresh-token`, `--user-id`, and `--username`; the README contains outdated spellings. These flags put tokens in process arguments. `--from-heroic` reads a plaintext Heroic auth file, and its Linux parser uses `XDG_CONFIG_PATH`, not the usual `XDG_CONFIG_HOME`. Neither is the preferred production handoff. [CLI source](https://github.com/imLinguin/comet/blob/3d7488ee72f83b7e8462a70fd4c4638d6da71360/src/main.rs), [Heroic import source](https://github.com/imLinguin/comet/blob/3d7488ee72f83b7e8462a70fd4c4638d6da71360/src/import_parsers/heroic.rs) + +Add a small Comet credential-input option that accepts JSON over stdin or an inherited file descriptor. The Android-owned supervisor launches Comet through the existing proot command builder, writes one validated snapshot into its stdin, closes it, and tracks the process. Prove this pipe survives the proot setup before committing to the plumbing; if it does not, use a short-lived private mode-600 file with immediate unlink and startup cleanup. + +The snapshot needs a usable access token, refresh token, numeric GOG user ID and username. Existing logins may lack refresh token or user ID: current `saveLogin` accepts both as optional and only saves the username from its user-data lookup. Recover identity through GOG's authenticated user endpoint where possible. If no refresh token exists, show a specific GOG re-sign-in requirement and let the game launch without achievement support. + +Comet exchanges the Galaxy refresh token for each game's SDK client credentials; the game's SDK handshake supplies its client ID/secret. Do not confuse the store product ID with the SDK client ID, and do not require those per-game secrets in a store sidecar. Explicit offline preloading is a separate case and can wait. + +Patch or sanitize Comet's HTTP error logging as well: token requests encode credentials in URLs, and raw `reqwest` errors can include those URLs. Filter before stderr reaches session logs, crash reports, or exported artifacts. Passing secrets through stdin alone is insufficient. [Token exchange source](https://github.com/imLinguin/comet/blob/3d7488ee72f83b7e8462a70fd4c4638d6da71360/src/api/gog/users.rs) + +### 3. One owned daemon for the session/account + +Comet binds `127.0.0.1:9977`. Proot does not provide a private network namespace. Use a serialized ensure-running operation for one owned daemon per active session/account, shared by GOG games in that session. Verify process identity, session generation and GOG account before reuse; an occupied port is not proof that our daemon is ready. If another service owns the port, report unavailable and continue launching the game. + +Prefer session ownership over `--quit`: upstream's idle quit requires a client to have connected, so games without Galaxy can leave it running indefinitely. Explicit lifecycle ownership also avoids stopping it while another GOG client is active. Readiness should come from the owned process's listening event plus liveness, with a bounded wait; a probe connection has side effects on Comet's client/idle accounting. + +Give Comet a persistent, private data location outside the session request tree and game directory through process-local XDG settings. Upstream stores gameplay databases by `//gameplay.db`. Preserve these databases across session resets and APK updates. Stop the old daemon on sign-out/account change, retain pending data under the old account, and never upload it using the replacement account. Provide explicit data deletion separately from sign-out. + +### 4. Provision the game's actual Wine prefix on its first launch + +Some Galaxy SDK versions need the Windows `GalaxyCommunication` service even though Comet itself runs on Linux. The release ZIP contains `GalaxyCommunication.exe`, `install-dummy-service.bat`, and `update-permissions.exe`; packaging only the standalone service executable misses part of upstream's setup. [Dummy-service instructions](https://github.com/imLinguin/comet/blob/3d7488ee72f83b7e8462a70fd4c4638d6da71360/dummy-service/README.md) + +Use the title's selected Proton build and `STEAM_COMPAT_DATA_PATH`, not a global prefix. After Proton has created/updated that prefix, copy the tools into the prefix and idempotently register the service with `sc`, including the upstream permission adjustment where necessary. Preserve an existing genuine Galaxy installation or service. Record completion only after service verification, with a versioned marker so tool updates can be applied safely. + +This is an explicit first-launch problem: the existing `droiddeck-store-launch` runs before Proton creates a new prefix, and several existing registry helpers defer until a later launch. Add a guarded bootstrap/prelaunch stage using the selected Proton, prevent recursive store preparation, wait for its wineserver as needed, then register the service before starting the game. Do not accept "works on the second launch" as completion. + +Test PE x64 service tools through DroidDeck's ARM64 Proton/FEX path and at least one 32-bit Galaxy game. Architecture fit of the Linux daemon does not prove the Windows helpers work. + +### 5. Keep sync and teardown honest + +Upstream persists changed achievements in SQLite, has a sync branch after ten seconds without another selected event, and performs a final sync when a client handler ends. This is not an unconditional ten-second timer or a guarantee of server delivery. Failed achievement PUTs retain database `changed` markers, but the examined sync routine clears its in-memory update flag after processing even when an individual upload fails. Require a targeted retry test and a narrow fix if it confirms that no retry occurs while the client remains connected. [Handler source](https://github.com/imLinguin/comet/blob/3d7488ee72f83b7e8462a70fd4c4638d6da71360/src/api/handlers.rs) + +Allow disconnect/final sync before daemon shutdown, then use SIGINT (upstream explicitly handles Ctrl-C) with a bounded grace period. Add shutdown handling before `SessionService` tears down proot; adding it only to `finishSessionStop` is too late. Forced termination must leave durable pending data for the next valid game connection. Avoid promising a headless "sync everything" action: upstream's pending data is game-scoped and its CLI does not expose that operation. + +Offline support depends on prior metadata/cache availability. First-ever offline starts are not guaranteed. Test cached offline unlocks, reconnection in the same launch, next online launch, forced stop, and app updates. Preserve both local dirty unlocks and unlocks made on another machine during reconciliation. [Offline issue](https://github.com/imLinguin/comet/issues/16) + +### 6. Keep the first UI small + +Add an opt-in **GOG achievements** setting and a per-game override. Keep this independent of the cloud-save toggle. Show actionable states such as sign-in needed, service unavailable, and connection established; distinguish local unlocks from uploads actually accepted by GOG. Include redacted diagnostics in the existing session report. + +Defer a unified achievement browser, Android unlock toasts, GOG overlay downloads/injection, leaderboards UI, multiplayer and playtime claims. Upstream can implement more than achievements, but those features require their own validation. Comet itself downloads Galaxy peer redists in the background, so measure that network/cache behavior before deciding whether to defer or package it. + +Keep a per-game escape hatch: upstream has reports of incompatible games and crashes when Comet is enabled, including [Rise of the Tomb Raider](https://github.com/imLinguin/comet/issues/80) and [unhandled message type 43](https://github.com/imLinguin/comet/issues/90). These are reports, not reproduced DroidDeck failures. + +## Implementation order and acceptance gates + +1. **Compatibility spike:** run the pinned ARM64 daemon's `--help` in the installed Thor runtime; prove localhost connectivity from Wine, proot stdin handoff and first-prefix dummy-service setup. Use a known Galaxy-achievement game owned by the test account. Do not change graphics or download behavior to mask a Comet failure. +2. **Build and credential foundation:** add reproducible assets, license notices, stdin/FD input, credential validation, refresh serialization and log redaction. Verify cached and uncached APK asset preparation. +3. **Launch and lifecycle integration:** add the serialized session supervisor, GOG prepare operation, first-launch prefix provisioning, persistent databases, sign-out handling and shutdown before guest teardown. Verify cloud-off still retains achievement lifecycle handling. +4. **Settings and targeted coverage:** add opt-in and per-game override, useful status, request timeout/error cases, process reuse/conflict cases, database preservation, and fake-secret leak checks. Extend `tools/tests/test_store_launch.py`, `test_store_shortcuts.py`, `test_game_environment.py`, `test_runtime_inputs.py` and relevant JVM tests rather than duplicating unrelated suites. +5. **Device acceptance:** verify an actual unlock appears on the correct GOG profile, then launch the same game twice and another game in a different prefix. Cover Steam Play, app launch, KDE, internal/SD installs, service-required and service-free titles, cloud-off, offline/reconnect, transient upload failure, suspend/resume, normal exit, drawer Stop, force stop, account change and app update. Record exact APK/runtime/Proton versions. + +For the initial spike, use an upstream-listed service-free title such as Hollow Knight and a service-required title such as Cuphead, subject to ownership and the current installed build. The [compatibility table](https://github.com/imLinguin/comet/blob/19b32597019ea1e64f1916010441960bcaac6e49/docs/wiki/Game-Compatibility.md) is a starting point, not a DroidDeck compatibility guarantee. + +A first GOG PR should include the complete minimal path through packaging, authentication, first launch, sync and teardown. Split Epic into a separate investigation/PR so a GOG success is not presented as fulfilling the Epic half of the request. + +## Epic investigation + +Choose a game with published Epic Games Store achievements and an easy unearned achievement. Establish current behavior before adding code: verify the exchange code, expected production sandbox/deployment, browser consent completion and successful EOS Auth/Connect identity. Observe the game's EOS achievement callback and verify the Epic profile independently. + +Current `droiddeck-store-launch` deliberately removes an old `HKCU\Software\Epic Games\EOS\OverlayPath` because that overlay setup caused a launch exit. Do not blindly restore it or globally replace game EOS DLLs. Determine whether the selected title needs additional EOS redistributable/overlay support, and reproduce any missing authentication or unlock behavior before patching. Sync, profile visibility and notification overlays are separate acceptance criteria. + +## Remaining uncertainties + +The original research was static. The implementation validation and remaining device acceptance tests are recorded above; controlled GOG SDK unlock/upload and failed-upload retry are verified; Epic EOS behavior remains unverified. Forced Android process teardown, account replacement with pending data, and wider real-game coverage remain acceptance tests. + +Additional source: [Heroic GOG launch integration](https://github.com/Heroic-Games-Launcher/HeroicGamesLauncher/blob/db5272043e74336b72bc60caad14ca74701082d8/src/backend/storeManagers/gog/games.ts), [Comet v0.3.2 release](https://github.com/imLinguin/comet/releases/tag/v0.3.2). diff --git a/tools/build_local.sh b/tools/build_local.sh index 6588914621..d5d45dbcbe 100755 --- a/tools/build_local.sh +++ b/tools/build_local.sh @@ -320,6 +320,7 @@ else fi cd "${repo_root}" +bash tools/comet/build-local.sh python3 tools/release/runtime_inputs.py record ./gradlew "${gradle_task}" --console=plain -PndkVersion="${ndk_version}" ${gradle_rust_arg} python3 tools/release/check_session_assets.py "app/build/outputs/apk/${build_variant}/app-${build_variant}.apk" diff --git a/tools/comet/LICENSE b/tools/comet/LICENSE new file mode 100644 index 0000000000..57bc88a15a --- /dev/null +++ b/tools/comet/LICENSE @@ -0,0 +1,202 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + diff --git a/tools/comet/README.md b/tools/comet/README.md new file mode 100644 index 0000000000..0d633f8b8c --- /dev/null +++ b/tools/comet/README.md @@ -0,0 +1,24 @@ +# GOG Galaxy achievements + +Comet v0.3.2, pinned to `3d7488ee72f83b7e8462a70fd4c4638d6da71360`, and its +protocol submodule are built for glibc ARM64. Apache-2.0; upstream: +https://github.com/imLinguin/comet. + +Build with Rust 1.90, the `aarch64-unknown-linux-gnu` target and +`aarch64-linux-gnu-gcc`: `bash tools/comet/build.sh`. +The APK's runtime-input manifest records the prepared binary and Windows tools. + +The patch accepts credentials on stdin, announces readiness and accepted uploads +without secrets, emits named local unlocks for the session notification, uses a periodic sync tick even during SDK traffic, and retains the retry flag +when an achievement upload fails. +The Android supervisor discards other upstream output instead of exporting +HTTP errors or protocol payloads containing game credentials. + +The dummy service tools are the hash-checked upstream v0.3.2 release. They are +registered through the game's selected Proton in its own prefix. GOG overlays +are not installed. Pending databases survive session resets and sign-out. + +Local unlocks show a five-second DroidDeck notification without taking input focus. +The notification queue belongs to the session, ignores duplicate SDK unlocks, and clears +on account replacement or session stop. Upload acknowledgements refresh the achievement +reader without replaying notifications. diff --git a/tools/comet/build-local.sh b/tools/comet/build-local.sh new file mode 100644 index 0000000000..fe5d40131e --- /dev/null +++ b/tools/comet/build-local.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -euo pipefail +repo=$(cd "$(dirname "$0")/../.." && pwd) +image=${DROIDDECK_BUILD_IMAGE:-droiddeck-local-cross:24.04-v3} +docker run --rm --platform linux/amd64 -v "$repo:/src" \ + -v "${HOME}/.cache/droiddeck-comet-rust:/rust" -w /src \ + -e CARGO_HOME=/rust/cargo -e RUSTUP_HOME=/rust/rustup "$image" bash -lc ' + set -euo pipefail + apt-get update -qq + apt-get install -y -qq curl git unzip ca-certificates pkg-config >/dev/null + export PATH=/rust/cargo/bin:$PATH + if ! command -v cargo >/dev/null; then + curl -fsSL https://sh.rustup.rs | sh -s -- -y --profile minimal --default-toolchain 1.90.0 + fi + export RUSTUP_TOOLCHAIN=1.90.0 + rustup target add aarch64-unknown-linux-gnu + bash tools/comet/build.sh + ' diff --git a/tools/comet/build.sh b/tools/comet/build.sh new file mode 100644 index 0000000000..b32fc75862 --- /dev/null +++ b/tools/comet/build.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +set -euo pipefail +repo=$(cd "$(dirname "$0")/../.." && pwd) +out=${1:-"$repo/app/src/main/assets/linuxfs/usr/local/lib/droiddeck-comet"} +cache=${COMET_BUILD_DIR:-"$repo/app/build/comet"} +commit=3d7488ee72f83b7e8462a70fd4c4638d6da71360 +mkdir -p "$cache" "$out" +if [ ! -d "$cache/source/.git" ]; then + git clone https://github.com/imLinguin/comet.git "$cache/source" +fi +git -C "$cache/source" reset --hard "$commit" +git -C "$cache/source" submodule update --init +git -C "$cache/source" apply "$repo/tools/comet/droiddeck.patch" +cd "$cache/source" +cargo build --locked --release --target aarch64-unknown-linux-gnu +install -m644 target/aarch64-unknown-linux-gnu/release/comet "$out/comet" +zip="$cache/dummy-service.zip" +if ! echo "7fc5facd1fce4d516cd3ce527e7e4dbe0bfd17649cea805e8ea7c37d4ba58d9a $zip" | sha256sum -c - >/dev/null 2>&1; then + curl -fsSL --retry 3 -o "$zip" https://github.com/imLinguin/comet/releases/download/v0.3.2/dummy-service.zip +fi +echo "7fc5facd1fce4d516cd3ce527e7e4dbe0bfd17649cea805e8ea7c37d4ba58d9a $zip" | sha256sum -c - +unzip -o "$zip" GalaxyCommunication.exe update-permissions.exe -d "$out" +install -m644 "$repo/tools/comet/LICENSE" "$out/LICENSE" +printf '%s\n' "$commit" > "$out/version" diff --git a/tools/comet/droiddeck.patch b/tools/comet/droiddeck.patch new file mode 100644 index 0000000000..f65fe9efa0 --- /dev/null +++ b/tools/comet/droiddeck.patch @@ -0,0 +1,174 @@ +diff --git a/src/api/gog/users.rs b/src/api/gog/users.rs +index 77d8ecf..6a6da12 100644 +--- a/src/api/gog/users.rs ++++ b/src/api/gog/users.rs +@@ -26,9 +26,9 @@ pub async fn get_token_for( + .get(url) + .timeout(time::Duration::from_secs(10)) + .send() +- .await?; ++ .await.map_err(reqwest::Error::without_url)?; + +- let result = result.error_for_status()?; +- let token: Token = result.json().await?; ++ let result = result.error_for_status().map_err(reqwest::Error::without_url)?; ++ let token: Token = result.json().await.map_err(reqwest::Error::without_url)?; + Ok(token) + } +diff --git a/src/api/handlers.rs b/src/api/handlers.rs +index 43a8180..fbbba1b 100644 +--- a/src/api/handlers.rs ++++ b/src/api/handlers.rs +@@ -59,6 +59,9 @@ pub async fn entry_point( + let mut topic_receiver_clone = topic_receiver.resubscribe(); + let mut overlay_event_receiver_clone = overlay_event_receiver.resubscribe(); + let main_socket = tokio::spawn(async move { ++ let mut sync_interval = time::interval(time::Duration::from_secs(10)); ++ sync_interval.set_missed_tick_behavior(time::MissedTickBehavior::Skip); ++ sync_interval.tick().await; + loop { + tokio::select! { + size_read = context_clone.socket_read_u16() => { +@@ -101,7 +104,7 @@ pub async fn entry_point( + } + } + +- _ = time::sleep(time::Duration::from_secs(10)) => { ++ _ = sync_interval.tick() => { + sync_routine(&context_clone, &reqwest_clone, user_clone.clone()).await + }, + +@@ -338,6 +341,7 @@ async fn sync_routine(context: &HandlerContext, reqwest_client: &Client, user_in + let Some(client_id) = &context.client_id().await else { + return; + }; ++ if !crate::client_allowed(client_id) { return; } + let Some(client_secret) = &context.client_secret().await else { + return; + }; +@@ -391,6 +395,8 @@ async fn sync_routine(context: &HandlerContext, reqwest_client: &Client, user_in + .expect("Failed to get database connection"); + let mut transaction = connection.begin().await.unwrap(); + ++ let mut pending = false; ++ let mut uploaded = 0; + for achievement in achievements { + debug!("Setting achievement {}", achievement.achievement_key()); + let result = gog::achievements::set_achievement( +@@ -409,11 +415,15 @@ async fn sync_routine(context: &HandlerContext, reqwest_client: &Client, user_in + .execute(&mut *transaction) + .await + .expect("Failed to update changed status"); ++ uploaded += 1; ++ } else { ++ pending = true; + } + } + transaction.commit().await.expect("Failed to save changes"); +- context.set_updated_achievements(false).await; +- info!("Uploaded achievements"); ++ context.set_updated_achievements(pending).await; ++ if uploaded > 0 { eprintln!("DROIDDECK_COMET_UPLOADED:{uploaded}"); } ++ if pending { eprintln!("DROIDDECK_COMET_PENDING"); } + } + Err(err) => error!("Failed to read local database {:?}", err), + } +diff --git a/src/api/handlers/communication_service.rs b/src/api/handlers/communication_service.rs +index c1bed3b..1dda644 100644 +--- a/src/api/handlers/communication_service.rs ++++ b/src/api/handlers/communication_service.rs +@@ -35,6 +35,9 @@ pub async fn entry_point( + debug!("Handling in communication service"); + let header = &payload.header; + ++ if let Some(client) = context.client_id().await { ++ if !crate::client_allowed(&client) { return Err(MessageHandlingError::unauthorized()); } ++ } + let message_type: i32 = header.type_().try_into().unwrap(); + + if message_type == MessageType::LIBRARY_INFO_REQUEST.value() { +@@ -132,6 +135,7 @@ async fn auth_info_request( + + let client_id = request_data.client_id(); + let client_secret = request_data.client_secret(); ++ if !crate::client_allowed(client_id) { return Err(MessageHandlingError::unauthorized()); } + let openid = request_data.openid(); + let pid = request_data.game_pid(); + +@@ -551,6 +555,14 @@ async fn unlock_user_achievement( + .await + .expect("Failed to write achievement to database"); + context.set_updated_achievements(true).await; ++ eprintln!("DROIDDECK_COMET_UNLOCKED:{}", serde_json::json!({ ++ "client_id": context.client_id().await, ++ "id": achievement.achievement_key(), ++ "name": achievement.name(), ++ "description": achievement.description(), ++ "icon": achievement.image_url_unlocked(), ++ "unlocked_at": timestamp, ++ })); + achievement.date_unlocked = timestamp_string; + let pid = context.get_pid().await; + let _ = context +diff --git a/src/lib.rs b/src/lib.rs +index 0c539f1..50fdd21 100644 +--- a/src/lib.rs ++++ b/src/lib.rs +@@ -16,3 +16,10 @@ lazy_static! { + .and_then(|x| if !x.contains("-") { None } else { Some(x) }) + .unwrap_or_else(|| String::from("en-US")); + } ++ ++/// A launcher may restrict Galaxy to explicitly enabled SDK clients in its private policy file. ++pub fn client_allowed(client_id: &str) -> bool { ++ let Ok(path) = std::env::var("COMET_ALLOWED_CLIENTS") else { return true; }; ++ std::fs::read(path).ok().and_then(|data| serde_json::from_slice::>(&data).ok()) ++ .is_some_and(|clients| clients.iter().any(|id| id == client_id)) ++} +diff --git a/src/main.rs b/src/main.rs +index 109098f..8301edb 100644 +--- a/src/main.rs ++++ b/src/main.rs +@@ -41,11 +41,13 @@ enum SubCommand { + struct Args { + #[arg(long, help = "Provide access token (for getting user data)")] + access_token: Option, ++ #[arg(long, help = "Read credentials as JSON from stdin")] ++ credentials_stdin: bool, + #[arg(long, help = "Provide refresh token (for creating game sessions)")] + refresh_token: Option, + #[arg(long, help = "Galaxy user id from /userData.json")] + user_id: Option, +- #[arg(long, help = "User name")] ++ #[arg(long, help = "User name", default_value = "")] + username: String, + #[arg( + long = "from-heroic", +@@ -86,7 +88,18 @@ struct Args { + + #[tokio::main] + async fn main() { +- let args = Args::parse(); ++ let mut args = Args::parse(); ++ if args.credentials_stdin { ++ use std::io::Read; ++ let value: serde_json::Value = serde_json::from_reader(std::io::stdin().take(32768)) ++ .expect("Invalid credential input"); ++ let field = |key: &str| value.get(key).and_then(|v| v.as_str()) ++ .filter(|v| !v.is_empty()).expect("Missing credential field").to_owned(); ++ args.access_token = Some(field("access_token")); ++ args.refresh_token = Some(field("refresh_token")); ++ args.user_id = Some(field("user_id")); ++ args.username = field("username"); ++ } + #[cfg(debug_assertions)] + let log_level = "debug"; + #[cfg(not(debug_assertions))] +@@ -306,6 +319,7 @@ async fn main() { + }); + + info!("Listening on port 9977"); ++ eprintln!("DROIDDECK_COMET_READY:{}", std::process::id()); + let socket_shutdown = cloned_shutdown.clone(); + let cloned_user_info = user_info.clone(); + diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch index cb93faa44f..710c58dd69 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-store-launch @@ -30,6 +30,9 @@ import json import os import sys import time +import subprocess +import tempfile +import hashlib from pathlib import Path LAUNCHER = ".droiddeck-launch.bat" @@ -262,6 +265,76 @@ def exited(argv): return 0 +COMET_DIR = Path("/usr/local/lib/droiddeck-comet") + + +def provision_galaxy(compat_data, proton, timeout=90): + """Proton creates the prefix and registers Galaxy before the first game runs.""" + if not compat_data or not proton or not Path(proton).is_file(): + return "no-proton" + data = Path(compat_data) + data.mkdir(parents=True, exist_ok=True) + marker = data / "pfx/.droiddeck-comet-service" + try: + version = (COMET_DIR / "version").read_text().strip() + tools = [COMET_DIR / "GalaxyCommunication.exe", COMET_DIR / "update-permissions.exe"] + if not all(p.is_file() for p in tools): + return "not-installed" + stamp = version + " " + " ".join(hashlib.sha256(p.read_bytes()).hexdigest() for p in tools) + if marker.is_file() and marker.read_text() == stamp and (data / "pfx/system.reg").is_file(): + return "ready" + # cmd/sc run through this title's selected Proton. The run verb bypasses the managed + # wrapper, so this cannot recursively request credentials or sync cloud saves. + with tempfile.TemporaryDirectory(prefix="droiddeck-galaxy-") as tmp: + folder = Path(tmp) + result = folder / "result" + bat = folder / "install.bat" + src = windows_path(str(COMET_DIR)) + answer = windows_path(str(result)) + target = r"C:\ProgramData\GOG.com\Galaxy\redists" + lines = ["@echo off", "setlocal", 'set "DD_DIR=%s"' % target, + "sc query GalaxyCommunication >nul 2>&1", + 'if not errorlevel 1 if not exist "%DD_DIR%\\droiddeck-comet" goto existing', + 'if not exist "%DD_DIR%" mkdir "%DD_DIR%"', + 'copy /y "%s\\GalaxyCommunication.exe" "%s\\GalaxyCommunication.exe" >nul' % (src, target), + 'if errorlevel 1 exit /b 1', + 'sc query GalaxyCommunication >nul 2>&1', + 'if errorlevel 1 sc create GalaxyCommunication binpath= "%s\\GalaxyCommunication.exe"' % target, + 'if errorlevel 1 exit /b 1', + '"%s\\update-permissions.exe"' % src, + 'if errorlevel 1 exit /b 1', + 'sc query GalaxyCommunication >nul 2>&1', + 'if errorlevel 1 exit /b 1', + 'echo managed>"%s\\droiddeck-comet"' % target, + ':existing', 'echo ready>"%s"' % answer, 'exit /b 0'] + bat.write_bytes(("\r\n".join(lines) + "\r\n").encode()) + env = os.environ.copy() + env["BL_COMET_BOOTSTRAP"] = "1" + run = subprocess.run([proton, "run", "cmd.exe", "/d", "/c", windows_path(str(bat))], + env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=timeout) + if run.returncode or not result.is_file() or result.read_text().strip() != "ready": + return "service-unavailable" + marker.write_text(stamp) + return "ready" + except subprocess.TimeoutExpired: + return "timeout" + except (OSError, ValueError): + return "service-unavailable" + + +def prepare_gog(sidecar, compat_data, launch_dir): + if not launch_dir: + log("gog achievements unavailable: no-channel") + return False + answer = ask(Path(launch_dir) / "stores", {"op": "gog-achievements", "store": "gog", "id": str(sidecar.get("id"))}, timeout=25.0) + if not isinstance(answer, dict) or not answer.get("ready"): + log("gog achievements: " + (str(answer.get("reason", "unavailable")) if isinstance(answer, dict) else "timeout")) + return False + state = provision_galaxy(compat_data, os.environ.get("BL_STORE_PROTON", "")) + log("gog achievements prefix: " + state) + return state == "ready" + + def main(argv): if argv and argv[0] == "--exited": return exited(argv[1:]) @@ -273,6 +346,8 @@ def main(argv): compat_data = os.environ.get("STEAM_COMPAT_DATA_PATH", "").rstrip("/") prefix_id = os.path.basename(compat_data) if compat_data else "" launch_dir = os.environ.get("BL_LAUNCH_DIR", "") + if store == "gog": + prepare_gog(sidecar, compat_data, launch_dir) if store == "epic": drop_stale(folder) choice = options(sidecar) diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index ab205ce27a..b64d721b74 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -435,6 +435,7 @@ if [ -n "$depot" ]; then bl_game_cpus bl_idtech3 "$@" bl_libpath + BL_STORE_PROTON="$depot/proton"; export BL_STORE_PROTON bl_store_launch "$@" BL_STORE_ARGS=("$@") bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$depot/proton" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" @@ -503,11 +504,12 @@ bl_directaudio "$here" bl_game_cpus bl_idtech3 "$@" bl_libpath +BL_STORE_PROTON="$here/%s"; export BL_STORE_PROTON bl_store_launch "$@" BL_STORE_ARGS=("$@") # Proton takes its base directory from dirname(sys.argv[0]) without resolving it, so it has to be # started from its own tree. -bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$here/%s" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" +bl_run ${BL_TASKSET:-} /usr/local/bin/droiddeck-game-env "$BL_STORE_PROTON" "$verb" "$@" "${BL_EXTRA_ARGS[@]}" """ EXTRA_COMPAT_TOOL_VDF = """"compatibilitytools" { diff --git a/tools/release/runtime_inputs.py b/tools/release/runtime_inputs.py index b332ee36ad..b4108dbc25 100644 --- a/tools/release/runtime_inputs.py +++ b/tools/release/runtime_inputs.py @@ -15,7 +15,7 @@ MANIFEST = Path('app/build/runtime-inputs.json') SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app/src/main/assets/pulseaudio.tzst', 'tools/linuxfs', 'tools/proot', 'tools/aaudio-sink', 'tools/directaudio', 'tools/gamescope/release.env', 'tools/droiddeck-esync/release.env', - 'tools/mangoapp', 'tools/msitools', 'tools/build_local.sh', + 'tools/mangoapp', 'tools/msitools', 'tools/comet', 'tools/build_local.sh', 'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal'] @@ -64,6 +64,8 @@ def required(): ['msiinfo', 'cabextract', '7z', '7z.so', 'libmsi-1.0.so.0', 'libgsf-1.so.114', 'libgcab-1.0.so.0']] paths += ['assets/linuxfs/usr/local/lib/mangoapp/' + name for name in ['mangoapp', 'libfmt.so.10', 'libspdlog.so.1.13', 'libglfw.so.3', 'libtraceevent.so.1', 'libtracefs.so.1']] + paths += ['assets/linuxfs/usr/local/lib/droiddeck-comet/' + name for name in + ['comet', 'GalaxyCommunication.exe', 'update-permissions.exe', 'LICENSE', 'version']] for driver in ['linux-wine11', 'linux-wine11-systhread']: paths += ['assets/directaudio/' + driver + '/' + name for name in ['aarch64-unix/winedirectaudio.so', 'aarch64-windows/winedirectaudio.drv', diff --git a/tools/tests/test_store_launch.py b/tools/tests/test_store_launch.py index 46a255f758..014be591e0 100644 --- a/tools/tests/test_store_launch.py +++ b/tools/tests/test_store_launch.py @@ -10,6 +10,7 @@ import tempfile import threading import time import unittest +from unittest.mock import patch BIN = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin' @@ -64,15 +65,17 @@ class Detection(unittest.TestCase): def test_only_epic_asks_for_a_code(self): with tempfile.TemporaryDirectory() as root: folder = game(root, store_id='gog') - # Cloud saves off: nothing else to ask for either. + # Cloud saves off: GOG still prepares achievements, never an Epic exchange code. sc = json.loads((folder / '.droiddeck-store.json').read_text()); sc['cloud'] = False (folder / '.droiddeck-store.json').write_text(json.dumps(sc)) os.environ['BL_LAUNCH_DIR'] = str(Path(root) / 'session') try: - self.assertEqual(0, store.main([str(folder / '.droiddeck-launch.bat')])) + with patch.object(store, 'ask', return_value={'ready': False, 'reason': 'off'}) as ask: + self.assertEqual(0, store.main([str(folder / '.droiddeck-launch.bat')])) + self.assertEqual('gog-achievements', ask.call_args.args[1]['op']) finally: del os.environ['BL_LAUNCH_DIR'] - self.assertFalse((Path(root) / 'session/stores').exists()) + class Asking(unittest.TestCase): @@ -225,9 +228,10 @@ class CloudHooks(unittest.TestCase): finally: store.ask = original del os.environ['BL_LAUNCH_DIR'] - self.assertEqual({'op': 'cloud-down', 'store': 'gog', 'id': '1207664643'}, asked[0][0]) - self.assertEqual(store.CLOUD_TIMEOUT, asked[0][1]) - self.assertEqual('cloud-up', asked[1][0]['op']) + self.assertEqual('gog-achievements', asked[0][0]['op']) + self.assertEqual({'op': 'cloud-down', 'store': 'gog', 'id': '1207664643'}, asked[1][0]) + self.assertEqual(store.CLOUD_TIMEOUT, asked[1][1]) + self.assertEqual('cloud-up', asked[2][0]['op']) def test_cloud_off_or_amazon_runs_proton_as_before(self): with tempfile.TemporaryDirectory() as root: @@ -260,3 +264,61 @@ class Launchers(unittest.TestCase): if __name__ == '__main__': unittest.main() + + +class GogAchievements(unittest.TestCase): + def test_cloud_off_still_prepares_the_gog_companion(self): + with tempfile.TemporaryDirectory() as root: + folder = game(root, store_id='gog') + sidecar = json.loads((folder / store.SIDECAR).read_text()) + sidecar['cloud'] = False + (folder / store.SIDECAR).write_text(json.dumps(sidecar)) + with patch.dict(os.environ, {'BL_LAUNCH_DIR': root, 'STEAM_COMPAT_DATA_PATH': root + '/compat', 'BL_STORE_PROTON': '/chosen/proton'}), patch.object(store, 'ask', return_value={'ready': True}) as ask, patch.object(store, 'provision_galaxy', return_value='ready') as provision: + self.assertEqual(0, store.main([str(folder / store.LAUNCHER)])) + ask.assert_called_once() + provision.assert_called_once_with(root + '/compat', '/chosen/proton') + + def test_unavailable_companion_does_not_provision_or_block_the_game(self): + with patch.object(store, 'ask', return_value=None), patch.object(store, 'provision_galaxy') as provision: + self.assertFalse(store.prepare_gog({'id': '1', 'achievements': False}, '/pfx', '/session')) + provision.assert_not_called() + + def test_a_fresh_prefix_is_bootstrapped_before_marking_success(self): + with tempfile.TemporaryDirectory() as root: + base = Path(root) + tools = base / 'tools'; tools.mkdir() + for name in ('GalaxyCommunication.exe', 'update-permissions.exe', 'version'): + (tools / name).write_text(name) + proton = base / 'proton'; proton.write_text('selected Proton') + data = base / 'compat' + seen = [] + def run(argv, **kwargs): + self.assertEqual([str(proton), 'run', 'cmd.exe', '/d', '/c'], argv[:5]) + bat = Path(argv[5][2:].replace('\\', '/')) + text = bat.read_text() + seen.append(text) + self.assertIn('goto existing', text) + self.assertIn('update-permissions.exe', text) + result = text.split('echo ready>"')[1].split('"')[0] + Path(result[2:].replace('\\', '/')).write_text('ready') + (data / 'pfx').mkdir(parents=True) + (data / 'pfx/system.reg').write_text('Wine registry') + return __import__('subprocess').CompletedProcess(argv, 0) + with patch.object(store, 'COMET_DIR', tools), patch.object(store.subprocess, 'run', side_effect=run): + self.assertEqual('ready', store.provision_galaxy(str(data), str(proton))) + self.assertTrue((data / 'pfx/.droiddeck-comet-service').is_file()) + self.assertEqual('ready', store.provision_galaxy(str(data), str(proton))) + self.assertEqual(1, len(seen)) + # A recreated prefix must be provisioned again, even with the same Proton. + __import__('shutil').rmtree(data / 'pfx') + self.assertEqual('ready', store.provision_galaxy(str(data), str(proton))) + self.assertEqual(2, len(seen)) + + def test_failed_service_install_is_not_marked_done(self): + with tempfile.TemporaryDirectory() as root: + base = Path(root) + for name in ('GalaxyCommunication.exe', 'update-permissions.exe', 'version', 'proton'): + (base / name).write_text(name) + with patch.object(store, 'COMET_DIR', base), patch.object(store.subprocess, 'run', return_value=__import__('subprocess').CompletedProcess([], 1)): + self.assertEqual('service-unavailable', store.provision_galaxy(str(base / 'compat'), str(base / 'proton'))) + self.assertFalse((base / 'compat/pfx/.droiddeck-comet-service').exists())