add minecraft dependency injection

This commit is contained in:
moehreag 2024-05-13 11:27:31 +02:00
parent 47393b74c9
commit 1e4b950c30
5 changed files with 63 additions and 38 deletions

View file

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">

View file

@ -9,6 +9,9 @@ version = "1.0.0"
repositories { repositories {
mavenCentral() mavenCentral()
maven {
url = uri("https://libraries.minecraft.net/")
}
} }
dependencies { dependencies {
@ -36,7 +39,7 @@ publishing {
publications { publications {
repositories { repositories {
mavenLocal { mavenLocal {
url = uri("file://home/ecorous/wawa/test-repo")
} }
} }
} }

View file

@ -2,21 +2,36 @@ package org.ecorous.esnesnon.gradle
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.artifacts.repositories.MavenArtifactRepository
import java.net.URI
import kotlin.io.path.Path
import kotlin.io.path.absolute
class NonsenseGradlePlugin : Plugin<Project> { class NonsenseGradlePlugin : Plugin<Project> {
val toInject = listOf("com.electronwill.night-config:toml:3.6.0", "") val toInject = listOf("com.electronwill.night-config:toml:3.6.0")
override fun apply(project: Project) { override fun apply(project: Project) {
println("> Applying Nonsense Gradle Plugin") println("> Applying Nonsense Gradle Plugin")
project.repositories.maven {
it.name = "Minecraft/Local"
it.url = project.gradle.gradleUserHomeDir.resolve("caches/nonsense-gradle/").toURI()
}
project.repositories.maven {
it.name = "Minecraft Libraries"
it.url = URI.create("https://libraries.minecraft.net/")
}
project.repositories.mavenCentral()
val buildTask = project.tasks.getByPath("build") val buildTask = project.tasks.getByPath("build")
buildTask.dependsOn("setupNonsense") buildTask.dependsOn("setupNonsense")
project.dependencies.apply { project.dependencies.apply {
add("implementation", "com.electronwill.night-config:toml:3.6.0") toInject.forEach {
// create("com.electronwill.night-config:toml:3.6.0") add("implementation", it)
} }
}
project.task("setupNonsense").apply { project.task("setupNonsense").apply {
group = "nonsense" group = "nonsense"

View file

@ -2,49 +2,44 @@ package org.ecorous.esnesnon.gradle
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import org.gradle.api.Project import java.io.File
import java.net.URI import java.net.URI
import java.net.http.HttpClient import java.net.http.HttpClient
import java.net.http.HttpRequest import java.net.http.HttpRequest
import java.net.http.HttpResponse import java.net.http.HttpResponse
import kotlin.io.path.Path import java.nio.file.Path
import kotlin.io.path.createDirectories import kotlin.io.path.*
import kotlin.io.path.exists
import kotlin.io.path.writeBytes
object VersionChecker { object VersionChecker {
private var validVersions = mutableListOf<VersionUrl>() private var validVersions = mutableListOf<VersionUrl>()
private var versionData: VersionData? = null
fun downloadClient(version: String) { fun downloadClient(version: String, gradleUserHomeDir: File): Path {
fetchVersionData(version)
val clientData = fetchClientDownload(version) val clientData = fetchClientDownload(version)
// download client data // download client data
val downloadDirectory = Path(".gradle/.nonsense-gradle/net/minecraft/client/$version/") val downloadDirectory = gradleUserHomeDir.toPath().resolve("caches/nonsense-gradle/net/minecraft/client/$version/")
println("Directory: "+downloadDirectory.absolutePathString())
val downloadFile = downloadDirectory.resolve("client-$version.jar") val downloadFile = downloadDirectory.resolve("client-$version.jar")
if (downloadFile.exists()) { if (downloadFile.exists()) {
println("Client already downloaded to $downloadFile. Assuming it's valid. FIXME: Add checksum validation.") println("Client already downloaded to $downloadFile. Assuming it's valid. FIXME: Add checksum validation.")
return return downloadFile
}
if (!downloadDirectory.parent.parent.parent.parent.exists() ||
!downloadDirectory.parent.parent.parent.exists() ||
!downloadDirectory.parent.parent.exists() ||
!downloadDirectory.parent.exists() ||
!downloadDirectory.exists()
) { // .gradle/.nonsense-gradle/
println("Creating directory: ${downloadDirectory.parent.parent.parent.parent}")
downloadDirectory.parent.parent.parent.parent.createDirectories() // .gradle/.nonsense-gradle/
println("Creating directory: ${downloadDirectory.parent.parent.parent}")
downloadDirectory.parent.parent.parent.createDirectories() // .gradle/.nonsense-gradle/net/
println("Creating directory: ${downloadDirectory.parent.parent}")
downloadDirectory.parent.parent.createDirectories() // .gradle/.nonsense-gradle/net/minecraft/
println("Creating directory: ${downloadDirectory.parent}")
downloadDirectory.parent.createDirectories() // .gradle/.nonsense-gradle/net/minecraft/minecraft/
println("Creating directory: $downloadDirectory")
downloadDirectory.createDirectories() // .gradle/.nonsense-gradle/net/minecraft/minecraft/$version/
} }
downloadDirectory.createDirectories() downloadDirectory.createDirectories()
val raw = rawDownload(clientData.url) val raw = rawDownload(clientData.url)
if (!downloadFile.exists()) {
downloadFile.createFile()
}
downloadFile.writeBytes(raw) downloadFile.writeBytes(raw)
println("Downloaded client to $downloadFile") println("Downloaded client to $downloadFile")
return downloadFile
}
fun getDependencies(version: String, action: (String) -> Unit) {
fetchVersionData(version).libraries.map { it.name }.forEach {
action.invoke(it)
}
} }
fun validateVersion(version: String): Boolean { fun validateVersion(version: String): Boolean {
@ -57,10 +52,17 @@ object VersionChecker {
return validVersions.first { it.id == version }.url return validVersions.first { it.id == version }.url
} }
fun fetchClientDownload(version: String): VersionClientData { fun fetchVersionData(version: String): VersionData {
if (versionData == null || versionData!!.id != version) {
val url = getVersionUrl(version) val url = getVersionUrl(version)
val response = getUrl(url) val response = getUrl(url)
return Gson().fromJson(response, VersionData::class.java).downloads.client versionData = Gson().fromJson(response, VersionData::class.java)
}
return versionData!!
}
fun fetchClientDownload(version: String): VersionClientData {
return fetchVersionData(version).downloads.client
} }
private fun rawDownload(url: String): ByteArray { private fun rawDownload(url: String): ByteArray {
@ -109,8 +111,11 @@ private class Version(val id: String, val type: String, val url: String, val tim
private class VersionUrl(val id: String, val url: String) private class VersionUrl(val id: String, val url: String)
private class VersionData(val downloads: VersionDownloads) class VersionData(val downloads: VersionDownloads, val libraries: List<LibraryDownload>, val id: String)
private class VersionDownloads(val client: VersionClientData) class LibraryDownload(val artifact: LibraryArtifact, val name: String)
class LibraryArtifact(val path: String, val sha1: String, val size: Int, val url: String)
class VersionDownloads(val client: VersionClientData)
class VersionClientData(val sha1: String, val size: Int, val url: String) class VersionClientData(val sha1: String, val size: Int, val url: String)

View file

@ -10,10 +10,13 @@ fun Project.minecraft(version: String) {
println("Client data: ${clientData.url}") println("Client data: ${clientData.url}")
// download client data // download client data
println("Downloading client...") println("Downloading client...")
VersionChecker.downloadClient(version) val clientJar = VersionChecker.downloadClient(version, gradle.gradleUserHomeDir)
println("Downloaded client!") println("Downloaded client!")
println("Time to setup Minecraft!") println("Time to setup Minecraft!")
dependencies.add("implementation", files(absoluteProjectPath(".gradle/.nonsense-gradle/net/minecraft/client/$version/client-$version.jar"))) VersionChecker.getDependencies(version){
dependencies.add("implementation", it)
}
dependencies.add("implementation", files(clientJar))
} else { } else {
println("Invalid version! $version") println("Invalid version! $version")
error("Invalid minecraft version provided: $version") error("Invalid minecraft version provided: $version")