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 a6d2f743d..4adc7e6bb 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 @@ -21,6 +21,7 @@ import net.mamoe.mirai.console.MiraiConsoleImplementation.Companion.start import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge import net.mamoe.mirai.console.internal.util.childScopeContext import net.mamoe.mirai.console.plugin.PluginLoader +import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.center.PluginCenter import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.util.ConsoleExperimentalAPI @@ -43,6 +44,10 @@ public interface MiraiConsole : CoroutineScope { * Console 运行根目录, 由前端决定确切路径. * * 所有子模块都会在这个目录之下创建子目录. + * + * @see PluginManager.pluginsPath + * @see PluginManager.pluginsDataPath + * @see PluginManager.pluginsConfigPath */ public val rootPath: Path 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 d138fb581..89084cdd5 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 @@ -21,6 +21,7 @@ import net.mamoe.mirai.console.plugin.ResourceContainer.Companion.asResourceCont 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.io.InputStream import java.nio.file.Path import java.util.concurrent.locks.ReentrantLock @@ -38,7 +39,7 @@ internal abstract class JvmPluginInternal( ) : JvmPlugin, CoroutineScope { - override var isEnabled: Boolean = false + final override var isEnabled: Boolean = false private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() } override fun getResourceAsStream(path: String): InputStream? = resourceContainerDelegate.getResourceAsStream(path) @@ -64,6 +65,18 @@ internal abstract class JvmPluginInternal( PluginManager.pluginsDataPath.resolve(description.name).apply { mkdir() } } + final override val dataFolder: File by lazy { + dataFolderPath.toFile() + } + + override val configFolderPath: Path by lazy { + PluginManager.pluginsConfigPath.resolve(description.name).apply { mkdir() } + } + + override val configFolder: File by lazy { + configFolderPath.toFile() + } + internal fun internalOnDisable() { firstRun = false kotlin.runCatching { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt index 7572025f6..c429959cc 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/plugin/PluginManagerImpl.kt @@ -21,12 +21,18 @@ import net.mamoe.mirai.console.plugin.description.PluginDependency import net.mamoe.mirai.console.plugin.description.PluginDescription import net.mamoe.mirai.console.plugin.description.PluginKind import net.mamoe.mirai.utils.info +import java.io.File import java.nio.file.Path import java.util.concurrent.locks.ReentrantLock internal object PluginManagerImpl : PluginManager { + override val pluginsPath: Path = MiraiConsole.rootPath.resolve("plugins").apply { mkdir() } + override val pluginsFolder: File = pluginsPath.toFile() override val pluginsDataPath: Path = MiraiConsole.rootPath.resolve("data").apply { mkdir() } + override val pluginsDataFolder: File = pluginsDataPath.toFile() + override val pluginsConfigPath: Path = MiraiConsole.rootPath.resolve("config").apply { mkdir() } + override val pluginsConfigFolder: File = pluginsConfigPath.toFile() @Suppress("ObjectPropertyName") private val _pluginLoaders: MutableList> = mutableListOf() 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 0502024ba..b4b2f3d53 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 @@ -12,11 +12,13 @@ package net.mamoe.mirai.console.plugin import net.mamoe.mirai.console.command.CommandOwner +import net.mamoe.mirai.console.data.PluginConfig +import net.mamoe.mirai.console.data.PluginData 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.safeLoader import net.mamoe.mirai.console.plugin.description.PluginDescription import net.mamoe.mirai.console.plugin.jvm.JvmPlugin -import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import java.io.File import java.nio.file.Path @@ -48,18 +50,32 @@ public interface Plugin : CommandOwner { public val loader: PluginLoader<*, *> } +/** + * 获取 + */ +public inline val Plugin.description: PluginDescription get() = this.safeLoader.getDescription(this) + /** * 支持文件系统存储的扩展. * + * @suppress 此接口只应由 [JvmPlugin] 继承 + * * @see JvmPlugin */ -@ConsoleExperimentalAPI("classname is subject to change") public interface PluginFileExtensions { /** - * 数据目录 + * 数据目录路径 + * @see PluginData */ public val dataFolderPath: Path + /** + * 数据目录. `dataFolderPath.toFile()` + * @see PluginData + */ + public val dataFolder: File + + /** * 从数据目录获取一个文件. * @see dataFolderPath @@ -87,9 +103,46 @@ public interface PluginFileExtensions { */ @JvmDefault public fun resolveDataPath(relativePath: Path): Path = dataFolderPath.resolve(relativePath) -} -/** - * @return `dataFolderPath.toFile()` - */ -public val PluginFileExtensions.dataFolder: File get() = dataFolderPath.toFile() \ No newline at end of file + + /** + * 插件配置保存路径 + * @see PluginConfig + */ + public val configFolderPath: Path + + /** + * 插件配置保存路径 + * @see PluginConfig + */ + public val configFolder: File + + + /** + * 从配置目录获取一个文件. + * @see configFolderPath + */ + @JvmDefault + public fun resolveConfigFile(relativePath: String): File = configFolderPath.resolve(relativePath).toFile() + + /** + * 从配置目录获取一个文件. + * @see configFolderPath + */ + @JvmDefault + public fun resolveConfigPath(relativePath: String): Path = configFolderPath.resolve(relativePath) + + /** + * 从配置目录获取一个文件. + * @see configFolderPath + */ + @JvmDefault + public fun resolveConfigFile(relativePath: Path): File = configFolderPath.resolve(relativePath).toFile() + + /** + * 从配置目录获取一个文件路径. + * @see configFolderPath + */ + @JvmDefault + public fun resolveConfigPath(relativePath: Path): Path = configFolderPath.resolve(relativePath) +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginManager.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginManager.kt index 99445ce12..577497d9a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginManager.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/PluginManager.kt @@ -31,24 +31,55 @@ import java.nio.file.Path * @see PluginLoader 插件加载器 */ public interface PluginManager { + // region paths + /** - * 插件自身存放路径. 由前端决定具体路径. + * 插件自身存放路径 [Path]. 由前端决定具体路径. * * **实现细节**: 在 terminal 前端实现为 `$rootPath/plugins` - * - * @see pluginsFolder [File] 类型 */ public val pluginsPath: Path /** - * 插件数据存放路径 + * 插件自身存放路径 [File]. 由前端决定具体路径. + * + * **实现细节**: 在 terminal 前端实现为 `$rootPath/plugins` + */ + public val pluginsFolder: File + + /** + * 插件内部数据存放路径 [Path] * * **实现细节**: 在 terminal 前端实现为 `$rootPath/data` - * - * @see pluginsDataFolder [File] 类型 */ public val pluginsDataPath: Path + /** + * 插件内部数据存放路径 [File] + * + * **实现细节**: 在 terminal 前端实现为 `$rootPath/data` + */ + public val pluginsDataFolder: File + + /** + * 插件配置存放路径 [Path] + * + * **实现细节**: 在 terminal 前端实现为 `$rootPath/config` + */ + public val pluginsConfigPath: Path + + /** + * 插件配置存放路径 [File] + * + * **实现细节**: 在 terminal 前端实现为 `$rootPath/config` + */ + public val pluginsConfigFolder: File + + // endregion + + + // region plugins & loaders + /** * 已加载的插件列表 * @@ -104,13 +135,15 @@ public interface PluginManager { public fun Plugin.enable(): Unit = safeLoader.enable(this) /** - * 经过泛型类型转换的 [PluginLoader] + * 经过泛型类型转换的 [Plugin.loader] */ @get:JvmSynthetic @Suppress("UNCHECKED_CAST") public val

P.safeLoader: PluginLoader get() = this.loader as PluginLoader + // endregion + public companion object INSTANCE : PluginManager by PluginManagerImpl { // due to Kotlin's bug public override val Plugin.description: PluginDescription get() = PluginManagerImpl.run { description } @@ -120,18 +153,4 @@ public interface PluginManager { public override fun Plugin.enable(): Unit = PluginManagerImpl.run { enable() } public override val

P.safeLoader: PluginLoader get() = PluginManagerImpl.run { safeLoader } } -} - -/** - * @see PluginManager.pluginsPath - */ -@get:JvmSynthetic -public inline val PluginManager.pluginsFolder: File - get() = pluginsPath.toFile() - -/** - * @see PluginManager.pluginsDataPath - */ -@get:JvmSynthetic -public inline val PluginManager.pluginsDataFolder: File - get() = pluginsDataPath.toFile() \ No newline at end of file +} \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt index 784664b19..3764496a8 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/jvm/JvmPlugin.kt @@ -24,6 +24,7 @@ import net.mamoe.mirai.console.data.PluginData import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.PluginFileExtensions import net.mamoe.mirai.console.plugin.ResourceContainer +import net.mamoe.mirai.console.plugin.getDescription import net.mamoe.mirai.utils.MiraiLogger @@ -48,7 +49,7 @@ public interface JvmPlugin : Plugin, CoroutineScope, public val logger: MiraiLogger /** 插件描述 */ - public val description: JvmPluginDescription + public val description: JvmPluginDescription get() = loader.getDescription(this) /** 所属插件加载器实例 */ @JvmDefault