From 3c408b90e0f19f931084ee1a540ef24af49e3460 Mon Sep 17 00:00:00 2001 From: Him188 Date: Sun, 15 Dec 2019 16:58:15 +0800 Subject: [PATCH] Fix GroupId.toGroupInternalId --- .../contact/GroupIdConvertions.kt | 26 ++++++++++--------- .../test/packetdebugger/PacketDebugger.kt | 4 +-- .../mirai/contact/GroupIdConversionsKtTest.kt | 10 ++++++- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt index e383d3bb8..342c31768 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/GroupIdConvertions.kt @@ -13,20 +13,22 @@ fun GroupId.toInternalId(): GroupInternalId { if (this.value <= `10EXP6`) { return GroupInternalId(this.value) } - val left: Long = this.value.toString().dropLast(6).toLong() - val right: Long = this.value.toString().takeLast(6).toLong() + val stringValue = this.value.toString() + + fun plusLeft(leftIncrement: Int, rightLength: Int): String = + stringValue.let { (it.dropLast(rightLength).toLong() + leftIncrement).toString() + it.takeLast(rightLength) } return GroupInternalId( - when (left) { - in 1..10 -> ((left + 202).toString() + right.toString()).toUInt() - in 11..19 -> ((left + 469).toString() + right.toString()).toUInt() - in 20..66 -> ((left + 208).toString() + right.toString()).toUInt() - in 67..156 -> ((left + 1943).toString() + right.toString()).toUInt() - in 157..209 -> ((left + 199).toString() + right.toString()).toUInt() - in 210..309 -> ((left + 389).toString() + right.toString()).toUInt() - in 310..499 -> ((left + 349).toString() + right.toString()).toUInt() - else -> this.value - } + when (stringValue.dropLast(6).toInt()) { + in 1..10 -> plusLeft(202, 6) + in 11..19 -> plusLeft(469, 6) + in 20..66 -> plusLeft(208, 7) + in 67..156 -> plusLeft(1943, 6) + in 157..209 -> plusLeft(1997, 7) + in 210..309 -> plusLeft(389, 7) + in 310..499 -> plusLeft(349, 7) + else -> null + }?.toUInt() ?: this.value ) } diff --git a/mirai-core/src/jvmTest/kotlin/mirai/test/packetdebugger/PacketDebugger.kt b/mirai-core/src/jvmTest/kotlin/mirai/test/packetdebugger/PacketDebugger.kt index 0d078bde9..bf333577c 100644 --- a/mirai-core/src/jvmTest/kotlin/mirai/test/packetdebugger/PacketDebugger.kt +++ b/mirai-core/src/jvmTest/kotlin/mirai/test/packetdebugger/PacketDebugger.kt @@ -183,13 +183,13 @@ internal object PacketDebugger { * 7. 运行完 `mov eax,dword ptr ss:[ebp+10]` * 8. 查看内存, `eax` 到 `eax+10` 的 16 字节就是 `sessionKey` */ - val sessionKey: SessionKey = SessionKey("F3 4A 4E F4 79 C4 92 62 EF 0F D8 6E D3 AB E3 80".hexToBytes()) + val sessionKey: SessionKey = SessionKey("95 F3 24 8E 7B B6 62 AA 98 C0 EE 45 CE CE 2B 69".hexToBytes()) // TODO: 2019/12/7 无法访问 internal 是 kotlin bug, KT-34849 /** * null 则不筛选 */ - val qq: UInt? = null + val qq: UInt? = 761025446u /** * 打开后则记录每一个包到文件. */ diff --git a/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt b/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt index 2c03fae54..505b6e282 100644 --- a/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt +++ b/mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/contact/GroupIdConversionsKtTest.kt @@ -4,9 +4,17 @@ import net.mamoe.mirai.test.shouldBeEqualTo import org.junit.Test import kotlin.random.Random +@UseExperimental(ExperimentalUnsignedTypes::class) internal class GroupIdConversionsKtTest { - @UseExperimental(ExperimentalUnsignedTypes::class) + @Test + fun checkToInternalId() { + GroupId(221056495u).toInternalId().value shouldBeEqualTo 4111056495u + // 61 056495 + //4111 056495 + } + + @Test fun toInternalId() { repeat(1000000) { _ ->