diff --git a/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt b/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt index 46fe659ae..8e3537675 100644 --- a/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt +++ b/backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt @@ -20,6 +20,7 @@ import net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadAllServices import net.mamoe.mirai.console.plugin.jvm.* import net.mamoe.mirai.console.plugin.loader.AbstractFilePluginLoader import net.mamoe.mirai.console.plugin.loader.PluginLoadException +import net.mamoe.mirai.console.plugin.name import net.mamoe.mirai.console.util.CoroutineScopeUtils.childScope import net.mamoe.mirai.utils.MiraiLogger import java.io.File @@ -93,12 +94,17 @@ internal object BuiltInJvmPluginLoaderImpl : return filePlugins.toSet().map { it.value } } + private val loadedPlugins = ConcurrentHashMap() + @Throws(PluginLoadException::class) override fun load(plugin: JvmPlugin) { ensureActive() + if (loadedPlugins.put(plugin, Unit) != null) { + error("Plugin '${plugin.name}' is already loaded and cannot be reloaded.") + } runCatching { - check(plugin is JvmPluginInternal) { "A JvmPlugin must extend AbstractJvmPlugin" } + check(plugin is JvmPluginInternal) { "A JvmPlugin must extend AbstractJvmPlugin to be loaded by JvmPluginLoader.BuiltIn" } plugin.internalOnLoad(plugin.componentStorage) }.getOrElse { throw PluginLoadException("Exception while loading ${plugin.description.name}", it) @@ -106,7 +112,7 @@ internal object BuiltInJvmPluginLoaderImpl : } override fun enable(plugin: JvmPlugin) { - if (plugin.isEnabled) return + if (plugin.isEnabled) error("Plugin '${plugin.name}' is already enabled and cannot be re-enabled.") ensureActive() runCatching { if (plugin is JvmPluginInternal) { @@ -118,7 +124,7 @@ internal object BuiltInJvmPluginLoaderImpl : } override fun disable(plugin: JvmPlugin) { - if (!plugin.isEnabled) return + if (!plugin.isEnabled) error("Plugin '${plugin.name}' is not already disabled and cannot be re-disabled.") if (MiraiConsole.isActive) ensureActive() diff --git a/backend/mirai-console/src/plugin/loader/PluginLoader.kt b/backend/mirai-console/src/plugin/loader/PluginLoader.kt index e4b9e863f..22021a7df 100644 --- a/backend/mirai-console/src/plugin/loader/PluginLoader.kt +++ b/backend/mirai-console/src/plugin/loader/PluginLoader.kt @@ -76,7 +76,7 @@ public interface PluginLoader

{ * @throws PluginLoadException 在加载插件遇到意料之中的错误时抛出 (如找不到主类等). * @throws IllegalStateException 在插件已经被加载时抛出. 这属于意料之外的情况. */ - @Throws(PluginLoadException::class) + @Throws(IllegalStateException::class, PluginLoadException::class) public fun load(plugin: P) /**