From d1528ea0933923c7f97ac86dd1ac498deaff3caf Mon Sep 17 00:00:00 2001 From: purofle Date: Tue, 2 Jan 2024 23:59:01 +0800 Subject: [PATCH] =?UTF-8?q?FabricVersion:=20=E5=AE=9E=E7=8E=B0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=20fabric=20=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nmsl/download/fabric/FabricVersion.kt | 35 +++++++++++++++++++ .../nmsl/download/fabric/FabricVersionTest.kt | 12 +++++++ .../purofle/nmsl/utils/io/HttpRequestTest.kt | 3 -- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 NMSLCore/src/test/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersionTest.kt diff --git a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersion.kt b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersion.kt index 0eea673..aabe2e7 100644 --- a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersion.kt +++ b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersion.kt @@ -1,6 +1,41 @@ package com.github.purofle.nmsl.download.fabric +import com.github.purofle.nmsl.utils.io.HttpRequest +import kotlinx.serialization.Serializable + //TODO object FabricVersion { + private const val GAME_META_URL = "https://meta.fabricmc.net/v2/versions/game" + private const val LOADER_META_URL = "https://meta.fabricmc.net/v2/versions/loader" + private suspend fun getGameVersions(url: String): List { + return HttpRequest.getJson>(url).map { it.version } + } + + suspend fun getFabricVersion(): List { + val gameVersions = getGameVersions(GAME_META_URL) + val loaderVersions = getGameVersions(LOADER_META_URL) + + return gameVersions.zip(loaderVersions) + .map { (gameVersion, loaderVersion) -> + RemoteFabricVersion( + gameVersion = gameVersion, + fabricVersion = loaderVersion, + metaUrl = "https://meta.fabricmc.net/v2/versions/loader/$gameVersion/$loaderVersion" + ) + } + } + + @Serializable + data class GameVersion( + val version: String, + val stable: Boolean, + val maven: String? = null, + ) + @Serializable + data class RemoteFabricVersion( + val gameVersion: String, + val fabricVersion: String, + val metaUrl: String + ) } \ No newline at end of file diff --git a/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersionTest.kt b/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersionTest.kt new file mode 100644 index 0000000..18def23 --- /dev/null +++ b/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/download/fabric/FabricVersionTest.kt @@ -0,0 +1,12 @@ +package com.github.purofle.nmsl.download.fabric + +import kotlinx.coroutines.runBlocking +import kotlin.test.Test + +class FabricVersionTest { + @Test + fun getFabricVersionTest() = runBlocking { + val fabricVersion = FabricVersion.getFabricVersion() + println(fabricVersion) + } +} \ No newline at end of file diff --git a/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/utils/io/HttpRequestTest.kt b/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/utils/io/HttpRequestTest.kt index a5e4426..e9fd257 100644 --- a/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/utils/io/HttpRequestTest.kt +++ b/NMSLCore/src/test/kotlin/com/github/purofle/nmsl/utils/io/HttpRequestTest.kt @@ -24,9 +24,6 @@ class HttpRequestTest { files = manifest.versions.map { HttpRequest.DownloadInfo(it.url, File.createTempFile(it.id, ".json")) }, - progress = { url, bytesSentTotal, contentLength -> - println("$url: $bytesSentTotal/$contentLength") - }, ) } }