PageRenderTime 43ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/forestry_common/core/forestry/core/config/Version.java

https://github.com/Shamboozle/ForestryMC
Java | 208 lines | 153 code | 40 blank | 15 comment | 27 complexity | 65abdf3a4457fd926fdc70ee7c65ea2f MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright 2011-2014 by SirSengir
  3. *
  4. * This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
  5. *
  6. * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/3.0/.
  7. ******************************************************************************/
  8. package forestry.core.config;
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. import java.util.ArrayList;
  14. import cpw.mods.fml.common.FMLCommonHandler;
  15. import cpw.mods.fml.common.versioning.DefaultArtifactVersion;
  16. import forestry.Forestry;
  17. import forestry.core.proxy.Proxies;
  18. /**
  19. * With permission from pahimar.
  20. *
  21. * @author Pahimar
  22. *
  23. */
  24. public class Version {
  25. public enum EnumUpdateState {
  26. CURRENT, OUTDATED, CONNECTION_ERROR
  27. }
  28. public static final String VERSION = "@VERSION@";
  29. public static final String BUILD_NUMBER = "@BUILD_NUMBER@";
  30. public static final String[] FAILED_CHANGELOG = new String[] { String.format("Unable to retrieve changelog for %s", Defaults.MOD) };
  31. public static final String FINGERPRINT = "@FINGERPRINT@";
  32. public static final String FORGEPRINT = "de4cf8a3f3bc15635810044c39240bf96804ea7d";
  33. private static final String REMOTE_VERSION_FILE = "http://bit.ly/forestryver";
  34. private static final String REMOTE_CHANGELOG_ROOT = "https://dl.dropbox.com/u/44760587/forestry/changelog/";
  35. private static final String REMOTE_FINGERPRINT_ROOT = new String(new byte[] { 104, 116, 116, 112, 115, 58, 47, 47, 100, 108, 46, 100, 114, 111, 112, 98,
  36. 111, 120, 46, 99, 111, 109, 47, 117, 47, 52, 52, 55, 54, 48, 53, 56, 55, 47, 102, 111, 114, 101, 115, 116, 114, 121, 47, 102, 105, 110, 103, 101,
  37. 114, 112, 114, 105, 110, 116, 115, 47 });
  38. public static EnumUpdateState currentVersion = EnumUpdateState.CURRENT;
  39. public static final String FORGE_VERSION = "@FORGE_VERSION@";
  40. public static String remoteFingerprint;
  41. public static String remoteForgeprint;
  42. private static String recommendedVersion;
  43. private static String[] cachedChangelog = FAILED_CHANGELOG;
  44. public static String getVersion() {
  45. return VERSION + " (" + BUILD_NUMBER + ")";
  46. }
  47. public static boolean isOutdated() {
  48. return currentVersion == EnumUpdateState.OUTDATED;
  49. }
  50. public static boolean needsUpdateNoticeAndMarkAsSeen() {
  51. if (!isOutdated())
  52. return false;
  53. Property property = Config.config.get("vars.version.seen", Config.CATEGORY_COMMON, VERSION);
  54. property.Comment = "indicates the last version the user has been informed about and will suppress further notices on it.";
  55. String seenVersion = property.Value;
  56. if (recommendedVersion == null || recommendedVersion.equals(seenVersion))
  57. return false;
  58. property.Value = recommendedVersion;
  59. Config.config.save();
  60. return true;
  61. }
  62. public static String getRecommendedVersion() {
  63. return recommendedVersion;
  64. }
  65. public static void updateRemoteFingerprints() {
  66. remoteFingerprint = retrieveRemoteString(REMOTE_FINGERPRINT_ROOT + new String(new byte[] { 102, 111, 114, 101, 115, 116, 114, 121, 46, 107, 101, 121 })
  67. + "1", FINGERPRINT);
  68. remoteFingerprint = retrieveRemoteString(REMOTE_FINGERPRINT_ROOT + new String(new byte[] { 102, 111, 114, 103, 101, 46, 107, 101, 121 }) + "1",
  69. FORGEPRINT);
  70. }
  71. public static String retrieveRemoteString(String location, String defaultValue) {
  72. try {
  73. HttpURLConnection conn = null;
  74. while (location != null && !location.isEmpty()) {
  75. URL url = new URL(location);
  76. conn = (HttpURLConnection) url.openConnection();
  77. conn.setRequestProperty("User-Agent",
  78. "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)");
  79. conn.connect();
  80. location = conn.getHeaderField("Location");
  81. }
  82. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  83. String line = null;
  84. if ((line = reader.readLine()) != null)
  85. return line;
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. Proxies.log.warning("Unable to read from remote fingerprint authority.");
  89. }
  90. return defaultValue;
  91. }
  92. private static class VersionChecker extends Thread {
  93. @Override
  94. public void run() {
  95. try {
  96. String location = REMOTE_VERSION_FILE;
  97. HttpURLConnection conn = null;
  98. while (location != null && !location.isEmpty()) {
  99. URL url = new URL(location);
  100. conn = (HttpURLConnection) url.openConnection();
  101. conn.setRequestProperty("User-Agent",
  102. "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)");
  103. conn.connect();
  104. location = conn.getHeaderField("Location");
  105. }
  106. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  107. String line = null;
  108. String mcVersion = Proxies.common.getMinecraftVersion();
  109. while ((line = reader.readLine()) != null) {
  110. if (line.startsWith(mcVersion) && line.contains(Defaults.MOD)) {
  111. String[] tokens = line.split(":");
  112. recommendedVersion = tokens[2];
  113. }
  114. }
  115. if (recommendedVersion == null)
  116. return;
  117. int result = FMLCommonHandler.instance().findContainerFor(Forestry.instance).getProcessedVersion().compareTo(new DefaultArtifactVersion(recommendedVersion));
  118. if (result >= 0) {
  119. Proxies.log.finer("Using the latest version [" + VERSION + " (build:" + BUILD_NUMBER + ")] for Minecraft " + mcVersion);
  120. currentVersion = EnumUpdateState.CURRENT;
  121. return;
  122. }
  123. cachedChangelog = grabChangelog(recommendedVersion);
  124. Proxies.log.warning("Using outdated version [" + VERSION + " (build:" + BUILD_NUMBER + ")] for Minecraft " + mcVersion + ". Consider updating.");
  125. currentVersion = EnumUpdateState.OUTDATED;
  126. } catch (Exception e) {
  127. // e.printStackTrace();
  128. Proxies.log.warning("Unable to read from remote version authority.");
  129. currentVersion = EnumUpdateState.CONNECTION_ERROR;
  130. }
  131. }
  132. }
  133. public static void versionCheck() {
  134. new VersionChecker().start();
  135. }
  136. public static String[] getChangelog() {
  137. return cachedChangelog;
  138. }
  139. public static String[] grabChangelog(String version) {
  140. try {
  141. String location = REMOTE_CHANGELOG_ROOT + version;
  142. HttpURLConnection conn = null;
  143. while (location != null && !location.isEmpty()) {
  144. URL url = new URL(location);
  145. conn = (HttpURLConnection) url.openConnection();
  146. conn.setRequestProperty("User-Agent",
  147. "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)");
  148. conn.connect();
  149. location = conn.getHeaderField("Location");
  150. }
  151. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  152. String line = null;
  153. ArrayList<String> changelog = new ArrayList<String>();
  154. while ((line = reader.readLine()) != null) {
  155. if (line.startsWith("#"))
  156. continue;
  157. if (line.isEmpty())
  158. continue;
  159. changelog.add(line);
  160. }
  161. return changelog.toArray(new String[0]);
  162. } catch (Exception ex) {
  163. // ex.printStackTrace();
  164. Proxies.log.warning("Unable to read changelog from remote site.");
  165. }
  166. return new String[] { String.format("Unable to retrieve changelog for %s %s", Defaults.MOD, version) };
  167. }
  168. }