EpicKnarvik97 85febdf41b
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
Improves version checking to prevent and update notice when running an unreleased version
2021-08-03 11:50:32 +02:00

121 lines
5.1 KiB
Java

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;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Paths;
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.readRemoteFile;
/**
* A utility used for updating the software
*/
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>
* @throws IOException <p>If the update data cannot be read</p>
*/
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");
}
String oldType = file.nextLine();
if (!file.hasNextLine()) {
throw new FileNotFoundException("File currentversion.csv is invalid");
}
String oldVer = file.nextLine();
file.close();
String data = readRemoteFile(updateURL);
JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject();
String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString();
if (!oldType.equals(updateChannel) || isVersionHigher(oldVer, 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();
break;
}
}
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);
}
}
}
/**
* Decides whether one version number is higher than another
*
* @param oldVersion <p>The old version to check</p>
* @param newVersion <p>The new version to check</p>
* @return <p>True if the new version is higher than the old one</p>
*/
public static boolean isVersionHigher(String oldVersion, String newVersion) {
String[] oldVersionParts = oldVersion.split("\\.");
String[] newVersionParts = newVersion.split("\\.");
int[] oldVersionInts = new int[]{Integer.parseInt(oldVersionParts[0]), Integer.parseInt(oldVersionParts[1]),
Integer.parseInt(oldVersionParts[2])};
int[] newVersionInts = new int[]{Integer.parseInt(newVersionParts[0]), Integer.parseInt(newVersionParts[1]),
Integer.parseInt(newVersionParts[2])};
if (newVersionInts[0] > oldVersionInts[0]) {
return true;
} else if (newVersionInts[0] == oldVersionInts[0]) {
if (newVersionInts[1] > oldVersionInts[1]) {
return true;
}
return newVersionInts[1] == oldVersionInts[1] && newVersionInts[2] > oldVersionInts[2];
}
return false;
}
/**
* Updates the software
*
* @param url <p>The URL of the new file to download</p>
* @throws IOException <p>If unable to run the updater</p>
*/
private static void runUpdater(String url) throws IOException {
String javaCommand = ServerLauncherController.getInstance().getJavaCommand();
ProcessBuilder builder;
builder = new ProcessBuilder(javaCommand, "-jar", "Updater.jar", url, "yes", targetFile, "5", "nogui");
builder.directory(new File(Main.getApplicationWorkDirectory()));
builder.redirectErrorStream(true);
builder.start();
System.exit(1);
}
}