From 74d09702a8e16ce8fd49c8e7aa7af70e2dc2d208 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Sun, 12 Mar 2023 02:48:17 +0100 Subject: [PATCH] feat: added basic UI, started on backend chore: added coroutines Signed-off-by: limited_dev --- build.gradle.kts | 2 + .../de/limited_dev/modpackinstaller/Main.kt | 28 ++++- .../modpackinstaller/backend/Manager.kt | 41 +++++++ .../modpackinstaller/gui/BottomBar.kt | 38 +++++++ .../limited_dev/modpackinstaller/gui/GUI.kt | 43 ++++++++ .../modpackinstaller/gui/MainPanel.kt | 103 ++++++++++++++++++ .../modpackinstaller/util/OSHelper.kt | 43 ++++++++ .../modpackinstaller/util/PopupFactory.kt | 42 +++++++ 8 files changed, 338 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/de/limited_dev/modpackinstaller/backend/Manager.kt create mode 100644 src/main/kotlin/de/limited_dev/modpackinstaller/gui/BottomBar.kt create mode 100644 src/main/kotlin/de/limited_dev/modpackinstaller/gui/GUI.kt create mode 100644 src/main/kotlin/de/limited_dev/modpackinstaller/gui/MainPanel.kt create mode 100644 src/main/kotlin/de/limited_dev/modpackinstaller/util/OSHelper.kt create mode 100644 src/main/kotlin/de/limited_dev/modpackinstaller/util/PopupFactory.kt diff --git a/build.gradle.kts b/build.gradle.kts index 0032a0b..fa4ae5f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,6 +51,7 @@ repositories { dependencies { implementation("com.formdev:flatlaf:2.4") + implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4") } val targetJavaVersion = 17 @@ -91,6 +92,7 @@ tasks { withType { dependencies { include(dependency("com.formdev:flatlaf:2.4")) + include(dependency("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")) } dependsOn("generateTemplates", "processResources") } diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/Main.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/Main.kt index 91a6e77..e9f249f 100644 --- a/src/main/kotlin/de/limited_dev/modpackinstaller/Main.kt +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/Main.kt @@ -1,8 +1,32 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + package de.limited_dev.modpackinstaller +import com.formdev.flatlaf.FlatDarculaLaf import de.limited_dev.modpackinstaller.build.BuildConstants +import de.limited_dev.modpackinstaller.gui.GUI fun main() { - println("Hello World") - println("${BuildConstants.version} ${BuildConstants.fabricDownloadLocation} ${BuildConstants.modpackDownloadLocation}") + println("Launching installer...") + println("${BuildConstants.version}") + println("Download Location Fabric: ${BuildConstants.fabricDownloadLocation}") + println("Download Modpacka Fabric: ${BuildConstants.modpackDownloadLocation}") + FlatDarculaLaf.setup() + GUI() } \ No newline at end of file diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/backend/Manager.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/backend/Manager.kt new file mode 100644 index 0000000..fe7d162 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/backend/Manager.kt @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.modpackinstaller.backend + +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch +import kotlinx.coroutines.runBlocking + +object Manager { + fun startDownloadAndInstall(dotMcLocation: String) { + val modloaderCoroutine = GlobalScope.launch { + println("Downloading Modloader") + } + + val modpackCoroutine = GlobalScope.launch { + println("Downloading Modpack") + } + + runBlocking { + modloaderCoroutine.join() + } + + + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/gui/BottomBar.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/gui/BottomBar.kt new file mode 100644 index 0000000..00597d6 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/gui/BottomBar.kt @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.modpackinstaller.gui + +import java.awt.GridLayout +import javax.swing.JLabel +import javax.swing.JPanel +import javax.swing.JSeparator +import javax.swing.border.EmptyBorder + +class BottomBar : JPanel() { + init { + this.layout = GridLayout(2, 4) + this.border = EmptyBorder(0, 10, 10, 10) + + val label = JLabel("(c) 2023 limited_dev, Licensed under GPL-3.0") + + this.add(JSeparator()) + this.add(label) + } + +} \ No newline at end of file diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/gui/GUI.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/gui/GUI.kt new file mode 100644 index 0000000..c46eefe --- /dev/null +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/gui/GUI.kt @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.modpackinstaller.gui + +import de.limited_dev.modpackinstaller.build.BuildConstants +import java.awt.BorderLayout +import javax.swing.JFrame +import javax.swing.WindowConstants + +class GUI : JFrame("Modpack installer " + BuildConstants.version) { + init { + this.isResizable = false + this.defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE + + val contentPane = this.contentPane + contentPane.layout = BorderLayout() + + contentPane.add(MainPanel(), BorderLayout.CENTER) + contentPane.add(BottomBar(), BorderLayout.SOUTH) + + this.pack() + this.setLocationRelativeTo(null) + this.isVisible = true + + } + +} \ No newline at end of file diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/gui/MainPanel.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/gui/MainPanel.kt new file mode 100644 index 0000000..1060e8d --- /dev/null +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/gui/MainPanel.kt @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.modpackinstaller.gui + +import de.limited_dev.modpackinstaller.backend.Manager +import de.limited_dev.modpackinstaller.util.OSHelper +import de.limited_dev.modpackinstaller.util.PopupFactory +import java.awt.* +import java.io.File +import javax.swing.* +import javax.swing.border.EmptyBorder + + +class MainPanel : JPanel() { + private val generatorButton = JButton("Download & Install") + private var fabricLabel: JLabel + private var modpackLabel: JLabel + private var dotMcLocationBar: JTextField + private var dotMcLocation: String = OSHelper.os.oSType + + init { + val layout = GridLayout(6, 1) + + this.layout = layout + border = EmptyBorder(10, 10, 10, 10) + + val info = JLabel("Modpack Installer") + info.horizontalAlignment = SwingConstants.CENTER + info.font = Font(font.name, font.style, font.size + 6) + this.add(info, BorderLayout.NORTH) + + val flayout = FlowLayout() + flayout.hgap = 10 + flayout.alignment = SwingConstants.VERTICAL + + fabricLabel = JLabel("Fabric mod loader") + modpackLabel = JLabel("Modpack") + + this.add(fabricLabel) + this.add(modpackLabel) + + dotMcLocationBar = JTextField(dotMcLocation) + dotMcLocationBar.isEditable = false + this.add(dotMcLocationBar) + + val openFileChooser = JButton("Select .minecraft Folder") + openFileChooser.addActionListener { + dotMcLocation = promptForFolder(openFileChooser) + dotMcLocationBar.text = dotMcLocation + } + this.add(openFileChooser) + generatorButton.addActionListener { + Manager.startDownloadAndInstall(dotMcLocation) + val dotMcMods = File(dotMcLocation + File.separator + "mods") + if (dotMcMods.exists()) { + val bool = PopupFactory.getYesOrNoError( + "Warning! .minecraft/mods folder found", + "This installer will clear your mods folder in order to install the right Modpack." + ) + if (bool) { + generatorButton.isEnabled = false + Manager.startDownloadAndInstall(dotMcLocation) + } + } else { + generatorButton.isEnabled = false + Manager.startDownloadAndInstall(dotMcLocation) + } + } + this.add(generatorButton) + } + + private fun promptForFolder(parent: Component?): String { + val fc = JFileChooser() + fc.fileSelectionMode = JFileChooser.DIRECTORIES_ONLY + return if (fc.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) { + fc.selectedFile.absolutePath + } else "Error" + } + + fun updateFabricModloader(tx: String) { + fabricLabel.text = "Fabric mod loader $tx" + } + + fun updateModpack(tx: String) { + modpackLabel.text = "Modpack $tx" + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/util/OSHelper.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/util/OSHelper.kt new file mode 100644 index 0000000..beda873 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/util/OSHelper.kt @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.modpackinstaller.util + +import java.io.File +import java.util.* + +enum class OSHelper(val oSType: String) { + WINDOWS(System.getenv("APPDATA") + File.separator + ".minecraft"), + MAC("Please select your .minecraft folder"), + LINUX(System.getProperty("user.home") + File.separator + ".minecraft"); + + companion object { + val os: OSHelper + get() { + val currentOS = System.getProperty("os.name").lowercase(Locale.getDefault()) + if (currentOS.startsWith("windows")) { + return WINDOWS + } else if (currentOS.startsWith("mac")) { + return MAC + } + return LINUX + } + val dotMinecraftLocation: String + get() = System.getProperty("os.name") + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/limited_dev/modpackinstaller/util/PopupFactory.kt b/src/main/kotlin/de/limited_dev/modpackinstaller/util/PopupFactory.kt new file mode 100644 index 0000000..27f9f64 --- /dev/null +++ b/src/main/kotlin/de/limited_dev/modpackinstaller/util/PopupFactory.kt @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2023 limited_dev + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package de.limited_dev.modpackinstaller.util + +import javax.swing.JOptionPane + +object PopupFactory { + + fun getYesOrNoError(title: String, msg: String): Boolean { + println("Popup created") + val res = JOptionPane.showConfirmDialog( + null, + msg, + title, + JOptionPane.OK_CANCEL_OPTION + ) + if (res == 0) { + println("Pressed OK") + return true + } else { + println("Pressed CANCEL") + return false + } + } + +} \ No newline at end of file