summaryrefslogtreecommitdiff
path: root/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt')
-rw-r--r--src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt b/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt
index 7ce8649..8c436c3 100644
--- a/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt
+++ b/src/main/kotlin/de/romjaki/pluggabledino/api/PluginLoader.kt
@@ -12,6 +12,8 @@ object PluginLoader {
private val klaxon = Klaxon()
private const val pluginFolder = "plugins"
+ private var devPlugin: String? = null
+
val plugins
get() = _plugins.toMap()
@@ -22,15 +24,20 @@ object PluginLoader {
Scanner(
cl.getResourceAsStream("plugin.json"))
.useDelimiter("\\A")
- .next())!!
- val pluginClass = cl.loadClass(pluginInfo.clazz)
+ .next()) ?: throw PluginMetaMissingException(url.toString())
+ return loadPlugin(pluginInfo, cl.loadClass(pluginInfo.clazz))
+ }
+
+ private fun loadPlugin(pluginInfo: PluginMeta, pluginClass: Class<*>): IPlugin? {
if (!IPlugin::class.java.isAssignableFrom(pluginClass)) {
return null
}
val plugin = pluginClass.newInstance() as IPlugin
plugin.meta = pluginInfo
_plugins[pluginInfo.name] = plugin
+
return plugin
+
}
private fun checkDependencies() {
@@ -56,10 +63,26 @@ object PluginLoader {
.map {
it.toURI().toURL()
}.forEach { loadPlugin(it) }
+ installDevPlugin()
checkDependencies()
injectDependencies()
}
+ private fun installDevPlugin() {
+ if (devPlugin == null) {
+ return
+ }
+ val clazz = Class.forName(devPlugin)
+ val meta = klaxon.parse<PluginMeta>(
+ Scanner(
+ clazz
+ .classLoader
+ .getResourceAsStream("plugin.json"))
+ .useDelimiter("\\A")
+ .next()) ?: throw PluginMetaMissingException("<dev>")
+ loadPlugin(meta, clazz)
+ }
+
private fun injectDependencies() {
for ((name, instance) in _plugins) {
@@ -73,4 +96,8 @@ object PluginLoader {
}
}
}
+
+ fun loadDevPlugin(className: String) {
+ devPlugin = className
+ }
} \ No newline at end of file