/
PluginInfo.java
513 lines (458 loc) · 17.5 KB
/
PluginInfo.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
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
package net.ME1312.Galaxi.Plugin;
import net.ME1312.Galaxi.Galaxi;
import net.ME1312.Galaxi.Library.Exception.IllegalPluginException;
import net.ME1312.Galaxi.Library.ExtraDataHandler;
import net.ME1312.Galaxi.Library.Log.Logger;
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Map.ObjectMapValue;
import net.ME1312.Galaxi.Library.Platform;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.*;
import java.util.List;
/**
* Plugin Info Class
*
* @see Plugin
*/
public class PluginInfo implements ExtraDataHandler {
private static final String ID_PATTERN = ".*?([A-Za-z0-9!#$&+\\-_. ]*).*?";
private static ArrayList<String> usedNames = new ArrayList<String>();
private static HashMap<Class<?>, PluginInfo> pluginMap = new HashMap<Class<?>, PluginInfo>();
private Object plugin;
private Image icon;
private String name;
private String display;
private Version version;
private Version signature;
private String state;
private List<String> authors;
private String desc;
private URL website;
private List<String> loadBefore;
private List<Dependency> depend;
private File dir = new File(System.getProperty("user.dir"));
private Logger logger = null;
private Runnable updateChecker = null;
private boolean enabled = false;
private ObjectMap<String> extra = new ObjectMap<String>();
public static class Dependency {
private String name;
private Version minversion, maxversion;
private boolean required;
/**
* Generate Plugin DependencyInfo
*
* @param name Dependency Name
* @param minversion Minimum Version
* @param maxversion Maximum Version
* @param required Required Status
*/
protected Dependency(String name, Version minversion, Version maxversion, boolean required) {
if (Util.isNull(name, required)) throw new NullPointerException();
this.name = name;
this.minversion = minversion;
this.maxversion = maxversion;
this.required = required;
}
/**
* Get the name of the Dependency
*
* @return Dependency name
*/
public String getName() {
return name;
}
/**
* Get the minimum required version of the Dependency
*
* @return Minimum required version (null for all)
*/
public Version getMinVersion() {
return minversion;
}
/**
* Get the maximum allowed version of the Dependency
*
* @return Maximum allowed version (null for all)
*/
public Version getMaxVersion() {
return maxversion;
}
/**
* Get if this Dependency is required
*
* @return Required Dependency Status
*/
public boolean isRequired() {
return required;
}
}
/**
* Get or register PluginInfo for objects tagged with @App/@Plugin
*
* @param main Class tagged with @App/@Plugin
* @return PluginInfo
* @throws InvocationTargetException
*/
public static PluginInfo getPluginInfo(Object main) throws InvocationTargetException {
Class<?> mainClass = main.getClass();
if (!pluginMap.keySet().contains(mainClass)) {
try {
if (mainClass.isAnnotationPresent(Plugin.class)) {
String name = mainClass.getAnnotation(Plugin.class).name().replaceAll(ID_PATTERN, "$1");
String display = (mainClass.getAnnotation(Plugin.class).display().length() > 0)?mainClass.getAnnotation(Plugin.class).display():mainClass.getAnnotation(Plugin.class).name();
Version version = Version.fromString(mainClass.getAnnotation(Plugin.class).version());
Version signature = (mainClass.getAnnotation(Plugin.class).signature().length() > 0)?Version.fromString(mainClass.getAnnotation(Plugin.class).signature()):null;
String state = (mainClass.getAnnotation(Plugin.class).signature().length() > 0)?mainClass.getAnnotation(Plugin.class).state():null;
List<String> authors = Arrays.asList(mainClass.getAnnotation(Plugin.class).authors());
String description = (mainClass.getAnnotation(Plugin.class).description().length() > 0)?mainClass.getAnnotation(Plugin.class).description():null;
URL website = (mainClass.getAnnotation(Plugin.class).website().length() > 0)?new URL(mainClass.getAnnotation(Plugin.class).website()):null;
List<String> loadBefore = Arrays.asList(mainClass.getAnnotation(Plugin.class).loadBefore());
List<Dependency> dependencies = new LinkedList<Dependency>();
for (net.ME1312.Galaxi.Plugin.Dependency dependency : mainClass.getAnnotation(Plugin.class).dependencies()) {
String dname = dependency.name().replaceAll(ID_PATTERN, "$1");
Version dminversion = (dependency.minVersion().length() > 0)?Version.fromString(dependency.minVersion()):null;
Version dmaxversion = (dependency.maxVersion().length() > 0)?Version.fromString(dependency.maxVersion()):null;
boolean drequired = dependency.required();
if (dname.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty dependency name");
if (dminversion != null && dmaxversion != null && dminversion.equals(dmaxversion)) throw new IllegalArgumentException("Cannot use the same dependency version for min and max");
dependencies.add(new Dependency(dname, dminversion, dmaxversion, drequired));
}
PluginInfo plugin = new PluginInfo(main, name, version, authors, description, website, loadBefore, dependencies);
plugin.setDisplayName(display);
plugin.setSignature(signature);
plugin.setState(state);
pluginMap.put(mainClass, plugin);
usedNames.add(name.toLowerCase());
} else if (mainClass.isAnnotationPresent(App.class)) {
String name = mainClass.getAnnotation(App.class).name().replaceAll(ID_PATTERN, "$1");
String display = (mainClass.getAnnotation(App.class).display().length() > 0)?mainClass.getAnnotation(App.class).display():mainClass.getAnnotation(App.class).name();
Version version = Version.fromString(mainClass.getAnnotation(App.class).version());
Version signature = (mainClass.getAnnotation(App.class).signature().length() > 0)?Version.fromString(mainClass.getAnnotation(App.class).signature()):null;
String state = (mainClass.getAnnotation(App.class).signature().length() > 0)?mainClass.getAnnotation(App.class).state():null;
List<String> authors = Arrays.asList(mainClass.getAnnotation(App.class).authors());
String description = (mainClass.getAnnotation(App.class).description().length() > 0)?mainClass.getAnnotation(App.class).description():null;
URL website = (mainClass.getAnnotation(App.class).website().length() > 0)?new URL(mainClass.getAnnotation(App.class).website()):null;
PluginInfo plugin = new PluginInfo(main, name, version, authors, description, website, Collections.emptyList(), Collections.emptyList());
plugin.setDisplayName(display);
plugin.setSignature(signature);
plugin.setState(state);
pluginMap.put(mainClass, plugin);
usedNames.add(name.toLowerCase());
} else {
throw new IllegalStateException("Class not annotated by @App or @Plugin: " + mainClass.getCanonicalName());
}
} catch (Throwable e) {
throw new IllegalPluginException(e, "Couldn't load plugin descriptor for main class: " + main);
}
}
return pluginMap.get(mainClass);
}
/**
* Generate PluginInfo
*
* @param plugin Plugin Object
* @param name Plugin Name
* @param version Plugin Version
* @param authors Authors List
* @param description Plugin Description
* @param website Author's Website
* @param loadBefore Load Before List
* @param dependencies Dependencies List
*/
protected PluginInfo(Object plugin, String name, Version version, List<String> authors, String description, URL website, List<String> loadBefore, List<Dependency> dependencies) {
if (Util.isNull(plugin, name, version, authors)) throw new NullPointerException();
if (name.length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty name");
if (version.toString().length() == 0) throw new StringIndexOutOfBoundsException("Cannot use an empty version");
if (authors.size() == 0) throw new ArrayIndexOutOfBoundsException("Cannot use an empty authors list");
if (description != null && description.length() == 0) description = null;
this.plugin = plugin;
this.name = name;
this.version = version;
this.authors = authors;
this.desc = description;
this.website = website;
this.loadBefore = (loadBefore == null)?Collections.emptyList():loadBefore;
this.depend = (dependencies == null)?Collections.emptyList():dependencies;
}
/**
* Get the Plugin's ClassLoader
*
* @return Plugin ClassLoader
*/
public ClassLoader getLoader() {
return plugin.getClass().getClassLoader();
}
/**
* Get Plugin Object
*
* @return Plugin Object
*/
public Object get() {
return plugin;
}
/**
* Get the Plugin's Icon
*
* @return Plugin Icon
*/
public Image getIcon() {
return icon;
}
/**
* Set the Plugin's Icon
*
* @param stream Icon Stream
*/
public void setIcon(InputStream stream) throws IOException {
icon = ImageIO.read(stream);
}
/**
* Get the Plugin's Name
*
* @return Plugin Name
*/
public String getName() {
return this.name;
}
/**
* Get the Plugin's Display Name
*
* @return Display Name
*/
public String getDisplayName() {
return (display == null)?getName():display;
}
/**
* Set the Plugin's Display Name
*
* @param value Value (or null to reset)
*/
public void setDisplayName(String value) {
if (value == null || value.length() == 0 || getName().equals(value)) {
this.display = null;
} else {
this.display = value;
}
}
/**
* Get the Plugin's Version
*
* @return Plugin Version
*/
public Version getVersion() {
return this.version;
}
/**
* Get the Plugin's Build Signature
*
* @return Plugin Build Signature
*/
public Version getSignature() {
return this.signature;
}
/**
* Set the Plugin's Build Signature (may only be done once)
*
* @param value Plugin Build Signature
*/
public void setSignature(Version value) {
if (signature == null) {
signature = value;
}
}
/**
* Get the Plugin's State
*
* @return Plugin State
*/
public String getState() {
return this.state;
}
/**
* Set the Plugin's State
*
* @param value Plugin State
*/
public void setState(String value) {
state = value;
}
/**
* Get Authors List
*
* @return Authors List
*/
public List<String> getAuthors() {
return this.authors;
}
/**
* Get Plugin Description
*
* @return Plugin Description
*/
public String getDescription() {
return this.desc;
}
/**
* Get Authors' Website
*
* @return Authors' Website
*/
public URL getWebsite() {
return this.website;
}
/**
* Gets the Load Before Plugins List
*
* @return Load Before Plugins List
*/
public List<String> getLoadBefore() {
return this.loadBefore;
}
/**
* Gets the Dependencies List
*
* @return Dependencies List
*/
public List<Dependency> getDependencies() {
return this.depend;
}
/**
* Scans to create a Full Dependency List (which includes the dependencies of your dependencies)
*
* @return Full Dependencies List
*/
public List<PluginInfo> scanDependencies() {
List<PluginInfo> used = new ArrayList<PluginInfo>();
used.add(this);
return scanDependencies(this, used);
}
private List<PluginInfo> scanDependencies(PluginInfo info, List<PluginInfo> used) {
LinkedList<PluginInfo> output = new LinkedList<PluginInfo>();
for (PluginInfo.Dependency depend : info.getDependencies()) {
if (Galaxi.getInstance().getPluginManager().getPlugins().get(depend.getName().toLowerCase()) != null) {
output.addAll(scanDependencies(Galaxi.getInstance().getPluginManager().getPlugin(depend.getName().toLowerCase()), used));
}
}
if (!used.contains(info)) {
output.add(info);
used.add(info);
}
return output;
}
/**
* Builds a stack of relevant platform and versioning information for this plugin (Useful for debugging!)
*
* @return Platform Information Stack
*/
public List<String> getPlatformStack() {
LinkedList<String> stack = new LinkedList<String>();
PluginInfo engine = Galaxi.getInstance().getEngineInfo();
PluginInfo app = Galaxi.getInstance().getAppInfo();
stack.addAll(Arrays.asList(
Platform.getSystemName() + ' ' + Platform.getSystemVersion() + ((!Platform.getSystemArchitecture().equals("unknown"))?" [" + Platform.getSystemArchitecture() + ']':"") + ",",
"Java " + Platform.getJavaVersion() + ((!Platform.getJavaArchitecture().equals("unknown"))?" [" + Platform.getJavaArchitecture() + ']':"") + ",",
engine.getName() + " v" + engine.getVersion().toExtendedString() + ((engine.getSignature() != null)?" (" + engine.getSignature() + ')':"") + ((engine == app)?" [Standalone]"+((engine == this)?"":","):",")
));
if (engine != app)
stack.add(app.getName() + " v" + app.getVersion().toExtendedString() + ((app.getSignature() != null)?" (" + app.getSignature() + ')':"") + ((app == this)?"":","));
if (app != this) {
for (PluginInfo plugin : scanDependencies()) {
stack.add(plugin.getDisplayName() + " v" + plugin.getVersion().toExtendedString() + ((plugin.getSignature() != null)?" (" + plugin.getSignature() + ')':"") + ((plugin.getState() != null)?" [" + plugin.getState() + ']':"") + ',');
}
stack.add(this.getDisplayName() + " v" + this.getVersion().toExtendedString() + ((this.getSignature() != null)?" (" + this.getSignature() + ')':"") + ((this.getState() != null)?" [" + this.getState() + ']':""));
}
return stack;
}
/**
* Sets the Plugin's Enabled Status
*
* @return Enabled Status
*/
public boolean isEnabled() {
return enabled;
}
/**
* Sets if the Plugin is Enabled
*
* @param value Value
*/
public void setEnabled(boolean value) {
enabled = value;
}
/**
* Replace this Plugin's Logger with a custom one
*
* @param value Value
*/
public void setLogger(Logger value) {
if (Util.isNull(value)) throw new NullPointerException();
logger = value;
}
/**
* Gets the default Logger for this Plugin
*
* @return Logger
*/
public Logger getLogger() {
if (logger == null) logger = new Logger(name);
return logger;
}
/**
* Get the Update Checker for this Plugin
*
* @return Update Checker
*/
public Runnable getUpdateChecker() {
return updateChecker;
}
/**
* Set the Update Checker for this Plugin
*
* @param checker Value
*/
public void setUpdateChecker(Runnable checker) {
this.updateChecker = checker;
}
/**
* Gets this Plugin's data folder
*
* @return Data Folder
*/
public File getDataFolder() {
if (!dir.exists()) dir.mkdirs();
return dir;
}
@Override
public void addExtra(String handle, Object value) {
if (Util.isNull(handle, value)) throw new NullPointerException();
extra.set(handle, value);
}
@Override
public boolean hasExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
return extra.getKeys().contains(handle);
}
@Override
public ObjectMapValue<String> getExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
return extra.get(handle);
}
@Override
public ObjectMap<String> getExtra() {
return extra.clone();
}
@Override
public void removeExtra(String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
extra.remove(handle);
}
}