Browse Source

Adjustments

Him188 6 years ago
parent
commit
d95a547347

+ 3 - 3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/BotSession.kt

@@ -62,7 +62,7 @@ class BotSession(
 
     /**
      * 发送一个数据包, 并期待接受一个特定的 [ServerPacket][P].
-     * 这个方法会立即返回.
+     * 这个方法会立即发出这个数据包然后返回一个 [CompletableDeferred].
      *
      * 实现方法:
      * ```kotlin
@@ -74,9 +74,9 @@ class BotSession(
      * ```
      * @sample net.mamoe.mirai.network.protocol.tim.packet.action.uploadImage
      *
-     * @param checkSequence 是否期待 [ServerPacket.sequenceId] 与 [OutgoingPacket.sequenceId] 相同的包.
+     * @param checkSequence 是否筛选 `sequenceId`, 即是否筛选发出的包对应的返回包.
      * @param P 期待的包
-     * @param handler 处理期待的包. 将会在调用 [sendAndExpect] 的函数所在 [coroutineContext] 下执行.
+     * @param handler 处理期待的包. 将会在调用本函数的 [coroutineContext] 下执行.
      *
      * @see Bot.withSession 转换接收器 (receiver, 即 `this` 的指向) 为 [BotSession]
      */

+ 3 - 3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/TIMBotNetworkHandler.kt

@@ -233,6 +233,9 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
             packet: TPacket,
             factory: PacketFactory<TPacket, *>
         ) {
+            if (ServerPacketReceivedEvent(bot, packet).broadcast().cancelled)
+                return
+
             if (!packet::class.annotations.filterIsInstance<NoLog>().any()) {
                 bot.logger.verbose("Packet received: $packet")
             }
@@ -250,9 +253,6 @@ internal class TIMBotNetworkHandler internal constructor(override inline val bot
                 it.doReceiveWithoutExceptions(packet)
             }
 
-            if (ServerPacketReceivedEvent(bot, packet).broadcast().cancelled)
-                return
-
             if (factory is SessionPacketFactory<*>) {
                 with(factory as SessionPacketFactory<TPacket>) {
                     handlePacket(packet)

+ 1 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/handler/ActionPacketHandler.kt

@@ -9,7 +9,7 @@ import kotlinx.coroutines.withContext
 import net.mamoe.mirai.network.BotSession
 import net.mamoe.mirai.network.isOpen
 import net.mamoe.mirai.network.protocol.tim.packet.Packet
-import net.mamoe.mirai.network.protocol.tim.packet.RequestAccountInfoPacket
+import net.mamoe.mirai.network.protocol.tim.packet.action.RequestAccountInfoPacket
 import net.mamoe.mirai.network.protocol.tim.packet.login.RequestSKeyPacket
 import net.mamoe.mirai.network.protocol.tim.packet.login.SKey
 import net.mamoe.mirai.network.qqAccount

+ 3 - 3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/AddContact.kt

@@ -10,8 +10,8 @@ import net.mamoe.mirai.network.BotNetworkHandler
 import net.mamoe.mirai.network.protocol.tim.TIMProtocol
 import net.mamoe.mirai.network.protocol.tim.packet.*
 import net.mamoe.mirai.network.protocol.tim.packet.action.CanAddFriendResponse.State
+import net.mamoe.mirai.network.protocol.tim.packet.event.EventPacket
 import net.mamoe.mirai.utils.io.*
-import kotlin.properties.Delegates
 
 
 // 01BC 曾用名查询. 查到的是这个人的
@@ -119,8 +119,8 @@ object CanAddFriendPacket : SessionPacketFactory<CanAddFriendResponse>() {
 
 }
 
-class CanAddFriendResponse : Packet {
-    var qq: UInt by Delegates.notNull()
+class CanAddFriendResponse : EventPacket {
+    var qq: UInt = 0u
     lateinit var state: State
 
     enum class State {

+ 2 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/GradeInfo.kt

@@ -1,11 +1,12 @@
 @file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_UNSIGNED_LITERALS")
 
-package net.mamoe.mirai.network.protocol.tim.packet
+package net.mamoe.mirai.network.protocol.tim.packet.action
 
 import kotlinx.io.core.ByteReadPacket
 import kotlinx.io.core.writeUByte
 import net.mamoe.mirai.network.BotNetworkHandler
 import net.mamoe.mirai.network.protocol.tim.TIMProtocol
+import net.mamoe.mirai.network.protocol.tim.packet.*
 import net.mamoe.mirai.utils.io.encryptAndWrite
 import net.mamoe.mirai.utils.io.writeHex
 import net.mamoe.mirai.utils.io.writeQQ

+ 4 - 2
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/Profile.kt

@@ -34,6 +34,7 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR
     //00 01 3E F8 FB E3 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 29 4E 22 4E 25 4E 26 4E 27 4E 29 4E 2A 4E 2B 4E 2D 4E 2E 4E 2F 4E 30 4E 31 4E 33 4E 35 4E 36 4E 37 4E 38 4E 3F 4E 40 4E 41 4E 42 4E 43 4E 45 4E 49 4E 4B 4E 4F 4E 54 4E 5B 52 0B 52 0F 5D C2 5D C8 65 97 69 9D 69 A9 9D A5 A4 91 A4 93 A4 94 A4 9C A4 B5
     //00 01 B1 89 BE 09 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 29 4E 22 4E 25 4E 26 4E 27 4E 29 4E 2A 4E 2B 4E 2D 4E 2E 4E 2F 4E 30 4E 31 4E 33 4E 35 4E 36 4E 37 4E 38 4E 3F 4E 40 4E 41 4E 42 4E 43 4E 45 4E 49 4E 4B 4E 4F 4E 54 4E 5B 52 0B 52 0F 5D C2 5D C8 65 97 69 9D 69 A9 9D A5 A4 91 A4 93 A4 94 A4 9C A4 B5
     //00 01 87 73 86 9D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 29 4E 22 4E 25 4E 26 4E 27 4E 29 4E 2A 4E 2B 4E 2D 4E 2E 4E 2F 4E 30 4E 31 4E 33 4E 35 4E 36 4E 37 4E 38 4E 3F 4E 40 4E 41 4E 42 4E 43 4E 45 4E 49 4E 4B 4E 4F 4E 54 4E 5B 52 0B 52 0F 5D C2 5D C8 65 97 69 9D 69 A9 9D A5 A4 91 A4 93 A4 94 A4 9C A4 B5
+    @PacketVersion(date = "2019.11.2", timVersion = "2.3.2.21173")
     operator fun invoke(
         bot: UInt,
         qq: UInt,
@@ -44,6 +45,7 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR
         writeHex("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 29 4E 22 4E 25 4E 26 4E 27 4E 29 4E 2A 4E 2B 4E 2D 4E 2E 4E 2F 4E 30 4E 31 4E 33 4E 35 4E 36 4E 37 4E 38 4E 3F 4E 40 4E 41 4E 42 4E 43 4E 45 4E 49 4E 4B 4E 4F 4E 54 4E 5B 52 0B 52 0F 5D C2 5D C8 65 97 69 9D 69 A9 9D A5 A4 91 A4 93 A4 94 A4 9C A4 B5")
     }
 
+    @PacketVersion(date = "2019.11.2", timVersion = "2.3.2.21173")
     override suspend fun ByteReadPacket.decode(id: PacketId, sequenceId: UShort, handler: BotNetworkHandler<*>): RequestProfileDetailsResponse =
         RequestProfileDetailsResponse().apply {
             discardExact(3)
@@ -59,8 +61,8 @@ object RequestProfileDetailsPacket : SessionPacketFactory<RequestProfileDetailsR
                     null -> error("Cannot determine gender, entry 0x4E29u not found")
                     0x02u -> Gender.FEMALE
                     0x01u -> Gender.MALE
-                    0x00u -> Gender.SECRET // 猜的
-                    else -> error("Cannot determine gender, bad value of 0x4E29u: ${map[0x4729u]!![0].toUHexString()}")
+                    else -> Gender.SECRET // 猜的
+                    //else -> error("Cannot determine gender, bad value of 0x4E29u: ${map[0x4729u]!![0].toUHexString()}")
                 },
                 birthday = map[0x4E3Fu]?.let { Date(it.toUInt().toInt()) }
             )

+ 1 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/action/UploadImage.kt

@@ -338,7 +338,7 @@ object GroupImageIdRequestPacket : SessionPacketFactory<GroupImageIdRequestPacke
         groupInternalId: GroupInternalId,
         image: ExternalImage,
         sessionKey: SessionKey
-    ) = buildOutgoingPacket {
+    ): OutgoingPacket = buildOutgoingPacket {
         writeQQ(bot)
         writeHex("04 00 00 00 01 01 01 00 00 68 20 00 00 00 00 00 00 00 00")
 

+ 1 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/FriendConversationIniliaze.kt

@@ -9,11 +9,11 @@ import net.mamoe.mirai.Bot
 import net.mamoe.mirai.network.protocol.tim.packet.PacketVersion
 
 
-@PacketVersion(date = "2019.11.2", timVersion = "2.3.2.21173")
 data class FriendConversationInitialize(
     val qq: UInt
 ) : EventPacket
 
+@PacketVersion(date = "2019.11.2", timVersion = "2.3.2.21173")
 object FriendConversationInitializedEventParserAndHandler : KnownEventParserAndHandler<FriendConversationInitialize>(0x0079u) {
     override suspend fun ByteReadPacket.parse(bot: Bot, identity: EventPacketIdentity): FriendConversationInitialize {
         discardExact(4)// 00 00 00 00

+ 4 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/PhoneMessage.kt

@@ -0,0 +1,4 @@
+package net.mamoe.mirai.network.protocol.tim.packet.event
+
+//来自 Android 给我的电脑发消息, ID 0211, 内容 "你好"
+//00 00 00 20 00 05 00 02 00 01 00 06 00 04 00 01 01 01 00 09 00 06 03 E9 20 02 EB 94 00 0A 00 04 01 00 00 00 00 00 00 54 00 00 00 3E 08 12 1A 16 08 07 10 91 04 18 DD F1 92 B7 07 20 83 76 38 DD F1 92 B7 07 50 04 42 21 08 DD F1 92 B7 07 10 DD F1 92 B7 07 18 01 20 07 40 DC 85 96 EE 05 48 DC 85 96 EE 05 50 FA AF FD 18 52 15 0A 06 08 01 10 00 50 01 1A 0B 08 E9 07 10 94 D7 8B 80 02 50 02 08 04 12 1D 08 E9 07 10 94 D7 8B 80 02 18 01 20 01 28 DD F1 92 B7 07 30 DD F1 92 B7 07 48 02 50 01 32 1B 08 80 80 B8 AE B5 02 10 01 18 00 20 01 2A 0C 0A 0A 08 01 12 06 E4 BD A0 E5 A5 BD

+ 3 - 2
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/network/protocol/tim/packet/event/Unknown.kt

@@ -13,9 +13,10 @@ import net.mamoe.mirai.utils.io.toUHexString
 
 data class UnknownEventPacket(
     val id: UShort,
+    val identity: EventPacketIdentity,
     val body: ByteReadPacket
 ) : EventPacket {
-    override fun toString(): String = "UnknownEventPacket(id=${id.toUHexString()})"
+    override fun toString(): String = "UnknownEventPacket(id=${id.toUHexString()}, identity=$identity)"
 }
 
 //TODO This class should be declared with `inline`, but a CompilationException will be thrown
@@ -23,7 +24,7 @@ class UnknownEventParserAndHandler(override val id: UShort) : EventParserAndHand
 
     override suspend fun ByteReadPacket.parse(bot: Bot, identity: EventPacketIdentity): UnknownEventPacket {
         MiraiLogger.debug("UnknownEventPacket(${id.toUHexString()}) = ${readBytes().toUHexString()}")
-        return UnknownEventPacket(id, this) //TODO the cause is that `this` reference.
+        return UnknownEventPacket(id, identity, this) //TODO the cause is that `this` reference.
     }
 
     override suspend fun BotNetworkHandler<*>.handlePacket(packet: UnknownEventPacket) {

+ 6 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/InputUtils.kt

@@ -49,6 +49,12 @@ fun Input.readTLVMap(expectingEOF: Boolean = false, tagSize: Int = 1): MutableMa
             type
         }.toUByte() != UByte.MAX_VALUE) {
 
+        check(!map.containsKey(type.toUInt())) {
+            "Count not readTLVMap: duplicated key 0x${type.toUInt().toUHexString("")}. " +
+                    "map=$map" +
+                    ", duplicating value=${this.readUShortLVByteArray()}" +
+                    ", remaining=" + if (expectingEOF) this.readBytes().toUHexString() else "[Not expecting EOF]"
+        }
         map[type.toUInt()] = this.readUShortLVByteArray()
     }
     return map