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.JarPluginLoader
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription 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.setting.SettingStorage
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
@ -56,14 +57,14 @@ internal object JarPluginLoaderImpl :
override val JvmPlugin.description: JvmPluginDescription override val JvmPlugin.description: JvmPluginDescription
get() = this.description 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() } return this.associateWith { URI("jar:file:${it.absolutePath.replace('\\', '/')}!/plugin.yml").toURL() }
.mapNotNull { (file, url) -> .mapNotNull { (file, url) ->
kotlin.runCatching { kotlin.runCatching {
url.readText() url.readText()
}.fold( }.fold(
onSuccess = { yaml -> onSuccess = { yaml ->
Yaml.nonStrict.decodeFromString(JvmPluginDescription.serializer(), yaml) Yaml.nonStrict.decodeFromString(JvmPluginDescriptionImpl.serializer(), yaml)
}, },
onFailure = { onFailure = {
logger.error("Cannot load plugin file ${file.name}", it) 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) @Throws(PluginLoadException::class)
override fun load(description: JvmPluginDescription): JvmPlugin = override fun load(description: JvmPluginDescription): JvmPlugin {
description.runCatching<JvmPluginDescription, JvmPlugin> { require(description is JvmPluginDescriptionImpl) {
"Illegal description: ${description::class.qualifiedName}"
}
return description.runCatching {
ensureActive() ensureActive()
val main = classLoader.loadPluginMainClassByJarFile( val main = classLoader.loadPluginMainClassByJarFile(
pluginName = name, pluginName = name,
@ -98,9 +101,10 @@ internal object JarPluginLoaderImpl :
main.internalOnLoad() main.internalOnLoad()
} else main.onLoad() } else main.onLoad()
main main
}.getOrElse<JvmPlugin, JvmPlugin> { }.getOrElse {
throw PluginLoadException("Exception while loading ${description.name}", it) throw PluginLoadException("Exception while loading ${description.name}", it)
} }
}
override fun enable(plugin: JvmPlugin) { override fun enable(plugin: JvmPlugin) {
ensureActive() ensureActive()

View File

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

View File

@ -14,14 +14,28 @@ import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import net.mamoe.mirai.console.internal.setting.SemverAsStringSerializerLoose 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.PluginDependency
import net.mamoe.mirai.console.plugin.PluginDescription import net.mamoe.mirai.console.plugin.PluginDescription
import net.mamoe.mirai.console.plugin.PluginKind import net.mamoe.mirai.console.plugin.PluginKind
import net.mamoe.mirai.utils.MiraiExperimentalAPI
import java.io.File import java.io.File
@MiraiExperimentalAPI
@Serializable @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 kind: PluginKind = PluginKind.NORMAL,
public override val name: String, public override val name: String,
@SerialName("main") @SerialName("main")
@ -31,7 +45,7 @@ public class JvmPluginDescription internal constructor(
public override val info: String = "", public override val info: String = "",
@SerialName("depends") @SerialName("depends")
public override val dependencies: List<@Serializable(with = PluginDependency.SmartSerializer::class) PluginDependency> = listOf() 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 this._file = file
} }
public override val file: File public val file: File
get() = _file ?: error("Internal error: JvmPluginDescription(name=$name)._file == null") get() = _file ?: error("Internal error: JvmPluginDescription(name=$name)._file == null")