Support receivers in resolving command

This commit is contained in:
Him188 2020-10-26 08:08:49 +08:00
parent 2957e42625
commit 77e344b4aa
2 changed files with 19 additions and 4 deletions

View File

@ -2,12 +2,15 @@ package net.mamoe.mirai.console.command.resolve
import net.mamoe.mirai.console.command.Command import net.mamoe.mirai.console.command.Command
import net.mamoe.mirai.console.command.CommandManager 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.*
import net.mamoe.mirai.console.command.descriptor.ArgumentAcceptance.Companion.isNotAcceptable 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.CommandCall
import net.mamoe.mirai.console.command.parse.CommandValueArgument import net.mamoe.mirai.console.command.parse.CommandValueArgument
import net.mamoe.mirai.console.command.parse.DefaultCommandValueArgument import net.mamoe.mirai.console.command.parse.DefaultCommandValueArgument
import net.mamoe.mirai.console.extensions.CommandCallResolverProvider 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.ConsoleExperimentalApi
import net.mamoe.mirai.console.util.safeCast import net.mamoe.mirai.console.util.safeCast
import net.mamoe.mirai.message.data.EmptyMessageChain import net.mamoe.mirai.message.data.EmptyMessageChain
@ -19,7 +22,7 @@ import net.mamoe.mirai.message.data.asMessageChain
@ConsoleExperimentalApi @ConsoleExperimentalApi
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors
public object BuiltInCommandCallResolver : CommandCallResolver { public object BuiltInCommandCallResolver : CommandCallResolver {
public object Provider : CommandCallResolverProvider(BuiltInCommandCallResolver) public object Provider : CommandCallResolverProvider by CommandCallResolverProviderImpl(BuiltInCommandCallResolver)
override fun resolve(call: CommandCall): ResolvedCommandCall? { override fun resolve(call: CommandCall): ResolvedCommandCall? {
val callee = CommandManager.matchCommand(call.calleeName) ?: return null val callee = CommandManager.matchCommand(call.calleeName) ?: return null
@ -27,7 +30,7 @@ public object BuiltInCommandCallResolver : CommandCallResolver {
val valueArguments = call.valueArguments val valueArguments = call.valueArguments
val context = callee.safeCast<CommandArgumentContextAware>()?.context val context = callee.safeCast<CommandArgumentContextAware>()?.context
val signature = resolveImpl(callee, valueArguments, context) ?: return null val signature = resolveImpl(call.caller, callee, valueArguments, context) ?: return null
return ResolvedCommandCallImpl(call.caller, return ResolvedCommandCallImpl(call.caller,
callee, callee,
@ -51,14 +54,18 @@ public object BuiltInCommandCallResolver : CommandCallResolver {
) )
private fun resolveImpl( private fun resolveImpl(
caller: CommandSender,
callee: Command, callee: Command,
valueArguments: List<CommandValueArgument>, valueArguments: List<CommandValueArgument>,
context: CommandArgumentContext?, context: CommandArgumentContext?,
): ResolveData? { ): ResolveData? {
callee.overloads callee.overloads
.mapNotNull l@{ signature -> .mapNotNull l@{ signature ->
if (signature.receiverParameter?.type?.classifierAsKClass()?.isInstance(caller) == false) {
return@l null // not compatible receiver
}
val valueParameters = signature.valueParameters val valueParameters = signature.valueParameters
val zipped = valueParameters.zip(valueArguments).toMutableList() val zipped = valueParameters.zip(valueArguments).toMutableList()

View File

@ -16,8 +16,16 @@ import net.mamoe.mirai.console.extension.AbstractInstanceExtensionPoint
import net.mamoe.mirai.console.extension.InstanceExtension import net.mamoe.mirai.console.extension.InstanceExtension
@ExperimentalCommandDescriptors @ExperimentalCommandDescriptors
public open class CommandCallResolverProvider(override val instance: CommandCallResolver) : InstanceExtension<CommandCallResolver> { public interface CommandCallResolverProvider : InstanceExtension<CommandCallResolver> {
public companion object ExtensionPoint : public companion object ExtensionPoint :
AbstractInstanceExtensionPoint<CommandCallResolverProvider, CommandCallResolver>(CommandCallResolverProvider::class, AbstractInstanceExtensionPoint<CommandCallResolverProvider, CommandCallResolver>(CommandCallResolverProvider::class,
BuiltInCommandCallResolver.Provider) 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)
}