From a56d4e38be8893daf6079b335971519d0cbbc76a Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:35:07 -0500 Subject: [PATCH] feat(recomps): Linux recomps become non-Steam games, and Android installs explain failures Each Linux recomp gets a play script and becomes a native non-Steam shortcut: written to the session's shortcut listing next to the Epic launcher, added to a running Steam client at once, and removed with the recomp. They also show in a Recomps chip in the Library and always start as the Linux app. Windows recomps were already added games. An APK without code for the device's CPUs (a 64-bit-only device and a 32-bit-only port) is stopped before Android's prompt, remembered as not for this device, and skipped when choosing a build. Install failures say what went wrong, name the game and stay on its page. Also finds AppImage recomps again: the scan looked one folder too high. --- .../droiddeck/launcher/FrontEndActivity.kt | 22 ++++ .../droiddeck/launcher/frontend/AddedGames.kt | 3 +- .../droiddeck/launcher/frontend/Library.kt | 7 +- .../launcher/runtime/RecompAndroid.kt | 61 ++++++++++- .../launcher/runtime/RecompCatalog.kt | 4 + .../launcher/runtime/RecompInstaller.kt | 5 +- .../launcher/runtime/RecompShortcuts.kt | 101 ++++++++++++++++++ .../droiddeck/launcher/store/RecompsState.kt | 34 +++++- .../com/droiddeck/launcher/ui/LibraryPage.kt | 2 + .../com/droiddeck/launcher/ui/ManageStores.kt | 7 +- .../com/droiddeck/launcher/ui/RecompsPage.kt | 25 ++--- .../com/droiddeck/launcher/ui/StoreChips.kt | 1 + app/src/main/res/values/strings.xml | 12 +++ .../launcher/runtime/RecompShortcutsTest.kt | 78 ++++++++++++++ .../launcher/ui/LibraryEntriesTest.kt | 4 +- .../launcher/ui/LibraryHiddenTest.kt | 2 +- docs/development/recomps.md | 33 ++++++ 17 files changed, 370 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/com/droiddeck/launcher/runtime/RecompShortcuts.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/runtime/RecompShortcutsTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index 0a4fbb3b5c..bef6de5a9a 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -1038,6 +1038,28 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet onHandled() return } + // A Linux recomp starts as the Linux app it is, whether or not Steam would launch it. + if (game.source == com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE) { + launchCoordinator.releaseHandling(request) + onHandled() + val key = game.storeId + Thread({ + val app = com.droiddeck.launcher.store.UserAppsState.items.firstOrNull { it.key == key } + ?: runCatching { com.droiddeck.launcher.runtime.UserApps.list(this).firstOrNull { it.key == key } }.getOrNull() + ui.post { + if (app == null) android.widget.Toast.makeText(this, R.string.recomps_launch_missing, android.widget.Toast.LENGTH_SHORT).show() + else { + Library.recordPlayed(this, game) + startActivity(Intent(this, SessionActivity::class.java) + .putExtra(SessionService.EXTRA_MODE, SessionService.MODE_RUN) + .putExtra(SessionService.EXTRA_PROGRAM, app.program) + .putExtra(SessionService.EXTRA_PROGRAM_ARGS, app.args.toTypedArray())) + onSettled() + } + } + }, "recomp-launch").start() + return + } val files = game.gameFiles if (game.source != com.droiddeck.launcher.stores.Store.XBOX.id || files == null) { if (startGame(game, returnHome, onHandled, request)) onSettled() diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt index b26c2536cc..e6804160c0 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/AddedGames.kt @@ -586,7 +586,8 @@ object AddedGames { }.getOrNull() fun writeListing(context: Context, games: List, - extra: List = listOfNotNull(com.droiddeck.launcher.stores.epic.EpicClient.steamListing(context))): File { + extra: List = listOfNotNull(com.droiddeck.launcher.stores.epic.EpicClient.steamListing(context)) + + com.droiddeck.launcher.runtime.RecompShortcuts.listing(context)): File { val file = listingFile(context).apply { parentFile?.mkdirs() } val json = StringBuilder("[") games.forEachIndexed { i, g -> diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt index 26c9c99813..1c196a48ae 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -248,11 +248,14 @@ object Library { AddedGames.guestPath(context, g.folder).let { it == null || it !in claimed }) } val played = shortcutsLastPlayed(SessionPrefs.steamRoot(context, arch)) - return (steam + own.map { g -> addedGame(context, g, roots, played[g.appId] ?: 0L) }).distinctBy { it.gameId } + val recomps = com.droiddeck.launcher.runtime.RecompShortcuts.library(context) { appId, gameId -> + maxOf(played[appId] ?: 0L, recordedPlay(context, gameId)) + } + return (steam + own.map { g -> addedGame(context, g, roots, played[g.appId] ?: 0L) } + recomps).distinctBy { it.gameId } } fun recordPlayed(context: Context, game: SteamGame) { - if (game.library == ADDED) recordPlayed(context, game.gameIdString) + if (game.library == ADDED || game.library == com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE) recordPlayed(context, game.gameIdString) } fun recordPlayed(context: Context, gameId: String) { diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RecompAndroid.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RecompAndroid.kt index 974e5d7525..28bbc9f5eb 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RecompAndroid.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RecompAndroid.kt @@ -18,8 +18,33 @@ object RecompAndroid { const val ACTION_STATUS = "com.droiddeck.launcher.recomps.INSTALL_STATUS" private const val RECORDS = "recomps-android" + private const val INCOMPATIBLE = "incompatible" + @Volatile private var registered = false @Volatile var onChanged: () -> Unit = {} + // Why the last install of each package failed, in words for the player. + @Volatile var onFailed: (pkg: String, message: String) -> Unit = { _, _ -> } + private val sessions = java.util.concurrent.ConcurrentHashMap>() + + private fun incompatibleFile(context: Context) = File(File(context.filesDir, RECORDS).apply { mkdirs() }, INCOMPATIBLE) + + // Packages whose APK has no code for this device's CPUs, found when one was downloaded or refused. + fun incompatible(context: Context): Set = + FileUtils.readString(incompatibleFile(context))?.lines()?.map { it.trim() }?.filter { it.isNotEmpty() }?.toSet().orEmpty() + + private fun markIncompatible(context: Context, pkg: String) { + val now = incompatible(context) + pkg + FileUtils.writeString(incompatibleFile(context), now.joinToString("\n")) + } + + // The CPUs an APK has native code for, from its lib// folders; empty when it has none (Java only). + internal fun apkAbis(apk: File): Set = runCatching { + java.util.zip.ZipFile(apk).use { zip -> + zip.entries().asSequence().map { it.name }.filter { it.startsWith("lib/") }.mapNotNull { it.split('/').getOrNull(1)?.takeIf { a -> a.isNotEmpty() } }.toSet() + } + }.getOrDefault(emptySet()) + + internal fun fits(abis: Set, device: List): Boolean = abis.isEmpty() || abis.any { it in device } private fun record(context: Context, pkg: String) = File(File(context.filesDir, RECORDS), pkg) @@ -38,6 +63,12 @@ object RecompAndroid { // names, so a replaced release asset cannot install as, or over, a different app. fun install(context: Context, entry: RecompCatalog.Entry, build: RecompCatalog.Build, release: RecompInstaller.Release, apk: File): String? { val pkg = build.pkg ?: return context.getString(R.string.recomps_apk_mismatch) + val abis = apkAbis(apk) + val device = Build.SUPPORTED_ABIS.toList() + if (!fits(abis, device)) { + markIncompatible(context, pkg) + return context.getString(R.string.recomps_apk_wrong_cpu, abis.sorted().joinToString(", "), device.joinToString(", ")) + } if (!SelfInstaller.canInstall(context)) { context.startActivity(SelfInstaller.permissionIntent(context).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) return context.getString(R.string.recomps_allow_installs) @@ -48,7 +79,7 @@ object RecompAndroid { if (build.signer != null && build.signer !in SelfInstaller.archiveSignerDigests(info)) return context.getString(R.string.recomps_apk_mismatch) listen(context) FileUtils.writeString(record(context, pkg).apply { parentFile?.mkdirs() }, "${release.tag}\n${release.published}\n") - SelfInstaller.install(context, apk, pkg, ACTION_STATUS) + SelfInstaller.install(context, apk, pkg, ACTION_STATUS) { id -> sessions[id] = pkg to entry.name } return null } @@ -70,7 +101,11 @@ object RecompAndroid { val app = context.applicationContext val receiver = object : BroadcastReceiver() { override fun onReceive(c: Context, intent: Intent) { - when (val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE)) { + val session = intent.getIntExtra(PackageInstaller.EXTRA_SESSION_ID, -1) + val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, PackageInstaller.STATUS_FAILURE) + val (pkg, name) = (if (status == PackageInstaller.STATUS_PENDING_USER_ACTION) sessions[session] else sessions.remove(session)) + ?: (intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME).orEmpty() to "") + when (status) { PackageInstaller.STATUS_PENDING_USER_ACTION -> { val confirm = if (Build.VERSION.SDK_INT >= 33) intent.getParcelableExtra(Intent.EXTRA_INTENT, Intent::class.java) else @Suppress("DEPRECATION") intent.getParcelableExtra(Intent.EXTRA_INTENT) @@ -78,8 +113,12 @@ object RecompAndroid { } PackageInstaller.STATUS_SUCCESS -> onChanged() else -> { - val message = SelfInstaller.describeFailure(app, status, intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE)) - Toast.makeText(app, message, Toast.LENGTH_LONG).show() + val raw = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE) + if (raw?.contains("NO_MATCHING_ABIS") == true && pkg.isNotEmpty()) markIncompatible(app, pkg) + val message = failure(app, status, raw) + Toast.makeText(app, if (name.isNotEmpty()) app.getString(R.string.recomps_apk_failed_named, name, message) else message, + Toast.LENGTH_LONG).show() + if (pkg.isNotEmpty()) onFailed(pkg, message) onChanged() } } @@ -89,4 +128,18 @@ object RecompAndroid { if (Build.VERSION.SDK_INT >= 33) app.registerReceiver(receiver, filter, Context.RECEIVER_NOT_EXPORTED) else app.registerReceiver(receiver, filter) } + + // What went wrong, in words: Android's own status message names the cause but not what to do about it. + internal fun failure(context: Context, status: Int, message: String?): String = when { + message?.contains("NO_MATCHING_ABIS") == true || status == PackageInstaller.STATUS_FAILURE_INCOMPATIBLE -> + context.getString(R.string.recomps_apk_fail_cpu, Build.SUPPORTED_ABIS.joinToString(", ")) + message?.contains("UPDATE_INCOMPATIBLE") == true || status == PackageInstaller.STATUS_FAILURE_CONFLICT -> + context.getString(R.string.recomps_apk_fail_conflict) + message?.contains("VERSION_DOWNGRADE") == true -> context.getString(R.string.recomps_apk_fail_downgrade) + status == PackageInstaller.STATUS_FAILURE_STORAGE -> context.getString(R.string.recomps_apk_fail_storage) + status == PackageInstaller.STATUS_FAILURE_ABORTED -> context.getString(R.string.recomps_apk_fail_cancelled) + status == PackageInstaller.STATUS_FAILURE_BLOCKED -> context.getString(R.string.recomps_apk_fail_blocked) + status == PackageInstaller.STATUS_FAILURE_INVALID -> context.getString(R.string.recomps_apk_fail_invalid) + else -> context.getString(R.string.recomps_apk_fail_other, message ?: status.toString()) + } } diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RecompCatalog.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RecompCatalog.kt index b581c051d9..6280d3dab6 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RecompCatalog.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RecompCatalog.kt @@ -78,6 +78,10 @@ object RecompCatalog { fun preferred(): Build? = PREFERENCE.firstNotNullOfOrNull { build(it) } + // The preferred build, passing over an Android one whose package cannot install on this device. + fun preferred(incompatible: Set): Build? = + PREFERENCE.firstNotNullOfOrNull { p -> build(p)?.takeIf { it.pkg == null || it.pkg !in incompatible } } + val android get() = build(Platform.ANDROID) val pc get() = origin in PC_ORIGINS diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RecompInstaller.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RecompInstaller.kt index 5142cc984f..9ea5387fcf 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/RecompInstaller.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RecompInstaller.kt @@ -35,7 +35,7 @@ object RecompInstaller { out[id] = Installed(id, "$prefix:${dir.name}", read(dir, META_TAG), read(dir, META_PUBLISHED), read(dir, META_PLATFORM), dir, read(dir, META_REPO)) } scan(UserApps.root(context), "script") - AppImageManager.dir(context, "").parentFile?.let { scan(it, "appimage") } + AppImageManager.dir(context, "x").parentFile?.let { scan(it, "appimage") } windowsRoots(context).forEach { scan(it, "added") } return out } @@ -186,6 +186,9 @@ object RecompInstaller { if (previous.platform != platform.key) uninstall(context, previous) else if (previous.dir != windowsTarget(context, entry)) FileUtils.delete(previous.dir) } + if (problem == null && platform != RecompCatalog.Platform.WINDOWS_X64) { + installed(context)[entry.id]?.let { runCatching { RecompShortcuts.prepare(entry, it) } } + } return problem } catch (e: Exception) { return e.message ?: context.getString(R.string.user_apps_failed) diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/RecompShortcuts.kt b/app/src/main/java/com/droiddeck/launcher/runtime/RecompShortcuts.kt new file mode 100644 index 0000000000..52e8b53ed4 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/runtime/RecompShortcuts.kt @@ -0,0 +1,101 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import android.system.Os +import android.util.Log +import com.droiddeck.launcher.core.Downloader +import com.droiddeck.launcher.core.FileUtils +import com.droiddeck.launcher.frontend.Library +import com.droiddeck.launcher.stores.SteamLiveShortcuts +import org.json.JSONObject +import java.io.File +import java.util.zip.CRC32 + +// Each Linux recomp (a script app or an AppImage) is also a non-Steam game: a native shortcut, without a +// compatibility tool, to a small `play` script beside the install. Windows recomps are added games, so Steam +// already has them; Android apps cannot be Steam shortcuts. +object RecompShortcuts { + private const val TAG = "RecompShortcuts" + const val SOURCE = "recomp" + private const val PLAY = "play" + private const val COVER = "cover" + private const val ICON = "icon.png" + + class Shortcut( + val id: String, val name: String, val appKey: String, val exe: String, val dir: String, val appId: Long, + val cover: File?, val icon: File?, val host: File, + ) { + val gameId: Long get() = (appId shl 32) or 0x02000000L + } + + // Steam's own ID for a non-Steam shortcut: a CRC32 of the quoted program and the name, with the top bit set. + fun appId(exe: String, name: String): Long = CRC32().apply { update("\"$exe\"$name".toByteArray()) }.value or 0x80000000L + + private fun guestDir(inst: RecompInstaller.Installed): String? = when { + inst.appKey.startsWith("script:") -> "${UserApps.GUEST_DIR}/${inst.dir.name}" + inst.appKey.startsWith("appimage:") -> "${AppImageManager.GUEST_DIR}/${inst.dir.name}" + else -> null + } + + internal fun playScript(inst: RecompInstaller.Installed, guest: String): String { + val launcher = if (inst.appKey.startsWith("appimage:")) AppImageManager.LAUNCHER else UserApps.SCRIPT_LAUNCHER + return "#!/bin/sh\nexec $launcher '$guest' \"\$@\"\n" + } + + private fun writePlay(inst: RecompInstaller.Installed, guest: String): File? = runCatching { + File(inst.dir, PLAY).also { f -> + val text = playScript(inst, guest) + if (FileUtils.readString(f) != text) FileUtils.writeString(f, text) + runCatching { Os.chmod(f.path, 0b111_101_101) } + f.setExecutable(true, false) + } + }.onFailure { Log.w(TAG, "play ${inst.dir}: $it") }.getOrNull() + + // Writes the play script and saves the catalog's art beside the install, for Steam's library and the Library tab. + fun prepare(entry: RecompCatalog.Entry, inst: RecompInstaller.Installed) { + val guest = guestDir(inst) ?: return + writePlay(inst, guest) + fun save(url: String?, name: String) { + if (url.isNullOrEmpty() || File(inst.dir, name).isFile) return + val tmp = File(inst.dir, "$name.part") + if (runCatching { Downloader.downloadFile(url, tmp, false) { } }.getOrDefault(false)) tmp.renameTo(File(inst.dir, name)) else tmp.delete() + } + save(entry.cover, COVER + "." + (entry.cover?.substringAfterLast('.')?.substringBefore('?')?.lowercase()?.takeIf { it in setOf("png", "jpg", "jpeg", "webp") } ?: "jpg")) + if (!File(inst.dir, ICON).isFile) save(entry.art, ICON) + } + + fun all(context: Context): List = RecompInstaller.installed(context).values.mapNotNull { inst -> + val guest = guestDir(inst) ?: return@mapNotNull null + val play = File(inst.dir, PLAY).takeIf { it.isFile } ?: writePlay(inst, guest) ?: return@mapNotNull null + val name = FileUtils.readString(File(inst.dir, "name"))?.trim()?.takeIf { it.isNotEmpty() } ?: inst.id + val exe = "$guest/${play.name}" + Shortcut(inst.id, name, inst.appKey, exe, guest, appId(exe, name), + inst.dir.listFiles()?.firstOrNull { it.name.startsWith("$COVER.") && it.isFile }, File(inst.dir, ICON).takeIf { it.isFile }, inst.dir) + } + + // Entries for the session's shortcut listing (droiddeck-steam-shortcuts): native, so no compatibility tool. + fun listing(context: Context): List = runCatching { all(context) }.getOrDefault(emptyList()).map { s -> + JSONObject().put("name", s.name).put("exe", s.exe).put("folder", s.dir).put("dir", s.dir).put("appid", s.appId) + .put("seen", 0).put("native", true).also { o -> + val art = JSONObject() + s.cover?.let { art.put("p", it.absolutePath) } + s.icon?.let { art.put("icon", it.absolutePath) } + if (art.length() > 0) o.put("art", art) + } + } + + fun library(context: Context, lastPlayed: (appId: Long, gameId: Long) -> Long = { _, _ -> 0L }): List = + runCatching { all(context) }.getOrDefault(emptyList()).map { s -> + Library.SteamGame( + s.appId.toInt(), s.name, s.cover ?: s.icon, SOURCE, s.gameId, lastPlayed = lastPlayed(s.appId, s.gameId), icon = s.icon, + source = SOURCE, storeId = s.appKey, installedAt = s.host.lastModified(), + ) + } + + // Adds the shortcut to a running Steam client now; the listing covers the next session either way. + fun addLive(context: Context, id: String) { + all(context).firstOrNull { it.id == id }?.let { s -> SteamLiveShortcuts.ensure(context, s.name, s.exe, s.dir, compat = false, appId = s.appId) } + } + + fun removeLive(context: Context, appId: Long) = runCatching { SteamLiveShortcuts.remove(context, appId) } +} diff --git a/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt b/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt index fd7c0f1fe3..550890d786 100644 --- a/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt +++ b/app/src/main/java/com/droiddeck/launcher/store/RecompsState.kt @@ -25,6 +25,11 @@ object RecompsState { private set var loading by mutableStateOf(false) private set + var incompatible by mutableStateOf>(emptySet()) + private set + // The last install failure of each recomp, kept on its page until the next attempt. + var errors by mutableStateOf>(emptyMap()) + private set private var seq = 0 private var checked = false private var lastSteam: List> = emptyList() @@ -44,6 +49,9 @@ object RecompsState { val app = context.applicationContext lastSteam = steamOwned.toList() RecompAndroid.onChanged = { main.post { refresh(app, lastSteam) } } + RecompAndroid.onFailed = { pkg, message -> + main.post { entries.filter { it.android?.pkg == pkg }.forEach { errors = errors + (it.id to message) } } + } val n = ++seq loading = entries.isEmpty() Thread({ @@ -60,9 +68,10 @@ object RecompsState { val inputs = runCatching { RecompData.inputs(app, steamOwned, list) }.getOrNull() val states = list.associate { e -> e.id to (inputs?.let { RecompData.resolve(e, it) } ?: RecompData.State.BringYourOwn(e.data)) } inputs?.done?.invoke() + val apks = runCatching { RecompAndroid.incompatible(app) }.getOrDefault(emptySet()) main.post { if (n != seq) return@post - entries = list; installed = have; data = states; loading = false + entries = list; installed = have; data = states; incompatible = apks; loading = false } if (!checked && have.isNotEmpty()) { checked = true @@ -83,15 +92,36 @@ object RecompsState { context: Context, entry: RecompCatalog.Entry, steamOwned: Collection>, release: RecompInstaller.Release? = null, chosen: RecompCatalog.Build? = null, ) { - val build = chosen ?: installed[entry.id]?.platform?.let { RecompCatalog.Platform.of(it) }?.let { buildFor(entry, it) } ?: entry.preferred() ?: return + val build = chosen ?: installed[entry.id]?.platform?.let { RecompCatalog.Platform.of(it) }?.let { buildFor(entry, it) } + ?: entry.preferred(incompatible) ?: return + errors = errors - entry.id val done = if (build.platform == RecompCatalog.Platform.ANDROID) R.string.recomps_android_started else R.string.recomps_installed if (chosen != null) choose(context, entry.id, chosen.platform) UserAppsState.run(context, entry.name, done, after = { updates = updates - entry.id + UserAppsState.lastError?.takeIf { it.startsWith(entry.name + ":") }?.let { errors = errors + (entry.id to it.removePrefix(entry.name + ":").trim()) } refresh(context, steamOwned) + com.droiddeck.launcher.frontend.AddedGamesPublisher.later(context) { com.droiddeck.launcher.runtime.RecompShortcuts.addLive(context.applicationContext, entry.id) } }) { app, progress -> RecompInstaller.install(app, entry, build, release, progress, allowTwin = chosen == null) } } + // Removes an installed recomp and, once it is gone, its non-Steam shortcut. + fun remove(context: Context, entry: RecompCatalog.Entry, steamOwned: Collection>, inst: RecompInstaller.Installed) { + val app = context.applicationContext + if (inst.appKey.startsWith("android:")) { + RecompAndroid.uninstall(app, inst.appKey.removePrefix("android:")) + return + } + val shortcut = runCatching { com.droiddeck.launcher.runtime.RecompShortcuts.all(app).firstOrNull { it.appKey == inst.appKey }?.appId }.getOrNull() + UserAppsState.run(app, entry.name, R.string.recomps_removed, after = { + refresh(app, steamOwned) + com.droiddeck.launcher.frontend.AddedGamesPublisher.later(app) { shortcut?.let { com.droiddeck.launcher.runtime.RecompShortcuts.removeLive(app, it) } } + }) { c, p -> + if (inst.appKey.startsWith("added:")) { RecompInstaller.remove(c, inst); null } + else com.droiddeck.launcher.runtime.UserApps.list(c).firstOrNull { it.key == inst.appKey }?.let { com.droiddeck.launcher.runtime.UserApps.remove(c, it, p) } + } + } + private fun choicesFile(context: Context) = java.io.File(java.io.File(context.filesDir, "recomps").apply { mkdirs() }, "choices.json") private fun choices(context: Context): Map = runCatching { diff --git a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt index 0b8debbe0d..228ea3842b 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/LibraryPage.kt @@ -935,6 +935,7 @@ private fun LibraryFilters(sources: List, source: String, onSource: (Str val label = when (id) { LIBRARY_ALL -> stringResource(R.string.stores_tab_all) Library.ADDED -> stringResource(R.string.library_custom_games) + com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE -> stringResource(R.string.rail_recomps) LIBRARY_FAVORITES -> stringResource(R.string.soundtracks_favorites) LIBRARY_HIDDEN -> stringResource(R.string.apps_filter_hidden) else -> store?.shortLabel ?: sourceLabel(id) @@ -967,6 +968,7 @@ private fun LibraryFilters(sources: List, source: String, onSource: (Str private fun sourceTitle(src: String): String = when (src) { LIBRARY_ALL -> stringResource(R.string.library_all_games) Library.ADDED -> stringResource(R.string.library_custom_games) + com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE -> stringResource(R.string.rail_recomps) LIBRARY_FAVORITES -> stringResource(R.string.soundtracks_favorites) LIBRARY_HIDDEN -> stringResource(R.string.apps_filter_hidden) else -> Store.byId(src)?.label ?: sourceLabel(src) diff --git a/app/src/main/java/com/droiddeck/launcher/ui/ManageStores.kt b/app/src/main/java/com/droiddeck/launcher/ui/ManageStores.kt index add703ce09..4a2aa21462 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/ManageStores.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/ManageStores.kt @@ -119,12 +119,12 @@ internal object StoreTabsState { } /** The Library tabs that are not stores but sort among them in Manage stores. Only Favorites, Custom games and Hidden can be turned off; Hidden starts off. */ -internal val LIBRARY_OWN_TABS = listOf(LIBRARY_FAVORITES, Library.SOURCE_STEAM, Library.ADDED, LIBRARY_HIDDEN) -internal val LIBRARY_HIDEABLE_TABS = setOf(LIBRARY_FAVORITES, Library.ADDED, LIBRARY_HIDDEN) +internal val LIBRARY_OWN_TABS = listOf(LIBRARY_FAVORITES, Library.SOURCE_STEAM, Library.ADDED, com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE, LIBRARY_HIDDEN) +internal val LIBRARY_HIDEABLE_TABS = setOf(LIBRARY_FAVORITES, Library.ADDED, com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE, LIBRARY_HIDDEN) /** Favorites, then Steam, ahead of the stores; Custom games and Hidden after them (null: the end). */ internal val DEFAULT_LIBRARY_PLACES: List> = - listOf(LIBRARY_FAVORITES to "", Library.SOURCE_STEAM to LIBRARY_FAVORITES, Library.ADDED to null, LIBRARY_HIDDEN to null) + listOf(LIBRARY_FAVORITES to "", Library.SOURCE_STEAM to LIBRARY_FAVORITES, Library.ADDED to null, com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE to Library.ADDED, LIBRARY_HIDDEN to null) /** * Puts the Library's own tabs into a store tab order. Each goes after the tab it follows ("" for first; null or a tab that is gone @@ -339,6 +339,7 @@ private fun LibraryTabSortRow(id: String, held: Boolean, offset: Float, hint: St val label = when (id) { LIBRARY_FAVORITES -> stringResource(R.string.soundtracks_favorites) Library.ADDED -> stringResource(R.string.library_custom_games) + com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE -> stringResource(R.string.rail_recomps) LIBRARY_HIDDEN -> stringResource(R.string.apps_filter_hidden) else -> stringResource(R.string.source_steam) } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt b/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt index 7fa744791a..df5b076942 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/RecompsPage.kt @@ -97,13 +97,16 @@ internal object RecompsView { internal class RecompRow( val entry: RecompCatalog.Entry, val data: RecompData.State, val installed: RecompInstaller.Installed?, - val update: RecompInstaller.Release?, + val update: RecompInstaller.Release?, val incompatible: Set = emptySet(), ) { - val platform get() = installed?.platform?.let { RecompCatalog.Platform.of(it) } ?: entry.preferred()?.platform + val platform get() = installed?.platform?.let { RecompCatalog.Platform.of(it) } ?: entry.preferred(incompatible)?.platform + + fun runs(build: RecompCatalog.Build) = build.pkg == null || build.pkg !in incompatible } internal fun recompRows(): List = RecompsState.entries.map { e -> - RecompRow(e, RecompsState.data[e.id] ?: RecompData.State.BringYourOwn(e.data), RecompsState.installed[e.id], RecompsState.updates[e.id]) + RecompRow(e, RecompsState.data[e.id] ?: RecompData.State.BringYourOwn(e.data), RecompsState.installed[e.id], RecompsState.updates[e.id], + RecompsState.incompatible) } private val NATIVE = setOf(RecompCatalog.Platform.LINUX_ARM64, RecompCatalog.Platform.ANDROID) @@ -166,7 +169,7 @@ internal fun RecompsPage(s: FrontEndState, a: FrontEndActions, onSelect: (String lifecycle.addObserver(observer) onDispose { lifecycle.removeObserver(observer) } } - val rows = remember(RecompsState.entries, RecompsState.data, RecompsState.installed, RecompsState.updates) { recompRows() } + val rows = remember(RecompsState.entries, RecompsState.data, RecompsState.installed, RecompsState.updates, RecompsState.incompatible) { recompRows() } val opened = RecompsView.open?.let { id -> rows.firstOrNull { it.entry.id == id } } if (opened != null) { BackHandler { RecompsView.open = null } @@ -459,6 +462,7 @@ private fun RecompDetail( else Row(horizontalArrangement = Arrangement.spacedBy(22.dp), modifier = Modifier.padding(top = 8.dp)) { cover(); Box(Modifier.weight(1f)) { heading() } } + RecompsState.errors[e.id]?.let { Box(Modifier.padding(top = 12.dp).testTag("recomp-error")) { Note(it) } } if (UserAppsState.working == e.name && UserAppsState.stage != null) { Text(UserAppsState.stage.orEmpty(), fontSize = 12.sp, color = pal.signal, modifier = Modifier.padding(top = 10.dp)) } @@ -478,6 +482,7 @@ private fun RecompDetail( e.builds.sortedBy { RecompCatalog.PREFERENCE.indexOf(it.platform).let { i -> if (i < 0) 99 else i } }.forEach { b -> val status = when { installedAs == b.platform -> stringResource(R.string.recomps_build_installed) + !row.runs(b) -> stringResource(R.string.recomps_not_for_device) installedAs == RecompCatalog.Platform.LINUX_ARM64 && b.platform == RecompCatalog.Platform.LINUX_X64 && e.build(RecompCatalog.Platform.LINUX_ARM64) == null -> stringResource(R.string.recomps_build_installed_arm) else -> b.tag @@ -550,17 +555,7 @@ private fun RecompActions( } SecondaryButton(stringResource(R.string.recomps_project)) { open(e.homepage) } if (inst != null) SecondaryButton(stringResource(R.string.recomps_remove), enabled = busy == null) { - when { - inst.appKey.startsWith("android:") -> RecompAndroid.uninstall(ctx, inst.appKey.removePrefix("android:")) - inst.appKey.startsWith("added:") -> UserAppsState.run(ctx, e.name, R.string.recomps_removed, after = { RecompsState.refresh(ctx, steamOwned) }) { app, _ -> - RecompInstaller.remove(app, inst); null - } - else -> UserAppsState.items.firstOrNull { it.key == inst.appKey }?.let { app -> - UserAppsState.run(ctx, e.name, R.string.recomps_removed, after = { RecompsState.refresh(ctx, steamOwned) }) { c, p -> - com.droiddeck.launcher.runtime.UserApps.remove(c, app, p) - } - } - } + RecompsState.remove(ctx, e, steamOwned, inst) } } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt b/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt index 806975b3ee..325f3a6a0e 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/StoreChips.kt @@ -49,6 +49,7 @@ internal fun sourceColours(source: String): SourceColours = when (source) { internal fun sourceLabel(source: String): String = when (source) { Library.SOURCE_STEAM, Store.STEAM.id -> stringResource(R.string.source_steam) Library.SOURCE_COLLECTION -> stringResource(R.string.soundtracks_collections) + com.droiddeck.launcher.runtime.RecompShortcuts.SOURCE -> stringResource(R.string.rail_recomps) Store.GOG.id -> Store.GOG.label Store.EPIC.id -> Store.EPIC.label Store.AMAZON.id -> Store.AMAZON.label diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 283beaf185..f1ab5fcc0d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4469,6 +4469,18 @@ Unpacking failed (exit %1$d) The download has no %1$s %1$s downloaded. Confirm the install. + This Android build only has code for %1$s, and this device needs %2$s. Pick another build. + %1$s didn\'t install: %2$s + it has no code for this device\'s processor (%1$s). Pick another build. + another copy of this app, signed by someone else, is installed. Remove it first. + a newer version is already installed. + there isn\'t enough free space. + the install was cancelled. + Android blocked the install. + the APK is damaged or isn\'t a valid app. + Android said %1$s. + This recomp\'s files are gone. Install it again from Recomps. + Not for this device The APK isn\'t the app the catalog lists, so it wasn\'t installed Allow DroidDeck to install apps, then press Install again Android app diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RecompShortcutsTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RecompShortcutsTest.kt new file mode 100644 index 0000000000..1d9ed43174 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RecompShortcutsTest.kt @@ -0,0 +1,78 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import android.content.pm.PackageInstaller +import androidx.test.core.app.ApplicationProvider +import com.droiddeck.launcher.frontend.Library +import java.io.File +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner + +@RunWith(RobolectricTestRunner::class) +class RecompShortcutsTest { + private val ctx = ApplicationProvider.getApplicationContext() + + private fun install(dir: String, id: String, name: String, root: String = UserApps.GUEST_DIR): File = + File(LinuxRuntime.rootDir(ctx), root.removePrefix("/") + "/" + dir).apply { + mkdirs() + File(this, RecompInstaller.META_ID).writeText(id) + File(this, "name").writeText(name) + File(this, RecompInstaller.META_PLATFORM).writeText("linux-arm64") + } + + @Test fun theShortcutIdIsSteamsCrcOfTheQuotedProgramAndName() { + val crc = java.util.zip.CRC32().apply { update("\"/opt/droiddeck-apps/zelda/play\"Zelda 64".toByteArray()) }.value or 0x80000000L + assertEquals(crc, RecompShortcuts.appId("/opt/droiddeck-apps/zelda/play", "Zelda 64")) + } + + @Test fun eachLinuxRecompIsANativeShortcutToItsPlayScript() { + val zelda = install("zelda-64", "zelda64recomp", "Zelda 64: Recompiled") + install("quake", "ironwail", "Ironwail", AppImageManager.GUEST_DIR).also { File(it, "app").mkdirs(); File(it, "app/AppRun").writeText("x") } + File(zelda, "icon.png").writeText("png") + val listing = RecompShortcuts.listing(ctx).associateBy { it.getString("name") } + val z = listing.getValue("Zelda 64: Recompiled") + assertEquals("/opt/droiddeck-apps/zelda-64/play", z.getString("exe")) + assertTrue(z.getBoolean("native")) + assertEquals(RecompShortcuts.appId(z.getString("exe"), z.getString("name")), z.getLong("appid")) + assertEquals(File(zelda, "icon.png").absolutePath, z.getJSONObject("art").getString("icon")) + assertEquals("#!/bin/sh\nexec ${UserApps.SCRIPT_LAUNCHER} '/opt/droiddeck-apps/zelda-64' \"\$@\"\n", File(zelda, "play").readText()) + assertTrue(File(zelda, "play").canExecute()) + val quake = listing.getValue("Ironwail") + assertEquals("${AppImageManager.GUEST_DIR}/quake/play", quake.getString("exe")) + val play = File(LinuxRuntime.rootDir(ctx), AppImageManager.GUEST_DIR.removePrefix("/") + "/quake/play").readText() + assertTrue(play.contains(AppImageManager.LAUNCHER)) + } + + @Test fun recompsJoinTheLibraryUnderTheirOwnSource() { + install("banjo", "banjorecomp", "Banjo: Recompiled") + val g = RecompShortcuts.library(ctx) { _, _ -> 42L }.single { it.name == "Banjo: Recompiled" } + assertEquals(RecompShortcuts.SOURCE, g.source) + assertEquals("script:banjo", g.storeId) + assertEquals(42L, g.lastPlayed) + val appId = RecompShortcuts.appId("/opt/droiddeck-apps/banjo/play", "Banjo: Recompiled") + assertEquals((appId shl 32) or 0x02000000L, g.gameId) + } + + @Test fun anApkWithoutCodeForTheDevicesCpuIsCaughtBeforeAndroidRefusesIt() { + val apk = File(ctx.cacheDir, "t.apk") + ZipOutputStream(apk.outputStream()).use { z -> + listOf("AndroidManifest.xml", "lib/armeabi-v7a/libmain.so", "lib/x86/libmain.so").forEach { z.putNextEntry(ZipEntry(it)); z.write(1); z.closeEntry() } + } + assertEquals(setOf("armeabi-v7a", "x86"), RecompAndroid.apkAbis(apk)) + assertTrue(!RecompAndroid.fits(setOf("armeabi-v7a", "x86"), listOf("arm64-v8a"))) + assertTrue(RecompAndroid.fits(setOf("armeabi-v7a", "arm64-v8a"), listOf("arm64-v8a"))) + assertTrue(RecompAndroid.fits(emptySet(), listOf("arm64-v8a"))) + } + + @Test fun installFailuresSayWhatHappened() { + assertTrue(RecompAndroid.failure(ctx, PackageInstaller.STATUS_FAILURE, "INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries").contains("processor")) + assertTrue(RecompAndroid.failure(ctx, PackageInstaller.STATUS_FAILURE_CONFLICT, null).contains("signed by someone else")) + assertTrue(RecompAndroid.failure(ctx, PackageInstaller.STATUS_FAILURE_STORAGE, null).contains("space")) + assertTrue(RecompAndroid.failure(ctx, 99, "odd").contains("odd")) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/ui/LibraryEntriesTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/LibraryEntriesTest.kt index a47bc8aa9b..3470307546 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/LibraryEntriesTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/LibraryEntriesTest.kt @@ -107,9 +107,9 @@ class LibraryEntriesTest { } @Test fun placesRoundTripThroughTheirOrder() { - val order = listOf("epic", "steam", "favorites", LIBRARY_HIDDEN, "gog", "xbox", "added") + val order = listOf("epic", "steam", "favorites", LIBRARY_HIDDEN, "gog", "xbox", "added", "recomp") assertEquals(order, placeLibraryTabs(listOf("epic", "gog", "xbox"), libraryPlacesOf(order))) - assertEquals(listOf("favorites", "steam", "epic", "gog", "added", LIBRARY_HIDDEN), placeLibraryTabs(listOf("epic", "gog"), emptyList())) + assertEquals(listOf("favorites", "steam", "epic", "gog", "added", "recomp", LIBRARY_HIDDEN), placeLibraryTabs(listOf("epic", "gog"), emptyList())) } @Test fun librarySourcesIgnoreUnknownAndDuplicateOrderEntriesAndAppendNewStores() { diff --git a/app/src/test/java/com/droiddeck/launcher/ui/LibraryHiddenTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/LibraryHiddenTest.kt index 0cea327e82..0e48f4bf3d 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/LibraryHiddenTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/LibraryHiddenTest.kt @@ -37,7 +37,7 @@ class LibraryHiddenTest { } @Test fun theHiddenTabSortsLastUnlessMoved() { - assertEquals(listOf(LIBRARY_FAVORITES, Library.SOURCE_STEAM, "epic", Library.ADDED, LIBRARY_HIDDEN), placeLibraryTabs(listOf("epic"), DEFAULT_LIBRARY_PLACES)) + assertEquals(listOf(LIBRARY_FAVORITES, Library.SOURCE_STEAM, "epic", Library.ADDED, "recomp", LIBRARY_HIDDEN), placeLibraryTabs(listOf("epic"), DEFAULT_LIBRARY_PLACES)) val moved = placeLibraryTabs(listOf("epic"), listOf(LIBRARY_HIDDEN to "epic")) assertEquals(listOf("epic", LIBRARY_HIDDEN), moved.filter { it == "epic" || it == LIBRARY_HIDDEN }) } diff --git a/docs/development/recomps.md b/docs/development/recomps.md index 18ef21dbcd..63871bec84 100644 --- a/docs/development/recomps.md +++ b/docs/development/recomps.md @@ -108,6 +108,39 @@ The build is chosen in the order linux-arm64 (native), android, linux-x64 (FEX), with its id, or else its repo, since a higher-ranked catalog can rename an entry. An update installs the new release first and then deletes the old folder. +## In Steam and the Library + +- **Linux recomps** (script apps and AppImages) are non-Steam games (`RecompShortcuts`): + - Each install gets a `play` script beside it that runs `droiddeck-script-run` or the AppImage + launcher, and the catalog's cover and icon are saved there. + - The shortcut is native (no compatibility tool) and its ID is Steam's CRC of the quoted program and + name. + - It is written to the session's shortcut listing next to the Epic launcher, so the next Steam start + has it. It is also added to a running client at once, and removed when the recomp is. +- **Windows recomps** are added games, so they are already non-Steam games. +- **Android apps** cannot be Steam shortcuts and stay in Apps. +- **Library:** Linux recomps also show in a **Recomps** chip, after Custom; it can be hidden like Custom. + Starting one from the Library always runs it as the Linux app, even when DroidDeck launches without + Steam. + +## Android installs + +- **CPU check:** before Android's prompt, the APK's `lib//` folders are checked against + `Build.SUPPORTED_ABIS`. Many newer handhelds (the Pocket FIT included) run 64-bit code only, so a + port that only ships `armeabi-v7a` can never install there. Such an APK is stopped with a sentence + naming both sets of CPUs. +- **Remembered:** the package is recorded in `files/recomps-android/incompatible` (as is one Android + refuses with `NO_MATCHING_ABIS`). Install then picks another build, "Runs natively" stops counting + it, and its build row says "Not for this device". +- **Failures in words, naming the game:** + - wrong CPU; + - another copy signed by someone else; + - a newer version installed; + - not enough space; + - cancelled, blocked, or a damaged APK. + + The last failure stays on the recomp's page until the next attempt. + ## Game data PC ports need the original PC game's files, which a store sells. For these entries (Decomp Buddy's "PC