From 77e344b4aa487295b62ed703424b88a551bfafb8 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 26 Oct 2020 08:08:49 +0800 Subject: [PATCH] Support receivers in resolving command --- .../command/resolve/BuiltInCommandCallResolver.kt | 13 ++++++++++--- .../extensions/CommandCallResolverProvider.kt | 10 +++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/resolve/BuiltInCommandCallResolver.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/resolve/BuiltInCommandCallResolver.kt index 97774d55c..7f43e3d2b 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/resolve/BuiltInCommandCallResolver.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/command/resolve/BuiltInCommandCallResolver.kt @@ -2,12 +2,15 @@ package net.mamoe.mirai.console.command.resolve import net.mamoe.mirai.console.command.Command import net.mamoe.mirai.console.command.CommandManager +import net.mamoe.mirai.console.command.CommandSender import net.mamoe.mirai.console.command.descriptor.* import net.mamoe.mirai.console.command.descriptor.ArgumentAcceptance.Companion.isNotAcceptable import net.mamoe.mirai.console.command.parse.CommandCall import net.mamoe.mirai.console.command.parse.CommandValueArgument import net.mamoe.mirai.console.command.parse.DefaultCommandValueArgument import net.mamoe.mirai.console.extensions.CommandCallResolverProvider +import net.mamoe.mirai.console.extensions.CommandCallResolverProviderImpl +import net.mamoe.mirai.console.internal.data.classifierAsKClass import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.console.util.safeCast import net.mamoe.mirai.message.data.EmptyMessageChain @@ -19,7 +22,7 @@ import net.mamoe.mirai.message.data.asMessageChain @ConsoleExperimentalApi @ExperimentalCommandDescriptors public object BuiltInCommandCallResolver : CommandCallResolver { - public object Provider : CommandCallResolverProvider(BuiltInCommandCallResolver) + public object Provider : CommandCallResolverProvider by CommandCallResolverProviderImpl(BuiltInCommandCallResolver) override fun resolve(call: CommandCall): ResolvedCommandCall? { val callee = CommandManager.matchCommand(call.calleeName) ?: return null @@ -27,7 +30,7 @@ public object BuiltInCommandCallResolver : CommandCallResolver { val valueArguments = call.valueArguments val context = callee.safeCast()?.context - val signature = resolveImpl(callee, valueArguments, context) ?: return null + val signature = resolveImpl(call.caller, callee, valueArguments, context) ?: return null return ResolvedCommandCallImpl(call.caller, callee, @@ -51,14 +54,18 @@ public object BuiltInCommandCallResolver : CommandCallResolver { ) private fun resolveImpl( + caller: CommandSender, callee: Command, valueArguments: List, context: CommandArgumentContext?, ): ResolveData? { - callee.overloads .mapNotNull l@{ signature -> + if (signature.receiverParameter?.type?.classifierAsKClass()?.isInstance(caller) == false) { + return@l null // not compatible receiver + } + val valueParameters = signature.valueParameters val zipped = valueParameters.zip(valueArguments).toMutableList() diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/CommandCallResolverProvider.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/CommandCallResolverProvider.kt index 8be92c470..43e2a6fca 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/CommandCallResolverProvider.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/extensions/CommandCallResolverProvider.kt @@ -16,8 +16,16 @@ import net.mamoe.mirai.console.extension.AbstractInstanceExtensionPoint import net.mamoe.mirai.console.extension.InstanceExtension @ExperimentalCommandDescriptors -public open class CommandCallResolverProvider(override val instance: CommandCallResolver) : InstanceExtension { +public interface CommandCallResolverProvider : InstanceExtension { public companion object ExtensionPoint : AbstractInstanceExtensionPoint(CommandCallResolverProvider::class, BuiltInCommandCallResolver.Provider) +} + +@ExperimentalCommandDescriptors +public class CommandCallResolverProviderImpl(override val instance: CommandCallResolver) : CommandCallResolverProvider + +@ExperimentalCommandDescriptors +public class CommandCallResolverProviderImplLazy(instanceCalculator: () -> CommandCallResolver) : CommandCallResolverProvider { + override val instance: CommandCallResolver by lazy(instanceCalculator) } \ No newline at end of file