Created
July 12, 2021 20:42
-
-
Save isXander/3b9e0063e43e37391d1a05019bd9f739 to your computer and use it in GitHub Desktop.
installs/updates evergreenhud and it's addons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.isxander.evergreenhud.installer | |
import co.uk.isxander.libinstaller.InstallerUtils | |
import org.apache.commons.io.FileUtils | |
import java.awt.Font | |
import java.io.* | |
import java.net.HttpURLConnection | |
import java.net.URL | |
import java.nio.charset.Charset | |
import java.util.jar.JarFile | |
import javax.swing.* | |
object EvergreenHUDInstaller { | |
private const val coreUrl = "https://static.isxander.co.uk/evergreenhud/core/%s.jar" | |
private const val coreVersion = "https://raw.githubusercontent.com/isXander/EvergreenHUD/main/version.json" | |
fun install(mcDir: File) { | |
val dataDir = File(mcDir, "evergreenhud") | |
if (!dataDir.exists()) dataDir.mkdirs() | |
installCore(dataDir) | |
updateAddons(dataDir) | |
} | |
private fun installCore(dataDir: File) { | |
val config = InstallerUtils.readFile(File(dataDir, "config/global.json")) | |
val releaseChannel = config.optString("release_channel", "release")!! | |
val localFile = File(dataDir, "metadata.json") | |
val localVersion = InstallerUtils.readFile(localFile) | |
val remoteVersion = InstallerUtils.fetchJSON(coreVersion) | |
val currentVersion = localVersion.optString(releaseChannel) | |
val latestVersion = remoteVersion.optString(releaseChannel)!! | |
val outputFile = File(dataDir, "core.jar") | |
if (currentVersion == null | |
|| localVersion.optString("release_channel", "release") != releaseChannel | |
|| latestVersion != currentVersion) { | |
if (InstallerUtils.download(String.format(coreUrl, latestVersion), outputFile, "EvergreenHUD", "${if (outputFile.exists()) "Updating" else "Downloading"} core...")) { | |
localVersion.put("release_channel", releaseChannel) | |
localVersion.put(releaseChannel, latestVersion) | |
FileUtils.write(localFile, localVersion.toString(), Charset.defaultCharset()) | |
InstallerUtils.addToClasspath(outputFile) | |
} | |
} | |
} | |
private fun updateAddons(dataDir: File) { | |
val addonsFolderFile = File(dataDir, "addons") | |
if (!addonsFolderFile.exists()) addonsFolderFile.mkdirs() | |
val jarFiles = addonsFolderFile.listFiles { _, name -> name.endsWith(".jar") }!! | |
val remove = ArrayList<File>() | |
for (addonFile in jarFiles) { | |
JarFile(addonFile).use { jar -> | |
val entry = jar.getEntry("evergreen.addon.json") | |
val addonJson = InstallerUtils.readFile(File(entry.name)) | |
val addonName = addonJson.optString("name", "Unknown")!! | |
val updateUrl = addonJson.optString("updateUrl", null) | |
if (updateUrl != null) { | |
val updateJson = InstallerUtils.fetchJSON(updateUrl) | |
val currentVersion = addonJson.optString("version", "1.0")!! | |
val latestVersion = updateJson.optString("version", "1.0")!! | |
if (currentVersion != latestVersion) { | |
InstallerUtils.download(updateJson.optString("download", null)!!, File(addonsFolderFile, "$addonName ($latestVersion).jar"), "EvergreenHUD", "Updating $addonName addon...") | |
remove.add(addonFile) | |
} | |
} | |
} | |
} | |
for (file in remove) { | |
file.delete() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment