Rename toMiraiCode to serializeToMiraiCode;

Rename parseFromMiraiCode to deserializeFromMiraiCode
This commit is contained in:
Him188 2021-01-11 22:15:06 +08:00
parent 78bc30ae5d
commit b41a742ece
8 changed files with 71 additions and 64 deletions

View File

@ -38,11 +38,6 @@
Mirai 支持富文本消息。各类型消息元素如下文表格所示。 Mirai 支持富文本消息。各类型消息元素如下文表格所示。
消息拥有三种转换到字符串的表示方式。
- `toMiraiCode()`: 消息的一种序列化方式,格式为 `[mirai:TYPE:PROP]`,其中 `TYPE` 为消息类型, `PROP` 为属性。
- `contentToSting()`: QQ 对话框中以纯文本方式会显示的消息内容。无法用纯文字表示的消息会丢失信息,如图片总是 `[图片]`
- `toString()`: Java 对象的 `toString()`,会尽可能包含多的信息用于调试作用,**行为可能不确定**。
[`PlainText`]: ../mirai-core-api/src/commonMain/kotlin/message/data/At.kt [`PlainText`]: ../mirai-core-api/src/commonMain/kotlin/message/data/At.kt
[`At`]: ../mirai-core-api/src/commonMain/kotlin/message/data/At.kt [`At`]: ../mirai-core-api/src/commonMain/kotlin/message/data/At.kt
[`AtAll`]: ../mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt [`AtAll`]: ../mirai-core-api/src/commonMain/kotlin/message/data/AtAll.kt
@ -60,23 +55,23 @@ Mirai 支持富文本消息。各类型消息元素如下文表格所示。
[`Voice`]: ../mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt [`Voice`]: ../mirai-core-api/src/commonMain/kotlin/message/data/Voice.kt
[`ForwardMessage`]: ../mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt [`ForwardMessage`]: ../mirai-core-api/src/commonMain/kotlin/message/data/ForwardMessage.kt
| 消息类型 | 属性 | 解释 | `contentToString()` | | 消息类型 | 属性 | 解释 | `contentToString()` |
|:------------------------:|:--------------------------------------------|:--------------------|:-----------------------------------| |:------------------------:|:--------------------------------------------|:--------------------|:------------------------|
| [`PlainText`] | `content: String` | 纯文本 | `$content` | | [`PlainText`] | `content: String` | 纯文本 | `$content` |
| [`Image`] | `imageId: String` | 自定义图片 | `[图片]` | | [`Image`] | `imageId: String` | 自定义图片 | `[图片]` |
| [`At`] | `target: Int` | 提及某人 | `@$target` | | [`At`] | `target: Int` | 提及某人 | `@$target` |
| [`AtAll`] | | 提及全体成员 | `@全体成员` | | [`AtAll`] | | 提及全体成员 | `@全体成员` |
| [`Face`] | `id: Int` | 原生表情 | `[表情对应的中文名]` | | [`Face`] | `id: Int` | 原生表情 | `[表情对应的中文名]` |
| [`FlashImage`] | `image: Image` | 闪照 | `[闪照]` | | [`FlashImage`] | `image: Image` | 闪照 | `[闪照]` |
| [`PokeMessage`] | `name: String`, `pokeType: Int` , `id: Int` | 戳一戳消息(消息非动作) | `[戳一戳]` | | [`PokeMessage`] | `name: String`, `pokeType: Int` , `id: Int` | 戳一戳消息(消息非动作) | `[戳一戳]` |
| [`VipFace`] | `kind: VipFace.Kind`, `count: Int` | VIP 表情 | `[${kind.name}]x$count` | | [`VipFace`] | `kind: VipFace.Kind`, `count: Int` | VIP 表情 | `[${kind.name}]x$count` |
| [`LightApp`] | `content: String` | 小程序 | `$content` | | [`LightApp`] | `content: String` | 小程序 | `$content` |
| [`Voice`] | `content: String` | 语音 | `$content` | | [`Voice`] | `content: String` | 语音 | `$content` |
| [`MarketFace`] | `id: Int, name: String` | 商城表情 | `[表情对应的中文名]` | | [`MarketFace`] | `id: Int, name: String` | 商城表情 | `[表情对应的中文名]` |
| [`MessageSource`] | ... | 消息来源元数据 | *空字符串* | | [`MessageSource`] | ... | 消息来源元数据 | *空字符串* |
| [`QuoteReply`] | `source: MessageSource` | 引用回复 | *空字符串* | | [`QuoteReply`] | `source: MessageSource` | 引用回复 | *空字符串* |
| [`ForwardMessage`] | ... | 合并转发 | *`[mirai:forward:NOT_IMPLEMENTED]` | | [`ForwardMessage`] | ... | 合并转发 | *`[转发消息]` |
| [`SimpleServiceMessage`] | `serviceId: Int, content: String` | (不稳定)服务消息 | `$content` | | [`SimpleServiceMessage`] | `serviceId: Int, content: String` | (不稳定)服务消息 | `$content` |
***注意:内容会首先被转义,详见 [转义规则](#转义规则)*** ***注意:内容会首先被转义,详见 [转义规则](#转义规则)***
@ -84,6 +79,11 @@ Mirai 支持富文本消息。各类型消息元素如下文表格所示。
## Mirai 码 ## Mirai 码
消息拥有三种转换到字符串的表示方式。
- `serializeToMiraiCode()`: 消息的一种序列化方式,格式为 `[mirai:TYPE:PROP]`,其中 `TYPE` 为消息类型, `PROP` 为属性。
- `contentToSting()`: QQ 对话框中以纯文本方式会显示的消息内容。无法用纯文字表示的消息会丢失信息,如图片总是 `[图片]`
- `toString()`: Java 对象的 `toString()`,会尽可能包含多的信息用于调试作用,**行为可能不确定**。
实现了接口 `CodableMessage` 的消息类型支持 mirai 码表示。 实现了接口 `CodableMessage` 的消息类型支持 mirai 码表示。
### 转义规则 ### 转义规则
@ -106,20 +106,20 @@ mirai 码内的属性字符串会被转义。
``` ```
val chain = messageChainOf(PlainText("plain"), At(123), AtAll) val chain = messageChainOf(PlainText("plain"), At(123), AtAll)
chain.toMiraiCode() // "plain[mirai:at:123][mirai:atall]" chain.serializeToMiraiCode() // "plain[mirai:at:123][mirai:atall]"
``` ```
### 由 `CodableMessage` 取得 mirai 码字符串 ### 由 `CodableMessage` 取得 mirai 码字符串
通过 `CodableMessage.toMiraiCode()`。 通过 `CodableMessage.serializeToMiraiCode()`。
``` ```
val at = At(123) val at = At(123)
at.toMiraiCode() // 结果为 `[mirai:at:123]` at.serializeToMiraiCode() // 结果为 `[mirai:at:123]`
``` ```
| 消息类型 | `toMiraiCode()` | | 消息类型 | `serializeToMiraiCode()` |
|:------------------------:|:-------------------------------------------------| |:------------------------:|:-------------------------------------------------|
| [`PlainText`] | `$content` | | [`PlainText`] | `$content` |
| [`Image`] | `[mirai:image:$imageId]` | | [`Image`] | `[mirai:image:$imageId]` |

View File

@ -9,15 +9,16 @@
package net.mamoe.mirai.message.code package net.mamoe.mirai.message.code
import net.mamoe.mirai.message.code.MiraiCode.parseMiraiCode import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import net.mamoe.mirai.utils.MiraiExperimentalApi import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.PlannedRemoval
/** /**
* 可以使用 mirai 码表示的 [Message] 类型. * 可以使用 mirai 码表示的 [Message] 类型.
* *
* 从字符串解析 mirai [MiraiCode.parseMiraiCode] * 从字符串解析 mirai [MiraiCode.deserializeMiraiCode]
* *
* @see At * @see At
* @see AtAll * @see AtAll
@ -33,7 +34,11 @@ public interface CodableMessage : Message {
/** /**
* 转换为 mirai . * 转换为 mirai .
*/ */
public fun toMiraiCode(): String = buildString { appendMiraiCodeTo(this) } public fun serializeToMiraiCode(): String = buildString { appendMiraiCodeTo(this) }
@PlannedRemoval("2.0.0")
@Deprecated("Use serializeToMiraiCode()", ReplaceWith("serializeToMiraiCode()"), DeprecationLevel.ERROR)
public fun toMiraiCode(): String = this.serializeToMiraiCode()
// Using StringBuilder faster than direct plus objects // Using StringBuilder faster than direct plus objects
@MiraiExperimentalApi @MiraiExperimentalApi

View File

@ -15,7 +15,7 @@ import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.code.internal.parseMiraiCodeImpl import net.mamoe.mirai.message.code.internal.parseMiraiCodeImpl
import net.mamoe.mirai.message.data.Message import net.mamoe.mirai.message.data.Message
import net.mamoe.mirai.message.data.MessageChain import net.mamoe.mirai.message.data.MessageChain
import net.mamoe.mirai.message.data.MessageChain.Companion.parseFromMiraiCode import net.mamoe.mirai.message.data.MessageChain.Companion.deserializeFromMiraiCode
import net.mamoe.mirai.utils.safeCast import net.mamoe.mirai.utils.safeCast
/** /**
@ -23,54 +23,56 @@ import net.mamoe.mirai.utils.safeCast
*/ */
public object MiraiCode { public object MiraiCode {
/** /**
* 解析形如 "[mirai:]" mirai , [CodableMessage.toMiraiCode] 返回的内容. * 解析形如 "[mirai:]" mirai , [CodableMessage.serializeToMiraiCode] 返回的内容.
* @see MessageChain.parseFromMiraiCode * @see MessageChain.deserializeFromMiraiCode
*/ */
@JvmName("parseMiraiCode1") @JvmName("parseMiraiCode1")
@JvmSynthetic @JvmSynthetic
public inline fun String.parseMiraiCode(contact: Contact? = null): MessageChain = parseMiraiCode(this, contact) public inline fun String.deserializeMiraiCode(contact: Contact? = null): MessageChain =
deserializeMiraiCode(this, contact)
/** /**
* 解析形如 "[mirai:]" mirai , [CodableMessage.toMiraiCode] 返回的内容. * 解析形如 "[mirai:]" mirai , [CodableMessage.serializeToMiraiCode] 返回的内容.
* @see MessageChain.parseFromMiraiCode * @see MessageChain.deserializeFromMiraiCode
*/ */
@JvmOverloads @JvmOverloads
@JvmStatic @JvmStatic
public fun parseMiraiCode(code: String, contact: Contact? = null): MessageChain = code.parseMiraiCodeImpl(contact) public fun deserializeMiraiCode(code: String, contact: Contact? = null): MessageChain =
code.parseMiraiCodeImpl(contact)
/** /**
* 转换得到 mirai . * 转换得到 mirai .
*/ */
@JvmStatic @JvmStatic
public fun Iterable<Message>.toMiraiCode(): String = iterator().toMiraiCode() public fun Iterable<Message>.serializeToMiraiCode(): String = iterator().serializeToMiraiCode()
/** /**
* 转换得到 mirai . * 转换得到 mirai .
*/ */
@JvmStatic @JvmStatic
public fun Sequence<Message>.toMiraiCode(): String = iterator().toMiraiCode() public fun Sequence<Message>.serializeToMiraiCode(): String = iterator().serializeToMiraiCode()
/** /**
* 转换得到 mirai . * 转换得到 mirai .
*/ */
@JvmStatic @JvmStatic
public fun Array<out Message>.toMiraiCode(): String = iterator().toMiraiCode() public fun Array<out Message>.serializeToMiraiCode(): String = iterator().serializeToMiraiCode()
/** /**
* 转换得到 mirai . * 转换得到 mirai .
*/ */
@JvmStatic @JvmStatic
public fun Iterator<Message>.toMiraiCode(): String = buildString { public fun Iterator<Message>.serializeToMiraiCode(): String = buildString {
this@toMiraiCode.forEach { this@serializeToMiraiCode.forEach {
it.safeCast<CodableMessage>()?.appendMiraiCodeTo(this) it.safeCast<CodableMessage>()?.appendMiraiCodeTo(this)
} }
} }
/** /**
* 转换得到 mirai . * 转换得到 mirai .
* @see CodableMessage.toMiraiCode * @see CodableMessage.serializeToMiraiCode
*/ */
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")// for better Java API. @Suppress("EXTENSION_SHADOWED_BY_MEMBER")// for better Java API.
@JvmStatic @JvmStatic
public fun CodableMessage.toMiraiCode(): String = this.toMiraiCode() // member function public fun CodableMessage.serializeToMiraiCode(): String = this.serializeToMiraiCode() // member function
} }

View File

@ -38,7 +38,7 @@ public object AtAll :
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
override fun contentToString(): String = display override fun contentToString(): String = display
override fun toString(): String = "[mirai:atall]" override fun toString(): String = "[mirai:atall]"
override fun toMiraiCode(): String = toString() override fun serializeToMiraiCode(): String = toString()
override fun hashCode(): Int = display.hashCode() override fun hashCode(): Int = display.hashCode()
override fun equals(other: Any?): Boolean = other === this override fun equals(other: Any?): Boolean = other === this

View File

@ -31,7 +31,7 @@ public data class Face(public val id: Int) : // used in delegation
public val name: String get() = contentToString().let { it.substring(1, it.length - 1) } public val name: String get() = contentToString().let { it.substring(1, it.length - 1) }
override fun toString(): String = toMiraiCode() override fun toString(): String = serializeToMiraiCode()
override fun contentToString(): String = names.getOrElse(id) { "[表情]" } override fun contentToString(): String = names.getOrElse(id) { "[表情]" }
@MiraiExperimentalApi @MiraiExperimentalApi

View File

@ -52,7 +52,7 @@ public data class FlashImage(
builder.append(stringValue) builder.append(stringValue)
} }
override fun toMiraiCode(): String = stringValue override fun serializeToMiraiCode(): String = stringValue
override fun toString(): String = stringValue override fun toString(): String = stringValue
override fun contentToString(): String = "[闪照]" override fun contentToString(): String = "[闪照]"

