[FEATURE] Added new version comparison system

This commit is contained in:
JKetelaar
2015-12-26 17:05:25 +01:00
parent 91a855df53
commit 3bb1916752
6 changed files with 96 additions and 8 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>org.parabot</groupId> <groupId>org.parabot</groupId>
<artifactId>client</artifactId> <artifactId>client</artifactId>
<version>2.4.1</version> <version>2.4.1.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
+1 -1
View File
@@ -45,7 +45,7 @@ public final class Landing {
"Please download the newest version of Parabot at " "Please download the newest version of Parabot at "
+ Configuration.DOWNLOAD_BOT, + Configuration.DOWNLOAD_BOT,
JOptionPane.INFORMATION_MESSAGE); JOptionPane.INFORMATION_MESSAGE);
URI uri = URI.create(Configuration.DOWNLOAD_BOT); URI uri = URI.create(Configuration.API_DOWNLOAD_BOT);
try { try {
Desktop.getDesktop().browse(uri); Desktop.getDesktop().browse(uri);
} catch (IOException e1) { } catch (IOException e1) {
@@ -1,5 +1,7 @@
package org.parabot.core; package org.parabot.core;
import org.parabot.environment.api.utils.Version;
/** /**
* Holds some important constants * 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 = "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_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 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 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 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 DATA_API = "http://bdn.parabot.org/api/v2/data/";
public static final String ITEM_API = DATA_API + "items/"; 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();
} }
+38 -1
View File
@@ -4,6 +4,7 @@ import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import org.parabot.Landing; import org.parabot.Landing;
import org.parabot.core.ui.utils.UILog; import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.Version;
import org.parabot.environment.api.utils.WebUtil; import org.parabot.environment.api.utils.WebUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
@@ -21,6 +22,7 @@ import java.security.NoSuchAlgorithmException;
* *
* @author Everel, JKetelaar * @author Everel, JKetelaar
*/ */
@SuppressWarnings("Duplicates")
public class Core { public class Core {
public static boolean mDebug; public static boolean mDebug;
private static boolean debug; private static boolean debug;
@@ -31,6 +33,9 @@ public class Core {
private static boolean validate = true; private static boolean validate = true;
private static boolean secure = true; private static boolean secure = true;
private static Version currentVersion = Configuration.BOT_VERSION;
private static Version latestVersion;
public static void disableValidation() { public static void disableValidation() {
Core.validate = false; 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 * Checks the version of the bot using a variable comparison from the bot code and the Parabot website
* @return <b>true</b> if no new version is found, otherwise <b>false</b>. * @return <b>true</b> if no new version is found, otherwise <b>false</b>.
*/ */
@@ -201,6 +208,36 @@ public class Core {
return true; 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(){ private static void validateCache(){
File[] cache = Directories.getCachePath().listFiles(); File[] cache = Directories.getCachePath().listFiles();
Integer lowest = null; Integer lowest = null;
@@ -235,7 +272,7 @@ public class Core {
Core.verbose("Checking for updates..."); Core.verbose("Checking for updates...");
validateCache(); validateCache();
if (versionValid() && checksumValid()){ if ((validVersion() && checksumValid()) || (!checksumValid() && currentVersion.compareTo(latestVersion) >= 0)){
Core.verbose("No updates available."); Core.verbose("No updates available.");
return true; return true;
}else{ }else{
@@ -1,5 +1,7 @@
package org.parabot.core; package org.parabot.core;
import org.parabot.environment.api.utils.Version;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
@@ -34,9 +36,8 @@ public class ProjectProperties {
return cached; return cached;
} }
public static Object getProjectVersion(){ public static Version getProjectVersion(){
System.out.println(getInstance().getCached().getProperty("application.version")); return new Version(getInstance().getCached().getProperty("application.version"));
return getInstance().getCached().getProperty("application.version");
} }
public static ProjectProperties getInstance(){ public static ProjectProperties getInstance(){
@@ -0,0 +1,47 @@
package org.parabot.environment.api.utils;
public class Version implements Comparable<Version> {
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;
}
}