Skip to content

Commit

Permalink
FabricDownloader: 实现安装 fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Jan 14, 2024
1 parent 40826b4 commit 495c9e1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String>
)

@Serializable
data class Libraries(
val common: List<FabricLibrary>,
)

@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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
Expand Down
11 changes: 11 additions & 0 deletions NMSLCore/src/test/kotlin/DownloadFabricTest.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

0 comments on commit 495c9e1

Please sign in to comment.