1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-24 20:43:33 +08:00

fix: SuperFace constructor

This commit is contained in:
cssxsh 2023-07-05 05:27:49 +08:00
parent 0655578b6e
commit 18b4e8cf2a
No known key found for this signature in database
GPG Key ID: 92849F91CA9D8ECE
3 changed files with 70 additions and 63 deletions
mirai-core-api/src/commonMain/kotlin/message/data
mirai-core/src
commonMain/kotlin/message/protocol/impl
commonTest/kotlin/message/protocol/impl

View File

@ -12,8 +12,10 @@ package net.mamoe.mirai.message.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.safeCast
import kotlin.jvm.Throws
/**
* 超级表情
@ -24,8 +26,10 @@ import net.mamoe.mirai.utils.safeCast
@Serializable
@SerialName(SuperFace.SERIAL_NAME)
@NotStableForInheritance
public data class SuperFace(
public val id: Int
public data class SuperFace @MiraiInternalApi constructor(
public val face: Int,
public val id: String,
@MiraiInternalApi public val type: Int
) : HummerMessage {
public companion object Key :
@ -36,12 +40,51 @@ public data class SuperFace(
public const val SERIAL_NAME: String = "SuperFace"
/**
* 将普通表情转换为动画.
*
* @see Face.animated
*/
* 将普通表情转换为超级表情.
**/
@JvmStatic
public fun from(face: Face): SuperFace = SuperFace(face.id)
@OptIn(MiraiInternalApi::class)
@Throws(UnsupportedOperationException::class)
public fun from(face: Face): SuperFace {
val stickerId = when (face.id) {
Face.DA_CALL -> "1"
Face.BIAN_XING -> "2"
Face.KE_DAO_LE -> "3"
Face.ZI_XI_FEN_XI -> "4"
Face.JIA_YOU -> "5"
Face.WO_MEI_SHI -> "6"
Face.CAI_GOU -> "7"
Face.CHONG_BAI -> "8"
Face.BI_XIN -> "9"
Face.QING_ZHU -> "10"
Face.LAO_SE_PI -> "11"
Face.CHI_TANG -> "12"
Face.LAN_QIU -> "13"
Face.JING_XIA -> "14"
Face.SHENG_QI -> "15"
Face.LIU_LEI -> "16"
Face.DAN_GAO -> "17"
Face.BIAN_PAO -> "18"
Face.YAN_HUA -> "19"
Face.WO_XIANG_KAI_LE -> "20"
Face.TIAN_PING -> "21"
Face.HUA_DUO_LIAN -> "22"
Face.RE_HUA_LE -> "23"
Face.DA_ZHAO_HU -> "24"
Face.NI_ZHEN_BANG_BANG -> "25"
Face.SUAN_Q -> "26"
Face.WO_FANG_LE -> "27"
Face.DA_YUAN_ZHONG -> "28"
Face.HONG_BAO_DUO_DUO -> "29"
else -> throw UnsupportedOperationException(face.name)
}
val stickerType = when (face.id) {
Face.LAN_QIU -> 2
else -> 1
}
return SuperFace(face = face.id, id = stickerId, type = stickerType)
}
}
override val key: MessageKey<SuperFace> get() = Key
@ -50,11 +93,14 @@ public data class SuperFace(
override fun toString(): String = contentToString()
override fun contentToString(): String = Face.names.getOrElse(id) { "[超级表情]" }
override fun contentToString(): String = Face.names.getOrElse(face) { "[超级表情]" }
}
/**
* 将普通表情转换为超级表情.
*/
@JvmSynthetic
public inline fun Face.animated(): SuperFace = SuperFace.from(this)
public fun SuperFace.toFace(): Face = Face(id = face)
@JvmSynthetic
public fun SuperFace(face: Face): SuperFace = SuperFace.from(face)
@JvmSynthetic
public fun SuperFace(face: Int): SuperFace = SuperFace.from(face = Face(id = face))

View File

