mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 16:49:04 +00:00
Replace fast-classpath-scanner with classgraph
This commit is contained in:
+1
-1
@@ -15,7 +15,7 @@ dependencies {
|
|||||||
compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: kotlinxCoroutinesVersion
|
compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: kotlinxCoroutinesVersion
|
||||||
|
|
||||||
implementation group: 'com.google.guava', name: 'guava', version: guavaVersion
|
implementation group: 'com.google.guava', name: 'guava', version: guavaVersion
|
||||||
implementation group: 'io.github.lukehutch', name: 'fast-classpath-scanner', version: classpathScannerVersion
|
implementation group: 'io.github.classgraph', name: 'classgraph', version: classGraphVersion
|
||||||
implementation group: 'com.lambdaworks', name: 'scrypt', version: scryptVersion
|
implementation group: 'com.lambdaworks', name: 'scrypt', version: scryptVersion
|
||||||
|
|
||||||
testImplementation group: 'junit', name: 'junit', version: junitVersion
|
testImplementation group: 'junit', name: 'junit', version: junitVersion
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.apollo.game.plugin;
|
package org.apollo.game.plugin;
|
||||||
|
|
||||||
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
|
import io.github.classgraph.ClassGraph;
|
||||||
|
import io.github.classgraph.ClassInfo;
|
||||||
|
import io.github.classgraph.ClassInfoList;
|
||||||
|
import io.github.classgraph.ScanResult;
|
||||||
import org.apollo.game.model.World;
|
import org.apollo.game.model.World;
|
||||||
import org.apollo.game.plugin.kotlin.KotlinPluginScript;
|
import org.apollo.game.plugin.kotlin.KotlinPluginScript;
|
||||||
|
|
||||||
@@ -13,6 +16,7 @@ import java.util.logging.Logger;
|
|||||||
public class KotlinPluginEnvironment implements PluginEnvironment {
|
public class KotlinPluginEnvironment implements PluginEnvironment {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(KotlinPluginEnvironment.class.getName());
|
private static final Logger logger = Logger.getLogger(KotlinPluginEnvironment.class.getName());
|
||||||
|
private static final String PLUGIN_SUFFIX = "_plugin";
|
||||||
|
|
||||||
private final World world;
|
private final World world;
|
||||||
private PluginContext context;
|
private PluginContext context;
|
||||||
@@ -26,25 +30,26 @@ public class KotlinPluginEnvironment implements PluginEnvironment {
|
|||||||
List<KotlinPluginScript> pluginScripts = new ArrayList<>();
|
List<KotlinPluginScript> pluginScripts = new ArrayList<>();
|
||||||
List<Class<? extends KotlinPluginScript>> pluginClasses = new ArrayList<>();
|
List<Class<? extends KotlinPluginScript>> pluginClasses = new ArrayList<>();
|
||||||
|
|
||||||
new FastClasspathScanner()
|
ClassGraph classGraph = new ClassGraph().enableAllInfo();
|
||||||
.matchSubclassesOf(KotlinPluginScript.class, pluginClasses::add)
|
|
||||||
.scan();
|
|
||||||
|
|
||||||
try {
|
try (ScanResult scanResult = classGraph.scan()) {
|
||||||
for (Class<? extends KotlinPluginScript> pluginClass : pluginClasses) {
|
ClassInfoList pluginClassList = scanResult
|
||||||
Constructor<? extends KotlinPluginScript> pluginConstructor =
|
.getSubclasses(KotlinPluginScript.class.getName())
|
||||||
pluginClass.getConstructor(World.class, PluginContext.class);
|
.directOnly();
|
||||||
|
|
||||||
pluginScripts.add(pluginConstructor.newInstance(world, context));
|
for (ClassInfo pluginClassInfo : pluginClassList) {
|
||||||
|
Class<KotlinPluginScript> scriptClass = pluginClassInfo.loadClass(KotlinPluginScript.class);
|
||||||
|
Constructor<KotlinPluginScript> scriptConstructor = scriptClass.getConstructor(World.class,
|
||||||
|
PluginContext.class);
|
||||||
|
|
||||||
|
pluginScripts.add(scriptConstructor.newInstance(world, context));
|
||||||
|
logger.info(String.format("Loaded plugin: %s", pluginDescriptor(scriptClass)));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginScripts.forEach(script -> {
|
pluginScripts.forEach(script -> script.doStart(world));
|
||||||
logger.info("Starting script: " + script.getClass().getName());
|
|
||||||
script.doStart(world);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -52,4 +57,11 @@ public class KotlinPluginEnvironment implements PluginEnvironment {
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String pluginDescriptor(Class<? extends KotlinPluginScript> clazz) {
|
||||||
|
String className = clazz.getSimpleName();
|
||||||
|
String name = className.substring(0, className.length() - PLUGIN_SUFFIX.length());
|
||||||
|
Package pkg = clazz.getPackage();
|
||||||
|
|
||||||
|
return pkg == null ? name : name + " from " + pkg.getName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,5 +10,5 @@ ext {
|
|||||||
guavaVersion = '19.0'
|
guavaVersion = '19.0'
|
||||||
commonsCompressVersion = '1.10'
|
commonsCompressVersion = '1.10'
|
||||||
assertjVersion = '3.8.0'
|
assertjVersion = '3.8.0'
|
||||||
classpathScannerVersion = '2.0.21'
|
classGraphVersion = '4.0.6'
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user