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.userinterface.GUI; import javax.swing.*; import java.io.*; 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.readFile; /** * 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
The URL used for checking for updates
* @param updateChannelThe release channel to use
* @throws IOExceptionIf the update data cannot be read
*/ 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 = 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(); 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); } } } /** * Updates the software * * @param urlThe URL of the new file to download
* @throws IOExceptionIf unable to run the updater
*/ 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); } }