@ -42,7 +42,7 @@ internal class SuperFaceProtocol : MessageProtocol() {
markAsConsumed()
val proto = data.commonElem.pbElem.loadAs(HummerCommelem.MsgElemInfoServtype37.serializer())
collect(SuperFace(proto.qsId))
collect(SuperFace(face = proto.qsId, id = proto.stickerId.decodeToString(), type = proto.stickerType))
}
}
@ -64,62 +64,19 @@ internal class SuperFaceProtocol : MessageProtocol() {
}
companion object {
fun SuperFace.stickerId(): String {
return when (id) {
Face.DA_CALL -> "1"
Face.BIAN_XING -> "2"
Face.KE_DAO_LE -> "3"
Face.ZI_XI_FEN_XI -> "4"
Face.JIA_YOU -> "5"
Face.WO_MEI_SHI -> "6"
Face.CAI_GOU -> "7"
Face.CHONG_BAI -> "8"
Face.BI_XIN -> "9"
Face.QING_ZHU -> "10"
Face.LAO_SE_PI -> "11"
Face.CHI_TANG -> "12"
Face.LAN_QIU -> "13"
Face.JING_XIA -> "14"
Face.SHENG_QI -> "15"
Face.LIU_LEI -> "16"
Face.DAN_GAO -> "17"
Face.BIAN_PAO -> "18"
Face.YAN_HUA -> "19"
Face.WO_XIANG_KAI_LE -> "20"
Face.TIAN_PING -> "21"
Face.HUA_DUO_LIAN -> "22"
Face.RE_HUA_LE -> "23"
Face.DA_ZHAO_HU -> "24"
Face.NI_ZHEN_BANG_BANG -> "25"
Face.SUAN_Q -> "26"
Face.WO_FANG_LE -> "27"
Face.DA_YUAN_ZHONG -> "28"
Face.HONG_BAO_DUO_DUO -> "29"
else -> throw UnsupportedOperationException("stickerId with QSid: $id")
}
}
fun SuperFace.stickerType(): Int {
return when (id) {
Face.LAN_QIU -> 2
else -> 1
}
}
fun SuperFace.toCommData(): ImMsgBody.CommonElem {
return ImMsgBody.CommonElem(
serviceType = 37,
pbElem = HummerCommelem.MsgElemInfoServtype37(
packId = "1".encodeToByteArray(),
stickerId = stickerId().encodeToByteArray(),
qsId = id,
stickerId = id.encodeToByteArray(),
qsId = face,
sourceType = 1,
stickerType = stickerType(),
stickerType = type,
text = "/${name}".toByteArray(),
randomType = 1
).toByteArray(HummerCommelem.MsgElemInfoServtype37.serializer()),
businessType = stickerType()
businessType = type
)
}
}

View File

@ -35,18 +35,22 @@ internal class SuperFaceProtocolTest : AbstractMessageProtocolTest() {
fun `group AnimatedSticker receive from Android client`() {
buildCodingChecks {
elem(
"AA03200825121A0A01311202313618052001280132003A072FE6B581E6B3AA48011801".hexToBytes()
"AA 03 20 08 25 12 1A 0A 01 31 12 02 31 36 18 05 20 01 28 01 32 00 3A 07 2F E6 B5 81 E6 B3 AA 48 01 18 01".hexToBytes()
.loadAs(net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody.Elem.serializer())
)
elem(
"AA 03 23 08 25 12 1D 0A 01 31 12 02 31 33 18 72 20 01 28 02 32 01 35 3A 07 2F E7 AF AE E7 90 83 42 00 48 01 18 02".hexToBytes()
.loadAs(net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody.Elem.serializer())
)
message(
SuperFace(id = Face.LIU_LEI)
SuperFace(face = Face.LAN_QIU)
)
}.doDecoderChecks()
}
@TestFactory
fun `test serialization`(): DynamicTestsResult {
val data = SuperFace(0)
val data = SuperFace(face = Face.LAN_QIU)
val serialName = SuperFace.SERIAL_NAME
return runDynamicTests(
testPolymorphicInMessageContent(data, serialName),