From 9c565e1fc425db2aa53d2c2e5ae183253f44be5a Mon Sep 17 00:00:00 2001 From: fabio1999ita Date: Sat, 7 Jan 2023 18:05:32 +0100 Subject: [PATCH 1/6] add auto updater --- .../java/com/github/manolo8/darkbot/Bot.java | 69 ++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/manolo8/darkbot/Bot.java b/src/main/java/com/github/manolo8/darkbot/Bot.java index 5b6f51623..8582246a4 100644 --- a/src/main/java/com/github/manolo8/darkbot/Bot.java +++ b/src/main/java/com/github/manolo8/darkbot/Bot.java @@ -4,14 +4,27 @@ import com.formdev.flatlaf.ui.FlatNativeWindowBorder; import com.formdev.flatlaf.util.SystemInfo; import com.github.manolo8.darkbot.extensions.plugins.PluginClassLoader; +import com.github.manolo8.darkbot.extensions.util.Version; +import com.github.manolo8.darkbot.gui.utils.Popups; import com.github.manolo8.darkbot.utils.LibSetup; import com.github.manolo8.darkbot.utils.LogUtils; import com.github.manolo8.darkbot.utils.StartupParams; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import javax.swing.*; import java.awt.*; +import java.io.File; import java.io.IOException; -import java.net.URLClassLoader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.URLConnection; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.security.AllPermission; import java.security.CodeSource; import java.security.Permission; @@ -33,6 +46,60 @@ public static void main(String[] args) throws IOException { LogUtils.setupLogOutput(); + try { + String fileName = Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); + if (fileName.equals("DarkBot.jar.tmp")) { + Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); + Runtime.getRuntime().exec("java -jar DarkBot.jar"); + System.exit(0); + } + if (fileName.equals("DarkBot.jar")) { + new File("DarkBot.jar.tmp").delete(); + + try { + URL url = new URL("https://"); + URLConnection request = url.openConnection(); + request.connect(); + + JsonObject jsonObjectAlt = JsonParser.parseReader(new InputStreamReader((InputStream) request.getContent())).getAsJsonObject(); + String version = jsonObjectAlt.get("version").getAsString(); + + Version lastVersion = new Version(version); + + if (Main.VERSION.compareTo(lastVersion) < 0) { + JButton cancel = new JButton("Cancel"); + JButton download = new JButton("Update"); + Popups.of("DarkBot updater", + new JOptionPane("A new version of Darkbot is available\n" + Main.VERSION + " ➜ " + lastVersion, + JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, + null, new Object[]{download, cancel}, download)).showSync(); + + cancel.addActionListener((event) -> SwingUtilities.getWindowAncestor(cancel).setVisible(false)); + + download.addActionListener((event) -> { + try { + URL website = new URL("https://"); + try (InputStream in = website.openStream()) { + Files.copy(in, Path.of("DarkBot.jar.tmp"), StandardCopyOption.REPLACE_EXISTING); + } + + Runtime.getRuntime().exec("java -jar DarkBot.jar.tmp"); + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } catch (InvalidPathException e) { + + } + try { UIManager.getFont("Label.font"); // Prevents a linux crash From e98b2e98674267f5a03c78acbd006ec74e1ba9d6 Mon Sep 17 00:00:00 2001 From: fabio1999ita Date: Fri, 13 Jan 2023 15:37:09 +0100 Subject: [PATCH 2/6] fix file name problem --- .../java/com/github/manolo8/darkbot/Bot.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/Bot.java b/src/main/java/com/github/manolo8/darkbot/Bot.java index 8582246a4..f1d92a1eb 100644 --- a/src/main/java/com/github/manolo8/darkbot/Bot.java +++ b/src/main/java/com/github/manolo8/darkbot/Bot.java @@ -48,12 +48,7 @@ public static void main(String[] args) throws IOException { try { String fileName = Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); - if (fileName.equals("DarkBot.jar.tmp")) { - Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); - Runtime.getRuntime().exec("java -jar DarkBot.jar"); - System.exit(0); - } - if (fileName.equals("DarkBot.jar")) { + if (fileName.endsWith(".jar")) { new File("DarkBot.jar.tmp").delete(); try { @@ -63,7 +58,6 @@ public static void main(String[] args) throws IOException { JsonObject jsonObjectAlt = JsonParser.parseReader(new InputStreamReader((InputStream) request.getContent())).getAsJsonObject(); String version = jsonObjectAlt.get("version").getAsString(); - Version lastVersion = new Version(version); if (Main.VERSION.compareTo(lastVersion) < 0) { @@ -94,10 +88,14 @@ public static void main(String[] args) throws IOException { e.printStackTrace(); } } - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } catch (InvalidPathException e) { + if (fileName.equals("DarkBot.jar.tmp")) { + Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); + Runtime.getRuntime().exec("java -jar DarkBot.jar"); + System.exit(0); + } + } catch (URISyntaxException | InvalidPathException e) { + e.printStackTrace(); } try { From eca8892bdda0cb554323fe83be0ce80e6b8be1c2 Mon Sep 17 00:00:00 2001 From: fabio1999ita Date: Sat, 14 Jan 2023 01:25:20 +0100 Subject: [PATCH 3/6] apply suggestion --- .../java/com/github/manolo8/darkbot/Bot.java | 69 +------------------ .../manolo8/darkbot/utils/BotUpdateUtils.java | 60 ++++++++++++++++ .../manolo8/darkbot/utils/LibSetup.java | 4 +- 3 files changed, 66 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java diff --git a/src/main/java/com/github/manolo8/darkbot/Bot.java b/src/main/java/com/github/manolo8/darkbot/Bot.java index f1d92a1eb..a3364da8c 100644 --- a/src/main/java/com/github/manolo8/darkbot/Bot.java +++ b/src/main/java/com/github/manolo8/darkbot/Bot.java @@ -4,27 +4,14 @@ import com.formdev.flatlaf.ui.FlatNativeWindowBorder; import com.formdev.flatlaf.util.SystemInfo; import com.github.manolo8.darkbot.extensions.plugins.PluginClassLoader; -import com.github.manolo8.darkbot.extensions.util.Version; -import com.github.manolo8.darkbot.gui.utils.Popups; +import com.github.manolo8.darkbot.utils.BotUpdateUtils; import com.github.manolo8.darkbot.utils.LibSetup; import com.github.manolo8.darkbot.utils.LogUtils; import com.github.manolo8.darkbot.utils.StartupParams; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import javax.swing.*; import java.awt.*; -import java.io.File; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URISyntaxException; -import java.net.URL; -import java.net.URLConnection; -import java.nio.file.Files; -import java.nio.file.InvalidPathException; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; import java.security.AllPermission; import java.security.CodeSource; import java.security.Permission; @@ -46,58 +33,6 @@ public static void main(String[] args) throws IOException { LogUtils.setupLogOutput(); - try { - String fileName = Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); - if (fileName.endsWith(".jar")) { - new File("DarkBot.jar.tmp").delete(); - - try { - URL url = new URL("https://"); - URLConnection request = url.openConnection(); - request.connect(); - - JsonObject jsonObjectAlt = JsonParser.parseReader(new InputStreamReader((InputStream) request.getContent())).getAsJsonObject(); - String version = jsonObjectAlt.get("version").getAsString(); - Version lastVersion = new Version(version); - - if (Main.VERSION.compareTo(lastVersion) < 0) { - JButton cancel = new JButton("Cancel"); - JButton download = new JButton("Update"); - Popups.of("DarkBot updater", - new JOptionPane("A new version of Darkbot is available\n" + Main.VERSION + " ➜ " + lastVersion, - JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, - null, new Object[]{download, cancel}, download)).showSync(); - - cancel.addActionListener((event) -> SwingUtilities.getWindowAncestor(cancel).setVisible(false)); - - download.addActionListener((event) -> { - try { - URL website = new URL("https://"); - try (InputStream in = website.openStream()) { - Files.copy(in, Path.of("DarkBot.jar.tmp"), StandardCopyOption.REPLACE_EXISTING); - } - - Runtime.getRuntime().exec("java -jar DarkBot.jar.tmp"); - System.exit(0); - } catch (IOException e) { - e.printStackTrace(); - } - }); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - if (fileName.equals("DarkBot.jar.tmp")) { - Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); - Runtime.getRuntime().exec("java -jar DarkBot.jar"); - System.exit(0); - } - } catch (URISyntaxException | InvalidPathException e) { - e.printStackTrace(); - } - try { UIManager.getFont("Label.font"); // Prevents a linux crash @@ -132,6 +67,8 @@ public static void main(String[] args) throws IOException { Runtime.getRuntime().addShutdownHook(new Thread(() -> new Throwable("DarkBot shutdown peacefully!").printStackTrace())); + BotUpdateUtils.checkAndUpdateBot(args); + SwingUtilities.invokeLater(() -> new Main(params)); } diff --git a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java new file mode 100644 index 000000000..79a9e704b --- /dev/null +++ b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java @@ -0,0 +1,60 @@ +package com.github.manolo8.darkbot.utils; + +import com.github.manolo8.darkbot.Bot; +import com.github.manolo8.darkbot.Main; +import com.github.manolo8.darkbot.gui.utils.Popups; + +import javax.swing.*; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +public class BotUpdateUtils { + + public static void checkAndUpdateBot(String... args) { + try { + String fileName = Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); + + StringBuilder sb = new StringBuilder(); + for (String arg : args) { + sb.append(" ").append("-").append(arg); + } + + if (fileName.endsWith(".jar")) { + new File("DarkBot.jar.tmp").delete(); + + LibSetup.Lib lib = LibSetup.getLib("DarkBot.jar"); + + if (Main.VERSION.compareTo(lib.version) < 0) { + System.out.println("DarkBot has an update from version " + Main.VERSION + " to " + lib.version); + int result = Popups.of("DarkBot updater", "A new version of DarkBot is available\n" + + Main.VERSION + " ➜ " + lib.version, JOptionPane.INFORMATION_MESSAGE) + .optionType(JOptionPane.OK_CANCEL_OPTION) + .showOptionSync(); + + if (result == JOptionPane.OK_OPTION) { + InputStream in = new URL(lib.download).openStream(); + Files.copy(in, Path.of("DarkBot.jar.tmp"), StandardCopyOption.REPLACE_EXISTING); + + Runtime.getRuntime().exec("java -jar DarkBot.jar.tmp" + sb); + System.exit(0); + } + } + } + + if (fileName.equals("DarkBot.jar.tmp")) { + Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); + Runtime.getRuntime().exec("java -jar DarkBot.jar" + sb); + System.exit(0); + } + } catch (URISyntaxException | InvalidPathException | IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/github/manolo8/darkbot/utils/LibSetup.java b/src/main/java/com/github/manolo8/darkbot/utils/LibSetup.java index b0c3b3f2b..935a84ead 100644 --- a/src/main/java/com/github/manolo8/darkbot/utils/LibSetup.java +++ b/src/main/java/com/github/manolo8/darkbot/utils/LibSetup.java @@ -1,6 +1,7 @@ package com.github.manolo8.darkbot.utils; import com.github.manolo8.darkbot.Main; +import com.github.manolo8.darkbot.extensions.util.Version; import com.github.manolo8.darkbot.utils.http.Http; import com.google.gson.reflect.TypeToken; import org.jetbrains.annotations.Nullable; @@ -31,6 +32,7 @@ public static class Lib { public Set altSha256; public String download; public boolean auto; + public Version version; } public static void setupLibraries() { @@ -48,7 +50,7 @@ public static void setupLibraries() { } } - private static Lib getLib(String path) { + protected static Lib getLib(String path) { return libraries != null ? libraries.get(path) : null; } From e06e5850bda22003bca943282e99d56368cb0d6e Mon Sep 17 00:00:00 2001 From: fabio1999ita Date: Sat, 14 Jan 2023 02:24:10 +0100 Subject: [PATCH 4/6] remove string builder --- .../manolo8/darkbot/utils/BotUpdateUtils.java | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java index 79a9e704b..95d7a1ad3 100644 --- a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java +++ b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java @@ -20,11 +20,7 @@ public class BotUpdateUtils { public static void checkAndUpdateBot(String... args) { try { String fileName = Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); - - StringBuilder sb = new StringBuilder(); - for (String arg : args) { - sb.append(" ").append("-").append(arg); - } + String[] command = prefix(args, "java", "-jar", "DarkBot.jar"); if (fileName.endsWith(".jar")) { new File("DarkBot.jar.tmp").delete(); @@ -42,7 +38,7 @@ public static void checkAndUpdateBot(String... args) { InputStream in = new URL(lib.download).openStream(); Files.copy(in, Path.of("DarkBot.jar.tmp"), StandardCopyOption.REPLACE_EXISTING); - Runtime.getRuntime().exec("java -jar DarkBot.jar.tmp" + sb); + Runtime.getRuntime().exec(command); System.exit(0); } } @@ -50,11 +46,18 @@ public static void checkAndUpdateBot(String... args) { if (fileName.equals("DarkBot.jar.tmp")) { Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); - Runtime.getRuntime().exec("java -jar DarkBot.jar" + sb); + Runtime.getRuntime().exec(command); System.exit(0); } } catch (URISyntaxException | InvalidPathException | IOException e) { e.printStackTrace(); } } + + public static String[] prefix(String[] array, String... prefix) { + String[] newArr = new String[array.length + prefix.length]; + System.arraycopy(prefix, 0, newArr, 0, prefix.length); + System.arraycopy(array, 0, newArr, prefix.length, array.length); + return newArr; + } } From 27f856ad9c0530e8e0e507c6ac846d3705c16217 Mon Sep 17 00:00:00 2001 From: fabio1999ita Date: Sun, 15 Jan 2023 19:25:25 +0100 Subject: [PATCH 5/6] suggestion --- .../manolo8/darkbot/utils/BotUpdateUtils.java | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java index 95d7a1ad3..f57dea24e 100644 --- a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java +++ b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java @@ -18,14 +18,18 @@ public class BotUpdateUtils { public static void checkAndUpdateBot(String... args) { + String defaultJarName = "DarkBot.jar"; + String defaultTempJarName = "DarkBot.jar.tmp"; + try { - String fileName = Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); - String[] command = prefix(args, "java", "-jar", "DarkBot.jar"); + String runningNameJar = getRunningNameJar(); + if (runningNameJar == null) return; + String[] command = prefix(args, "java", "-jar", defaultJarName); - if (fileName.endsWith(".jar")) { - new File("DarkBot.jar.tmp").delete(); + if (runningNameJar.endsWith(".jar")) { + new File(defaultTempJarName).delete(); - LibSetup.Lib lib = LibSetup.getLib("DarkBot.jar"); + LibSetup.Lib lib = LibSetup.getLib(defaultJarName); if (Main.VERSION.compareTo(lib.version) < 0) { System.out.println("DarkBot has an update from version " + Main.VERSION + " to " + lib.version); @@ -36,28 +40,41 @@ public static void checkAndUpdateBot(String... args) { if (result == JOptionPane.OK_OPTION) { InputStream in = new URL(lib.download).openStream(); - Files.copy(in, Path.of("DarkBot.jar.tmp"), StandardCopyOption.REPLACE_EXISTING); - - Runtime.getRuntime().exec(command); - System.exit(0); + copyAndRun(in, Path.of(defaultTempJarName), command); } } + } else if (runningNameJar.equals(defaultTempJarName)) { + copyAndRun(Files.newInputStream(Path.of(runningNameJar)), Path.of(defaultJarName), command); } - - if (fileName.equals("DarkBot.jar.tmp")) { - Files.copy(Path.of(fileName), Path.of("DarkBot.jar"), StandardCopyOption.REPLACE_EXISTING); - Runtime.getRuntime().exec(command); - System.exit(0); - } - } catch (URISyntaxException | InvalidPathException | IOException e) { + } catch (InvalidPathException | IOException e) { e.printStackTrace(); } } + public static String getRunningNameJar() { + try { + return Path.of(Bot.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getFileName().toString(); + } catch (URISyntaxException e) { + return null; + } + } + public static String[] prefix(String[] array, String... prefix) { String[] newArr = new String[array.length + prefix.length]; System.arraycopy(prefix, 0, newArr, 0, prefix.length); System.arraycopy(array, 0, newArr, prefix.length, array.length); return newArr; } + + private static void copyAndRun(InputStream newExecutable, Path path, String... args) { + try { + Files.copy(newExecutable, path, StandardCopyOption.REPLACE_EXISTING); + String[] command = prefix(args, "java", "-jar", path.getFileName().toString()); + Runtime.getRuntime().exec(command); + System.out.println("Closing process, updated jar '" + path + "' started running!"); + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + } } From 3006b55e7cae812b32dcde5b57cd76db394e9e72 Mon Sep 17 00:00:00 2001 From: fabio1999ita Date: Mon, 16 Jan 2023 19:54:20 +0100 Subject: [PATCH 6/6] prevent tmp jar can never close --- .../java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java index f57dea24e..3c0b1774e 100644 --- a/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java +++ b/src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java @@ -45,6 +45,7 @@ public static void checkAndUpdateBot(String... args) { } } else if (runningNameJar.equals(defaultTempJarName)) { copyAndRun(Files.newInputStream(Path.of(runningNameJar)), Path.of(defaultJarName), command); + System.exit(0); } } catch (InvalidPathException | IOException e) { e.printStackTrace();