From b2e5c4f6a54842fda563ceb2ac7a8ae55c4cf55a Mon Sep 17 00:00:00 2001 From: xBrownieCodez <72617433+xBrownieCodezV2@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:53:19 +0100 Subject: [PATCH] better updateChecker with changelog --- .../jbytemod/utils/UpdateChecker.java | 65 ------------------- .../jbytemod/utils/update/UpdateChecker.java | 53 +++++++++++++++ .../utils/update/objects/Version.java | 37 +++++++++++ .../utils/update/ui/UpdateDialogFrame.java | 51 +++++++++++++++ src/main/java/me/grax/jbytemod/JByteMod.java | 7 +- 5 files changed, 144 insertions(+), 69 deletions(-) delete mode 100644 src/main/java/de/xbrowniecodez/jbytemod/utils/UpdateChecker.java create mode 100644 src/main/java/de/xbrowniecodez/jbytemod/utils/update/UpdateChecker.java create mode 100644 src/main/java/de/xbrowniecodez/jbytemod/utils/update/objects/Version.java create mode 100644 src/main/java/de/xbrowniecodez/jbytemod/utils/update/ui/UpdateDialogFrame.java diff --git a/src/main/java/de/xbrowniecodez/jbytemod/utils/UpdateChecker.java b/src/main/java/de/xbrowniecodez/jbytemod/utils/UpdateChecker.java deleted file mode 100644 index 2f52e7e..0000000 --- a/src/main/java/de/xbrowniecodez/jbytemod/utils/UpdateChecker.java +++ /dev/null @@ -1,65 +0,0 @@ -package de.xbrowniecodez.jbytemod.utils; - -import java.awt.Desktop; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URI; -import java.net.URL; -import java.net.URLConnection; - -import javax.swing.JOptionPane; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; - -import lombok.SneakyThrows; -import me.grax.jbytemod.JByteMod; - -public class UpdateChecker { - - public UpdateChecker() { - JByteMod.LOGGER.log("Checking for updates..."); - JsonObject releaseInfo = fetchLatestReleaseInfo(); - - if (releaseInfo != null) { - String latestVersion = releaseInfo.get("name").getAsString(); - - if (!latestVersion.equals(JByteMod.version)) { - showUpdateDialog(latestVersion); - } - } - } - - @SneakyThrows - private JsonObject fetchLatestReleaseInfo() { - URL url = new URL("https://api.github.com/repos/xBrownieCodezV2/JByteMod-Remastered/releases/latest"); - URLConnection connection = url.openConnection(); - - try (InputStream inputStream = connection.getInputStream(); - InputStreamReader reader = new InputStreamReader(inputStream)) { - return JsonParser.parseReader(reader).getAsJsonObject(); - } - } - - private void showUpdateDialog(String latestVersion) { - int result = JOptionPane.showConfirmDialog(null, - String.format("Version %s is available, would you like to download it?", latestVersion), - "Update available", JOptionPane.YES_NO_OPTION); - - if (result == JOptionPane.YES_OPTION) { - openDownloadLink(latestVersion); - } - } - - @SneakyThrows - private void openDownloadLink(String version) { - URI downloadUri = new URI(String.format( - "https://github.com/xBrownieCodezV2/JByteMod-Remastered/releases/download/%s/JByteMod-Remastered-%s.jar", - version, version)); - - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { - Desktop.getDesktop().browse(downloadUri); - } - } -} diff --git a/src/main/java/de/xbrowniecodez/jbytemod/utils/update/UpdateChecker.java b/src/main/java/de/xbrowniecodez/jbytemod/utils/update/UpdateChecker.java new file mode 100644 index 0000000..72b8879 --- /dev/null +++ b/src/main/java/de/xbrowniecodez/jbytemod/utils/update/UpdateChecker.java @@ -0,0 +1,53 @@ +package de.xbrowniecodez.jbytemod.utils.update; + +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.net.URLConnection; + +import javax.swing.*; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + +import de.xbrowniecodez.jbytemod.utils.update.objects.Version; +import de.xbrowniecodez.jbytemod.utils.update.ui.UpdateDialogFrame; +import lombok.SneakyThrows; +import me.grax.jbytemod.JByteMod; + +public class UpdateChecker { + + public UpdateChecker() { + JByteMod.LOGGER.log("Checking for updates..."); + JsonObject releaseInfo = fetchLatestReleaseInfo(); + if (releaseInfo != null) { + Version latestVersion = new Version(releaseInfo.get("name").getAsString()); + String changelog = releaseInfo.get("body").getAsString(); + if(latestVersion.isNewer(JByteMod.version)) + showUpdateDialog(String.valueOf(latestVersion), changelog); + } + } + + + @SneakyThrows + private JsonObject fetchLatestReleaseInfo() { + URL url = new URL("https://api.github.com/repos/xBrownieCodezV2/JByteMod-Remastered/releases/latest"); + URLConnection connection = url.openConnection(); + + try (InputStream inputStream = connection.getInputStream(); + InputStreamReader reader = new InputStreamReader(inputStream)) { + return JsonParser.parseReader(reader).getAsJsonObject(); + } + } + + private void showUpdateDialog(String latestVersion, String changelog) { + SwingUtilities.invokeLater(() -> { + JFrame updateDialogFrame = new UpdateDialogFrame(latestVersion, changelog); + updateDialogFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + updateDialogFrame.setSize(600, 500); + updateDialogFrame.setLocationRelativeTo(null); + updateDialogFrame.setVisible(true); + }); + } + +} diff --git a/src/main/java/de/xbrowniecodez/jbytemod/utils/update/objects/Version.java b/src/main/java/de/xbrowniecodez/jbytemod/utils/update/objects/Version.java new file mode 100644 index 0000000..0551380 --- /dev/null +++ b/src/main/java/de/xbrowniecodez/jbytemod/utils/update/objects/Version.java @@ -0,0 +1,37 @@ +package de.xbrowniecodez.jbytemod.utils.update.objects; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public final class Version { + private final int majorVersion; + private final int minorVersion; + + public Version(int majorVersion, int minorVersion) { + this.majorVersion = majorVersion; + this.minorVersion = minorVersion; + } + + public Version(String version) { + String[] split = version.split("\\."); + this.majorVersion = Integer.parseInt(split[0]); + this.minorVersion = Integer.parseInt(split[1]); + } + + /** + * Compares this version to another version. + * + * @param version The version to compare to. + * @return true if the version is greater than the other version, false otherwise. + */ + public boolean isNewer(Version version) { + return (majorVersion > version.majorVersion) + || (minorVersion > version.minorVersion); + } + + public String toString() { + return String.format("%d.%d", majorVersion, minorVersion); + } +} diff --git a/src/main/java/de/xbrowniecodez/jbytemod/utils/update/ui/UpdateDialogFrame.java b/src/main/java/de/xbrowniecodez/jbytemod/utils/update/ui/UpdateDialogFrame.java new file mode 100644 index 0000000..f6cce7b --- /dev/null +++ b/src/main/java/de/xbrowniecodez/jbytemod/utils/update/ui/UpdateDialogFrame.java @@ -0,0 +1,51 @@ +package de.xbrowniecodez.jbytemod.utils.update.ui; + +import lombok.SneakyThrows; + +import javax.swing.*; +import java.awt.*; +import java.net.URI; + +public class UpdateDialogFrame extends JFrame { + public UpdateDialogFrame(String latestVersion, String changelog) { + JLabel label = new JLabel(String.format("Version %s is available, would you like to download it?", latestVersion)); + label.setBounds(10, 10, 400, 20); + + JTextArea textArea = new JTextArea(); + textArea.setEditable(false); + textArea.setLineWrap(true); + textArea.setWrapStyleWord(true); + textArea.setText(changelog); + JScrollPane scrollPane = new JScrollPane(textArea); + scrollPane.setBounds(10, 40, 570, 370); + + JButton yesButton = new JButton("Yes"); + yesButton.setBounds(10, 420, 80, 30); + yesButton.addActionListener(e -> { + openDownloadLink(latestVersion); + dispose(); + }); + + JButton noButton = new JButton("No"); + noButton.setBounds(100, 420, 80, 30); + noButton.addActionListener(e -> dispose()); + + add(label); + add(scrollPane); + add(yesButton); + add(noButton); + setLayout(null); + } + + @SneakyThrows + private void openDownloadLink(String version) { + URI downloadUri = new URI(String.format( + "https://github.com/xBrownieCodezV2/JByteMod-Remastered/releases/download/%s/JByteMod-Remastered-%s.jar", + version, version)); + + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + Desktop.getDesktop().browse(downloadUri); + } + } + +} diff --git a/src/main/java/me/grax/jbytemod/JByteMod.java b/src/main/java/me/grax/jbytemod/JByteMod.java index a6e96b6..adaf1f8 100644 --- a/src/main/java/me/grax/jbytemod/JByteMod.java +++ b/src/main/java/me/grax/jbytemod/JByteMod.java @@ -3,8 +3,9 @@ import com.sun.tools.attach.VirtualMachine; import de.xbrowniecodez.jbytemod.securitymanager.CustomSecurityManager; import de.xbrowniecodez.jbytemod.utils.BytecodeUtils; -import de.xbrowniecodez.jbytemod.utils.UpdateChecker; import de.xbrowniecodez.jbytemod.utils.Utils; +import de.xbrowniecodez.jbytemod.utils.update.UpdateChecker; +import de.xbrowniecodez.jbytemod.utils.update.objects.Version; import lombok.Getter; import me.grax.jbytemod.discord.Discord; import me.grax.jbytemod.logging.Logging; @@ -21,8 +22,6 @@ import me.grax.jbytemod.ui.tree.SortedTreeNode; import me.grax.jbytemod.utils.ErrorDisplay; import me.grax.jbytemod.utils.FileUtils; -import me.grax.jbytemod.utils.asm.FrameGen; -import me.grax.jbytemod.utils.attach.RuntimeJarArchive; import me.grax.jbytemod.utils.gui.LookUtils; import me.grax.jbytemod.utils.task.AttachTask; import me.grax.jbytemod.utils.task.RetransformTask; @@ -51,7 +50,7 @@ import java.util.LinkedHashMap; public class JByteMod extends JFrame { - public final static String version = Utils.readPropertiesFile().getProperty("version"); + public static final Version version = new Version(Utils.readPropertiesFile().getProperty("version")); private static final String jbytemod = "JByteMod Remastered v" + version; public static File workingDir = new File(".");