From 335dff170d9c9b13be0efc24ccb833cfa8120a3e Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Fri, 14 Jan 2022 21:12:14 +0800 Subject: [PATCH] Add console launch options --- .../src/MiraiConsoleImplementation.kt | 25 +++++++++++++++++++ .../MiraiConsoleImplementationBridge.kt | 2 ++ .../src/internal/plugin/JvmPluginInternal.kt | 10 ++++++-- .../src/ConsoleTerminalSettings.kt | 5 ++++ .../src/MiraiConsoleImplementationTerminal.kt | 3 +++ 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt b/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt index 746ddad86..2fbea225c 100644 --- a/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt +++ b/mirai-console/backend/mirai-console/src/MiraiConsoleImplementation.kt @@ -33,6 +33,7 @@ import net.mamoe.mirai.console.logging.LoggerController import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader import net.mamoe.mirai.console.plugin.loader.PluginLoader +import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.ConsoleInput import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.utils.BotConfiguration @@ -330,6 +331,25 @@ public interface MiraiConsoleImplementation : CoroutineScope { public val backendAccess: BackendAccess get() = backendAccessInstance + + /////////////////////////////////////////////////////////////////////////// + // ConsoleLaunchOptions + /////////////////////////////////////////////////////////////////////////// + + /** + * Console 启动参数, 修改参数会改变默认行为 + * @since 2.10.0-RC + */ + @ConsoleExperimentalApi + public class ConsoleLaunchOptions { + @JvmField + public var crashWhenPluginLoadFailed: Boolean = false + } + + @ConsoleExperimentalApi + public val consoleLaunchOptions: ConsoleLaunchOptions + get() = ConsoleLaunchOptions() + public companion object { private val backendAccessInstance = object : BackendAccess { override val globalComponentStorage: ComponentStorage get() = GlobalComponentStorage @@ -339,6 +359,10 @@ public interface MiraiConsoleImplementation : CoroutineScope { @Volatile internal var instance: MiraiConsoleImplementation? = null internal val instanceInitialized: Boolean get() = instance != null + + @JvmSynthetic + internal var options: ConsoleLaunchOptions = ConsoleLaunchOptions() + private val initLock = ReentrantLock() /** @@ -362,6 +386,7 @@ public interface MiraiConsoleImplementation : CoroutineScope { "Run MiraiConsole.cancel to kill old instance before starting another instance." ) } + options = this.consoleLaunchOptions this@Companion.instance = this kotlin.runCatching { MiraiConsoleImplementationBridge.doStart() diff --git a/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt b/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt index 02910c1bf..190766dfa 100644 --- a/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt +++ b/mirai-console/backend/mirai-console/src/internal/MiraiConsoleImplementationBridge.kt @@ -81,6 +81,8 @@ internal object MiraiConsoleImplementationBridge : CoroutineScope, override val mainLogger: MiraiLogger by lazy { createLogger("main") } + override val consoleLaunchOptions: MiraiConsoleImplementation.ConsoleLaunchOptions get() = MiraiConsoleImplementation.options + init { // TODO: Replace to standard api @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt index 4fac04f23..4516e3041 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt @@ -13,9 +13,9 @@ import kotlinx.atomicfu.AtomicLong import kotlinx.atomicfu.locks.withLock import kotlinx.coroutines.* import net.mamoe.mirai.console.MiraiConsole +import net.mamoe.mirai.console.MiraiConsoleImplementation import net.mamoe.mirai.console.data.runCatchingLog import net.mamoe.mirai.console.extension.PluginComponentStorage -import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge import net.mamoe.mirai.console.internal.data.mkdir import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage import net.mamoe.mirai.console.permission.Permission @@ -93,7 +93,10 @@ internal abstract class JvmPluginInternal( cancel(CancellationException("plugin disabled")) }, onFailure = { - cancel(CancellationException("Exception while enabling plugin", it)) + cancel(CancellationException("Exception while disabling plugin", it)) + if (MiraiConsoleImplementation.options.crashWhenPluginLoadFailed) { + throw it + } } ) isEnabled = false @@ -120,6 +123,9 @@ internal abstract class JvmPluginInternal( onFailure = { cancel(CancellationException("Exception while enabling plugin", it)) logger.error(it) + if (MiraiConsoleImplementation.options.crashWhenPluginLoadFailed) { + throw it + } return false } ) diff --git a/mirai-console/frontend/mirai-console-terminal/src/ConsoleTerminalSettings.kt b/mirai-console/frontend/mirai-console-terminal/src/ConsoleTerminalSettings.kt index 8600e3478..476a7a8ef 100644 --- a/mirai-console/frontend/mirai-console-terminal/src/ConsoleTerminalSettings.kt +++ b/mirai-console/frontend/mirai-console-terminal/src/ConsoleTerminalSettings.kt @@ -12,6 +12,8 @@ package net.mamoe.mirai.console.terminal +import net.mamoe.mirai.console.MiraiConsoleImplementation + @Retention(AnnotationRetention.BINARY) @RequiresOptIn(level = RequiresOptIn.Level.WARNING) @Target( @@ -27,6 +29,9 @@ annotation class ConsoleTerminalExperimentalApi @ConsoleTerminalExperimentalApi object ConsoleTerminalSettings { + @JvmField + var launchOptions: MiraiConsoleImplementation.ConsoleLaunchOptions = MiraiConsoleImplementation.ConsoleLaunchOptions() + @JvmField var setupAnsi: Boolean = System.getProperty("os.name") .lowercase() diff --git a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt index a0a2443dc..d06bc947a 100644 --- a/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt +++ b/mirai-console/frontend/mirai-console-terminal/src/MiraiConsoleImplementationTerminal.kt @@ -100,6 +100,9 @@ open class MiraiConsoleImplementationTerminal require(isDirectory) { "rootDir $absolutePath is not a directory" } } } + + override val consoleLaunchOptions: MiraiConsoleImplementation.ConsoleLaunchOptions + get() = ConsoleTerminalSettings.launchOptions } val lineReader: LineReader by lazy {