Karlatemp пре 3 година
родитељ
комит
dc02b88bb6

+ 8 - 0
buildSrc/src/main/kotlin/Versions.kt

@@ -51,6 +51,10 @@ object Versions {
 
 
     const val junit = "5.7.2"
     const val junit = "5.7.2"
 
 
+    // https://github.com/google/jimfs
+    // Java In Memory File System
+    const val jimfs = "1.2"
+
     // If you the versions below, you need to sync changes to mirai-console/buildSrc/src/main/kotlin/Versions.kt
     // If you the versions below, you need to sync changes to mirai-console/buildSrc/src/main/kotlin/Versions.kt
 
 
     const val yamlkt = "0.10.2"
     const val yamlkt = "0.10.2"
@@ -109,6 +113,10 @@ val `ktor-client-logging-jvm` = ktor("client-logging-jvm", Versions.ktor)
 val `ktor-network-jvm` = ktor("network-jvm", Versions.ktor)
 val `ktor-network-jvm` = ktor("network-jvm", Versions.ktor)
 val `ktor-client-serialization-jvm` = ktor("client-serialization-jvm", Versions.ktor)
 val `ktor-client-serialization-jvm` = ktor("client-serialization-jvm", Versions.ktor)
 
 
+val `ktor-server-core` = ktor("server-core", Versions.ktor)
+val `ktor-server-netty` = ktor("server-netty", Versions.ktor)
+const val `java-in-memory-file-system` = "com.google.jimfs:jimfs:" + Versions.jimfs
+
 const val `logback-classic` = "ch.qos.logback:logback-classic:" + Versions.logback
 const val `logback-classic` = "ch.qos.logback:logback-classic:" + Versions.logback
 
 
 const val `slf4j-api` = "org.slf4j:slf4j-api:" + Versions.slf4j
 const val `slf4j-api` = "org.slf4j:slf4j-api:" + Versions.slf4j

+ 43 - 0
mirai-core-utils/src/androidMain/kotlin/Image.android.kt

@@ -0,0 +1,43 @@
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+@file:JvmMultifileClass
+@file:JvmName("MiraiUtils")
+@file:Suppress("NOTHING_TO_INLINE")
+
+package net.mamoe.mirai.utils
+
+import android.graphics.Bitmap
+import java.io.ByteArrayOutputStream
+
+public fun randomImage_android(): Bitmap {
+    val width = (500..800).random()
+    val height = (500..800).random()
+    val bitmap = Bitmap.createBitmap(
+        width, height, Bitmap.Config.RGB_565
+    )
+    for (x in 0 until width) {
+        for (y in 0 until height) {
+            bitmap.setPixel(
+                x, y,
+                (0..0xFFFFFF).random()
+            )
+        }
+    }
+    return bitmap
+}
+
+public fun Bitmap.saveToBytes(): ByteArray = ByteArrayOutputStream().also { output ->
+    compress(Bitmap.CompressFormat.PNG, 100, output)
+}.toByteArray()
+
+public actual fun randomImageContent(): ByteArray {
+    return randomImage_android().saveToBytes()
+}
+

+ 13 - 0
mirai-core-utils/src/commonMain/kotlin/Conversions.kt

@@ -138,6 +138,19 @@ public fun ByteArray.toInt(): Int =
         255
         255
     ) shl 0)
     ) shl 0)
 
 
+public fun ByteArray.toLong(): Long {
+    var rsp: Long = 0
+    rsp += this[0].toLong().and(255).shl(56)
+    rsp += this[1].toLong().and(255).shl(48)
+    rsp += this[2].toLong().and(255).shl(40)
+    rsp += this[3].toLong().and(255).shl(32)
+    rsp += this[4].toLong().and(255).shl(24)
+    rsp += this[5].toLong().and(255).shl(16)
+    rsp += this[6].toLong().and(255).shl(8)
+    rsp += this[7].toLong().and(255).shl(0)
+    return rsp
+}
+
 
 
 ///////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////
 // hexToBytes
 // hexToBytes

+ 14 - 0
mirai-core-utils/src/commonMain/kotlin/IO.kt

@@ -17,6 +17,7 @@ package net.mamoe.mirai.utils
 import kotlinx.io.charsets.Charset
 import kotlinx.io.charsets.Charset
 import kotlinx.io.core.*
 import kotlinx.io.core.*
 import java.io.File
 import java.io.File
+import java.io.InputStream
 import kotlin.text.String
 import kotlin.text.String
 import java.nio.Buffer as JNioBuffer
 import java.nio.Buffer as JNioBuffer
 
 
