From f6af60191bc129a5ddb430c9995f58fb8f3a0aa6 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 23 May 2020 20:28:25 +0800 Subject: [PATCH] Implement plugin recognizing --- .../mirai/console/plugins/PluginManager.kt | 1 + .../plugins/builtin/JarPluginLoader.kt | 29 +++++++++++-------- .../plugins/builtin/JvmPluginDescription.kt | 5 ++-- .../mirai/console/plugins/description.kt | 19 ++++++++++-- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt index e91396594..fcba50859 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/PluginManager.kt @@ -60,6 +60,7 @@ object PluginManager { // region LOADING private fun

PluginLoader.loadPluginNoEnable(description: D): P { + // TODO: 2020/5/23 HANDLE INITIALIZATION EXCEPTION return this.load(description).also { resolvedPlugins.add(it) } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/builtin/JarPluginLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/builtin/JarPluginLoader.kt index 084e0bce1..0d132becd 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/builtin/JarPluginLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/builtin/JarPluginLoader.kt @@ -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.mapToDescription(): List { - 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 ) : PluginDescription, FilePluginDescription { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/description.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/description.kt index 15622c178..284025f84 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/description.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugins/description.kt @@ -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 { + 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()) + } + } } /**