Add debug functions

This commit is contained in:
Him188 2019-11-25 13:59:01 +08:00
parent 9145eeb4b3
commit ef7dd73dc0
2 changed files with 28 additions and 24 deletions

View File

@ -6,10 +6,7 @@ import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.readBytes import kotlinx.io.core.readBytes
import kotlinx.io.core.readUInt import kotlinx.io.core.readUInt
import kotlinx.io.core.readULong import kotlinx.io.core.readULong
import net.mamoe.mirai.utils.io.UVarInt import net.mamoe.mirai.utils.io.*
import net.mamoe.mirai.utils.io.readUVarInt
import net.mamoe.mirai.utils.io.toReadPacket
import net.mamoe.mirai.utils.io.toUHexString
import kotlin.jvm.JvmStatic import kotlin.jvm.JvmStatic
// ProtoBuf utilities // ProtoBuf utilities
@ -143,6 +140,7 @@ fun ByteReadPacket.readProtoMap(length: Long = this.remaining): ProtoMap {
while (this.remaining != expectingRemaining) { while (this.remaining != expectingRemaining) {
require(this.remaining > expectingRemaining) { "Expecting to read $length bytes, but read ${expectingRemaining + length - this.remaining}" } require(this.remaining > expectingRemaining) { "Expecting to read $length bytes, but read ${expectingRemaining + length - this.remaining}" }
try {
val id = ProtoFieldId(readUVarInt()) val id = ProtoFieldId(readUVarInt())
fun readValue(): Any = when (id.type) { fun readValue(): Any = when (id.type) {
@ -167,6 +165,10 @@ fun ByteReadPacket.readProtoMap(length: Long = this.remaining): ProtoMap {
} else { } else {
map[id] = readValue() map[id] = readValue()
} }
} catch (e: IllegalStateException) {
e.logStacktrace()
return map
}
} }
return map return map
} }

View File

@ -10,6 +10,8 @@ import net.mamoe.mirai.utils.internal.printCompareHex
internal object DebugLogger : MiraiLogger by DefaultLogger("Packet Debug") internal object DebugLogger : MiraiLogger by DefaultLogger("Packet Debug")
internal fun Throwable.logStacktrace(message: String? = null) = DebugLogger.error(message, this)
internal fun debugPrintln(any: Any?) = DebugLogger.debug(any) internal fun debugPrintln(any: Any?) = DebugLogger.debug(any)
@Deprecated("Low efficiency, only for debug purpose", ReplaceWith("this")) @Deprecated("Low efficiency, only for debug purpose", ReplaceWith("this"))