|
@@ -34,7 +34,20 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
|
|
|
* 插件被分配的data folder, 如果插件改名了 data folder 也会变 请注意
|
|
* 插件被分配的data folder, 如果插件改名了 data folder 也会变 请注意
|
|
|
*/
|
|
*/
|
|
|
val dataFolder: File by lazy {
|
|
val dataFolder: File by lazy {
|
|
|
- File(PluginManager.pluginsPath + pluginDescription.name).also { it.mkdir() }
|
|
|
|
|
|
|
+ File(_getDataFolder()).also {
|
|
|
|
|
+ it.mkdir()
|
|
|
|
|
+ println(it.absolutePath)
|
|
|
|
|
+ println(pluginName)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ internal fun _getDataFolder():String{
|
|
|
|
|
+ return if(inited){
|
|
|
|
|
+ PluginManager.pluginsPath + "/" + pluginName
|
|
|
|
|
+ }else{
|
|
|
|
|
+ PluginManager.pluginsPath + "/" + PluginManager.lastPluginName//for init
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -75,7 +88,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
|
|
|
* 这个config是read-write的
|
|
* 这个config是read-write的
|
|
|
*/
|
|
*/
|
|
|
fun loadConfig(fileName: String): Config {
|
|
fun loadConfig(fileName: String): Config {
|
|
|
- return Config.load(dataFolder.absolutePath + fileName)
|
|
|
|
|
|
|
+ return Config.load(dataFolder.absolutePath + "/" + fileName)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@JvmOverloads
|
|
@JvmOverloads
|
|
@@ -84,20 +97,21 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
|
|
|
this.onDisable()
|
|
this.onDisable()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private lateinit var pluginDescription: PluginDescription
|
|
|
|
|
|
|
+ internal var pluginName:String = ""
|
|
|
|
|
|
|
|
- internal fun init(pluginDescription: PluginDescription) {
|
|
|
|
|
- this.pluginDescription = pluginDescription
|
|
|
|
|
|
|
+ private var inited = false
|
|
|
|
|
+ internal fun init() {
|
|
|
this.onLoad()
|
|
this.onLoad()
|
|
|
|
|
+ inited = true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
val pluginManager = PluginManager
|
|
val pluginManager = PluginManager
|
|
|
|
|
|
|
|
val logger: MiraiLogger by lazy {
|
|
val logger: MiraiLogger by lazy {
|
|
|
- SimpleLogger("Plugin ${pluginDescription.name}") { _, message, e ->
|
|
|
|
|
- MiraiConsole.logger("[${pluginDescription.name}]", 0, message)
|
|
|
|
|
|
|
+ SimpleLogger("Plugin ${pluginName}") { _, message, e ->
|
|
|
|
|
+ MiraiConsole.logger("[${pluginName}]", 0, message)
|
|
|
if (e != null) {
|
|
if (e != null) {
|
|
|
- MiraiConsole.logger("[${pluginDescription.name}]", 0, e.toString())
|
|
|
|
|
|
|
+ MiraiConsole.logger("[${pluginName}]", 0, e.toString())
|
|
|
e.printStackTrace()
|
|
e.printStackTrace()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -111,7 +125,7 @@ abstract class PluginBase(coroutineContext: CoroutineContext) : CoroutineScope {
|
|
|
this.javaClass.classLoader.getResourceAsStream(fileName)
|
|
this.javaClass.classLoader.getResourceAsStream(fileName)
|
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
|
PluginManager.getFileInJarByName(
|
|
PluginManager.getFileInJarByName(
|
|
|
- this.pluginDescription.name,
|
|
|
|
|
|
|
+ this.pluginName,
|
|
|
fileName
|
|
fileName
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|
|
@@ -147,15 +161,35 @@ class PluginDescription(
|
|
|
|
|
|
|
|
companion object {
|
|
companion object {
|
|
|
fun readFromContent(content_: String): PluginDescription {
|
|
fun readFromContent(content_: String): PluginDescription {
|
|
|
- with(Config.load(content_)){
|
|
|
|
|
|
|
+ with(Config.load(content_,"yml")){
|
|
|
try {
|
|
try {
|
|
|
return PluginDescription(
|
|
return PluginDescription(
|
|
|
- this.getString("name"),
|
|
|
|
|
- this.getString("author"),
|
|
|
|
|
- this.getString("path"),
|
|
|
|
|
- this.getString("version"),
|
|
|
|
|
- this.getString("info"),
|
|
|
|
|
- this.getStringList("depends")
|
|
|
|
|
|
|
+ name = this.getString("name"),
|
|
|
|
|
+ author = try{
|
|
|
|
|
+ this.getString("author")
|
|
|
|
|
+ }catch (e:Exception){
|
|
|
|
|
+ "unknown"
|
|
|
|
|
+ },
|
|
|
|
|
+ basePath = try{
|
|
|
|
|
+ this.getString("path")
|
|
|
|
|
+ }catch (e:Exception){
|
|
|
|
|
+ this.getString("main")
|
|
|
|
|
+ },
|
|
|
|
|
+ version = try{
|
|
|
|
|
+ this.getString("version")
|
|
|
|
|
+ }catch (e:Exception){
|
|
|
|
|
+ "unknown"
|
|
|
|
|
+ },
|
|
|
|
|
+ info = try{
|
|
|
|
|
+ this.getString("info")
|
|
|
|
|
+ }catch (e:Exception){
|
|
|
|
|
+ "unknown"
|
|
|
|
|
+ },
|
|
|
|
|
+ depends = try{
|
|
|
|
|
+ this.getStringList("depends")
|
|
|
|
|
+ }catch (e:Exception){
|
|
|
|
|
+ listOf<String>()
|
|
|
|
|
+ }
|
|
|
)
|
|
)
|
|
|
}catch (e:Exception){
|
|
}catch (e:Exception){
|
|
|
error("Failed to read Plugin.YML")
|
|
error("Failed to read Plugin.YML")
|
|
@@ -169,7 +203,10 @@ internal class PluginClassLoader(file: File, parent: ClassLoader) :
|
|
|
URLClassLoader(arrayOf(file.toURI().toURL()), parent)
|
|
URLClassLoader(arrayOf(file.toURI().toURL()), parent)
|
|
|
|
|
|
|
|
object PluginManager {
|
|
object PluginManager {
|
|
|
- internal val pluginsPath = System.getProperty("user.dir") + "/plugins/".replace("//", "/").also {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ internal var lastPluginName: String = ""
|
|
|
|
|
+
|
|
|
|
|
+ internal val pluginsPath = (System.getProperty("user.dir") + "/plugins/").replace("//", "/").also {
|
|
|
File(it).mkdirs()
|
|
File(it).mkdirs()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -187,6 +224,19 @@ object PluginManager {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ fun getPluginDescriptions(base:PluginBase):PluginDescription{
|
|
|
|
|
+ nameToPluginBaseMap.forEach{ (s, pluginBase) ->
|
|
|
|
|
+ if(pluginBase == base){
|
|
|
|
|
+ return pluginDescriptions[s]!!
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ error("can not find plugin description")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fun getPluginDataFolder(){
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
fun getAllPluginDescriptions(): Collection<PluginDescription> {
|
|
fun getAllPluginDescriptions(): Collection<PluginDescription> {
|
|
|
return pluginDescriptions.values
|
|
return pluginDescriptions.values
|
|
|
}
|
|
}
|
|
@@ -297,16 +347,19 @@ object PluginManager {
|
|
|
.loadClass("${description.basePath}Kt")
|
|
.loadClass("${description.basePath}Kt")
|
|
|
}
|
|
}
|
|
|
return try {
|
|
return try {
|
|
|
|
|
+ lastPluginName = description.name
|
|
|
val subClass = pluginClass.asSubclass(PluginBase::class.java)
|
|
val subClass = pluginClass.asSubclass(PluginBase::class.java)
|
|
|
val plugin: PluginBase =
|
|
val plugin: PluginBase =
|
|
|
subClass.kotlin.objectInstance ?: subClass.getDeclaredConstructor().newInstance()
|
|
subClass.kotlin.objectInstance ?: subClass.getDeclaredConstructor().newInstance()
|
|
|
|
|
+ println("aaaaaa")
|
|
|
description.loaded = true
|
|
description.loaded = true
|
|
|
logger.info("successfully loaded plugin " + description.name + " version " + description.version + " by " + description.author)
|
|
logger.info("successfully loaded plugin " + description.name + " version " + description.version + " by " + description.author)
|
|
|
logger.info(description.info)
|
|
logger.info(description.info)
|
|
|
|
|
|
|
|
nameToPluginBaseMap[description.name] = plugin
|
|
nameToPluginBaseMap[description.name] = plugin
|
|
|
pluginDescriptions[description.name] = description
|
|
pluginDescriptions[description.name] = description
|
|
|
- plugin.init(description)
|
|
|
|
|
|
|
+ plugin.pluginName = description.name
|
|
|
|
|
+ plugin.init()
|
|
|
true
|
|
true
|
|
|
} catch (e: ClassCastException) {
|
|
} catch (e: ClassCastException) {
|
|
|
logger.error("failed to load plugin " + description.name + " , Main class does not extends PluginBase ")
|
|
logger.error("failed to load plugin " + description.name + " , Main class does not extends PluginBase ")
|