Unified group imageId

This commit is contained in:
Him188 2020-02-21 20:28:48 +08:00
parent 2cd2591ff6
commit 0015f3817c
2 changed files with 37 additions and 15 deletions

View File

@ -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<ImMsgBody.Elem> {
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()
}
}

View File

@ -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)]"