Implement configDataFolder and pathas

This commit is contained in:
Him188 2020-08-27 09:52:10 +08:00
parent cb50ca8e64
commit 21150742dc
6 changed files with 129 additions and 32 deletions

View File

@ -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.MiraiConsoleImplementationBridge
import net.mamoe.mirai.console.internal.util.childScopeContext import net.mamoe.mirai.console.internal.util.childScopeContext
import net.mamoe.mirai.console.plugin.PluginLoader 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.center.PluginCenter
import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader import net.mamoe.mirai.console.plugin.jvm.JarPluginLoader
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
@ -43,6 +44,10 @@ public interface MiraiConsole : CoroutineScope {
* Console 运行根目录, 由前端决定确切路径. * Console 运行根目录, 由前端决定确切路径.
* *
* 所有子模块都会在这个目录之下创建子目录. * 所有子模块都会在这个目录之下创建子目录.
*
* @see PluginManager.pluginsPath
* @see PluginManager.pluginsDataPath
* @see PluginManager.pluginsConfigPath
*/ */
public val rootPath: Path public val rootPath: Path

View File

@ -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.JvmPlugin
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
import java.io.File
import java.io.InputStream import java.io.InputStream
import java.nio.file.Path import java.nio.file.Path
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
@ -38,7 +39,7 @@ internal abstract class JvmPluginInternal(
) : JvmPlugin, ) : JvmPlugin,
CoroutineScope { CoroutineScope {
override var isEnabled: Boolean = false final override var isEnabled: Boolean = false
private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() } private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() }
override fun getResourceAsStream(path: String): InputStream? = resourceContainerDelegate.getResourceAsStream(path) override fun getResourceAsStream(path: String): InputStream? = resourceContainerDelegate.getResourceAsStream(path)
@ -64,6 +65,18 @@ internal abstract class JvmPluginInternal(
PluginManager.pluginsDataPath.resolve(description.name).apply { mkdir() } 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() { internal fun internalOnDisable() {
firstRun = false firstRun = false
kotlin.runCatching { kotlin.runCatching {

View File

@ -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.PluginDescription
import net.mamoe.mirai.console.plugin.description.PluginKind import net.mamoe.mirai.console.plugin.description.PluginKind
import net.mamoe.mirai.utils.info import net.mamoe.mirai.utils.info
import java.io.File
import java.nio.file.Path import java.nio.file.Path
import java.util.concurrent.locks.ReentrantLock import java.util.concurrent.locks.ReentrantLock
internal object PluginManagerImpl : PluginManager { internal object PluginManagerImpl : PluginManager {
override val pluginsPath: Path = MiraiConsole.rootPath.resolve("plugins").apply { mkdir() } 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 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") @Suppress("ObjectPropertyName")
private val _pluginLoaders: MutableList<PluginLoader<*, *>> = mutableListOf() private val _pluginLoaders: MutableList<PluginLoader<*, *>> = mutableListOf()

View File

@ -12,11 +12,13 @@
package net.mamoe.mirai.console.plugin package net.mamoe.mirai.console.plugin
import net.mamoe.mirai.console.command.CommandOwner 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.disable
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.enable 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.description.PluginDescription
import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPlugin
import net.mamoe.mirai.console.util.ConsoleExperimentalAPI
import java.io.File import java.io.File
import java.nio.file.Path import java.nio.file.Path
@ -48,18 +50,32 @@ public interface Plugin : CommandOwner {
public val loader: PluginLoader<*, *> public val loader: PluginLoader<*, *>
} }
/**
* 获取
*/
public inline val Plugin.description: PluginDescription get() = this.safeLoader.getDescription(this)
/** /**
* 支持文件系统存储的扩展. * 支持文件系统存储的扩展.
* *
* @suppress 此接口只应由 [JvmPlugin] 继承
*
* @see JvmPlugin * @see JvmPlugin
*/ */
@ConsoleExperimentalAPI("classname is subject to change")
public interface PluginFileExtensions { public interface PluginFileExtensions {
/** /**
* 数据目录 * 数据目录路径
* @see PluginData
*/ */
public val dataFolderPath: Path public val dataFolderPath: Path
/**
* 数据目录. `dataFolderPath.toFile()`
* @see PluginData
*/
public val dataFolder: File
/** /**
* 从数据目录获取一个文件. * 从数据目录获取一个文件.
* @see dataFolderPath * @see dataFolderPath
@ -87,9 +103,46 @@ public interface PluginFileExtensions {
*/ */
@JvmDefault @JvmDefault
public fun resolveDataPath(relativePath: Path): Path = dataFolderPath.resolve(relativePath) public fun resolveDataPath(relativePath: Path): Path = dataFolderPath.resolve(relativePath)
}
/**
* @return `dataFolderPath.toFile()` /**
*/ * 插件配置保存路径
public val PluginFileExtensions.dataFolder: File get() = dataFolderPath.toFile() * @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)
}

View File

@ -31,24 +31,55 @@ import java.nio.file.Path
* @see PluginLoader 插件加载器 * @see PluginLoader 插件加载器
*/ */
public interface PluginManager { public interface PluginManager {
// region paths
/** /**
* 插件自身存放路径. 由前端决定具体路径. * 插件自身存放路径 [Path]. 由前端决定具体路径.
* *
* **实现细节**: terminal 前端实现为 `$rootPath/plugins` * **实现细节**: terminal 前端实现为 `$rootPath/plugins`
*
* @see pluginsFolder [File] 类型
*/ */
public val pluginsPath: Path public val pluginsPath: Path
/** /**
* 插件数据存放路径 * 插件自身存放路径 [File]. 由前端决定具体路径.
*
* **实现细节**: terminal 前端实现为 `$rootPath/plugins`
*/
public val pluginsFolder: File
/**
* 插件内部数据存放路径 [Path]
* *
* **实现细节**: terminal 前端实现为 `$rootPath/data` * **实现细节**: terminal 前端实现为 `$rootPath/data`
*
* @see pluginsDataFolder [File] 类型
*/ */
public val pluginsDataPath: Path 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) public fun Plugin.enable(): Unit = safeLoader.enable(this)
/** /**
* 经过泛型类型转换的 [PluginLoader] * 经过泛型类型转换的 [Plugin.loader]
*/ */
@get:JvmSynthetic @get:JvmSynthetic
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
public val <P : Plugin> P.safeLoader: PluginLoader<P, PluginDescription> public val <P : Plugin> P.safeLoader: PluginLoader<P, PluginDescription>
get() = this.loader as PluginLoader<P, PluginDescription> get() = this.loader as PluginLoader<P, PluginDescription>
// endregion
public companion object INSTANCE : PluginManager by PluginManagerImpl { public companion object INSTANCE : PluginManager by PluginManagerImpl {
// due to Kotlin's bug // due to Kotlin's bug
public override val Plugin.description: PluginDescription get() = PluginManagerImpl.run { description } public override val Plugin.description: PluginDescription get() = PluginManagerImpl.run { description }
@ -121,17 +154,3 @@ public interface PluginManager {
public override val <P : Plugin> P.safeLoader: PluginLoader<P, PluginDescription> get() = PluginManagerImpl.run { safeLoader } public override val <P : Plugin> P.safeLoader: PluginLoader<P, PluginDescription> 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()

View File

@ -24,6 +24,7 @@ import net.mamoe.mirai.console.data.PluginData
import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.Plugin
import net.mamoe.mirai.console.plugin.PluginFileExtensions import net.mamoe.mirai.console.plugin.PluginFileExtensions
import net.mamoe.mirai.console.plugin.ResourceContainer import net.mamoe.mirai.console.plugin.ResourceContainer
import net.mamoe.mirai.console.plugin.getDescription
import net.mamoe.mirai.utils.MiraiLogger import net.mamoe.mirai.utils.MiraiLogger
@ -48,7 +49,7 @@ public interface JvmPlugin : Plugin, CoroutineScope,
public val logger: MiraiLogger public val logger: MiraiLogger
/** 插件描述 */ /** 插件描述 */
public val description: JvmPluginDescription public val description: JvmPluginDescription get() = loader.getDescription(this)
/** 所属插件加载器实例 */ /** 所属插件加载器实例 */
@JvmDefault @JvmDefault