Using PluginId as folder name (#418)

* using pluginId as folder name

* using pluginId as folder name

* if failure, abort

* move to `BuiltInJvmPluginLoaderImpl`

* improve hint(1/2)

Co-authored-by: Him188 <Him188@mamoe.net>

* improve hint(2/2)

Co-authored-by: Him188 <Him188@mamoe.net>

* modify as require

* Update BuiltInJvmPluginLoaderImpl.kt

* Update backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt

Co-authored-by: Him188 <Him188@mamoe.net>

* modify as requirements, tested

* upload tests

* bypass

* change function name and except cancel

* support moving to empty folder, tested

* modify

* Fix reference

Co-authored-by: Him188 <Him188@mamoe.net>
This commit is contained in:
Eritque arcus 2021-12-04 03:37:43 +08:00 committed by GitHub
parent 0675ccbbd8
commit 22e2ea18e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 2 deletions

View File

@ -17,6 +17,7 @@ import net.mamoe.mirai.console.data.PluginDataStorage
import net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge
import net.mamoe.mirai.console.internal.util.PluginServiceHelper.findServices
import net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadAllServices
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.jvm.*
import net.mamoe.mirai.console.plugin.loader.AbstractFilePluginLoader
import net.mamoe.mirai.console.plugin.loader.PluginLoadException
@ -111,6 +112,33 @@ internal object BuiltInJvmPluginLoaderImpl :
}.getOrElse {
throw PluginLoadException("Exception while loading ${plugin.description.smartToString()}", it)
}
val nameFolder = PluginManager.pluginsDataPath.resolve(plugin.description.name).toFile()
if (plugin.description.name != plugin.description.id && nameFolder.exists()) {
// need move
val idFolder = PluginManager.pluginsDataPath.resolve(plugin.description.id).toFile()
val moveDescription = "移动 ${plugin.description.smartToString()} 的配置目录(${nameFolder.path})到 ${idFolder.path}"
if (idFolder.exists()) {
if (idFolder.listFiles()?.size != 0) {
logger.error("$moveDescription 失败, 原因:配置目录(${idFolder.path})被占用")
logger.error("Mirai Console 将自动关闭, 请删除或移动该目录后再启动")
MiraiConsole.job.cancel()
} else
idFolder.delete()
}
kotlin.runCatching {
logger.info(moveDescription)
if (!nameFolder.renameTo(idFolder)) {
logger.error("$moveDescription 失败")
logger.error("Mirai Console 将自动关闭, 请手动移动该文件夹后再启动")
MiraiConsole.job.cancel()
}
}.onFailure {
logger.error("$moveDescription 失败, 原因:\n", it)
logger.error("Mirai Console 将自动关闭, 请解决该错误后再启动")
MiraiConsole.job.cancel()
}
logger.info("$moveDescription 完成")
}
}
override fun enable(plugin: JvmPlugin) {
@ -139,4 +167,4 @@ internal object BuiltInJvmPluginLoaderImpl :
plugin.internalOnDisable()
} else plugin.onDisable()
}
}
}

View File

@ -68,7 +68,7 @@ internal abstract class JvmPluginInternal(
private var firstRun = true
final override val dataFolderPath: Path by lazy {
PluginManager.pluginsDataPath.resolve(description.name).apply { mkdir() }
PluginManager.pluginsDataPath.resolve(description.id).apply { mkdir() }
}
final override val dataFolder: File by lazy {

View File

@ -0,0 +1,54 @@
/*
* Copyright 2019-2021 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/dev/LICENSE
*/
package net.mamoe.mirai.console.data
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.framework.AbstractConsoleTest
import net.mamoe.mirai.console.internal.data.mkdir
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.load
import net.mamoe.mirai.console.plugin.id
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.console.plugin.name
import org.junit.jupiter.api.Test
class PluginMovingTests : AbstractConsoleTest() {
private val mockPluginWithName = object : KotlinPlugin(JvmPluginDescription("org.test1.test1", "1.0.0", "test1")) {}
private val mockPluginWithName2 =
object : KotlinPlugin(JvmPluginDescription("org.test2.test2", "1.0.0", "test2")) {}
private val mockPluginWithName3 =
object : KotlinPlugin(JvmPluginDescription("org.test2.test3", "1.0.0", "test3")) {}
private fun mkdir(abstractPath: String) = PluginManager.pluginsDataPath.resolve(abstractPath).mkdir()
@Test
fun movingPluginPath() {
// Normal move
mkdir(mockPlugin.name)
mockPlugin.load()
assert(!MiraiConsole.job.isCancelled)
// when id == name
mkdir(mockPluginWithName.name)
mockPluginWithName.load()
assert(!MiraiConsole.job.isCancelled)
// move to empty folder
mkdir(mockPluginWithName2.name)
mkdir(mockPluginWithName2.id)
mockPluginWithName2.load()
assert(!MiraiConsole.job.isCancelled)
// fail move
mkdir(mockPluginWithName3.name)
mkdir(mockPluginWithName3.id)
PluginManager.pluginsDataPath.resolve(mockPluginWithName3.id).toFile().resolve("x").createNewFile()
mockPluginWithName3.load()
assert(MiraiConsole.job.isCancelled)
}
}