From f82d01e606142c9a10c4b67bba8ea62d8229d2cc Mon Sep 17 00:00:00 2001 From: Him188 Date: Fri, 22 May 2020 19:56:02 +0800 Subject: [PATCH] Use kotlinx.serialization, remove jsoup dependency --- backend/mirai-console/build.gradle.kts | 2 - .../mirai/console/center/CuiPluginCenter.kt | 99 +++++++------------ .../mirai/console/center/PluginCenter.kt | 21 +++- .../net/mamoe/mirai/console/setting/Value.kt | 16 --- 4 files changed, 53 insertions(+), 85 deletions(-) delete mode 100644 backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt diff --git a/backend/mirai-console/build.gradle.kts b/backend/mirai-console/build.gradle.kts index 1eb497fc3..1377bd738 100644 --- a/backend/mirai-console/build.gradle.kts +++ b/backend/mirai-console/build.gradle.kts @@ -34,8 +34,6 @@ dependencies { api("net.mamoe.yamlkt:yamlkt:0.3.1") - api("org.jsoup:jsoup:1.12.1") - api("org.jetbrains:annotations:19.0.0") testApi("net.mamoe:mirai-core-qqandroid:${Versions.Mirai.core}") diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/CuiPluginCenter.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/CuiPluginCenter.kt index 60b041c17..1c9c6ee23 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/CuiPluginCenter.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/CuiPluginCenter.kt @@ -1,89 +1,73 @@ +@file:OptIn(MiraiExperimentalAPI::class) + package net.mamoe.mirai.console.center import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import io.ktor.client.request.get import io.ktor.util.KtorExperimentalAPI -import kotlinx.serialization.json.* +import kotlinx.serialization.Serializable +import kotlinx.serialization.UnstableDefault +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonConfiguration import net.mamoe.mirai.console.utils.retryCatching +import net.mamoe.mirai.utils.MiraiExperimentalAPI import java.io.File +@OptIn(UnstableDefault::class) +internal val json = Json(JsonConfiguration(isLenient = true, ignoreUnknownKeys = true)) + +@OptIn(KtorExperimentalAPI::class) +internal val Http = HttpClient(CIO) + internal object CuiPluginCenter : PluginCenter { - var plugins: JsonArray? = null + var plugins: List? = null /** - * 一页10个吧,pageMinNum=1 + * 一页 10 个 pageMinNum=1 */ override suspend fun fetchPlugin(page: Int): Map { check(page > 0) val startIndex = (page - 1) * 10 val endIndex = startIndex + 9 val map = mutableMapOf() - (startIndex until endIndex).forEach { - if (plugins == null) { + (startIndex until endIndex).forEach { index -> + val plugins = plugins ?: kotlin.run { refresh() - } - if (it >= plugins!!.size) { + plugins + } ?: return mapOf() + + if (index >= plugins.size) { return@forEach } - val info = plugins!![it] - with(info.jsonObject) { - map[this["name"]!!.toString()] = PluginCenter.PluginInsight( - this["name"]?.primitive?.content ?: "", - this["version"]?.primitive?.content ?: "", - this["core"]?.primitive?.content ?: "", - this["console"]?.primitive?.content ?: "", - this["author"]?.primitive?.content ?: "", - this["description"]?.primitive?.content ?: "", - this["tags"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf(), - this["commands"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf() - ) - } + + map[name] = plugins[index] } return map } - @OptIn(KtorExperimentalAPI::class) - private val Http = HttpClient(CIO) - override suspend fun findPlugin(name: String): PluginCenter.PluginInfo? { val result = retryCatching(3) { Http.get("https://miraiapi.jasonczc.cn/getPluginDetailedInfo?name=$name") - }.recover { - return null - }.getOrNull() ?: return null - - if (result == "err:not found") { - return null - } - - return result.asJson().run { - PluginCenter.PluginInfo( - this["name"]?.primitive?.content ?: "", - this["version"]?.primitive?.content ?: "", - this["core"]?.primitive?.content ?: "", - this["console"]?.primitive?.content ?: "", - this["tags"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf(), - this["author"]?.primitive?.content ?: "", - this["contact"]?.primitive?.content ?: "", - this["description"]?.primitive?.content ?: "", - this["usage"]?.primitive?.content ?: "", - this["vsc"]?.primitive?.content ?: "", - this["commands"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf(), - this["changeLog"]?.jsonArray?.map { it.primitive.content } ?: arrayListOf() - ) - } + }.getOrElse { return null } + if (result == "err:not found") return null + return json.parse(PluginCenter.PluginInfo.serializer(), result) } override suspend fun refresh() { - val results = Http.get("https://miraiapi.jasonczc.cn/getPluginList").asJson() - if (!(results.containsKey("success") && results["success"]?.boolean == true)) { - error("Failed to fetch plugin list from Cui Cloud") - } - plugins = results["result"]?.jsonArray//先不解析 + @Serializable + data class Result( + val success: Boolean, + val result: List + ) + + val result = json.parse(Result.serializer(), Http.get("https://miraiapi.jasonczc.cn/getPluginList")) + + check(result.success) { "Failed to fetch plugin list from Cui Cloud" } + plugins = result.result } override suspend fun T.downloadPlugin(name: String, progressListener: T.(Float) -> Unit): File { @@ -112,15 +96,6 @@ internal object CuiPluginCenter : PluginCenter { */ } - override val name: String - get() = "崔云" - - - private val json = Json(JsonConfiguration.Stable) - - private fun String.asJson(): JsonObject { - return json.parseJson(this).jsonObject - } - + override val name: String get() = "崔云" } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/PluginCenter.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/PluginCenter.kt index 4456b7f34..26c4f63a7 100644 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/PluginCenter.kt +++ b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/center/PluginCenter.kt @@ -1,13 +1,20 @@ package net.mamoe.mirai.console.center +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import net.mamoe.mirai.utils.MiraiExperimentalAPI import java.io.File +@MiraiExperimentalAPI interface PluginCenter { + @Serializable data class PluginInsight( val name: String, val version: String, + @SerialName("core") val coreVersion: String, + @SerialName("console") val consoleVersion: String, val author: String, val description: String, @@ -15,10 +22,13 @@ interface PluginCenter { val commands: List ) + @Serializable data class PluginInfo( val name: String, val version: String, + @SerialName("core") val coreVersion: String, + @SerialName("console") val consoleVersion: String, val tags: List, val author: String, @@ -32,7 +42,7 @@ interface PluginCenter { /** * 获取一些中心的插件基本信息, - * 能获取到多少由实际的PluginCenter决定 + * 能获取到多少由实际的 [PluginCenter] 决定 * 返回 插件名->Insight */ suspend fun fetchPlugin(page: Int): Map @@ -41,17 +51,18 @@ interface PluginCenter { * 尝试获取到某个插件 by 全名, case sensitive * null 则没有 */ - suspend fun findPlugin(name:String):PluginInfo? + suspend fun findPlugin(name: String): PluginInfo? - suspend fun T.downloadPlugin(name:String, progressListener:T.(Float) -> Unit): File + suspend fun T.downloadPlugin(name: String, progressListener: T.(Float) -> Unit): File - suspend fun downloadPlugin(name:String, progressListener:PluginCenter.(Float) -> Unit): File = downloadPlugin(name,progressListener) + suspend fun downloadPlugin(name: String, progressListener: PluginCenter.(Float) -> Unit): File = + downloadPlugin(name, progressListener) /** * 刷新 */ suspend fun refresh() - val name:String + val name: String } diff --git a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt b/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt deleted file mode 100644 index 989ece08b..000000000 --- a/backend/mirai-console/src/main/kotlin/net/mamoe/mirai/console/setting/Value.kt +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright 2020 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/master/LICENSE - */ - -package net.mamoe.mirai.console.setting - -object ValueSerializerMark - -/* - * More generic ones - */