diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleImplementation.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleImplementation.kt index a1fd6347a..c2cab53e7 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleImplementation.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleImplementation.kt @@ -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) + } } } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extension/PluginComponentStorage.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extension/PluginComponentStorage.kt index 2a6a8a63f..74acd845c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extension/PluginComponentStorage.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extension/PluginComponentStorage.kt @@ -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 }) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/BotConfigurationAlterer.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/BotConfigurationAlterer.kt index 45ce4f680..642d5ea9b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/BotConfigurationAlterer.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/BotConfigurationAlterer.kt @@ -21,6 +21,7 @@ import net.mamoe.mirai.utils.BotConfiguration * * @see MiraiConsole.addBot */ +@Suppress("SpellCheckingInspection") // alterer public fun interface BotConfigurationAlterer : FunctionExtension { /** diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PluginLoaderProvider.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PluginLoaderProvider.kt index 510a1119b..3b96f4ac6 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PluginLoaderProvider.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PluginLoaderProvider.kt @@ -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> { public companion object ExtensionPoint : AbstractExtensionPoint(PluginLoaderProvider::class) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PostStartupExtension.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PostStartupExtension.kt index ee8a97f45..f18185e17 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PostStartupExtension.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/PostStartupExtension.kt @@ -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::class) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt index e3ebe4c50..fd587247b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/MiraiConsoleImplementationBridge.kt @@ -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." } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/loader/PluginLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/loader/PluginLoader.kt index 40a2ad152..208f4e743 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/loader/PluginLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/loader/PluginLoader.kt @@ -37,6 +37,7 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader * 直接实现接口 [PluginLoader] 或 [FilePluginLoader], 并注册 [PluginLoaderProvider] * * @see JvmPluginLoader Jar 插件加载器 + * @see PluginLoaderProvider 扩展 */ public interface PluginLoader

{ /**