Rework JvmPluginDescription, Introduce JvmMemoryPluginDescription, JvmPluginDescriptionImpl.

This commit is contained in:
Him188 2020-08-20 20:12:50 +08:00
parent a6d98426e3
commit 61a42d9370
3 changed files with 29 additions and 11 deletions

View File

@ -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<File>.mapToDescription(): List<JvmPluginDescription> {
override fun Sequence<File>.mapToDescription(): List<JvmPluginDescriptionImpl> {
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<JvmPluginDescription, JvmPlugin> {
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<JvmPlugin, JvmPlugin> {
}.getOrElse {
throw PluginLoadException("Exception while loading ${description.name}", it)
}
}
override fun enable(plugin: JvmPlugin) {
ensureActive()

View File

@ -56,7 +56,7 @@ public enum class PluginKind {
}
/** 插件的一个依赖的信息 */
@Serializable
@Serializable(with = PluginDependency.SmartSerializer::class)
public data class PluginDependency(
/** 依赖插件名 */
public val name: String,

View File

@ -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")