-
Notifications
You must be signed in to change notification settings - Fork 497
/
Version.java
266 lines (207 loc) · 7.4 KB
/
Version.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import net.minecraft.command.ICommandSender;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.event.FMLInterModComms;
import net.minecraftforge.common.config.Property;
import buildcraft.BuildCraftCore;
import buildcraft.api.core.BCLog;
import buildcraft.core.proxy.CoreProxy;
public class Version implements Runnable {
public enum EnumUpdateState {
CURRENT, OUTDATED, CONNECTION_ERROR
}
public static final String VERSION = "@VERSION@";
public static EnumUpdateState currentVersion = EnumUpdateState.CURRENT;
private static final String REMOTE_VERSION_FILE =
"https://raw.githubusercontent.com/BuildCraft/BuildCraft/master/buildcraft_resources/versions.txt";
private static final String REMOTE_CHANGELOG_ROOT =
"https://raw.githubusercontent.com/BuildCraft/BuildCraft/master/buildcraft_resources/changelog/";
private static String recommendedVersion;
private static String[] cachedChangelog;
private static Version instance = new Version();
private static boolean sentIMCOutdatedMessage = false;
public static String getVersion() {
return VERSION;
}
public static boolean isOutdated() {
return currentVersion == EnumUpdateState.OUTDATED;
}
public static boolean needsUpdateNoticeAndMarkAsSeen() {
if (!isOutdated() || sentIMCOutdatedMessage) {
return false;
}
Property property = BuildCraftCore.mainConfiguration.get("vars", "version.seen", VERSION);
property.comment = "indicates the last version the user has been informed about and will suppress further notices on it.";
String seenVersion = property.getString();
if (recommendedVersion == null || recommendedVersion.equals(seenVersion)) {
return false;
}
property.set(recommendedVersion);
BuildCraftCore.mainConfiguration.save();
return true;
}
public static String getRecommendedVersion() {
return recommendedVersion;
}
public static void versionCheck() {
try {
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) {
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) {
throw new NullPointerException();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
String mcVersion = CoreProxy.proxy.getMinecraftVersion();
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(":");
if (mcVersion.matches(tokens[0])) {
if (DefaultProps.MOD.matches(tokens[1])) {
recommendedVersion = tokens[2];
if (VERSION.matches(tokens[2])) {
BCLog.logger.trace("Using the latest version [" + getVersion() + "] for Minecraft " + mcVersion);
currentVersion = EnumUpdateState.CURRENT;
return;
}
}
}
}
BCLog.logger.warn("Using outdated version [" + VERSION + "] for Minecraft " + mcVersion
+ ". Consider updating to " + recommendedVersion + ".");
currentVersion = EnumUpdateState.OUTDATED;
sendIMCOutdatedMessage();
conn.disconnect();
reader.close();
} catch (Exception e) {
BCLog.logger.warn("Unable to read from remote version authority.");
BCLog.logger.warn(e.toString());
currentVersion = EnumUpdateState.CONNECTION_ERROR;
}
}
public static String[] getChangelog() {
if (cachedChangelog == null) {
cachedChangelog = grabChangelog(recommendedVersion);
}
return cachedChangelog;
}
public static String[] grabChangelog(String version) {
try {
String location = REMOTE_CHANGELOG_ROOT + version;
HttpURLConnection conn = null;
while (location != null && !location.isEmpty()) {
URL url = new URL(location);
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) {
throw new NullPointerException();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
ArrayList<String> changelog = new ArrayList<String>();
while ((line = reader.readLine()) != null) {
if (line.isEmpty()) {
continue;
}
changelog.add(line);
}
conn.disconnect();
return changelog.toArray(new String[changelog.size()]);
} catch (Exception ex) {
ex.printStackTrace();
BCLog.logger.warn("Unable to read changelog from remote site.");
}
return new String[]{String.format("Unable to retrieve changelog for %s %s", DefaultProps.MOD, version)};
}
@Override
public void run() {
int count = 0;
currentVersion = null;
BCLog.logger.info("Beginning version check");
try {
while ((count < 3) && ((currentVersion == null) || (currentVersion == EnumUpdateState.CONNECTION_ERROR))) {
versionCheck();
count++;
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
BCLog.logger.info("Version check attempt " + count + " failed, trying again in 10 seconds");
Thread.sleep(10000);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
if (currentVersion == EnumUpdateState.CONNECTION_ERROR) {
BCLog.logger.info("Version check failed");
}
}
/**
* This is an integration with Dynious Version Checker See
* http://www.minecraftforum.net/topic/2721902-
*/
public static void sendIMCOutdatedMessage() {
if (Loader.isModLoaded("VersionChecker")) {
NBTTagCompound compound = new NBTTagCompound();
compound.setString("modDisplayName", "BuildCraft");
compound.setString("oldVersion", VERSION);
compound.setString("newVersion", getRecommendedVersion());
compound.setString("updateUrl", "http://www.mod-buildcraft.com/download/");
compound.setBoolean("isDirectLink", false);
StringBuilder stringBuilder = new StringBuilder();
for (String changeLogLine : getChangelog()) {
stringBuilder.append(changeLogLine).append("\n");
}
compound.setString("changeLog", stringBuilder.toString());
FMLInterModComms.sendRuntimeMessage("BuildCraft|Core", "VersionChecker", "addUpdate", compound);
sentIMCOutdatedMessage = true;
}
}
public static void displayChangelog(ICommandSender sender) {
int nb = 0;
for (String updateLine : Version.getChangelog()) {
sender.addChatMessage(new ChatComponentText("\u00A79" + updateLine));
nb++;
if (nb > 3) {
sender.addChatMessage(new ChatComponentText("\u00A79[...]"));
break;
}
}
}
public static void check() {
new Thread(instance).start();
}
}