Skip to content

Commit

Permalink
Can now update by using comman /update
Browse files Browse the repository at this point in the history
  • Loading branch information
defaultsamson committed Oct 5, 2016
1 parent 89a094e commit 05ddd5b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 42 deletions.
43 changes: 5 additions & 38 deletions src/ca/afroman/client/ClientGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ public static void main(String[] args)
game = new ClientGame();
game.startThis();
}
UpdateUtil.purgeOld();
}

private static ThreadGroup newDefaultThreadGroupInstance()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1246,6 +1211,8 @@ public void stopThis()
setCurrentScreen(null);

if (this.isHostingServer()) ServerGame.instance().stopThis();

Assets.dispose();
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/ca/afroman/gui/GuiMainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -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?");
Expand Down
3 changes: 2 additions & 1 deletion src/ca/afroman/server/ConsoleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <playerNum> <x> <y> [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 <playerNum> <x> <y> [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;
Expand Down
28 changes: 26 additions & 2 deletions src/ca/afroman/util/CommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class CommandUtil
{
private static boolean wentThroughUpdate = false;

private static void displayHelp(ConsoleCommand command)
{
ALogger.logA("-----------------------------");
Expand Down Expand Up @@ -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:
Expand All @@ -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...");
Expand Down Expand Up @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions src/ca/afroman/util/UpdateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 05ddd5b

Please sign in to comment.