update new plugindes loader

This commit is contained in:
jiahua.liu 2020-02-28 17:51:17 +08:00
parent 1dba6601f3
commit afe0b0e46e

View File

@ -147,50 +147,20 @@ class PluginDescription(
companion object { companion object {
fun readFromContent(content_: String): PluginDescription { fun readFromContent(content_: String): PluginDescription {
val content = content_.split("\n") with(Config.load(content_)){
try {
var name = "Plugin" return PluginDescription(
var author = "Unknown" this.getString("name"),
var basePath = "net.mamoe.mirai.PluginMain" this.getString("author"),
var info = "Unknown" this.getString("path"),
var version = "1.0.0" this.getString("version"),
val depends = mutableListOf<String>(); this.getString("info"),
this.getStringList("depends")
content.forEach { )
val line = it.trim() }catch (e:Exception){
val lowercaseLine = line.toLowerCase() error("Failed to read Plugin.YML")
if (it.contains(":")) {
when {
lowercaseLine.startsWith("name") -> {
name = line.substringAfter(":").trim()
}
lowercaseLine.startsWith("author") -> {
author = line.substringAfter(":").trim()
}
lowercaseLine.startsWith("info") || lowercaseLine.startsWith("information") -> {
info = line.substringAfter(":").trim()
}
lowercaseLine.startsWith("main") || lowercaseLine.startsWith("path") || lowercaseLine.startsWith(
"basepath"
) -> {
basePath = line.substringAfter(":").trim()
}
lowercaseLine.startsWith("version") || lowercaseLine.startsWith("ver") -> {
version = line.substringAfter(":").trim()
}
}
} else if (line.startsWith("-")) {
depends.add(line.substringAfter("-").trim())
} }
} }
return PluginDescription(
name,
author,
basePath,
version,
info,
depends
)
} }
} }
} }
@ -238,13 +208,17 @@ object PluginManager {
if (pluginYml == null) { if (pluginYml == null) {
logger.info("plugin.yml not found in jar " + jar.name + ", it will not be consider as a Plugin") logger.info("plugin.yml not found in jar " + jar.name + ", it will not be consider as a Plugin")
} else { } else {
val description = try {
PluginDescription.readFromContent( val description =
URL("jar:file:" + file.absoluteFile + "!/" + pluginYml.name).openConnection().inputStream.use { PluginDescription.readFromContent(
it.readBytes().encodeToString() URL("jar:file:" + file.absoluteFile + "!/" + pluginYml.name).openConnection().inputStream.use {
}) it.readBytes().encodeToString()
pluginsFound[description.name] = description })
pluginsLocation[description.name] = file pluginsFound[description.name] = description
pluginsLocation[description.name] = file
}catch (e:Exception){
logger.info(e.message)
}
} }
} }
} }