From 4acd74974af02e9f272bf285e7e33f69a53c0b1a Mon Sep 17 00:00:00 2001 From: Him188 Date: Wed, 13 Apr 2022 13:07:04 +0100 Subject: [PATCH] Migrate deprecated usages and cleanup code --- .../src/IntegrationTestBootstrap.kt | 9 ++-- .../src/command/BuiltInCommands.kt | 10 ++--- .../mirai-console/src/extension/Extension.kt | 4 +- .../src/extension/ExtensionPoint.kt | 8 ++-- .../src/extension/PluginComponentStorage.kt | 26 ++++++++++- .../SingletonExtensionSelectorImpl.kt | 2 + .../internal/plugin/JvmPluginClassLoader.kt | 3 +- .../plugin/JvmPluginDependencyDownload.kt | 7 ++- .../kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt | 10 ++--- .../src/main/kotlin/publishing.kt | 9 +++- .../src/creator/tasks/TaskUtils.kt | 10 ++--- .../ContextualParametersChecker.kt | 13 +++--- .../kotlin/contact/SendMessageHandler.kt | 17 ++++--- .../kotlin/message/outgoingSourceImpl.kt | 44 ++++++++++--------- .../network/components/ConfigPushProcessor.kt | 13 +++++- .../network/components/MessageSvcSyncer.kt | 11 ++--- ...mizeByteArrayAsHexStringTransformerTest.kt | 6 +-- .../kotlin/message/data/MessageReceiptTest.kt | 26 ++++++----- 18 files changed, 141 insertions(+), 87 deletions(-) diff --git a/mirai-console/backend/integration-test/src/IntegrationTestBootstrap.kt b/mirai-console/backend/integration-test/src/IntegrationTestBootstrap.kt index 0fecda322..814c08542 100644 --- a/mirai-console/backend/integration-test/src/IntegrationTestBootstrap.kt +++ b/mirai-console/backend/integration-test/src/IntegrationTestBootstrap.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. @@ -44,7 +44,7 @@ internal object IntegrationTestBootstrapContext { @PublishedApi internal fun main() { // PRE CHECK - kotlin.run { + run { if (!System.getenv("MIRAI_CONSOLE_INTEGRATION_TEST").orEmpty().toBoolean()) { error("Don't launch IntegrationTestBootstrap directly. See /test/MiraiConsoleIntegrationTestBootstrap.kt") } @@ -63,7 +63,10 @@ internal fun main() { val testUnits: List = readStringListFromEnv("IT_POINTS").asSequence() .onEach { println("[MCIT] Loading test point: $it") } .map { Class.forName(it) } - .map { it.kotlin.objectInstance ?: it.newInstance() } + .map { + @Suppress("DEPRECATION") + it.kotlin.objectInstance ?: it.newInstance() + } .map { it.cast() } .toList() diff --git a/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt b/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt index e9db3a213..bd5d30f6f 100644 --- a/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt +++ b/mirai-console/backend/mirai-console/src/command/BuiltInCommands.kt @@ -43,7 +43,6 @@ import net.mamoe.mirai.console.permission.PermissionId import net.mamoe.mirai.console.permission.PermissionService import net.mamoe.mirai.console.permission.PermissionService.Companion.cancel import net.mamoe.mirai.console.permission.PermissionService.Companion.findCorrespondingPermissionOrFail -import net.mamoe.mirai.console.permission.PermissionService.Companion.getPermittedPermissions import net.mamoe.mirai.console.permission.PermissionService.Companion.hasPermission import net.mamoe.mirai.console.permission.PermissionService.Companion.permit import net.mamoe.mirai.console.permission.PermitteeId @@ -57,6 +56,7 @@ import net.mamoe.mirai.event.events.EventCancelledException import net.mamoe.mirai.utils.BotConfiguration import net.mamoe.mirai.utils.MiraiLogger import java.lang.management.ManagementFactory +import java.lang.management.MemoryMXBean import java.lang.management.MemoryUsage import java.time.ZoneId import java.time.format.DateTimeFormatter @@ -346,7 +346,7 @@ public object BuiltInCommands { } sendAnsiMessage { ansi(BG_BLACK).white() - permMapping.forEach { (pid, tree) -> + permMapping.forEach { (_, tree) -> if (!tree.linked) { render(0, tree, this) } @@ -555,13 +555,13 @@ public object BuiltInCommands { val objectPendingFinalizationCount: Int } - internal val memoryUsageGet: MemoryUsageGet = kotlin.runCatching { + private val memoryUsageGet: MemoryUsageGet = kotlin.runCatching { ByMemoryMXBean }.getOrElse { ByRuntime } internal object ByMemoryMXBean : MemoryUsageGet { - val memoryMXBean = ManagementFactory.getMemoryMXBean() - val MemoryUsage.m: MUsage + private val memoryMXBean: MemoryMXBean = ManagementFactory.getMemoryMXBean() + private val MemoryUsage.m: MUsage get() = MUsage( committed, init, used, max ) diff --git a/mirai-console/backend/mirai-console/src/extension/Extension.kt b/mirai-console/backend/mirai-console/src/extension/Extension.kt index fc5721fa0..468ece311 100644 --- a/mirai-console/backend/mirai-console/src/extension/Extension.kt +++ b/mirai-console/backend/mirai-console/src/extension/Extension.kt @@ -12,8 +12,6 @@ package net.mamoe.mirai.console.extension import net.mamoe.mirai.console.command.parse.SpaceSeparatedCommandCallParser import net.mamoe.mirai.console.extensions.PermissionServiceProvider import net.mamoe.mirai.console.extensions.PluginLoaderProvider -import net.mamoe.mirai.console.extensions.SingletonExtensionSelector -import net.mamoe.mirai.console.extensions.SingletonExtensionSelector.ExtensionPoint.selectSingleton import net.mamoe.mirai.console.plugin.jvm.JvmPlugin import net.mamoe.mirai.console.plugin.jvm.JvmPlugin.Companion.onLoad import net.mamoe.mirai.console.util.ConsoleExperimentalApi @@ -61,7 +59,7 @@ public interface FunctionExtension : Extension /** * 为某单例服务注册的 [Extension]. * - * 若同时有多个实例可用, 将会使用 [SingletonExtensionSelector.selectSingleton] 选择 + * 若同时有多个实例可用, 将会使用 [net.mamoe.mirai.console.extensions.SingletonExtensionSelector.selectSingleton] 选择 * * @see PermissionServiceProvider */ diff --git a/mirai-console/backend/mirai-console/src/extension/ExtensionPoint.kt b/mirai-console/backend/mirai-console/src/extension/ExtensionPoint.kt index ddd28ac2a..381164338 100644 --- a/mirai-console/backend/mirai-console/src/extension/ExtensionPoint.kt +++ b/mirai-console/backend/mirai-console/src/extension/ExtensionPoint.kt @@ -11,7 +11,6 @@ package net.mamoe.mirai.console.extension -import net.mamoe.mirai.console.extensions.SingletonExtensionSelector import net.mamoe.mirai.console.util.ConsoleExperimentalApi import net.mamoe.mirai.utils.DeprecatedSinceMirai import kotlin.reflect.KClass @@ -39,6 +38,7 @@ public abstract class AbstractExtensionPoint( /** * 表示一个 [SingletonExtension] 的 [ExtensionPoint] */ +@Suppress("DEPRECATION") @Deprecated("Please use InstanceExtensionPoint instead.", replaceWith = ReplaceWith("InstanceExtensionPoint")) @DeprecatedSinceMirai(warningSince = "2.11") public interface SingletonExtensionPoint> : ExtensionPoint @@ -80,7 +80,7 @@ public constructor( /** * 内建的实现列表. */ - builtinImplementations: () -> E, + @Suppress("UNUSED_PARAMETER") builtinImplementations: () -> E, ) : this(extensionType) /** @@ -95,7 +95,7 @@ public constructor( ) @DeprecatedSinceMirai(errorSince = "2.11") // for removal @ConsoleExperimentalApi // was experimental since 2.0 - public constructor(extensionType: KClass, vararg builtinImplementations: E) : this( + public constructor(extensionType: KClass, @Suppress("UNUSED_PARAMETER") vararg builtinImplementations: E) : this( extensionType, ) } @@ -134,7 +134,7 @@ constructor( ) /** - * 由 [SingletonExtensionSelector] 选择后的实例. + * 由 [net.mamoe.mirai.console.extensions.SingletonExtensionSelector] 选择后的实例. */ @ConsoleExperimentalApi public open val selectedInstance: T diff --git a/mirai-console/backend/mirai-console/src/extension/PluginComponentStorage.kt b/mirai-console/backend/mirai-console/src/extension/PluginComponentStorage.kt index 3c2cff111..0d037506d 100644 --- a/mirai-console/backend/mirai-console/src/extension/PluginComponentStorage.kt +++ b/mirai-console/backend/mirai-console/src/extension/PluginComponentStorage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. @@ -18,6 +18,7 @@ import net.mamoe.mirai.console.internal.extension.AbstractConcurrentComponentSto import net.mamoe.mirai.console.permission.PermissionService import net.mamoe.mirai.console.plugin.Plugin import net.mamoe.mirai.console.plugin.loader.PluginLoader +import net.mamoe.mirai.utils.DeprecatedSinceMirai import kotlin.reflect.full.companionObjectInstance /** @@ -57,6 +58,12 @@ public class PluginComponentStorage( /////////////////////////////////////////////////////////////////////////// /** 注册一个 [SingletonExtensionSelector] */ + @Suppress("DeprecatedCallableAddReplaceWith", "DEPRECATION") + @Deprecated( + "Order of extensions is now determined by its priority property since 2.11. SingletonExtensionSelector is not needed anymore. ", + level = DeprecationLevel.WARNING + ) + @DeprecatedSinceMirai(warningSince = "2.11") public fun contributeSingletonExtensionSelector(lazyInstance: () -> SingletonExtensionSelector): Unit = contribute(SingletonExtensionSelector, plugin, lazyInstance) @@ -77,6 +84,9 @@ public class PluginComponentStorage( /////////////////////////////////////////////////////////////////////////// /** 注册一个 [PermissionServiceProvider] */ + @Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith") + @Deprecated("Deprecated for removal. Please implement your own CommandCallResolverProvider, and use contributePermissionService(provider).") + @DeprecatedSinceMirai(warningSince = "2.11") // for removal. @OverloadResolutionByLambdaReturnType public fun contributePermissionService(lazyInstance: () -> PermissionService<*>): Unit = contribute(PermissionServiceProvider, plugin, PermissionServiceProviderImplLazy(lazyInstance)) @@ -90,6 +100,9 @@ public class PluginComponentStorage( ///////////////////////////////////// /** 注册一个 [PluginLoaderProvider] */ + @Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith") + @Deprecated("Deprecated for removal. Please implement your own CommandCallResolverProvider, and use contributePluginLoader(provider).") + @DeprecatedSinceMirai(warningSince = "2.11") // for removal. @OverloadResolutionByLambdaReturnType public fun contributePluginLoader(lazyInstance: () -> PluginLoader<*, *>): Unit = contribute(PluginLoaderProvider, plugin, PluginLoaderProviderImplLazy(lazyInstance)) @@ -103,6 +116,9 @@ public class PluginComponentStorage( ///////////////////////////////////// /** 注册一个 [CommandCallParserProvider] */ + @Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith") + @Deprecated("Deprecated for removal. Please implement your own CommandCallResolverProvider, and use contributeCommandCallParser(provider).") + @DeprecatedSinceMirai(warningSince = "2.11") // for removal. @ExperimentalCommandDescriptors @OverloadResolutionByLambdaReturnType public fun contributeCommandCallParser(lazyInstance: () -> CommandCallParser): Unit = @@ -117,7 +133,10 @@ public class PluginComponentStorage( ///////////////////////////////////// - /** 注册一个 [CommandCallResolverProvider] */ + + @Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith") + @Deprecated("Deprecated for removal. Please implement your own CommandCallResolverProvider, and use contributeCommandCallResolver(provider).") + @DeprecatedSinceMirai(warningSince = "2.11") // for removal. @ExperimentalCommandDescriptors @OverloadResolutionByLambdaReturnType public fun contributeCommandCallResolver(lazyInstance: () -> CommandCallResolver): Unit = @@ -133,6 +152,9 @@ public class PluginComponentStorage( ///////////////////////////////////// /** 注册一个 [CommandCallInterceptorProvider] */ + @Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith") + @Deprecated("Deprecated for removal. Please implement your own CommandCallResolverProvider, and use contributeCommandCallInterceptor(provider).") + @DeprecatedSinceMirai(warningSince = "2.11") // for removal. @ExperimentalCommandDescriptors @OverloadResolutionByLambdaReturnType public fun contributeCommandCallInterceptor(lazyInstance: () -> CommandCallInterceptor): Unit = diff --git a/mirai-console/backend/mirai-console/src/internal/extension/SingletonExtensionSelectorImpl.kt b/mirai-console/backend/mirai-console/src/internal/extension/SingletonExtensionSelectorImpl.kt index a630c6a9d..8b56d7335 100644 --- a/mirai-console/backend/mirai-console/src/internal/extension/SingletonExtensionSelectorImpl.kt +++ b/mirai-console/backend/mirai-console/src/internal/extension/SingletonExtensionSelectorImpl.kt @@ -7,6 +7,8 @@ * https://github.com/mamoe/mirai/blob/dev/LICENSE */ +@file:Suppress("DEPRECATION") + package net.mamoe.mirai.console.internal.extension import kotlinx.coroutines.runBlocking diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginClassLoader.kt b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginClassLoader.kt index 2a402d4b3..7d0fbe70e 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginClassLoader.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginClassLoader.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. @@ -124,6 +124,7 @@ internal class JvmPluginClassLoaderN : URLClassLoader { var linkedLogger by lateinitMutableProperty { MiraiConsole.createLogger("JvmPlugin[" + file_.name + "]") } val undefinedDependencies = mutableSetOf() + @Suppress("UNUSED_PARAMETER") private constructor(file: File, ctx: JvmPluginsLoadingCtx, unused: Unit) : super( arrayOf(), ctx.sharedLibrariesLoader ) { diff --git a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginDependencyDownload.kt b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginDependencyDownload.kt index c5692e9b0..ed0bbdcc5 100644 --- a/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginDependencyDownload.kt +++ b/mirai-console/backend/mirai-console/src/internal/plugin/JvmPluginDependencyDownload.kt @@ -37,7 +37,6 @@ import org.eclipse.aether.resolution.DependencyRequest import org.eclipse.aether.resolution.DependencyResult import org.eclipse.aether.spi.connector.RepositoryConnectorFactory import org.eclipse.aether.spi.connector.transport.TransporterFactory -import org.eclipse.aether.spi.locator.ServiceLocator import org.eclipse.aether.transfer.AbstractTransferListener import org.eclipse.aether.transfer.TransferEvent import org.eclipse.aether.transport.http.HttpTransporterFactory @@ -50,9 +49,9 @@ internal class JvmPluginDependencyDownloader( ) { val repositories: MutableList val session: RepositorySystemSession - val locator: ServiceLocator + val locator: org.eclipse.aether.spi.locator.ServiceLocator val repository: RepositorySystem - val dependencyFilter: DependencyFilter = DependencyFilter { node, parents -> + val dependencyFilter: DependencyFilter = DependencyFilter { node, _ -> if (node == null || node.artifact == null) return@DependencyFilter true val artGroup = node.artifact.groupId @@ -205,7 +204,7 @@ internal class JvmPluginDependencyDownloader( logger.debug { "Remote server: " + config.repoLoc } } - public fun resolveDependencies(deps: Collection, vararg filters: DependencyFilter): DependencyResult { + fun resolveDependencies(deps: Collection, vararg filters: DependencyFilter): DependencyResult { val dependencies: MutableList = ArrayList() for (library in deps) { diff --git a/mirai-console/tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt b/mirai-console/tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt index c715e55af..03731c053 100644 --- a/mirai-console/tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt +++ b/mirai-console/tools/gradle-plugin/src/main/kotlin/IGNORED_DEPENDENCIES_IN_SHADOW.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -143,6 +143,6 @@ internal val IGNORED_DEPENDENCIES_IN_SHADOW = arrayOf( "net.mamoe:kotlin-jvm-blocking-bridge-common", "net.mamoe:kotlin-jvm-blocking-bridge-metadata", "net.mamoe:kotlin-jvm-blocking-bridge-jvm" -).map { it.toLowerCase() } +).map { it.lowercase() } .map { MiraiConsoleExtension.ExcludedDependency(it.substringBefore(':'), it.substringAfterLast(':')) } .toTypedArray() \ No newline at end of file diff --git a/mirai-console/tools/gradle-plugin/src/main/kotlin/publishing.kt b/mirai-console/tools/gradle-plugin/src/main/kotlin/publishing.kt index a13f452f0..604000317 100644 --- a/mirai-console/tools/gradle-plugin/src/main/kotlin/publishing.kt +++ b/mirai-console/tools/gradle-plugin/src/main/kotlin/publishing.kt @@ -23,6 +23,7 @@ import org.gradle.kotlin.dsl.provideDelegate import org.gradle.kotlin.dsl.registering import org.jetbrains.kotlin.gradle.plugin.KotlinTarget import java.io.File +import java.util.* private val Project.selfAndParentProjects: Sequence @@ -80,7 +81,13 @@ internal data class PluginMetadata( ) internal fun String.wrapNameWithPlatform(target: KotlinTarget, isSingleTarget: Boolean): String { - return if (isSingleTarget) this else "$this${target.name.capitalize()}" + return if (isSingleTarget) this else "$this${ + target.name.replaceFirstChar { + if (it.isLowerCase()) it.titlecase( + Locale.getDefault() + ) else it.toString() + } + }" } private fun Project.registerPublishPluginTasks(target: KotlinTarget, isSingleTarget: Boolean) { diff --git a/mirai-console/tools/intellij-plugin/src/creator/tasks/TaskUtils.kt b/mirai-console/tools/intellij-plugin/src/creator/tasks/TaskUtils.kt index 73b384856..68c9ee3de 100644 --- a/mirai-console/tools/intellij-plugin/src/creator/tasks/TaskUtils.kt +++ b/mirai-console/tools/intellij-plugin/src/creator/tasks/TaskUtils.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @@ -118,7 +118,7 @@ fun String.adjustToClassName(): String? { if (this.isEmpty()) append('_') append(char) } - char.isLetter() -> append(char.toUpperCase()) + char.isLetter() -> append(char.uppercase()) char == '-' -> append("_") else -> append(char) } diff --git a/mirai-console/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt b/mirai-console/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt index f8d7048eb..defbd4c4c 100644 --- a/mirai-console/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt +++ b/mirai-console/tools/intellij-plugin/src/diagnostics/ContextualParametersChecker.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.console.intellij.diagnostics @@ -32,7 +32,6 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic import org.jetbrains.kotlin.idea.inspections.collections.isCalling import org.jetbrains.kotlin.psi.KtReferenceExpression import org.jetbrains.kotlin.psi.ValueArgument -import org.jetbrains.kotlin.psi.psiUtil.referenceExpression import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall @@ -118,7 +117,7 @@ class ContextualParametersChecker : CallChecker { "插件 Id '$value' 无效. 插件 Id 必须同时包含 groupId 和插件名称. $syntax" ) - val lowercaseId = value.toLowerCase() + val lowercaseId = value.lowercase() if (ID_REGEX.matchEntire(value) == null) { return ILLEGAL_PLUGIN_DESCRIPTION.on( @@ -135,7 +134,7 @@ class ContextualParametersChecker : CallChecker { fun checkPluginName(inspectionTarget: PsiElement, value: String): Diagnostic? { if (value.isBlank()) return ILLEGAL_PLUGIN_DESCRIPTION.on(inspectionTarget, "插件名不能为空") - val lowercaseName = value.toLowerCase() + val lowercaseName = value.lowercase() FORBIDDEN_ID_NAMES.firstOrNull { it == lowercaseName }?.let { illegal -> return ILLEGAL_PLUGIN_DESCRIPTION.on(inspectionTarget, "'$illegal' 不允许作为插件名. 确保插件名不完全是这个名称") } diff --git a/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt b/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt index 2c4c19e57..0cea159b7 100644 --- a/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt +++ b/mirai-core/src/commonMain/kotlin/contact/SendMessageHandler.kt @@ -12,8 +12,11 @@ package net.mamoe.mirai.internal.contact import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.Deferred import kotlinx.coroutines.async +import kotlinx.coroutines.withTimeoutOrNull import net.mamoe.mirai.contact.* -import net.mamoe.mirai.event.nextEventOrNull +import net.mamoe.mirai.event.EventPriority +import net.mamoe.mirai.event.GlobalEventChannel +import net.mamoe.mirai.event.nextEvent import net.mamoe.mirai.internal.asQQAndroidBot import net.mamoe.mirai.internal.getMiraiImpl import net.mamoe.mirai.internal.message.* @@ -393,8 +396,10 @@ internal class FriendSendMessageHandler( fromAppId: Int ): OnlineMessageSource.Outgoing { - val receipt: SendPrivateMessageReceipt = nextEventOrNull(3000) { - it.bot === bot && it.fromAppId == fromAppId + val receipt: SendPrivateMessageReceipt = withTimeoutOrNull(3000) { + GlobalEventChannel.parentScope(this).nextEvent(EventPriority.MONITOR) { + it.bot === bot && it.fromAppId == fromAppId + } } ?: SendPrivateMessageReceipt.EMPTY return OnlineMessageSourceToFriendImpl( @@ -448,8 +453,10 @@ internal open class GroupSendMessageHandler( fromAppId: Int, ): OnlineMessageSource.Outgoing { - val receipt: SendGroupMessageReceipt = nextEventOrNull(3000) { - it.bot === bot && it.fromAppId == fromAppId + val receipt: SendGroupMessageReceipt = withTimeoutOrNull(3000) { + GlobalEventChannel.parentScope(this).nextEvent(EventPriority.MONITOR) { + it.bot === bot && it.fromAppId == fromAppId + } } ?: SendGroupMessageReceipt.EMPTY return OnlineMessageSourceToGroupImpl( diff --git a/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt index 389a7ec6c..a01b29f4d 100644 --- a/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt @@ -1,24 +1,23 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_OVERRIDE", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") package net.mamoe.mirai.internal.message -import kotlinx.coroutines.CompletableDeferred -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.* import kotlinx.serialization.Serializable import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.* -import net.mamoe.mirai.event.asyncFromEventOrNull +import net.mamoe.mirai.event.EventPriority +import net.mamoe.mirai.event.GlobalEventChannel +import net.mamoe.mirai.event.syncFromEvent import net.mamoe.mirai.internal.contact.uin import net.mamoe.mirai.internal.network.notice.group.GroupMessageProcessor.SendGroupMessageReceipt import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody @@ -169,18 +168,23 @@ internal class OnlineMessageSourceToGroupImpl( private val sequenceIdDeferred: Deferred = providedSequenceIds?.let { CompletableDeferred(it) } ?: run { val multi = mutableMapOf() - coroutineScope.asyncFromEventOrNull( - timeoutMillis = 3000L * this@OnlineMessageSourceToGroupImpl.internalIds.size - ) { - if (it.bot !== this.bot) return@asyncFromEventOrNull null - if (it.messageRandom in this@OnlineMessageSourceToGroupImpl.internalIds) { - multi[it.messageRandom] = it.sequenceId - if (multi.size == this@OnlineMessageSourceToGroupImpl.internalIds.size) { - IntArray(multi.size) { index -> - multi[this@OnlineMessageSourceToGroupImpl.internalIds[index]]!! + coroutineScope.async { + withTimeoutOrNull( + timeMillis = 3000L * this@OnlineMessageSourceToGroupImpl.internalIds.size + ) { + GlobalEventChannel.parentScope(this) + .syncFromEvent(EventPriority.MONITOR) { receipt -> + if (receipt.bot !== bot) return@syncFromEvent null + if (receipt.messageRandom in this@OnlineMessageSourceToGroupImpl.internalIds) { + multi[receipt.messageRandom] = receipt.sequenceId + if (multi.size == this@OnlineMessageSourceToGroupImpl.internalIds.size) { + IntArray(multi.size) { index -> + multi[this@OnlineMessageSourceToGroupImpl.internalIds[index]]!! + } + } else null + } else null } - } else null - } else null + } } } diff --git a/mirai-core/src/commonMain/kotlin/network/components/ConfigPushProcessor.kt b/mirai-core/src/commonMain/kotlin/network/components/ConfigPushProcessor.kt index 45c6e18a0..7301a23df 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/ConfigPushProcessor.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/ConfigPushProcessor.kt @@ -9,8 +9,11 @@ package net.mamoe.mirai.internal.network.components +import kotlinx.coroutines.withTimeoutOrNull +import net.mamoe.mirai.event.EventPriority import net.mamoe.mirai.event.events.BotOfflineEvent -import net.mamoe.mirai.event.nextEventOrNull +import net.mamoe.mirai.event.globalEventChannel +import net.mamoe.mirai.event.nextEvent import net.mamoe.mirai.internal.network.component.ComponentKey import net.mamoe.mirai.internal.network.handler.NetworkHandler import net.mamoe.mirai.internal.network.protocol.packet.login.ConfigPushSvc @@ -30,7 +33,13 @@ internal class ConfigPushProcessorImpl( private val logger: MiraiLogger, ) : ConfigPushProcessor { override suspend fun syncConfigPush(network: NetworkHandler) { - if (nextEventOrNull(60_000) { it.bot == network.context.bot } == null) { + val resp = withTimeoutOrNull(60_000) { + globalEventChannel().nextEvent( + EventPriority.MONITOR + ) { it.bot == network.context.bot } + } + + if (resp == null) { val bdhSyncer = network.context[BdhSessionSyncer] if (!bdhSyncer.hasSession) { val e = IllegalStateException("Timeout waiting for ConfigPush.") diff --git a/mirai-core/src/commonMain/kotlin/network/components/MessageSvcSyncer.kt b/mirai-core/src/commonMain/kotlin/network/components/MessageSvcSyncer.kt index 75bd4ff86..6712ec697 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/MessageSvcSyncer.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/MessageSvcSyncer.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.internal.network.components @@ -12,6 +12,7 @@ package net.mamoe.mirai.internal.network.components import kotlinx.coroutines.* import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import net.mamoe.mirai.event.globalEventChannel import net.mamoe.mirai.event.nextEvent import net.mamoe.mirai.internal.QQAndroidBot import net.mamoe.mirai.internal.network.component.ComponentKey @@ -59,7 +60,7 @@ internal class MessageSvcSyncerImpl( logger.info { "Syncing friend message history..." } withTimeoutOrNull(30000) { launch(CoroutineName("Syncing friend message history")) { - nextEvent { + globalEventChannel().nextEvent { it.bot == this@MessageSvcSyncerImpl.bot } } diff --git a/mirai-core/src/commonTest/kotlin/testFramework/codegen/test/OptimizeByteArrayAsHexStringTransformerTest.kt b/mirai-core/src/commonTest/kotlin/testFramework/codegen/test/OptimizeByteArrayAsHexStringTransformerTest.kt index 0e7c8f061..23b3cc76e 100644 --- a/mirai-core/src/commonTest/kotlin/testFramework/codegen/test/OptimizeByteArrayAsHexStringTransformerTest.kt +++ b/mirai-core/src/commonTest/kotlin/testFramework/codegen/test/OptimizeByteArrayAsHexStringTransformerTest.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. @@ -24,10 +24,10 @@ internal class OptimizeByteArrayAsHexStringTransformerTest : AbstractTest() { private inline fun analyzeTransformAndRender( value: T, renderer: ValueDescToStringRenderer = ValueDescToStringRenderer() - ): String? { + ): String { return ValueDescAnalyzer.analyze(value) .transform(OptimizeByteArrayAsHexStringTransformer()) - ?.renderToString(renderer) + .renderToString(renderer) } @Test diff --git a/mirai-core/src/jvmTest/kotlin/message/data/MessageReceiptTest.kt b/mirai-core/src/jvmTest/kotlin/message/data/MessageReceiptTest.kt index 9bfd4a4da..49896a086 100644 --- a/mirai-core/src/jvmTest/kotlin/message/data/MessageReceiptTest.kt +++ b/mirai-core/src/jvmTest/kotlin/message/data/MessageReceiptTest.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.internal.message.data @@ -42,7 +42,7 @@ internal class MessageReceiptTest : AbstractTestWithMiraiImpl() { private val bot = MockBot() /** - * This test is very ugly but we cannot do anything else. + * This test is very ugly, but we cannot do anything else. */ // We need #1304 @Test fun `refine ForwardMessageInternal for MessageReceipt`() = runBlockingUnit { @@ -56,17 +56,19 @@ internal class MessageReceiptTest : AbstractTestWithMiraiImpl() { val handler = object : GroupSendMessageHandler(group) { override val messageSvcSendMessage: (client: QQAndroidClient, contact: GroupImpl, message: MessageChain, fragmented: Boolean, sourceCallback: (Deferred) -> Unit) -> List = - { _, contact, message, fragmented, sourceCallback -> + { _, _, message, _, sourceCallback -> assertIs(message[ForwardMessageInternal]) assertSame(forward, message[ForwardMessageInternal]?.origin) - sourceCallback(CompletableDeferred(OnlineMessageSourceToGroupImpl( - group, - internalIds = intArrayOf(1), - sender = bot, - target = group, - time = currentTimeSeconds().toInt(), + sourceCallback( + CompletableDeferred( + OnlineMessageSourceToGroupImpl( + group, + internalIds = intArrayOf(1), + sender = bot, + target = group, + time = currentTimeSeconds().toInt(), originalMessage = message //, // sourceMessage = message )))