Skip to content

Commit

Permalink
feat: add an update Essential button to kotlin error message
Browse files Browse the repository at this point in the history
  • Loading branch information
My-Name-Is-Jeff committed Dec 19, 2023
1 parent 49366c1 commit f176398
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 + "<br>The culprit seems to be " + name + "<br>It bundles version " + KotlinVersion.CURRENT + "</p></html>");
String errorMessage = kotlinErrorMessage + "<br>The culprit seems to be " + name + "<br>It bundles version " + KotlinVersion.CURRENT + "</p></html>";
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")) {
Expand Down
61 changes: 60 additions & 1 deletion src/main/java/gg/skytils/skytilsmod/tweaker/TweakerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -134,14 +141,66 @@ 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);
conn.addRequestProperty("User-Agent", "Skytils/" + Reference.VERSION);
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() {
Expand Down

0 comments on commit f176398

Please sign in to comment.