PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/common/buildcraft/core/Version.java

https://github.com/Slutnick/BuildCraft
Java | 190 lines | 145 code | 45 blank | 0 comment | 31 complexity | 409d82c5f598d4186aa47901b7de1075 MD5 | raw file
  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 java.util.ArrayList;
  7. import net.minecraftforge.common.Property;
  8. import buildcraft.BuildCraftCore;
  9. import buildcraft.core.proxy.CoreProxy;
  10. public class Version implements Runnable {
  11. private static Version instance = new Version();
  12. public enum EnumUpdateState {
  13. CURRENT, OUTDATED, CONNECTION_ERROR
  14. }
  15. public static final String VERSION = "@VERSION@";
  16. public static final String BUILD_NUMBER = "@BUILD_NUMBER@";
  17. private static final String REMOTE_VERSION_FILE = "http://bit.ly/buildcraftver";
  18. private static final String REMOTE_CHANGELOG_ROOT = "https://dl.dropbox.com/u/44760587/buildcraft/changelog/";
  19. public static EnumUpdateState currentVersion = EnumUpdateState.CURRENT;
  20. public static final int FORGE_VERSION_MAJOR = 4;
  21. public static final int FORGE_VERSION_MINOR = 0;
  22. public static final int FORGE_VERSION_PATCH = 0;
  23. private static String recommendedVersion;
  24. private static String[] cachedChangelog;
  25. public static String getVersion() {
  26. return VERSION + " (:" + BUILD_NUMBER + ")";
  27. }
  28. public static boolean isOutdated() {
  29. return currentVersion == EnumUpdateState.OUTDATED;
  30. }
  31. public static boolean needsUpdateNoticeAndMarkAsSeen() {
  32. if (!isOutdated())
  33. return false;
  34. Property property = BuildCraftCore.mainConfiguration.get("vars", "version.seen", VERSION);
  35. property.comment = "indicates the last version the user has been informed about and will suppress further notices on it.";
  36. String seenVersion = property.value;
  37. if (recommendedVersion == null || recommendedVersion.equals(seenVersion))
  38. return false;
  39. property.value = recommendedVersion;
  40. BuildCraftCore.mainConfiguration.save();
  41. return true;
  42. }
  43. public static String getRecommendedVersion() {
  44. return recommendedVersion;
  45. }
  46. public static void versionCheck() {
  47. try {
  48. String location = REMOTE_VERSION_FILE;
  49. HttpURLConnection conn = null;
  50. while (location != null && !location.isEmpty()) {
  51. URL url = new URL(location);
  52. conn = (HttpURLConnection) url.openConnection();
  53. conn.setRequestProperty("User-Agent",
  54. "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)");
  55. conn.connect();
  56. location = conn.getHeaderField("Location");
  57. }
  58. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  59. String line = null;
  60. String mcVersion = CoreProxy.proxy.getMinecraftVersion();
  61. while ((line = reader.readLine()) != null) {
  62. if (line.startsWith(mcVersion)) {
  63. if (line.contains(DefaultProps.MOD)) {
  64. String[] tokens = line.split(":");
  65. recommendedVersion = tokens[2];
  66. if (line.endsWith(VERSION)) {
  67. BuildCraftCore.bcLog.finer("Using the latest version [" + getVersion() + "] for Minecraft " + mcVersion);
  68. currentVersion = EnumUpdateState.CURRENT;
  69. return;
  70. }
  71. }
  72. }
  73. }
  74. BuildCraftCore.bcLog.warning("Using outdated version [" + VERSION + " (build:" + BUILD_NUMBER + ")] for Minecraft " + mcVersion
  75. + ". Consider updating.");
  76. currentVersion = EnumUpdateState.OUTDATED;
  77. } catch (Exception e) {
  78. BuildCraftCore.bcLog.warning("Unable to read from remote version authority.");
  79. BuildCraftCore.bcLog.warning(e.toString());
  80. currentVersion = EnumUpdateState.CONNECTION_ERROR;
  81. }
  82. }
  83. public static String[] getChangelog() {
  84. if (cachedChangelog == null) {
  85. cachedChangelog = grabChangelog(recommendedVersion);
  86. }
  87. return cachedChangelog;
  88. }
  89. public static String[] grabChangelog(String version) {
  90. try {
  91. String location = REMOTE_CHANGELOG_ROOT + version;
  92. HttpURLConnection conn = null;
  93. while (location != null && !location.isEmpty()) {
  94. URL url = new URL(location);
  95. conn = (HttpURLConnection) url.openConnection();
  96. conn.setRequestProperty("User-Agent",
  97. "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)");
  98. conn.connect();
  99. location = conn.getHeaderField("Location");
  100. }
  101. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  102. String line = null;
  103. ArrayList<String> changelog = new ArrayList<String>();
  104. while ((line = reader.readLine()) != null) {
  105. if (line.startsWith("#")) {
  106. continue;
  107. }
  108. if (line.isEmpty()) {
  109. continue;
  110. }
  111. changelog.add(line);
  112. }
  113. return changelog.toArray(new String[0]);
  114. } catch (Exception ex) {
  115. ex.printStackTrace();
  116. BuildCraftCore.bcLog.warning("Unable to read changelog from remote site.");
  117. }
  118. return new String[] { String.format("Unable to retrieve changelog for %s %s", DefaultProps.MOD, version) };
  119. }
  120. @Override
  121. public void run() {
  122. int count = 0;
  123. currentVersion = null;
  124. BuildCraftCore.bcLog.info("Beginning version check");
  125. try {
  126. while ((count < 3) && ((currentVersion == null) || (currentVersion == EnumUpdateState.CONNECTION_ERROR))) {
  127. versionCheck();
  128. count++;
  129. if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
  130. BuildCraftCore.bcLog.info("Version check attempt " + count + " failed, trying again in 10 seconds");
  131. Thread.sleep(10000);
  132. }
  133. }
  134. }
  135. catch (InterruptedException e) {
  136. e.printStackTrace();
  137. }
  138. if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
  139. BuildCraftCore.bcLog.info("Version check failed");
  140. }
  141. }
  142. public static void check() {
  143. new Thread(instance).start();
  144. }
  145. }