Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 支持CompositeCommand间组合 #2857

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public abstract class net/mamoe/mirai/console/command/AbstractPluginCustomComman
protected abstract fun sendMessageImpl (Ljava/lang/String;)V
}

public abstract class net/mamoe/mirai/console/command/AbstractSubCommandGroup : net/mamoe/mirai/console/command/SubCommandGroup, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public fun <init> ()V
public fun <init> (Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;)V
public synthetic fun <init> (Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getContext ()Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;
public final fun getOverloads ()Ljava/util/List;
}

public abstract class net/mamoe/mirai/console/command/AbstractUserCommandSender : net/mamoe/mirai/console/command/AbstractCommandSender, net/mamoe/mirai/console/command/UserCommandSender {
public fun getBot ()Lnet/mamoe/mirai/Bot;
public final fun getName ()Ljava/lang/String;
Expand Down Expand Up @@ -371,7 +379,7 @@ public abstract interface class net/mamoe/mirai/console/command/CommandSenderOnM
public abstract fun getFromEvent ()Lnet/mamoe/mirai/event/events/MessageEvent;
}

public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mamoe/mirai/console/command/AbstractCommand, net/mamoe/mirai/console/command/Command, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mamoe/mirai/console/command/AbstractCommand, net/mamoe/mirai/console/command/Command, net/mamoe/mirai/console/command/SubCommandGroup, net/mamoe/mirai/console/command/descriptor/CommandArgumentContextAware {
public fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Lnet/mamoe/mirai/console/permission/Permission;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;)V
public synthetic fun <init> (Lnet/mamoe/mirai/console/command/CommandOwner;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Lnet/mamoe/mirai/console/permission/Permission;Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun getContext ()Lnet/mamoe/mirai/console/command/descriptor/CommandArgumentContext;
Expand Down Expand Up @@ -605,6 +613,13 @@ public final class net/mamoe/mirai/console/command/StrangerCommandSenderOnMessag
public fun getFromEvent ()Lnet/mamoe/mirai/event/events/StrangerMessageEvent;
}

public abstract interface class net/mamoe/mirai/console/command/SubCommandGroup {
public abstract fun getOverloads ()Ljava/util/List;
}

public abstract interface annotation class net/mamoe/mirai/console/command/SubCommandGroup$FlattenSubCommands : java/lang/annotation/Annotation {
}

public abstract interface class net/mamoe/mirai/console/command/SystemCommandSender : net/mamoe/mirai/console/command/CommandSender {
public abstract fun isAnsiSupported ()Z
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2019-2022 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/dev/LICENSE
*/

package net.mamoe.mirai.console.command

import net.mamoe.mirai.console.command.descriptor.*
import net.mamoe.mirai.console.internal.command.GroupedCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.internal.command.SubCommandReflector

public abstract class AbstractSubCommandGroup(
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext,
) : CommandArgumentContextAware, SubCommandGroup {

private val reflector by lazy { SubCommandReflector(this, GroupedCommandSubCommandAnnotationResolver) }

@ExperimentalCommandDescriptors
public final override val overloads: List<CommandSignatureFromKFunction> by lazy {
reflector.findSubCommands().also {
reflector.validate(it)
}
}

/**
* 智能参数解析环境
*/ // open since 2.12
public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext

}


Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION


/**
* 复合指令. 指令注册时候会通过反射构造指令解析器.
*
Expand Down Expand Up @@ -92,7 +91,7 @@ public abstract class CompositeCommand(
parentPermission: Permission = owner.parentPermission,
overrideContext: CommandArgumentContext = EmptyCommandArgumentContext,
) : Command, AbstractCommand(owner, primaryName, secondaryNames = secondaryNames, description, parentPermission),
CommandArgumentContextAware {
CommandArgumentContextAware, SubCommandGroup {

private val reflector by lazy { CommandReflector(this, CompositeCommandSubCommandAnnotationResolver) }

Expand Down
24 changes: 24 additions & 0 deletions mirai-console/backend/mirai-console/src/command/SubCommandGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.mamoe.mirai.console.command

import net.mamoe.mirai.console.command.descriptor.CommandSignatureFromKFunction
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.compiler.common.ResolveContext
import net.mamoe.mirai.console.util.ConsoleExperimentalApi

public interface SubCommandGroup {

/**
* 被聚合时提供的子指令
*/
@ExperimentalCommandDescriptors
public val overloads: List<@JvmWildcard CommandSignatureFromKFunction>

/**
* 标记一个属性为子指令集合,且使用flat策略
*/
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.PROPERTY)
public annotation class FlattenSubCommands(
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public open class CommandDeclarationClashException(
public val signatures: List<CommandSignature>,
) : CommandDeclarationException("Declaration clash for command '${command.primaryName}': \n${signatures.joinToString("\n")}")

@ExperimentalCommandDescriptors
public open class SubcommandDeclarationClashException(
public val owner: Any,
public val signatures: List<CommandSignature>,
) : CommandDeclarationException("Declaration clash for owner '${owner::class.qualifiedName}': \n${signatures.joinToString("\n")}")


public open class CommandDeclarationException : RuntimeException {
public constructor() : super()
public constructor(message: String?) : super(message)
Expand Down
Loading
Loading