-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDependencyDownloader.java
415 lines (376 loc) · 10 KB
/
DependencyDownloader.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import org.bukkit.configuration.file.*;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import org.json.simple.*;
import org.json.simple.parser.*;
import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;
import java.util.logging.Level;
import java.util.zip.*;
/**
* Downloads dependencies for your plugins <br>
* <br>
* <b>You must have a option in your config to disable the downloader!</b>
*
* @author Zombie_Striker
*
* --Based on the Updater by
* @author ArsenArsen
*/
public class DependencyDownloader {
private static final String HOST = "https://api.curseforge.com";
private static final String QUERY = "/servermods/files?projectIds=";
private static final String AGENT = "Mozilla/5.0 Dependency Downloader by Zombie_Striker";
private static final File WORKING_DIR = new File("plugins" + File.separator
+ "DependencyDownloader" + File.separator);
private static final File CONFIG_FILE = new File(WORKING_DIR, "global.yml");
private static final char[] HEX_CHAR_ARRAY = "0123456789abcdef"
.toCharArray();
private int id = -1;
private Plugin p;
private boolean debug = false;
private String downloadURL = null;
private String futuremd5;
private String downloadName;
private List<Channel> allowedChannels = Arrays.asList(Channel.ALPHA,
Channel.BETA, Channel.RELEASE);
private FileConfiguration global;
public boolean downloaderActive = false;
/**
* Downloads the dependency with the id
*
* @param p
* The plugin
* @param id
* Plugin ID
*/
public DependencyDownloader(Plugin p, int id) {
setID(id);
this.p = p;
if (!CONFIG_FILE.exists()) {
try {
CONFIG_FILE.getParentFile().mkdirs();
CONFIG_FILE.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
global = YamlConfiguration.loadConfiguration(CONFIG_FILE);
global.options()
.header("DependencyDownloader by Zombie_Striker\nGlobal config");
if (!global.isSet("download")) {
global.set("download", true);
try {
global.save(CONFIG_FILE);
} catch (IOException e) {
e.printStackTrace();
}
}
downloaderActive = global.getBoolean("download");
downloadDependency();
Bukkit.reload();
}
/**
* Downloads the dependency with the id
*
* @param p
* The plugin
* @param id
* Plugin ID
*/
public DependencyDownloader(Plugin p, ProjectID id) {
this(p,id.getID());
}
/**
* Gets the plugin ID
*
* @return the plugin ID
*/
public int getID() {
return id;
}
/**
* Sets the plugin ID
*
* @param id
* The plugin ID
*/
public void setID(int id) {
this.id = id;
}
/**
* Attempts a update
*/
public void downloadDependency() {
if (downloaderActive) {
debug("Disabled!");
return;
}
new BukkitRunnable() {
@Override
public void run() {
debug("Downloading STARTED!");
download();
}
}.runTaskAsynchronously(p);
}
private void download() {
String target = HOST + QUERY + id;
debug(target);
try {
URL url = new URL(target);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.addRequestProperty("User-Agent", AGENT);
connection.connect();
debug("Connecting!");
BufferedReader responseReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuilder responseBuffer = new StringBuilder();
String line;
while ((line = responseReader.readLine()) != null) {
responseBuffer.append(line);
}
responseReader.close();
String response = responseBuffer.toString();
int counter = 1;
if (connection.getResponseCode() == 200) {
try {
debug("RESCODE 200");
while (true) {
debug("Counter: " + counter);
JSONParser parser = new JSONParser();
JSONArray json = (JSONArray) parser.parse(response);
if (json.size() - counter < 0) {
debug("No version available!");
break;
// :Should never happen, but keep it here just
// in case
}
JSONObject latest = (JSONObject) json.get(json.size()
- counter);
futuremd5 = (String) latest.get("md5");
String channel = (String) latest.get("releaseType");
//String name = (String) latest.get("name");
if (allowedChannels.contains(Channel
.matchChannel(channel.toUpperCase()))
/*&& !hasTag(name)*/) {
downloadURL = ((String) latest.get("downloadUrl"))
.replace(" ", "%20");
downloadName = (String) latest.get("fileName");
break;
} else
counter++;
}
} catch (ParseException e) {
p.getLogger().log(Level.SEVERE,
"Could not parse API Response for " + target, e);
}
}
} catch (IOException e) {
p.getLogger().log(Level.SEVERE,
"Could not check for the dependencies for the plugin " + p.getName(), e);
}
try {
File downloadTo = new File(p.getDataFolder().getParentFile()
.getAbsolutePath(), downloadName);
downloadTo.getParentFile().mkdirs();
downloadTo.delete();
debug("Started download!");
downloadIsSeperateBecauseGotoGotRemoved(downloadTo);
debug("Ended download!");
if (!fileHash(downloadTo).equalsIgnoreCase(futuremd5))
return;
if (downloadTo.getName().endsWith(".jar")) {
return;
} else
unzip(downloadTo);
} catch (IOException e) {
p.getLogger().log(Level.SEVERE,
"Couldn't download " + downloadName, e);
return;
}
}
/**
* God damn it Gosling, <a
* href="http://stackoverflow.com/a/4547764/3809164">reference here.</a>
*/
private void downloadIsSeperateBecauseGotoGotRemoved(File downloadTo)
throws IOException {
URL url = new URL(downloadURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", AGENT);
connection.connect();
if (connection.getResponseCode() >= 300
&& connection.getResponseCode() < 400) {
downloadURL = connection.getHeaderField("Location");
downloadIsSeperateBecauseGotoGotRemoved(downloadTo);
} else {
debug(connection.getResponseCode() + " "
+ connection.getResponseMessage() + " when requesting "
+ downloadURL);
copy(connection.getInputStream(), new FileOutputStream(downloadTo));
}
}
private long copy(InputStream in, OutputStream out) throws IOException {
long bytes = 0;
byte[] buf = new byte[0x1000];
while (true) {
int r = in.read(buf);
if (r == -1)
break;
out.write(buf, 0, r);
bytes += r;
debug("Another 4K, current: " + r);
}
out.flush();
out.close();
in.close();
return bytes;
}
private void unzip(File download) {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(download);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry entry;
File downloadedFile = new File(p.getDataFolder().getParentFile()
.getAbsoluteFile(), downloadName);
while ((entry = entries.nextElement()) != null) {
File target = new File(downloadedFile, entry.getName());
File inPlugins = new File(p.getDataFolder().getParentFile(),
downloadName);
if (!inPlugins.exists()) {
target = inPlugins;
}
if (!entry.isDirectory()) {
target.getParentFile().mkdirs();
InputStream zipStream = zipFile.getInputStream(entry);
OutputStream fileStream = new FileOutputStream(target);
copy(zipStream, fileStream);
}
}
return;
} catch (IOException e) {
if (e instanceof ZipException) {
p.getLogger().log(Level.SEVERE,
"Could not unzip downloaded file!", e);
return;
} else {
p.getLogger().log(
Level.SEVERE,
"An IOException occured while trying to unzip %s!"
.replace("%s", p.getName()), e);
return;
}
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ignored) {
}
}
}
}
private void debug(String message) {
if (debug)
p.getLogger().info(
message + ' ' + new Throwable().getStackTrace()[1]);
}
/**
* Sets allowed channels, AKA release types
*
* @param channels
* The allowed channels
*/
public void setChannels(Channel... channels) {
allowedChannels.clear();
allowedChannels.addAll(Arrays.asList(channels));
}
public enum Channel {
/**
* Normal release
*/
RELEASE("release"),
/**
* Beta release
*/
BETA("beta"),
/**
* Alpha release
*/
ALPHA("alpha");
private String channel;
Channel(String channel) {
this.channel = channel;
}
/**
* Gets the channel value
*
* @return the channel value
*/
public String getChannel() {
return channel;
}
/**
* Returns channel whose channel value matches the given string
*
* @param channel
* The channel value
* @return The Channel constant
*/
public static Channel matchChannel(String channel) {
for (Channel c : values()) {
if (c.channel.equalsIgnoreCase(channel)) {
return c;
}
}
return null;
}
}
/**
* Calculates files MD5 hash
*
* @param file
* The file to digest
* @return The MD5 hex or null, if the operation failed
*/
public String fileHash(File file) {
FileInputStream is;
try {
is = new FileInputStream(file);
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = new byte[2048];
int numBytes;
while ((numBytes = is.read(bytes)) != -1) {
md.update(bytes, 0, numBytes);
}
byte[] digest = md.digest();
char[] hexChars = new char[digest.length * 2];
for (int j = 0; j < digest.length; j++) {
int v = digest[j] & 0xFF;
hexChars[j * 2] = HEX_CHAR_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_CHAR_ARRAY[v & 0x0F];
}
is.close();
return new String(hexChars);
} catch (IOException | NoSuchAlgorithmException e) {
p.getLogger().log(Level.SEVERE,
"Could not digest " + file.getPath(), e);
return null;
}
}
public enum ProjectID {
PROTOCOLLIB(45564), WORLDGUARD(31054), WORLDEDIT(31043), VAULT(33184), HOLOGRAPHICDISPLAYS(
75097), CITIZENS(31073), FACTIONS(31292), DYNMAP(31620);
private int id;
private ProjectID(int i) {
id = i;
}
public int getID() {
return id;
}
}
}