From b2421cfea768328aa4668c0f1ccebe85e216d81d Mon Sep 17 00:00:00 2001 From: Falxie_ <94479957+falxie@users.noreply.github.com> Date: Sun, 6 Aug 2023 09:54:30 -0700 Subject: [PATCH] Add open missing mods button (#66) * add open missing mods button * hide missing mods button if none are found * add logging to openUrl --- .../metadata/curseforge/CurseForgeSourcer.kt | 3 +- .../installer/ui/data/ExceptionDetails.kt | 5 +- .../installer/ui/gui/ExceptionListWindow.kt | 46 ++++++++++++++----- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/link/infra/packwiz/installer/metadata/curseforge/CurseForgeSourcer.kt b/src/main/kotlin/link/infra/packwiz/installer/metadata/curseforge/CurseForgeSourcer.kt index 5c42089..4ac827f 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/metadata/curseforge/CurseForgeSourcer.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/metadata/curseforge/CurseForgeSourcer.kt @@ -147,8 +147,9 @@ fun resolveCfMetadata(mods: List, packFolder: PackwizFilePath, c } for (indexFile in fileIdMap[fileId]!!) { + var modUrl = "${mod.links?.websiteUrl}/files/${fileId}" failures.add(ExceptionDetails(indexFile.name, Exception("This mod is excluded from the CurseForge API and must be downloaded manually.\n" + - "Please go to ${mod.links?.websiteUrl}/files/${fileId} and save this file to ${indexFile.destURI.rebase(packFolder).nioPath.absolute()}"))) + "Please go to ${modUrl} and save this file to ${indexFile.destURI.rebase(packFolder).nioPath.absolute()}"), modUrl)) } } } diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt index 6908048..ffde901 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt @@ -2,5 +2,6 @@ package link.infra.packwiz.installer.ui.data data class ExceptionDetails( val name: String, - val exception: Exception -) \ No newline at end of file + val exception: Exception, + val modUrl: String? = null +) diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt index 69339df..fc70e14 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt @@ -1,5 +1,6 @@ package link.infra.packwiz.installer.ui.gui +import link.infra.packwiz.installer.util.Log import link.infra.packwiz.installer.ui.IUserInterface import link.infra.packwiz.installer.ui.data.ExceptionDetails import java.awt.BorderLayout @@ -24,6 +25,24 @@ class ExceptionListWindow(eList: List, future: CompletableFutu fun getExceptionAt(index: Int) = details[index].exception } + private fun openUrl(url: String) { + try { + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + Desktop.getDesktop().browse(URI(url)) + } else { + val process = Runtime.getRuntime().exec(arrayOf("xdg-open", url)); + val exitValue = process.waitFor() + if (exitValue > 0) { + Log.warn("Failed to open $url: xdg-open exited with code $exitValue") + } + } + } catch (e: IOException) { + Log.warn("Failed to open $url", e) + } catch (e: URISyntaxException) { + Log.warn("Failed to open $url", e) + } + } + /** * Create the dialog. */ @@ -112,6 +131,19 @@ class ExceptionListWindow(eList: List, future: CompletableFutu this@ExceptionListWindow.dispose() } }) + + val missingMods = eList.filter { it.modUrl != null }.map { it.modUrl!! }.toSet() + + if (!missingMods.isEmpty()) { + add(JButton("Open missing mods").apply { + toolTipText = "Open missing mods in your browser" + addActionListener { + missingMods.forEach { + openUrl(it) + } + } + }) + } }, BorderLayout.EAST) // Errored label @@ -122,16 +154,8 @@ class ExceptionListWindow(eList: List, future: CompletableFutu // Left buttons add(JPanel().apply { add(JButton("Report issue").apply { - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { - addActionListener { - try { - Desktop.getDesktop().browse(URI("https://github.com/packwiz/packwiz-installer/issues/new")) - } catch (e: IOException) { - // lol the button just won't work i guess - } catch (e: URISyntaxException) {} - } - } else { - isEnabled = false + addActionListener { + openUrl("https://github.com/packwiz/packwiz-installer/issues/new") } }) }, BorderLayout.WEST) @@ -150,4 +174,4 @@ class ExceptionListWindow(eList: List, future: CompletableFutu } }) } -} \ No newline at end of file +}