105 lines
4.2 KiB
Java
Raw Normal View History

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.userinterface.GUI;
import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
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;
/**
* A utility used for updating the software
*/
public final class Updater {
/**
* 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 {
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)) {
String dir = getApplicationWorkDirectory() + File.separator;
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();
}
}
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.");
}
}
}
/**
* Asks the user whether to update and downloads the new update
*
* @param dir <p>The directory to save the updated file to</p>
* @param gui <p>The GUI to write output to</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.");
}
}
}