From 2d26f9476935371ba75fb9bfd7de3a9fb3730160 Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 14 Jan 2022 19:01:04 +0000 Subject: [PATCH] Move `AbstractImage`, `FriendImage`, `GroupImage` from mirai-core-api to mirai-core, close #1670 --- .../commonMain/kotlin/message/data/Image.kt | 65 +---------------- .../commonMain/kotlin/message/imagesImpl.kt | 69 +++++++++++++++++-- .../commonMain/kotlin/utils/ImagePatcher.kt | 5 +- 3 files changed, 67 insertions(+), 72 deletions(-) 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 6728601d182..d8c460a4eff 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Image.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. @@ -39,7 +39,6 @@ import net.mamoe.mirai.message.data.Image.Key.queryUrl import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ExternalResource.Companion.sendAsImageTo import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsImage -import kotlin.LazyThreadSafetyMode.NONE /** * 自定义表情 (收藏的表情) 和普通图片. @@ -410,68 +409,6 @@ public val Image.md5: ByteArray get() = Image.calculateImageMd5ByImageId(imageId) -/** - * 所有 [Image] 实现的基类. - */ -@MiraiInternalApi -public sealed class AbstractImage : Image { - private val _stringValue: String? by lazy(NONE) { "[mirai:image:$imageId]" } - - override val size: Long - get() = 0L - override val width: Int - get() = 0 - override val height: Int - get() = 0 - override val imageType: ImageType - get() = ImageType.UNKNOWN - - final override fun toString(): String = _stringValue!! - final override fun contentToString(): String = if (isEmoji) { - "[动画表情]" - } else { - "[图片]" - } - - override fun appendMiraiCodeTo(builder: StringBuilder) { - builder.append("[mirai:image:").append(imageId).append("]") - } - - final override fun hashCode(): Int = imageId.hashCode() - final override fun equals(other: Any?): Boolean { - if (other === this) return true - if (other !is Image) return false - return this.imageId == other.imageId - } -} - - -/** - * 好友图片 - * - * [imageId] 形如 `/f8f1ab55-bf8e-4236-b55e-955848d7069f` (37 长度) 或 `/000000000-3814297509-BFB7027B9354B8F899A062061D74E206` (54 长度) - */ -// NotOnlineImage -@MiraiInternalApi -public abstract class FriendImage @MiraiInternalApi public constructor() : - AbstractImage() { // change to sealed in the future. - public companion object -} - -/** - * 群图片. - * - * @property imageId 形如 `{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.ext` (ext系扩展名) - * @see Image 查看更多说明 - */ -// CustomFace -@MiraiInternalApi -public abstract class GroupImage @MiraiInternalApi public constructor() : - AbstractImage() { // change to sealed in the future. - public companion object -} - - /** * 内部图片协议实现 * @since 2.9.0-M1 diff --git a/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt b/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt index 8ac56f25ac4..02f9d89c928 100644 --- a/mirai-core/src/commonMain/kotlin/message/imagesImpl.kt +++ b/mirai-core/src/commonMain/kotlin/message/imagesImpl.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("DEPRECATION_ERROR") @@ -24,11 +24,70 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.* import net.mamoe.mirai.internal.utils.io.serialization.loadAs import net.mamoe.mirai.internal.utils.io.serialization.toByteArray import net.mamoe.mirai.internal.utils.structureToString -import net.mamoe.mirai.message.data.* +import net.mamoe.mirai.message.data.FlashImage +import net.mamoe.mirai.message.data.Image import net.mamoe.mirai.message.data.Image.Key.IMAGE_ID_REGEX +import net.mamoe.mirai.message.data.ImageType import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.ExternalResource.Companion.DEFAULT_FORMAT_NAME + +/** + * 所有 [Image] 实现的基类. + */ +// moved from mirai-core-api since 2.11 +internal sealed class AbstractImage : Image { + private val _stringValue: String? by lazy(LazyThreadSafetyMode.NONE) { "[mirai:image:$imageId]" } + + override val size: Long + get() = 0L + override val width: Int + get() = 0 + override val height: Int + get() = 0 + override val imageType: ImageType + get() = ImageType.UNKNOWN + + final override fun toString(): String = _stringValue!! + final override fun contentToString(): String = if (isEmoji) { + "[动画表情]" + } else { + "[图片]" + } + + override fun appendMiraiCodeTo(builder: StringBuilder) { + builder.append("[mirai:image:").append(imageId).append("]") + } + + final override fun hashCode(): Int = imageId.hashCode() + final override fun equals(other: Any?): Boolean { + if (other === this) return true + if (other !is Image) return false + return this.imageId == other.imageId + } +} + + +/** + * 好友图片 + * + * [imageId] 形如 `/f8f1ab55-bf8e-4236-b55e-955848d7069f` (37 长度) 或 `/000000000-3814297509-BFB7027B9354B8F899A062061D74E206` (54 长度) + */ +// NotOnlineImage +// moved from mirai-core-api since 2.11 +internal sealed class FriendImage : AbstractImage() + +/** + * 群图片. + * + * @property imageId 形如 `{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.ext` (ext系扩展名) + * @see Image 查看更多说明 + */ +// CustomFace +// moved from mirai-core-api since 2.11 +internal sealed class GroupImage : AbstractImage() + + @Suppress("SERIALIZER_TYPE_INCOMPATIBLE") @Serializable(with = OnlineGroupImageImpl.Serializer::class) internal class OnlineGroupImageImpl( diff --git a/mirai-core/src/commonMain/kotlin/utils/ImagePatcher.kt b/mirai-core/src/commonMain/kotlin/utils/ImagePatcher.kt index 6a9818a00a8..5a4c532eaf7 100644 --- a/mirai-core/src/commonMain/kotlin/utils/ImagePatcher.kt +++ b/mirai-core/src/commonMain/kotlin/utils/ImagePatcher.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. @@ -10,12 +10,11 @@ package net.mamoe.mirai.internal.utils import net.mamoe.mirai.internal.contact.GroupImpl +import net.mamoe.mirai.internal.message.FriendImage import net.mamoe.mirai.internal.message.OfflineGroupImage import net.mamoe.mirai.internal.network.component.ComponentKey import net.mamoe.mirai.internal.network.protocol.packet.chat.image.ImgStore import net.mamoe.mirai.internal.network.protocol.packet.sendAndExpect -import net.mamoe.mirai.message.data.FriendImage -import net.mamoe.mirai.message.data.md5 import net.mamoe.mirai.utils.ResourceAccessLock import net.mamoe.mirai.utils.UnsafeMutableNonNullProperty import net.mamoe.mirai.utils.currentTimeMillis