diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt index c3576ca47..c5988ece7 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsole.kt @@ -15,6 +15,7 @@ import kotlinx.io.charsets.Charset import net.mamoe.mirai.Bot import net.mamoe.mirai.console.MiraiConsole.INSTANCE import net.mamoe.mirai.console.command.ConsoleCommandOwner +import net.mamoe.mirai.console.command.ConsoleCommandSender import net.mamoe.mirai.console.plugin.PluginLoader import net.mamoe.mirai.console.plugin.center.CuiPluginCenter import net.mamoe.mirai.console.plugin.center.PluginCenter @@ -93,61 +94,42 @@ internal object MiraiConsoleBuildConstants { // auto-filled on build (task :mira internal object MiraiConsoleInternal : CoroutineScope, IMiraiConsole, MiraiConsole { override val pluginCenter: PluginCenter get() = CuiPluginCenter - private val instance: IMiraiConsole - get() = MiraiConsoleInitializer.instance - - /** - * `mirai-console` build 号 - * - * UTC+8 时间 - */ - override val buildDate: Date - get() = MiraiConsoleBuildConstants.buildDate - - /** - * `mirai-console` 版本 - */ + private val instance: IMiraiConsole get() = MiraiConsoleInitializer.instance + override val buildDate: Date get() = MiraiConsoleBuildConstants.buildDate override val version: String get() = MiraiConsoleBuildConstants.version - - /** - * Console 运行路径 - */ override val rootDir: File get() = instance.rootDir - - /** - * Console 前端接口 - */ override val frontEnd: MiraiConsoleFrontEnd get() = instance.frontEnd - /** - * 与前端交互所使用的 Logger - */ @MiraiExperimentalAPI override val mainLogger: MiraiLogger get() = instance.mainLogger - override val coroutineContext: CoroutineContext get() = instance.coroutineContext - override val builtInPluginLoaders: List> get() = instance.builtInPluginLoaders - - override val consoleCommandOwner: ConsoleCommandOwner - get() = instance.consoleCommandOwner + override val consoleCommandOwner: ConsoleCommandOwner get() = instance.consoleCommandOwner + override val consoleCommandSender: ConsoleCommandSender get() = instance.consoleCommandSender init { DefaultLogger = { identity -> this.newLogger(identity) } - this.coroutineContext[Job]!!.invokeOnCompletion { - Bot.botInstances.forEach { kotlin.runCatching { it.close() }.exceptionOrNull()?.let(mainLogger::error) } - } } @MiraiExperimentalAPI override fun newLogger(identity: String?): MiraiLogger = frontEnd.loggerFor(identity) internal fun initialize() { + if (coroutineContext[Job] == null) { + throw IllegalMiraiConsoleImplementationError("The coroutineContext given to MiraiConsole must have a Job in it.") + } + this.coroutineContext[Job]!!.invokeOnCompletion { + Bot.botInstances.forEach { kotlin.runCatching { it.close() }.exceptionOrNull()?.let(mainLogger::error) } + } // Only for initialize } } +class IllegalMiraiConsoleImplementationError( + override val message: String? +) : Error() + // 前端使用 internal interface IMiraiConsole : CoroutineScope { @@ -173,6 +155,9 @@ internal interface IMiraiConsole : CoroutineScope { @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") internal val consoleCommandOwner: ConsoleCommandOwner + + @Suppress("WRONG_MODIFIER_CONTAINING_DECLARATION") + internal val consoleCommandSender: ConsoleCommandSender } /** diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt index 48043cb76..ec6d47082 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/MiraiConsoleFrontEnd.kt @@ -22,13 +22,6 @@ import net.mamoe.mirai.utils.MiraiLogger interface MiraiConsoleFrontEnd { fun loggerFor(identity: String?): MiraiLogger - /** - * 让 UI 层准备接受新增的一个BOT - */ - fun prePushBot( - identity: Long - ) - /** * 让 UI 层接受一个新的bot * */ @@ -41,18 +34,8 @@ interface MiraiConsoleFrontEnd { */ suspend fun requestInput(hint: String): String - - /** - * 让 UI 层更新 bot 管理员的数据 - */ - fun pushBotAdminStatus( - identity: Long, - admins: List - ) - /** * 由 UI 层创建一个 [LoginSolver] */ fun createLoginSolver(): LoginSolver - } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt index 27633a20f..62332426c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt @@ -33,22 +33,20 @@ object JarPluginLoader : AbstractFilePluginLoader logger.error("Unhandled Jar plugin exception: ${throwable.message}", throwable) - } + }.also { init() } } - private val supervisor: Job = coroutineContext[Job]!! - private val classLoader: PluginsLoader = - PluginsLoader(this.javaClass.classLoader) + private val classLoader: PluginsLoader by lazy { PluginsLoader(this.javaClass.classLoader) } - init { - supervisor.invokeOnCompletion { + private fun init() { // delayed + coroutineContext[Job]!!.invokeOnCompletion { classLoader.clear() } }