Skip to content

Commit

Permalink
better updateChecker with changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
apkreader committed Jan 12, 2024
1 parent e1d94a4 commit b2e5c4f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 69 deletions.
65 changes: 0 additions & 65 deletions src/main/java/de/xbrowniecodez/jbytemod/utils/UpdateChecker.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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);
});
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

}
7 changes: 3 additions & 4 deletions src/main/java/me/grax/jbytemod/JByteMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(".");
Expand Down

0 comments on commit b2e5c4f

Please sign in to comment.