mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-27 00:30:17 +08:00
Support JvmMemoryPluginDescription for JarPluginLoader.load; Add JvmMemoryPluginDescription.instance
This commit is contained in:
parent
a8b5299ba8
commit
523c34f01f
@ -16,10 +16,7 @@ import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
|
|||||||
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
||||||
import net.mamoe.mirai.console.plugin.AbstractFilePluginLoader
|
import net.mamoe.mirai.console.plugin.AbstractFilePluginLoader
|
||||||
import net.mamoe.mirai.console.plugin.PluginLoadException
|
import net.mamoe.mirai.console.plugin.PluginLoadException
|
||||||
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
|
import net.mamoe.mirai.console.plugin.jvm.*
|
||||||
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.util.ConsoleExperimentalAPI
|
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
||||||
import net.mamoe.mirai.utils.MiraiLogger
|
import net.mamoe.mirai.utils.MiraiLogger
|
||||||
import net.mamoe.yamlkt.Yaml
|
import net.mamoe.yamlkt.Yaml
|
||||||
@ -79,23 +76,29 @@ internal object JarPluginLoaderImpl :
|
|||||||
|
|
||||||
@Throws(PluginLoadException::class)
|
@Throws(PluginLoadException::class)
|
||||||
override fun load(description: JvmPluginDescription): JvmPlugin {
|
override fun load(description: JvmPluginDescription): JvmPlugin {
|
||||||
require(description is JvmPluginDescriptionImpl) {
|
val main = when (description) {
|
||||||
"Illegal description: ${description::class.qualifiedName}"
|
is JvmMemoryPluginDescription -> {
|
||||||
|
description.instance
|
||||||
|
}
|
||||||
|
is JvmPluginDescriptionImpl -> with(description) {
|
||||||
|
classLoader.loadPluginMainClassByJarFile(
|
||||||
|
pluginName = name,
|
||||||
|
mainClass = mainClassName,
|
||||||
|
jarFile = file
|
||||||
|
).kotlin.run {
|
||||||
|
objectInstance
|
||||||
|
?: createInstanceOrNull()
|
||||||
|
?: (java.constructors + java.declaredConstructors)
|
||||||
|
.firstOrNull { it.parameterCount == 0 }
|
||||||
|
?.apply { kotlin.runCatching { isAccessible = true } }
|
||||||
|
?.newInstance()
|
||||||
|
} ?: error("No Kotlin object or public no-arg constructor found for $mainClassName")
|
||||||
|
}
|
||||||
|
else -> error("Illegal description: ${description::class.qualifiedName}")
|
||||||
}
|
}
|
||||||
return description.runCatching {
|
|
||||||
|
description.runCatching {
|
||||||
ensureActive()
|
ensureActive()
|
||||||
val main = classLoader.loadPluginMainClassByJarFile(
|
|
||||||
pluginName = name,
|
|
||||||
mainClass = mainClassName,
|
|
||||||
jarFile = file
|
|
||||||
).kotlin.run {
|
|
||||||
objectInstance
|
|
||||||
?: createInstanceOrNull()
|
|
||||||
?: (java.constructors + java.declaredConstructors)
|
|
||||||
.firstOrNull { it.parameterCount == 0 }
|
|
||||||
?.apply { kotlin.runCatching { isAccessible = true } }
|
|
||||||
?.newInstance()
|
|
||||||
} ?: error("No Kotlin object or public no-arg constructor found for $mainClassName")
|
|
||||||
|
|
||||||
check(main is JvmPlugin) { "The main class of Jar plugin must extend JvmPlugin, recommended JavaPlugin or KotlinPlugin" }
|
check(main is JvmPlugin) { "The main class of Jar plugin must extend JvmPlugin, recommended JavaPlugin or KotlinPlugin" }
|
||||||
|
|
||||||
@ -103,7 +106,8 @@ internal object JarPluginLoaderImpl :
|
|||||||
main._description = description
|
main._description = description
|
||||||
main.internalOnLoad()
|
main.internalOnLoad()
|
||||||
} else main.onLoad()
|
} else main.onLoad()
|
||||||
main
|
|
||||||
|
return main
|
||||||
}.getOrElse {
|
}.getOrElse {
|
||||||
throw PluginLoadException("Exception while loading ${description.name}", it)
|
throw PluginLoadException("Exception while loading ${description.name}", it)
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,8 @@ public data class JvmMemoryPluginDescription(
|
|||||||
public override val author: String,
|
public override val author: String,
|
||||||
public override val version: Semver,
|
public override val version: Semver,
|
||||||
public override val info: String,
|
public override val info: String,
|
||||||
public override val dependencies: List<PluginDependency>
|
public override val dependencies: List<PluginDependency>,
|
||||||
|
val instance: JvmPlugin
|
||||||
) : JvmPluginDescription {
|
) : JvmPluginDescription {
|
||||||
init {
|
init {
|
||||||
require(!name.contains(':')) { "':' is forbidden in plugin name" }
|
require(!name.contains(':')) { "':' is forbidden in plugin name" }
|
||||||
|
Loading…
Reference in New Issue
Block a user