Compare commits

...

2 commits

Author SHA1 Message Date
moehreag e7974cd487 update plugin, add game remapping 2024-05-13 13:21:37 +02:00
moehreag 1e4b950c30 add minecraft dependency injection 2024-05-13 11:27:31 +02:00
6 changed files with 72 additions and 40 deletions

View file

@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -9,9 +9,12 @@ version = "1.0.0"
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
//implementation(files("mojmap-patcher-1.0.0-SNAPSHOT.jar"))
implementation("org.ecorous.esnesnon:mojmap-patcher:1.0.0-SNAPSHOT")
implementation("com.google.code.gson:gson:2.10.1")
testImplementation(kotlin("test"))
}
@ -29,14 +32,14 @@ tasks.test {
useJUnitPlatform()
}
kotlin {
jvmToolchain(17)
jvmToolchain(21)
}
publishing {
publications {
repositories {
mavenLocal {
url = uri("file://home/ecorous/wawa/test-repo")
}
}
}

View file

@ -1,5 +1,5 @@
plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}
rootProject.name = "nonsense-gradle"

View file

@ -2,22 +2,37 @@ package org.ecorous.esnesnon.gradle
import org.gradle.api.Plugin
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> {
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) {
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")
buildTask.dependsOn("setupNonsense")
project.dependencies.apply {
add("implementation", "com.electronwill.night-config:toml:3.6.0")
// create("com.electronwill.night-config:toml:3.6.0")
toInject.forEach {
add("implementation", it)
}
}
project.task("setupNonsense").apply {
group = "nonsense"
doFirst {

View file

@ -2,49 +2,44 @@ package org.ecorous.esnesnon.gradle
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import org.gradle.api.Project
import java.io.File
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
import kotlin.io.path.writeBytes
import java.nio.file.Path
import kotlin.io.path.*
object VersionChecker {
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)
// 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")
if (downloadFile.exists()) {
println("Client already downloaded to $downloadFile. Assuming it's valid. FIXME: Add checksum validation.")
return
}
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/
return downloadFile
}
downloadDirectory.createDirectories()
val raw = rawDownload(clientData.url)
if (!downloadFile.exists()) {
downloadFile.createFile()
}
downloadFile.writeBytes(raw)
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 {
@ -57,10 +52,17 @@ object VersionChecker {
return validVersions.first { it.id == version }.url
}
fun fetchVersionData(version: String): VersionData {
if (versionData == null || versionData!!.id != version) {
val url = getVersionUrl(version)
val response = getUrl(url)
versionData = Gson().fromJson(response, VersionData::class.java)
}
return versionData!!
}
fun fetchClientDownload(version: String): VersionClientData {
val url = getVersionUrl(version)
val response = getUrl(url)
return Gson().fromJson(response, VersionData::class.java).downloads.client
return fetchVersionData(version).downloads.client
}
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 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)

View file

@ -1,7 +1,9 @@
package org.ecorous.esnesnon.gradle.ext
import org.ecorous.esnesnon.gradle.VersionChecker
import org.ecorous.esnesnon.mojmap_patcher.MojMapPatcher
import org.gradle.api.Project
import kotlin.io.path.notExists
fun Project.minecraft(version: String) {
if (VersionChecker.validateVersion(version)) {
@ -10,10 +12,17 @@ fun Project.minecraft(version: String) {
println("Client data: ${clientData.url}")
// download client data
println("Downloading client...")
VersionChecker.downloadClient(version)
val clientJar = VersionChecker.downloadClient(version, gradle.gradleUserHomeDir)
println("Downloaded client!")
val remappedJar = clientJar.resolveSibling("client-$version-remapped.jar")
println("Time to setup Minecraft!")
dependencies.add("implementation", files(absoluteProjectPath(".gradle/.nonsense-gradle/net/minecraft/client/$version/client-$version.jar")))
if (remappedJar.notExists()) {
MojMapPatcher.run(version, clientJar, remappedJar)
}
VersionChecker.getDependencies(version){
dependencies.add("implementation", it)
}
dependencies.add("implementation", files(remappedJar))
} else {
println("Invalid version! $version")
error("Invalid minecraft version provided: $version")