diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt index 35047be7d..b63bbaf42 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/io/serialization/jce/JceDecoder.kt @@ -36,12 +36,14 @@ internal class JceDecoder( ) } - fun SerialDescriptor.getJceTagId(index: Int): Int { + private fun SerialDescriptor.getJceTagId(index: Int): Int { return getElementAnnotations(index).filterIsInstance().single().id } - private val ByteArraySerializer = ByteArraySerializer() + companion object { + private val ByteArraySerializer: KSerializer = ByteArraySerializer() + } // TODO: 2020/3/6 can be object private inner class SimpleByteArrayReader : CompositeDecoder by this { @@ -64,16 +66,14 @@ internal class JceDecoder( // TODO: 2020/3/6 can be object private inner class ListReader : CompositeDecoder by this { override fun decodeSequentially(): Boolean = true - override fun decodeElementIndex(descriptor: SerialDescriptor): Int { - error("should not be reached") - } - + override fun decodeElementIndex(descriptor: SerialDescriptor): Int = error("should not be reached") override fun endStructure(descriptor: SerialDescriptor) { println("endStructure: ${descriptor.serialName}") } override fun decodeCollectionSize(descriptor: SerialDescriptor): Int { - return jce.useHead { jce.readJceIntValue(it) } + // 不读下一个 head + return jce.currentHead.let { jce.readJceIntValue(it) }.also { println("listSize=$it") } } } @@ -86,24 +86,23 @@ internal class JceDecoder( override fun beginStructure(descriptor: SerialDescriptor, vararg typeParams: KSerializer<*>): CompositeDecoder { println("beginStructure: ${descriptor.serialName}") - if (descriptor == ByteArraySerializer.descriptor) { - println("!! ByteArray") - println("decoderTag: $currentTagOrNull") - println("jceHead: " + jce.currentHeadOrNull) - return jce.skipToHeadAndUseIfPossibleOrFail(popTag().id) { - println("listHead: $it") - when (it.type) { - Jce.SIMPLE_LIST -> SimpleByteArrayReader().also { jce.prepareNextHead() } // 无用的元素类型 - Jce.LIST -> error(""); //ListReader() - else -> error("type mismatch. Expected SIMPLE_LIST or LIST, got ${it.type} instead") - } - } - } return when (descriptor.kind) { StructureKind.MAP -> { error("map") } - StructureKind.LIST -> ListReader() + StructureKind.LIST -> { + println("!! ByteArray") + println("decoderTag: $currentTagOrNull") + println("jceHead: " + jce.currentHeadOrNull) + return jce.skipToHeadAndUseIfPossibleOrFail(popTag().id) { + println("listHead: $it") + when (it.type) { + Jce.SIMPLE_LIST -> SimpleByteArrayReader().also { jce.prepareNextHead() } // 无用的元素类型 + Jce.LIST -> ListReader() + else -> error("type mismatch. Expected SIMPLE_LIST or LIST, got ${it.type} instead") + } + } + } else -> this@JceDecoder } diff --git a/mirai-core-qqandroid/src/commonTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt b/mirai-core-qqandroid/src/commonTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt index 63b7d54f7..eed7b65e4 100644 --- a/mirai-core-qqandroid/src/commonTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt +++ b/mirai-core-qqandroid/src/commonTest/kotlin/net.mamoe.mirai.qqandroid.io.serialization/JceInputTest.kt @@ -45,7 +45,7 @@ internal class JceInputTest { @Serializable data class TestSerializableClassA( @JceId(0) val byteArray: ByteArray = byteArrayOf(1, 2, 3), - @JceId(3) val byteArray2: ByteArray = byteArrayOf(1, 2, 3, 4) + @JceId(3) val byteArray2: List = listOf(1, 2, 3, 4) ) { override fun equals(other: Any?): Boolean { if (this === other) return true @@ -54,14 +54,13 @@ internal class JceInputTest { other as TestSerializableClassA if (!byteArray.contentEquals(other.byteArray)) return false - if (!byteArray2.contentEquals(other.byteArray2)) return false + if (byteArray2 != other.byteArray2) return false return true } - override fun hashCode(): Int { var result = byteArray.contentHashCode() - result = 31 * result + byteArray2.contentHashCode() + result = 31 * result + byteArray2.hashCode() return result } }