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() })
}
}