diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt index 90e8180b93..1890db152c 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/BattleNet.kt @@ -223,10 +223,8 @@ object BattleNet { */ fun launchCode(context: Context, uid: String, folder: File?): String? { if (folder == null) return runCatching { games(context) }.getOrNull()?.let { launchCode(it, uid, null) } - // games() keeps one copy per launch code; the registrations of this folder are authoritative. - runCatching { registrationsAt(context, uid, folder) }.getOrNull()?.firstOrNull() - ?.let { registered -> title(registered)?.code?.let { return it } } - return runCatching { games(context) }.getOrNull()?.let { launchCode(it, uid, folder) } + if (!folder.isDirectory) return null + return runCatching { installedRegistrationAt(context, uid, folder) }.getOrNull()?.second?.code } /** @@ -245,7 +243,7 @@ object BattleNet { val pfx = File(prefix(context), "pfx") val agent = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db") val registry = File(pfx, "system.reg") - if (agent.isFile && runCatching { agent.readBytes() }.isFailure) return false + if (agent.exists() && runCatching { productDbRegistrations(agent.readBytes()) }.getOrNull() == null) return false if (registry.isFile && runCatching { registry.useLines { uninstallEntries(it) } }.isFailure) return false return true } @@ -257,6 +255,44 @@ object BattleNet { return same.singleOrNull()?.code ?: title(uid)?.code } + internal fun installedRegistrationAt(context: Context, id: String, folder: File): Pair? { + if (!folder.isDirectory) return null + val pfx = File(prefix(context), "pfx") + val agentFile = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db") + val registryFile = File(pfx, "system.reg") + val agent = if (agentFile.exists()) { + val bytes = agentFile.readBytes() + if (productDbRegistrations(bytes) == null) return null + productDbInstalls(bytes).orEmpty() + } else null + val registry = if (registryFile.exists()) registryFile.useLines { uninstallEntries(it) } else emptyList() + val candidates = if (agent == null) registry.map { AgentInstall(it.first, it.second, "") } + else agent + return candidates.firstNotNullOfOrNull { row -> + val found = title(row.uid) ?: titleForProduct(row.product) ?: return@firstNotNullOfOrNull null + if (!sameTitle(found.uid, id)) return@firstNotNullOfOrNull null + val registered = hostFolder(context, row.path) ?: return@firstNotNullOfOrNull null + if (registered.absolutePath != folder.absolutePath && + (found.flavor != folder.name || registered.absolutePath != folder.parentFile?.absolutePath) + ) return@firstNotNullOfOrNull null + row.uid to found + } + } + + /** A registration for any title in this tree makes recursive deletion unsafe. */ + internal fun folderHasRegistrations(context: Context, folder: File): Boolean? = runCatching { + val pfx = File(prefix(context), "pfx") + val agentFile = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db") + val registryFile = File(pfx, "system.reg") + val agent = if (agentFile.exists()) productDbRegistrations(agentFile.readBytes()) ?: return null else emptyList() + val registry = if (registryFile.exists()) registryFile.useLines { uninstallEntries(it) } else emptyList() + val target = folder.canonicalFile.toPath() + (agent.map { it.path } + registry.map { it.second }).any { path -> + val registered = hostFolder(context, path)?.canonicalFile?.toPath() ?: return null + registered.startsWith(target) + } + }.getOrNull() + internal fun registrationsAt(context: Context, id: String, folder: File): List? { val pfx = File(prefix(context), "pfx") val agentFile = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db") 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 8f19c9d17a..55e6bf9175 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/StoresState.kt @@ -351,7 +351,7 @@ object StoresState { if (game.sidecar.store == Store.BATTLENET) { val listed = com.droiddeck.launcher.runtime.BattleNet.games(app).any { it.folder.absolutePath == game.folder.absolutePath } // A registration we could not read is not the same as no registration: keep the folder. - if (listed || !com.droiddeck.launcher.runtime.BattleNet.registrationsReadable(app)) { + if (listed || com.droiddeck.launcher.runtime.BattleNet.folderHasRegistrations(app, game.folder) != false) { main.post { uninstalling.remove(key); onDone() } return } diff --git a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt index 9425b8d920..88ca196a55 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstall.kt @@ -155,7 +155,7 @@ object BattleNetInstall { /** The registration of this folder: games() keeps one copy per launch code, so a second copy is not in it. */ internal fun installUid(context: Context, game: InstalledStoreGame): String? = - runCatching { BattleNet.registrationsAt(context, game.sidecar.id, game.folder) }.getOrNull()?.firstOrNull() + runCatching { BattleNet.installedRegistrationAt(context, game.sidecar.id, game.folder) }.getOrNull()?.first fun uninstall(context: Context, game: InstalledStoreGame): Boolean { if (product(game.sidecar.id) == null || !BattleNet.installed(context)) return false diff --git a/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt index e0dea65ba3..4ceb255a8b 100644 --- a/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt +++ b/app/src/main/java/com/droiddeck/launcher/stores/download/DownloadQueue.kt @@ -345,10 +345,10 @@ object DownloadQueue { item = items[key] ?: return if (item.entry.state == DownloadState.RUNNING || item.entry.state == DownloadState.QUEUED) return if (item.entry.state == DownloadState.FAILED || item.entry.state == DownloadState.PAUSED) { - cleanup = beginCleanup(key) - if (cleanup) { - val candidate = item.entry.cleanupKey?.takeIf { item.factory != null } - if (candidate != null && cancelling.add(candidate)) cleanupKey = candidate + val candidate = item.entry.cleanupKey?.takeIf { item.factory != null } + if (candidate == null || cancelling.add(candidate)) { + cleanup = beginCleanup(key) + if (cleanup) cleanupKey = candidate else cleanupFinished(candidate) } } items.remove(key) diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt index 2ae191a073..e53c249dbe 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/BattleNetTest.kt @@ -472,6 +472,34 @@ class BattleNetTest { assertEquals("Fen", BattleNet.launchCode(context, "fen", folder)) } + @Test fun aMissingInstallFolderCannotLaunchEvenWhenRegistered() { + val pfx = File(BattleNet.prefix(context), "pfx") + val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() } + val folder = File(pfx, "drive_c/Games/D3") + db.writeBytes(message(field(1, message(field(1, "d3cn"), field(3, message(field(1, "C:/Games/D3"))))))) + assertNull(BattleNet.launchCode(context, "diablo3", folder)) + assertEquals(true, BattleNet.folderHasRegistrations(context, folder)) + } + + @Test fun anUnreadableAgentDatabaseDoesNotAuthorizeFolderDeletion() { + val pfx = File(BattleNet.prefix(context), "pfx") + val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() } + val folder = File(pfx, "drive_c/Games/D3").apply { mkdirs() } + db.writeBytes(byteArrayOf(0x0a, 0x7f)) + assertFalse(BattleNet.registrationsReadable(context)) + assertNull(BattleNet.folderHasRegistrations(context, folder)) + } + + @Test fun productFallbackResolvesBothLaunchCodeAndManagementUid() { + val pfx = File(BattleNet.prefix(context), "pfx") + val db = File(pfx, "drive_c/ProgramData/Battle.net/Agent/product.db").apply { parentFile!!.mkdirs() } + val folder = File(pfx, "drive_c/Games/WoW").apply { mkdirs() } + db.writeBytes(message(field(1, message(field(1, "wowf_live"), field(2, "wowf"), field(3, message(field(1, "C:/Games/WoW"))))))) + assertEquals("WoWF", BattleNet.launchCode(context, "wow_forever", folder)) + val sidecar = StoreGameSidecar(Store.BATTLENET, "wow_forever", "World of Warcraft", "") + assertEquals("wowf_live", BattleNetInstall.installUid(context, InstalledStoreGame(sidecar, folder))) + } + @Test fun aRegistrationForAMissingFolderStillCountsAsPreexisting() { val pfx = File(BattleNet.prefix(context), "pfx").apply { mkdirs() } File(pfx, "drive_c/ProgramData/Battle.net/Agent").mkdirs() diff --git a/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstallTest.kt b/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstallTest.kt index f3b40eb3be..51e89f8456 100644 --- a/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstallTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/stores/battlenet/BattleNetInstallTest.kt @@ -166,7 +166,7 @@ class BattleNetInstallTest { assertEquals(2, scheduled.size) assertEquals(60_000L, scheduled[1].second) - var next = 2 + var next = 1 repeat(4) { if (next < scheduled.size) { scheduled[next].first()