From 2926e55590cb6f6664573c933a89d04ab261cee9 Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 9 Sep 2020 10:02:56 +0800 Subject: [PATCH] Introduce parentPermission for commands --- .../net/mamoe/mirai/console/command/Command.kt | 4 ++-- .../net/mamoe/mirai/console/command/CommandOwner.kt | 12 +++++++++++- .../mamoe/mirai/console/command/CompositeCommand.kt | 4 ++-- .../net/mamoe/mirai/console/command/RawCommand.kt | 6 +++--- .../mamoe/mirai/console/command/SimpleCommand.kt | 2 +- .../mirai/console/command/java/JCompositeCommand.kt | 4 ++-- .../mamoe/mirai/console/command/java/JRawCommand.kt | 4 ++-- .../internal/command/CompositeCommandInternal.kt | 6 +++--- .../mirai/console/internal/command/internal.kt | 7 ++----- .../console/internal/plugin/JvmPluginInternal.kt | 13 +++++++++++++ .../AbstractConcurrentPermissionService.kt | 4 ++-- .../console/permission/BuiltInPermissionServices.kt | 10 +++++----- .../mamoe/mirai/console/permission/Permission.kt | 7 ++++--- .../mamoe/mirai/console/permission/PermissionId.kt | 3 +++ .../mirai/console/permission/PermissionService.kt | 5 ++++- .../console/plugin/description/PluginDescription.kt | 2 +- 16 files changed, 60 insertions(+), 33 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt index 096be5791..237376a1b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/Command.kt @@ -105,7 +105,7 @@ public abstract class AbstractCommand public override val owner: CommandOwner, vararg names: String, description: String = "", - basePermission: PermissionId? = null, + parentPermission: PermissionId = owner.basePermission, /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ public override val prefixOptional: Boolean = false ) : Command { @@ -116,5 +116,5 @@ public abstract class AbstractCommand }.toTypedArray() @OptIn(ExperimentalPermission::class) - public override val permission: Permission by lazy { createCommandPermission(basePermission) } + public override val permission: Permission by lazy { createCommandPermission(parentPermission) } } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandOwner.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandOwner.kt index 49932f8df..ba6e0d6cf 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandOwner.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CommandOwner.kt @@ -10,6 +10,7 @@ package net.mamoe.mirai.console.command import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterAllCommands +import net.mamoe.mirai.console.permission.BasePermission import net.mamoe.mirai.console.permission.ExperimentalPermission import net.mamoe.mirai.console.permission.PermissionId import net.mamoe.mirai.console.permission.PermissionIdNamespace @@ -23,12 +24,21 @@ import net.mamoe.mirai.console.plugin.jvm.JvmPlugin * * @see JvmPlugin 是一个 [CommandOwner] */ -public interface CommandOwner : PermissionIdNamespace +public interface CommandOwner : PermissionIdNamespace { + /** + * 此 [PermissionIdNamespace] 拥有的指令都默认将 [basePermission] 作为父权限. + * + * TODO document + */ + public val basePermission: PermissionId +} /** * 代表控制台所有者. 所有的 mirai-console 内建的指令都属于 [ConsoleCommandOwner]. */ internal object ConsoleCommandOwner : CommandOwner { + override val basePermission: PermissionId get() = BasePermission.id + @ExperimentalPermission override fun permissionId(id: String): PermissionId = PermissionId("console", id) } \ No newline at end of file diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt index 26a71f9d8..6689d0712 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/CompositeCommand.kt @@ -86,10 +86,10 @@ public abstract class CompositeCommand( owner: CommandOwner, vararg names: String, description: String = "no description available", - basePermission: PermissionId? = null, + parentPermission: PermissionId = owner.basePermission, prefixOptional: Boolean = false, overrideContext: CommandArgumentContext = EmptyCommandArgumentContext -) : Command, AbstractReflectionCommand(owner, names, description, basePermission, prefixOptional), +) : Command, AbstractReflectionCommand(owner, names, description, parentPermission, prefixOptional), CommandArgumentContextAware { /** diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt index fd1a36515..a1ad43cc0 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/RawCommand.kt @@ -43,13 +43,13 @@ public abstract class RawCommand( public override val usage: String = "", /** 指令描述, 用于显示在 [BuiltInCommands.Help] */ public override val description: String = "", - /** 指令权限 */ - basePermission: PermissionId? = null, + /** 指令父权限 */ + parentPermission: PermissionId = owner.basePermission, /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ public override val prefixOptional: Boolean = false ) : Command { @OptIn(ExperimentalPermission::class) - public override val permission: Permission by lazy { createCommandPermission(basePermission) } + public override val permission: Permission by lazy { createCommandPermission(parentPermission) } /** * 在指令被执行时调用. diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt index 9c83c9228..cafad6148 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/SimpleCommand.kt @@ -52,7 +52,7 @@ public abstract class SimpleCommand( owner: CommandOwner, vararg names: String, description: String = "no description available", - basePermission: PermissionId? = null, + basePermission: PermissionId = owner.basePermission, prefixOptional: Boolean = false, overrideContext: CommandArgumentContext = EmptyCommandArgumentContext ) : Command, AbstractReflectionCommand(owner, names, description, basePermission, prefixOptional), diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt index ef400b3c0..2b1dbb076 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JCompositeCommand.kt @@ -72,8 +72,8 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI public abstract class JCompositeCommand @JvmOverloads constructor( owner: CommandOwner, vararg names: String, - basePermission: PermissionId? = null, -) : CompositeCommand(owner, *names, basePermission = basePermission) { + parentPermission: PermissionId = owner.basePermission, +) : CompositeCommand(owner, *names, parentPermission = parentPermission) { /** 指令描述, 用于显示在 [BuiltInCommands.Help] */ public final override var description: String = "" protected set diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt index f53daf865..ec9f894ca 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JRawCommand.kt @@ -53,7 +53,7 @@ public abstract class JRawCommand @JvmOverloads constructor( public override val owner: CommandOwner, /** 指令名. 需要至少有一个元素. 所有元素都不能带有空格 */ public override vararg val names: String, - basePermission: PermissionId? = null, + parentPermission: PermissionId = owner.basePermission, ) : Command { /** 用法说明, 用于发送给用户 */ public override var usage: String = "" @@ -64,7 +64,7 @@ public abstract class JRawCommand @JvmOverloads constructor( protected set /** 指令权限 */ - public final override var permission: Permission = createCommandPermission(basePermission) + public final override var permission: Permission = createCommandPermission(parentPermission) protected set /** 为 `true` 时表示 [指令前缀][CommandManager.commandPrefix] 可选 */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt index cda7b6fd4..6e5564b7a 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/CompositeCommandInternal.kt @@ -48,13 +48,13 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( owner: CommandOwner, names: Array, description: String = "", - basePermission: PermissionId? = null, + parentPermission: PermissionId = owner.basePermission, prefixOptional: Boolean = false ) : Command, AbstractCommand( owner, names = names, description = description, - basePermission = basePermission, + parentPermission = parentPermission, prefixOptional = prefixOptional ), CommandArgumentContextAware { internal abstract val subCommandAnnotationResolver: SubCommandAnnotationResolver @@ -74,7 +74,7 @@ internal abstract class AbstractReflectionCommand @JvmOverloads constructor( internal val defaultSubCommand: DefaultSubCommandDescriptor by lazy { DefaultSubCommandDescriptor( "", - createCommandPermission(basePermission), + createCommandPermission(parentPermission), onCommand = { sender: CommandSender, args: MessageChain -> sender.onDefault(args) } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt index f89fc1731..c6a2bc374 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/internal/command/internal.kt @@ -139,15 +139,12 @@ internal fun Group.fuzzySearchMember( } @OptIn(ExperimentalPermission::class) -internal fun Command.createCommandPermission(basePermission: PermissionId?): Permission { - return PermissionService.INSTANCE.register(owner.permissionId(primaryName), description, basePermission) +internal fun Command.createCommandPermission(parent: PermissionId): Permission { + return PermissionService.INSTANCE.register(owner.permissionId(primaryName), description, parent) } //// internal -@JvmSynthetic -internal inline fun List.dropToTypedArray(n: Int): Array = Array(size - n) { this[n + it] } - @OptIn(ExperimentalPermission::class) @JvmSynthetic @Throws(CommandExecutionException::class) 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 05c7f5127..e200e05b8 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 @@ -17,11 +17,14 @@ 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.PermissionId +import net.mamoe.mirai.console.permission.PermissionService +import net.mamoe.mirai.console.permission.allocatePermissionIdForPlugin import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.PluginManager import net.mamoe.mirai.console.plugin.PluginManager.INSTANCE.safeLoader import net.mamoe.mirai.console.plugin.ResourceContainer.Companion.asResourceContainer import net.mamoe.mirai.console.plugin.jvm.JvmPlugin +import net.mamoe.mirai.console.plugin.name import net.mamoe.mirai.console.util.NamedSupervisorJob import net.mamoe.mirai.utils.MiraiLogger import java.io.File @@ -40,6 +43,14 @@ internal abstract class JvmPluginInternal( parentCoroutineContext: CoroutineContext ) : JvmPlugin, CoroutineScope { + @OptIn(ExperimentalPermission::class) + final override val basePermission: PermissionId by lazy { + PermissionService.INSTANCE.register( + PermissionService.INSTANCE.allocatePermissionIdForPlugin(name, "*"), + "The base permission" + ).id + } + final override var isEnabled: Boolean = false private val resourceContainerDelegate by lazy { this::class.java.classLoader.asResourceContainer() } @@ -99,6 +110,7 @@ internal abstract class JvmPluginInternal( } internal fun internalOnEnable(): Boolean { + basePermission if (!firstRun) refreshCoroutineContext() kotlin.runCatching { onEnable() @@ -124,6 +136,7 @@ internal abstract class JvmPluginInternal( internal val _intrinsicCoroutineContext: CoroutineContext by lazy { CoroutineName("Plugin $dataHolderName") } + @JvmField internal val coroutineContextInitializer = { CoroutineExceptionHandler { context, throwable -> diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt index fe5022af0..1e0085e29 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/AbstractConcurrentPermissionService.kt @@ -20,12 +20,12 @@ public abstract class AbstractConcurrentPermissionService

: Perm protected abstract fun createPermission( id: PermissionId, description: String, - base: PermissionId? + base: PermissionId = BasePermission.id ): P override fun get(id: PermissionId): P? = permissions[id] - override fun register(id: PermissionId, description: String, base: PermissionId?): P { + override fun register(id: PermissionId, description: String, base: PermissionId): P { grantedPermissionsMap[id] = CopyOnWriteArrayList() // mutations are not quite often performed val instance = createPermission(id, description, base) if (permissions.putIfAbsent(id, instance) != null) { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt index 1a69b2e71..e286fcc1f 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/BuiltInPermissionServices.kt @@ -32,7 +32,7 @@ public object AllGrantPermissionService : PermissionService { override fun register( id: PermissionId, description: String, - base: PermissionId? + base: PermissionId ): PermissionImpl { val new = PermissionImpl(id, description, base) if (all.putIfAbsent(id, new) != null) { @@ -48,7 +48,7 @@ public object AllGrantPermissionService : PermissionService { override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) { } - override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl): Boolean = + override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permissionId: PermissionId): Boolean = true override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) { @@ -64,7 +64,7 @@ public object AllDenyPermissionService : PermissionService { override fun register( id: PermissionId, description: String, - base: PermissionId? + base: PermissionId ): PermissionImpl { val new = PermissionImpl(id, description, base) if (all.putIfAbsent(id, new) != null) { @@ -80,7 +80,7 @@ public object AllDenyPermissionService : PermissionService { override fun grant(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) { } - override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl): Boolean = + override fun testPermission(permissibleIdentifier: PermissibleIdentifier, permissionId: PermissionId): Boolean = false override fun deny(permissibleIdentifier: PermissibleIdentifier, permission: PermissionImpl) { @@ -100,7 +100,7 @@ internal object BuiltInPermissionService : AbstractConcurrentPermissionService

> get() = config.grantedPermissionMap as MutableMap> - override fun createPermission(id: PermissionId, description: String, base: PermissionId?): PermissionImpl = + override fun createPermission(id: PermissionId, description: String, base: PermissionId): PermissionImpl = PermissionImpl(id, description, base) internal val config: ConcurrentSaveData = diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt index c51d96dfc..2abbeae7c 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/Permission.kt @@ -14,7 +14,9 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalAPI /** - * 一个权限节点 + * 一个权限节点. + * + * 由 [PermissionService] 实现不同, [Permission] 可能会有多种实例. 但一个权限总是拥有确定的 [id] */ @ExperimentalPermission public interface Permission { @@ -29,10 +31,9 @@ public interface Permission { @ExperimentalPermission public object BasePermission : Permission { - override val id: PermissionId = PermissionId("console", "base") + override val id: PermissionId = PermissionId("*", "*") override val description: String get() = "The parent of any permission" override val parent: PermissionId get() = id - } @ConsoleExperimentalAPI diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt index e3f01f193..0b88192cb 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionId.kt @@ -16,6 +16,9 @@ import kotlinx.serialization.builtins.serializer import net.mamoe.mirai.console.internal.data.map +/** + * [PermissionId] 与 [Permission] 唯一对应. + */ @Serializable @ExperimentalPermission public data class PermissionId( diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt index 7d770db76..3aa630c3d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionService.kt @@ -43,7 +43,7 @@ public interface PermissionService

{ public fun register( id: PermissionId, description: String, - base: PermissionId? = null + base: PermissionId = BasePermission.id ): P /////////////////////////////////////////////////////////////////////////// @@ -60,6 +60,9 @@ public interface PermissionService

{ } } +internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) = + PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase()) + @ExperimentalPermission public fun PermissionId.findCorrespondingPermission(): Permission? = PermissionService.INSTANCE[this] diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt index 64e9058f2..c9e40ad0d 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/plugin/description/PluginDescription.kt @@ -27,7 +27,7 @@ public interface PluginDescription { public val kind: PluginKind /** - * 插件名称. 不允许存在 ":". + * 插件名称. 不允许存在 ":", 推荐全英文. * * 插件名称不能完全是以下其中一种. * - console