Introduce ResolveContext for permission and command

This commit is contained in:
Him188 2020-09-18 23:50:02 +08:00
parent c1f1b6954a
commit b0e082000c
13 changed files with 54 additions and 21 deletions

View File

@ -15,6 +15,8 @@ import net.mamoe.kjbb.JvmBlockingBridge
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
import net.mamoe.mirai.console.command.java.JCommand
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.internal.command.createOrFindCommandPermission
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
@ -37,12 +39,14 @@ public interface Command {
*
* 不允许包含 [空格][Char.isWhitespace], '.', ':'.
*/
@ResolveContext(COMMAND_NAME)
public val primaryName: String
/**
* 次要指令名
* @see Command.primaryName 获取主指令名
*/
@ResolveContext(COMMAND_NAME)
public val secondaryNames: Array<out String>
/**
@ -102,7 +106,7 @@ public interface Command {
* 检查指令名的合法性. 在非法时抛出 [IllegalArgumentException]
*/
@Throws(IllegalArgumentException::class)
public fun checkCommandName(name: String) {
public fun checkCommandName(@ResolveContext(COMMAND_NAME) name: String) {
when {
name.isBlank() -> throw IllegalArgumentException("Command name should not be blank.")
name.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces is not yet allowed in command name.")

View File

@ -10,6 +10,8 @@
package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterAllCommands
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.PERMISSION_NAME
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermissionIdNamespace
@ -36,5 +38,7 @@ public interface CommandOwner : PermissionIdNamespace {
internal object ConsoleCommandOwner : CommandOwner {
override val parentPermission: Permission get() = BuiltInCommands.parentPermission
override fun permissionId(name: String): PermissionId = PermissionId("console", name)
override fun permissionId(
@ResolveContext(PERMISSION_NAME) name: String,
): PermissionId = PermissionId("console", name)
}

View File

@ -18,6 +18,8 @@
package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.description.*
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.internal.command.AbstractReflectionCommand
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.Permission
@ -81,8 +83,8 @@ import kotlin.annotation.AnnotationTarget.FUNCTION
*/
public abstract class CompositeCommand(
owner: CommandOwner,
primaryName: String,
vararg secondaryNames: String,
@ResolveContext(COMMAND_NAME) primaryName: String,
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
description: String = "no description available",
parentPermission: Permission = owner.parentPermission,
prefixOptional: Boolean = false,
@ -106,7 +108,9 @@ public abstract class CompositeCommand(
*/
@Retention(RUNTIME)
@Target(FUNCTION)
protected annotation class SubCommand(vararg val value: String)
protected annotation class SubCommand(
@ResolveContext(COMMAND_NAME) vararg val value: String,
)
/** 指令描述 */
@Retention(RUNTIME)

View File

@ -14,6 +14,7 @@ package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.java.JRawCommand
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.internal.command.createOrFindCommandPermission
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.message.data.MessageChain
@ -36,9 +37,9 @@ public abstract class RawCommand(
*/
public override val owner: CommandOwner,
/** 主指令名. */
public override val primaryName: String,
@ResolveContext(ResolveContext.Kind.COMMAND_NAME) public override val primaryName: String,
/** 次要指令名. */
public override vararg val secondaryNames: String,
@ResolveContext(ResolveContext.Kind.COMMAND_NAME) public override vararg val secondaryNames: String,
/** 用法说明, 用于发送给用户 */
public override val usage: String = "<no usages given>",
/** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */

View File

@ -20,6 +20,8 @@ package net.mamoe.mirai.console.command
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.description.*
import net.mamoe.mirai.console.command.java.JSimpleCommand
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.internal.command.AbstractReflectionCommand
import net.mamoe.mirai.console.internal.command.SimpleCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.permission.Permission
@ -50,8 +52,8 @@ import net.mamoe.mirai.message.data.MessageChain
*/
public abstract class SimpleCommand(
owner: CommandOwner,
primaryName: String,
vararg secondaryNames: String,
@ResolveContext(COMMAND_NAME) primaryName: String,
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
description: String = "no description available",
parentPermission: Permission = owner.parentPermission,
prefixOptional: Boolean = false,

View File

@ -14,6 +14,8 @@ import net.mamoe.mirai.console.command.CommandManager
import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.command.CompositeCommand
import net.mamoe.mirai.console.command.description.buildCommandArgumentContext
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.permission.Permission
/**
@ -69,8 +71,8 @@ import net.mamoe.mirai.console.permission.Permission
public abstract class JCompositeCommand
@JvmOverloads constructor(
owner: CommandOwner,
primaryName: String,
vararg secondaryNames: String,
@ResolveContext(COMMAND_NAME) primaryName: String,
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
parentPermission: Permission = owner.parentPermission,
) : CompositeCommand(owner, primaryName, secondaryNames = secondaryNames, parentPermission = parentPermission) {
/** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */

View File

@ -13,6 +13,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.internal.command.createOrFindCommandPermission
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.message.data.MessageChain
@ -51,8 +53,8 @@ public abstract class JRawCommand
* @see CommandOwner
*/
public override val owner: CommandOwner,
/** 指令名. 需要至少有一个元素. 所有元素都不能带有空格 */
public override vararg val secondaryNames: String,
@ResolveContext(COMMAND_NAME) public override val primaryName: String,
@ResolveContext(COMMAND_NAME) public override vararg val secondaryNames: String,
parentPermission: Permission = owner.parentPermission,
) : Command {
/** 用法说明, 用于发送给用户 */

View File

@ -14,6 +14,8 @@ import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
import net.mamoe.mirai.console.command.CommandOwner
import net.mamoe.mirai.console.command.SimpleCommand
import net.mamoe.mirai.console.command.description.CommandArgumentContext
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.permission.Permission
/**
@ -42,8 +44,8 @@ import net.mamoe.mirai.console.permission.Permission
*/
public abstract class JSimpleCommand(
owner: CommandOwner,
primaryName: String,
vararg secondaryNames: String,
@ResolveContext(COMMAND_NAME) primaryName: String,
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
basePermission: Permission,
) : SimpleCommand(owner, primaryName, secondaryNames = secondaryNames, parentPermission = basePermission) {
public override var description: String = super.description

View File

@ -36,6 +36,11 @@ public annotation class ResolveContext(
PLUGIN_NAME,
PLUGIN_VERSION,
COMMAND_NAME,
PERMISSION_NAMESPACE,
PERMISSION_NAME,
/**
* Custom serializers allowed
*/

View File

@ -12,6 +12,9 @@ package net.mamoe.mirai.console.permission
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.serializer
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.PERMISSION_NAME
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.PERMISSION_NAMESPACE
import net.mamoe.mirai.console.internal.data.map
@ -25,8 +28,8 @@ import net.mamoe.mirai.console.internal.data.map
*/
@Serializable(with = PermissionId.PermissionIdAsStringSerializer::class)
public data class PermissionId(
public val namespace: String,
public val name: String,
@ResolveContext(PERMISSION_NAMESPACE) public val namespace: String,
@ResolveContext(PERMISSION_NAME) public val name: String,
) {
init {
require(!namespace.contains(':')) {

View File

@ -10,6 +10,8 @@
package net.mamoe.mirai.console.permission
import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.PERMISSION_NAME
/**
* [PermissionId] 的命名空间. 用于提供 [PermissionId.namespace].
@ -20,5 +22,5 @@ public interface PermissionIdNamespace {
*
* 在指令初始化时, 会申请对应权限. 此时 [name] "command.$primaryName` 其中 [primaryName][Command.primaryName].
*/
public fun permissionId(name: String): PermissionId
public fun permissionId(@ResolveContext(PERMISSION_NAME) name: String): PermissionId
}

View File

@ -11,6 +11,8 @@
package net.mamoe.mirai.console.permission
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.extensions.PermissionServiceProvider
import net.mamoe.mirai.console.internal.permission.checkType
import net.mamoe.mirai.console.permission.Permission.Companion.parentsWithSelf
@ -129,8 +131,8 @@ public interface PermissionService<P : Permission> {
public fun <P : Permission> PermissionService<P>.getOrFail(id: PermissionId): P =
get(id) ?: throw NoSuchElementException("Permission not found: $id")
internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) =
PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase())
internal fun PermissionService<*>.allocatePermissionIdForPlugin(pluginName: String, @ResolveContext(COMMAND_NAME) permissionName: String) =
PermissionId("plugin.${pluginName.toLowerCase()}", permissionName.toLowerCase())
public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this]

View File

@ -169,7 +169,7 @@ public interface PermitteeId {
* ExactTemp
* ```
*/
@Serializable(with = AbstractPermitteeId.AsStringSerializer::class)
@Serializable(with = AsStringSerializer::class)
public sealed class AbstractPermitteeId(
public final override vararg val directParents: PermitteeId,
) : PermitteeId {