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 1c308c530..9d39cfa7e 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 @@ -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 /** @@ -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.") 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 f9c627edf..f59033c95 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,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) } \ 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 27ed74c57..21fbd2239 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 @@ -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) 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 3e967be97..6133e5da2 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 @@ -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 = "", /** 指令描述, 用于显示在 [BuiltInCommands.HelpCommand] */ 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 2a49ebcb0..27bb0f6e3 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 @@ -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, 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 09bc507e6..d09d2275c 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 @@ -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] */ 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 8f773cd51..84b06ea5c 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 @@ -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 { /** 用法说明, 用于发送给用户 */ diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt index e7a3eb3fd..98e650ebd 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/java/JSimpleCommand.kt @@ -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 diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt index 61e481370..714c40f9e 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/compiler/common/ResolveContext.kt @@ -36,6 +36,11 @@ public annotation class ResolveContext( PLUGIN_NAME, PLUGIN_VERSION, + COMMAND_NAME, + + PERMISSION_NAMESPACE, + PERMISSION_NAME, + /** * Custom serializers allowed */ 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 2d527d1e2..abac4bf12 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 @@ -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(':')) { diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionIdNamespace.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionIdNamespace.kt index 3b4c16777..90a043608 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionIdNamespace.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermissionIdNamespace.kt @@ -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 } \ No newline at end of file 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 522fc88d1..88c55556d 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 @@ -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

{ public fun

PermissionService

.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] diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermitteeId.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermitteeId.kt index dc73b4d52..ec46d5da7 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermitteeId.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/permission/PermitteeId.kt @@ -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 {