mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-27 17:00:14 +08:00
Change CommandCallResolverProvider to SingletonExtension
This commit is contained in:
parent
2b0fa30c35
commit
8da8615721
@ -17,8 +17,6 @@ import net.mamoe.mirai.console.command.descriptor.ArgumentAcceptance.Companion.i
|
|||||||
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.CommandCallResolverProviderImpl
|
|
||||||
import net.mamoe.mirai.console.internal.data.classifierAsKClass
|
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
|
||||||
@ -31,21 +29,20 @@ import net.mamoe.mirai.message.data.asMessageChain
|
|||||||
@ConsoleExperimentalApi
|
@ConsoleExperimentalApi
|
||||||
@ExperimentalCommandDescriptors
|
@ExperimentalCommandDescriptors
|
||||||
public object BuiltInCommandCallResolver : CommandCallResolver {
|
public object BuiltInCommandCallResolver : CommandCallResolver {
|
||||||
public object Provider : CommandCallResolverProvider by CommandCallResolverProviderImpl(BuiltInCommandCallResolver)
|
override fun resolve(call: CommandCall): CommandResolveResult {
|
||||||
|
val callee = CommandManager.matchCommand(call.calleeName) ?: return CommandResolveResult(null)
|
||||||
override fun resolve(call: CommandCall): ResolvedCommandCall? {
|
|
||||||
val callee = CommandManager.matchCommand(call.calleeName) ?: return null
|
|
||||||
|
|
||||||
val valueArguments = call.valueArguments
|
val valueArguments = call.valueArguments
|
||||||
val context = callee.safeCast<CommandArgumentContextAware>()?.context
|
val context = callee.safeCast<CommandArgumentContextAware>()?.context
|
||||||
|
|
||||||
val signature = resolveImpl(call.caller, callee, valueArguments, context) ?: return null
|
val signature = resolveImpl(call.caller, callee, valueArguments, context) ?: return CommandResolveResult(null)
|
||||||
|
|
||||||
return ResolvedCommandCallImpl(call.caller,
|
return CommandResolveResult(ResolvedCommandCallImpl(call.caller,
|
||||||
callee,
|
callee,
|
||||||
signature.signature,
|
signature.signature,
|
||||||
signature.zippedArguments.map { it.second },
|
signature.zippedArguments.map { it.second },
|
||||||
context ?: EmptyCommandArgumentContext)
|
context ?: EmptyCommandArgumentContext)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class ResolveData(
|
private data class ResolveData(
|
||||||
|
@ -9,11 +9,45 @@
|
|||||||
|
|
||||||
package net.mamoe.mirai.console.command.resolve
|
package net.mamoe.mirai.console.command.resolve
|
||||||
|
|
||||||
|
import net.mamoe.mirai.console.command.CommandExecuteResult
|
||||||
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
||||||
import net.mamoe.mirai.console.command.parse.CommandCall
|
import net.mamoe.mirai.console.command.parse.CommandCall
|
||||||
import net.mamoe.mirai.console.extensions.CommandCallResolverProvider
|
import net.mamoe.mirai.console.extensions.CommandCallResolverProvider
|
||||||
import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage
|
import net.mamoe.mirai.console.internal.extension.GlobalComponentStorage
|
||||||
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
||||||
|
import net.mamoe.mirai.console.util.safeCast
|
||||||
|
import org.jetbrains.annotations.Contract
|
||||||
|
import kotlin.contracts.InvocationKind
|
||||||
|
import kotlin.contracts.contract
|
||||||
|
|
||||||
|
@ExperimentalCommandDescriptors
|
||||||
|
public class CommandResolveResult private constructor(
|
||||||
|
internal val value: Any?,
|
||||||
|
) {
|
||||||
|
@get:Contract(pure = true)
|
||||||
|
public val call: ResolvedCommandCall?
|
||||||
|
get() = value.safeCast()
|
||||||
|
|
||||||
|
@get:Contract(pure = true)
|
||||||
|
public val failure: CommandExecuteResult.Failure?
|
||||||
|
get() = value.safeCast()
|
||||||
|
|
||||||
|
public inline fun <R> fold(
|
||||||
|
onSuccess: (ResolvedCommandCall) -> R,
|
||||||
|
onFailure: (CommandExecuteResult.Failure) -> R,
|
||||||
|
): R {
|
||||||
|
contract {
|
||||||
|
callsInPlace(onSuccess, InvocationKind.AT_MOST_ONCE)
|
||||||
|
callsInPlace(onFailure, InvocationKind.AT_MOST_ONCE)
|
||||||
|
}
|
||||||
|
call?.let(onSuccess)
|
||||||
|
failure?.let(onFailure)
|
||||||
|
null!!
|
||||||
|
}
|
||||||
|
|
||||||
|
public constructor(call: ResolvedCommandCall?) : this(call as Any?)
|
||||||
|
public constructor(failure: CommandExecuteResult.Failure) : this(failure as Any)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The resolver converting a [CommandCall] into [ResolvedCommandCall] based on registered []
|
* The resolver converting a [CommandCall] into [ResolvedCommandCall] based on registered []
|
||||||
@ -23,19 +57,17 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
|
|||||||
*/
|
*/
|
||||||
@ExperimentalCommandDescriptors
|
@ExperimentalCommandDescriptors
|
||||||
public interface CommandCallResolver {
|
public interface CommandCallResolver {
|
||||||
public fun resolve(call: CommandCall): ResolvedCommandCall?
|
public fun resolve(call: CommandCall): CommandResolveResult
|
||||||
|
|
||||||
public companion object {
|
public companion object {
|
||||||
@JvmName("resolveCall")
|
@JvmName("resolveCall")
|
||||||
@ConsoleExperimentalApi
|
@ConsoleExperimentalApi
|
||||||
@ExperimentalCommandDescriptors
|
@ExperimentalCommandDescriptors
|
||||||
public fun CommandCall.resolve(): ResolvedCommandCall? {
|
public fun CommandCall.resolve(): CommandResolveResult {
|
||||||
GlobalComponentStorage.run {
|
GlobalComponentStorage.run {
|
||||||
CommandCallResolverProvider.useExtensions { provider ->
|
val instance = CommandCallResolverProvider.findSingletonInstance(CommandCallResolverProvider.builtinImplementation)
|
||||||
provider.instance.resolve(this@resolve)?.let { return it }
|
return instance.resolve(this@resolve)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return null
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,14 +12,13 @@ package net.mamoe.mirai.console.extensions
|
|||||||
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
|
||||||
import net.mamoe.mirai.console.command.resolve.BuiltInCommandCallResolver
|
import net.mamoe.mirai.console.command.resolve.BuiltInCommandCallResolver
|
||||||
import net.mamoe.mirai.console.command.resolve.CommandCallResolver
|
import net.mamoe.mirai.console.command.resolve.CommandCallResolver
|
||||||
import net.mamoe.mirai.console.extension.AbstractInstanceExtensionPoint
|
import net.mamoe.mirai.console.extension.AbstractSingletonExtensionPoint
|
||||||
import net.mamoe.mirai.console.extension.InstanceExtension
|
import net.mamoe.mirai.console.extension.SingletonExtension
|
||||||
|
|
||||||
@ExperimentalCommandDescriptors
|
@ExperimentalCommandDescriptors
|
||||||
public interface CommandCallResolverProvider : InstanceExtension<CommandCallResolver> {
|
public interface CommandCallResolverProvider : SingletonExtension<CommandCallResolver> {
|
||||||
public companion object ExtensionPoint :
|
public companion object ExtensionPoint :
|
||||||
AbstractInstanceExtensionPoint<CommandCallResolverProvider, CommandCallResolver>(CommandCallResolverProvider::class,
|
AbstractSingletonExtensionPoint<CommandCallResolverProvider, CommandCallResolver>(CommandCallResolverProvider::class, BuiltInCommandCallResolver)
|
||||||
BuiltInCommandCallResolver.Provider)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExperimentalCommandDescriptors
|
@ExperimentalCommandDescriptors
|
||||||
|
Loading…
Reference in New Issue
Block a user