1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-14 23:20:49 +08:00

(internal api) Rename debugPrintIfFail to debugIfFail

This commit is contained in:
Him188 2020-01-20 20:47:46 +08:00
parent 767790f6e1
commit 2ffd62c883
6 changed files with 39 additions and 9 deletions
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet
mirai-core-timpc/src/commonMain/kotlin/net.mamoe.mirai.timpc/network/packet/event
mirai-core/src/commonMain/kotlin/net.mamoe.mirai

View File

@ -8,8 +8,6 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.login.LoginPacket
import net.mamoe.mirai.qqandroid.network.protocol.packet.login.NullPacketId
import net.mamoe.mirai.qqandroid.network.protocol.packet.login.NullPacketId.commandName
import net.mamoe.mirai.qqandroid.network.protocol.packet.login.PacketId
import net.mamoe.mirai.utils.cryptor.Decrypter
import net.mamoe.mirai.utils.cryptor.DecrypterType
import net.mamoe.mirai.utils.cryptor.adjustToPublicKey
import net.mamoe.mirai.utils.cryptor.decryptBy
import net.mamoe.mirai.utils.io.*
@ -59,12 +57,12 @@ internal object KnownPacketFactories : List<PacketFactory<*>> by mutableListOf(
// do not inline. Exceptions thrown will not be reported correctly
suspend fun parseIncomingPacket(bot: QQAndroidBot, rawInput: ByteReadPacket, consumer: PacketConsumer) =
rawInput.debugPrintIfFail("Incoming packet") {
rawInput.debugIfFail("Incoming packet") {
require(remaining < Int.MAX_VALUE) { "rawInput is too long" }
val expectedLength = readUInt().toInt() - 4
if (expectedLength > 16e7) {
bot.logger.warning("Detect incomplete packet, ignoring.")
return@debugPrintIfFail
return@debugIfFail
}
check(remaining.toInt() == expectedLength) { "Invalid packet length. Expected $expectedLength, got ${rawInput.remaining} Probably packets merged? " }
// login
@ -87,7 +85,7 @@ internal object KnownPacketFactories : List<PacketFactory<*>> by mutableListOf(
@UseExperimental(ExperimentalUnsignedTypes::class)
private suspend fun parseLoginSsoPacket(bot: QQAndroidBot, rawInput: ByteReadPacket, consumer: PacketConsumer) =
rawInput.debugPrintIfFail("Login sso packet") {
rawInput.debugIfFail("Login sso packet") {
val commandName: String
val ssoSequenceId: Int
readIoBuffer(readInt() - 4).withUse {

View File

@ -10,7 +10,7 @@ import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.Member
import net.mamoe.mirai.data.*
import net.mamoe.mirai.utils.io.debugPrintIfFail
import net.mamoe.mirai.utils.io.debugIfFail
import net.mamoe.mirai.utils.io.readQQ
import net.mamoe.mirai.utils.io.readRemainingBytes
import net.mamoe.mirai.utils.io.toUHexString
@ -62,7 +62,7 @@ internal object MemberMuteEventPacketParserAndHandler : KnownEventParserAndHandl
Unknown0x02DCPacketFlag0x0EMaybeMutePacket(readRemainingBytes())
}
0x11u -> debugPrintIfFail("解析禁言包(0x02DC)时"){ // 猜测这个失败是撤回??
0x11u -> debugIfFail("解析禁言包(0x02DC)时"){ // 猜测这个失败是撤回??
// 00 0A 00 04 01 00 00 00 00 0C 00 05 00 01 00 01 01 27 0B 60 E7 11 00 33 08 07 20 E7 C1 AD B8 02 5A 29 08 A6 FE C0 A4 0A 1A 19 08 BC 15 10 C1 95 BC F0 05 18 CA CA 8F DE 04 20 00 28 00 30 A6 FE C0 A4 0A 2A 02 08 00 30 00 38 00
// 失败

View File

@ -9,7 +9,7 @@ import net.mamoe.mirai.utils.io.*
import net.mamoe.mirai.utils.unzip
internal fun IoBuffer.parseMessageFace(): Face {
debugPrintIfFail("Analyzing Face") {
debugIfFail("Analyzing Face") {
discardExact(1)
//00 01 AF 0B 00 08 00 01 00 04 52 CC F5 D0 FF 00 02 14 F0

View File

@ -5,6 +5,9 @@ import kotlinx.io.pool.useInstance
import net.mamoe.mirai.utils.DefaultLogger
import net.mamoe.mirai.utils.MiraiLogger
import net.mamoe.mirai.utils.withSwitch
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
object DebugLogger : MiraiLogger by DefaultLogger("Packet Debug").withSwitch()
@ -51,7 +54,18 @@ fun ByteReadPacket.debugPrint(name: String = ""): ByteReadPacket {
}
}
inline fun <R> Input.debugPrintIfFail(name: String = "", block: ByteReadPacket.() -> R): R {
/**
* 备份数据, 并在 [block] 失败后执行 [onFail].
*
* 此方法非常低效. 请仅在测试环境使用.
*/
@MiraiDebugAPI
@UseExperimental(ExperimentalContracts::class)
inline fun <R> Input.debugIfFail(name: String = "", onFail: (ByteArray) -> ByteReadPacket = { it.toReadPacket() }, block: ByteReadPacket.() -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
callsInPlace(onFail, InvocationKind.UNKNOWN)
}
ByteArrayPool.useInstance {
val count = this.readAvailable(it)
try {

View File

@ -99,6 +99,8 @@ fun UByte.fixToUHex(): String = if (this.toInt() in 0..15) "0${this.toString(16)
/**
* 将无符号 Hex 转为 [ByteArray], 有根据 hex [hashCode] 建立的缓存.
*
* 这个方法很累, 不建议经常使用.
*/
fun String.hexToBytes(): ByteArray =
this.split(" ")
@ -110,12 +112,24 @@ fun String.hexToBytes(): ByteArray =
/**
* 2 char 为一组, 转换 Hex [ByteArray]
*
* 这个方法很累, 不建议经常使用.
*/
fun String.chunkedHexToBytes(): ByteArray =
this.asSequence().chunked(2).map { (it[0].toString() + it[1]).toUByte(16).toByte() }.toList().toByteArray()
/**
* 删掉全部空格和换行后每 2 char 为一组, 转换 Hex [ByteArray].
*
* 这个方法很累, 不建议经常使用.
*/
fun String.autoHexToBytes(): ByteArray =
this.replace("\n", "").replace(" ", "").asSequence().chunked(2).map { (it[0].toString() + it[1]).toUByte(16).toByte() }.toList().toByteArray()
/**
* 将无符号 Hex 转为 [UByteArray], 有根据 hex [hashCode] 建立的缓存.
*
* 这个方法很累, 不建议经常使用.
*/
fun String.hexToUBytes(): UByteArray =
this.split(" ")