Merge remote-tracking branch 'origin/master' into logger

This commit is contained in:
Karlatemp 2020-10-26 22:59:30 +08:00
commit 7d36bc6f7d
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
8 changed files with 57 additions and 14 deletions

View File

@ -22,6 +22,7 @@ import net.mamoe.mirai.console.util.ConsoleExperimentalApi
import kotlin.reflect.KClass
import kotlin.reflect.KFunction
import kotlin.reflect.KType
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.isSubtypeOf
import kotlin.reflect.typeOf
@ -183,9 +184,13 @@ public class CommandReceiverParameter<T : CommandSender>(
override val name: String get() = PARAMETER_NAME
init {
check(type.classifier is KClass<*>) {
val classifier = type.classifier
require(classifier is KClass<*>) {
"CommandReceiverParameter.type.classifier must be KClass."
}
require(classifier.isSubclassOf(CommandSender::class)) {
"CommandReceiverParameter.type.classifier must be subclass of CommandSender."
}
}
public companion object {

View File

@ -29,13 +29,20 @@ public open class NoValueArgumentMappingException(
public val forType: KType,
) : CommandResolutionException("Cannot find a CommandArgument mapping for ${forType.qualifiedName}")
public open class CommandResolutionException : RuntimeException {
public constructor() : super()
public constructor(message: String?) : super(message)
public constructor(message: String?, cause: Throwable?) : super(message, cause)
public constructor(cause: Throwable?) : super(cause)
}
@ExperimentalCommandDescriptors
public open class CommandDeclarationClashException(
public val command: Command,
public val signatures: List<CommandSignature>,
) : CommandResolutionException("Command declaration clash: \n${signatures.joinToString("\n")}")
) : CommandDeclarationException("Declaration clash for command '${command.primaryName}': \n${signatures.joinToString("\n")}")
public open class CommandResolutionException : RuntimeException {
public open class CommandDeclarationException : RuntimeException {
public constructor() : super()
public constructor(message: String?) : super(message)
public constructor(message: String?, cause: Throwable?) : super(message, cause)

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.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<CommandArgumentContextAware>()?.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<CommandValueArgument>,
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()

View File

@ -11,6 +11,7 @@ package net.mamoe.mirai.console.extension
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
import net.mamoe.mirai.console.command.parse.CommandCallParser
import net.mamoe.mirai.console.command.resolve.CommandCallResolver
import net.mamoe.mirai.console.extensions.*
import net.mamoe.mirai.console.internal.extension.AbstractConcurrentComponentStorage
import net.mamoe.mirai.console.permission.PermissionService
@ -77,7 +78,7 @@ public class PluginComponentStorage(
/** 注册一个 [PermissionServiceProvider] */
@OverloadResolutionByLambdaReturnType
public fun contributePermissionService(lazyInstance: () -> PermissionService<*>): Unit =
contribute(PermissionServiceProvider, plugin, LazyPermissionServiceProviderImpl(lazyInstance))
contribute(PermissionServiceProvider, plugin, PermissionServiceProviderImplLazy(lazyInstance))
/** 注册一个 [PermissionServiceProvider] */
@JvmName("contributePermissionServiceProvider")
@ -90,7 +91,7 @@ public class PluginComponentStorage(
/** 注册一个 [PluginLoaderProvider] */
@OverloadResolutionByLambdaReturnType
public fun contributePluginLoader(lazyInstance: () -> PluginLoader<*, *>): Unit =
contribute(PluginLoaderProvider, plugin, LazyPluginLoaderProviderImpl(lazyInstance))
contribute(PluginLoaderProvider, plugin, PluginLoaderProviderImplLazy(lazyInstance))
/** 注册一个 [PluginLoaderProvider] */
@JvmName("contributePluginLoaderProvider")
@ -104,7 +105,7 @@ public class PluginComponentStorage(
@ExperimentalCommandDescriptors
@OverloadResolutionByLambdaReturnType
public fun contributeCommandCallParser(lazyInstance: () -> CommandCallParser): Unit =
contribute(CommandCallParserProvider, plugin, LazyCommandCallParserProviderImpl(lazyInstance))
contribute(CommandCallParserProvider, plugin, CommandCallParserProviderImplLazy(lazyInstance))
/** 注册一个 [CommandCallParserProvider] */
@ExperimentalCommandDescriptors
@ -112,4 +113,19 @@ public class PluginComponentStorage(
@OverloadResolutionByLambdaReturnType
public fun contributeCommandCallParser(provider: CommandCallParserProvider): Unit =
contribute(CommandCallParserProvider, plugin, provider)
/////////////////////////////////////
/** 注册一个 [CommandCallResolverProvider] */
@ExperimentalCommandDescriptors
@OverloadResolutionByLambdaReturnType
public fun contributeCommandCallResolver(lazyInstance: () -> CommandCallResolver): Unit =
contribute(CommandCallResolverProvider, plugin, CommandCallResolverProviderImplLazy(lazyInstance))
/** 注册一个 [CommandCallResolverProvider] */
@ExperimentalCommandDescriptors
@JvmName("contributeCommandCallResolverProvider")
@OverloadResolutionByLambdaReturnType
public fun contributeCommandCallParser(provider: CommandCallResolverProvider): Unit =
contribute(CommandCallResolverProvider, plugin, provider)
}

View File

@ -30,6 +30,6 @@ public interface CommandCallParserProvider : InstanceExtension<CommandCallParser
public class CommandCallParserProviderImpl(override val instance: CommandCallParser) : CommandCallParserProvider
@ExperimentalCommandDescriptors
public class LazyCommandCallParserProviderImpl(instanceCalculator: () -> CommandCallParser) : CommandCallParserProvider {
public class CommandCallParserProviderImplLazy(instanceCalculator: () -> CommandCallParser) : CommandCallParserProvider {
override val instance: CommandCallParser by lazy(instanceCalculator)
}

View File

@ -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<CommandCallResolver> {
public interface CommandCallResolverProvider : InstanceExtension<CommandCallResolver> {
public companion object ExtensionPoint :
AbstractInstanceExtensionPoint<CommandCallResolverProvider, CommandCallResolver>(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)
}

View File

@ -32,6 +32,6 @@ public class PermissionServiceProviderImpl(override val instance: PermissionServ
/**
* @see PermissionServiceProvider
*/
public class LazyPermissionServiceProviderImpl(initializer: () -> PermissionService<*>) : PermissionServiceProvider {
public class PermissionServiceProviderImplLazy(initializer: () -> PermissionService<*>) : PermissionServiceProvider {
override val instance: PermissionService<*> by lazy(initializer)
}

View File

@ -24,7 +24,7 @@ import net.mamoe.mirai.console.plugin.loader.PluginLoader
* @see Extension
* @see PluginLoader
*
* @see LazyPluginLoaderProviderImpl
* @see PluginLoaderProviderImplLazy
*/
public interface PluginLoaderProvider : InstanceExtension<PluginLoader<*, *>> {
public companion object ExtensionPoint : AbstractExtensionPoint<PluginLoaderProvider>(PluginLoaderProvider::class)
@ -32,6 +32,6 @@ public interface PluginLoaderProvider : InstanceExtension<PluginLoader<*, *>> {
public class PluginLoaderProviderImpl(override val instance: PluginLoader<*, *>) : PluginLoaderProvider
public class LazyPluginLoaderProviderImpl(initializer: () -> PluginLoader<*, *>) : PluginLoaderProvider {
public class PluginLoaderProviderImplLazy(initializer: () -> PluginLoader<*, *>) : PluginLoaderProvider {
override val instance: PluginLoader<*, *> by lazy(initializer)
}