Implement plugin recognizing

This commit is contained in:
Him188 2020-05-23 20:28:25 +08:00
parent 8cc09c77fb
commit f6af60191b
4 changed files with 38 additions and 16 deletions

View File

@ -60,6 +60,7 @@ object PluginManager {
// region LOADING
private fun <P : Plugin, D : PluginDescription> PluginLoader<P, D>.loadPluginNoEnable(description: D): P {
// TODO: 2020/5/23 HANDLE INITIALIZATION EXCEPTION
return this.load(description).also { resolvedPlugins.add(it) }
}

View File

@ -6,7 +6,10 @@ import net.mamoe.mirai.console.plugins.AbstractFilePluginLoader
import net.mamoe.mirai.console.plugins.PluginLoadException
import net.mamoe.mirai.console.plugins.PluginsLoader
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.error
import net.mamoe.yamlkt.Yaml
import java.io.File
import java.net.URL
import kotlin.coroutines.CoroutineContext
import kotlin.reflect.full.createInstance
@ -38,14 +41,19 @@ object JarPluginLoader : AbstractFilePluginLoader<JvmPlugin, JvmPluginDescriptio
override fun getPluginDescription(plugin: JvmPlugin): JvmPluginDescription = plugin.description
override fun Sequence<File>.mapToDescription(): List<JvmPluginDescription> {
TODO(
"""
CHECK IS JAR FILE AND CAN BE READ
READ JAR FILE, EXTRACT PLUGIN DESCRIPTION
SET JvmPluginDescription._file
RETURN PLUGIN
""".trimIndent()
)
return this.associateWith { URL("jar:${it.absolutePath}!/plugin.yml") }.mapNotNull { (file, url) ->
kotlin.runCatching {
url.readText()
}.fold(
onSuccess = { yaml ->
Yaml.nonStrict.parse(JvmPluginDescription.serializer(), yaml)
},
onFailure = {
logger.error("Cannot load plugin file ${file.name}", it)
null
}
)?.also { it._file = file }
}
}
@Throws(PluginLoadException::class)
@ -68,10 +76,7 @@ object JarPluginLoader : AbstractFilePluginLoader<JvmPlugin, JvmPluginDescriptio
} else main.onLoad()
main
}.getOrElse {
throw PluginLoadException(
"Exception while loading ${description.name}",
it
)
throw PluginLoadException("Exception while loading ${description.name}", it)
}
override fun enable(plugin: JvmPlugin) {

View File

@ -15,9 +15,10 @@ class JvmPluginDescription internal constructor(
override val name: String,
@SerialName("main")
val mainClassName: String,
override val author: String,
override val author: String = "",
override val version: String,
override val info: String,
override val info: String = "",
@SerialName("depends")
override val dependencies: List<PluginDependency>
) : PluginDescription, FilePluginDescription {

View File

@ -9,17 +9,32 @@
package net.mamoe.mirai.console.plugins
import kotlinx.serialization.Serializable
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import java.io.File
/** 插件类型 */
@Serializable(with = PluginKind.Serializer::class)
enum class PluginKind {
/** 表示此插件提供一个 [PluginLoader], 应在加载其他 [NORMAL] 类型插件前加载 */
LOADER,
/** 表示此插件为一个通常的插件, 按照正常的依赖关系加载. */
NORMAL
NORMAL;
companion object Serializer : KSerializer<PluginKind> {
override val descriptor: SerialDescriptor get() = String.serializer().descriptor
override fun deserialize(decoder: Decoder): PluginKind {
val name = String.serializer().deserialize(decoder)
return values().firstOrNull { it.name.equals(name, ignoreCase = true) } ?: NORMAL
}
override fun serialize(encoder: Encoder, value: PluginKind) {
return String.serializer().serialize(encoder, value.toString())
}
}
}
/**