mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Implement plugin recognizing
This commit is contained in:
parent
8cc09c77fb
commit
f6af60191b
@ -60,6 +60,7 @@ object PluginManager {
|
|||||||
// region LOADING
|
// region LOADING
|
||||||
|
|
||||||
private fun <P : Plugin, D : PluginDescription> PluginLoader<P, D>.loadPluginNoEnable(description: D): P {
|
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) }
|
return this.load(description).also { resolvedPlugins.add(it) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,10 @@ import net.mamoe.mirai.console.plugins.AbstractFilePluginLoader
|
|||||||
import net.mamoe.mirai.console.plugins.PluginLoadException
|
import net.mamoe.mirai.console.plugins.PluginLoadException
|
||||||
import net.mamoe.mirai.console.plugins.PluginsLoader
|
import net.mamoe.mirai.console.plugins.PluginsLoader
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
|
import net.mamoe.mirai.utils.error
|
||||||
|
import net.mamoe.yamlkt.Yaml
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.net.URL
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.reflect.full.createInstance
|
import kotlin.reflect.full.createInstance
|
||||||
|
|
||||||
@ -38,14 +41,19 @@ object JarPluginLoader : AbstractFilePluginLoader<JvmPlugin, JvmPluginDescriptio
|
|||||||
override fun getPluginDescription(plugin: JvmPlugin): JvmPluginDescription = plugin.description
|
override fun getPluginDescription(plugin: JvmPlugin): JvmPluginDescription = plugin.description
|
||||||
|
|
||||||
override fun Sequence<File>.mapToDescription(): List<JvmPluginDescription> {
|
override fun Sequence<File>.mapToDescription(): List<JvmPluginDescription> {
|
||||||
TODO(
|
return this.associateWith { URL("jar:${it.absolutePath}!/plugin.yml") }.mapNotNull { (file, url) ->
|
||||||
"""
|
kotlin.runCatching {
|
||||||
CHECK IS JAR FILE AND CAN BE READ
|
url.readText()
|
||||||
READ JAR FILE, EXTRACT PLUGIN DESCRIPTION
|
}.fold(
|
||||||
SET JvmPluginDescription._file
|
onSuccess = { yaml ->
|
||||||
RETURN PLUGIN
|
Yaml.nonStrict.parse(JvmPluginDescription.serializer(), yaml)
|
||||||
""".trimIndent()
|
},
|
||||||
)
|
onFailure = {
|
||||||
|
logger.error("Cannot load plugin file ${file.name}", it)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
)?.also { it._file = file }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Throws(PluginLoadException::class)
|
@Throws(PluginLoadException::class)
|
||||||
@ -68,10 +76,7 @@ object JarPluginLoader : AbstractFilePluginLoader<JvmPlugin, JvmPluginDescriptio
|
|||||||
} else main.onLoad()
|
} else main.onLoad()
|
||||||
main
|
main
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
throw PluginLoadException(
|
throw PluginLoadException("Exception while loading ${description.name}", it)
|
||||||
"Exception while loading ${description.name}",
|
|
||||||
it
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun enable(plugin: JvmPlugin) {
|
override fun enable(plugin: JvmPlugin) {
|
||||||
|
@ -15,9 +15,10 @@ class JvmPluginDescription internal constructor(
|
|||||||
override val name: String,
|
override val name: String,
|
||||||
@SerialName("main")
|
@SerialName("main")
|
||||||
val mainClassName: String,
|
val mainClassName: String,
|
||||||
override val author: String,
|
override val author: String = "",
|
||||||
override val version: String,
|
override val version: String,
|
||||||
override val info: String,
|
override val info: String = "",
|
||||||
|
@SerialName("depends")
|
||||||
override val dependencies: List<PluginDependency>
|
override val dependencies: List<PluginDependency>
|
||||||
) : PluginDescription, FilePluginDescription {
|
) : PluginDescription, FilePluginDescription {
|
||||||
|
|
||||||
|
@ -9,17 +9,32 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.console.plugins
|
package net.mamoe.mirai.console.plugins
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.*
|
||||||
|
import kotlinx.serialization.builtins.serializer
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
|
|
||||||
/** 插件类型 */
|
/** 插件类型 */
|
||||||
|
@Serializable(with = PluginKind.Serializer::class)
|
||||||
enum class PluginKind {
|
enum class PluginKind {
|
||||||
/** 表示此插件提供一个 [PluginLoader], 应在加载其他 [NORMAL] 类型插件前加载 */
|
/** 表示此插件提供一个 [PluginLoader], 应在加载其他 [NORMAL] 类型插件前加载 */
|
||||||
LOADER,
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user