diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt index 1902510e6..45b4ea85a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt @@ -17,6 +17,7 @@ import net.mamoe.mirai.console.plugin.PluginLoadException import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription +import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionImpl import net.mamoe.mirai.console.setting.SettingStorage import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.utils.MiraiLogger @@ -56,14 +57,14 @@ internal object JarPluginLoaderImpl : override val JvmPlugin.description: JvmPluginDescription get() = this.description - override fun Sequence.mapToDescription(): List { + override fun Sequence.mapToDescription(): List { return this.associateWith { URI("jar:file:${it.absolutePath.replace('\\', '/')}!/plugin.yml").toURL() } .mapNotNull { (file, url) -> kotlin.runCatching { url.readText() }.fold( onSuccess = { yaml -> - Yaml.nonStrict.decodeFromString(JvmPluginDescription.serializer(), yaml) + Yaml.nonStrict.decodeFromString(JvmPluginDescriptionImpl.serializer(), yaml) }, onFailure = { logger.error("Cannot load plugin file ${file.name}", it) @@ -73,10 +74,12 @@ internal object JarPluginLoaderImpl : } } - @Suppress("RemoveExplicitTypeArguments") // until Kotlin 1.4 NI @Throws(PluginLoadException::class) - override fun load(description: JvmPluginDescription): JvmPlugin = - description.runCatching { + override fun load(description: JvmPluginDescription): JvmPlugin { + require(description is JvmPluginDescriptionImpl) { + "Illegal description: ${description::class.qualifiedName}" + } + return description.runCatching { ensureActive() val main = classLoader.loadPluginMainClassByJarFile( pluginName = name, @@ -98,9 +101,10 @@ internal object JarPluginLoaderImpl : main.internalOnLoad() } else main.onLoad() main - }.getOrElse { + }.getOrElse { throw PluginLoadException("Exception while loading ${description.name}", it) } + } override fun enable(plugin: JvmPlugin) { ensureActive() diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description.kt index ae7cfe85a..f7ef9c8c0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description.kt @@ -56,7 +56,7 @@ public enum class PluginKind { } /** 插件的一个依赖的信息 */ -@Serializable +@Serializable(with = PluginDependency.SmartSerializer::class) public data class PluginDependency( /** 依赖插件名 */ public val name: String, diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescriptionImpl.kt similarity index 75% rename from backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt rename to backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescriptionImpl.kt index 9d1d193d8..100e48294 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescription.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPluginDescriptionImpl.kt @@ -14,14 +14,28 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import net.mamoe.mirai.console.internal.setting.SemverAsStringSerializerLoose -import net.mamoe.mirai.console.plugin.FilePluginDescription import net.mamoe.mirai.console.plugin.PluginDependency import net.mamoe.mirai.console.plugin.PluginDescription import net.mamoe.mirai.console.plugin.PluginKind +import net.mamoe.mirai.utils.MiraiExperimentalAPI import java.io.File +@MiraiExperimentalAPI @Serializable -public class JvmPluginDescription internal constructor( +public data class JvmMemoryPluginDescription( + public override val kind: PluginKind, + public override val name: String, + public override val author: String, + public override val version: @Serializable(with = SemverAsStringSerializerLoose::class) Semver, + public override val info: String, + public override val dependencies: List<@Serializable(with = PluginDependency.SmartSerializer::class) PluginDependency> +) : JvmPluginDescription + +public interface JvmPluginDescription : PluginDescription + +@MiraiExperimentalAPI +@Serializable +public class JvmPluginDescriptionImpl internal constructor( public override val kind: PluginKind = PluginKind.NORMAL, public override val name: String, @SerialName("main") @@ -31,7 +45,7 @@ public class JvmPluginDescription internal constructor( public override val info: String = "", @SerialName("depends") public override val dependencies: List<@Serializable(with = PluginDependency.SmartSerializer::class) PluginDependency> = listOf() -) : PluginDescription, FilePluginDescription { +) : JvmPluginDescription { /** * 在手动实现时使用这个构造器. @@ -45,7 +59,7 @@ public class JvmPluginDescription internal constructor( this._file = file } - public override val file: File + public val file: File get() = _file ?: error("Internal error: JvmPluginDescription(name=$name)._file == null")