PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/common/buildcraft/core/Version.java

https://github.com/MatthewSleeman/BuildCraft
Java | 79 lines | 59 code | 20 blank | 0 comment | 7 complexity | 1f5d6337b5a07205ede684bc555dbe70 MD5 | raw file
Possible License(s): LGPL-2.0
  1. package buildcraft.core;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6. import buildcraft.core.proxy.CoreProxy;
  7. import cpw.mods.fml.common.FMLLog;
  8. public class Version {
  9. public enum EnumUpdateState { CURRENT, OUTDATED, CONNECTION_ERROR }
  10. public static final String VERSION = "@VERSION@";
  11. public static final String BUILD_NUMBER = "@BUILD_NUMBER@";
  12. private static final String REMOTE_VERSION_FILE = "http://bit.ly/buildcraftver";
  13. public static EnumUpdateState currentVersion = EnumUpdateState.CURRENT;
  14. public static final int FORGE_VERSION_MAJOR = 4;
  15. public static final int FORGE_VERSION_MINOR = 0;
  16. public static final int FORGE_VERSION_PATCH = 0;
  17. private static String recommendedVersion;
  18. public static String getVersion() {
  19. return VERSION + " (:"+ BUILD_NUMBER +")";
  20. }
  21. public static String getRecommendedVersion() {
  22. return recommendedVersion;
  23. }
  24. public static void versionCheck() {
  25. try {
  26. String location = REMOTE_VERSION_FILE;
  27. HttpURLConnection conn = null;
  28. while(location != null && !location.isEmpty()) {
  29. URL url = new URL(location);
  30. conn = (HttpURLConnection)url.openConnection();
  31. 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)");
  32. conn.connect();
  33. location = conn.getHeaderField("Location");
  34. }
  35. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  36. String line = null;
  37. String mcVersion = CoreProxy.proxy.getMinecraftVersion();
  38. while ((line = reader.readLine()) != null) {
  39. if (line.startsWith(mcVersion)) {
  40. if (line.contains(DefaultProps.MOD)) {
  41. String[] tokens = line.split(":");
  42. recommendedVersion = tokens[2];
  43. if (line.endsWith(VERSION)) {
  44. FMLLog.finer(DefaultProps.MOD + ": Using the latest version ["+ getVersion() +"] for Minecraft " + mcVersion);
  45. currentVersion = EnumUpdateState.CURRENT;
  46. return;
  47. }
  48. }
  49. }
  50. }
  51. FMLLog.warning(DefaultProps.MOD + ": Using outdated version ["+ VERSION +" (build:"+ BUILD_NUMBER +")] for Minecraft " + mcVersion + ". Consider updating.");
  52. currentVersion = EnumUpdateState.OUTDATED;
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. FMLLog.warning(DefaultProps.MOD + ": Unable to read from remote version authority.");
  56. currentVersion = EnumUpdateState.CONNECTION_ERROR;
  57. }
  58. }
  59. }