Fix version check

This commit is contained in:
CovertJaguar 2013-09-27 22:13:13 -07:00
parent 1a639f7afb
commit b6cb556274

View file

@ -11,23 +11,20 @@ import net.minecraftforge.common.Property;
public class Version implements Runnable {
private static Version instance = new Version();
private static Version instance = new Version();
public enum EnumUpdateState {
CURRENT, OUTDATED, CONNECTION_ERROR
}
public static final String VERSION = "@VERSION@";
public static final String BUILD_NUMBER = "@BUILD_NUMBER@";
private static final String REMOTE_VERSION_FILE = "http://bit.ly/buildcraftver";
private static final String REMOTE_CHANGELOG_ROOT = "https://dl.dropbox.com/u/44760587/buildcraft/changelog/";
public static EnumUpdateState currentVersion = EnumUpdateState.CURRENT;
public static final int FORGE_VERSION_MAJOR = 4;
public static final int FORGE_VERSION_MINOR = 0;
public static final int FORGE_VERSION_PATCH = 0;
private static String recommendedVersion;
private static String[] cachedChangelog;
@ -62,30 +59,30 @@ public class Version implements Runnable {
public static void versionCheck() {
try {
if ("0.0.0".equals(VERSION)) return;
if ("0.0.0".equals(VERSION))
return;
String location = REMOTE_VERSION_FILE;
HttpURLConnection conn = null;
while (location != null && !location.isEmpty()) {
URL url = new URL(location);
if(conn != null)
if (conn != null)
conn.disconnect();
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)");
conn.connect();
location = conn.getHeaderField("Location");
}
if(conn == null)
if (conn == null)
throw new NullPointerException();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
conn.disconnect();
String line = null;
String line;
String mcVersion = CoreProxy.proxy.getMinecraftVersion();
while ((line = reader.readLine()) != null) {
if (line.startsWith(mcVersion)) {
@ -95,7 +92,7 @@ public class Version implements Runnable {
recommendedVersion = tokens[2];
if (line.endsWith(VERSION)) {
BuildCraftCore.bcLog.finer("Using the latest version [" + getVersion() + "] for Minecraft " + mcVersion);
BuildCraftCore.bcLog.finer("Using the latest version [" + getVersion() + "] for Minecraft " + mcVersion);
currentVersion = EnumUpdateState.CURRENT;
return;
}
@ -107,6 +104,8 @@ public class Version implements Runnable {
+ ". Consider updating.");
currentVersion = EnumUpdateState.OUTDATED;
conn.disconnect();
reader.close();
} catch (Exception e) {
BuildCraftCore.bcLog.warning("Unable to read from remote version authority.");
BuildCraftCore.bcLog.warning(e.toString());
@ -130,20 +129,20 @@ public class Version implements Runnable {
HttpURLConnection conn = null;
while (location != null && !location.isEmpty()) {
URL url = new URL(location);
if(conn != null)
if (conn != null)
conn.disconnect();
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)");
conn.connect();
location = conn.getHeaderField("Location");
}
if(conn == null)
if (conn == null)
throw new NullPointerException();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
conn.disconnect();
@ -167,41 +166,39 @@ public class Version implements Runnable {
BuildCraftCore.bcLog.warning("Unable to read changelog from remote site.");
}
return new String[] { String.format("Unable to retrieve changelog for %s %s", DefaultProps.MOD, version) };
return new String[]{String.format("Unable to retrieve changelog for %s %s", DefaultProps.MOD, version)};
}
@Override
public void run() {
@Override
public void run() {
int count = 0;
currentVersion = null;
int count = 0;
currentVersion = null;
BuildCraftCore.bcLog.info("Beginning version check");
BuildCraftCore.bcLog.info("Beginning version check");
try {
while ((count < 3) && ((currentVersion == null) || (currentVersion == EnumUpdateState.CONNECTION_ERROR))) {
versionCheck();
count++;
try {
while ((count < 3) && ((currentVersion == null) || (currentVersion == EnumUpdateState.CONNECTION_ERROR))) {
versionCheck();
count++;
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
BuildCraftCore.bcLog.info("Version check attempt " + count + " failed, trying again in 10 seconds");
Thread.sleep(10000);
}
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
BuildCraftCore.bcLog.info("Version check attempt " + count + " failed, trying again in 10 seconds");
Thread.sleep(10000);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
BuildCraftCore.bcLog.info("Version check failed");
}
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
BuildCraftCore.bcLog.info("Version check failed");
}
}
}
public static void check() {
new Thread(instance).start();
}
public static void check() {
new Thread(instance).start();
}
}