Review extensions

This commit is contained in:
Him188 2020-10-25 21:10:45 +08:00
parent 1f790158e6
commit 077e4055cc
7 changed files with 38 additions and 3 deletions

View File

@ -30,6 +30,7 @@ import java.util.*
import java.util.concurrent.locks.ReentrantLock
import kotlin.annotation.AnnotationTarget.*
import kotlin.coroutines.CoroutineContext
import kotlin.system.exitProcess
/**
@ -176,7 +177,7 @@ public interface MiraiConsoleImplementation : CoroutineScope {
/**
* 可由前端调用, 获取当前的 [MiraiConsoleImplementation] 实例
*
* 必须在 [start] 之后才能使用.
* 必须在 [start] 之后才能使用, 否则抛出 [UninitializedPropertyAccessException]
*/
@JvmStatic
@ConsoleFrontEndImplementation
@ -189,7 +190,22 @@ public interface MiraiConsoleImplementation : CoroutineScope {
public fun MiraiConsoleImplementation.start(): Unit = initLock.withLock {
if (::instance.isInitialized) error("Mirai Console is already initialized.")
this@Companion.instance = this
MiraiConsoleImplementationBridge.doStart()
kotlin.runCatching {
MiraiConsoleImplementationBridge.doStart()
}.onFailure { e ->
kotlin.runCatching {
MiraiConsole.mainLogger.error("Failed to init MiraiConsole.", e)
}.onFailure {
e.printStackTrace()
}
kotlin.runCatching {
MiraiConsole.cancel()
}.onFailure {
it.printStackTrace()
}
exitProcess(1)
}
}
}
}

View File

@ -56,6 +56,7 @@ public class PluginComponentStorage(
public fun contributeSingletonExtensionSelector(lazyInstance: () -> SingletonExtensionSelector): Unit =
contribute(SingletonExtensionSelector, plugin, lazyInstance)
@Suppress("SpellCheckingInspection") // alterer
/** 注册一个 [BotConfigurationAlterer] */
public fun contributeBotConfigurationAlterer(instance: BotConfigurationAlterer): Unit =
contribute(BotConfigurationAlterer, plugin, lazyInstance = { instance })

View File

@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.BotConfiguration
*
* @see MiraiConsole.addBot
*/
@Suppress("SpellCheckingInspection") // alterer
public fun interface BotConfigurationAlterer : FunctionExtension {
/**

View File

@ -12,12 +12,19 @@ package net.mamoe.mirai.console.extensions
import net.mamoe.mirai.console.extension.AbstractExtensionPoint
import net.mamoe.mirai.console.extension.Extension
import net.mamoe.mirai.console.extension.InstanceExtension
import net.mamoe.mirai.console.extension.PluginComponentStorage
import net.mamoe.mirai.console.plugin.loader.PluginLoader
/**
* 提供扩展 [PluginLoader]
*
* @see PluginComponentStorage.contributePluginLoader
*
*
* @see Extension
* @see PluginLoader
*
* @see LazyPluginLoaderProviderImpl
*/
public interface PluginLoaderProvider : InstanceExtension<PluginLoader<*, *>> {
public companion object ExtensionPoint : AbstractExtensionPoint<PluginLoaderProvider>(PluginLoaderProvider::class)

View File

@ -9,8 +9,11 @@
package net.mamoe.mirai.console.extensions
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.extension.AbstractExtensionPoint
import net.mamoe.mirai.console.extension.ExtensionException
import net.mamoe.mirai.console.extension.FunctionExtension
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
/**
* Console 启动完成后立即在主线程调用的扩展. 用于进行一些必要的延迟初始化.
@ -20,7 +23,13 @@ import net.mamoe.mirai.console.extension.FunctionExtension
public fun interface PostStartupExtension : FunctionExtension {
/**
* 将在 Console 主线程执行.
*
* @throws Exception 所有抛出的 [Exception] 都会被捕获并包装为 [ExtensionException] 抛出, 并停止 [MiraiConsole]
*
* #### 内部实现细节
* [MiraiConsoleImplementationBridge.doStart] 所有 [MiraiConsoleImplementationBridge.phase] 执行完成后顺序调用.
*/
@Throws(Exception::class)
public operator fun invoke()
public companion object ExtensionPoint : AbstractExtensionPoint<PostStartupExtension>(PostStartupExtension::class)

View File

@ -217,7 +217,7 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, MiraiConsoleI
}
GlobalComponentStorage.run {
PostStartupExtension.useExtensions { it() }
PostStartupExtension.useExtensions { it() } // exceptions thrown will be caught by caller of `doStart`.
}
mainLogger.info { "mirai-console started successfully." }

View File

@ -37,6 +37,7 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader
* 直接实现接口 [PluginLoader] [FilePluginLoader], 并注册 [PluginLoaderProvider]
*
* @see JvmPluginLoader Jar 插件加载器
* @see PluginLoaderProvider 扩展
*/
public interface PluginLoader<P : Plugin, D : PluginDescription> {
/**