From a19e8c85330b635575c8df554dc77c7868a63ecf Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Sun, 10 Apr 2022 23:10:29 -0400 Subject: [PATCH] Add automatic updater with config option --- build.gradle | 2 +- jaml-installer.iss | 1 + .../java/tech/nevets/jaml/AutoUpdater.java | 25 +++++++++++++++++++ src/main/java/tech/nevets/jaml/Config.java | 2 ++ .../java/tech/nevets/jaml/gui/StartupGui.java | 13 +++++++--- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 3a95e30..e6fe088 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'tech.nevets.jaml' -version '0.8.0' +version '0.8.1' def build = 'dev' repositories { diff --git a/jaml-installer.iss b/jaml-installer.iss index 33d8159..170b156 100644 --- a/jaml-installer.iss +++ b/jaml-installer.iss @@ -42,6 +42,7 @@ Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{ [Files] Source: "./build/launch4j/{#MyAppExeName}"; DestDir: "{app}"; Flags: ignoreversion Source: "./jre/*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs +Source: "./JAML-Updater.jar"; Flags: ignoreversion ; NOTE: Don't use "Flags: ignoreversion" on any shared system files [Registry] diff --git a/src/main/java/tech/nevets/jaml/AutoUpdater.java b/src/main/java/tech/nevets/jaml/AutoUpdater.java index dd9ac22..15c9ba9 100644 --- a/src/main/java/tech/nevets/jaml/AutoUpdater.java +++ b/src/main/java/tech/nevets/jaml/AutoUpdater.java @@ -1,4 +1,29 @@ package tech.nevets.jaml; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import tech.nevets.jaml.gui.StartupGui; +import tech.nevets.jaml.util.HttpUtils; + +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; + public class AutoUpdater { + + public static void checkForUpdate() { + try { + String version = HttpUtils.get(new URI("https://api.nevets.tech/jaml/version.json"), new HashMap<>()); + JsonElement versionJson = new Gson().fromJson(version, JsonElement.class); + + if (versionJson.getAsJsonObject().get("version").toString().replace("\"", "").equalsIgnoreCase(Config.CONFIG.getString("launcher.version"))) { + String command = JAML.path + "\\jre\\bin\\javaw.exe -jar " + JAML.path + "\\JAML-Updater.jar"; + Runtime.getRuntime().exec(command); + } + + } catch (URISyntaxException | IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/tech/nevets/jaml/Config.java b/src/main/java/tech/nevets/jaml/Config.java index c740e1e..b593367 100644 --- a/src/main/java/tech/nevets/jaml/Config.java +++ b/src/main/java/tech/nevets/jaml/Config.java @@ -57,6 +57,8 @@ public class Config { YML_FILE.setComment("launcher.keep-open", "Keeps the launcher open after launching the game"); YML_FILE.addDefault("launcher.first-launch", true); YML_FILE.setComment("launcher.first-launch", "Do Not Change, will regenerate files"); + YML_FILE.addDefault("launcher.auto-update", true); + YML_FILE.setComment("launcher.auto-update", "Enables a simple automatic updates system."); YML_FILE.addDefault("launcher.verbose", false); YML_FILE.setComment("launcher.verbose", "If true, will print out all the messages"); YML_FILE.addDefault("launcher.version", JAML.VERSION); diff --git a/src/main/java/tech/nevets/jaml/gui/StartupGui.java b/src/main/java/tech/nevets/jaml/gui/StartupGui.java index 94aece3..19ef76d 100644 --- a/src/main/java/tech/nevets/jaml/gui/StartupGui.java +++ b/src/main/java/tech/nevets/jaml/gui/StartupGui.java @@ -1,5 +1,6 @@ package tech.nevets.jaml.gui; +import tech.nevets.jaml.AutoUpdater; import tech.nevets.jaml.Config; import tech.nevets.jaml.JAML; import tech.nevets.jaml.gui.panels.CustomProgressBarPanel; @@ -10,14 +11,13 @@ import tech.nevets.jaml.util.VersionUtils; import javax.swing.*; import javax.swing.border.EmptyBorder; -import java.io.IOException; import java.nio.file.Path; public class StartupGui extends JFrame { JPanel contentPane; private static JLabel currentTaskLabel; private static CustomProgressBarPanel progressBar; - private static final int TASKS = 7; + private static final int TASKS = 8; private static int task = 0; public StartupGui() { @@ -73,6 +73,13 @@ public class StartupGui extends JFrame { Config.loadConfig(); smoothIncrease(); + task++; + if (Config.CONFIG.getBoolean("launcher.auto-update")) { + currentTaskLabel.setText("Checking for updates..."); + AutoUpdater.checkForUpdate(); + } + smoothIncrease(); + currentTaskLabel.setText("Downloading Version List..."); task++; VersionUtils.downloadList(); @@ -115,7 +122,7 @@ public class StartupGui extends JFrame { currentTaskLabel.setText(currentTask); } - private static void makeDirs() { + public static void makeDirs() { if (System.getenv("JAML_HOME") == null) { JAML.path = Path.of(System.getenv("APPDATA") + "\\.jaml\\"); } else {