mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-11 02:50:15 +08:00
Introduce ResolveContext for permission and command
This commit is contained in:
parent
c1f1b6954a
commit
b0e082000c
@ -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.executeCommand
|
||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
|
||||||
import net.mamoe.mirai.console.command.java.JCommand
|
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.internal.command.createOrFindCommandPermission
|
||||||
import net.mamoe.mirai.console.permission.Permission
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
import net.mamoe.mirai.console.permission.PermissionId
|
import net.mamoe.mirai.console.permission.PermissionId
|
||||||
@ -37,12 +39,14 @@ public interface Command {
|
|||||||
*
|
*
|
||||||
* 不允许包含 [空格][Char.isWhitespace], '.', ':'.
|
* 不允许包含 [空格][Char.isWhitespace], '.', ':'.
|
||||||
*/
|
*/
|
||||||
|
@ResolveContext(COMMAND_NAME)
|
||||||
public val primaryName: String
|
public val primaryName: String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 次要指令名
|
* 次要指令名
|
||||||
* @see Command.primaryName 获取主指令名
|
* @see Command.primaryName 获取主指令名
|
||||||
*/
|
*/
|
||||||
|
@ResolveContext(COMMAND_NAME)
|
||||||
public val secondaryNames: Array<out String>
|
public val secondaryNames: Array<out String>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,7 +106,7 @@ public interface Command {
|
|||||||
* 检查指令名的合法性. 在非法时抛出 [IllegalArgumentException]
|
* 检查指令名的合法性. 在非法时抛出 [IllegalArgumentException]
|
||||||
*/
|
*/
|
||||||
@Throws(IllegalArgumentException::class)
|
@Throws(IllegalArgumentException::class)
|
||||||
public fun checkCommandName(name: String) {
|
public fun checkCommandName(@ResolveContext(COMMAND_NAME) name: String) {
|
||||||
when {
|
when {
|
||||||
name.isBlank() -> throw IllegalArgumentException("Command name should not be blank.")
|
name.isBlank() -> throw IllegalArgumentException("Command name should not be blank.")
|
||||||
name.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces is not yet allowed in command name.")
|
name.any { it.isWhitespace() } -> throw IllegalArgumentException("Spaces is not yet allowed in command name.")
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
package net.mamoe.mirai.console.command
|
package net.mamoe.mirai.console.command
|
||||||
|
|
||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.unregisterAllCommands
|
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.Permission
|
||||||
import net.mamoe.mirai.console.permission.PermissionId
|
import net.mamoe.mirai.console.permission.PermissionId
|
||||||
import net.mamoe.mirai.console.permission.PermissionIdNamespace
|
import net.mamoe.mirai.console.permission.PermissionIdNamespace
|
||||||
@ -36,5 +38,7 @@ public interface CommandOwner : PermissionIdNamespace {
|
|||||||
internal object ConsoleCommandOwner : CommandOwner {
|
internal object ConsoleCommandOwner : CommandOwner {
|
||||||
override val parentPermission: Permission get() = BuiltInCommands.parentPermission
|
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)
|
||||||
}
|
}
|
@ -18,6 +18,8 @@
|
|||||||
package net.mamoe.mirai.console.command
|
package net.mamoe.mirai.console.command
|
||||||
|
|
||||||
import net.mamoe.mirai.console.command.description.*
|
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.AbstractReflectionCommand
|
||||||
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
|
import net.mamoe.mirai.console.internal.command.CompositeCommandSubCommandAnnotationResolver
|
||||||
import net.mamoe.mirai.console.permission.Permission
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
@ -81,8 +83,8 @@ import kotlin.annotation.AnnotationTarget.FUNCTION
|
|||||||
*/
|
*/
|
||||||
public abstract class CompositeCommand(
|
public abstract class CompositeCommand(
|
||||||
owner: CommandOwner,
|
owner: CommandOwner,
|
||||||
primaryName: String,
|
@ResolveContext(COMMAND_NAME) primaryName: String,
|
||||||
vararg secondaryNames: String,
|
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
|
||||||
description: String = "no description available",
|
description: String = "no description available",
|
||||||
parentPermission: Permission = owner.parentPermission,
|
parentPermission: Permission = owner.parentPermission,
|
||||||
prefixOptional: Boolean = false,
|
prefixOptional: Boolean = false,
|
||||||
@ -106,7 +108,9 @@ public abstract class CompositeCommand(
|
|||||||
*/
|
*/
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
@Target(FUNCTION)
|
@Target(FUNCTION)
|
||||||
protected annotation class SubCommand(vararg val value: String)
|
protected annotation class SubCommand(
|
||||||
|
@ResolveContext(COMMAND_NAME) vararg val value: String,
|
||||||
|
)
|
||||||
|
|
||||||
/** 指令描述 */
|
/** 指令描述 */
|
||||||
@Retention(RUNTIME)
|
@Retention(RUNTIME)
|
||||||
|
@ -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.execute
|
||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
|
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.executeCommand
|
||||||
import net.mamoe.mirai.console.command.java.JRawCommand
|
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.internal.command.createOrFindCommandPermission
|
||||||
import net.mamoe.mirai.console.permission.Permission
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
import net.mamoe.mirai.message.data.MessageChain
|
import net.mamoe.mirai.message.data.MessageChain
|
||||||
@ -36,9 +37,9 @@ public abstract class RawCommand(
|
|||||||
*/
|
*/
|
||||||
public override val owner: CommandOwner,
|
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>",
|
public override val usage: String = "<no usages given>",
|
||||||
/** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */
|
/** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */
|
||||||
|
@ -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.CommandManager.INSTANCE.executeCommand
|
||||||
import net.mamoe.mirai.console.command.description.*
|
import net.mamoe.mirai.console.command.description.*
|
||||||
import net.mamoe.mirai.console.command.java.JSimpleCommand
|
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.AbstractReflectionCommand
|
||||||
import net.mamoe.mirai.console.internal.command.SimpleCommandSubCommandAnnotationResolver
|
import net.mamoe.mirai.console.internal.command.SimpleCommandSubCommandAnnotationResolver
|
||||||
import net.mamoe.mirai.console.permission.Permission
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
@ -50,8 +52,8 @@ import net.mamoe.mirai.message.data.MessageChain
|
|||||||
*/
|
*/
|
||||||
public abstract class SimpleCommand(
|
public abstract class SimpleCommand(
|
||||||
owner: CommandOwner,
|
owner: CommandOwner,
|
||||||
primaryName: String,
|
@ResolveContext(COMMAND_NAME) primaryName: String,
|
||||||
vararg secondaryNames: String,
|
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
|
||||||
description: String = "no description available",
|
description: String = "no description available",
|
||||||
parentPermission: Permission = owner.parentPermission,
|
parentPermission: Permission = owner.parentPermission,
|
||||||
prefixOptional: Boolean = false,
|
prefixOptional: Boolean = false,
|
||||||
|
@ -14,6 +14,8 @@ import net.mamoe.mirai.console.command.CommandManager
|
|||||||
import net.mamoe.mirai.console.command.CommandOwner
|
import net.mamoe.mirai.console.command.CommandOwner
|
||||||
import net.mamoe.mirai.console.command.CompositeCommand
|
import net.mamoe.mirai.console.command.CompositeCommand
|
||||||
import net.mamoe.mirai.console.command.description.buildCommandArgumentContext
|
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
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,8 +71,8 @@ import net.mamoe.mirai.console.permission.Permission
|
|||||||
public abstract class JCompositeCommand
|
public abstract class JCompositeCommand
|
||||||
@JvmOverloads constructor(
|
@JvmOverloads constructor(
|
||||||
owner: CommandOwner,
|
owner: CommandOwner,
|
||||||
primaryName: String,
|
@ResolveContext(COMMAND_NAME) primaryName: String,
|
||||||
vararg secondaryNames: String,
|
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
|
||||||
parentPermission: Permission = owner.parentPermission,
|
parentPermission: Permission = owner.parentPermission,
|
||||||
) : CompositeCommand(owner, primaryName, secondaryNames = secondaryNames, parentPermission = parentPermission) {
|
) : CompositeCommand(owner, primaryName, secondaryNames = secondaryNames, parentPermission = parentPermission) {
|
||||||
/** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */
|
/** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */
|
||||||
|
@ -13,6 +13,8 @@ import kotlinx.coroutines.Dispatchers
|
|||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import net.mamoe.mirai.console.command.*
|
import net.mamoe.mirai.console.command.*
|
||||||
import net.mamoe.mirai.console.command.CommandManager.INSTANCE.execute
|
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.internal.command.createOrFindCommandPermission
|
||||||
import net.mamoe.mirai.console.permission.Permission
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
import net.mamoe.mirai.message.data.MessageChain
|
import net.mamoe.mirai.message.data.MessageChain
|
||||||
@ -51,8 +53,8 @@ public abstract class JRawCommand
|
|||||||
* @see CommandOwner
|
* @see CommandOwner
|
||||||
*/
|
*/
|
||||||
public override val owner: CommandOwner,
|
public override val owner: CommandOwner,
|
||||||
/** 指令名. 需要至少有一个元素. 所有元素都不能带有空格 */
|
@ResolveContext(COMMAND_NAME) public override val primaryName: String,
|
||||||
public override vararg val secondaryNames: String,
|
@ResolveContext(COMMAND_NAME) public override vararg val secondaryNames: String,
|
||||||
parentPermission: Permission = owner.parentPermission,
|
parentPermission: Permission = owner.parentPermission,
|
||||||
) : Command {
|
) : Command {
|
||||||
/** 用法说明, 用于发送给用户 */
|
/** 用法说明, 用于发送给用户 */
|
||||||
|
@ -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.CommandOwner
|
||||||
import net.mamoe.mirai.console.command.SimpleCommand
|
import net.mamoe.mirai.console.command.SimpleCommand
|
||||||
import net.mamoe.mirai.console.command.description.CommandArgumentContext
|
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
|
import net.mamoe.mirai.console.permission.Permission
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,8 +44,8 @@ import net.mamoe.mirai.console.permission.Permission
|
|||||||
*/
|
*/
|
||||||
public abstract class JSimpleCommand(
|
public abstract class JSimpleCommand(
|
||||||
owner: CommandOwner,
|
owner: CommandOwner,
|
||||||
primaryName: String,
|
@ResolveContext(COMMAND_NAME) primaryName: String,
|
||||||
vararg secondaryNames: String,
|
@ResolveContext(COMMAND_NAME) vararg secondaryNames: String,
|
||||||
basePermission: Permission,
|
basePermission: Permission,
|
||||||
) : SimpleCommand(owner, primaryName, secondaryNames = secondaryNames, parentPermission = basePermission) {
|
) : SimpleCommand(owner, primaryName, secondaryNames = secondaryNames, parentPermission = basePermission) {
|
||||||
public override var description: String = super.description
|
public override var description: String = super.description
|
||||||
|
@ -36,6 +36,11 @@ public annotation class ResolveContext(
|
|||||||
PLUGIN_NAME,
|
PLUGIN_NAME,
|
||||||
PLUGIN_VERSION,
|
PLUGIN_VERSION,
|
||||||
|
|
||||||
|
COMMAND_NAME,
|
||||||
|
|
||||||
|
PERMISSION_NAMESPACE,
|
||||||
|
PERMISSION_NAME,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Custom serializers allowed
|
* Custom serializers allowed
|
||||||
*/
|
*/
|
||||||
|
@ -12,6 +12,9 @@ package net.mamoe.mirai.console.permission
|
|||||||
import kotlinx.serialization.KSerializer
|
import kotlinx.serialization.KSerializer
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.builtins.serializer
|
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
|
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)
|
@Serializable(with = PermissionId.PermissionIdAsStringSerializer::class)
|
||||||
public data class PermissionId(
|
public data class PermissionId(
|
||||||
public val namespace: String,
|
@ResolveContext(PERMISSION_NAMESPACE) public val namespace: String,
|
||||||
public val name: String,
|
@ResolveContext(PERMISSION_NAME) public val name: String,
|
||||||
) {
|
) {
|
||||||
init {
|
init {
|
||||||
require(!namespace.contains(':')) {
|
require(!namespace.contains(':')) {
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
package net.mamoe.mirai.console.permission
|
package net.mamoe.mirai.console.permission
|
||||||
|
|
||||||
import net.mamoe.mirai.console.command.Command
|
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].
|
* [PermissionId] 的命名空间. 用于提供 [PermissionId.namespace].
|
||||||
@ -20,5 +22,5 @@ public interface PermissionIdNamespace {
|
|||||||
*
|
*
|
||||||
* 在指令初始化时, 会申请对应权限. 此时 [name] 为 "command.$primaryName` 其中 [primaryName][Command.primaryName].
|
* 在指令初始化时, 会申请对应权限. 此时 [name] 为 "command.$primaryName` 其中 [primaryName][Command.primaryName].
|
||||||
*/
|
*/
|
||||||
public fun permissionId(name: String): PermissionId
|
public fun permissionId(@ResolveContext(PERMISSION_NAME) name: String): PermissionId
|
||||||
}
|
}
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.console.permission
|
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.extensions.PermissionServiceProvider
|
||||||
import net.mamoe.mirai.console.internal.permission.checkType
|
import net.mamoe.mirai.console.internal.permission.checkType
|
||||||
import net.mamoe.mirai.console.permission.Permission.Companion.parentsWithSelf
|
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 =
|
public fun <P : Permission> PermissionService<P>.getOrFail(id: PermissionId): P =
|
||||||
get(id) ?: throw NoSuchElementException("Permission not found: $id")
|
get(id) ?: throw NoSuchElementException("Permission not found: $id")
|
||||||
|
|
||||||
internal fun PermissionService<*>.allocatePermissionIdForPlugin(name: String, id: String) =
|
internal fun PermissionService<*>.allocatePermissionIdForPlugin(pluginName: String, @ResolveContext(COMMAND_NAME) permissionName: String) =
|
||||||
PermissionId("plugin.${name.toLowerCase()}", id.toLowerCase())
|
PermissionId("plugin.${pluginName.toLowerCase()}", permissionName.toLowerCase())
|
||||||
|
|
||||||
public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this]
|
public fun PermissionId.findCorrespondingPermission(): Permission? = INSTANCE[this]
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ public interface PermitteeId {
|
|||||||
* ExactTemp
|
* ExactTemp
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@Serializable(with = AbstractPermitteeId.AsStringSerializer::class)
|
@Serializable(with = AsStringSerializer::class)
|
||||||
public sealed class AbstractPermitteeId(
|
public sealed class AbstractPermitteeId(
|
||||||
public final override vararg val directParents: PermitteeId,
|
public final override vararg val directParents: PermitteeId,
|
||||||
) : PermitteeId {
|
) : PermitteeId {
|
||||||
|
Loading…
Reference in New Issue
Block a user