From 3955546868d0ddb9535dba64094151060e37fdc2 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 14 Jan 2022 19:40:01 +0000 Subject: [PATCH] Add `MessageVisitor`, `MessageVisitorEx`, `Message.accept` and `Message.acceptChildren` as internal API --- .../android/api/android.api | 3 + .../compatibility-validation/jvm/api/jvm.api | 3 + .../src/commonMain/kotlin/message/data/At.kt | 15 +- .../commonMain/kotlin/message/data/AtAll.kt | 15 +- .../commonMain/kotlin/message/data/Audio.kt | 6 + .../kotlin/message/data/CustomMessage.kt | 14 +- .../commonMain/kotlin/message/data/Dice.kt | 15 +- .../commonMain/kotlin/message/data/Face.kt | 15 +- .../kotlin/message/data/FileMessage.kt | 8 +- .../kotlin/message/data/FlashImage.kt | 15 +- .../kotlin/message/data/ForwardMessage.kt | 21 +-- .../kotlin/message/data/HummerMessage.kt | 9 +- .../commonMain/kotlin/message/data/Image.kt | 6 + .../kotlin/message/data/MarketFace.kt | 9 +- .../commonMain/kotlin/message/data/Message.kt | 25 ++- .../kotlin/message/data/MessageChain.kt | 17 +- .../kotlin/message/data/MessageOrigin.kt | 4 + .../kotlin/message/data/MessageSource.kt | 8 +- .../kotlin/message/data/MusicShare.kt | 5 + .../kotlin/message/data/PlainText.kt | 15 +- .../kotlin/message/data/PokeMessage.kt | 14 +- .../kotlin/message/data/QuoteReply.kt | 15 +- .../kotlin/message/data/RichMessage.kt | 29 ++- .../kotlin/message/data/ShowImageFlag.kt | 15 +- .../kotlin/message/data/SingleMessage.kt | 22 ++- .../kotlin/message/data/UnsupportedMessage.kt | 8 +- .../commonMain/kotlin/message/data/VipFace.kt | 14 +- .../commonMain/kotlin/message/data/Voice.kt | 6 + .../message/data/visitor/MessageVisitor.kt | 168 ++++++++++++++++++ .../kotlin/message/ForceAsLongMessage.kt | 36 +++- .../kotlin/message/LongMessageInternal.kt | 21 ++- .../kotlin/message/MarketFaceImpl.kt | 18 +- .../kotlin/message/MessageSourceInternal.kt | 9 +- .../kotlin/message/incomingSourceImpl.kt | 25 ++- .../kotlin/message/offlineSourceImpl.kt | 13 +- .../kotlin/message/outgoingSourceImpl.kt | 17 ++ .../message/visitor/MessageVisitorEx.kt | 63 +++++++ 37 files changed, 624 insertions(+), 97 deletions(-) create mode 100644 mirai-core-api/src/commonMain/kotlin/message/data/visitor/MessageVisitor.kt create mode 100644 mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt diff --git a/mirai-core-api/compatibility-validation/android/api/android.api b/mirai-core-api/compatibility-validation/android/api/android.api index c56d421d3..d91ff9c99 100644 --- a/mirai-core-api/compatibility-validation/android/api/android.api +++ b/mirai-core-api/compatibility-validation/android/api/android.api @@ -5629,6 +5629,9 @@ public final class net/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder { public static synthetic fun title$default (Lnet/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)V } +public final class net/mamoe/mirai/message/data/visitor/MessageVisitorKt { +} + public abstract class net/mamoe/mirai/network/CustomLoginFailedException : net/mamoe/mirai/network/LoginFailedException { public fun (Z)V public fun (ZLjava/lang/String;)V diff --git a/mirai-core-api/compatibility-validation/jvm/api/jvm.api b/mirai-core-api/compatibility-validation/jvm/api/jvm.api index d620955df..4c79993ac 100644 --- a/mirai-core-api/compatibility-validation/jvm/api/jvm.api +++ b/mirai-core-api/compatibility-validation/jvm/api/jvm.api @@ -5629,6 +5629,9 @@ public final class net/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder { public static synthetic fun title$default (Lnet/mamoe/mirai/message/data/XmlMessageBuilder$ItemBuilder;Ljava/lang/String;ILjava/lang/String;ILjava/lang/Object;)V } +public final class net/mamoe/mirai/message/data/visitor/MessageVisitorKt { +} + public abstract class net/mamoe/mirai/network/CustomLoginFailedException : net/mamoe/mirai/network/LoginFailedException { public fun (Z)V public fun (ZLjava/lang/String;)V diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/At.kt b/mirai-core-api/src/commonMain/kotlin/message/data/At.kt index 66e536782..31e4c7a3d 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/At.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/At.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -21,7 +21,9 @@ import net.mamoe.mirai.contact.Member import net.mamoe.mirai.contact.UserOrBot import net.mamoe.mirai.contact.nameCardOrNick import net.mamoe.mirai.message.code.CodableMessage +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi /** @@ -68,6 +70,11 @@ public data class At( } return super.followedBy(PlainText(" ")) + tail } + + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitAt(this, data) + } } /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt b/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt index 6b0b51267..369d7da04 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -16,7 +16,9 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi /** * "@全体成员". @@ -68,4 +70,9 @@ public object AtAll : } return super.followedBy(PlainText(" ")) + tail } + + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitAtAll(this, data) + } } \ No newline at end of file diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Audio.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Audio.kt index 9fe747afd..c9aa7dc3b 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Audio.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Audio.kt @@ -20,6 +20,7 @@ import net.mamoe.mirai.contact.AudioSupported import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.message.MessageSerializers import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.* import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -101,6 +102,11 @@ public sealed interface Audio : MessageContent { */ public override fun toString(): String public override fun contentToString(): String = "[语音消息]" + + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitAudio(this, data) + } } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt index 85a678d7b..645af1b26 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/CustomMessage.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:OptIn(MiraiInternalApi::class) @@ -18,6 +18,7 @@ import kotlinx.serialization.json.Json import kotlinx.serialization.protobuf.ProtoBuf import kotlinx.serialization.protobuf.ProtoNumber import net.mamoe.mirai.message.MessageSerializers +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiInternalApi import java.util.concurrent.ConcurrentLinkedQueue @@ -205,6 +206,11 @@ public abstract class CustomMessageMetadata : CustomMessage(), MessageMetadata { final override fun toString(): String = "[mirai:custom:${getFactory().typeName}:${String(customToString())}]" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitCustomMessageMetadata(this, data) + } + public companion object } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Dice.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Dice.kt index 0bc286556..a0ad2d3fb 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Dice.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Dice.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("NOTHING_TO_INLINE") @@ -16,7 +16,9 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast import org.jetbrains.annotations.Range import kotlin.random.Random @@ -55,6 +57,11 @@ public data class Dice( override fun toString(): String = "[mirai:dice:$value]" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitDice(this, data) + } + public companion object Key : AbstractPolymorphicMessageKey(MarketFace, { it.safeCast() }) { public const val SERIAL_NAME: String = "Dice" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt index 6b2c15de3..f07a53c7a 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Face.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -16,7 +16,9 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi /** * QQ 自带表情 @@ -44,6 +46,11 @@ public data class Face(public val id: Int) : // used in delegation override fun equals(other: Any?): Boolean = other is Face && other.id == this.id override fun hashCode(): Int = id + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitFace(this, data) + } + //Auto generated @Suppress("NonAsciiCharacters", "unused", "SpellCheckingInspection", "all", "ObjectPropertyName") public companion object { diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt index 85a5a7122..a00b84777 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/FileMessage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -23,6 +23,7 @@ import net.mamoe.mirai.contact.file.AbsoluteFile import net.mamoe.mirai.event.events.MessageEvent import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.* /** @@ -95,6 +96,11 @@ public interface FileMessage : MessageContent, ConstrainSingle, CodableMessage { override val key: Key get() = Key + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitFileMessage(this, data) + } + /** * 注意, baseKey [MessageContent] 不稳定. 未来可能会有变更. */ diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/FlashImage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/FlashImage.kt index c379aee05..cba5303fb 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/FlashImage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/FlashImage.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("NOTHING_TO_INLINE") @@ -14,7 +14,9 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast /** @@ -60,6 +62,11 @@ public data class FlashImage( override fun toString(): String = stringValue override fun contentToString(): String = "[闪照]" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitFlashImage(this, data) + } + public companion object Key : AbstractPolymorphicMessageKey(HummerMessage, { it.safeCast() }) { public const val SERIAL_NAME: String = "FlashImage" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt index d802961ca..5c367a7e0 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt @@ -1,28 +1,24 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("MemberVisibilityCanBePrivate", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "unused") package net.mamoe.mirai.message.data -import io.ktor.http.* -import io.ktor.util.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.Bot import net.mamoe.mirai.contact.* import net.mamoe.mirai.event.events.MessageEvent import net.mamoe.mirai.message.data.ForwardMessage.DisplayStrategy -import net.mamoe.mirai.utils.MiraiExperimentalApi -import net.mamoe.mirai.utils.currentTimeSeconds -import net.mamoe.mirai.utils.safeCast -import net.mamoe.mirai.utils.toLongUnsigned +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.* /** @@ -118,6 +114,11 @@ public data class ForwardMessage( override fun contentToString(): String = "[转发消息]" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitForwardMessage(this, data) + } + // use data-class generated toString() diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt index bd76a4c1e..7b1d24579 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/HummerMessage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -13,7 +13,9 @@ package net.mamoe.mirai.message.data +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.NotStableForInheritance import net.mamoe.mirai.utils.castOrNull @@ -30,6 +32,11 @@ import net.mamoe.mirai.utils.castOrNull @MiraiExperimentalApi @NotStableForInheritance public interface HummerMessage : MessageContent, ConstrainSingle { + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitHummerMessage(this, data) + } + public companion object Key : AbstractPolymorphicMessageKey(MessageContent, { it.castOrNull() }) // has service type etc. diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt index 12056f858..db0921dda 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt @@ -38,6 +38,7 @@ import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_1 import net.mamoe.mirai.message.data.Image.Key.IMAGE_RESOURCE_ID_REGEX_2 import net.mamoe.mirai.message.data.Image.Key.isUploaded import net.mamoe.mirai.message.data.Image.Key.queryUrl +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage @@ -166,6 +167,11 @@ public interface Image : Message, MessageContent, CodableMessage { */ // was an extension on Image before 2.9.0-M1. public val md5: ByteArray get() = calculateImageMd5ByImageId(imageId) + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitImage(this, data) + } + public object AsStringSerializer : KSerializer by String.serializer().mapPrimitive( SERIAL_NAME, serialize = { imageId }, diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MarketFace.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MarketFace.kt index 61ce84f3e..43c2a79f0 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MarketFace.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MarketFace.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -9,7 +9,9 @@ package net.mamoe.mirai.message.data +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.NotStableForInheritance import net.mamoe.mirai.utils.safeCast @@ -37,6 +39,11 @@ public interface MarketFace : HummerMessage { override fun contentToString(): String = name.ifEmpty { "[商城表情]" } + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitMarketFace(this, data) + } + public companion object Key : AbstractPolymorphicMessageKey(HummerMessage, { it.safeCast() }) { public const val SERIAL_NAME: String = "MarketFace" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt index a6c8946a7..d3fa27a5d 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Message.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress( @@ -25,6 +25,8 @@ import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.code.MiraiCode import net.mamoe.mirai.message.code.MiraiCode.serializeToMiraiCode import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.MiraiInternalApi import kotlin.internal.LowPriorityInOverloadResolution /** @@ -273,6 +275,21 @@ public interface Message { public operator fun plus(another: Sequence): MessageChain = another.fold(this, Message::plus).toMessageChain() + /** + * @suppress 这是内部 API, 不要在任何情况下调用 + * @since MESSAGE_VISITOR + */ + @MiraiInternalApi + public fun accept(visitor: MessageVisitor, data: D): R = visitor.visitMessage(this, data) + + /** + * @suppress 这是内部 API, 不要在任何情况下调用 + * @since MESSAGE_VISITOR + */ + @MiraiInternalApi + public fun acceptChildren(visitor: MessageVisitor, data: D) { + } + public companion object } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt index ee5b2c38c..60848850c 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageChain.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -14,7 +14,6 @@ package net.mamoe.mirai.message.data import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.collect import kotlinx.serialization.* import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.descriptors.SerialDescriptor @@ -33,10 +32,8 @@ import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.recall import net.mamoe.mirai.message.data.MessageSource.Key.recallIn -import net.mamoe.mirai.utils.DeprecatedSinceMirai -import net.mamoe.mirai.utils.MiraiExperimentalApi -import net.mamoe.mirai.utils.NotStableForInheritance -import net.mamoe.mirai.utils.safeCast +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.* import java.util.stream.Stream import kotlin.reflect.KProperty import kotlin.streams.asSequence @@ -253,6 +250,14 @@ public sealed interface MessageChain : forEach { it.safeCast()?.appendMiraiCodeTo(builder) } } + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R = visitor.visitMessageChain(this, data) + + @MiraiInternalApi + override fun acceptChildren(visitor: MessageVisitor, data: D) { + forEach { it.accept(visitor, data) } + } + /** * 将 [MessageChain] 作为 `List` 序列化. 使用 [多态序列化][Polymorphic]. * diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageOrigin.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageOrigin.kt index cc0d450b0..1ebd0533f 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageOrigin.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageOrigin.kt @@ -15,7 +15,9 @@ import kotlinx.serialization.Polymorphic import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.IMirai +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast /** @@ -87,6 +89,8 @@ public class MessageOrigin( return result } + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R = visitor.visitMessageOrigin(this, data) public companion object Key : AbstractMessageKey({ it.safeCast() }) { public const val SERIAL_NAME: String = "MessageOrigin" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt index 3975292fc..d30f6d0de 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MessageSource.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -28,6 +28,7 @@ import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.action.AsyncRecallResult import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.recall +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.NotStableForInheritance import net.mamoe.mirai.utils.safeCast @@ -185,6 +186,11 @@ public sealed class MessageSource : Message, MessageMetadata, ConstrainSingle { public final override fun toString(): String = "[mirai:source:${ids.contentToString()},${internalIds.contentToString()}]" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitMessageSource(this, data) + } + public object Serializer : MessageSourceSerializerImpl("MessageSource") public companion object Key : AbstractMessageKey({ it.safeCast() }) { diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/MusicShare.kt b/mirai-core-api/src/commonMain/kotlin/message/data/MusicShare.kt index 245e520f2..7944da786 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/MusicShare.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/MusicShare.kt @@ -15,6 +15,7 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast @@ -129,6 +130,10 @@ public data class MusicShare( .append(']') } + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitMusicShare(this, data) + } /** * 注意, baseKey [MessageContent] 不稳定. 未来可能会有变更. diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt b/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt index 87a10ca86..de68a1aeb 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/PlainText.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -17,7 +17,9 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi +import net.mamoe.mirai.utils.MiraiInternalApi /** * 纯文本. @@ -48,6 +50,11 @@ public data class PlainText( builder.appendStringAsMiraiCode(content) } + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitPlainText(this, data) + } + public companion object { public const val SERIAL_NAME: String = "PlainText" } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/PokeMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/PokeMessage.kt index 2e3e1315e..dca31fdfd 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/PokeMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/PokeMessage.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.message.data @@ -14,6 +14,7 @@ import kotlinx.serialization.Serializable import net.mamoe.mirai.message.action.Nudge import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.castOrNull @@ -57,6 +58,11 @@ public data class PokeMessage @MiraiInternalApi constructor( //serviceType=0x00000002(2) + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitPokeMessage(this, data) + } + public companion object Key : AbstractPolymorphicMessageKey(HummerMessage, { it.castOrNull() }) { diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt b/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt index 71b03918b..4e23cf027 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/QuoteReply.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:JvmMultifileClass @@ -17,6 +17,8 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.data.MessageSource.Key.quote import net.mamoe.mirai.message.data.MessageSource.Key.recall +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast @@ -57,6 +59,11 @@ public data class QuoteReply( public override fun equals(other: Any?): Boolean = other is QuoteReply && other.source == this.source public override fun hashCode(): Int = source.hashCode() + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitQuoteReply(this, data) + } + public companion object Key : AbstractMessageKey({ it.safeCast() }) { public const val SERIAL_NAME: String = "QuoteReply" } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt index 359f76202..63012a6f9 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/RichMessage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -17,7 +17,9 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.internal.appendStringAsMiraiCode +import net.mamoe.mirai.message.data.visitor.MessageVisitor 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.annotation.AnnotationTarget.* @@ -48,6 +50,11 @@ public interface RichMessage : MessageContent, ConstrainSingle { */ public val content: String + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitRichMessage(this, data) + } + /** * 一些模板 * @suppress 此 API 不稳定, 可能在任意时刻被删除 @@ -110,6 +117,11 @@ public data class LightApp(override val content: String) : RichMessage, CodableM public override fun toString(): String = "[mirai:app:$content]" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitLightApp(this, data) + } + @MiraiExperimentalApi override fun appendMiraiCodeTo(builder: StringBuilder) { builder.append("[mirai:app:").appendStringAsMiraiCode(content).append(']') @@ -148,6 +160,11 @@ public class SimpleServiceMessage( return result } + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitSimpleServiceMessage(this, data) + } + public companion object { public const val SERIAL_NAME: String = "SimpleServiceMessage" } @@ -175,6 +192,11 @@ public interface ServiceMessage : RichMessage, CodableMessage { */ public val serviceId: Int + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitServiceMessage(this, data) + } + @MiraiExperimentalApi override fun appendMiraiCodeTo(builder: StringBuilder) { builder.append("[mirai:service:").append(serviceId).append(',').appendStringAsMiraiCode(content).append(']') @@ -185,6 +207,11 @@ public interface ServiceMessage : RichMessage, CodableMessage { @Serializable public abstract class AbstractServiceMessage : ServiceMessage { public override fun toString(): String = "[mirai:service:$serviceId,$content]" + + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitAbstractServiceMessage(this, data) + } } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/ShowImageFlag.kt b/mirai-core-api/src/commonMain/kotlin/message/data/ShowImageFlag.kt index 88e39e2ee..9f6d729c1 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/ShowImageFlag.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/ShowImageFlag.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.message.data @@ -18,6 +18,8 @@ import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.decodeStructure import kotlinx.serialization.encoding.encodeStructure +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast /** @@ -45,6 +47,11 @@ public object ShowImageFlag : MessageMetadata, ConstrainSingle, AbstractMessageK override fun toString(): String = "ShowImageFlag" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitShowImageFlag(this, data) + } + /** * @since 2.4 */ diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt index 4e37fbc19..da96631d1 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/SingleMessage.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress( @@ -19,7 +19,9 @@ package net.mamoe.mirai.message.data import kotlinx.serialization.KSerializer import kotlinx.serialization.PolymorphicSerializer +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.DeprecatedSinceMirai +import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast /** @@ -27,6 +29,8 @@ import net.mamoe.mirai.utils.safeCast */ // @Serializable(SingleMessage.Serializer::class) public interface SingleMessage : Message { + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R = visitor.visitSingleMessage(this, data) /** * @suppress deprecated since 2.4.0 @@ -66,6 +70,11 @@ public interface MessageMetadata : SingleMessage { * 返回空字符串 */ override fun contentToString(): String = "" + + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitMessageMetadata(this, data) + } } /** @@ -85,6 +94,11 @@ public interface MessageMetadata : SingleMessage { * @see MusicShare 音乐分享 */ public interface MessageContent : SingleMessage { + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitMessageContent(this, data) + } + public companion object Key : AbstractMessageKey({ it.safeCast() }) } diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt b/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt index 4d107998a..ac9ada537 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/UnsupportedMessage.kt @@ -1,5 +1,5 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. @@ -19,6 +19,7 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.IMirai import net.mamoe.mirai.Mirai +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.* /** @@ -39,6 +40,11 @@ public interface UnsupportedMessage : MessageContent { */ public val struct: ByteArray + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitUnsupportedMessage(this, data) + } + public companion object { public const val SERIAL_NAME: String = "UnsupportedMessage" diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/VipFace.kt b/mirai-core-api/src/commonMain/kotlin/message/data/VipFace.kt index 8ee00b8bc..b07d2b357 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/VipFace.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/VipFace.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("NOTHING_TO_INLINE") @@ -15,6 +15,7 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.data.VipFace.Kind +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiInternalApi import net.mamoe.mirai.utils.safeCast @@ -48,6 +49,11 @@ public data class VipFace @MiraiInternalApi constructor( override fun toString(): String = "[mirai:vipface:$kind,$count]" override fun contentToString(): String = "[${kind.name}]x$count" + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitVipFace(this, data) + } + @Serializable public data class Kind( val id: Int, diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt index 76034c20c..40182bf93 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt @@ -17,6 +17,7 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import net.mamoe.mirai.contact.Group +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.* @@ -176,6 +177,11 @@ public open class Voice @MiraiInternalApi constructor( result = 15 * result + _url.hashCode() return result } + + @MiraiInternalApi + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.visitVoice(this, data) + } } /** diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/visitor/MessageVisitor.kt b/mirai-core-api/src/commonMain/kotlin/message/data/visitor/MessageVisitor.kt new file mode 100644 index 000000000..4e4e0f61f --- /dev/null +++ b/mirai-core-api/src/commonMain/kotlin/message/data/visitor/MessageVisitor.kt @@ -0,0 +1,168 @@ +/* + * Copyright 2019-2022 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.visitor + +import net.mamoe.mirai.message.data.* +import net.mamoe.mirai.utils.MiraiInternalApi + +/** + * @suppress 这是内部 API, 请不要调用 + * @since MESSAGE_VISITOR + */ +@MiraiInternalApi +public interface MessageVisitor { + public fun visitMessage(message: Message, data: D): R + + + public fun visitSingleMessage(message: SingleMessage, data: D): R { + return visitMessage(message, data) + } + + public fun visitMessageChain(messageChain: MessageChain, data: D): R { + return visitMessage(messageChain, data) + } + + + public fun visitMessageContent(message: MessageContent, data: D): R { + return visitSingleMessage(message, data) + } + + public fun visitMessageMetadata(message: MessageMetadata, data: D): R { + return visitSingleMessage(message, data) + } + + + public fun visitMessageOrigin(message: MessageOrigin, data: D): R { + return visitMessageMetadata(message, data) + } + + public fun visitMessageSource(message: MessageSource, data: D): R { + return visitMessageMetadata(message, data) + } + + public fun visitQuoteReply(message: QuoteReply, data: D): R { + return visitMessageMetadata(message, data) + } + + public fun visitCustomMessageMetadata(message: CustomMessageMetadata, data: D): R { + return visitMessageMetadata(message, data) + } + + public fun visitShowImageFlag(message: ShowImageFlag, data: D): R { + return visitMessageMetadata(message, data) + } + + + public fun visitPlainText(message: PlainText, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitAt(message: At, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitAtAll(message: AtAll, data: D): R { + return visitMessageContent(message, data) + } + + @Suppress("DEPRECATION_ERROR") + public fun visitVoice(message: Voice, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitAudio(message: Audio, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitHummerMessage(message: HummerMessage, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitFlashImage(message: FlashImage, data: D): R { + return visitHummerMessage(message, data) + } + + public fun visitPokeMessage(message: PokeMessage, data: D): R { + return visitHummerMessage(message, data) + } + + public fun visitVipFace(message: VipFace, data: D): R { + return visitHummerMessage(message, data) + } + + public fun visitMarketFace(message: MarketFace, data: D): R { + return visitHummerMessage(message, data) + } + + public fun visitDice(message: Dice, data: D): R { + return visitMarketFace(message, data) + } + + public fun visitFace(message: Face, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitFileMessage(message: FileMessage, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitImage(message: Image, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitForwardMessage(message: ForwardMessage, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitMusicShare(message: MusicShare, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitUnsupportedMessage(message: UnsupportedMessage, data: D): R { + return visitMessageContent(message, data) + } + + + public fun visitRichMessage(message: RichMessage, data: D): R { + return visitMessageContent(message, data) + } + + public fun visitServiceMessage(message: ServiceMessage, data: D): R { + return visitRichMessage(message, data) + } + + public fun visitSimpleServiceMessage(message: SimpleServiceMessage, data: D): R { + return visitServiceMessage(message, data) + } + + public fun visitLightApp(message: LightApp, data: D): R { + return visitRichMessage(message, data) + } + + public fun visitAbstractServiceMessage(message: AbstractServiceMessage, data: D): R { + return visitServiceMessage(message, data) + } +} + +/** + * @suppress 这是内部 API, 请不要调用 + * @since 2.11 + */ +@MiraiInternalApi +public interface MessageVisitorUnit : MessageVisitor { + override fun visitMessage(message: Message, data: D): Unit = Unit +} + +/** + * @suppress 这是内部 API, 请不要调用 + * @since 2.11 + */ +@MiraiInternalApi +public fun Message.accept(visitor: MessageVisitor): R = this.accept(visitor, Unit) \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/ForceAsLongMessage.kt b/mirai-core/src/commonMain/kotlin/message/ForceAsLongMessage.kt index d78794359..f97ad84a6 100644 --- a/mirai-core/src/commonMain/kotlin/message/ForceAsLongMessage.kt +++ b/mirai-core/src/commonMain/kotlin/message/ForceAsLongMessage.kt @@ -1,17 +1,22 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("unused") package net.mamoe.mirai.internal.message -import net.mamoe.mirai.message.data.* +import net.mamoe.mirai.internal.message.visitor.ex +import net.mamoe.mirai.message.data.AbstractMessageKey +import net.mamoe.mirai.message.data.ConstrainSingle +import net.mamoe.mirai.message.data.MessageKey +import net.mamoe.mirai.message.data.MessageMetadata +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.safeCast /** @@ -22,6 +27,10 @@ internal object ForceAsLongMessage : MessageMetadata, ConstrainSingle, InternalF override val key: MessageKey get() = this override fun toString(): String = "" + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitForceAsLongMessage(this, data) ?: super.accept(visitor, data) + } } /** @@ -32,6 +41,10 @@ internal object DontAsLongMessage : MessageMetadata, ConstrainSingle, InternalFl override val key: MessageKey get() = this override fun toString(): String = "" + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitDontAsLongMessage(this, data) ?: super.accept(visitor, data) + } } internal object IgnoreLengthCheck : MessageMetadata, ConstrainSingle, InternalFlagOnlyMessage, @@ -39,6 +52,10 @@ internal object IgnoreLengthCheck : MessageMetadata, ConstrainSingle, InternalFl override val key: MessageKey get() = this override fun toString(): String = "" + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitIgnoreLengthCheck(this, data) ?: super.accept(visitor, data) + } } /** @@ -48,9 +65,16 @@ internal object MiraiInternalMessageFlag : MessageMetadata, ConstrainSingle, Int AbstractMessageKey({ it.safeCast() }) { override val key: MessageKey get() = this override fun toString(): String = "" + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitMiraiInternalMessageFlag(this, data) ?: super.accept( + visitor, + data + ) + } } /** * Ignore on transformation */ -internal interface InternalFlagOnlyMessage : SingleMessage \ No newline at end of file +internal sealed interface InternalFlagOnlyMessage : MessageMetadata \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt index d0f6e794e..c9ebb3261 100644 --- a/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt +++ b/mirai-core/src/commonMain/kotlin/message/LongMessageInternal.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ package net.mamoe.mirai.internal.message @@ -13,9 +13,11 @@ import net.mamoe.mirai.Bot import net.mamoe.mirai.Mirai import net.mamoe.mirai.internal.asQQAndroidBot import net.mamoe.mirai.internal.getMiraiImpl +import net.mamoe.mirai.internal.message.visitor.ex import net.mamoe.mirai.internal.network.protocol.data.proto.MsgTransmit import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.data.* +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.safeCast // internal runtime value, not serializable @@ -30,6 +32,10 @@ internal data class LongMessageInternal internal constructor(override val conten return MessageOrigin(SimpleServiceMessage(serviceId, content), resId, MessageOriginKind.LONG) + long } + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitLongMessageInternal(this, data) ?: super.accept(visitor, data) + } + companion object Key : AbstractPolymorphicMessageKey(ServiceMessage, { it.safeCast() }) } @@ -118,6 +124,13 @@ internal data class ForwardMessageInternal( ) } + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitForwardMessageInternal(this, data) ?: super.accept( + visitor, + data + ) + } + companion object Key : AbstractPolymorphicMessageKey(ServiceMessage, { it.safeCast() }) { diff --git a/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt b/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt index 67dc69ee3..e7e1157e1 100644 --- a/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/MarketFaceImpl.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @@ -14,11 +14,13 @@ import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.Transient import net.mamoe.mirai.Bot +import net.mamoe.mirai.internal.message.visitor.ex import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.message.data.Dice import net.mamoe.mirai.message.data.MarketFace import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.MessageChain +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.hexToBytes @SerialName(MarketFace.SERIAL_NAME) @@ -33,6 +35,10 @@ internal data class MarketFaceImpl internal constructor( override val id: Int = delegate.tabId override fun toString() = "[mirai:marketface:$id,$name]" + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitMarketFaceImpl(this, data) ?: super.accept(visitor, data) + } } /** @@ -50,6 +56,10 @@ internal class MarketFaceInternal( } override fun toString(): String = "[mirai:marketface:$id,$name]" + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitMarketFaceInternal(this, data) ?: super.accept(visitor, data) + } } // From https://github.com/mamoe/mirai/issues/1012 diff --git a/mirai-core/src/commonMain/kotlin/message/MessageSourceInternal.kt b/mirai-core/src/commonMain/kotlin/message/MessageSourceInternal.kt index d02b7a354..fc5c6cff7 100644 --- a/mirai-core/src/commonMain/kotlin/message/MessageSourceInternal.kt +++ b/mirai-core/src/commonMain/kotlin/message/MessageSourceInternal.kt @@ -14,16 +14,19 @@ import net.mamoe.mirai.contact.Contact import net.mamoe.mirai.internal.contact.SendMessageHandler import net.mamoe.mirai.internal.message.LightMessageRefiner.dropMiraiInternalFlags import net.mamoe.mirai.internal.message.LightMessageRefiner.refineLight +import net.mamoe.mirai.internal.message.visitor.ex import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.message.MessageReceipt import net.mamoe.mirai.message.data.* +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.cast import java.util.concurrent.atomic.AtomicBoolean /** * All [MessageSource] should implement this interface. */ -internal interface MessageSourceInternal { +internal interface MessageSourceInternal : MessageMetadata { @Transient val sequenceIds: IntArray // ids @@ -38,6 +41,10 @@ internal interface MessageSourceInternal { val isRecalledOrPlanned: AtomicBoolean fun toJceData(): ImMsgBody.SourceMsg + + override fun accept(visitor: MessageVisitor, data: D): R { + return visitor.ex()?.visitMessageSourceInternal(this.cast(), data) ?: super.accept(visitor, data) + } } /** diff --git a/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt index ffca228e6..b57d1856d 100644 --- a/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/incomingSourceImpl.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @file:Suppress("EXPERIMENTAL_API_USAGE", "EXPERIMENTAL_OVERRIDE", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") @@ -30,6 +30,7 @@ import net.mamoe.mirai.internal.utils.structureToString import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageSourceKind import net.mamoe.mirai.message.data.OnlineMessageSource +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY import net.mamoe.mirai.utils.encodeBase64 import net.mamoe.mirai.utils.mapToIntArray @@ -59,6 +60,10 @@ internal class OnlineMessageSourceFromFriendImpl( private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @@ -85,6 +90,10 @@ internal class OnlineMessageSourceFromStrangerImpl( private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } private fun List.toJceDataPrivate(ids: IntArray): ImMsgBody.SourceMsg { @@ -154,6 +163,10 @@ internal class OnlineMessageSourceFromTempImpl( private val jceData: ImMsgBody.SourceMsg by lazy { msg.toJceDataPrivate(internalIds) } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @@ -216,4 +229,8 @@ internal class OnlineMessageSourceFromGroupImpl( override fun toJceData(): ImMsgBody.SourceMsg { return jceData } + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/offlineSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/offlineSourceImpl.kt index 632f67600..5775316f0 100644 --- a/mirai-core/src/commonMain/kotlin/message/offlineSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/offlineSourceImpl.kt @@ -1,10 +1,10 @@ /* - * Copyright 2019-2021 Mamoe Technologies and contributors. + * Copyright 2019-2022 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. + * 此源代码的使用受 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/master/LICENSE + * https://github.com/mamoe/mirai/blob/dev/LICENSE */ @@ -20,6 +20,7 @@ import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageSourceKind import net.mamoe.mirai.message.data.OfflineMessageSource +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.EMPTY_BYTE_ARRAY import net.mamoe.mirai.utils.mapToIntArray import java.util.concurrent.atomic.AtomicBoolean @@ -105,6 +106,10 @@ internal class OfflineMessageSourceImplData( result = 31 * result + internalIds.contentHashCode() return result } + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } internal fun OfflineMessageSourceImplData( diff --git a/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt b/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt index a01b29f4d..bed5f7734 100644 --- a/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/outgoingSourceImpl.kt @@ -27,6 +27,7 @@ import net.mamoe.mirai.internal.utils.io.serialization.toByteArray import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageSource import net.mamoe.mirai.message.data.OnlineMessageSource +import net.mamoe.mirai.message.data.visitor.MessageVisitor import net.mamoe.mirai.utils.toLongUnsigned import java.util.concurrent.atomic.AtomicBoolean @@ -92,6 +93,10 @@ internal class OnlineMessageSourceToFriendImpl( override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false) private val jceData: ImMsgBody.SourceMsg by lazy { toJceDataImpl(subject) } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @@ -119,6 +124,10 @@ internal class OnlineMessageSourceToStrangerImpl( override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false) private val jceData: ImMsgBody.SourceMsg by lazy { toJceDataImpl(subject) } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @@ -145,6 +154,10 @@ internal class OnlineMessageSourceToTempImpl( override var isRecalledOrPlanned: AtomicBoolean = AtomicBoolean(false) private val jceData: ImMsgBody.SourceMsg by lazy { toJceDataImpl(subject) } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @@ -236,4 +249,8 @@ internal class OnlineMessageSourceToGroupImpl( } override fun toJceData(): ImMsgBody.SourceMsg = jceData + + override fun accept(visitor: MessageVisitor, data: D): R { + return super.accept(visitor, data) + } } \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt b/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt new file mode 100644 index 000000000..76d54b5f7 --- /dev/null +++ b/mirai-core/src/commonMain/kotlin/message/visitor/MessageVisitorEx.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2019-2022 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.visitor + +import net.mamoe.mirai.internal.message.* +import net.mamoe.mirai.message.data.MessageSource +import net.mamoe.mirai.message.data.visitor.MessageVisitor +import net.mamoe.mirai.utils.castOrNull + +internal fun MessageVisitor?.ex(): MessageVisitorEx? = castOrNull() + +/** + * For mirai-core-specific types + */ +internal interface MessageVisitorEx : MessageVisitor { + fun visitMessageSourceInternal(message: T, data: D): R where T : MessageSourceInternal, T : MessageSource { + return visitMessageSource(message, data) + } + + fun visitForwardMessageInternal(message: ForwardMessageInternal, data: D): R { + return visitAbstractServiceMessage(message, data) + } + + fun visitLongMessageInternal(message: LongMessageInternal, data: D): R { + return visitAbstractServiceMessage(message, data) + } + + fun visitMarketFaceInternal(message: MarketFaceInternal, data: D): R { + return visitMarketFace(message, data) + } + + fun visitMarketFaceImpl(message: MarketFaceImpl, data: D): R { + return visitMarketFace(message, data) + } + + fun visitInternalFlagOnlyMessage(message: InternalFlagOnlyMessage, data: D): R { + return visitMessageMetadata(message, data) + } + + fun visitForceAsLongMessage(message: ForceAsLongMessage, data: D): R { + return visitInternalFlagOnlyMessage(message, data) + } + + fun visitDontAsLongMessage(message: DontAsLongMessage, data: D): R { + return visitInternalFlagOnlyMessage(message, data) + } + + fun visitIgnoreLengthCheck(message: IgnoreLengthCheck, data: D): R { + return visitInternalFlagOnlyMessage(message, data) + } + + fun visitMiraiInternalMessageFlag(message: MiraiInternalMessageFlag, data: D): R { + return visitInternalFlagOnlyMessage(message, data) + } +} \ No newline at end of file