From c37457cafbf74f5cd43925c887df9531ce192a7f Mon Sep 17 00:00:00 2001 From: Him188 Date: Sat, 19 Jun 2021 18:28:21 +0800 Subject: [PATCH] Cleanup and move local serializable classes to top-level --- .../commonMain/kotlin/utils/Annotations.kt | 2 +- .../src/commonMain/kotlin/utils/DeviceInfo.kt | 26 ++++---- .../kotlin/message/MarketFaceImpl.kt | 2 +- ...AbstractKeepAliveNetworkHandlerSelector.kt | 2 +- .../protocol/packet/chat/image/LongConn.kt | 4 +- .../chat/receive/MessageSvc.PbGetMsg.kt | 2 +- .../chat/receive/OnlinePush.PbPushTransMsg.kt | 11 ++-- .../packet/chat/receive/OnlinePush.ReqPush.kt | 61 ++++++++++--------- .../network/protocol/packet/login/StatSvc.kt | 2 +- .../message/data/MessageSerializationTest.kt | 30 ++++----- 10 files changed, 72 insertions(+), 70 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/utils/Annotations.kt b/mirai-core-api/src/commonMain/kotlin/utils/Annotations.kt index 23ea3cb41..5187ad6ca 100644 --- a/mirai-core-api/src/commonMain/kotlin/utils/Annotations.kt +++ b/mirai-core-api/src/commonMain/kotlin/utils/Annotations.kt @@ -47,7 +47,7 @@ public annotation class MiraiInternalFile */ @Retention(AnnotationRetention.BINARY) @RequiresOptIn(level = RequiresOptIn.Level.WARNING) -@Target(CLASS, TYPEALIAS, FUNCTION, PROPERTY, FIELD, CONSTRUCTOR) +@Target(CLASS, TYPEALIAS, FUNCTION, PROPERTY, FIELD, CONSTRUCTOR, TYPE) @MustBeDocumented public annotation class MiraiExperimentalApi( public val message: String = "" diff --git a/mirai-core-api/src/commonMain/kotlin/utils/DeviceInfo.kt b/mirai-core-api/src/commonMain/kotlin/utils/DeviceInfo.kt index 04782c3a4..48dd90967 100644 --- a/mirai-core-api/src/commonMain/kotlin/utils/DeviceInfo.kt +++ b/mirai-core-api/src/commonMain/kotlin/utils/DeviceInfo.kt @@ -107,20 +107,20 @@ public class DeviceInfo( } } -public fun DeviceInfo.generateDeviceInfoData(): ByteArray { - @Serializable - class DevInfo( - @ProtoNumber(1) val bootloader: ByteArray, - @ProtoNumber(2) val procVersion: ByteArray, - @ProtoNumber(3) val codename: ByteArray, - @ProtoNumber(4) val incremental: ByteArray, - @ProtoNumber(5) val fingerprint: ByteArray, - @ProtoNumber(6) val bootId: ByteArray, - @ProtoNumber(7) val androidId: ByteArray, - @ProtoNumber(8) val baseBand: ByteArray, - @ProtoNumber(9) val innerVersion: ByteArray - ) +@Serializable +private class DevInfo( + @ProtoNumber(1) val bootloader: ByteArray, + @ProtoNumber(2) val procVersion: ByteArray, + @ProtoNumber(3) val codename: ByteArray, + @ProtoNumber(4) val incremental: ByteArray, + @ProtoNumber(5) val fingerprint: ByteArray, + @ProtoNumber(6) val bootId: ByteArray, + @ProtoNumber(7) val androidId: ByteArray, + @ProtoNumber(8) val baseBand: ByteArray, + @ProtoNumber(9) val innerVersion: ByteArray +) +public fun DeviceInfo.generateDeviceInfoData(): ByteArray { return ProtoBuf.encodeToByteArray( DevInfo.serializer(), DevInfo( bootloader, diff --git a/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt b/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt index 9d9b2940b..2f5f50c5d 100644 --- a/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt @@ -39,7 +39,7 @@ internal data class MarketFaceImpl internal constructor( * For refinement */ internal class MarketFaceInternal( - @JvmField private val delegate: ImMsgBody.MarketFace, + private val delegate: ImMsgBody.MarketFace, ) : MarketFace, RefinableMessage { override val name: String get() = delegate.faceName.decodeToString() override val id: Int get() = delegate.tabId diff --git a/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt b/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt index abdc5a0a8..633ded59e 100644 --- a/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt +++ b/mirai-core/src/commonMain/kotlin/network/handler/selector/AbstractKeepAliveNetworkHandlerSelector.kt @@ -93,7 +93,7 @@ internal abstract class AbstractKeepAliveNetworkHandlerSelector { if (this@AbstractKeepAliveNetworkHandlerSelector.current.compareAndSet(current, null)) { // invalidate the instance and try again. diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/image/LongConn.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/image/LongConn.kt index 227f5b1db..704303115 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/image/LongConn.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/image/LongConn.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. @@ -51,7 +51,7 @@ internal class LongConn { check(resp.subcmd == 1) val imgRsp = resp.msgTryupImgRsp.first() if (imgRsp.result != 0) { - return Response.Failed(imgRsp.failMsg ?: "") + return Response.Failed(imgRsp.failMsg) } return if (imgRsp.boolFileExit) { diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt index dd9d947a7..3eea3b67a 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/chat/receive/MessageSvc.PbGetMsg.kt @@ -228,7 +228,7 @@ internal object MessageSvcPbGetMsg : OutgoingPacketFactory, + @ProtoNumber(2) val appId: Int, // 1 + @ProtoNumber(3) val instId: Int, // 1 + @ProtoNumber(4) val longMessageFlag: Int, // 0 + @ProtoNumber(5) val reserved: ByteArray? = null // struct{ boolean(1), boolean(2) } +) : ProtoBuf + + // uSubMsgType to vProtobuf // 138 or 139: top_package/akln.java:1568 // 66: top_package/nhz.java:269 @@ -527,35 +557,6 @@ internal inline fun lambda528(crossinline block: suspend MsgType0x210.(QQAndroid internal object Transformers528 : Map by mapOf( 0x8AL to lambda528 { bot -> - @Serializable - class Wording( - @ProtoNumber(1) val itemID: Int = 0, - @ProtoNumber(2) val itemName: String = "" - ) : ProtoBuf - - @Serializable - class Sub8AMsgInfo( - @ProtoNumber(1) val fromUin: Long, - @ProtoNumber(2) val botUin: Long, - @ProtoNumber(3) val srcId: Int, - @ProtoNumber(4) val srcInternalId: Long, - @ProtoNumber(5) val time: Long, - @ProtoNumber(6) val random: Int, - @ProtoNumber(7) val pkgNum: Int, // 1 - @ProtoNumber(8) val pkgIndex: Int, // 0 - @ProtoNumber(9) val devSeq: Int, // 0 - @ProtoNumber(12) val flag: Int, // 1 - @ProtoNumber(13) val wording: Wording - ) : ProtoBuf - - @Serializable - class Sub8A( - @ProtoNumber(1) val msgInfo: List, - @ProtoNumber(2) val appId: Int, // 1 - @ProtoNumber(3) val instId: Int, // 1 - @ProtoNumber(4) val longMessageFlag: Int, // 0 - @ProtoNumber(5) val reserved: ByteArray? = null // struct{ boolean(1), boolean(2) } - ) : ProtoBuf return@lambda528 vProtobuf.loadAs(Sub8A.serializer()).msgInfo.asSequence() .filter { it.botUin == bot.id }.mapNotNull { info -> diff --git a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt index b28682831..eec4df725 100644 --- a/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt +++ b/mirai-core/src/commonMain/kotlin/network/protocol/packet/login/StatSvc.kt @@ -137,7 +137,7 @@ internal class StatSvc { fun online( client: QQAndroidClient, regPushReason: RegPushReason = RegPushReason.appRegister - ) = impl("online", client, 1 or 2 or 4, client.onlineStatus, regPushReason) { + ) = impl("online", client, 1L or 2 or 4, client.onlineStatus, regPushReason) { client.bot.components[ContactCacheService].friendListCache?.let { friendListCache: FriendListCache -> iLargeSeq = friendListCache.friendListSeq // timeStamp = friendListCache.timeStamp diff --git a/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt b/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt index ad90a6e53..1a259ecb1 100644 --- a/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt +++ b/mirai-core/src/jvmTest/kotlin/message/data/MessageSerializationTest.kt @@ -126,25 +126,25 @@ internal class MessageSerializationTest { } } + @Serializable + data class W( + val m: FileMessage + ) + @Test fun `test FileMessage serialization`() { - @Serializable - data class W( - val m: FileMessage - ) - val w = W(FileMessageImpl("id", 2, "name", 1)) println(w.serialize(W.serializer())) assertEquals(w, w.serialize(W.serializer()).deserialize(W.serializer())) } + @Serializable + data class RichWrapper( + val richMessage: RichMessage + ) + @Test fun `test polymorphic serialization`() { - @Serializable - data class RichWrapper( - val richMessage: RichMessage - ) - val string = format.encodeToString(RichWrapper.serializer(), RichWrapper(SimpleServiceMessage(1, "content"))) println(string) var element = format.parseToJsonElement(string) @@ -155,13 +155,13 @@ internal class MessageSerializationTest { assertEquals(1, element["serviceId"]?.cast()?.content?.toInt()) } + @Serializable + data class Wrapper( + val message: @Polymorphic SingleMessage + ) + @Test fun `test ShowImageFlag serialization`() { - @Serializable - data class Wrapper( - val message: @Polymorphic SingleMessage - ) - val string = format.encodeToString(Wrapper.serializer(), Wrapper(ShowImageFlag)) println(string) var element = format.parseToJsonElement(string)