summaryrefslogtreecommitdiff
path: root/plugin/src/main/kotlin/moe/nea/archenemy/mojang/DownloadMinecraftVersionManifest.kt
blob: 2a21c2e3f01f4f4247f5ef4f28223471b76f1db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package moe.nea.archenemy.mojang

import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.net.URL

abstract class DownloadMinecraftVersionManifest : DefaultTask() {

    @get:OutputFile
    abstract val manifestFile: RegularFileProperty

    @get:Input
    abstract val manifestUrl: Property<String>

    init {
        manifestUrl.convention("https://launchermeta.mojang.com/mc/game/version_manifest.json")
        manifestFile.convention(project.layout.buildDirectory.file("mojang-version-manifest.json"))
    }

    @TaskAction
    fun downloadManifest() {
        val url = URL(manifestUrl.get())
        val file = manifestFile.asFile.get()
        file.parentFile.mkdirs()
        url.openStream().use { input ->
            file.outputStream().use { output ->
                input.copyTo(output)
            }
        }
    }

    @Internal
    fun getManifestNow(): MojangVersionManifest {
        // Force resolution
        project.objects.fileCollection().from(manifestFile).files
        return getManifest().get()
    }

    @Internal
    fun getManifest(): Provider<MojangVersionManifest> {
        return manifestFile.asFile.map {
            val manifestText = it.readText()
            Json.decodeFromString<MojangVersionManifest>(manifestText)
        }
    }
}