Просмотр исходного кода

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 <[email protected]>

* improve hint(2/2)

Co-authored-by: Him188 <[email protected]>

* modify as require

* Update BuiltInJvmPluginLoaderImpl.kt

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

Co-authored-by: Him188 <[email protected]>

* 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 <[email protected]>
Eritque arcus 4 лет назад
Родитель
Сommit
22e2ea18e6

+ 29 - 1
backend/mirai-console/src/internal/plugin/BuiltInJvmPluginLoaderImpl.kt

@@ -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()
     }
-}
+}

+ 1 - 1
backend/mirai-console/src/internal/plugin/JvmPluginInternal.kt

@@ -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 {

+ 54 - 0
backend/mirai-console/test/data/PluginMovingTests.kt

@@ -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)
+    }
+}