@@ -140,6 +141,19 @@ public inline fun Input.readString(length: Byte, charset: Charset = Charsets.UTF
 public fun Input.readUShortLVString(): String = String(this.readUShortLVByteArray())
 public fun Input.readUShortLVString(): String = String(this.readUShortLVByteArray())
 public fun Input.readUShortLVByteArray(): ByteArray = this.readBytes(this.readUShort().toInt())
 public fun Input.readUShortLVByteArray(): ByteArray = this.readBytes(this.readUShort().toInt())
 
 
+public fun InputStream.dropContentAndClose(bufferSize: Int = 2048): Unit = use {
+    dropContent(bufferSize)
+}
+
+public fun InputStream.dropContent(bufferSize: Int = 2048) {
+    val buffer = ByteArray(bufferSize)
+    while (true) {
+        if (read(buffer) == -1) {
+            return
+        }
+    }
+}
+
 public fun File.createFileIfNotExists() {
 public fun File.createFileIfNotExists() {
     if (!this.exists()) {
     if (!this.exists()) {
         this.parentFile.mkdirs()
         this.parentFile.mkdirs()

+ 34 - 0
mirai-core-utils/src/commonMain/kotlin/Images.kt

@@ -0,0 +1,34 @@
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+@file:JvmMultifileClass
+@file:JvmName("MiraiUtils")
+@file:Suppress("NOTHING_TO_INLINE")
+
+package net.mamoe.mirai.utils
+
+public expect fun randomImageContent(): ByteArray

+ 36 - 0
mirai-core-utils/src/commonMain/kotlin/http.kt

@@ -0,0 +1,36 @@
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+package net.mamoe.mirai.utils
+
+public fun String.plusHttpSubpath(subpath: String): String {
+
+    if (this[this.lastIndex] == '/') return this + subpath
+
+    return "$this/$subpath"
+}
+

+ 49 - 0
mirai-core-utils/src/jvmMain/kotlin/IO.jvm.kt

@@ -0,0 +1,49 @@
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+@file:JvmMultifileClass
+@file:JvmName("MiraiUtils")
+
+@file:Suppress("NOTHING_TO_INLINE")
+
+package net.mamoe.mirai.utils
+
+import java.nio.file.Files
+import java.nio.file.Path
+import kotlin.io.path.deleteIfExists
+import kotlin.io.path.exists
+import kotlin.io.path.isDirectory
+import kotlin.io.path.listDirectoryEntries
+
+public val Path.isFile: Boolean get() = Files.exists(this) && !Files.isDirectory(this)
+
+public inline fun Path.mkdir() {
+    Files.createDirectory(this)
+}
+
+public inline fun Path.mkdirs() {
+    Files.createDirectories(this)
+}
+
+public fun Path.mkParentDirs() {
+    val current = parent ?: return
+    if (current == this) return
+    if (current.exists()) return
+    current.mkParentDirs()
+    current.mkdir()
+}
+
+public fun Path.deleteRecursively(): Boolean {
+    if (isFile) return deleteIfExists()
+    if (isDirectory()) {
+        listDirectoryEntries().forEach { it.deleteRecursively() }
+        return deleteIfExists()
+    }
+    return false
+}

+ 46 - 0
mirai-core-utils/src/jvmMain/kotlin/Image.jvm.kt

@@ -0,0 +1,46 @@
+/*
+ * Copyright 2019-2021 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.
+ *
+ * https://github.com/mamoe/mirai/blob/dev/LICENSE
+ */
+
+@file:JvmMultifileClass
+@file:JvmName("MiraiUtils")
+@file:Suppress("NOTHING_TO_INLINE")
+
+package net.mamoe.mirai.utils
+
+import java.awt.Color
+import java.awt.image.BufferedImage
+import java.io.ByteArrayOutputStream
+import javax.imageio.ImageIO
+
+public fun randomImage_awt_jvm(): BufferedImage {
+    val width = (500..800).random()
+    val height = (500..800).random()
+    val image = BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
+    val graphics = image.createGraphics()
+    for (x in 0 until width) {
+        for (y in 0 until height) {
+            graphics.color = Color(
+                (0..0xFFFFFF).random()
+            )
+            graphics.drawRect(x, y, 1, 1)
+        }
+    }
+    graphics.dispose()
+    return image
+}
+
+public fun BufferedImage.saveToBytes(): ByteArray = ByteArrayOutputStream().apply {
+    ImageIO.write(this@saveToBytes, "png", this)
+}.toByteArray()
+
+
+public actual fun randomImageContent(): ByteArray {
+    return randomImage_awt_jvm().saveToBytes()
+}
+