From 05ddd5bb6ae7e46b1387f8b1156f97341d70605e Mon Sep 17 00:00:00 2001 From: qwertysam Date: Wed, 5 Oct 2016 09:31:59 -0400 Subject: [PATCH] Can now update by using comman /update --- src/ca/afroman/client/ClientGame.java | 43 +++----------------- src/ca/afroman/gui/GuiMainMenu.java | 1 - src/ca/afroman/server/ConsoleCommand.java | 3 +- src/ca/afroman/util/CommandUtil.java | 28 ++++++++++++- src/ca/afroman/util/UpdateUtil.java | 48 +++++++++++++++++++++++ 5 files changed, 81 insertions(+), 42 deletions(-) diff --git a/src/ca/afroman/client/ClientGame.java b/src/ca/afroman/client/ClientGame.java index 0d099f2..ea140ec 100644 --- a/src/ca/afroman/client/ClientGame.java +++ b/src/ca/afroman/client/ClientGame.java @@ -117,7 +117,6 @@ public static void main(String[] args) game = new ClientGame(); game.startThis(); } - UpdateUtil.purgeOld(); } private static ThreadGroup newDefaultThreadGroupInstance() @@ -822,45 +821,11 @@ public void quit() public void quit(boolean update) { - // Stops the client properly when being closed - try - { - ClientGame.instance().stopThis(); - - Assets.dispose(); - - if (update) - { - boolean successful = UpdateUtil.update(); - - if (successful) - { - System.out.println("Successfully updated"); - // Relaunches the game - switch (UpdateUtil.runningFile) - { - case EXE:// TODO test on windows - Runtime.getRuntime().exec(UpdateUtil.selfName); - break; - case JAR: // TODO test on mac - Runtime.getRuntime().exec("java -jar " + UpdateUtil.selfName); - break; - default: - break; - } - } - else - { - System.exit(1); - } - } - System.exit(0); - } - catch (Exception er) + if (update) { - ClientGame.instance().logger().log(ALogType.CRITICAL, "Failed to exit the program properly", er); - System.exit(2); + UpdateUtil.applyUpdate(); } + System.exit(0); } @Override @@ -1246,6 +1211,8 @@ public void stopThis() setCurrentScreen(null); if (this.isHostingServer()) ServerGame.instance().stopThis(); + + Assets.dispose(); } @Override diff --git a/src/ca/afroman/gui/GuiMainMenu.java b/src/ca/afroman/gui/GuiMainMenu.java index aff1846..6a6fd07 100644 --- a/src/ca/afroman/gui/GuiMainMenu.java +++ b/src/ca/afroman/gui/GuiMainMenu.java @@ -81,7 +81,6 @@ public void releaseAction(int buttonID) ClientGame.instance().quit(); break; case 3: // Check for updates - if (UpdateUtil.updateQuery()) { new GuiYesNoPrompt(this, 30, "Update found (" + VersionUtil.toString(UpdateUtil.serverVersion) + ")", "Would you like to update?"); diff --git a/src/ca/afroman/server/ConsoleCommand.java b/src/ca/afroman/server/ConsoleCommand.java index ab117c2..d9c9dfb 100644 --- a/src/ca/afroman/server/ConsoleCommand.java +++ b/src/ca/afroman/server/ConsoleCommand.java @@ -7,7 +7,8 @@ public enum ConsoleCommand DESTROY("Resets the game", "Resets the game to the very beginning, deleting any previous save file.", "destroy"), REBOOT("Reboots the server", "Stops the server and starts it back up immediately", "reboot"), HELP("Displays commands", "Displays a list of commands which can be issued to control the server.", "help [command]", "help save - Displays help information about the save command."), - TP("Teleports player", "Teleports a player to a location. Optionally, can be teleported into a different level.", "tp [levelNum]", "tp 0 512 -32 0 - Teleports player 1 to (512, -32) in level 1.", "tp 1 2 444 4 - Teleports player 2 to (2, 444) in level 5.", "tp 1 1 1 - Teleports player 2 to (1, 1) in their current level."); + TP("Teleports player", "Teleports a player to a location. Optionally, can be teleported into a different level.", "tp [levelNum]", "tp 0 512 -32 0 - Teleports player 1 to (512, -32) in level 1.", "tp 1 2 444 4 - Teleports player 2 to (2, 444) in level 5.", "tp 1 1 1 - Teleports player 2 to (1, 1) in their current level."), + UPDATE("Updates Afro Man", "Downloads and replaces the currently running file with the latest version of Afro Man", "update"); private String sh; private String full; diff --git a/src/ca/afroman/util/CommandUtil.java b/src/ca/afroman/util/CommandUtil.java index 752b1c6..cd19cb7 100644 --- a/src/ca/afroman/util/CommandUtil.java +++ b/src/ca/afroman/util/CommandUtil.java @@ -8,6 +8,8 @@ public class CommandUtil { + private static boolean wentThroughUpdate = false; + private static void displayHelp(ConsoleCommand command) { ALogger.logA("-----------------------------"); @@ -55,6 +57,13 @@ public static void issueCommand(String input) { try { + boolean isCommandLine = false; + + if (ServerGame.instance() != null) + { + isCommandLine = ServerGame.instance().isCommandLine(); + } + switch (command) { default: @@ -74,8 +83,6 @@ public static void issueCommand(String input) case REBOOT: if (ServerGame.instance() != null) { - boolean isCommandLine = ServerGame.instance().isCommandLine(); - if (isCommandLine) { ALogger.logA(ALogType.DEBUG, "Rebooting server..."); @@ -130,6 +137,23 @@ public static void issueCommand(String input) ALogger.logA(""); break; + case UPDATE: + if (wentThroughUpdate) + { + UpdateUtil.applyUpdate(isCommandLine); + } + + if (UpdateUtil.updateQuery()) + { + ALogger.logA("Update found (" + VersionUtil.toString(UpdateUtil.serverVersion) + ")"); + ALogger.logA("Type \"update\" again to update"); + wentThroughUpdate = true; + } + else + { + ALogger.logA("No updates found"); + } + break; } } catch (Exception e) diff --git a/src/ca/afroman/util/UpdateUtil.java b/src/ca/afroman/util/UpdateUtil.java index 1f3cf11..f2470b1 100644 --- a/src/ca/afroman/util/UpdateUtil.java +++ b/src/ca/afroman/util/UpdateUtil.java @@ -31,6 +31,54 @@ public class UpdateUtil public static String selfName; public static FileType runningFile = FileType.INVALID; + public static void applyUpdate() + { + applyUpdate(false); + } + + public static void applyUpdate(boolean commandLine) + { + try + { + // Stops the client properly when being closed + boolean successful = UpdateUtil.update(); + + UpdateUtil.purgeOld(); + + if (successful) + { + System.out.println("Successfully updated"); + // Relaunches the game + if (!commandLine) + { + switch (UpdateUtil.runningFile) + { + case EXE:// TODO test on windows + Runtime.getRuntime().exec(UpdateUtil.selfName); + break; + case JAR: // TODO test on mac + Runtime.getRuntime().exec("java -jar " + UpdateUtil.selfName); + break; + default: + break; + } + } + } + else + { + System.exit(1); + } + + System.exit(0); + + } + catch (Exception e) + { + System.out.println("Failed to update"); + System.exit(2); + } + } + /** * Downloads a file from a URL. *