fix(frontend): keep a frontend file only for an added game on an unmounted folder

While an added games folder is unreadable, the sync kept the file of every
vanished shortcut-id game, including games removed in the app, removed Epic
or Battle.net games, and old-card games already added again from a new card.
It now keeps only added games whose folder lies on an unreadable bind, is
not removed, and has no listed added game with the same folder name.
This commit is contained in:
The412Banner committed 2026-11-27 20:16:53 +00:00
1 parent c74c20b880
commit 1f55688c37
5 files changed
+51 -18

No files matched your search

@@ -403,6 +403,18 @@ object AddedGames {
Log.i(TAG, "games: bound $dir at $guest")
}
fun unmountedShortcuts(context: Context, listed: List<Library.SteamGame>): Set<String> {
val gone = roots(context).filter { it.host.listFiles() == null }.map { it.host.path }
if (gone.isEmpty()) return emptySet()
val removed = SessionPrefs.removedAddedGames(context).toSet()
val present = listed.filter { it.library == Library.ADDED }.mapNotNull { it.gameFiles?.name?.lowercase() }.toSet()
return AddedExes.list(context).map { File(it.folder) }
.filter { f -> f.path !in removed && gone.any { f.path == it || inside(f.path, it) } && !f.isDirectory && f.name.lowercase() !in present }
.mapNotNull { SessionPrefs.addedGameStoredAppId(context, it.path) }
.map(com.droiddeck.launcher.stores.epic.EpicClient::gameId)
.toSet()
}
private fun listedFolders(context: Context, removed: Set<String>): List<String> {
val out = ArrayList<String>()
val entries = entryFolders(context)
@@ -46,8 +46,8 @@ object GameFileSync {
check(libraryReadable(File(previousStorage))) { context.getString(R.string.game_sync_err_previous_sd) }
}
storage?.let { check(libraryReadable(File(it.path))) { context.getString(R.string.game_sync_err_game_storage) } }
val addedReadable = AddedGames.roots(context).all { it.host.listFiles() != null }
GameFiles.sync(context, folder, Library.launchableGames(context, strictRead = true), pruneShortcuts = addedReadable)
val games = Library.launchableGames(context, strictRead = true)
GameFiles.sync(context, folder, games, keep = AddedGames.unmountedShortcuts(context, games))
prefs(context).edit().putString("storageSetting", storageSetting).putString("storagePath", storage?.path).apply()
}
@@ -144,7 +144,7 @@ object GameFiles {
if (!target.exists()) AtomicFile(target).writeBytes(art.readBytes())
}
@Synchronized fun sync(context: Context, folder: File, games: List<Library.SteamGame>, pruneShortcuts: Boolean = true) {
@Synchronized fun sync(context: Context, folder: File, games: List<Library.SteamGame>, keep: Set<String> = emptySet()) {
require(folder.isDirectory) { context.getString(R.string.gfiles_folder_unavailable) }
val inventory = child(context, folder, INVENTORY)
val previous = if (inventory.exists()) {
@@ -168,14 +168,14 @@ object GameFiles {
if (!existed || owned) next.put(file.name, game.gameIdString)
file
}
val listed = unique.map { it.gameIdString }.toSet()
val kept = keep - unique.map { it.gameIdString }.toSet()
for (name in previous.keys()) {
val id = previous.getString(name)
if (!next.has(name) && ours(name) && GameLaunchLink.validId(id)) {
val file = child(context, folder, name)
if (matches(file, contents(id))) {
val retained = exports.any { java.nio.file.Files.isSameFile(it.toPath(), file.toPath()) }
if (!retained && !pruneShortcuts && id !in listed && shortcut(id)) next.put(name, id)
if (!retained && id in kept) next.put(name, id)
else if (!retained) check(file.delete()) { context.getString(R.string.gfiles_remove_failed, name) }
}
}
@@ -183,8 +183,6 @@ object GameFiles {
if (previous.toString() != next.toString()) write(inventory, JSONObject().put("format", FORMAT).put("games", next).toString())
}
private fun shortcut(id: String) = (id.toULong() and 0xFFFFFFFFuL) == 0x02000000uL
private fun ours(name: String) = name.substringAfterLast('.', "").lowercase(java.util.Locale.ROOT)
.let { it == EXTENSION || it in GameRequest.extensions }
@@ -72,7 +72,7 @@ class GameFileSyncTest {
GameFileSync.disable(context)
}
@Test fun unreadableAddedGamesFolderKeepsShortcutFilesWithoutStoppingTheSync() {
@Test fun unmountedAddedGamesKeepTheirFilesWithoutStoppingTheSync() {
val context = RuntimeEnvironment.getApplication()
val root = LinuxRuntime.rootDir(context)
listOf("usr/bin/gamescope", LinuxRuntime.SESSION_SCRIPT.removePrefix("/")).forEach { path ->
@@ -86,25 +86,44 @@ class GameFileSyncTest {
writeText("\"AppState\"\n{\n\"name\" \"Geometry Wars\"\n\"StateFlags\" \"4\"\n}")
}
val card = File(context.filesDir, "old-card/Games")
SessionPrefs.setGameFolderBinds(context, listOf(card.path to "${AddedGames.GUEST_DIR}/Games"))
val solo = File(context.filesDir, "old-card/Solo")
SessionPrefs.setGameFolderBinds(context, listOf(card.path to "${AddedGames.GUEST_DIR}/Games", solo.path to "${AddedGames.GUEST_DIR}/Solo"))
val shortcuts = listOf("Card Game" to 0xC0FFEE01L, "Card Removed" to 0xC0FFEE02L, "Card Copy" to 0xC0FFEE03L, "Solo" to 0xC0FFEE04L).map { (name, appId) ->
val dir = if (name == "Solo") solo else File(card, name)
AddedExes.put(context, AddedExes.Entry(dir.path, File(dir, "game.exe").path))
SessionPrefs.addedGameAppId(context, dir.path, appId)
Library.SteamGame(0, name, null, Library.ADDED, (appId shl 32) or 0x02000000L, gameFiles = dir)
}
val (kept, removed, copied, single) = shortcuts
val folder = File(context.filesDir, "exports-added").apply { mkdirs() }
GameFileSync.enable(context, folder)
assertEquals(folder.path, GameFileSync.folder(context))
assertEquals("8400", File(folder, "Geometry Wars.steam").readText())
val shortcut = Library.SteamGame(0, "Card Game", null, Library.ADDED, (0xC0FFEE01L shl 32) or 0x02000000L)
GameFiles.sync(context, folder, Library.launchableGames(context, strictRead = true) + shortcut)
assertEquals(shortcut.gameIdString, File(folder, "Card Game.steam").readText())
GameFiles.sync(context, folder, Library.launchableGames(context, strictRead = true) + shortcuts)
shortcuts.forEach { assertEquals(it.gameIdString, File(folder, "${it.name}.steam").readText()) }
SessionPrefs.setAddedGameRemoved(context, removed.gameFiles!!.path, true)
assertTrue(manifest.delete())
File(steamapps, "appmanifest_620.acf").writeText("\"AppState\"\n{\n\"name\" \"Portal 2\"\n\"StateFlags\" \"4\"\n}")
GameFileSync.syncConfigured(context)
assertEquals("620", File(folder, "Portal 2.steam").readText())
assertFalse(File(folder, "Geometry Wars.steam").exists())
assertEquals(shortcut.gameIdString, File(folder, "Card Game.steam").readText())
assertEquals(kept.gameIdString, File(folder, "Card Game.steam").readText())
assertEquals(copied.gameIdString, File(folder, "Card Copy.steam").readText())
assertEquals(single.gameIdString, File(folder, "Solo.steam").readText())
assertFalse(File(folder, "Card Removed.steam").exists())
val copy = Library.SteamGame(0, "Card Copy", null, Library.ADDED, (0xBEEF0003L shl 32) or 0x02000000L,
gameFiles = File(context.filesDir, "new-card/Games/Card Copy"))
val steamCopy = Library.SteamGame(8400, "Card Game", null, "internal", gameFiles = File(context.filesDir, "steam/Card Game"))
assertEquals(setOf(kept.gameIdString, single.gameIdString), AddedGames.unmountedShortcuts(context, listOf(copy, steamCopy)))
assertTrue(card.mkdirs())
assertEquals(setOf(single.gameIdString), AddedGames.unmountedShortcuts(context, emptyList()))
assertTrue(solo.mkdirs())
assertEquals(emptySet<String>(), AddedGames.unmountedShortcuts(context, emptyList()))
GameFileSync.syncConfigured(context)
assertFalse(File(folder, "Card Game.steam").exists())
shortcuts.forEach { assertFalse(File(folder, "${it.name}.steam").exists()) }
assertTrue(File(folder, "Portal 2.steam").isFile)
GameFileSync.disable(context)
shortcuts.forEach { AddedExes.drop(context, it.gameFiles!!.path) }
SessionPrefs.setGameFolderBinds(context, emptyList())
}
@@ -142,25 +142,29 @@ class GameFilesTest {
assertEquals("user edit", modified.readText())
}
@Test fun syncWithoutShortcutPruneKeepsOnlyVanishedShortcuts() {
@Test fun syncKeepsOnlyVanishedGamesItIsAskedToKeep() {
val folder = tmp.newFolder()
val steam = game()
val second = game("620", "Portal 2")
val shortcut = game(java.lang.Long.toUnsignedString((0xC0FFEE01L shl 32) or 0x02000000L), "Card Game")
val dropped = game(java.lang.Long.toUnsignedString((0xC0FFEE03L shl 32) or 0x02000000L), "Card Removed")
val moved = game(java.lang.Long.toUnsignedString((0xC0FFEE02L shl 32) or 0x02000000L), "Card Tool")
GameFiles.sync(app, folder, listOf(steam, second, shortcut, moved))
GameFiles.sync(app, folder, listOf(steam, second, shortcut, dropped, moved))
val renamed = game("620", "Portal 2 Remastered")
val third = game("1", "New install")
val movedRenamed = game(moved.gameIdString, "Card Tool Deluxe")
GameFiles.sync(app, folder, listOf(renamed, third, movedRenamed), pruneShortcuts = false)
assertFalse(File(folder, GameFiles.filename(steam)).exists())
val keep = setOf(steam.gameIdString, shortcut.gameIdString, moved.gameIdString)
GameFiles.sync(app, folder, listOf(renamed, third, movedRenamed), keep = keep)
assertEquals("8400", File(folder, GameFiles.filename(steam)).readText())
assertEquals(shortcut.gameIdString, File(folder, GameFiles.filename(shortcut)).readText())
assertFalse(File(folder, GameFiles.filename(dropped)).exists())
assertFalse(File(folder, GameFiles.filename(second)).exists())
assertFalse(File(folder, GameFiles.filename(moved)).exists())
assertEquals("620", File(folder, GameFiles.filename(renamed)).readText())
assertEquals("1", File(folder, GameFiles.filename(third)).readText())
assertEquals(moved.gameIdString, File(folder, GameFiles.filename(movedRenamed)).readText())
GameFiles.sync(app, folder, listOf(renamed, third, movedRenamed))
assertFalse(File(folder, GameFiles.filename(steam)).exists())
assertFalse(File(folder, GameFiles.filename(shortcut)).exists())
assertTrue(File(folder, GameFiles.filename(renamed)).isFile)
assertTrue(File(folder, GameFiles.filename(third)).isFile)