View File

@ -26,8 +26,8 @@ import net.mamoe.mirai.event.events.MessageEvent
import net.mamoe.mirai.message.MessageSerializers import net.mamoe.mirai.message.MessageSerializers
import net.mamoe.mirai.message.code.CodableMessage import net.mamoe.mirai.message.code.CodableMessage
import net.mamoe.mirai.message.code.MiraiCode import net.mamoe.mirai.message.code.MiraiCode
import net.mamoe.mirai.message.code.MiraiCode.parseMiraiCode import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
import net.mamoe.mirai.message.data.MessageChain.Companion.parseFromMiraiCode import net.mamoe.mirai.message.data.MessageChain.Companion.deserializeFromMiraiCode
import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString 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.quote
import net.mamoe.mirai.message.data.MessageSource.Key.recall import net.mamoe.mirai.message.data.MessageSource.Key.recall
@ -169,8 +169,8 @@ import kotlin.streams.asSequence
* *
* 详见 [MiraiCode] * 详见 [MiraiCode]
* *
* - 使用 [MessageChain.toMiraiCode] [MessageChain] 序列化为 Mirai Code [String]. * - 使用 [MessageChain.serializeToMiraiCode] [MessageChain] 序列化为 Mirai Code [String].
* - 使用 [MessageChain.parseFromMiraiCode] Mirai Code [String] 反序列化为 [MessageChain]. * - 使用 [MessageChain.deserializeFromMiraiCode] Mirai Code [String] 反序列化为 [MessageChain].
* *
*/ */
@Serializable(MessageChain.Serializer::class) @Serializable(MessageChain.Serializer::class)
@ -315,12 +315,12 @@ public interface MessageChain :
format.encodeToString(Serializer, this) format.encodeToString(Serializer, this)
/** /**
* 解析形如 "[mirai:]" mirai , [CodableMessage.toMiraiCode] 返回的内容. * 解析形如 "[mirai:]" mirai , [CodableMessage.serializeToMiraiCode] 返回的内容.
* @see MiraiCode.parseMiraiCode * @see MiraiCode.deserializeMiraiCode
*/ */
@JvmStatic @JvmStatic
public fun MessageChain.parseFromMiraiCode(miraiCode: String, contact: Contact? = null): MessageChain = public fun MessageChain.deserializeFromMiraiCode(miraiCode: String, contact: Contact? = null): MessageChain =
miraiCode.parseMiraiCode(contact) miraiCode.deserializeMiraiCode(contact)
} }
} }
@ -333,7 +333,7 @@ public object EmptyMessageChain : MessageChain, List<SingleMessage> by emptyList
override fun toString(): String = "" override fun toString(): String = ""
override fun contentToString(): String = "" override fun contentToString(): String = ""
override fun toMiraiCode(): String = "" override fun serializeToMiraiCode(): String = ""
@MiraiExperimentalApi @MiraiExperimentalApi
override fun appendMiraiCodeTo(builder: StringBuilder) { override fun appendMiraiCodeTo(builder: StringBuilder) {

View File

@ -9,7 +9,7 @@
package net.mamoe.mirai.message.code package net.mamoe.mirai.message.code
import net.mamoe.mirai.message.code.MiraiCode.parseMiraiCode import net.mamoe.mirai.message.code.MiraiCode.deserializeMiraiCode
import net.mamoe.mirai.message.data.* import net.mamoe.mirai.message.data.*
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@ -17,31 +17,31 @@ import kotlin.test.assertEquals
class TestMiraiCode { class TestMiraiCode {
@Test @Test
fun testCodes() { fun testCodes() {
assertEquals(AtAll.toMessageChain(), "[mirai:atall]".parseMiraiCode()) assertEquals(AtAll.toMessageChain(), "[mirai:atall]".deserializeMiraiCode())
assertEquals(PlainText("[Hello").toMessageChain(), "\\[Hello".parseMiraiCode()) assertEquals(PlainText("[Hello").toMessageChain(), "\\[Hello".deserializeMiraiCode())
assertEquals(buildMessageChain { assertEquals(buildMessageChain {
+PlainText("1") +PlainText("1")
+AtAll +AtAll
+PlainText("2345") +PlainText("2345")
+AtAll +AtAll
}, "1[mirai:atall]2345[mirai:atall]".parseMiraiCode()) }, "1[mirai:atall]2345[mirai:atall]".deserializeMiraiCode())
assertEquals(buildMessageChain { assertEquals(buildMessageChain {
+PlainText("1") +PlainText("1")
+AtAll +AtAll
+PlainText("2345[mirai:atall") +PlainText("2345[mirai:atall")
}, "1[mirai:atall]2345[mirai:atall".parseMiraiCode()) }, "1[mirai:atall]2345[mirai:atall".deserializeMiraiCode())
assertEquals(buildMessageChain { assertEquals(buildMessageChain {
+PlainText("[mirai:atall]") +PlainText("[mirai:atall]")
}, "\\[mirai:atall]".parseMiraiCode()) }, "\\[mirai:atall]".deserializeMiraiCode())
assertEquals(buildMessageChain { assertEquals(buildMessageChain {
+PlainText("[mirai:atall]") +PlainText("[mirai:atall]")
}, "[mirai:atall\\]".parseMiraiCode()) }, "[mirai:atall\\]".deserializeMiraiCode())
assertEquals(buildMessageChain { assertEquals(buildMessageChain {
+PlainText("[mirai:atall]") +PlainText("[mirai:atall]")
}, "[mirai\\:atall]".parseMiraiCode()) }, "[mirai\\:atall]".deserializeMiraiCode())
assertEquals(buildMessageChain { assertEquals(buildMessageChain {
+SimpleServiceMessage(1, "[HiHi!!!\\]") +SimpleServiceMessage(1, "[HiHi!!!\\]")
+PlainText(" XE") +PlainText(" XE")
}, "[mirai:service:1,\\[HiHi!!!\\\\\\]] XE".parseMiraiCode()) }, "[mirai:service:1,\\[HiHi!!!\\\\\\]] XE".deserializeMiraiCode())
} }
} }