Check forbidden plugin names

This commit is contained in:
Him188 2020-09-06 11:54:16 +08:00
parent 9add8f71ef
commit 52d417ad0e
3 changed files with 27 additions and 4 deletions

View File

@ -15,6 +15,8 @@ import kotlinx.coroutines.*
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.data.runCatchingLog
import net.mamoe.mirai.console.internal.data.mkdir
import net.mamoe.mirai.console.permission.ExperimentalPermission
import net.mamoe.mirai.console.permission.Identifier
import net.mamoe.mirai.console.plugin.Plugin
import net.mamoe.mirai.console.plugin.PluginManager
import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader
@ -41,7 +43,13 @@ internal abstract class JvmPluginInternal(
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)
final override fun getResourceAsStream(path: String): InputStream? =
resourceContainerDelegate.getResourceAsStream(path)
@OptIn(ExperimentalPermission::class)
override fun allocate(identifierString: String): Identifier {
return Identifier(description.name)
}
// region JvmPlugin
final override val logger: MiraiLogger by lazy {
@ -62,11 +70,11 @@ internal abstract class JvmPluginInternal(
dataFolderPath.toFile()
}
override val configFolderPath: Path by lazy {
final override val configFolderPath: Path by lazy {
PluginManager.pluginsConfigPath.resolve(description.name).apply { mkdir() }
}
override val configFolder: File by lazy {
final override val configFolder: File by lazy {
configFolderPath.toFile()
}

View File

@ -159,6 +159,13 @@ internal object PluginManagerImpl : PluginManager, CoroutineScope by MiraiConsol
}
}
@kotlin.jvm.Throws(PluginLoadException::class)
internal fun checkPluginDescription(description: PluginDescription) {
when (description.name.toLowerCase()) {
"main", "console", "plugin", "config", "data" -> throw PluginLoadException("Plugin name '${description.name}' is forbidden.")
}
}
/**
* @return [builtInLoaders] 可以加载的插件. 已经完成了 [PluginLoader.load], 但没有 [PluginLoader.enable]
*/
@ -171,6 +178,7 @@ internal object PluginManagerImpl : PluginManager, CoroutineScope by MiraiConsol
.onEach { (loader, descriptions) ->
loader as PluginLoader<Plugin, PluginDescription>
descriptions.forEach(PluginManagerImpl::checkPluginDescription)
descriptions.filter { it.kind == PluginKind.LOADER }.sortByDependencies().loadAndEnableAllInOrder()
}
.flatMap { it.second.asSequence() }

View File

@ -27,7 +27,14 @@ public interface PluginDescription {
public val kind: PluginKind
/**
* 插件名称. 不允许存在 ":"
* 插件名称. 不允许存在 ":".
*
* 插件名称不能完全是以下其中一种.
* - console
* - main
* - plugin
* - config
* - data
*/
public val name: String