From 9cc05f682bd633b29450118e28a7d61fae93d17c Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 23 Aug 2020 19:52:40 +0800 Subject: [PATCH] Improve plugin enabling and disabling --- .../mirai/console/internal/plugin/JarPluginLoaderImpl.kt | 4 ++-- .../mirai/console/internal/plugin/JvmPluginInternal.kt | 7 ++++--- .../kotlin/net/mamoe/mirai/console/plugin/PluginLoader.kt | 8 ++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt index 83af255ca..76c3e6cd3 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JarPluginLoaderImpl.kt @@ -110,7 +110,7 @@ internal object JarPluginLoaderImpl : } override fun enable(plugin: JvmPlugin) { - if (plugin.isEnabled) throw IllegalStateException("Plugin is already enabled") + if (plugin.isEnabled) return ensureActive() if (plugin is JvmPluginInternal) { plugin.internalOnEnable() @@ -118,7 +118,7 @@ internal object JarPluginLoaderImpl : } override fun disable(plugin: JvmPlugin) { - if (!plugin.isEnabled) throw IllegalStateException("Plugin is already disabled") + if (!plugin.isEnabled) return if (plugin is JvmPluginInternal) { plugin.internalOnDisable() diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt index 4526263a6..c89337b73 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/JvmPluginInternal.kt @@ -38,8 +38,7 @@ internal abstract class JvmPluginInternal( ) : JvmPlugin, CoroutineScope { - override val isEnabled: Boolean - get() = job.isActive + override var isEnabled: Boolean = false private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() } override fun getResourceAsStream(path: String): InputStream? = resourceContainerDelegate.getResourceAsStream(path) @@ -77,6 +76,7 @@ internal abstract class JvmPluginInternal( cancel(CancellationException("Exception while enabling plugin", it)) } ) + isEnabled = false } @Throws(Throwable::class) @@ -90,6 +90,7 @@ internal abstract class JvmPluginInternal( onEnable() }.fold( onSuccess = { + isEnabled = true return true }, onFailure = { @@ -121,7 +122,7 @@ internal abstract class JvmPluginInternal( job.invokeOnCompletion { e -> if (e != null) { logger.error(e) - safeLoader.disable(this) + if (this.isEnabled) safeLoader.disable(this) } } } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginLoader.kt index d95351ef2..669f3810c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginLoader.kt @@ -11,6 +11,8 @@ package net.mamoe.mirai.console.plugin +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.disable +import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.register import net.mamoe.mirai.console.plugin.dsecription.PluginDescription import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader @@ -75,8 +77,9 @@ public interface PluginLoader

{ * **实现细节**: 此函数可抛出 [PluginLoadException] 作为正常失败原因, 其他任意异常都属于意外错误. * 当异常发生时, 插件将会直接被放弃加载, 并影响依赖它的其他插件. * - * @throws IllegalStateException 当插件已经启用时抛出 * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等). + * + * @see PluginManager.enable */ @Throws(IllegalStateException::class, PluginLoadException::class) public fun enable(plugin: P) @@ -87,8 +90,9 @@ public interface PluginLoader

{ * **实现细节**: 此函数可抛出 [PluginLoadException] 作为正常失败原因, 其他任意异常都属于意外错误. * 当异常发生时, 插件将会直接被放弃加载, 并影响依赖它的其他插件. * - * @throws IllegalStateException 当插件已经禁用时抛出 * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等). + * + * @see PluginManager.disable */ @Throws(IllegalStateException::class, PluginLoadException::class) public fun disable(plugin: P)