fix(addons): show installed addons as installed while the manager rescans (#3057)

* fix(addons): show installed addons as installed while the manager rescans

Opening the download dialog starts a fresh scan of the addon manager's
installed projects, and every row showed Install until it answered
(about 20 s). Rows now start from the manager's last report, kept only
for projects whose folders are all still on disk, and its new report
takes over, Update state included.

* fix(addons): match installed addons by their toc project id before the first report

Before the addon manager has reported at all, a search row whose project id
matches an installed addon's X-Curse-Project-ID shows as installed. Placeholder
ids are ignored and the manager's report always takes over.

* fix(addons): read toc fields from flavour tocs and keep known state when a scan fails

An addon whose main toc is a stub keeps its version and project id in the
flavour tocs, so missing fields are now filled from the next toc in order.
A failed manager scan now falls back to the last known installed state
instead of clearing it.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-23 05:57:05 -05:00
1 parent 43b17483fb
commit c78adda7ca
5 files changed
+148 -14

No files matched your search

@@ -40,14 +40,15 @@ object WowAddons {
class Addon(val name: String, val root: String)
class Installed(val name: String, val version: String?, val wowiId: String?)
class Installed(val name: String, val version: String?, val wowiId: String?, val curseId: String? = null)
class Origin(val id: String, val version: String)
private const val ORIGINS = ".droiddeck-sources"
private const val TOC_VERSION = "Version"
private const val TOC_WOWI_ID = "X-WoWI-ID"
private val TOC_KEYS = setOf(TOC_VERSION, TOC_WOWI_ID)
private const val TOC_CURSE_ID = "X-Curse-Project-ID"
private val TOC_KEYS = setOf(TOC_VERSION, TOC_WOWI_ID, TOC_CURSE_ID)
internal const val MAX_EXPANDED_BYTES = 4L * 1024 * 1024 * 1024
@@ -84,7 +85,7 @@ object WowAddons {
return installed(target).map { name ->
val toc = toc(File(target.addOns, name))
val origin = origins[name]
Installed(name, origin?.version ?: toc[TOC_VERSION], origin?.id ?: toc[TOC_WOWI_ID])
Installed(name, origin?.version ?: toc[TOC_VERSION], origin?.id ?: toc[TOC_WOWI_ID], toc[TOC_CURSE_ID])
}
}
@@ -103,11 +104,21 @@ object WowAddons {
}
private fun toc(folder: File): Map<String, String> {
val tocs = folder.listFiles().orEmpty().filter { it.isFile && it.name.endsWith(".toc", ignoreCase = true) }
val main = tocs.firstOrNull { it.name.dropLast(4).equals(folder.name, ignoreCase = true) }
?: tocs.firstOrNull { it.name.dropLast(4).replace(SUFFIX, "").equals(folder.name, ignoreCase = true) }
?: tocs.minByOrNull { it.name.length } ?: return emptyMap()
return runCatching { main.bufferedReader().useLines { tocFields(it) } }.getOrDefault(emptyMap())
val tocs = folder.listFiles().orEmpty().filter { it.isFile && it.name.endsWith(".toc", ignoreCase = true) }.sortedWith(
compareBy<File>(
{ !it.name.dropLast(4).equals(folder.name, ignoreCase = true) },
{ !it.name.dropLast(4).replace(SUFFIX, "").equals(folder.name, ignoreCase = true) },
{ it.name.length },
{ it.name },
),
)
val out = LinkedHashMap<String, String>()
for (file in tocs) {
if (out.size == TOC_KEYS.size) break
val missing = TOC_KEYS - out.keys
runCatching { file.bufferedReader().useLines { tocFields(it, missing) } }.getOrNull()?.let(out::putAll)
}
return out
}
internal fun parseOrigins(text: String): Map<String, Origin> = text.lineSequence().mapNotNull { line ->
@@ -80,6 +80,7 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
@@ -118,11 +119,17 @@ internal class CurseLink(val app: UserApps.App) {
fun installed(context: Context, target: WowAddons.Target, key: Any): List<CurseForgeApp.Project> = CurseForgeApp.onDisk(
synchronized(scanLock) {
scans[target.addOns.path]?.takeIf { it.first == key }?.second
?: call(context, target) { CurseForgeApp.projects(context, it.inspector, it.guid, rescan = true) }.also { scans[target.addOns.path] = key to it }
?: call(context, target) { CurseForgeApp.projects(context, it.inspector, it.guid, rescan = true) }.also {
scans[target.addOns.path] = key to it
report(target, it)
}
},
target.addOns,
)
fun marks(context: Context, target: WowAddons.Target, key: Any): Map<String, String?> =
runCatching { installed(context, target, key).associate { it.id to curseMark(it) } }.getOrElse { lastReported(target) }
fun <T> call(context: Context, target: WowAddons.Target, launch: Boolean = true, block: (Linked) -> T): T {
val linked = synchronized(lock) { link(context, target, launch) }
try {
@@ -190,6 +197,16 @@ internal class CurseLink(val app: UserApps.App) {
companion object {
private val current = AtomicReference<CurseLink?>()
private val reported = ConcurrentHashMap<String, List<CurseForgeApp.Project>>()
fun lastReported(target: WowAddons.Target, disk: () -> List<WowAddons.Installed> = { WowAddons.details(target) }): Map<String, String?> {
val last = reported[target.addOns.path] ?: return disk().mapNotNull { it.curseId }.associateWith { null }
return CurseForgeApp.onDisk(last, target.addOns).associate { it.id to null }
}
internal fun report(target: WowAddons.Target, projects: List<CurseForgeApp.Project>) {
reported[target.addOns.path] = projects
}
fun live(): CurseLink? = current.get()
}
@@ -250,10 +267,9 @@ internal fun AddonBrowserDialog(targets: List<WowAddons.Target>, initial: WowAdd
val snapshot by produceState(presentKey to Present(), target.addOns, scanKey, curse, curseInstalls, curse == null && results.items.isEmpty()) {
val into = target
val key = curse to into.addOns
if (curse != null && link != null && value.first != key) value = key to withContext(Dispatchers.IO) { Present(curse = CurseLink.lastReported(into)) }
value = key to withContext(Dispatchers.IO) {
if (curse != null && link != null) Present(curse = runCatching {
link.installed(context, into, scanKey).associate { it.id to curseMark(it) }
}.getOrDefault(emptyMap()))
if (curse != null && link != null) Present(curse = link.marks(context, into, scanKey))
else Present(disk = WowAddons.details(into).associateBy { it.name.lowercase() })
}
}
@@ -231,10 +231,11 @@ class WowAddonsTest {
val lines = sequence {
yield("## Version: 2.0")
yield("## X-WoWI-ID: 5")
yield("## X-Curse-Project-ID: 9")
repeat(100_000) { read++; yield("## X-Data-$it: payload") }
}
val fields = WowAddons.tocFields(lines)
assertEquals(mapOf("Version" to "2.0", "X-WoWI-ID" to "5"), fields)
assertEquals(mapOf("Version" to "2.0", "X-WoWI-ID" to "5", "X-Curse-Project-ID" to "9"), fields)
assertEquals(0, read)
}
@@ -254,15 +255,39 @@ class WowAddonsTest {
@Test fun detailsUseTheTocAndTheRecordedSource() {
val target = target()
val foo = File(target.addOns, "Foo").apply { mkdirs() }
File(foo, "Foo_Mainline.toc").writeText("## Version: 1.0\n## X-WoWI-ID: 77\n")
File(foo, "Foo_Mainline.toc").writeText("## Version: 1.0\n## X-WoWI-ID: 77\n## X-Curse-Project-ID: 334372\n")
File(target.addOns, "Bar").mkdirs()
val details = WowAddons.details(target).associateBy { it.name }
assertEquals(setOf("Bar", "Foo"), details.keys)
assertEquals("1.0", details.getValue("Foo").version)
assertEquals("77", details.getValue("Foo").wowiId)
assertEquals("334372", details.getValue("Foo").curseId)
assertEquals(null, details.getValue("Bar").curseId)
assertEquals(null, details.getValue("Bar").version)
}
@Test fun detailsFillFieldsTheMainTocLacksFromTheFlavourTocs() {
val target = target()
val questie = File(target.addOns, "Questie").apply { mkdirs() }
File(questie, "Questie.toc").writeText("## Interface: 00000\n## Title: Questie\n")
File(questie, "Questie_Vanilla.toc").writeText("## Version: 12.6.4\n## X-Curse-Project-ID: 334372\n")
File(questie, "Questie_Wrath.toc").writeText("## Version: 12.6.4\n")
val questieDetails = WowAddons.details(target).single()
assertEquals("12.6.4", questieDetails.version)
assertEquals("334372", questieDetails.curseId)
}
@Test fun detailsKeepTheMainTocWhenItHasTheFields() {
val target = target()
val foo = File(target.addOns, "Foo").apply { mkdirs() }
File(foo, "Foo.toc").writeText("## Version: 2.0\n## X-Curse-Project-ID: 5\n")
File(foo, "Foo_Classic.toc").writeText("## Version: 1.0\n## X-Curse-Project-ID: 6\n## X-WoWI-ID: 7\n")
val fooDetails = WowAddons.details(target).single()
assertEquals("2.0", fooDetails.version)
assertEquals("5", fooDetails.curseId)
assertEquals("7", fooDetails.wowiId)
}
@Test fun importRecordsTheSourceAndPlainImportsClearIt() {
val target = target()
WowAddons.import(zip("Foo/Foo.toc" to "## Version: 1.0\n"), target, WowAddons.Origin("55", "2.0"))
@@ -0,0 +1,68 @@
package com.droiddeck.launcher.ui
import com.droiddeck.launcher.runtime.CurseForgeApp
import com.droiddeck.launcher.runtime.WowAddons
import org.junit.Assert.assertEquals
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class CurseLinkReportTest {
@get:Rule val temp = TemporaryFolder()
private fun target(vararg folders: String): WowAddons.Target {
val addOns = temp.newFolder("AddOns")
folders.forEach { java.io.File(addOns, it).mkdirs() }
return WowAddons.Target("_retail_", addOns)
}
private fun project(id: String, vararg folders: String, update: Boolean = false) = CurseForgeApp.Project(id, "m$id", folders.toList(), update)
@Test fun beforeAnyReportNothingIsKnown() {
assertEquals(emptyMap<String, String?>(), CurseLink.lastReported(target("Questie")))
}
@Test fun beforeCurseForgeReportsAddonsWhoseFoldersAreAllOnDiskShowAsInstalled() {
val target = target("questie", "QuestieDB", "Auctionator")
CurseLink.report(target, listOf(
project("334372", "Questie", "QuestieDB", update = true),
project("1", "Auctionator"),
project("2", "Details", "Details_Streamer"),
project("3", "Auctionator", "Auctionator_Extra"),
))
assertEquals(mapOf<String, String?>("334372" to null, "1" to null), CurseLink.lastReported(target))
}
@Test fun anAddonRemovedFromDiskSinceTheLastReportIsNotInstalled() {
val target = target("Questie")
CurseLink.report(target, listOf(project("334372", "Questie")))
java.io.File(target.addOns, "Questie").delete()
assertEquals(emptyMap<String, String?>(), CurseLink.lastReported(target))
}
private fun toc(target: WowAddons.Target, folder: String, curseId: String) {
val dir = java.io.File(target.addOns, folder).apply { mkdirs() }
java.io.File(dir, "$folder.toc").writeText("## Interface: 110207\n## Title: $folder\n## X-Curse-Project-ID: $curseId\n$folder.lua\n")
}
@Test fun beforeAnyReportAnAddonWhoseTocNamesTheProjectShowsAsInstalled() {
val target = target()
toc(target, "Questie", "334372")
toc(target, "Auctionator", "@project-id@")
assertEquals(mapOf<String, String?>("334372" to null), CurseLink.lastReported(target))
}
@Test fun aPlaceholderProjectIdIsIgnored() {
val target = target()
toc(target, "Auctionator", "@project-id@")
assertEquals(emptyMap<String, String?>(), CurseLink.lastReported(target))
}
@Test fun curseForgesReportOverridesTheTocProjectId() {
val target = target()
toc(target, "Questie", "334372")
toc(target, "Auctionator", "1")
CurseLink.report(target, listOf(project("1", "Auctionator", update = true)))
assertEquals(mapOf<String, String?>("1" to null), CurseLink.lastReported(target))
}
}
@@ -128,6 +128,20 @@ class CurseLinkTabTest {
}
}
@Test
fun aFailedScanKeepsTheLastReportInsteadOfClearingIt() {
val target = wow()
val link = CurseLink(app)
try {
val reported = link.marks(context, target, 0)
assertTrue(reported.isNotEmpty())
runBlocking { link.close(context).join() }
assertEquals(reported.mapValues { null }, link.marks(context, target, 1))
} finally {
runBlocking { link.close(context).join() }
}
}
@Test
fun noCurseForgeStartsWhileASessionRuns() {
wow()