From 594ae1f7789c3fc9bd0c7efd6bcaf91d8351c112 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 25 May 2020 18:09:20 +0800 Subject: [PATCH] Rearrange implementations --- .../net/mamoe/mirai/console/MiraiConsole.kt | 4 +- .../net/mamoe/mirai/console/plugin/Plugin.kt | 2 +- .../mirai/console/plugin/PluginLoader.kt | 2 +- .../mirai/console/plugin/builtin/JvmPlugin.kt | 166 ------------------ .../console/plugin/internal/JvmPluginImpl.kt | 102 +++++++++++ .../plugin/{ => internal}/PluginsLoader.kt | 10 +- .../{builtin => jvm}/JarPluginLoader.kt | 8 +- .../mirai/console/plugin/jvm/JavaPlugin.kt | 31 ++++ .../mirai/console/plugin/jvm/JvmPlugin.kt | 48 +++++ .../{builtin => jvm}/JvmPluginDescription.kt | 2 +- .../mirai/console/plugin/jvm/KotlinPlugin.kt | 42 +++++ .../console/utils/JavaPluginScheduler.kt | 2 +- .../mirai/console/command/TestCommands.kt | 2 +- 13 files changed, 243 insertions(+), 178 deletions(-) delete mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JvmPlugin.kt create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/JvmPluginImpl.kt rename backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/{ => internal}/PluginsLoader.kt (94%) rename backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/{builtin => jvm}/JarPluginLoader.kt (93%) create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JavaPlugin.kt create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt rename backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/{builtin => jvm}/JvmPluginDescription.kt (97%) create mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/KotlinPlugin.kt 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 3f538b345..634f33bf6 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 @@ -14,8 +14,8 @@ import kotlinx.coroutines.Job import kotlinx.io.charsets.Charset import net.mamoe.mirai.Bot import net.mamoe.mirai.console.plugin.PluginLoader -import net.mamoe.mirai.console.plugin.builtin.JarPluginLoader -import net.mamoe.mirai.console.plugin.builtin.JvmPlugin +import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader +import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.setting.SettingStorage import net.mamoe.mirai.utils.DefaultLogger import net.mamoe.mirai.utils.MiraiExperimentalAPI diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt index 0b462367a..f5a8c5142 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/Plugin.kt @@ -9,7 +9,7 @@ package net.mamoe.mirai.console.plugin -import net.mamoe.mirai.console.plugin.builtin.JvmPlugin +import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import java.io.File /** 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 d775269ad..d0e3fdb8b 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,7 +11,7 @@ package net.mamoe.mirai.console.plugin -import net.mamoe.mirai.console.plugin.builtin.JarPluginLoader +import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import java.io.File /** diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JvmPlugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JvmPlugin.kt deleted file mode 100644 index 2b810c1c2..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JvmPlugin.kt +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2020 Mamoe Technologies and contributors. - * - * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. - * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. - * - * https://github.com/mamoe/mirai/blob/master/LICENSE - */ - -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE", "EXPOSED_SUPER_CLASS") - -package net.mamoe.mirai.console.plugin.builtin - -import kotlinx.atomicfu.locks.withLock -import kotlinx.coroutines.* -import net.mamoe.mirai.console.MiraiConsole -import net.mamoe.mirai.console.plugin.Plugin -import net.mamoe.mirai.console.plugin.PluginLoader -import net.mamoe.mirai.console.plugin.PluginManager -import net.mamoe.mirai.console.setting.* -import net.mamoe.mirai.console.utils.JavaPluginScheduler -import net.mamoe.mirai.utils.MiraiLogger -import java.io.File -import java.util.* -import java.util.concurrent.locks.ReentrantLock -import kotlin.collections.AbstractCollection -import kotlin.coroutines.CoroutineContext -import kotlin.coroutines.EmptyCoroutineContext -import kotlin.reflect.KProperty - - -/** - * Java 或 Kotlin Jar 插件 - * - * @see JavaPlugin Java 插件 - * @see KotlinPlugin Kotlin 插件 - */ -interface JvmPlugin : Plugin, CoroutineScope { - /** 日志 */ - val logger: MiraiLogger - - /** 插件描述 */ - val description: JvmPluginDescription - - /** 所属插件加载器实例 */ - override val loader: JarPluginLoader get() = JarPluginLoader - - @JvmDefault - fun onLoad() { - } - - @JvmDefault - fun onEnable() { - } - - @JvmDefault - fun onDisable() { - } -} - -/** - * [JavaPlugin] 和 [KotlinPlugin] 的父类 - */ -sealed class AbstractJvmPlugin(parentCoroutineContext: CoroutineContext) : JvmPluginImpl(parentCoroutineContext) { - abstract inner class PluginSetting : Setting() { - private val track = - @Suppress("LeakingThis") - loader.settingStorage.trackOn(this) - - init { - this@AbstractJvmPlugin.job.invokeOnCompletion { - track.close() - } - } - - override fun onElementChanged(value: Value<*>) { - TODO() - } - } -} - -/** - * Java 插件的父类 - */ -abstract class JavaPlugin @JvmOverloads constructor( - parentCoroutineContext: CoroutineContext = EmptyCoroutineContext -) : JvmPlugin, AbstractJvmPlugin(parentCoroutineContext) { - - /** - * Java API Scheduler - */ - val scheduler: JavaPluginScheduler = - JavaPluginScheduler(this.coroutineContext) -} - -/** - * Kotlin 插件的父类 - */ -abstract class KotlinPlugin @JvmOverloads constructor( - parentCoroutineContext: CoroutineContext = EmptyCoroutineContext -) : JvmPlugin, AbstractJvmPlugin(parentCoroutineContext) - -internal val T.job: Job where T : CoroutineScope, T : Plugin get() = this.coroutineContext[Job]!! - -internal sealed class JvmPluginImpl( - parentCoroutineContext: CoroutineContext -) : JvmPlugin, CoroutineScope { - // region JvmPlugin - /** - * Initialized immediately after construction of [JvmPluginImpl] instance - */ - @Suppress("PropertyName") - internal lateinit var _description: JvmPluginDescription - - override val description: JvmPluginDescription get() = _description - - final override val logger: MiraiLogger by lazy { MiraiConsole.newLogger(this._description.name) } - - private var firstRun = true - - override val dataFolder: File by lazy { - File(PluginManager.pluginsDataFolder, description.name).apply { mkdir() } - } - - internal fun internalOnDisable() { - firstRun = false - this.onDisable() - } - - internal fun internalOnLoad() { - this.onLoad() - } - - internal fun internalOnEnable() { - if (!firstRun) refreshCoroutineContext() - this.onEnable() - } - - // endregion - - // region CoroutineScope - - // for future use - @Suppress("PropertyName") - @JvmField - internal var _intrinsicCoroutineContext: CoroutineContext = EmptyCoroutineContext - - @JvmField - internal val coroutineContextInitializer = { - CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } - .plus(parentCoroutineContext) - .plus(SupervisorJob(parentCoroutineContext[Job])) + _intrinsicCoroutineContext - } - - private fun refreshCoroutineContext(): CoroutineContext { - return coroutineContextInitializer().also { _coroutineContext = it } - } - - private val contextUpdateLock: ReentrantLock = ReentrantLock() - private var _coroutineContext: CoroutineContext? = null - final override val coroutineContext: CoroutineContext - get() = _coroutineContext - ?: contextUpdateLock.withLock { _coroutineContext ?: refreshCoroutineContext() } - - // endregion -} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/JvmPluginImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/JvmPluginImpl.kt new file mode 100644 index 000000000..b10898d4d --- /dev/null +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/JvmPluginImpl.kt @@ -0,0 +1,102 @@ +/* + * Copyright 2020 Mamoe Technologies and contributors. + * + * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证. + * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link. + * + * https://github.com/mamoe/mirai/blob/master/LICENSE + */ + +package net.mamoe.mirai.console.plugin.internal + +import kotlinx.atomicfu.locks.withLock +import kotlinx.coroutines.CoroutineExceptionHandler +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.SupervisorJob +import net.mamoe.mirai.console.MiraiConsole +import net.mamoe.mirai.console.plugin.Plugin +import net.mamoe.mirai.console.plugin.PluginManager +import net.mamoe.mirai.console.plugin.jvm.JvmPlugin +import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription +import net.mamoe.mirai.utils.MiraiLogger +import java.io.File +import java.util.concurrent.locks.ReentrantLock +import kotlin.coroutines.CoroutineContext +import kotlin.coroutines.EmptyCoroutineContext + +internal val T.job: Job where T : CoroutineScope, T : Plugin get() = this.coroutineContext[Job]!! + +@PublishedApi +internal abstract class JvmPluginImpl( + parentCoroutineContext: CoroutineContext +) : JvmPlugin, + CoroutineScope { + // region JvmPlugin + /** + * Initialized immediately after construction of [JvmPluginImpl] instance + */ + @Suppress("PropertyName") + internal lateinit var _description: JvmPluginDescription + + override val description: JvmPluginDescription get() = _description + + final override val logger: MiraiLogger by lazy { + MiraiConsole.newLogger( + this._description.name + ) + } + + private var firstRun = true + + override val dataFolder: File by lazy { + File( + PluginManager.pluginsDataFolder, + description.name + ).apply { mkdir() } + } + + internal fun internalOnDisable() { + firstRun = false + this.onDisable() + } + + internal fun internalOnLoad() { + this.onLoad() + } + + internal fun internalOnEnable() { + if (!firstRun) refreshCoroutineContext() + this.onEnable() + } + + // endregion + + // region CoroutineScope + + // for future use + @Suppress("PropertyName") + @JvmField + internal var _intrinsicCoroutineContext: CoroutineContext = + EmptyCoroutineContext + + @JvmField + internal val coroutineContextInitializer = { + CoroutineExceptionHandler { _, throwable -> logger.error(throwable) } + .plus(parentCoroutineContext) + .plus(SupervisorJob(parentCoroutineContext[Job])) + _intrinsicCoroutineContext + } + + private fun refreshCoroutineContext(): CoroutineContext { + return coroutineContextInitializer().also { _coroutineContext = it } + } + + private val contextUpdateLock: ReentrantLock = + ReentrantLock() + private var _coroutineContext: CoroutineContext? = null + final override val coroutineContext: CoroutineContext + get() = _coroutineContext + ?: contextUpdateLock.withLock { _coroutineContext ?: refreshCoroutineContext() } + + // endregion +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginsLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt similarity index 94% rename from backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginsLoader.kt rename to backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt index 069b4d925..217fbeb98 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginsLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/internal/PluginsLoader.kt @@ -7,7 +7,7 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package net.mamoe.mirai.console.plugin +package net.mamoe.mirai.console.plugin.internal import net.mamoe.mirai.console.MiraiConsole import java.io.File @@ -50,7 +50,13 @@ internal class PluginsLoader(private val parentClassLoader: ClassLoader) { fun loadPluginMainClassByJarFile(pluginName: String, mainClass: String, jarFile: File): Class<*> { try { if (!pluginLoaders.containsKey(pluginName)) { - pluginLoaders[pluginName] = PluginClassLoader(pluginName, jarFile, this, parentClassLoader) + pluginLoaders[pluginName] = + PluginClassLoader( + pluginName, + jarFile, + this, + parentClassLoader + ) } return pluginLoaders[pluginName]!!.loadClass(mainClass) } catch (e: ClassNotFoundException) { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JarPluginLoader.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt similarity index 93% rename from backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JarPluginLoader.kt rename to backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt index 47855db46..6491cfd7f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/builtin/JarPluginLoader.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JarPluginLoader.kt @@ -7,13 +7,14 @@ * https://github.com/mamoe/mirai/blob/master/LICENSE */ -package net.mamoe.mirai.console.plugin.builtin +package net.mamoe.mirai.console.plugin.jvm import kotlinx.coroutines.* import net.mamoe.mirai.console.MiraiConsole import net.mamoe.mirai.console.plugin.AbstractFilePluginLoader import net.mamoe.mirai.console.plugin.PluginLoadException -import net.mamoe.mirai.console.plugin.PluginsLoader +import net.mamoe.mirai.console.plugin.internal.JvmPluginImpl +import net.mamoe.mirai.console.plugin.internal.PluginsLoader import net.mamoe.mirai.console.setting.SettingStorage import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.yamlkt.Yaml @@ -39,7 +40,8 @@ object JarPluginLoader : AbstractFilePluginLoader) { + TODO() + } + } +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/JavaPluginScheduler.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/JavaPluginScheduler.kt index 89f3bb44e..af24ad333 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/JavaPluginScheduler.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/utils/JavaPluginScheduler.kt @@ -11,7 +11,7 @@ package net.mamoe.mirai.console.utils import kotlinx.coroutines.* import kotlinx.coroutines.future.future -import net.mamoe.mirai.console.plugin.builtin.JavaPlugin +import net.mamoe.mirai.console.plugin.jvm.JavaPlugin import java.util.concurrent.Callable import java.util.concurrent.CompletableFuture import java.util.concurrent.Future diff --git a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt index fd949483e..bfcc0bd25 100644 --- a/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt +++ b/backend/mirai-console/src/test/kotlin/net/mamoe/mirai/console/command/TestCommands.kt @@ -12,7 +12,7 @@ package net.mamoe.mirai.console.command import net.mamoe.mirai.Bot -import net.mamoe.mirai.console.plugin.builtin.KotlinPlugin +import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin import net.mamoe.mirai.console.setting.value import net.mamoe.mirai.message.data.* import org.junit.jupiter.api.Test