Updates the updater to use the new updater .jar

This commit is contained in:
Kristian Knarvik 2020-08-29 14:03:26 +02:00
parent 5b15fea7b3
commit 39a8c14ece

View File

@ -4,15 +4,14 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.nio.file.Paths;
import java.util.Scanner;
import static net.knarcraft.minecraftserverlauncher.Main.getApplicationWorkDirectory;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsScanner;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
@ -22,15 +21,19 @@ import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.read
*/
public final class Updater {
private static final String updaterFile = Main.getApplicationWorkDirectory() + File.separator + "Updater.jar";
private static final String updaterURL = "https://jenkins.knarcraft.net/job/KnarCraft/job/Jar-Updater/job/master/" +
"lastSuccessfulBuild/artifact/target/jar-updater-1.0-SNAPSHOT.jar";
private static final String targetFile = "minecraft-server-launcher.jar";
/**
* Checks if a newer version is available
*
* @param updateURL <p>The URL used for checking for updates</p>
* @param updateChannel <p>The release channel to use</p>
* @param gui <p>The GUI to use for output</p>
* @throws IOException <p>If the update data cannot be read</p>
*/
public static void checkForUpdate(String updateURL, String updateChannel, GUI gui) throws IOException {
public static void checkForUpdate(String updateURL, String updateChannel) throws IOException {
Scanner file = getResourceAsScanner("currentversion.csv");
if (!file.hasNextLine()) {
throw new FileNotFoundException("File currentversion.csv is invalid");
@ -47,7 +50,6 @@ public final class Updater {
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();
if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) {
String dir = getApplicationWorkDirectory() + File.separator;
JsonArray versionList = jsonObject.getAsJsonArray("versions");
String url = "";
for (JsonElement elem : versionList) {
@ -56,49 +58,35 @@ public final class Updater {
String type = obj.get("type").getAsString();
if (ver.equals(latest) && type.equals(updateChannel)) {
url = obj.get("url").getAsString();
break;
}
}
if (downloadFile(url, new File(dir + "update.jar").toPath())) {
doUpdate(dir, gui);
} else {
gui.showError("Update available",
"An update is available, but could not be downloaded.");
if (!new File(updaterFile).exists()) {
downloadFile(updaterURL, Paths.get(updaterFile));
}
int answer = JOptionPane.showConfirmDialog(null,
"An update is available. Do you want to update?", "Update available",
JOptionPane.YES_NO_OPTION
);
if (answer == JOptionPane.YES_NO_OPTION) {
runUpdater(url);
}
}
}
/**
* Asks the user whether to update and downloads the new update
* Updates the software
*
* @param dir <p>The directory to save the updated file to</p>
* @param gui <p>The GUI to write output to</p>
* @param url <p>The URL of the new file to download</p>
* @throws IOException <p>If unable to run the updater</p>
*/
public static void doUpdate(String dir, GUI gui) {
int answer = JOptionPane.showConfirmDialog(
null,
"An update is available. Do you want to update?",
"Update available",
JOptionPane.YES_NO_OPTION
);
if (answer == JOptionPane.YES_NO_OPTION) {
if (new File(dir + "Minecraft-Server-Launcher.jar").renameTo(new File(dir +
"Old.jar"))) {
if (new File(dir + "update.jar").renameTo(new File(dir +
"Minecraft-Server-Launcher.jar"))) {
if (new File(dir + "Old.jar").delete()) {
gui.showMessage("Update complete",
"Update finished. Please run the software again.");
System.exit(0);
}
} else {
if (!new File(dir + "Old.jar").renameTo(new File(dir +
"Minecraft-Server-Launcher.jar"))) {
System.out.println("Shit");
}
}
}
gui.showError("Update failed",
"Could not replace the main .jar with the downloaded .jar.");
}
private static void runUpdater(String url) throws IOException {
ProcessBuilder builder;
builder = new ProcessBuilder("java", "-jar", "Updater.jar", url, "yes", targetFile, "5", "nogui");
builder.directory(new File(Main.getApplicationWorkDirectory()));
builder.redirectErrorStream(true);
builder.start();
System.exit(1);
}
}