diff --git a/pom.xml b/pom.xml index 5d96e1f..570f293 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.parabot client - 2.4.1 + 2.4.1.1 jar diff --git a/src/main/java/org/parabot/Landing.java b/src/main/java/org/parabot/Landing.java index 8f47b43..ed931a7 100644 --- a/src/main/java/org/parabot/Landing.java +++ b/src/main/java/org/parabot/Landing.java @@ -45,7 +45,7 @@ public final class Landing { "Please download the newest version of Parabot at " + Configuration.DOWNLOAD_BOT, JOptionPane.INFORMATION_MESSAGE); - URI uri = URI.create(Configuration.DOWNLOAD_BOT); + URI uri = URI.create(Configuration.API_DOWNLOAD_BOT); try { Desktop.getDesktop().browse(uri); } catch (IOException e1) { diff --git a/src/main/java/org/parabot/core/Configuration.java b/src/main/java/org/parabot/core/Configuration.java index e9232eb..4754b11 100644 --- a/src/main/java/org/parabot/core/Configuration.java +++ b/src/main/java/org/parabot/core/Configuration.java @@ -1,5 +1,7 @@ package org.parabot.core; +import org.parabot.environment.api.utils.Version; + /** * Holds some important constants * @@ -14,11 +16,12 @@ public class Configuration { public static final String GET_SERVER_PROVIDER = "http://bdn.parabot.org/api/get.php?action=server_provider&name="; public static final String GET_SERVER_PROVIDER_INFO = "http://bdn.parabot.org/api/get.php?action=server_information&name="; public static final String GET_BOT_VERSION = "http://bdn.parabot.org/api/v2/bot/version"; - public static final String DOWNLOAD_BOT = "http://bdn.parabot.org/api/v2/bot/download/client/"; + public static final String API_DOWNLOAD_BOT = "http://bdn.parabot.org/api/v2/bot/download/client/"; + public static final String DOWNLOAD_BOT = "http://bdn.parabot.org/versions/"; public static final String REGISTRATION_PAGE = "https://www.parabot.org/community/register/"; public static final String GET_RANDOMS = "http://bdn.parabot.org/api/get.php?action=randoms"; public static final String DATA_API = "http://bdn.parabot.org/api/v2/data/"; public static final String ITEM_API = DATA_API + "items/"; - public static final String BOT_VERSION = (String) ProjectProperties.getProjectVersion(); + public static final Version BOT_VERSION = ProjectProperties.getProjectVersion(); } diff --git a/src/main/java/org/parabot/core/Core.java b/src/main/java/org/parabot/core/Core.java index 8e2e683..bb39d45 100644 --- a/src/main/java/org/parabot/core/Core.java +++ b/src/main/java/org/parabot/core/Core.java @@ -4,6 +4,7 @@ import org.json.simple.JSONObject; import org.json.simple.parser.ParseException; import org.parabot.Landing; import org.parabot.core.ui.utils.UILog; +import org.parabot.environment.api.utils.Version; import org.parabot.environment.api.utils.WebUtil; import java.io.BufferedReader; @@ -21,6 +22,7 @@ import java.security.NoSuchAlgorithmException; * * @author Everel, JKetelaar */ +@SuppressWarnings("Duplicates") public class Core { public static boolean mDebug; private static boolean debug; @@ -31,6 +33,9 @@ public class Core { private static boolean validate = true; private static boolean secure = true; + private static Version currentVersion = Configuration.BOT_VERSION; + private static Version latestVersion; + public static void disableValidation() { Core.validate = false; } @@ -168,6 +173,8 @@ public class Core { } /** + * @Deprecated use #validVersion instead + * * Checks the version of the bot using a variable comparison from the bot code and the Parabot website * @return true if no new version is found, otherwise false. */ @@ -201,6 +208,36 @@ public class Core { return true; } + public static boolean validVersion() { + BufferedReader br = WebUtil.getReader(Configuration.GET_BOT_VERSION); + try { + latestVersion = null; + if (br != null) { + JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(br); + latestVersion = new Version((String) object.get("result")); + } + if (latestVersion != null) { + if (Configuration.BOT_VERSION.compareTo(latestVersion) < 0) { + Core.verbose("Our version: " + Configuration.BOT_VERSION.get()); + Core.verbose("Latest version: " + latestVersion.get()); + return false; + } + } + } catch (NumberFormatException | IOException | ParseException e) { + e.printStackTrace(); + } finally { + try { + if (br != null) { + br.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + return true; + } + private static void validateCache(){ File[] cache = Directories.getCachePath().listFiles(); Integer lowest = null; @@ -235,7 +272,7 @@ public class Core { Core.verbose("Checking for updates..."); validateCache(); - if (versionValid() && checksumValid()){ + if ((validVersion() && checksumValid()) || (!checksumValid() && currentVersion.compareTo(latestVersion) >= 0)){ Core.verbose("No updates available."); return true; }else{ diff --git a/src/main/java/org/parabot/core/ProjectProperties.java b/src/main/java/org/parabot/core/ProjectProperties.java index ba5e22f..4ed1c36 100644 --- a/src/main/java/org/parabot/core/ProjectProperties.java +++ b/src/main/java/org/parabot/core/ProjectProperties.java @@ -1,5 +1,7 @@ package org.parabot.core; +import org.parabot.environment.api.utils.Version; + import java.io.IOException; import java.io.InputStream; import java.util.Properties; @@ -34,9 +36,8 @@ public class ProjectProperties { return cached; } - public static Object getProjectVersion(){ - System.out.println(getInstance().getCached().getProperty("application.version")); - return getInstance().getCached().getProperty("application.version"); + public static Version getProjectVersion(){ + return new Version(getInstance().getCached().getProperty("application.version")); } public static ProjectProperties getInstance(){ diff --git a/src/main/java/org/parabot/environment/api/utils/Version.java b/src/main/java/org/parabot/environment/api/utils/Version.java new file mode 100644 index 0000000..d90026f --- /dev/null +++ b/src/main/java/org/parabot/environment/api/utils/Version.java @@ -0,0 +1,47 @@ +package org.parabot.environment.api.utils; + +public class Version implements Comparable { + + private String version; + + public final String get() { + return this.version; + } + + public Version(String version) { + if(version == null) { + throw new IllegalArgumentException("Version can not be null"); + } + if(!version.matches("[0-9]+(\\.[0-9]+)*")) { + throw new IllegalArgumentException("Invalid version format"); + } + this.version = version; + } + + @Override public int compareTo(Version that) { + if(that == null) { + return 1; + } + String[] thisParts = this.get().split("\\."); + String[] thatParts = that.get().split("\\."); + int length = Math.max(thisParts.length, thatParts.length); + for(int i = 0; i < length; i++) { + int thisPart = i < thisParts.length ? + Integer.parseInt(thisParts[i]) : 0; + int thatPart = i < thatParts.length ? + Integer.parseInt(thatParts[i]) : 0; + if(thisPart < thatPart) { + return -1; + } + if(thisPart > thatPart) { + return 1; + } + } + return 0; + } + + @Override public boolean equals(Object that) { + return this == that || that != null && this.getClass() == that.getClass() && this.compareTo((Version) that) == 0; + } + +} \ No newline at end of file