Procházet zdrojové kódy

Clearer function name

Him188 před 6 roky
rodič
revize
8c86b934e8

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/Event.kt

@@ -74,6 +74,7 @@ suspend fun <E : Event> E.broadcast(context: CoroutineContext = EmptyCoroutineCo
         EventLogger.debug(this::class.simpleName + " pre broadcast")
     }
     try {
+        @Suppress("EXPERIMENTAL_API_USAGE")
         return withContext(EventScope.newCoroutineContext(context)) { [email protected]() }
     } finally {
         if (EventDebuggingFlag) {

+ 1 - 0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/MessageSubscribers.kt

@@ -47,6 +47,7 @@ abstract class SenderAndMessage<TContact : Contact>(
     suspend inline fun Image.send() = this.sendTo(subject)
     suspend inline fun ImageId.send() = this.sendTo(subject)
     suspend inline fun Message.send() = this.sendTo(subject)
+    suspend inline fun String.send() = this.toMessage().sendTo(subject)
 
     // endregion
 }

+ 12 - 9
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/internal/MessageDataInternal.kt

@@ -19,15 +19,17 @@ internal fun IoBuffer.parseMessageFace(): Face {
 
 internal fun IoBuffer.parsePlainText(): PlainText {
     discardExact(1)//0x01
-    return PlainText(readLVString())
+    return PlainText(readUShortLVString())
 }
 
 internal fun IoBuffer.parseLongText0x19(): PlainText {
-    //AA 02 33 50 00 60 00 68 00 9A 01 2A 08 0A 20 C1 50 78 A7 C0 04 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00 98 03 00 A0 03 00 B0 03 00 C0 03 00 D0 03 00 E8 03 00
     discardExact(1)//0x01
-    val raw = readLVByteArray()
+    discard()
+    //AA 02 33 50 00 60 00 68 00 9A 01 2A 08 0A 20 C1 50 78 A7 C0 04 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00 98 03 00 A0 03 00 B0 03 00 C0 03 00 D0 03 00 E8 03 00
+    //AA 02 33 50 00 60 00 68 00 9A 01 2A 08 09 20 CB 50 78 80 80 04 C8 01 00 F0 01 00 F8 01 00 90 02 00 98 03 D3 02 A0 03 10 B0 03 00 C0 03 AF 9C 01 D0 03 00 E8 03 00
+    //AA 02 30 50 00 60 00 68 00 9A 01 27 08 0A 78 A7 C0 04 80 01 01 C8 01 00 F0 01 00 F8 01 00 90 02 00 98 03 00 A0 03 00 B0 03 00 C0 03 00 D0 03 00 E8 03 00
     // 应该是手机发送时的字体或气泡之类的
-    println("parseLongText0x19.raw=${raw.toUHexString()}")
+    // println("parseLongText0x19.raw=${raw.toUHexString()}")
     return PlainText("")
 }
 
@@ -60,7 +62,7 @@ fun main() {
 internal fun IoBuffer.parseMessageImage0x03(): Image {
     discardExact(1)
 
-    return Image(ImageId(String(readLVByteArray()).adjustImageId()))
+    return Image(ImageId(String(readUShortLVByteArray()).adjustImageId()))
 }
 
 private operator fun String.get(range: IntRange) = this.substring(range)
@@ -86,9 +88,9 @@ internal fun ByteReadPacket.readMessage(): Message? {
             0x06 -> sectionData.parseMessageImage0x06()
 
 
-            0x19 -> {//长文本前一部分? 可能不是长文本, 总长度为 0x5C=92, body长度为0x59=89
+            0x19 -> {
                 //19 00  5C / 01  00  59  AA  02  56  30  01  3A  40  6E  35  46  4F  62  68  75  4B  6F  65  31  4E  63  45  41  6B  77  4B  51  5A  5A  4C  47  54  57  43  68  30  4B  56  7A  57  44  38  67  58  70  37  62  77  6A  67  51  69  66  66  53  4A  63  4F  69  78  4F  75  37  36  49  49  4F  37  48  32  55  63  9A  01  0F  80  01  01  C8  01  00  F0  01  00  F8  01  00  90  02  00  14  01  75  01  01  6B  01  78  9C  CD  92  BB  4E  C3  30  14  86  77  9E  C2  32  73  DA  A4  21  24  48  4E  AA  F4  06  A5  B4  51  55  A0  A8  0B  4A  5D  27  35  E4  82  72  69  4B  B7  6E  08  06  C4  C0  06  42  48  30  20  21  60  62  EB  E3  34  F4  31  70  4A  11  23  23  FC  96  2C  F9  D8  BF  CF  F1  77  8C  F2  23  D7  01  03  12  84  D4  F7  54  28  64  78  08  88  87  FD  1E  F5  6C  15  C6  91  C5  29  30  AF  AD  00  26  E4  86  36  E8  06  94  58  2A  CC  FC  73  41  E0  1E  5A  D4  21  0D  D3  25  2A  2C  55  0A  1B  D2  BA  5E  E0  24  91  D7  B9  B5  72  41  E1  74  B9  5C  E0  78  25  27  8B  92  28  14  45  45  FF  76  B4  E8  98  39  18  05  13  47  0B  24  03  4A  86  F5  D8  89  68  3D  B4  21  B0  1C  93  71  11  21  08  49  30  A0  98  54  4B  6C  25  A5  E6  80  84  B4  A7  42  4F  AA  18  DD  7E  5C  F3  89  D0  C0  65  FD  78  58  6B  76  3A  3B  9B  BB  ED  62  9F  AF  ED  8F  DB  25  C5  3E  38  91  BB  C3  23  BB  49  2D  AB  B5  8D  0D  3A  32  62  79  BD  5A  35  E4  AD  DC  1E  86  40  03  88  46  C4  05  8E  79  EA  C7  11  EB  09  64  91  88  46  0E  D1  C0  5F  73  FD  4D  00  65  97  95  02  D4  0F  34  94  65  D3  B2  78  80  7D  C7  0F  54  B8  AA  F0  E9  60  8F  4A  EE  1E  3F  6E  2E  84  E4  F6  7E  3E  7D  9E  5D  5E  25  EF  67  C9  E4  15  FC  DC  81  B2  29  08  0D  85  7E  1C  60  02  BC  45  33  E7  93  F3  D9  C3  D3  FC  E5  6D  36  BD  86  2C  C3  D7  66  7A  98  FD  4F  ED  13  9B  C7  C1  78  02  00  04  00  00  00  23  0E  00  07  01  00  04  00  00  00  09
-                //1000个 "." 被压缩为上面这条消息.
+                //1000个 "." 被压缩为上面这条消息?
 
                 //bot手机自己跟自己发消息会出这个
                 //似乎手机发消息就会有这个?
@@ -96,7 +98,8 @@ internal fun ByteReadPacket.readMessage(): Message? {
                 //             01 00 1C AA 02 19 08 00 88 01 00 9A 01 11 78 00 C8 01 00 F0 01 00 F8 01 00 90 02 00 C8 02 00
                 //return null
 
-                sectionData.parseLongText0x19()
+                return null
+                // sectionData.parseLongText0x19()
             }
 
 
@@ -105,7 +108,7 @@ internal fun ByteReadPacket.readMessage(): Message? {
 
                 //是否要用 sectionData.read?
                 discardExact(1)
-                val value = readLVByteArray()
+                val value = readUShortLVByteArray()
                 println(value.size)
                 println("0x14的未知压缩的data=" + value.toUHexString())
                 //todo 未知压缩算法

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

@@ -22,7 +22,7 @@ object TIMProtocol {
     }()//不使用lazy, 在初始化时就加载.
 
     const val head = "02"
-    const val ver = "37 13"
+    const val ver = "37 13"// TIM 最新版中这个有时候是 38 03
     const val fixVer = "03 00 00 00 01 2E 01 00 00 68 52 00 00 00 00"
     const val tail = "03"
     /**

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

@@ -45,7 +45,7 @@ fun ByteReadPacket.parseServerPacket(size: Int): ServerPacket {
                         135 -> {//包数据错误. 目前怀疑是 tlv0006
                             this.readRemainingBytes().cutTail(1).decryptBy(TIMProtocol.shareKey).read {
                                 discardExact(51)
-                                MiraiLogger.error("Internal error: " + readLVString())//抱歉,请重新输入密码。
+                                MiraiLogger.error("Internal error: " + readUShortLVString())//抱歉,请重新输入密码。
                             }
 
                             LoginResult.INTERNAL_ERROR
@@ -76,6 +76,7 @@ fun ByteReadPacket.parseServerPacket(size: Int): ServerPacket {
         HEARTBEAT -> ResponsePacket.Encrypted<HeartbeatPacket.Response>(this)
         GROUP_IMAGE_ID -> ResponsePacket.Encrypted<GroupImageIdRequestPacket.Response>(this)
         FRIEND_IMAGE_ID -> ResponsePacket.Encrypted<FriendImageIdRequestPacket.Response>(this)
+        REQUEST_PROFILE_DETAILS -> ResponsePacket.Encrypted<RequestProfileDetailsPacket.Response>(this)
         // 0x01_BDu -> EventResponse.Encrypted<SubmitImageFilenamePacket.Response>(this)
 
         else -> UnknownServerPacket.Encrypted(this, PacketId(id), sequenceId)
@@ -90,39 +91,38 @@ fun Input.readIP(): String = buildString(4 + 3) {
     }
 }
 
-fun Input.readLVString(): String = String(this.readLVByteArray())
+fun Input.readUShortLVString(): String = String(this.readUShortLVByteArray())
 
-fun Input.readLVByteArray(): ByteArray = this.readBytes(this.readShort().toInt())
+fun Input.readUShortLVByteArray(): ByteArray = this.readBytes(this.readUShort().toInt())
 
-fun Input.readTLVMap(expectingEOF: Boolean = false): Map<Int, ByteArray> {
-    val map = mutableMapOf<Int, ByteArray>()
-    var type: UByte
+private inline fun <R> inline(block: () -> R): R = block()
 
-    try {
-        type = readUByte()
-    } catch (e: EOFException) {
-        if (expectingEOF) {
-            return map
-        }
-        throw e
-    }
-
-    while (type != UByte.MAX_VALUE) {
-        map[type.toInt()] = this.readLVByteArray()
+fun Input.readTLVMap(expectingEOF: Boolean = false, tagSize: Int = 1): MutableMap<UInt, ByteArray> {
+    val map = mutableMapOf<UInt, ByteArray>()
+    var type: UShort = 0u
 
-        try {
-            type = readUByte()
-        } catch (e: EOFException) {
-            if (expectingEOF) {
-                return map
+    while (inline {
+            try {
+                type = when (tagSize) {
+                    1 -> readUByte().toUShort()
+                    2 -> readUShort()
+                    else -> error("Unsupported tag size: $tagSize")
+                }
+            } catch (e: EOFException) {
+                if (expectingEOF) {
+                    return map
+                }
+                throw e
             }
-            throw e
-        }
+            type
+        }.toUByte() != UByte.MAX_VALUE) {
+
+        map[type.toUInt()] = this.readUShortLVByteArray()
     }
     return map
 }
 
-fun Map<Int, ByteArray>.printTLVMap(name: String) =
+fun Map<*, ByteArray>.printTLVMap(name: String) =
     debugPrintln("TLVMap $name= " + this.mapValues { (_, value) -> value.toUHexString() })
 
 fun Input.readString(length: Number): String = String(this.readBytes(length.toInt()))

+ 11 - 1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/TypeConvertion.kt

@@ -25,6 +25,11 @@ fun Int.toByteArray(): ByteArray = byteArrayOf(
     (shr(0) and 0xFF).toByte()
 )
 
+/**
+ * 转 [ByteArray] 后再转 hex
+ */
+fun Int.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator)
+
 /**
  * 255 -> 00 FF
  */
@@ -35,6 +40,11 @@ fun UShort.toByteArray(): ByteArray = with(toUInt()) {
     )
 }
 
+/**
+ * 转 [ByteArray] 后再转 hex
+ */
+fun UShort.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator)
+
 /**
  * 255u -> 00 00 00 FF
  */
@@ -48,7 +58,7 @@ fun UInt.toByteArray(): ByteArray = byteArrayOf(
 /**
  * 转 [ByteArray] 后再转 hex
  */
-fun Int.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator)
+fun UInt.toUHexString(separator: String = " "): String = this.toByteArray().toUHexString(separator)
 
 /**
  * 转无符号十六进制表示, 并补充首位 `0`.