diff --git a/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt b/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt index de9322f069..5251fb12bf 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/AddonBrowser.kt @@ -80,6 +80,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File +import java.io.IOException import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicReference @@ -367,10 +368,10 @@ internal fun AddonBrowserDialog(targets: List, initial: WowAdd scope.launch { val done = withContext(Dispatchers.IO) { runCatching { - val folders = if (via != null) via.call(context, into) { linked -> + val folders = if (via != null) curseFolders(into, addon.id, via.call(context, into) { linked -> CurseForgeApp.projects(context, linked.inspector, linked.guid).firstOrNull { it.id == addon.id }?.folders.orEmpty() - } else addon.folders - folders.forEach { name -> WowAddons.folder(into.addOns, name)?.let { deleteAddon(context, into, it.name, link) } } + }) else addon.folders + deleteAddons(context, into, folders, link) } } installing.remove(key) @@ -589,6 +590,15 @@ internal fun diskStatus(folders: List, onDisk: Map): List = + listed.ifEmpty { WowAddons.details(target).filter { it.curseId == id }.map { it.name } } + +internal fun deleteAddons(context: Context, target: WowAddons.Target, folders: List, link: CurseLink?) { + val found = folders.mapNotNull { WowAddons.folder(target.addOns, it) } + if (found.isEmpty()) throw IOException("No addon folder to delete") + found.forEach { deleteAddon(context, target, it.name, link) } +} + internal fun deleteAddon(context: Context, target: WowAddons.Target, folder: String, link: CurseLink? = CurseLink.live()) { val managed = link?.let { runCatching { it.uninstall(context, target, folder) }.getOrDefault(false) } == true val left = WowAddons.folder(target.addOns, folder) diff --git a/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt b/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt index 16d4c35e9c..0ea4048e3a 100644 --- a/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/ui/CurseLinkTabTest.kt @@ -19,7 +19,9 @@ import org.json.JSONArray import org.json.JSONObject import org.junit.After import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse import org.junit.Assert.assertNull +import org.junit.Assert.assertThrows import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test @@ -29,6 +31,9 @@ import org.robolectric.RobolectricTestRunner import org.robolectric.RuntimeEnvironment import org.robolectric.annotation.Config import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.nio.file.LinkOption import java.util.Collections @RunWith(RobolectricTestRunner::class) @@ -142,6 +147,47 @@ class CurseLinkTabTest { } } + private fun tocFolder(addOns: File, name: String, curseId: String) = + File(addOns, name).apply { mkdirs(); File(this, "$name.toc").writeText("## X-Curse-Project-ID: $curseId\n") } + + @Test + fun anAddonTheManagerDoesNotListIsFoundByItsTocProjectId() { + val target = WowAddons.Target("_retail_", temp.newFolder("AddOns")) + tocFolder(target.addOns, "Questie", "334372") + tocFolder(target.addOns, "Other", "1") + assertEquals(listOf("Questie"), curseFolders(target, "334372", emptyList())) + assertEquals(listOf("Listed"), curseFolders(target, "334372", listOf("Listed"))) + } + + @Test + fun uninstallingByTocProjectIdDeletesOnlyTheMatchedFolders() { + val target = WowAddons.Target("_retail_", temp.newFolder("AddOns")) + tocFolder(target.addOns, "Questie", "334372") + tocFolder(target.addOns, "Other", "1") + deleteAddons(context, target, curseFolders(target, "334372", emptyList()), null) + assertEquals(listOf("Other"), WowAddons.installed(target)) + } + + @Test + fun uninstallingALinkedAddonLeavesTheLinkTargetAlone() { + val target = WowAddons.Target("_retail_", temp.newFolder("AddOns")) + val outside = tocFolder(temp.newFolder("outside"), "Questie", "334372") + File(outside, "keep.lua").writeText("keep") + Files.createSymbolicLink(File(target.addOns, "Questie").toPath(), outside.toPath()) + deleteAddons(context, target, curseFolders(target, "334372", emptyList()), null) + assertFalse(Files.exists(File(target.addOns, "Questie").toPath(), LinkOption.NOFOLLOW_LINKS)) + assertEquals("keep", File(outside, "keep.lua").readText()) + assertTrue(File(outside, "Questie.toc").isFile) + } + + @Test + fun uninstallingWithNothingToDeleteFails() { + val target = WowAddons.Target("_retail_", temp.newFolder("AddOns")) + tocFolder(target.addOns, "Other", "1") + assertThrows(IOException::class.java) { deleteAddons(context, target, curseFolders(target, "334372", emptyList()), null) } + assertEquals(listOf("Other"), WowAddons.installed(target)) + } + @Test fun noCurseForgeStartsWhileASessionRuns() { wow()