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

[core] 优化 SuperFace API

This commit is contained in:
Him188 2023-09-02 11:50:23 +08:00
parent 59ff38a776
commit 71adb5facf
No known key found for this signature in database
GPG Key ID: 0B199D2C430CD6A1
4 changed files with 37 additions and 17 deletions
mirai-core-api
compatibility-validation
android/api
jvm/api
src
commonMain/kotlin/message/data
commonTest/kotlin/message.data

View File

@ -5312,6 +5312,7 @@ public final class net/mamoe/mirai/message/data/SuperFace : net/mamoe/mirai/mess
public fun contentToString ()Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public static final fun from (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final fun fromOrNull (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public final fun getFace ()I
public final fun getId ()Ljava/lang/String;
public fun getKey ()Lnet/mamoe/mirai/message/data/MessageKey;
@ -5319,7 +5320,6 @@ public final class net/mamoe/mirai/message/data/SuperFace : net/mamoe/mirai/mess
public final fun getType ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
public static final fun tryFrom (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final fun write$Self (Lnet/mamoe/mirai/message/data/SuperFace;Lkotlinx/serialization/encoding/CompositeEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;)V
}
@ -5336,14 +5336,14 @@ public final class net/mamoe/mirai/message/data/SuperFace$$serializer : kotlinx/
public final class net/mamoe/mirai/message/data/SuperFace$Key : net/mamoe/mirai/message/data/AbstractPolymorphicMessageKey {
public final fun from (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public final fun fromOrNull (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public final fun serializer ()Lkotlinx/serialization/KSerializer;
public final fun tryFrom (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
}
public final class net/mamoe/mirai/message/data/SuperFaceKt {
public static final synthetic fun SuperFace (I)Lnet/mamoe/mirai/message/data/SuperFace;
public static final synthetic fun SuperFace (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final synthetic fun toFace (Lnet/mamoe/mirai/message/data/SuperFace;)Lnet/mamoe/mirai/message/data/Face;
public static final synthetic fun toSuperFace (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final synthetic fun toSuperFaceOrNull (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
}
public abstract interface class net/mamoe/mirai/message/data/UnsupportedMessage : net/mamoe/mirai/message/data/MessageContent {

View File

@ -5312,6 +5312,7 @@ public final class net/mamoe/mirai/message/data/SuperFace : net/mamoe/mirai/mess
public fun contentToString ()Ljava/lang/String;
public fun equals (Ljava/lang/Object;)Z
public static final fun from (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final fun fromOrNull (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public final fun getFace ()I
public final fun getId ()Ljava/lang/String;
public fun getKey ()Lnet/mamoe/mirai/message/data/MessageKey;
@ -5319,7 +5320,6 @@ public final class net/mamoe/mirai/message/data/SuperFace : net/mamoe/mirai/mess
public final fun getType ()I
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
public static final fun tryFrom (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final fun write$Self (Lnet/mamoe/mirai/message/data/SuperFace;Lkotlinx/serialization/encoding/CompositeEncoder;Lkotlinx/serialization/descriptors/SerialDescriptor;)V
}
@ -5336,14 +5336,14 @@ public final class net/mamoe/mirai/message/data/SuperFace$$serializer : kotlinx/
public final class net/mamoe/mirai/message/data/SuperFace$Key : net/mamoe/mirai/message/data/AbstractPolymorphicMessageKey {
public final fun from (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public final fun fromOrNull (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public final fun serializer ()Lkotlinx/serialization/KSerializer;
public final fun tryFrom (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
}
public final class net/mamoe/mirai/message/data/SuperFaceKt {
public static final synthetic fun SuperFace (I)Lnet/mamoe/mirai/message/data/SuperFace;
public static final synthetic fun SuperFace (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final synthetic fun toFace (Lnet/mamoe/mirai/message/data/SuperFace;)Lnet/mamoe/mirai/message/data/Face;
public static final synthetic fun toSuperFace (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
public static final synthetic fun toSuperFaceOrNull (Lnet/mamoe/mirai/message/data/Face;)Lnet/mamoe/mirai/message/data/SuperFace;
}
public abstract interface class net/mamoe/mirai/message/data/UnsupportedMessage : net/mamoe/mirai/message/data/MessageContent {

View File

@ -17,10 +17,14 @@ import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.NotStableForInheritance
import net.mamoe.mirai.utils.safeCast
import kotlin.jvm.Throws
/**
* 超级表情
* 表示一个超级表情.
*
* 超级表情由[普通表情][Face]转换而来. 不是所有的普通表情都有对应的超级表情.
*
* 要构造超级表情, 使用 [SuperFace.from] [SuperFace.fromOrNull].
* Kotlin 还可以使用对应扩展函数: [Face.toSuperFace] [Face.toSuperFaceOrNull].
*
* @see Face
* @since 2.16
@ -46,12 +50,12 @@ public class SuperFace @MiraiInternalApi constructor(
/**
* 将普通表情转换为超级表情.
*
* @throws UnsupportedOperationException 无法转换时抛出
* @throws IllegalArgumentException 无法转换时抛出
**/
@JvmStatic
@Throws(UnsupportedOperationException::class)
public fun from(face: Face): SuperFace {
return tryFrom(face = face) ?: throw UnsupportedOperationException("${face.id} - ${face.name}")
return fromOrNull(face = face)
?: throw IllegalArgumentException("No SuperFace mapping from Face(id=${face.id}, name='${face.name}')")
}
/**
@ -61,7 +65,7 @@ public class SuperFace @MiraiInternalApi constructor(
**/
@JvmStatic
@OptIn(MiraiInternalApi::class)
public fun tryFrom(face: Face): SuperFace? {
public fun fromOrNull(face: Face): SuperFace? {
val stickerId = when (face.id) {
Face.DA_CALL -> "1"
Face.BIAN_XING -> "2"
@ -134,11 +138,27 @@ public class SuperFace @MiraiInternalApi constructor(
}
}
/**
* 将超级表情转换为普通表情
*
* @since 2.16
*/
@JvmSynthetic
public fun SuperFace.toFace(): Face = Face(id = face)
/**
* 将普通表情转换为超级表情
*
* @since 2.16
* @throws IllegalArgumentException 无法转换时抛出
*/
@JvmSynthetic
public fun SuperFace(face: Face): SuperFace = SuperFace.from(face)
public fun Face.toSuperFace(): SuperFace = SuperFace.from(this)
/**
* 将普通表情转换为超级表情, 在无法转换时返回 `null`
*
* @since 2.16
*/
@JvmSynthetic
public fun SuperFace(face: Int): SuperFace = SuperFace.from(face = Face(id = face))
public fun Face.toSuperFaceOrNull(): SuperFace? = SuperFace.fromOrNull(this)

View File

@ -302,7 +302,7 @@ internal class MessageVisitorTest {
"visitSingleMessage",
"visitMessage",
),
SuperFace(Face.DA_CALL).accept(GetCalledMethodNames)
SuperFace.from(Face(Face.DA_CALL)).accept(GetCalledMethodNames)
)
assertContentEquals(