From bcdff1336ba668a1b8301cacf65b698e81e91113 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 27 May 2021 14:39:13 +0800 Subject: [PATCH] Kotlin 1.5 and dependencies updates (#1246) * Kotlin 1.5 and dependencies updates * Update mirai-console ref * Use Kotlin 1.5.10 * 2.7-M1-dev-3 * Update console ref --- .../api/binary-compatibility-validator-android.api | 9 +++++---- .../api/binary-compatibility-validator.api | 9 +++++---- buildSrc/src/main/kotlin/ProjectConfigure.kt | 4 +++- buildSrc/src/main/kotlin/Versions.kt | 14 +++++++------- mirai-console | 2 +- .../src/commonMain/kotlin/contact/ContactOrBot.kt | 2 +- .../src/commonMain/kotlin/event/select.kt | 3 ++- .../kotlin/message/action/AsyncRecallResult.kt | 6 ++++-- .../src/commonMain/kotlin/message/data/Message.kt | 2 +- .../commonMain/kotlin/message/data/MessageChain.kt | 7 ++++--- .../kotlin/message/data/SingleMessage.kt | 6 +++--- .../commonMain/kotlin/utils/BotConfiguration.kt | 5 ++--- .../src/commonMain/kotlin/utils/RemoteFile.kt | 2 +- .../src/commonTest/kotlin/utils/TimeTest.kt | 4 ++-- .../src/jvmMain/kotlin/utils/LoginSolver.jvm.kt | 6 ++---- .../src/jvmTest/kotlin/event/EventTests.kt | 3 +-- mirai-core-utils/src/commonMain/kotlin/Bytes.kt | 11 +++++------ .../src/commonMain/kotlin/Conversions.kt | 4 ++-- .../src/commonMain/kotlin/StandardUtils.kt | 5 +++++ .../src/commonMain/kotlin/message/imagesImpl.kt | 2 +- .../commonMain/kotlin/network/highway/Highway.kt | 12 +----------- .../kotlin/network/protocol/LoginType.kt | 5 +++-- .../kotlin/network/protocol/data/proto/OIDB.kt | 1 + .../kotlin/network/protocol/packet/Tlv.kt | 3 ++- .../packet/chat/receive/OnlinePush.ReqPush.kt | 11 ++++------- .../src/commonMain/kotlin/utils/GuidSource.kt | 3 ++- .../src/commonMain/kotlin/utils/NetworkType.kt | 6 ++++-- .../src/commonMain/kotlin/utils/ScheduledJob.kt | 2 +- mirai-core/src/commonMain/kotlin/utils/flags.kt | 3 ++- .../io/serialization/tars/internal/TarsDecoder.kt | 7 +++---- .../io/serialization/tars/internal/TarsOld.kt | 4 ++-- .../kotlin/utils/io/serialization/utils.kt | 3 ++- 32 files changed, 84 insertions(+), 82 deletions(-) diff --git a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api index a63661948..fe0decbec 100644 --- a/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api +++ b/binary-compatibility-validator/android/api/binary-compatibility-validator-android.api @@ -510,8 +510,8 @@ public final class net/mamoe/mirai/contact/NormalMemberKt { public static final fun getNameCardOrNick (Lnet/mamoe/mirai/contact/User;)Ljava/lang/String; public static final fun getNameCardOrNick (Lnet/mamoe/mirai/contact/UserOrBot;)Ljava/lang/String; public static final fun isMuted (Lnet/mamoe/mirai/contact/NormalMember;)Z - public static final fun mute-8Mi8wO0 (Lnet/mamoe/mirai/contact/NormalMember;DLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun mute-fcu0wV4 (Lnet/mamoe/mirai/contact/NormalMember;DLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun mute-8Mi8wO0 (Lnet/mamoe/mirai/contact/NormalMember;JLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final synthetic fun mute-fcu0wV4 (Lnet/mamoe/mirai/contact/NormalMember;JLkotlin/coroutines/Continuation;)Ljava/lang/Object; } public abstract interface class net/mamoe/mirai/contact/OtherClient : net/mamoe/mirai/contact/Contact { @@ -5519,6 +5519,7 @@ public final class net/mamoe/mirai/network/ForceOfflineException : java/util/con } public abstract class net/mamoe/mirai/network/LoginFailedException : java/lang/RuntimeException { + public synthetic fun (ZLjava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun (ZLjava/lang/String;Ljava/lang/Throwable;Lkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getKillBot ()Z } @@ -5641,11 +5642,11 @@ public final class net/mamoe/mirai/utils/BotConfiguration$ContactListCache { public fun ()V public final fun getFriendListCacheEnabled ()Z public final fun getGroupMemberListCacheEnabled ()Z - public final synthetic fun getSaveInterval-UwyO8pc ()D + public final synthetic fun getSaveInterval-UwyO8pc ()J public final fun getSaveIntervalMillis ()J public final fun setFriendListCacheEnabled (Z)V public final fun setGroupMemberListCacheEnabled (Z)V - public final synthetic fun setSaveInterval-LRDsOJo (D)V + public final synthetic fun setSaveInterval-LRDsOJo (J)V public final fun setSaveIntervalMillis (J)V } diff --git a/binary-compatibility-validator/api/binary-compatibility-validator.api b/binary-compatibility-validator/api/binary-compatibility-validator.api index cf37e2747..b40dfcf5c 100644 --- a/binary-compatibility-validator/api/binary-compatibility-validator.api +++ b/binary-compatibility-validator/api/binary-compatibility-validator.api @@ -510,8 +510,8 @@ public final class net/mamoe/mirai/contact/NormalMemberKt { public static final fun getNameCardOrNick (Lnet/mamoe/mirai/contact/User;)Ljava/lang/String; public static final fun getNameCardOrNick (Lnet/mamoe/mirai/contact/UserOrBot;)Ljava/lang/String; public static final fun isMuted (Lnet/mamoe/mirai/contact/NormalMember;)Z - public static final fun mute-8Mi8wO0 (Lnet/mamoe/mirai/contact/NormalMember;DLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public static final synthetic fun mute-fcu0wV4 (Lnet/mamoe/mirai/contact/NormalMember;DLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final fun mute-8Mi8wO0 (Lnet/mamoe/mirai/contact/NormalMember;JLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static final synthetic fun mute-fcu0wV4 (Lnet/mamoe/mirai/contact/NormalMember;JLkotlin/coroutines/Continuation;)Ljava/lang/Object; } public abstract interface class net/mamoe/mirai/contact/OtherClient : net/mamoe/mirai/contact/Contact { @@ -5519,6 +5519,7 @@ public final class net/mamoe/mirai/network/ForceOfflineException : java/util/con } public abstract class net/mamoe/mirai/network/LoginFailedException : java/lang/RuntimeException { + public synthetic fun (ZLjava/lang/String;Ljava/lang/Throwable;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun (ZLjava/lang/String;Ljava/lang/Throwable;Lkotlin/jvm/internal/DefaultConstructorMarker;)V public final fun getKillBot ()Z } @@ -5641,11 +5642,11 @@ public final class net/mamoe/mirai/utils/BotConfiguration$ContactListCache { public fun ()V public final fun getFriendListCacheEnabled ()Z public final fun getGroupMemberListCacheEnabled ()Z - public final synthetic fun getSaveInterval-UwyO8pc ()D + public final synthetic fun getSaveInterval-UwyO8pc ()J public final fun getSaveIntervalMillis ()J public final fun setFriendListCacheEnabled (Z)V public final fun setGroupMemberListCacheEnabled (Z)V - public final synthetic fun setSaveInterval-LRDsOJo (D)V + public final synthetic fun setSaveInterval-LRDsOJo (J)V public final fun setSaveIntervalMillis (J)V } diff --git a/buildSrc/src/main/kotlin/ProjectConfigure.kt b/buildSrc/src/main/kotlin/ProjectConfigure.kt index ca98ff9e1..155a0cae6 100644 --- a/buildSrc/src/main/kotlin/ProjectConfigure.kt +++ b/buildSrc/src/main/kotlin/ProjectConfigure.kt @@ -31,9 +31,11 @@ fun Project.configureJvmTarget() { val defaultVer = JavaVersion.VERSION_1_8 tasks.withType(KotlinJvmCompile::class.java) { - kotlinOptions.languageVersion = "1.4" + kotlinOptions.languageVersion = "1.5" kotlinOptions.jvmTarget = defaultVer.toString() kotlinOptions.freeCompilerArgs += "-Xjvm-default=all" + kotlinOptions.freeCompilerArgs += "-XXLanguage:-JvmIrEnabledByDefault" + // TODO: 2021/5/6 We are still using legacy JVM backend since kotlinx.serialization is not yet supported in Kotlin 1.5.0 } extensions.findByType(JavaPluginExtension::class.java)?.run { diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 330536860..4e2b3fa1f 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -12,20 +12,20 @@ import org.gradle.api.attributes.Attribute object Versions { - const val project = "2.6.4" + const val project = "2.7-M1-dev-3" const val core = project const val console = project const val consoleTerminal = project - const val kotlinCompiler = "1.4.30" - const val kotlinStdlib = "1.4.30" - const val dokka = "1.4.20" + const val kotlinCompiler = "1.5.10" + const val kotlinStdlib = "1.5.10" + const val dokka = "1.4.32" - const val coroutines = "1.4.2" - const val atomicFU = "0.15.1" + const val coroutines = "1.5.0" + const val atomicFU = "0.16.1" const val serialization = "1.1.0" - const val ktor = "1.5.1" + const val ktor = "1.5.4" const val binaryValidator = "0.4.0" diff --git a/mirai-console b/mirai-console index 410cdd02e..19abf48aa 160000 --- a/mirai-console +++ b/mirai-console @@ -1 +1 @@ -Subproject commit 410cdd02efaa70ed5a64603d121c44fd4f952985 +Subproject commit 19abf48aac4ad0eed63fd721ae9723f0686097d5 diff --git a/mirai-core-api/src/commonMain/kotlin/contact/ContactOrBot.kt b/mirai-core-api/src/commonMain/kotlin/contact/ContactOrBot.kt index 3bf5a2b1a..e568bb624 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/ContactOrBot.kt +++ b/mirai-core-api/src/commonMain/kotlin/contact/ContactOrBot.kt @@ -21,7 +21,7 @@ import net.mamoe.mirai.Bot * @see Contact * @see Bot */ -public interface ContactOrBot : CoroutineScope { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5 +public interface ContactOrBot : CoroutineScope { /** * QQ 号或群号. */ diff --git a/mirai-core-api/src/commonMain/kotlin/event/select.kt b/mirai-core-api/src/commonMain/kotlin/event/select.kt index 457b2dabd..bf385fc75 100644 --- a/mirai-core-api/src/commonMain/kotlin/event/select.kt +++ b/mirai-core-api/src/commonMain/kotlin/event/select.kt @@ -492,8 +492,9 @@ public abstract class MessageSelectBuilderUnit @PublishedAp protected abstract fun obtainCurrentDeferred(): CompletableDeferred? } +@JvmInline @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") -public inline class MessageSelectionTimeoutChecker internal constructor(public val timeoutMillis: Long) +public value class MessageSelectionTimeoutChecker internal constructor(public val timeoutMillis: Long) public class MessageSelectionTimeoutException : RuntimeException() diff --git a/mirai-core-api/src/commonMain/kotlin/message/action/AsyncRecallResult.kt b/mirai-core-api/src/commonMain/kotlin/message/action/AsyncRecallResult.kt index b3afffb69..598baa16d 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/action/AsyncRecallResult.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/action/AsyncRecallResult.kt @@ -37,8 +37,10 @@ public class AsyncRecallResult internal constructor( * 撤回是否成功. Kotlin [Deferred] API. */ public val isSuccess: Deferred by lazy { - GlobalScope.async { - kotlin.runCatching { exception.await() == null }.getOrElse { false } + CompletableDeferred().apply { + exception.invokeOnCompletion { + complete(it == null) + } } } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt index 69df72363..a6c8946a7 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt @@ -130,7 +130,7 @@ import kotlin.internal.LowPriorityInOverloadResolution * * @suppress **注意:** [Message] 类型大多有隐藏的协议实现, 不能被第三方应用继承. */ -public interface Message { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5 +public interface Message { /** * 得到包含 mirai 消息元素代码的, 易读的字符串. 如 `At(member) + "test"` 将转为 `"[mirai:at:qqId]test"`. diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt index 05dad41e9..dbcca5439 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt @@ -184,8 +184,8 @@ import kotlin.streams.asSequence * */ @Serializable(MessageChain.Serializer::class) -public interface MessageChain : - Message, List, RandomAccess, CodableMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5 +public sealed interface MessageChain : + Message, List, RandomAccess, CodableMessage { /** * 获取第一个类型为 [key] 的 [Message] 实例. 若不存在此实例, 返回 `null`. @@ -519,8 +519,9 @@ public inline operator fun MessageChain.getValue(thi * 可空的委托 * @see orNull */ +@JvmInline @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") -public inline class OrNullDelegate @PublishedApi internal constructor(@JvmField @PublishedApi internal val value: Any?) { +public value class OrNullDelegate @PublishedApi internal constructor(@JvmField @PublishedApi internal val value: Any?) { @Suppress("UNCHECKED_CAST") // don't inline, IC error public operator fun getValue(thisRef: Any?, property: KProperty<*>): R = value as R } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt index f75829793..86cadcda1 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt @@ -25,7 +25,7 @@ import net.mamoe.mirai.utils.safeCast * 单个消息元素. 与之相对的是 [MessageChain], 是多个 [SingleMessage] 的集合. */ // @Serializable(SingleMessage.Serializer::class) -public interface SingleMessage : Message { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5 +public interface SingleMessage : Message { /** * @suppress deprecated since 2.4.0 @@ -59,7 +59,7 @@ public interface SingleMessage : Message { // TODO: 2021/1/10 Make sealed interf * * @see ConstrainSingle 约束一个 [MessageChain] 中只存在这一种类型的元素 */ -public interface MessageMetadata : SingleMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5 +public interface MessageMetadata : SingleMessage { /** * 返回空字符串 */ @@ -82,7 +82,7 @@ public interface MessageMetadata : SingleMessage { // TODO: 2021/1/10 Make seale * @see MarketFace 商城表情 * @see MusicShare 音乐分享 */ -public interface MessageContent : SingleMessage { // TODO: 2021/1/10 Make sealed interface in Kotlin 1.5 +public interface MessageContent : SingleMessage { public companion object Key : AbstractMessageKey({ it.safeCast() }) } diff --git a/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt b/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt index f5b1015fb..5aedd1e32 100644 --- a/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt +++ b/mirai-core-api/src/commonMain/kotlin/utils/BotConfiguration.kt @@ -28,7 +28,6 @@ import kotlin.coroutines.EmptyCoroutineContext import kotlin.coroutines.coroutineContext import kotlin.time.Duration import kotlin.time.ExperimentalTime -import kotlin.time.milliseconds /** * [Bot] 配置. 用于 [BotFactory.newBot] @@ -471,9 +470,9 @@ public open class BotConfiguration { // open for Java */ @ExperimentalTime public inline var saveInterval: Duration - @JvmSynthetic inline get() = saveIntervalMillis.milliseconds + @JvmSynthetic inline get() = Duration.milliseconds(saveIntervalMillis) @JvmSynthetic inline set(v) { - saveIntervalMillis = v.toLongMilliseconds() + saveIntervalMillis = v.inWholeMilliseconds } /** diff --git a/mirai-core-api/src/commonMain/kotlin/utils/RemoteFile.kt b/mirai-core-api/src/commonMain/kotlin/utils/RemoteFile.kt index 242933b74..b3099bced 100644 --- a/mirai-core-api/src/commonMain/kotlin/utils/RemoteFile.kt +++ b/mirai-core-api/src/commonMain/kotlin/utils/RemoteFile.kt @@ -391,7 +391,7 @@ public interface RemoteFile { public fun SendChannel.asProgressionCallback(closeOnFinish: Boolean = true): ProgressionCallback { return object : ProgressionCallback { override fun onProgression(file: RemoteFile, resource: ExternalResource, downloadedSize: Long) { - offer(downloadedSize) + trySend(downloadedSize) } override fun onSuccess(file: RemoteFile, resource: ExternalResource) { diff --git a/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt b/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt index 634ebf2de..0ef3eec83 100644 --- a/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt +++ b/mirai-core-api/src/commonTest/kotlin/utils/TimeTest.kt @@ -38,11 +38,11 @@ internal class TimeTest { val time0 = (1.toDuration(DurationUnit.DAYS) + 20.toDuration(DurationUnit.HOURS) + 15.toDuration(DurationUnit.MINUTES) + - 2057.toDuration(DurationUnit.MILLISECONDS)).toLongMilliseconds() + 2057.toDuration(DurationUnit.MILLISECONDS)).inWholeMilliseconds println(time0.millisToHumanReadableString()) assertTrue { time0.millisToHumanReadableString() == "1d 20h 15min 2.057s" } - val time1 = (1.toDuration(DurationUnit.DAYS) + 59.toDuration(DurationUnit.MINUTES)).toLongMilliseconds() + val time1 = (1.toDuration(DurationUnit.DAYS) + 59.toDuration(DurationUnit.MINUTES)).inWholeMilliseconds println(time1.millisToHumanReadableString()) assertTrue { time1.millisToHumanReadableString() == "1d 59min 0.0s" } } diff --git a/mirai-core-api/src/jvmMain/kotlin/utils/LoginSolver.jvm.kt b/mirai-core-api/src/jvmMain/kotlin/utils/LoginSolver.jvm.kt index 2e900ec0f..1f4af7af0 100644 --- a/mirai-core-api/src/jvmMain/kotlin/utils/LoginSolver.jvm.kt +++ b/mirai-core-api/src/jvmMain/kotlin/utils/LoginSolver.jvm.kt @@ -10,9 +10,7 @@ package net.mamoe.mirai.utils -import kotlinx.coroutines.CoroutineName -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.* import kotlinx.coroutines.io.ByteWriteChannel import kotlinx.coroutines.io.close import kotlinx.coroutines.io.jvm.nio.copyTo @@ -20,7 +18,6 @@ import kotlinx.coroutines.io.reader import kotlinx.coroutines.io.writeFully import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock -import kotlinx.coroutines.withContext import net.mamoe.mirai.Bot import net.mamoe.mirai.internal.utils.SeleniumLoginSolver import net.mamoe.mirai.internal.utils.isSliderCaptchaSupportKind @@ -224,6 +221,7 @@ public class StandardCharImageLoginSolver @JvmOverloads constructor( } // Copied from Ktor CIO +@OptIn(DelicateCoroutinesApi::class) private fun File.writeChannel( coroutineContext: CoroutineContext = Dispatchers.IO ): ByteWriteChannel = GlobalScope.reader(CoroutineName("file-writer") + coroutineContext, autoFlush = true) { diff --git a/mirai-core-api/src/jvmTest/kotlin/event/EventTests.kt b/mirai-core-api/src/jvmTest/kotlin/event/EventTests.kt index 379573202..48d0f1014 100644 --- a/mirai-core-api/src/jvmTest/kotlin/event/EventTests.kt +++ b/mirai-core-api/src/jvmTest/kotlin/event/EventTests.kt @@ -10,7 +10,6 @@ package net.mamoe.mirai.event import kotlinx.coroutines.* -import net.mamoe.mirai.internal.event.GlobalEventListeners import net.mamoe.mirai.utils.StepUtil import org.junit.jupiter.api.AfterEach import java.util.concurrent.Executor @@ -47,7 +46,7 @@ class EventTests { fun testSubscribeGlobalScope() { resetEventListeners() runBlocking { - val listener = GlobalScope.globalEventChannel().subscribeAlways { + val listener = globalEventChannel().subscribeAlways { triggered = true } diff --git a/mirai-core-utils/src/commonMain/kotlin/Bytes.kt b/mirai-core-utils/src/commonMain/kotlin/Bytes.kt index fb881502d..5f4da0bf2 100644 --- a/mirai-core-utils/src/commonMain/kotlin/Bytes.kt +++ b/mirai-core-utils/src/commonMain/kotlin/Bytes.kt @@ -15,7 +15,6 @@ package net.mamoe.mirai.utils import kotlinx.io.charsets.Charset import kotlinx.io.core.ByteReadPacket -import java.util.* import kotlin.contracts.InvocationKind import kotlin.contracts.contract @@ -69,8 +68,8 @@ public operator fun ByteArray.get(rangeStart: Int, rangeEnd: Int): String = buil private fun Byte.fixToString(): String { return when (val b = this.toInt() and 0xff) { - in 0..15 -> "0${this.toString(16).toUpperCase()}" - else -> b.toString(16).toUpperCase() + in 0..15 -> "0${this.toString(16).uppercase()}" + else -> b.toString(16).uppercase() } } @@ -90,7 +89,7 @@ public fun ByteArray.toUHexString( return buildString(length * 2) { this@toUHexString.forEachIndexed { index, it -> if (index in offset until lastIndex) { - var ret = it.toUByte().toString(16).toUpperCase() + var ret = it.toUByte().toString(16).uppercase() if (ret.length == 1) ret = "0$ret" append(ret) if (index < lastIndex - 1) append(separator) @@ -120,7 +119,7 @@ public fun List.toUHexString(separator: String = " ", offset: Int = 0, len return buildString(length * 2) { this@toUHexString.forEachIndexed { index, it -> if (index in offset until lastIndex) { - var ret = it.toUByte().toString(16).toUpperCase() + var ret = it.toUByte().toString(16).uppercase() if (ret.length == 1) ret = "0$ret" append(ret) if (index < lastIndex - 1) append(separator) @@ -140,7 +139,7 @@ public fun UByteArray.toUHexString(separator: String = " ", offset: Int = 0, len return buildString(length * 2) { this@toUHexString.forEachIndexed { index, it -> if (index in offset until lastIndex) { - var ret = it.toByte().toUByte().toString(16).toUpperCase() + var ret = it.toByte().toUByte().toString(16).uppercase() if (ret.length == 1) ret = "0$ret" append(ret) if (index < lastIndex - 1) append(separator) diff --git a/mirai-core-utils/src/commonMain/kotlin/Conversions.kt b/mirai-core-utils/src/commonMain/kotlin/Conversions.kt index 5adee0990..197af27cb 100644 --- a/mirai-core-utils/src/commonMain/kotlin/Conversions.kt +++ b/mirai-core-utils/src/commonMain/kotlin/Conversions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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. @@ -113,7 +113,7 @@ public fun Byte.fixToUHex(): String = this.toUByte().fixToUHex() * 转无符号十六进制表示, 并补充首位 `0`. */ public fun UByte.fixToUHex(): String = - if (this.toInt() in 0..15) "0${this.toString(16).toUpperCase()}" else this.toString(16).toUpperCase() + if (this.toInt() in 0..15) "0${this.toString(16).uppercase()}" else this.toString(16).uppercase() public fun String.hexToBytes(): ByteArray = this.split(" ") diff --git a/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt b/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt index 1b4f41b01..4804a6cbf 100644 --- a/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt +++ b/mirai-core-utils/src/commonMain/kotlin/StandardUtils.kt @@ -12,6 +12,7 @@ package net.mamoe.mirai.utils +import java.util.* import kotlin.reflect.KClass public inline fun Any?.cast(): T = this as T @@ -194,4 +195,8 @@ public fun Throwable.causes(maxDepth: Int = 20): Sequence = sequence rootCause = rootCause.cause if (depth++ >= maxDepth) break } +} + +public fun String.capitalize(): String { + return replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.ROOT) else it.toString() } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt b/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt index aa2ebc35b..c10111399 100644 --- a/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt @@ -37,7 +37,7 @@ internal class OnlineGroupImageImpl( override val imageId: String = generateImageId( delegate.picMd5, - delegate.filePath.substringAfterLast('.').toLowerCase().let { ext -> + delegate.filePath.substringAfterLast('.').lowercase().let { ext -> if (ext == "null") { // official clients might send `null` getImageType(delegate.imageType) diff --git a/mirai-core/src/commonMain/kotlin/network/highway/Highway.kt b/mirai-core/src/commonMain/kotlin/network/highway/Highway.kt index d68b538c7..f34ceb33e 100644 --- a/mirai-core/src/commonMain/kotlin/network/highway/Highway.kt +++ b/mirai-core/src/commonMain/kotlin/network/highway/Highway.kt @@ -12,7 +12,6 @@ package net.mamoe.mirai.internal.network.highway import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel -import kotlinx.coroutines.channels.receiveOrNull import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.produceIn @@ -325,7 +324,7 @@ internal suspend fun ChunkedFlowSession.sendConcurrently( launch(CoroutineName("Worker $it")) { val socket = createConnection() while (isActive) { - val next = channel.tryReceive() ?: break // concurrent-safe receive + val next = channel.receiveCatching().getOrNull() ?: return@launch // concurrent-safe receive val result = next.withUse { socket.sendReceiveHighway(next, resultChecker) } @@ -335,15 +334,6 @@ internal suspend fun ChunkedFlowSession.sendConcurrently( } } -private suspend fun ReceiveChannel.tryReceive(): E? { - return kotlin.runCatching { - @OptIn(ExperimentalCoroutinesApi::class) - receiveOrNull() // this is experimental api - }.recoverCatching { - // in case binary changes - receive() - }.getOrNull() -} private suspend fun HighwayProtocolChannel.sendReceiveHighway( it: ByteReadPacket, diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/LoginType.kt b/mirai-core/src/commonMain/kotlin/network/protocol/LoginType.kt index e2baf73c1..261e6fb00 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/LoginType.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/LoginType.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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. @@ -9,7 +9,8 @@ package net.mamoe.mirai.internal.network.protocol -internal inline class LoginType( +@JvmInline +internal value class LoginType( val value: Int ) { companion object { diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/data/proto/OIDB.kt b/mirai-core/src/commonMain/kotlin/network/protocol/data/proto/OIDB.kt index 54f114142..b5c0d241d 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/data/proto/OIDB.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/data/proto/OIDB.kt @@ -14,6 +14,7 @@ import kotlinx.serialization.protobuf.ProtoNumber import net.mamoe.mirai.internal.network.Packet import net.mamoe.mirai.internal.utils.io.ProtoBuf import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY +import net.mamoe.mirai.utils.capitalize internal class Oidb0x5d4 : ProtoBuf { @Serializable diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/Tlv.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/Tlv.kt index 13b0a10a7..1093e43b3 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/Tlv.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/Tlv.kt @@ -45,7 +45,8 @@ internal fun TlvMap.smartToString(leadingLineBreak: Boolean = true, sorted: Bool /** * 显式表示一个 [ByteArray] 是一个 tlv 的 body */ -internal inline class Tlv(val value: ByteArray) +@JvmInline +internal value class Tlv(val value: ByteArray) internal fun BytePacketBuilder.t1(uin: Long, ip: ByteArray) { require(ip.size == 4) { "ip.size must == 4" } diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt index 816cc132f..8d5f915a2 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/OnlinePush.ReqPush.kt @@ -19,10 +19,7 @@ import kotlinx.io.core.* import kotlinx.serialization.Serializable import kotlinx.serialization.protobuf.ProtoNumber import net.mamoe.mirai.Mirai -import net.mamoe.mirai.contact.Member -import net.mamoe.mirai.contact.MemberPermission -import net.mamoe.mirai.contact.NormalMember -import net.mamoe.mirai.contact.User +import net.mamoe.mirai.contact.* import net.mamoe.mirai.data.GroupHonorType import net.mamoe.mirai.event.events.* import net.mamoe.mirai.internal.QQAndroidBot @@ -49,7 +46,7 @@ import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacket import net.mamoe.mirai.internal.network.protocol.packet.buildResponseUniPacket import net.mamoe.mirai.internal.network.protocol.packet.list.FriendList import net.mamoe.mirai.internal.network.protocol.packet.sendAndExpect -import net.mamoe.mirai.internal.utils._miraiContentToString +import net.mamoe.mirai.internal.utils.* import net.mamoe.mirai.internal.utils.io.ProtoBuf import net.mamoe.mirai.internal.utils.io.serialization.* import net.mamoe.mirai.internal.utils.parseToMessageDataList @@ -445,7 +442,7 @@ private object Transformers732 : Map by mapOf( else -> { /* bot.network.logger.debug("unknown Transformer732 0xunknown type: ${dataBytes[0].toString(16) - .toUpperCase()}") + .uppercase()}") bot.network.logger.debug("unknown Transformer732 0xdata= ${readBytes().toUHexString()}") */ return@lambda732 emptySequence() @@ -818,7 +815,7 @@ internal object Transformers528 : Map by mapOf( return@mapNotNull MemberCardChangeEvent(old, new, member) } 2 -> { - if (info.value.singleOrNull()?.toInt() != 0) { + if (info.value.singleOrNull()?.code != 0) { bot.logger.debug { "Unknown Transformers528 0x27L ModGroupMemberProfile, field=${info.field}, value=${info.value}" } diff --git a/mirai-core/src/commonMain/kotlin/utils/GuidSource.kt b/mirai-core/src/commonMain/kotlin/utils/GuidSource.kt index 716a0c987..ce92248f9 100644 --- a/mirai-core/src/commonMain/kotlin/utils/GuidSource.kt +++ b/mirai-core/src/commonMain/kotlin/utils/GuidSource.kt @@ -18,7 +18,8 @@ package net.mamoe.mirai.internal.utils * 17: 以前没保存但现在生成成功; */ @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") -internal inline class GuidSource private constructor(val id: Long) { // uint actually +@JvmInline +internal value class GuidSource private constructor(val id: Long) { // uint actually companion object { /** * 初始值 diff --git a/mirai-core/src/commonMain/kotlin/utils/NetworkType.kt b/mirai-core/src/commonMain/kotlin/utils/NetworkType.kt index ac83b7ade..98c8fabde 100644 --- a/mirai-core/src/commonMain/kotlin/utils/NetworkType.kt +++ b/mirai-core/src/commonMain/kotlin/utils/NetworkType.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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. @@ -12,12 +12,14 @@ package net.mamoe.mirai.internal.utils /** * 连接类型 */ -internal inline class NetworkType(val value: Int) { +@JvmInline +internal value class NetworkType(val value: Int) { companion object { /** * 移动网络 */ val MOBILE = NetworkType(1) + /** * Wifi */ diff --git a/mirai-core/src/commonMain/kotlin/utils/ScheduledJob.kt b/mirai-core/src/commonMain/kotlin/utils/ScheduledJob.kt index a51165601..d99f41bf7 100644 --- a/mirai-core/src/commonMain/kotlin/utils/ScheduledJob.kt +++ b/mirai-core/src/commonMain/kotlin/utils/ScheduledJob.kt @@ -34,7 +34,7 @@ internal class ScheduledJob( fun notice() { if (intervalMillis == 0L) { launch { task() } - } else channel.offer(Unit) + } else channel.trySend(Unit) } private suspend fun doTask() { diff --git a/mirai-core/src/commonMain/kotlin/utils/flags.kt b/mirai-core/src/commonMain/kotlin/utils/flags.kt index ac0bb9eb4..2c99fd53a 100644 --- a/mirai-core/src/commonMain/kotlin/utils/flags.kt +++ b/mirai-core/src/commonMain/kotlin/utils/flags.kt @@ -9,7 +9,8 @@ package net.mamoe.mirai.internal.utils -internal inline class MacOrAndroidIdChangeFlag(val value: Long = 0) { +@JvmInline +internal value class MacOrAndroidIdChangeFlag(val value: Long = 0) { fun macChanged(): MacOrAndroidIdChangeFlag = MacOrAndroidIdChangeFlag(this.value or 0x1) diff --git a/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsDecoder.kt b/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsDecoder.kt index ae71bc149..a8a06404e 100644 --- a/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsDecoder.kt +++ b/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsDecoder.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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. @@ -20,7 +20,6 @@ import kotlinx.serialization.internal.TaggedDecoder import kotlinx.serialization.modules.SerializersModule import net.mamoe.mirai.internal.utils.io.serialization.tars.Tars import net.mamoe.mirai.internal.utils.io.serialization.tars.TarsId -import net.mamoe.mirai.internal.utils.io.serialization.tars.internal.* @OptIn(InternalSerializationApi::class, ExperimentalSerializationApi::class) @@ -110,7 +109,7 @@ internal class TarsDecoder( override fun decodeFloat(): Float = input.useHead { input.readTarsFloatValue(it) } override fun decodeDouble(): Double = input.useHead { input.readTarsDoubleValue(it) } override fun decodeBoolean(): Boolean = input.useHead { input.readTarsBooleanValue(it) } - override fun decodeChar(): Char = decodeByte().toChar() + override fun decodeChar(): Char = decodeByte().toInt().toChar() override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = decodeInt() override fun decodeString(): String = input.useHead { input.readTarsStringValue(it) } @@ -158,7 +157,7 @@ internal class TarsDecoder( override fun decodeDouble(): Double = input.useHead { input.readTarsDoubleValue(it) } override fun decodeBoolean(): Boolean = input.useHead { input.readTarsBooleanValue(it) } - override fun decodeChar(): Char = decodeByte().toChar() + override fun decodeChar(): Char = decodeByte().toInt().toChar() override fun decodeEnum(enumDescriptor: SerialDescriptor): Int = decodeInt() override fun decodeString(): String = input.useHead { head -> input.readTarsStringValue(head).also { diff --git a/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsOld.kt b/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsOld.kt index c8ef2526e..970539acd 100644 --- a/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsOld.kt +++ b/mirai-core/src/commonMain/kotlin/utils/io/serialization/tars/internal/TarsOld.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2020 Mamoe Technologies and contributors. + * Copyright 2019-2021 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. @@ -235,7 +235,7 @@ internal class TarsOld internal constructor(private val charset: Charset, overri } public override fun encodeTaggedChar(tag: Int, value: Char) { - encodeTaggedByte(tag, value.toByte()) + encodeTaggedByte(tag, value.code.toByte()) } public override fun encodeTaggedEnum(tag: Int, enumDescriptor: SerialDescriptor, ordinal: Int) { diff --git a/mirai-core/src/commonMain/kotlin/utils/io/serialization/utils.kt b/mirai-core/src/commonMain/kotlin/utils/io/serialization/utils.kt index 8a3d91986..7a92475a6 100644 --- a/mirai-core/src/commonMain/kotlin/utils/io/serialization/utils.kt +++ b/mirai-core/src/commonMain/kotlin/utils/io/serialization/utils.kt @@ -179,7 +179,8 @@ internal fun ByteReadPacket.readProtoBuf( ): T = KtProtoBuf.decodeFromByteArray(serializer, this.readBytes(length)) @Suppress("NON_PUBLIC_PRIMARY_CONSTRUCTOR_OF_INLINE_CLASS") -internal inline class OidbBodyOrFailure private constructor( +@JvmInline +internal value class OidbBodyOrFailure private constructor( private val v: Any ) { internal class Failure(