plugin supporting

This commit is contained in:
jiahua.liu 2020-01-18 21:31:08 +08:00
parent bda3e6dc0f
commit 5aba7196d5
5 changed files with 26 additions and 68 deletions

View File

@ -3,7 +3,8 @@ apply plugin: "java"
dependencies {
api project(':mirai-core')
runtimeOnly files(':mirai-core-timpc')
api project(':mirai-core-timpc')
runtimeOnly files('../mirai-core-timpc/build/classes/kotlin/jvm/main')
runtimeOnly files('../mirai-core/build/classes/kotlin/jvm/main')
// classpath is not set correctly by IDE
}

View File

@ -18,34 +18,27 @@ fun main() {
println("\"login qqnumber qqpassword \" to login a bot")
println("\"login qq号 qq密码 \" 来登陆一个BOT")
thread { processNextCommandLine() }
thread {
loop@ while (true) {
var command = readLine()
if (command != null) {
var commandArgs = command.split(" ")
when (commandArgs[0]) {
"login" -> {
if (commandArgs.size < 3) {
println("\"login qqnumber qqpassword \" to login a bot")
println("\"login qq号 qq密码 \" 来登陆一个BOT")
continue@loop
}
val qqNumber = commandArgs[1].toLong()
val qqPassword = commandArgs[2]
println("login...")
GlobalScope.launch {
Bot(qqNumber, qqPassword)
}
}
}
PluginManager.loadPlugins()
}
tailrec fun processNextCommandLine() {
val commandArgs = readLine()?.split(" ") ?: return
when (commandArgs[0]) {
"login" -> {
if (commandArgs.size < 3) {
println("\"login qqnumber qqpassword \" to login a bot")
println("\"login qq号 qq密码 \" 来登录一个BOT")
return processNextCommandLine()
}
val qqNumber = commandArgs[1].toLong()
val qqPassword = commandArgs[2]
println("login...")
GlobalScope.launch {
Bot(qqNumber, qqPassword)
}
}
}
PluginManager.loadPlugins()
return processNextCommandLine()
}

View File

@ -7,7 +7,6 @@ import java.io.InputStream
import java.io.InputStreamReader
import java.net.JarURLConnection
import java.net.URL
import java.net.URLClassLoader
import java.util.jar.JarFile
@ -103,13 +102,6 @@ class PluginDescription(
}
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 {
File(it).mkdirs()
@ -127,7 +119,7 @@ object PluginManager {
*/
fun loadPlugins(){
val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf()
val pluginsLocation: MutableMap<String, File> = mutableMapOf()
val pluginsLocation: MutableMap<String, JarFile> = mutableMapOf()
File(pluginsPath).listFiles()?.forEach { file ->
if (file != null) {
@ -151,7 +143,7 @@ object PluginManager {
val description = PluginDescription.readFromContent(sb.toString())
println(description)
pluginsFound[description.pluginName] = description
pluginsLocation[description.pluginName] = file
pluginsLocation[description.pluginName] = jar
}
}
}
@ -217,11 +209,9 @@ object PluginManager {
logger.info("loading plugin " + description.pluginName)
try {
val pluginClass =
PluginClassLoader((pluginsLocation[description.pluginName]!!), this.javaClass.classLoader)
.loadClass(description.pluginBasePath)
this.javaClass.classLoader.loadClass(description.pluginBasePath)
return try {
val subClass = pluginClass.asSubclass(PluginBase::class.java)
val subClass = javaClass.asSubclass(PluginBase::class.java)
val plugin: PluginBase = subClass.getDeclaredConstructor().newInstance()
description.loaded = true
logger.info("successfully loaded plugin " + description.pluginName)
@ -229,6 +219,7 @@ object PluginManager {
nameToPluginBaseMap[description.pluginName] = plugin
plugin.init(description)
true
} catch (e: ClassCastException) {
false.also {

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime (3)" level="project" />
<orderEntry type="module" module-name="mirai-console_main" />
</component>
</module>

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>