diff --git a/src/main/java/gg/skytils/skytilsmod/tweaker/SkytilsLoadingPlugin.java b/src/main/java/gg/skytils/skytilsmod/tweaker/SkytilsLoadingPlugin.java index 6e6c83876..028653e6f 100644 --- a/src/main/java/gg/skytils/skytilsmod/tweaker/SkytilsLoadingPlugin.java +++ b/src/main/java/gg/skytils/skytilsmod/tweaker/SkytilsLoadingPlugin.java @@ -22,12 +22,16 @@ import kotlin.text.StringsKt; import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin; +import javax.swing.*; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; import java.io.File; import java.net.URISyntaxException; import java.util.Map; import static gg.skytils.skytilsmod.tweaker.TweakerUtil.exit; import static gg.skytils.skytilsmod.tweaker.TweakerUtil.showMessage; +import static gg.skytils.skytilsmod.tweaker.TweakerUtil.updateEssential; @IFMLLoadingPlugin.Name("Skytils On Top") @IFMLLoadingPlugin.SortingIndex(69) @@ -122,8 +126,21 @@ public SkytilsLoadingPlugin() throws URISyntaxException { String name = realFile.getName().contains(".jar") ? realFile.getName() : StringsKt.substringAfterLast(StringsKt.substringBeforeLast(file.getAbsolutePath(), ".jar", "unknown"), "/", "Unknown"); if (name.endsWith("!")) name = name.substring(0, name.length() - 1); - - showMessage(kotlinErrorMessage + "
The culprit seems to be " + name + "
It bundles version " + KotlinVersion.CURRENT + "

"); + String errorMessage = kotlinErrorMessage + "
The culprit seems to be " + name + "
It bundles version " + KotlinVersion.CURRENT + "

"; + if (name.equals("Essential (forge_1.8.9).jar")) { + JButton button = new JButton("Update Essential"); + final File essentialLoc = realFile; + button.addMouseListener(new MouseAdapter() { + @Override + public void mouseClicked(MouseEvent event) { + updateEssential(essentialLoc); + exit(); + } + }); + showMessage(errorMessage, button); + } else { + showMessage(errorMessage); + } exit(); } if (checkForClass("com.sky.voidchat.EDFMLLoadingPlugin")) { diff --git a/src/main/java/gg/skytils/skytilsmod/tweaker/TweakerUtil.java b/src/main/java/gg/skytils/skytilsmod/tweaker/TweakerUtil.java index 441658f71..9374c8e3b 100644 --- a/src/main/java/gg/skytils/skytilsmod/tweaker/TweakerUtil.java +++ b/src/main/java/gg/skytils/skytilsmod/tweaker/TweakerUtil.java @@ -18,6 +18,8 @@ package gg.skytils.skytilsmod.tweaker; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import gg.skytils.skytilsmod.Reference; import net.minecraft.launchwrapper.Launch; import org.apache.commons.io.IOUtils; @@ -27,15 +29,20 @@ import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.Arrays; +import java.util.Locale; public class TweakerUtil { public static void exit() { @@ -134,6 +141,12 @@ static void registerTransformerExclusions(String... classes) { } public static String makeRequest(String url) throws IOException { + try (InputStream in = makeRequestStream(url)) { + return IOUtils.toString(in); + } + } + + public static InputStream makeRequestStream(String url) throws IOException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setRequestMethod("GET"); conn.setDoOutput(true); @@ -141,7 +154,53 @@ public static String makeRequest(String url) throws IOException { conn.setConnectTimeout(10000); conn.setReadTimeout(30000); - return IOUtils.toString(conn.getInputStream()); + return conn.getInputStream(); + } + + public static void downloadFile(String url, File file) throws IOException { + try (InputStream in = makeRequestStream(url)) { + Files.copy(in, file.toPath(), StandardCopyOption.REPLACE_EXISTING); + } + } + + static void updateEssential(File essentialLoc) { + try { + System.out.println("Attempting to replace Essential at " + essentialLoc); + + System.out.println("Requesting Essential metadata"); + JsonObject jsonObject = JsonParser.parseString(makeRequest("https://downloads.essential.gg/v1/mods/essential/essential/")).getAsJsonObject(); + System.out.println("Got metadata: " + jsonObject); + String essentialDownloadURL = jsonObject.getAsJsonObject("stable").getAsJsonObject("forge_1-8-9").get("url").getAsString(); + System.out.println("Latest essential build: " + essentialDownloadURL); + + File updateDir = new File(new File(Launch.minecraftHome, "skytils"), "updates"); + String taskURL = "https://github.com/Skytils/SkytilsMod-Data/releases/download/files/SkytilsInstaller-1.2.0.jar"; + File taskFile = new File(new File(updateDir, "tasks"), "SkytilsInstaller-1.2.0.jar"); + if (taskFile.mkdirs() || taskFile.createNewFile()) { + System.out.println("Downloading task file"); + downloadFile(taskURL, taskFile); + System.out.println("Successfully downloaded task file"); + } + System.out.println("Downloading Essential"); + File newEssentialJar = new File(updateDir, "Essential.jar"); + downloadFile(essentialDownloadURL, newEssentialJar); + System.out.println("Successfully downloaded Essential"); + + String runtime = getJavaRuntime(); + System.out.println("Using runtime " + runtime); + Runtime.getRuntime().exec("\"" + runtime + "\" -jar \"" + taskFile.getAbsolutePath() + "\" replace \"" + essentialLoc.getAbsolutePath() + "\" \"" + newEssentialJar.getAbsolutePath() + "\""); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static String getJavaRuntime() throws IOException { + String os = System.getProperty("os.name"); + String java = System.getProperty("java.home") + File.separator + "bin" + File.separator + (os != null && os.toLowerCase(Locale.ENGLISH).startsWith("windows") ? "java.exe" : "java"); + if (!(new File(java)).isFile()) { + throw new IOException("Unable to find suitable java runtime at $java"); + } + return java; } public static void trySetLookAndFeel() {