From 291cc67537b8227c915573cced8d2e6c2ff37664 Mon Sep 17 00:00:00 2001 From: The412Banner <205237651+The412Banner@users.noreply.github.com> Date: Mon, 30 Nov 2026 19:40:53 +0000 Subject: [PATCH] fix(library): a Steam game whose files were deleted can be uninstalled --- .../droiddeck/launcher/FrontEndActivity.kt | 6 +- .../launcher/frontend/GameRemoval.kt | 16 +++-- .../droiddeck/launcher/frontend/Library.kt | 4 +- .../com/droiddeck/launcher/ui/GameDetail.kt | 3 +- .../launcher/frontend/GameRemovalTest.kt | 69 +++++++++++++++++++ 5 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/frontend/GameRemovalTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt index 293b75fea8..26b6c69278 100644 --- a/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/FrontEndActivity.kt @@ -3182,10 +3182,10 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet private fun removeGame(g: Library.SteamGame) { if (g.soundtrack) { removeSoundtrack(g); return } - val folder = g.gameFiles ?: return + val folder = g.gameFiles when (com.droiddeck.launcher.frontend.GameRemoval.kind(g)) { com.droiddeck.launcher.frontend.GameRemoval.Kind.LAUNCHER -> Unit - com.droiddeck.launcher.frontend.GameRemoval.Kind.SHORTCUT -> removeAddedGame(folder.path, g.gameId, g.name) + com.droiddeck.launcher.frontend.GameRemoval.Kind.SHORTCUT -> if (folder != null) removeAddedGame(folder.path, g.gameId, g.name) com.droiddeck.launcher.frontend.GameRemoval.Kind.STORE -> { val installed = com.droiddeck.launcher.frontend.GameRemoval.storeGame(g) if (installed != null) { @@ -3193,7 +3193,7 @@ class FrontEndActivity : ComponentActivity(), com.droiddeck.launcher.ui.AgentPet return } if (g.library == Library.ADDED) { - removeAddedGame(folder.path, g.gameId, g.name) + if (folder != null) removeAddedGame(folder.path, g.gameId, g.name) return } val target = com.droiddeck.launcher.frontend.GameRemoval.steamFiles(g) ?: return diff --git a/app/src/main/java/com/droiddeck/launcher/frontend/GameRemoval.kt b/app/src/main/java/com/droiddeck/launcher/frontend/GameRemoval.kt index a35c71342b..258b397b2a 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/GameRemoval.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/GameRemoval.kt @@ -16,10 +16,10 @@ object GameRemoval { else -> Kind.STORE } - class SteamFiles(val dir: File, val manifest: File, val partial: File?) + class SteamFiles(val dir: File?, val manifest: File, val partial: File?) fun steamFiles(game: Library.SteamGame): SteamFiles? { - val dir = game.gameFiles ?: return null + val dir = game.gameFiles ?: return leftover(game) val common = dir.parentFile ?: return null if (common.name != (if (game.soundtrack) "music" else "common")) return null val steamapps = common.parentFile ?: return null @@ -31,8 +31,10 @@ object GameRemoval { } fun delete(target: SteamFiles): Boolean { - StoreInstalls.deleteTree(target.dir) - if (target.dir.exists()) return false + target.dir?.let { dir -> + StoreInstalls.deleteTree(dir) + if (dir.exists()) return false + } target.partial?.let { StoreInstalls.deleteTree(it) } target.manifest.delete() return !target.manifest.exists() @@ -47,6 +49,12 @@ object GameRemoval { return InstalledStoreGame(sidecar, folder) } + private fun leftover(game: Library.SteamGame): SteamFiles? { + val manifest = game.manifest?.takeIf { it.isFile && declares(it, game.appId) } ?: return null + val steamapps = manifest.parentFile ?: return null + return SteamFiles(null, manifest, File(steamapps, "downloading/${game.appId}")) + } + private fun declares(manifest: File, appId: Int): Boolean = runCatching { manifest.readText() }.getOrNull()?.let { Regex("\"appid\"\\s+\"$appId\"").containsMatchIn(it) } == true } 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..9972e46874 100644 --- a/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt +++ b/app/src/main/java/com/droiddeck/launcher/frontend/Library.kt @@ -20,9 +20,10 @@ object Library { val logo: File? = null, val installedAt: Long = 0L, val soundtrack: Boolean = false, + val manifest: File? = null, ) { fun withArt(art: File?, hero: File?, icon: File?, logo: File? = this.logo) = - SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo, installedAt, soundtrack) + SteamGame(appId, name, art, library, gameId, hero, lastPlayed, gameFiles, protonPrefix, icon, source, storeId, logo, installedAt, soundtrack, manifest) val gameIdString: String get() = java.lang.Long.toUnsignedString(gameId) val launchKey: String get() = if (storeId != null) "$source:$storeId" else gameIdString @@ -167,6 +168,7 @@ object Library { protonPrefix = protonPrefix(context, appId.toLong(), library, roots), icon = images.icon(), logo = images.find(STEAM_LOGOS), + manifest = manifest, )) } } diff --git a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt index 6d64f033b9..b1adf3d627 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/GameDetail.kt @@ -886,7 +886,8 @@ internal fun RemoveButton(title: String, game: Library.SteamGame?, installed: In } val g = game ?: return val kind = GameRemoval.kind(g) - if (kind == GameRemoval.Kind.LAUNCHER || g.gameFiles?.isDirectory != true) return + val leftover = remember(g) { g.gameFiles == null && GameRemoval.steamFiles(g) != null } + if (kind == GameRemoval.Kind.LAUNCHER || (g.gameFiles?.isDirectory != true && !leftover)) return var asking by remember(g.gameId) { mutableStateOf(false) } val shortcut = kind == GameRemoval.Kind.SHORTCUT val label = stringResource(if (shortcut) R.string.edit_remove else R.string.common_uninstall) diff --git a/app/src/test/java/com/droiddeck/launcher/frontend/GameRemovalTest.kt b/app/src/test/java/com/droiddeck/launcher/frontend/GameRemovalTest.kt new file mode 100644 index 0000000000..852344ccdb --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/frontend/GameRemovalTest.kt @@ -0,0 +1,69 @@ +package com.droiddeck.launcher.frontend + +import com.droiddeck.launcher.session.SessionPrefs +import java.io.File +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +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 GameRemovalTest { + private val app get() = RuntimeEnvironment.getApplication() + private lateinit var apps: File + + @Before fun setUp() { + SessionPrefs.setGameStorage(app, SessionPrefs.GAME_STORAGE_OFF, "") + SessionPrefs.setSteamClientArch(app, "arm64") + apps = File(SessionPrefs.steamRoot(app, "arm64"), "steamapps").apply { mkdirs() } + } + + private fun manifest(appId: Int, name: String, dir: String) = + File(apps, "appmanifest_$appId.acf").apply { + writeText("\"AppState\"\n{\n\t\"appid\"\t\t\"$appId\"\n\t\"name\"\t\t\"$name\"\n\t\"StateFlags\"\t\t\"4\"\n\t\"installdir\"\t\t\"$dir\"\n}\n") + } + + @Test fun aGameWhoseFilesWereDeletedCanStillBeRemoved() { + val acf = manifest(17470, "Example", "Example") + val partial = File(apps, "downloading/17470").apply { mkdirs() } + File(partial, "chunk").writeText("x") + val game = Library.steamGames(app, arch = "arm64").single() + assertNull(game.gameFiles) + + val target = requireNotNull(GameRemoval.steamFiles(game)) + assertNull(target.dir) + assertEquals(acf, target.manifest) + assertTrue(GameRemoval.delete(target)) + assertFalse(acf.exists()) + assertFalse(partial.exists()) + assertEquals(emptyList(), Library.steamGames(app, arch = "arm64")) + } + + @Test fun anInstalledGameStillRemovesItsFolder() { + val acf = manifest(440, "Installed", "Installed") + val folder = File(apps, "common/Installed").apply { mkdirs() } + File(folder, "game.exe").writeText("x") + val game = Library.steamGames(app, arch = "arm64").single() + assertEquals(folder, game.gameFiles) + + val target = requireNotNull(GameRemoval.steamFiles(game)) + assertEquals(folder, target.dir) + assertTrue(GameRemoval.delete(target)) + assertFalse(folder.exists()) + assertFalse(acf.exists()) + } + + @Test fun aRowWithoutFilesOrManifestOffersNoRemoval() { + assertNull(GameRemoval.steamFiles(Library.SteamGame(17470, "Example", null, "steam"))) + val other = manifest(570, "Other", "Other") + assertNull(GameRemoval.steamFiles(Library.SteamGame(17470, "Example", null, "steam", manifest = other))) + assertTrue(other.exists()) + } +}