Commit f78c4734 authored by Him188's avatar Him188

Add suspend shortcuts for toExternalImage

parent 34f10fe2
@file:Suppress("EXPERIMENTAL_API_USAGE")
package net.mamoe.mirai.network.protocol.tim.packet
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.IoBuffer
import net.mamoe.mirai.utils.io.toUHexString
import java.lang.reflect.Field
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.jvm.kotlinProperty
internal object PacketNameFormatter {
@JvmStatic
private var longestNameLength: Int = 43
@JvmStatic
fun adjustName(name: String): String {
if (name.length > longestNameLength) {
longestNameLength = name.length
return name
}
return " ".repeat(longestNameLength - name.length) + name
}
}
private object IgnoreIdListEquals : List<String> by listOf(
"idHex",
"id",
"packetId",
"sequenceIdInternal",
"sequenceId",
"fixedId",
"idByteArray",
"encoded",
"packet",
"EMPTY_ID_HEX",
"input",
"sequenceId",
"output",
"bot",
"UninitializedByteReadPacket",
"sessionKey"
)
private object IgnoreIdListInclude : List<String> by listOf(
"Companion",
"EMPTY_ID_HEX",
"input",
"output",
"this\$",
"\$\$delegatedProperties",
"UninitializedByteReadPacket",
"\$FU",
"RefVolatile"
)
/**
* 这个方法会翻倍内存占用, 考虑修改.
*/
@Suppress("UNCHECKED_CAST")
internal actual fun Packet.packetToString(): String = PacketNameFormatter.adjustName(this::class.simpleName + "(${this.idHexString})") + this::class.java.allDeclaredFields
.filterNot { field ->
IgnoreIdListEquals.any { field.name.replace("\$delegate", "") == it } || IgnoreIdListInclude.any { it in field.name }
}
.joinToString(", ", "{", "}") {
it.isAccessible = true
it.name.replace("\$delegate", "") + "=" + it.get(this).let { value ->
when (value) {
null -> null
is ByteArray -> value.toUHexString()
is UByteArray -> value.toUHexString()
is ByteReadPacket -> "[ByteReadPacket(${value.remaining})]"
//is ByteReadPacket -> value.copy().readBytes().toUHexString()
is IoBuffer -> "[IoBuffer(${value.readRemaining})]"
is Lazy<*> -> "[Lazy]"
is ReadWriteProperty<*, *> -> (value as ReadWriteProperty<Packet, *>).getValue(this, it.kotlinProperty!!)
else -> value.toString()
}
}
}
private val Class<*>.allDeclaredFields: List<Field>
get() {
val list = mutableListOf<Field>()
var clazz: Class<*> = this
do {
list.addAll(clazz.declaredFields)
} while (clazz.let { clazz = it.superclass; clazz.kotlin != Any::class })
return list
}
\ No newline at end of file
......@@ -3,6 +3,8 @@
package net.mamoe.mirai.utils
import io.ktor.util.asStream
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
import kotlinx.io.core.Input
import kotlinx.io.core.IoBuffer
import kotlinx.io.core.buildPacket
......@@ -44,6 +46,9 @@ fun BufferedImage.toExternalImage(formatName: String = "gif"): ExternalImage {
return ExternalImage(width, height, digest.digest(), formatName, buffer)
}
@Suppress("unused")
suspend fun BufferedImage.suspendToExternalImage() = withContext(IO) { toExternalImage() }
/**
* 读取文件头识别图片属性, 然后构造 [ExternalImage]
*/
......@@ -51,7 +56,8 @@ fun BufferedImage.toExternalImage(formatName: String = "gif"): ExternalImage {
fun File.toExternalImage(): ExternalImage {
val input = ImageIO.createImageInputStream(this)
checkNotNull(input) { "Unable to read file(path=${this.path}), no ImageInputStream found" }
val image = ImageIO.getImageReaders(input).asSequence().firstOrNull() ?: error("Unable to read file(path=${this.path}), no ImageReader found")
val image = ImageIO.getImageReaders(input).asSequence().firstOrNull()
?: error("Unable to read file(path=${this.path}), no ImageReader found")
image.input = input
return ExternalImage(
......@@ -64,6 +70,12 @@ fun File.toExternalImage(): ExternalImage {
)
}
/**
* 在 [IO] 中进行 [File.toExternalImage]
*/
@Suppress("unused")
suspend fun File.suspendToExternalImage() = withContext(IO) { toExternalImage() }
/**
* 下载文件到临时目录然后调用 [File.toExternalImage]
*/
......@@ -74,6 +86,12 @@ fun URL.toExternalImage(): ExternalImage {
return file.toExternalImage()
}
/**
* 在 [IO] 中进行 [URL.toExternalImage]
*/
@Suppress("unused")
suspend fun URL.suspendToExternalImage() = withContext(IO) { toExternalImage() }
/**
* 保存为临时文件然后调用 [File.toExternalImage]
*/
......@@ -85,6 +103,12 @@ fun InputStream.toExternalImage(): ExternalImage {
return file.toExternalImage()
}
/**
* 在 [IO] 中进行 [InputStream.toExternalImage]
*/
@Suppress("unused")
suspend fun InputStream.suspendToExternalImage() = withContext(IO) { toExternalImage() }
/**
* 保存为临时文件然后调用 [File.toExternalImage]
*/
......@@ -93,4 +117,10 @@ fun Input.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() }
this.asStream().transferTo(FileOutputStream(file))
return file.toExternalImage()
}
\ No newline at end of file
}
/**
* 在 [IO] 中进行 [Input.toExternalImage]
*/
@Suppress("unused")
suspend fun Input.suspendToExternalImage() = withContext(IO) { toExternalImage() }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment