From 0015f3817c9accd93d38c3fc8277c3fea449cc8b Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 21 Feb 2020 20:28:48 +0800 Subject: [PATCH] Unified group imageId --- .../mamoe/mirai/qqandroid/message/messages.kt | 9 ++-- .../net.mamoe.mirai/utils/ExternalImage.kt | 43 ++++++++++++++----- 2 files changed, 37 insertions(+), 15 deletions(-) diff --git a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt index 5d0068691..8f0f42630 100644 --- a/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt +++ b/mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt @@ -15,6 +15,7 @@ import kotlinx.io.core.readUInt import net.mamoe.mirai.message.data.* import net.mamoe.mirai.qqandroid.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.qqandroid.network.protocol.data.proto.MsgComm +import net.mamoe.mirai.utils.ExternalImage import net.mamoe.mirai.utils.MiraiDebugAPI import net.mamoe.mirai.utils.MiraiInternalAPI import net.mamoe.mirai.utils.io.discardExact @@ -249,7 +250,7 @@ internal fun MessageChain.toRichTextElems(): MutableList { internal class CustomFaceFromServer( internal val delegate: ImMsgBody.CustomFace ) : CustomFace() { - override val filepath: String get() = delegate.filePath + override val filepath: String = delegate.filePath override val fileId: Int get() = delegate.fileId override val serverIp: Int get() = delegate.serverIp override val serverPort: Int get() = delegate.serverPort @@ -265,14 +266,14 @@ internal class CustomFaceFromServer( override val size: Int get() = delegate.size override val original: Int get() = delegate.origin override val pbReserve: ByteArray get() = delegate.pbReserve - override val imageId: String get() = delegate.filePath + override val imageId: String = ExternalImage.generateImageId(delegate.md5, imageType) override fun equals(other: Any?): Boolean { return other is CustomFaceFromServer && other.filepath == this.filepath && other.md5.contentEquals(this.md5) } override fun hashCode(): Int { - return filepath.hashCode() + 31 * md5.hashCode() + return imageId.hashCode() + 31 * md5.hashCode() } } @@ -296,7 +297,7 @@ internal class NotOnlineImageFromServer( } override fun hashCode(): Int { - return resourceId.hashCode() + 31 * md5.hashCode() + return imageId.hashCode() + 31 * md5.hashCode() } } diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ExternalImage.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ExternalImage.kt index d389e4119..06447bf96 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ExternalImage.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/ExternalImage.kt @@ -51,9 +51,39 @@ class ExternalImage( filename: String ): ExternalImage = ExternalImage(width, height, md5, format, data, data.remaining, filename) - fun generateUUID(md5: ByteArray): String{ + fun generateUUID(md5: ByteArray): String { return "${md5[0..3]}-${md5[4..5]}-${md5[6..7]}-${md5[8..9]}-${md5[10..15]}" } + + fun generateImageId(md5: ByteArray, imageType: Int): String { + return """{${generateUUID(md5)}}.${determineFormat(imageType)}""" + } + + fun determineImageType(format: String): Int { + return when (format) { + "jpg" -> 1000 + "png" -> 1001 + "webp" -> 1002 + "bmp" -> 1005 + "gig" -> 2000 + "apng" -> 2001 + "sharpp" -> 1004 + else -> 1000 // unsupported, just make it jpg + } + } + + fun determineFormat(imageType: Int): String { + return when (imageType) { + 1000 -> "jpg" + 1001 -> "png" + 1002 -> "webp" + 1005 -> "bmp" + 2000 -> "gig" + 2001 -> "apng" + 1004 -> "sharpp" + else -> "jpg" // unsupported, just make it jpg + } + } } val format: String = @@ -73,16 +103,7 @@ class ExternalImage( * SHARPP: 1004 */ val imageType: Int - get() = when (format) { - "jpg" -> 1000 - "png" -> 1001 - "webp" -> 1002 - "bmp" -> 1005 - "gig" -> 2000 - "apng" -> 2001 - "sharpp" -> 1004 - else -> 1000 // unsupported, just make it jpg - } + get() = determineImageType(format) override fun toString(): String = "[ExternalImage(${width}x$height $format)]"