LoadableModFile.java
15.7 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package com.mumfrey.liteloader.core.api;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import joptsimple.internal.Strings;
import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.mumfrey.liteloader.api.manager.APIProvider;
import com.mumfrey.liteloader.core.LiteLoader;
import com.mumfrey.liteloader.interfaces.LoadableFile;
import com.mumfrey.liteloader.interfaces.LoadableMod;
import com.mumfrey.liteloader.launch.InjectionStrategy;
import com.mumfrey.liteloader.launch.LoaderEnvironment;
import com.mumfrey.liteloader.util.log.LiteLoaderLogger;
/**
* Wrapper for file which represents a mod file to load with associated version
* information and metadata. Retrieve this from litemod.json at enumeration
* time. We also override comparable to provide our own custom sorting logic
* based on version info.
*
* @author Adam Mummery-Smith
*/
public class LoadableModFile extends LoadableFile implements LoadableMod<File>
{
private static final long serialVersionUID = -7952147161905688459L;
/**
* Maximum recursion depth for mod discovery
*/
private static final int MAX_DISCOVERY_DEPTH = 16;
/**
* Gson parser for JSON
*/
protected static Gson gson = new Gson();
/**
* True if the metadata information is parsed successfully, the mod will be
* added.
*/
protected boolean valid = false;
/**
* Name of the mod specified in the JSON file, this can be any string but
* should be the same between mod versions.
*/
protected String modName;
/**
* Loader version
*/
protected String targetVersion;
/**
* Name of the class transof
*/
protected List<String> classTransformerClassNames = new ArrayList<String>();
/**
* File time stamp, used as sorting criteria when no revision information is
* found.
*/
protected long timeStamp;
/**
* Revision number from the json file
*/
protected float revision = 0.0F;
/**
* True if the revision number was successfully read, used as a semaphore so
* that we know when revision is a valid number.
*/
protected boolean hasRevision = false;
/**
* ALL of the parsed metadata from the file, associated with the mod later
* on for retrieval via the loader.
*/
protected Map<String, Object> metaData = new HashMap<String, Object>();
/**
* Dependencies declared in the metadata
*/
private Set<String> dependencies = new HashSet<String>();
/**
* Dependencies which are missing
*/
private Set<String> missingDependencies = new HashSet<String>();;
/**
* Required APIs declared in the metadata
*/
private Set<String> requiredAPIs = new HashSet<String>();
/**
* Required APIs which are missing
*/
private Set<String> missingAPIs = new HashSet<String>();
/**
* Classes in this container
*/
protected List<String> classNames = null;
/**
* @param file
* @param metaData
*/
protected LoadableModFile(File file, String metaData)
{
super(file.getAbsolutePath());
this.init(metaData);
}
/**
* @param file
* @param metaData
*/
protected LoadableModFile(LoadableFile file, String metaData)
{
super(file);
this.init(metaData);
}
/**
* @param metaData
*/
@SuppressWarnings("unchecked")
protected void init(String metaData)
{
this.timeStamp = this.lastModified();
this.tweakPriority = 0;
if (!Strings.isNullOrEmpty(metaData))
{
try
{
this.metaData = LoadableModFile.gson.fromJson(metaData, HashMap.class);
}
catch (JsonSyntaxException jsx)
{
LiteLoaderLogger.warning("Error reading %s in %s, JSON syntax exception: %s",
LoadableMod.METADATA_FILENAME, this.getAbsolutePath(), jsx.getMessage());
return;
}
this.valid = this.parseMetaData();
}
}
protected boolean parseMetaData()
{
try
{
this.modName = this.getMetaValue("name", this.getDefaultName());
this.displayName = this.getMetaValue("displayName", this.modName);
this.version = this.getMetaValue("version", "Unknown");
this.author = this.getMetaValue("author", "Unknown");
if (!this.parseVersions()) return false;
this.injectionStrategy = InjectionStrategy.parseStrategy(this.getMetaValue("injectAt", null));
this.tweakClassName = this.getMetaValue("tweakClass", this.tweakClassName);
this.getMetaValuesInto(this.classTransformerClassNames, "classTransformerClasses", ",");
this.getMetaValuesInto(this.dependencies, "dependsOn", ",");
this.getMetaValuesInto(this.requiredAPIs, "requiredAPIs", ",");
this.getMetaValuesInto(this.mixinConfigs, "mixinConfigs", ",");
}
catch (ClassCastException ex)
{
LiteLoaderLogger.debug(ex);
LiteLoaderLogger.warning("Error parsing version metadata file in %s, check the format of the file", this.getAbsolutePath());
}
return true;
}
public boolean parseVersions()
{
this.targetVersion = this.getMetaValue("mcversion", null);
if (this.targetVersion == null)
{
LiteLoaderLogger.warning("Mod in %s has no loader version number reading %s", this.getAbsolutePath(), LoadableMod.METADATA_FILENAME);
return false;
}
try
{
this.revision = Float.parseFloat(this.getMetaValue("revision", null));
this.hasRevision = true;
}
catch (NullPointerException ex) {}
catch (Exception ex)
{
LiteLoaderLogger.warning("Mod in %s has an invalid revision number reading %s", this.getAbsolutePath(), LoadableMod.METADATA_FILENAME);
}
return true;
}
protected String getDefaultName()
{
return this.getName().replaceAll("[^a-zA-Z]", "");
}
@Override
public String getModName()
{
return this.modName;
}
@Override
public String getIdentifier()
{
return this.modName.toLowerCase();
}
@Override
public String getDescription(String key)
{
if (this.missingAPIs.size() > 0)
{
return LiteLoader.translate("gui.description.missingapis", "\n" + this.compileMissingAPIList());
}
if (this.missingDependencies.size() > 0)
{
return LiteLoader.translate("gui.description.missingdeps", "\n" + this.missingDependencies.toString());
}
String descriptionKey = "description";
if (key != null && key.length() > 0)
{
descriptionKey += "." + key.toLowerCase();
}
return this.getMetaValue(descriptionKey, this.getMetaValue("description", ""));
}
private String compileMissingAPIList()
{
StringBuilder missingAPIList = new StringBuilder();
for (String missingAPI : this.missingAPIs)
{
if (missingAPI != null)
{
if (missingAPI.contains("@"))
{
Matcher matcher = APIProvider.idAndRevisionPattern.matcher(missingAPI);
if (matcher.matches())
{
missingAPIList.append(" ").append(matcher.group(1)).append(" (revision ").append(matcher.group(2)).append(")\n");
continue;
}
}
missingAPIList.append(" ").append(missingAPI).append("\n");
}
}
return missingAPIList.toString();
}
@Override
public boolean isEnabled(LoaderEnvironment environment)
{
return this.missingDependencies.size() == 0 && this.missingAPIs.size() == 0 && super.isEnabled(environment);
}
@Override
public boolean isExternalJar()
{
return false;
}
@Override
public boolean isToggleable()
{
return true;
}
@Override
public boolean hasValidMetaData()
{
return this.valid;
}
@Override
public String getTargetVersion()
{
return this.targetVersion;
}
@Override
public float getRevision()
{
return this.revision;
}
protected Object getMetaValue(String metaKey)
{
Object metaValue = this.metaData.get(metaKey);
if (metaValue != null) return metaValue;
return this.metaData.get(metaKey.toLowerCase());
}
@Override
public String getMetaValue(String metaKey, String defaultValue)
{
Object metaValue = this.getMetaValue(metaKey);
return metaValue != null ? metaValue.toString() : defaultValue;
}
@SuppressWarnings("unchecked")
public String[] getMetaValues(String metaKey, String separator)
{
Object metaValue = this.getMetaValue(metaKey);
if (metaValue instanceof String)
{
return ((String)metaValue).split(separator);
}
else if (metaValue instanceof ArrayList)
{
return ((ArrayList<String>)metaValue).toArray(new String[0]);
}
return new String[0];
}
protected void getMetaValuesInto(Collection<String> collection, String metaKey, String separator)
{
for (String name : this.getMetaValues(metaKey, separator))
{
if (!Strings.isNullOrEmpty(name))
{
collection.add(name);
}
}
}
@Override
public Set<String> getMetaDataKeys()
{
return Collections.unmodifiableSet(this.metaData.keySet());
}
@Override
public boolean hasClassTransformers()
{
return this.classTransformerClassNames.size() > 0;
}
@Override
public List<String> getClassTransformerClassNames()
{
return this.classTransformerClassNames;
}
@Override
public boolean hasResources()
{
return true;
}
@Override
public boolean hasDependencies()
{
return this.dependencies.size() > 0;
}
@Override
public Set<String> getDependencies()
{
return this.dependencies;
}
@Override
public void registerMissingDependency(String dependency)
{
this.missingDependencies.add(dependency);
}
@Override
public Set<String> getMissingDependencies()
{
return this.missingDependencies;
}
@Override
public Set<String> getRequiredAPIs()
{
return this.requiredAPIs;
}
@Override
public void registerMissingAPI(String identifier)
{
this.missingAPIs.add(identifier);
}
@Override
public Set<String> getMissingAPIs()
{
return this.missingAPIs;
}
@Override
public List<String> getContainedClassNames()
{
if (this.classNames == null)
{
this.classNames = this.enumerateClassNames();
}
return this.classNames;
}
protected List<String> enumerateClassNames()
{
if (this.isDirectory())
{
return LoadableModFile.enumerateDirectory(new ArrayList<String>(), this, "", 0);
}
return LoadableModFile.enumerateZipFile(this);
}
@Override
public void addContainedMod(String modName)
{
}
@Override
public int compareTo(File other)
{
if (other == null || !(other instanceof LoadableModFile)) return -1;
LoadableModFile otherMod = (LoadableModFile)other;
// If the other object has a revision, compare revisions
if (otherMod.hasRevision)
{
return this.hasRevision && this.revision - otherMod.revision > 0 ? -1 : 1;
}
// If we have a revision and the other object doesn't, then we are higher
if (this.hasRevision)
{
return -1;
}
// Give up and use timestamp
return (int)(otherMod.timeStamp - this.timeStamp);
}
protected static List<String> enumerateZipFile(File file)
{
List<String> classes = new ArrayList<String>();
ZipFile zipFile;
try
{
zipFile = new ZipFile(file);
}
catch (IOException ex)
{
return classes;
}
@SuppressWarnings("unchecked")
Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>)zipFile.entries();
while (entries.hasMoreElements())
{
ZipEntry entry = entries.nextElement();
String entryName = entry.getName();
if (entry.getSize() > 0 && entryName.endsWith(".class"))
{
classes.add(entryName.substring(0, entryName.length() - 6).replace('/', '.'));
}
}
try
{
zipFile.close();
}
catch (IOException ex) {}
return classes;
}
/**
* Recursive function to enumerate classes inside a classpath folder
*
* @param classes
* @param packagePath
* @param packageName
*/
protected static List<String> enumerateDirectory(List<String> classes, File packagePath, String packageName, int depth)
{
// Prevent crash due to broken recursion
if (depth > MAX_DISCOVERY_DEPTH)
{
return classes;
}
File[] classFiles = packagePath.listFiles();
for (File classFile : classFiles)
{
if (classFile.isDirectory())
{
LoadableModFile.enumerateDirectory(classes, classFile, packageName + classFile.getName() + ".", depth + 1);
}
else
{
if (classFile.getName().endsWith(".class"))
{
String classFileName = classFile.getName();
classes.add(packageName + classFileName.substring(0, classFileName.length() - 6));
}
}
}
return classes;
}
/**
* @param zip
* @param entry
* @throws IOException
*/
public static String zipEntryToString(ZipFile zip, ZipEntry entry) throws IOException
{
InputStream stream = null;
Charset charset = Charsets.UTF_8;
int bomOffset = 0;
byte[] bytes;
try
{
stream = zip.getInputStream(entry);
bytes = ByteStreams.toByteArray(stream);
}
finally
{
if (stream != null) stream.close();
}
if (bytes == null || bytes.length == 0) return "";
// Handle unicode by looking for BOM
if (bytes.length > 1)
{
if (bytes[0] == (byte)0xFF && bytes[1] == (byte)0xFE)
{
charset = Charsets.UTF_16LE;
bomOffset = 2;
}
else if (bytes[0] == (byte)0xFE && bytes[1] == (byte)0xFF)
{
charset = Charsets.UTF_16BE;
bomOffset = 2;
}
}
return new String(bytes, bomOffset, bytes.length - bomOffset, charset);
}
protected static String getVersionMetaDataString(File file)
{
return LoadableFile.getFileContents(file, LoadableMod.METADATA_FILENAME, Charsets.UTF_8);
}
}