feat: finished backend stuff, added installing and unziping of loader and zipfiles, added other utils

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-03-12 03:50:23 +01:00
parent 74d09702a8
commit e9967c9d8e
7 changed files with 169 additions and 6 deletions

View file

@ -18,24 +18,90 @@
package de.limited_dev.modpackinstaller.backend
import de.limited_dev.modpackinstaller.build.BuildConstants
import de.limited_dev.modpackinstaller.gui.DataUtil
import de.limited_dev.modpackinstaller.gui.DataUtil.downloadFile
import de.limited_dev.modpackinstaller.gui.MainPanel
import de.limited_dev.modpackinstaller.util.PopupFactory
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File
import kotlin.system.exitProcess
object Manager {
fun startDownloadAndInstall(dotMcLocation: String) {
val tempFolder = File(dotMcLocation + File.separator + "temp")
val modFolder = File(dotMcLocation + File.separator + "mods")
Runtime.getRuntime().addShutdownHook(Thread {
println("Application is shutting down")
val tempFolder2 = File(dotMcLocation + File.separator + "temp")
if (tempFolder.exists())
tempFolder2.deleteRecursively()
})
if (tempFolder.exists())
tempFolder.deleteRecursively()
tempFolder.mkdirs()
val modloaderCoroutine = GlobalScope.launch {
println("Downloading Modloader")
val megabytesDownloaded = runBlocking {
downloadFile(
BuildConstants.fabricDownloadLocation,
dotMcLocation + File.separator + "temp" + File.separator + "fabric.jar",
true
)
}
println("Downloaded $megabytesDownloaded MB Modloader")
MainPanel.updateFabricModloader("Installing...")
val command = "java"
val args = listOf(
"-jar",
"fabric.jar",
"client",
"-dir $dotMcLocation",
"-mcversion ${BuildConstants.mcversion}",
"win32"
)
val processBuilder = ProcessBuilder(command, *args.toTypedArray())
.directory(tempFolder)
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
val process = processBuilder.start()
val exitCode = process.waitFor()
MainPanel.updateFabricModloader("Done!")
println("Java application exited with code: $exitCode")
}
val modpackCoroutine = GlobalScope.launch {
println("Downloading Modpack")
val megabytesDownloaded = runBlocking {
downloadFile(
BuildConstants.modpackDownloadLocation,
dotMcLocation + File.separator + "temp" + File.separator + "pack.zip",
false
)
}
println("Downloaded $megabytesDownloaded MB Modpack\nUnzipping...")
MainPanel.updateModpack("Unzipping...")
var modzip = File(tempFolder.absolutePath + File.separator + "pack.zip")
DataUtil.unzip(modzip, modFolder)
MainPanel.updateModpack("Done!")
println("Done!")
}
runBlocking {
modloaderCoroutine.join()
modpackCoroutine.join()
}
println("Done.")
PopupFactory.getOk(
"Installed!",
"The Modpack is now installed!\nYou may now (re)open your Minecraft Launcher!\n\nHave fun playing! ~limited_dev"
)
exitProcess(0)
}
}