Commit 5aba7196 authored by jiahua.liu's avatar jiahua.liu

plugin supporting

parent bda3e6dc
...@@ -3,7 +3,8 @@ apply plugin: "java" ...@@ -3,7 +3,8 @@ apply plugin: "java"
dependencies { dependencies {
api project(':mirai-core') 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') runtimeOnly files('../mirai-core/build/classes/kotlin/jvm/main')
// classpath is not set correctly by IDE // classpath is not set correctly by IDE
} }
...@@ -18,34 +18,27 @@ fun main() { ...@@ -18,34 +18,27 @@ fun main() {
println("\"login qqnumber qqpassword \" to login a bot") println("\"login qqnumber qqpassword \" to login a bot")
println("\"login qq号 qq密码 \" 来登陆一个BOT") println("\"login qq号 qq密码 \" 来登陆一个BOT")
thread { processNextCommandLine() }
thread { PluginManager.loadPlugins()
loop@ while (true) { }
var command = readLine()
if (command != null) { tailrec fun processNextCommandLine() {
var commandArgs = command.split(" ") val commandArgs = readLine()?.split(" ") ?: return
when (commandArgs[0]) { when (commandArgs[0]) {
"login" -> { "login" -> {
if (commandArgs.size < 3) { if (commandArgs.size < 3) {
println("\"login qqnumber qqpassword \" to login a bot") println("\"login qqnumber qqpassword \" to login a bot")
println("\"login qq号 qq密码 \" 来登陆一个BOT") println("\"login qq号 qq密码 \" 来登录一个BOT")
continue@loop return processNextCommandLine()
} }
val qqNumber = commandArgs[1].toLong() val qqNumber = commandArgs[1].toLong()
val qqPassword = commandArgs[2] val qqPassword = commandArgs[2]
println("login...") println("login...")
GlobalScope.launch { GlobalScope.launch {
Bot(qqNumber, qqPassword) Bot(qqNumber, qqPassword)
}
}
}
} }
} }
} }
return processNextCommandLine()
PluginManager.loadPlugins()
} }
...@@ -7,7 +7,6 @@ import java.io.InputStream ...@@ -7,7 +7,6 @@ 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
...@@ -103,13 +102,6 @@ class PluginDescription( ...@@ -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 { 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()
...@@ -127,7 +119,7 @@ object PluginManager { ...@@ -127,7 +119,7 @@ object PluginManager {
*/ */
fun loadPlugins(){ fun loadPlugins(){
val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf() val pluginsFound: MutableMap<String, PluginDescription> = mutableMapOf()
val pluginsLocation: MutableMap<String, File> = mutableMapOf() val pluginsLocation: MutableMap<String, JarFile> = mutableMapOf()
File(pluginsPath).listFiles()?.forEach { file -> File(pluginsPath).listFiles()?.forEach { file ->
if (file != null) { if (file != null) {
...@@ -151,7 +143,7 @@ object PluginManager { ...@@ -151,7 +143,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] = file pluginsLocation[description.pluginName] = jar
} }
} }
} }
...@@ -217,11 +209,9 @@ object PluginManager { ...@@ -217,11 +209,9 @@ object PluginManager {
logger.info("loading plugin " + description.pluginName) logger.info("loading plugin " + description.pluginName)
try { try {
val pluginClass = this.javaClass.classLoader.loadClass(description.pluginBasePath)
PluginClassLoader((pluginsLocation[description.pluginName]!!), this.javaClass.classLoader)
.loadClass(description.pluginBasePath)
return try { return try {
val subClass = pluginClass.asSubclass(PluginBase::class.java) val subClass = javaClass.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)
...@@ -229,6 +219,7 @@ object PluginManager { ...@@ -229,6 +219,7 @@ 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 {
......
<?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>
\ No newline at end of file
<?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>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment