From 495c9e1ecf6e3de9b730a59f526afa51cd3b4dd9 Mon Sep 17 00:00:00 2001 From: purofle Date: Sun, 14 Jan 2024 14:49:09 +0800 Subject: [PATCH] =?UTF-8?q?FabricDownloader:=20=E5=AE=9E=E7=8E=B0=E5=AE=89?= =?UTF-8?q?=E8=A3=85=20fabric?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nmsl/download/fabric/FabricDownloader.kt | 56 +++++++++++++++++++ .../nmsl/download/fabric/FabricMeta.kt | 47 ++++++++++++++++ .../purofle/nmsl/utils/io/HttpRequest.kt | 11 +++- .../src/test/kotlin/DownloadFabricTest.kt | 11 ++++ .../nmsl/download/fabric/FabricVersionTest.kt | 3 +- 5 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricDownloader.kt create mode 100644 NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricMeta.kt create mode 100644 NMSLCore/src/test/kotlin/DownloadFabricTest.kt diff --git a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricDownloader.kt b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricDownloader.kt new file mode 100644 index 0000000..1c01da6 --- /dev/null +++ b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricDownloader.kt @@ -0,0 +1,56 @@ +package com.github.purofle.nmsl.download.fabric + +import com.github.purofle.nmsl.download.DownloadProvider +import com.github.purofle.nmsl.download.MCBBSDownloadProvider +import com.github.purofle.nmsl.game.GameJson +import com.github.purofle.nmsl.utils.io.HttpRequest +import com.github.purofle.nmsl.utils.io.HttpRequest.downloadFile +import com.github.purofle.nmsl.utils.os.OS +import org.apache.log4j.LogManager +import org.apache.log4j.Logger +import kotlin.io.path.createDirectories +import kotlin.io.path.div + +class FabricDownloader( + private val provider: DownloadProvider = MCBBSDownloadProvider(), + private val version: GameJson, +) { + + private val versionDir = OS.minecraftWorkingDirectory() / "versions" / version.id + private val librariesDir = OS.minecraftWorkingDirectory() / "libraries" + + private val gameJsonFile = versionDir / "${version.id}.json" + + private val logger: Logger = LogManager.getLogger(this::class.java) + + suspend fun downloadFabric() { + val fabricVersion = FabricVersion.getFabricVersion() + val fabricMeta = getFabricMeta(fabricVersion.first { it.gameVersion == version.id }.metaUrl) + + fabricMeta.loader.download() + fabricMeta.intermediary.download() + + val libraries = fabricMeta.launcherMeta.libraries.common.map { + val file = librariesDir.resolve(getPath(it.name)) + file.parent.createDirectories() + HttpRequest.DownloadInfo(getLibraryUrl(maven = it.name), file.toFile(), it.sha1) + } + + HttpRequest.downloadFiles(libraries, onDownloadComplete = {}) + logger.info("Downloaded libraries") + } + + suspend fun editGameJson() { + } + + suspend fun Maven.download() { + val path = librariesDir.resolve(getPath(maven)) + path.parent.createDirectories() + + val url = getLibraryUrl(maven = maven) + downloadFile { + this.url = url + saveFile = path.toFile() + } + } +} \ No newline at end of file diff --git a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricMeta.kt b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricMeta.kt new file mode 100644 index 0000000..3fef9ca --- /dev/null +++ b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/download/fabric/FabricMeta.kt @@ -0,0 +1,47 @@ +package com.github.purofle.nmsl.download.fabric + +import com.github.purofle.nmsl.utils.io.HttpRequest +import kotlinx.serialization.Serializable + +suspend fun getFabricMeta(url: String): FabricMeta { + return HttpRequest.getJson(url) +} + +@Serializable +data class FabricMeta( + val loader: Maven, + val intermediary: Maven, + val launcherMeta: LauncherMeta +) + +@Serializable +data class Maven( + val maven: String, + val version: String +) + +@Serializable +data class LauncherMeta( + val libraries: Libraries, + val mainClass: Map +) + +@Serializable +data class Libraries( + val common: List, +) + +@Serializable +data class FabricLibrary( + val name: String, + val url: String, + val sha1: String, + val size: Int, +) + +fun getLibraryUrl(url: String = "https://maven.fabricmc.net/", maven: String): String = "${url}${getPath(maven)}" + +fun getPath(maven: String): String { + val (group, artifact, version) = maven.split(":") + return "${group.replace(".", "/")}/${artifact}/$version/${artifact}-$version.jar" +} \ No newline at end of file diff --git a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/utils/io/HttpRequest.kt b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/utils/io/HttpRequest.kt index 0f38a1e..e5e0985 100644 --- a/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/utils/io/HttpRequest.kt +++ b/NMSLCore/src/main/kotlin/com/github/purofle/nmsl/utils/io/HttpRequest.kt @@ -13,10 +13,13 @@ import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.serialization.kotlinx.json.* import io.ktor.utils.io.core.* -import kotlinx.coroutines.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll import kotlinx.coroutines.channels.ClosedReceiveChannelException import kotlinx.coroutines.sync.Semaphore import kotlinx.coroutines.sync.withPermit +import kotlinx.coroutines.withContext import org.apache.logging.log4j.LogManager import java.io.File import kotlin.coroutines.CoroutineContext @@ -89,8 +92,10 @@ object HttpRequest { download.saveFile.writeBytes(request.readBytes()) // check sha1 - require(download.sha1 != null && download.saveFile.sha1String() == download.sha1) { - logger.error("sha1 check failed: ${download.saveFile.name}, sha1: ${download.sha1}, file sha1: ${download.saveFile.sha1String()}") + if (download.sha1 != null) { + require(download.saveFile.sha1String() == download.sha1) { + logger.error("sha1 check failed: ${download.saveFile.name}, sha1: ${download.sha1}, file sha1: ${download.saveFile.sha1String()}") + } } logger.info("Downloaded ${download.url}") diff --git a/NMSLCore/src/test/kotlin/DownloadFabricTest.kt b/NMSLCore/src/test/kotlin/DownloadFabricTest.kt new file mode 100644 index 0000000..b00e992 --- /dev/null +++ b/NMSLCore/src/test/kotlin/DownloadFabricTest.kt @@ -0,0 +1,11 @@ +import com.github.purofle.nmsl.download.MCBBSDownloadProvider +import com.github.purofle.nmsl.download.fabric.FabricDownloader +import com.github.purofle.nmsl.game.GameManager + +suspend fun main() { + val provider = MCBBSDownloadProvider() + val versions = GameManager.getVersionJson(GameManager.versions.first()) + FabricDownloader(provider, versions).apply { + downloadFabric() + } +} \ 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 index 18def23..630606e 100644 --- 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 @@ -7,6 +7,7 @@ class FabricVersionTest { @Test fun getFabricVersionTest() = runBlocking { val fabricVersion = FabricVersion.getFabricVersion() - println(fabricVersion) + val fabricMeta = getFabricMeta(fabricVersion.first().metaUrl) + println(fabricMeta.launcherMeta.libraries.common.first()) } } \ No newline at end of file