Skip to content

Commit

Permalink
interface SubCommandProvider方案
Browse files Browse the repository at this point in the history
  • Loading branch information
hundun000 committed Jul 3, 2022
1 parent 826dbe7 commit df170b9
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ 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 getProvideOverloads ()Ljava/util/List;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractSubCommandGroup$AnotherCombinedCommand : java/lang/annotation/Annotation {
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractSubCommandGroup$AnotherDescription : java/lang/annotation/Annotation {
public abstract fun value ()Ljava/lang/String;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/AbstractSubCommandGroup$AnotherSubCommand : java/lang/annotation/Annotation {
public abstract fun value ()[Ljava/lang/String;
}

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 @@ -389,7 +408,7 @@ public abstract class net/mamoe/mirai/console/command/CompositeCommand : net/mam
public fun getUsage ()Ljava/lang/String;
}

protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$ChildCommand : java/lang/annotation/Annotation {
protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$CombinedCommand : java/lang/annotation/Annotation {
}

protected abstract interface annotation class net/mamoe/mirai/console/command/CompositeCommand$Description : java/lang/annotation/Annotation {
Expand Down Expand Up @@ -613,6 +632,10 @@ 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 getProvideOverloads ()Ljava/util/List;
}

public abstract interface class net/mamoe/mirai/console/command/SystemCommandSender : net/mamoe/mirai/console/command/CommandSender {
public abstract fun isAnsiSupported ()Z
}
Expand Down Expand Up @@ -1097,6 +1120,12 @@ public final class net/mamoe/mirai/console/command/descriptor/StringValueArgumen
public fun parse (Ljava/lang/String;Lnet/mamoe/mirai/console/command/CommandSender;)Ljava/lang/String;
}

public class net/mamoe/mirai/console/command/descriptor/SubcommandDeclarationClashException : net/mamoe/mirai/console/command/descriptor/CommandDeclarationException {
public fun <init> (Ljava/lang/Object;Ljava/util/List;)V
public final fun getOwner ()Ljava/lang/Object;
public final fun getSignatures ()Ljava/util/List;
}

public abstract interface class net/mamoe/mirai/console/command/descriptor/TypeVariant {
public static final field Companion Lnet/mamoe/mirai/console/command/descriptor/TypeVariant$Companion;
public abstract fun getOutType ()Lkotlin/reflect/KType;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.compiler.common.ResolveContext
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.RESTRICTED_CONSOLE_COMMAND_OWNER
import net.mamoe.mirai.console.internal.command.GroupedCommandSubCommandAnnotationResolver
import net.mamoe.mirai.console.internal.command.SubCommandReflector
import net.mamoe.mirai.console.compiler.common.ResolveContext.Kind.COMMAND_NAME
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY

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

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

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

/**
* 标记一个属性为子指令集合
*/
@Retention(RUNTIME)
@Target(PROPERTY)
protected annotation class AnotherCombinedCommand(
)

/**
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* @param value 子指令名
*/
@Retention(RUNTIME)
@Target(FUNCTION)
protected annotation class AnotherSubCommand(
@ResolveContext(COMMAND_NAME) vararg val value: String = [],
)

/** 指令描述 */
@Retention(RUNTIME)
@Target(FUNCTION)
protected annotation class AnotherDescription(val value: String)

/** 参数名, 由具体Command决定用途 */
@ConsoleExperimentalApi("Classname might change")
@Retention(RUNTIME)
@Target(AnnotationTarget.VALUE_PARAMETER)
protected annotation class AnotherName(val value: String)

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

}


Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import kotlin.annotation.AnnotationRetention.RUNTIME
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.PROPERTY


/**
* 复合指令. 指令注册时候会通过反射构造指令解析器.
*
Expand Down Expand Up @@ -118,21 +117,21 @@ public abstract class CompositeCommand(
public override val context: CommandArgumentContext = CommandArgumentContext.Builtins + overrideContext

/**
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* @param value 子指令名
* 标记一个属性为子指令集合
*/
@Retention(RUNTIME)
@Target(FUNCTION)
protected annotation class SubCommand(
@ResolveContext(COMMAND_NAME) vararg val value: String = [],
@Target(PROPERTY)
protected annotation class CombinedCommand(
)

/**
* 标记一个属性为子指令集合
* 标记一个函数为子指令, 当 [value] 为空时使用函数名.
* @param value 子指令名
*/
@Retention(RUNTIME)
@Target(PROPERTY)
protected annotation class ChildCommand(
@Target(FUNCTION)
protected annotation class SubCommand(
@ResolveContext(COMMAND_NAME) vararg val value: String = [],
)

/** 指令描述 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public abstract class SimpleCommand(
}
}


/**
* 自动根据带有 [Handler] 注解的函数签名生成 [usage]. 也可以被覆盖.
*/
Expand Down
16 changes: 16 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,16 @@
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.util.ConsoleExperimentalApi

public interface SubCommandGroup {

/**
* 被聚合时提供的子指令
*/
@ConsoleExperimentalApi("Property name is experimental")
@ExperimentalCommandDescriptors
public val provideOverloads: List<@JvmWildcard CommandSignatureFromKFunction>

}
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

0 comments on commit df170b9

Please sign in to comment.