Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/com/github/manolo8/darkbot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +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.utils.BotUpdateUtils;
import com.github.manolo8.darkbot.utils.LibSetup;
import com.github.manolo8.darkbot.utils.LogUtils;
import com.github.manolo8.darkbot.utils.StartupParams;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.net.URLClassLoader;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permission;
Expand Down Expand Up @@ -67,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));
}

Expand Down
81 changes: 81 additions & 0 deletions src/main/java/com/github/manolo8/darkbot/utils/BotUpdateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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) {
String defaultJarName = "DarkBot.jar";
String defaultTempJarName = "DarkBot.jar.tmp";

try {
String runningNameJar = getRunningNameJar();
if (runningNameJar == null) return;
String[] command = prefix(args, "java", "-jar", defaultJarName);

if (runningNameJar.endsWith(".jar")) {
new File(defaultTempJarName).delete();

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);
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();
copyAndRun(in, Path.of(defaultTempJarName), command);
}
}
} else if (runningNameJar.equals(defaultTempJarName)) {
copyAndRun(Files.newInputStream(Path.of(runningNameJar)), Path.of(defaultJarName), command);
System.exit(0);
}
} 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();
}
}
}
4 changes: 3 additions & 1 deletion src/main/java/com/github/manolo8/darkbot/utils/LibSetup.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -31,6 +32,7 @@ public static class Lib {
public Set<String> altSha256;
public String download;
public boolean auto;
public Version version;
}

public static void setupLibraries() {
Expand All @@ -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;
}

Expand Down