mirror of
https://gitlab.com/2009scape/rt4-client.git
synced 2026-08-28 05:45:15 -06:00
Update to allow plugin metadata definition from annotations and load other classes in same folder as plugin.class.
This commit is contained in:
parent
9750195081
commit
3e12a9115d
15 changed files with 122 additions and 68 deletions
|
|
@ -1,4 +0,0 @@
|
|||
|
||||
AUTHOR='Ceikry'
|
||||
DESCRIPTION='Draws some info about varp changes on the screen.'
|
||||
VERSION=-1.0
|
||||
|
|
@ -1,5 +1,7 @@
|
|||
package plugin;
|
||||
|
||||
import plugin.annotations.PluginMeta;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
|
|
@ -42,7 +44,21 @@ class PluginInfo {
|
|||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public static PluginInfo loadFromClass(Class<?> clazz) {
|
||||
PluginMeta info = clazz.getAnnotation(PluginMeta.class);
|
||||
|
||||
if (info == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new PluginInfo(
|
||||
info.author(),
|
||||
info.description(),
|
||||
info.version()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,9 @@ import java.net.URL;
|
|||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Responsible for loading and broadcasting methods to all plugins.
|
||||
|
|
@ -39,16 +41,11 @@ public class PluginRepository {
|
|||
|
||||
for(File file : Objects.requireNonNull(pluginsDirectory.listFiles())) {
|
||||
if(!file.isDirectory()) continue;
|
||||
if(file.getName().equals("META-INF")) continue;
|
||||
|
||||
File infoFile = new File(file.getAbsoluteFile() + File.separator + "plugin.properties");
|
||||
File pluginRoot = new File(file.getAbsoluteFile() + File.separator + "plugin.class");
|
||||
|
||||
PluginInfo info;
|
||||
if (infoFile.exists())
|
||||
info = PluginInfo.loadFromFile(infoFile);
|
||||
else
|
||||
info = new PluginInfo("Unknown", "", 1.0);
|
||||
|
||||
if (!pluginRoot.exists()) {
|
||||
System.err.println("Unable to load plugin " + file.getName() + " because plugin.class is absent!");
|
||||
continue;
|
||||
|
|
@ -56,6 +53,17 @@ public class PluginRepository {
|
|||
|
||||
Class<?> clazz = loader.loadClass(file.getName() + ".plugin");
|
||||
|
||||
PluginInfo info;
|
||||
if (infoFile.exists())
|
||||
info = PluginInfo.loadFromFile(infoFile);
|
||||
else
|
||||
info = PluginInfo.loadFromClass(clazz);
|
||||
|
||||
if (info == null) {
|
||||
System.err.println("Unable to load plugin " + file.getName() + " because it contains no information about author, version, etc!");
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
Plugin thisPlugin = (Plugin) clazz.newInstance();
|
||||
thisPlugin._init();
|
||||
|
|
@ -66,6 +74,15 @@ public class PluginRepository {
|
|||
return;
|
||||
}
|
||||
|
||||
List<File> otherClasses = Arrays.stream(Objects.requireNonNull(file.listFiles()))
|
||||
.filter((f) ->
|
||||
!f.getName().equals("plugin.class") && f.getName().contains(".class"))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (File f : otherClasses) {
|
||||
loader.loadClass(file.getName() + "." + f.getName().replace(".class",""));
|
||||
}
|
||||
|
||||
System.out.println("Successfully loaded plugin " + file.getName() + ", version " + info.version);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
|
|
|||
11
client/src/main/java/plugin/annotations/PluginMeta.java
Normal file
11
client/src/main/java/plugin/annotations/PluginMeta.java
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package plugin.annotations;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface PluginMeta {
|
||||
String author();
|
||||
String description();
|
||||
double version();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue