mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 09:50:16 +08:00
plugin supporting
This commit is contained in:
parent
c4f55f3a34
commit
bda3e6dc0f
@ -7,6 +7,7 @@ import java.io.InputStream
|
|||||||
import java.io.InputStreamReader
|
import java.io.InputStreamReader
|
||||||
import java.net.JarURLConnection
|
import java.net.JarURLConnection
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.net.URLClassLoader
|
||||||
import java.util.jar.JarFile
|
import java.util.jar.JarFile
|
||||||
|
|
||||||
|
|
||||||
@ -92,17 +93,24 @@ class PluginDescription(
|
|||||||
version = line.substringAfter(":").trim()
|
version = line.substringAfter(":").trim()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}else if(line.startsWith("-")){
|
} else if (line.startsWith("-")) {
|
||||||
depends.add(line.substringAfter("-").trim())
|
depends.add(line.substringAfter("-").trim())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PluginDescription(name,author,basePath,version,info,depends)
|
return PluginDescription(name, author, basePath, version, info, depends)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
object PluginManager{
|
class PluginClassLoader(file: File, parent: ClassLoader) : URLClassLoader(arrayOf(file.toURI().toURL()), parent) {
|
||||||
|
override fun findClass(moduleName: String?, name: String?): Class<*> {
|
||||||
|
return super.findClass(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
object PluginManager {
|
||||||
internal val pluginsPath = System.getProperty("user.dir") + "/plugins/".replace("//", "/").also {
|
internal val pluginsPath = System.getProperty("user.dir") + "/plugins/".replace("//", "/").also {
|
||||||
File(it).mkdirs()
|
File(it).mkdirs()
|
||||||
}
|
}
|
||||||
@ -119,7 +127,7 @@ object PluginManager{
|
|||||||
*/
|
*/
|
||||||
fun loadPlugins(){
|
fun loadPlugins(){
|
||||||
val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf()
|
val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf()
|
||||||
val pluginsLocation: MutableMap<String, JarFile> = mutableMapOf()
|
val pluginsLocation: MutableMap<String, File> = mutableMapOf()
|
||||||
|
|
||||||
File(pluginsPath).listFiles()?.forEach { file ->
|
File(pluginsPath).listFiles()?.forEach { file ->
|
||||||
if (file != null) {
|
if (file != null) {
|
||||||
@ -143,7 +151,7 @@ object PluginManager{
|
|||||||
val description = PluginDescription.readFromContent(sb.toString())
|
val description = PluginDescription.readFromContent(sb.toString())
|
||||||
println(description)
|
println(description)
|
||||||
pluginsFound[description.pluginName] = description
|
pluginsFound[description.pluginName] = description
|
||||||
pluginsLocation[description.pluginName] = jar
|
pluginsLocation[description.pluginName] = file
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,9 +217,11 @@ object PluginManager{
|
|||||||
logger.info("loading plugin " + description.pluginName)
|
logger.info("loading plugin " + description.pluginName)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
this.javaClass.classLoader.loadClass(description.pluginBasePath)
|
val pluginClass =
|
||||||
|
PluginClassLoader((pluginsLocation[description.pluginName]!!), this.javaClass.classLoader)
|
||||||
|
.loadClass(description.pluginBasePath)
|
||||||
return try {
|
return try {
|
||||||
val subClass = javaClass.asSubclass(PluginBase::class.java)
|
val subClass = pluginClass.asSubclass(PluginBase::class.java)
|
||||||
val plugin: PluginBase = subClass.getDeclaredConstructor().newInstance()
|
val plugin: PluginBase = subClass.getDeclaredConstructor().newInstance()
|
||||||
description.loaded = true
|
description.loaded = true
|
||||||
logger.info("successfully loaded plugin " + description.pluginName)
|
logger.info("successfully loaded plugin " + description.pluginName)
|
||||||
@ -219,7 +229,6 @@ object PluginManager{
|
|||||||
|
|
||||||
nameToPluginBaseMap[description.pluginName] = plugin
|
nameToPluginBaseMap[description.pluginName] = plugin
|
||||||
plugin.init(description)
|
plugin.init(description)
|
||||||
|
|
||||||
true
|
true
|
||||||
} catch (e: ClassCastException) {
|
} catch (e: ClassCastException) {
|
||||||
false.also {
|
false.also {
|
||||||
|
Binary file not shown.
@ -3,6 +3,10 @@ package net.mamoe.mirai.imageplugin;
|
|||||||
import net.mamoe.mirai.plugin.PluginBase;
|
import net.mamoe.mirai.plugin.PluginBase;
|
||||||
|
|
||||||
public class ImagePluginMain extends PluginBase {
|
public class ImagePluginMain extends PluginBase {
|
||||||
|
static {
|
||||||
|
System.out.println("Static Loaded");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
System.out.println("Loaded");
|
System.out.println("Loaded");
|
||||||
|
Loading…
Reference in New Issue
Block a user