mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 04:50:26 +08:00
add: protocol
This commit is contained in:
parent
96e93e670e
commit
4461e45691
mirai-core-api/src/commonMain/kotlin/message/data
mirai-core/src/commonMain
kotlin
message/protocol/impl
network/protocol/data/proto
utils
resources/META-INF/services
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2019-2023 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
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.NotStableForInheritance
|
||||
import net.mamoe.mirai.utils.safeCast
|
||||
|
||||
/**
|
||||
* 动画表情
|
||||
*
|
||||
* @see Face
|
||||
*/
|
||||
@OptIn(MiraiExperimentalApi::class)
|
||||
@Serializable
|
||||
@SerialName(AnimatedSticker.SERIAL_NAME)
|
||||
@NotStableForInheritance
|
||||
public data class AnimatedSticker(
|
||||
public val id: Int
|
||||
) : HummerMessage {
|
||||
|
||||
public companion object Key :
|
||||
AbstractPolymorphicMessageKey<MessageContent, AnimatedSticker>(
|
||||
MessageContent,
|
||||
{ it.safeCast() }) {
|
||||
|
||||
public const val SERIAL_NAME: String = "AnimatedSticker"
|
||||
|
||||
/**
|
||||
* 将普通表情转换为动画.
|
||||
*
|
||||
* @see Image.flash
|
||||
*/
|
||||
@JvmStatic
|
||||
public fun from(face: Face): AnimatedSticker = AnimatedSticker(face.id)
|
||||
}
|
||||
|
||||
override val key: MessageKey<AnimatedSticker> get() = Key
|
||||
|
||||
public val name: String get() = contentToString().let { it.substring(1, it.length - 1) }
|
||||
|
||||
override fun toString(): String = contentToString()
|
||||
|
||||
override fun contentToString(): String = Face.names.getOrElse(id) { "[动画表情]" }
|
||||
}
|
||||
|
||||
/**
|
||||
* 将普通表情转换为动画.
|
||||
*/
|
||||
@JvmSynthetic
|
||||
public inline fun Face.animated(): AnimatedSticker = AnimatedSticker.from(this)
|
@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2019-2023 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
*
|
||||
* https://github.com/mamoe/mirai/blob/dev/LICENSE
|
||||
*/
|
||||
|
||||
package net.mamoe.mirai.internal.message.protocol.impl
|
||||
|
||||
import net.mamoe.mirai.internal.message.protocol.MessageProtocol
|
||||
import net.mamoe.mirai.internal.message.protocol.ProcessorCollector
|
||||
import net.mamoe.mirai.internal.message.protocol.decode.MessageDecoder
|
||||
import net.mamoe.mirai.internal.message.protocol.decode.MessageDecoderContext
|
||||
import net.mamoe.mirai.internal.message.protocol.encode.MessageEncoder
|
||||
import net.mamoe.mirai.internal.message.protocol.encode.MessageEncoderContext
|
||||
import net.mamoe.mirai.internal.message.protocol.serialization.MessageSerializer
|
||||
import net.mamoe.mirai.internal.network.protocol.data.proto.HummerCommelem
|
||||
import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.loadAs
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.toByteArray
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import kotlin.text.toByteArray
|
||||
|
||||
internal class AnimatedStickerProtocol : MessageProtocol() {
|
||||
override fun ProcessorCollector.collectProcessorsImpl() {
|
||||
add(Decoder())
|
||||
add(Encoder())
|
||||
|
||||
MessageSerializer.superclassesScope(MessageContent::class, SingleMessage::class) {
|
||||
add(MessageSerializer(AnimatedSticker::class, AnimatedSticker.serializer()))
|
||||
}
|
||||
}
|
||||
|
||||
private class Decoder : MessageDecoder {
|
||||
override suspend fun MessageDecoderContext.process(data: ImMsgBody.Elem) {
|
||||
if (data.commonElem == null) return
|
||||
if (data.commonElem.serviceType != 37) return
|
||||
|
||||
markAsConsumed()
|
||||
|
||||
val proto = data.commonElem.pbElem.loadAs(HummerCommelem.MsgElemInfoServtype37.serializer())
|
||||
collect(AnimatedSticker(proto.qsId))
|
||||
}
|
||||
}
|
||||
|
||||
private class Encoder : MessageEncoder<AnimatedSticker> {
|
||||
override suspend fun MessageEncoderContext.process(data: AnimatedSticker) {
|
||||
markAsConsumed()
|
||||
|
||||
val businessType = when (data.id) {
|
||||
114 -> 2
|
||||
else -> 1
|
||||
}
|
||||
|
||||
val pbElem = HummerCommelem.MsgElemInfoServtype37(
|
||||
packId = "1".encodeToByteArray(),
|
||||
stickerId = byteArrayOf(), // TODO
|
||||
qsId = data.id,
|
||||
sourceType = 1,
|
||||
stickerType = businessType,
|
||||
text = "/${data.name}".toByteArray(),
|
||||
randomType = 1
|
||||
).toByteArray(HummerCommelem.MsgElemInfoServtype37.serializer())
|
||||
|
||||
val commonElem = ImMsgBody.CommonElem(
|
||||
serviceType = 37,
|
||||
pbElem = pbElem,
|
||||
businessType = businessType
|
||||
)
|
||||
|
||||
collect(ImMsgBody.Elem(commonElem = commonElem))
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
* Copyright 2019-2023 Mamoe Technologies and contributors.
|
||||
*
|
||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
@ -242,6 +242,19 @@ internal class HummerCommelem : ProtoBuf {
|
||||
@ProtoNumber(4) @JvmField val buf: ByteArray = EMPTY_BYTE_ARRAY
|
||||
) : ProtoBuf
|
||||
|
||||
@Serializable
|
||||
internal class MsgElemInfoServtype37(
|
||||
@ProtoNumber(1) @JvmField val packId: ByteArray = EMPTY_BYTE_ARRAY,
|
||||
@ProtoNumber(2) @JvmField val stickerId: ByteArray = EMPTY_BYTE_ARRAY,
|
||||
@ProtoNumber(3) @JvmField val qsId: Int = 0,
|
||||
@ProtoNumber(4) @JvmField val sourceType: Int = 0,
|
||||
@ProtoNumber(5) @JvmField val stickerType: Int = 0,
|
||||
@ProtoNumber(6) @JvmField val resultId: ByteArray = EMPTY_BYTE_ARRAY,
|
||||
@ProtoNumber(6) @JvmField val text: ByteArray = EMPTY_BYTE_ARRAY,
|
||||
@ProtoNumber(7) @JvmField val surpriseId: ByteArray = EMPTY_BYTE_ARRAY,
|
||||
@ProtoNumber(8) @JvmField val randomType: Int = 0
|
||||
) : ProtoBuf
|
||||
|
||||
@Serializable
|
||||
internal class MsgElemInfoServtype4(
|
||||
@ProtoNumber(1) @JvmField val imsgType: Int = 0,
|
||||
|
@ -60,6 +60,10 @@ internal object MiraiCoreServices {
|
||||
msgProtocol,
|
||||
"net.mamoe.mirai.internal.message.protocol.impl.MarketFaceProtocol"
|
||||
) { net.mamoe.mirai.internal.message.protocol.impl.MarketFaceProtocol() }
|
||||
Services.register(
|
||||
msgProtocol,
|
||||
"net.mamoe.mirai.internal.message.protocol.impl.AnimatedStickerProtocol"
|
||||
) { net.mamoe.mirai.internal.message.protocol.impl.AnimatedStickerProtocol() }
|
||||
Services.register(
|
||||
msgProtocol,
|
||||
"net.mamoe.mirai.internal.message.protocol.impl.MusicShareProtocol"
|
||||
|
@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright 2019-2022 Mamoe Technologies and contributors.
|
||||
# Copyright 2019-2023 Mamoe Technologies and contributors.
|
||||
#
|
||||
# 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||
# Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||
@ -15,6 +15,7 @@ net.mamoe.mirai.internal.message.protocol.impl.FlashImageProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.IgnoredMessagesProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.ImageProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.MarketFaceProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.AnimatedStickerProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.MusicShareProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.PokeMessageProtocol
|
||||
net.mamoe.mirai.internal.message.protocol.impl.PttMessageProtocol
|
||||
|
Loading…
Reference in New Issue
Block a user