2020-08-11 19:29:28 +02:00
|
|
|
package net.knarcraft.minecraftserverlauncher.utility;
|
|
|
|
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.google.gson.JsonParser;
|
2020-08-29 14:03:26 +02:00
|
|
|
import net.knarcraft.minecraftserverlauncher.Main;
|
2020-08-11 19:29:28 +02:00
|
|
|
import net.knarcraft.minecraftserverlauncher.userinterface.GUI;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
2020-08-29 14:03:26 +02:00
|
|
|
import java.io.*;
|
|
|
|
import java.nio.file.Paths;
|
2020-08-11 19:29:28 +02:00
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.downloadFile;
|
|
|
|
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.getResourceAsScanner;
|
|
|
|
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.readFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A utility used for updating the software
|
|
|
|
*/
|
|
|
|
public final class Updater {
|
|
|
|
|
2020-08-29 14:03:26 +02:00
|
|
|
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";
|
|
|
|
|
2020-08-11 19:29:28 +02:00
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
* @throws IOException <p>If the update data cannot be read</p>
|
|
|
|
*/
|
2020-08-29 14:03:26 +02:00
|
|
|
public static void checkForUpdate(String updateURL, String updateChannel) throws IOException {
|
2020-08-11 19:29:28 +02:00
|
|
|
Scanner file = getResourceAsScanner("currentversion.csv");
|
|
|
|
if (!file.hasNextLine()) {
|
|
|
|
throw new FileNotFoundException("File currentversion.csv is invalid");
|
|
|
|
}
|
|
|
|
String oldType = file.nextLine();
|
|
|
|
if (!file.hasNextLine()) {
|
|
|
|
throw new FileNotFoundException("File currentversion.csv is invalid");
|
|
|
|
}
|
|
|
|
String oldVer = file.nextLine();
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
String data = readFile(updateURL);
|
|
|
|
JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
|
|
|
|
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();
|
|
|
|
|
|
|
|
if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) {
|
|
|
|
JsonArray versionList = jsonObject.getAsJsonArray("versions");
|
|
|
|
String url = "";
|
|
|
|
for (JsonElement elem : versionList) {
|
|
|
|
JsonObject obj = elem.getAsJsonObject();
|
|
|
|
String ver = obj.get("id").getAsString();
|
|
|
|
String type = obj.get("type").getAsString();
|
|
|
|
if (ver.equals(latest) && type.equals(updateChannel)) {
|
|
|
|
url = obj.get("url").getAsString();
|
2020-08-29 14:03:26 +02:00
|
|
|
break;
|
2020-08-11 19:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-29 14:03:26 +02:00
|
|
|
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);
|
2020-08-11 19:29:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-08-29 14:03:26 +02:00
|
|
|
* Updates the software
|
2020-08-11 19:29:28 +02:00
|
|
|
*
|
2020-08-29 14:03:26 +02:00
|
|
|
* @param url <p>The URL of the new file to download</p>
|
|
|
|
* @throws IOException <p>If unable to run the updater</p>
|
2020-08-11 19:29:28 +02:00
|
|
|
*/
|
2020-08-29 14:03:26 +02:00
|
|
|
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);
|
2020-08-11 19:29:28 +02:00
|
|
|
}
|
|
|
|
}
|