mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-10 18:40:15 +08:00
Introduce ExtensionPoint, Add extension points PluginLoaderProvider and BotConfigurationAlterer
This commit is contained in:
parent
e92d063405
commit
bbf36e164d
@ -18,6 +18,8 @@ import kotlinx.coroutines.Job
|
||||
import net.mamoe.mirai.Bot
|
||||
import net.mamoe.mirai.console.MiraiConsole.INSTANCE
|
||||
import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start
|
||||
import net.mamoe.mirai.console.extensions.BotConfigurationAlterer
|
||||
import net.mamoe.mirai.console.extensions.foldExtensions
|
||||
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
|
||||
import net.mamoe.mirai.console.plugin.PluginLoader
|
||||
import net.mamoe.mirai.console.plugin.PluginManager
|
||||
@ -101,6 +103,8 @@ public interface MiraiConsole : CoroutineScope {
|
||||
* 调用 [Bot.login] 可登录.
|
||||
*
|
||||
* @see Bot.botInstances 获取现有 [Bot] 实例列表
|
||||
*
|
||||
* @see BotConfigurationAl
|
||||
*/
|
||||
// don't static
|
||||
@ConsoleExperimentalAPI("This is a low-level API and might be removed in the future.")
|
||||
@ -120,7 +124,7 @@ public interface MiraiConsole : CoroutineScope {
|
||||
|
||||
@Suppress("UNREACHABLE_CODE")
|
||||
private fun addBotImpl(id: Long, password: Any, configuration: BotConfiguration.() -> Unit = {}): Bot {
|
||||
val config: BotConfiguration.() -> Unit = {
|
||||
var config = BotConfiguration().apply {
|
||||
fileBasedDeviceInfo()
|
||||
redirectNetworkLogToDirectory()
|
||||
parentCoroutineContext = MiraiConsole.childScopeContext("Bot $id")
|
||||
@ -128,6 +132,11 @@ public interface MiraiConsole : CoroutineScope {
|
||||
this.loginSolver = MiraiConsoleImplementationBridge.createLoginSolver(id, this)
|
||||
configuration()
|
||||
}
|
||||
|
||||
config = BotConfigurationAlterer.foldExtensions(config) { acc, extension ->
|
||||
extension.alterConfiguration(id, acc)
|
||||
}
|
||||
|
||||
return when (password) {
|
||||
is ByteArray -> Bot(id, password, config)
|
||||
is String -> Bot(id, password, config)
|
||||
|
@ -33,9 +33,9 @@ import net.mamoe.mirai.console.internal.command.qualifiedNameOrTip
|
||||
import net.mamoe.mirai.console.internal.data.castOrNull
|
||||
import net.mamoe.mirai.console.internal.plugin.rootCauseOrSelf
|
||||
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
||||
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
|
||||
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScopeContext
|
||||
import net.mamoe.mirai.console.util.MessageScope
|
||||
import net.mamoe.mirai.console.util.childScope
|
||||
import net.mamoe.mirai.console.util.childScopeContext
|
||||
import net.mamoe.mirai.contact.*
|
||||
import net.mamoe.mirai.message.*
|
||||
import net.mamoe.mirai.message.data.Message
|
||||
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 2020 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
@file:Suppress("unused", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
|
||||
|
||||
package net.mamoe.mirai.console.extensions
|
||||
|
||||
import net.mamoe.mirai.console.internal.data.kClassQualifiedNameOrTip
|
||||
import net.mamoe.mirai.console.plugin.Plugin
|
||||
import net.mamoe.mirai.console.plugin.PluginLoader
|
||||
import net.mamoe.mirai.console.plugin.name
|
||||
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
import kotlin.contracts.contract
|
||||
import kotlin.internal.LowPriorityInOverloadResolution
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
@ConsoleExperimentalAPI
|
||||
public open class AbstractExtensionPoint<T : Any>(
|
||||
@ConsoleExperimentalAPI
|
||||
public val type: KClass<T>
|
||||
) {
|
||||
|
||||
@ConsoleExperimentalAPI
|
||||
public data class ExtensionRegistry<T>(
|
||||
public val plugin: Plugin,
|
||||
public val extension: T
|
||||
)
|
||||
|
||||
private val instances: MutableSet<ExtensionRegistry<T>> = CopyOnWriteArraySet()
|
||||
|
||||
@Synchronized
|
||||
@ConsoleExperimentalAPI
|
||||
public fun registerExtension(plugin: Plugin, extension: T) {
|
||||
require(plugin.isEnabled) { "Plugin $plugin must be enabled before registering an extension." }
|
||||
instances.add(ExtensionRegistry(plugin, extension))
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
internal fun getExtensions(): Set<ExtensionRegistry<T>> = instances
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 在调用一个 extension 时遇到的异常.
|
||||
*
|
||||
* @see PluginLoader.load
|
||||
* @see PluginLoader.enable
|
||||
* @see PluginLoader.disable
|
||||
* @see PluginLoader.description
|
||||
*/
|
||||
@ConsoleExperimentalAPI
|
||||
public open class ExtensionException : RuntimeException {
|
||||
public constructor() : super()
|
||||
public constructor(message: String?) : super(message)
|
||||
public constructor(message: String?, cause: Throwable?) : super(message, cause)
|
||||
public constructor(cause: Throwable?) : super(cause)
|
||||
}
|
||||
|
||||
internal inline fun <T : Any> AbstractExtensionPoint<T>.withExtensions(block: T.() -> Unit) {
|
||||
return withExtensions { _ -> block() }
|
||||
}
|
||||
|
||||
@LowPriorityInOverloadResolution
|
||||
internal inline fun <T : Any> AbstractExtensionPoint<T>.withExtensions(block: T.(plugin: Plugin) -> Unit) {
|
||||
contract {
|
||||
callsInPlace(block)
|
||||
}
|
||||
for ((plugin, extension) in this.getExtensions()) {
|
||||
kotlin.runCatching {
|
||||
block.invoke(extension, plugin)
|
||||
}.getOrElse { throwable ->
|
||||
throwExtensionException(extension, plugin, throwable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun <T : Any, E> AbstractExtensionPoint<T>.foldExtensions(
|
||||
initial: E,
|
||||
block: (acc: E, extension: T) -> E
|
||||
): E {
|
||||
contract {
|
||||
callsInPlace(block)
|
||||
}
|
||||
var e: E = initial
|
||||
for ((plugin, extension) in this.getExtensions()) {
|
||||
kotlin.runCatching {
|
||||
e = block.invoke(e, extension)
|
||||
}.getOrElse { throwable ->
|
||||
throwExtensionException(extension, plugin, throwable)
|
||||
}
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
internal fun <T : Any> AbstractExtensionPoint<T>.throwExtensionException(
|
||||
extension: T,
|
||||
plugin: Plugin,
|
||||
throwable: Throwable
|
||||
) {
|
||||
throw ExtensionException(
|
||||
"Exception while executing extension ${extension.kClassQualifiedNameOrTip} from ${plugin.name}, registered for ${this.type.qualifiedName}",
|
||||
throwable
|
||||
)
|
||||
}
|
||||
|
||||
internal inline fun <T : Any> AbstractExtensionPoint<T>.useExtensions(block: (extension: T) -> Unit): Unit =
|
||||
withExtensions(block)
|
||||
|
||||
@LowPriorityInOverloadResolution
|
||||
internal inline fun <T : Any> AbstractExtensionPoint<T>.useExtensions(block: (extension: T, plugin: Plugin) -> Unit): Unit =
|
||||
withExtensions(block)
|
@ -0,0 +1,28 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package net.mamoe.mirai.console.extensions
|
||||
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
|
||||
import net.mamoe.mirai.utils.BotConfiguration
|
||||
|
||||
/**
|
||||
* [MiraiConsole.addBot] 时的 [BotConfiguration] 修改扩展
|
||||
*
|
||||
* @see MiraiConsole.addBot
|
||||
*/
|
||||
@ConsoleExperimentalAPI
|
||||
public interface BotConfigurationAlterer {
|
||||
|
||||
/**
|
||||
* 修改 [configuration], 返回修改完成的 [BotConfiguration]
|
||||
*/
|
||||
@JvmDefault
|
||||
public fun alterConfiguration(
|
||||
botId: Long,
|
||||
configuration: BotConfiguration
|
||||
): BotConfiguration = configuration
|
||||
|
||||
public companion object ExtensionPoint :
|
||||
AbstractExtensionPoint<BotConfigurationAlterer>(BotConfigurationAlterer::class)
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package net.mamoe.mirai.console.extensions
|
||||
|
||||
import net.mamoe.mirai.console.internal.util.PluginServiceHelper
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
// not public now
|
||||
internal object ServiceContainer {
|
||||
private val instances: MutableMap<KClass<*>, List<*>> = mutableMapOf()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
@JvmStatic
|
||||
@Synchronized
|
||||
fun <T : Any> getService(clazz: KClass<T>): List<T> {
|
||||
instances[clazz]?.let { return it as List<T> }
|
||||
PluginServiceHelper.loadAllServicesFromMemoryAndPluginClassLoaders(clazz).let {
|
||||
instances[clazz] = it
|
||||
return it
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> getService(): List<T> = getService(T::class)
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package net.mamoe.mirai.console.extensions
|
||||
|
||||
import net.mamoe.mirai.console.plugin.PluginLoader
|
||||
|
||||
/**
|
||||
* 提供扩展 [PluginLoader]
|
||||
*/
|
||||
public interface PluginLoaderProvider {
|
||||
public val instance: PluginLoader<*, *>
|
||||
|
||||
public companion object ExtensionPoint : AbstractExtensionPoint<PluginLoaderProvider>(PluginLoaderProvider::class)
|
||||
}
|
@ -72,3 +72,6 @@ internal fun KClass<*>.findValueName(): String =
|
||||
internal fun Int.isOdd() = this and 0b1 != 0
|
||||
|
||||
internal val KProperty<*>.valueName: String get() = this.findAnnotation<ValueName>()?.value ?: this.name
|
||||
|
||||
internal inline val Any.kClassQualifiedName: String? get() = this::class.qualifiedName
|
||||
internal inline val Any.kClassQualifiedNameOrTip: String get() = this::class.qualifiedNameOrTip
|
@ -15,12 +15,12 @@ import kotlinx.coroutines.ensureActive
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.data.PluginDataStorage
|
||||
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
|
||||
import net.mamoe.mirai.console.internal.util.ServiceHelper.findServices
|
||||
import net.mamoe.mirai.console.internal.util.ServiceHelper.loadAllServices
|
||||
import net.mamoe.mirai.console.internal.util.PluginServiceHelper.findServices
|
||||
import net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadAllServices
|
||||
import net.mamoe.mirai.console.plugin.AbstractFilePluginLoader
|
||||
import net.mamoe.mirai.console.plugin.PluginLoadException
|
||||
import net.mamoe.mirai.console.plugin.jvm.*
|
||||
import net.mamoe.mirai.console.util.childScope
|
||||
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
|
||||
import net.mamoe.mirai.utils.MiraiLogger
|
||||
import net.mamoe.mirai.utils.info
|
||||
import java.io.File
|
||||
|
@ -15,16 +15,16 @@ import kotlinx.atomicfu.locks.withLock
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import net.mamoe.mirai.console.MiraiConsole
|
||||
import net.mamoe.mirai.console.extensions.PluginLoaderProvider
|
||||
import net.mamoe.mirai.console.extensions.useExtensions
|
||||
import net.mamoe.mirai.console.internal.data.cast
|
||||
import net.mamoe.mirai.console.internal.data.mkdir
|
||||
import net.mamoe.mirai.console.internal.util.ServiceHelper.findServices
|
||||
import net.mamoe.mirai.console.internal.util.ServiceHelper.loadAllServices
|
||||
import net.mamoe.mirai.console.plugin.*
|
||||
import net.mamoe.mirai.console.plugin.description.PluginDependency
|
||||
import net.mamoe.mirai.console.plugin.description.PluginDescription
|
||||
import net.mamoe.mirai.console.plugin.description.PluginKind
|
||||
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
|
||||
import net.mamoe.mirai.console.util.childScope
|
||||
import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope
|
||||
import net.mamoe.mirai.utils.info
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
@ -144,16 +144,10 @@ internal object PluginManagerImpl : PluginManager, CoroutineScope by MiraiConsol
|
||||
|
||||
private fun loadPluginLoaderProvidedByPlugins() {
|
||||
loadersLock.withLock {
|
||||
JarPluginLoaderImpl.classLoaders.asSequence()
|
||||
.flatMap { pluginClassLoader ->
|
||||
pluginClassLoader.findServices<PluginLoader<*, *>>().loadAllServices()
|
||||
}
|
||||
.onEach { loaded ->
|
||||
logger.info { "Successfully loaded PluginLoader ${loaded}." }
|
||||
}
|
||||
.forEach {
|
||||
_pluginLoaders.add(it)
|
||||
}
|
||||
PluginLoaderProvider.useExtensions {
|
||||
logger.info { "Loaded PluginLoader ${it.instance} from $" }
|
||||
_pluginLoaders.add(it.instance)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,8 +11,10 @@ package net.mamoe.mirai.console.internal.util
|
||||
|
||||
import net.mamoe.mirai.console.internal.data.cast
|
||||
import net.mamoe.mirai.console.internal.data.createInstanceOrNull
|
||||
import net.mamoe.mirai.console.internal.plugin.JarPluginLoaderImpl
|
||||
import java.io.InputStream
|
||||
import java.lang.reflect.Modifier
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import java.lang.reflect.Member as JReflectionMember
|
||||
|
||||
@ -22,7 +24,7 @@ internal class ServiceList<T>(
|
||||
internal val delegate: List<String>
|
||||
)
|
||||
|
||||
internal object ServiceHelper {
|
||||
internal object PluginServiceHelper {
|
||||
inline fun <reified T : Any> ClassLoader.findServices(): ServiceList<T> = findServices(T::class)
|
||||
|
||||
fun <T : Any> ClassLoader.findServices(vararg serviceTypes: KClass<out T>): ServiceList<T> =
|
||||
@ -60,6 +62,11 @@ internal object ServiceHelper {
|
||||
)*/
|
||||
} as T?
|
||||
}
|
||||
|
||||
fun <T : Any> loadAllServicesFromMemoryAndPluginClassLoaders(service: KClass<T>): List<T> {
|
||||
val list = ServiceLoader.load(service.java, this::class.java.classLoader).toList()
|
||||
return list + JarPluginLoaderImpl.classLoaders.flatMap { it.findServices(service).loadAllServices() }
|
||||
}
|
||||
}
|
||||
|
||||
internal fun JReflectionMember.isStatic(): Boolean = this.modifiers.and(Modifier.STATIC) != 0
|
Loading…
Reference in New Issue
Block a user