Skip to content

Commit

Permalink
FabricDownloader: 编辑游戏Json
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Jan 14, 2024
1 parent 495c9e1 commit b6c0cbf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ 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.Artifact
import com.github.purofle.nmsl.game.GameJson
import com.github.purofle.nmsl.game.Library
import com.github.purofle.nmsl.game.LibraryDownload
import com.github.purofle.nmsl.utils.io.HttpRequest
import com.github.purofle.nmsl.utils.io.HttpRequest.downloadFile
import com.github.purofle.nmsl.utils.json.JsonUtils.toJsonString
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
import kotlin.io.path.writeText

class FabricDownloader(
private val provider: DownloadProvider = MCBBSDownloadProvider(),
Expand All @@ -23,9 +28,15 @@ class FabricDownloader(

private val logger: Logger = LogManager.getLogger(this::class.java)

suspend fun downloadFabric() {
private lateinit var fabricMeta: FabricMeta

private suspend fun downloadFabricMeta() {
val fabricVersion = FabricVersion.getFabricVersion()
val fabricMeta = getFabricMeta(fabricVersion.first { it.gameVersion == version.id }.metaUrl)
fabricMeta = getFabricMeta(fabricVersion.first { it.gameVersion == version.id }.metaUrl)
}

suspend fun downloadFabric() {
downloadFabricMeta()

fabricMeta.loader.download()
fabricMeta.intermediary.download()
Expand All @@ -40,7 +51,47 @@ class FabricDownloader(
logger.info("Downloaded libraries")
}

suspend fun editGameJson() {
fun editGameJson() {

val loader = Library(
name = fabricMeta.loader.maven, downloads = LibraryDownload(
Artifact(
path = getPath(fabricMeta.loader.maven),
sha1 = null,
size = 0,
url = getLibraryUrl(maven = fabricMeta.loader.maven)
)
)
)

val intermediary = Library(
name = fabricMeta.intermediary.maven, downloads = LibraryDownload(
Artifact(
path = getPath(fabricMeta.intermediary.maven),
sha1 = null,
size = 0,
url = getLibraryUrl(maven = fabricMeta.intermediary.maven)
)
)
)

val newVersion = version.copy(
mainClass = fabricMeta.launcherMeta.mainClass["client"] ?: error("mainClass not found"),
libraries = version.libraries + loader + intermediary + fabricMeta.launcherMeta.libraries.common.map {
Library(
name = it.name,
downloads = LibraryDownload(
Artifact(
path = getPath(it.name),
sha1 = it.sha1,
size = 0,
url = getLibraryUrl(maven = it.name)
)
)
)
}
)
gameJsonFile.writeText(newVersion.toJsonString())
}

suspend fun Maven.download() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ data class Natives(
@Serializable
data class Artifact(
val path: String,
val sha1: String,
val sha1: String?,
val size: Int,
val url: String,
)
Expand Down
1 change: 1 addition & 0 deletions NMSLCore/src/test/kotlin/DownloadFabricTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ suspend fun main() {
val versions = GameManager.getVersionJson(GameManager.versions.first())
FabricDownloader(provider, versions).apply {
downloadFabric()
editGameJson()
}
}

0 comments on commit b6c0cbf

Please sign in to comment.