From 392c2fd730df2b9efbe3131479ab7b04713acbd7 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 30 Jan 2020 19:17:22 +0800 Subject: [PATCH] Move files --- .../io/serialization/SerializationHelper.kt | 16 ------------ .../io/serialization/SerializationUtils.kt | 26 ++++++++++++++++++- 2 files changed, 25 insertions(+), 17 deletions(-) delete mode 100644 mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationHelper.kt diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationHelper.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationHelper.kt deleted file mode 100644 index e75bde933..000000000 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationHelper.kt +++ /dev/null @@ -1,16 +0,0 @@ -package net.mamoe.mirai.qqandroid.io.serialization - -import kotlinx.serialization.SerialDescriptor - -/* - * Helper for kotlinx.serialization - */ - -internal inline fun SerialDescriptor.findAnnotation(elementIndex: Int): A? { - val candidates = getElementAnnotations(elementIndex).filterIsInstance() - return when (candidates.size) { - 0 -> null - 1 -> candidates[0] - else -> throw IllegalStateException("There are duplicate annotations of type ${A::class} in the descriptor $this") - } -} diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationUtils.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationUtils.kt index 09e042a5f..bf3daae17 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationUtils.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/SerializationUtils.kt @@ -2,6 +2,7 @@ package net.mamoe.mirai.qqandroid.io.serialization import kotlinx.io.core.* import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.SerialDescriptor import kotlinx.serialization.SerializationStrategy import net.mamoe.mirai.qqandroid.io.JceStruct import net.mamoe.mirai.qqandroid.io.ProtoBuf @@ -91,4 +92,27 @@ fun ByteArray.loadAs(deserializer: DeserializationStrategy): T */ fun Input.readRemainingAsProtoBuf(serializer: DeserializationStrategy): T { return ProtoBufWithNullableSupport.load(serializer, this.readBytes()) -} \ No newline at end of file +} + +/** + * 构造 [RequestPacket] 的 [RequestPacket.sBuffer] + */ +fun jceRequestSBuffer(name: String, serializer: SerializationStrategy, jceStruct: T): ByteArray { + return RequestDataVersion3( + mapOf( + name to JCE_STRUCT_HEAD_OF_TAG_0 + jceStruct.toByteArray(serializer) + JCE_STRUCT_TAIL_OF_TAG_0 + ) + ).toByteArray(RequestDataVersion3.serializer()) +} + +private val JCE_STRUCT_HEAD_OF_TAG_0 = byteArrayOf(0x0A) +private val JCE_STRUCT_TAIL_OF_TAG_0 = byteArrayOf(0x0B) + +internal inline fun SerialDescriptor.findAnnotation(elementIndex: Int): A? { + val candidates = getElementAnnotations(elementIndex).filterIsInstance() + return when (candidates.size) { + 0 -> null + 1 -> candidates[0] + else -> throw IllegalStateException("There are duplicate annotations of type ${A::class} in the descriptor $this") + } +}