fix run config generation (again); add http caching for offline operation
All checks were successful
Publish to snapshot maven / build (push) Successful in 24s

This commit is contained in:
moehreag 2024-05-19 14:51:01 +02:00
parent 5af9b0eb10
commit b33e11fffa
7 changed files with 86 additions and 33 deletions

View file

@ -13,6 +13,7 @@ import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences
import java.io.PrintStream import java.io.PrintStream
import java.net.URI import java.net.URI
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.deleteExisting import kotlin.io.path.deleteExisting
import kotlin.io.path.exists import kotlin.io.path.exists
@ -21,6 +22,9 @@ class NonsenseGradlePlugin : Plugin<Project> {
override fun apply(project: Project) { override fun apply(project: Project) {
println("> Applying Nonsense Gradle Plugin") println("> Applying Nonsense Gradle Plugin")
nonsenseCacheDir = project.gradle.gradleUserHomeDir.resolve("caches/nonsense-gradle/").toPath()
nonsenseCacheDir.createDirectories()
project.apply { project.apply {
mapOf("plugin" to "java") mapOf("plugin" to "java")
mapOf("plugin" to "eclipse") mapOf("plugin" to "eclipse")
@ -37,6 +41,10 @@ class NonsenseGradlePlugin : Plugin<Project> {
it.name = "Esnesnon Snapshots" it.name = "Esnesnon Snapshots"
it.url = URI.create("https://maven-esnesnon.ecorous.org/snapshots") it.url = URI.create("https://maven-esnesnon.ecorous.org/snapshots")
} }
maven {
it.name = "FabricMC"
it.url = URI.create("https://maven.fabricmc.net")
}
maven { maven {
it.name = "Minecraft/Local" it.name = "Minecraft/Local"
it.url = project.gradle.gradleUserHomeDir.resolve("caches/nonsense-gradle/").toURI() it.url = project.gradle.gradleUserHomeDir.resolve("caches/nonsense-gradle/").toURI()
@ -119,6 +127,7 @@ class NonsenseGradlePlugin : Plugin<Project> {
} }
companion object { companion object {
lateinit var nonsenseCacheDir: Path
lateinit var minecraftVersion: String lateinit var minecraftVersion: String
lateinit var remappedGameJarPath: Path lateinit var remappedGameJarPath: Path
lateinit var parchmentVersion: String lateinit var parchmentVersion: String

View file

@ -2,11 +2,8 @@ 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 java.io.File import org.ecorous.esnesnon.gradle.common.CachingHttpClient
import java.net.URI import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.* import kotlin.io.path.*
@ -15,12 +12,12 @@ object VersionChecker {
private var validVersions = mutableListOf<VersionUrl>() private var validVersions = mutableListOf<VersionUrl>()
private var versionData: VersionData? = null private var versionData: VersionData? = null
fun downloadClient(version: String, gradleUserHomeDir: File): Path { fun downloadClient(version: String): Path {
fetchVersionData(version) fetchVersionData(version)
val clientData = fetchClientDownload(version) val clientData = fetchClientDownload(version)
// download client data // download client data
val downloadDirectory = gradleUserHomeDir.toPath().resolve("caches/nonsense-gradle/net/minecraft/client/$version/") val downloadDirectory = NonsenseGradlePlugin.nonsenseCacheDir.resolve("net/minecraft/client/$version/")
println("Directory: "+downloadDirectory.absolutePathString()) println("Directory: "+downloadDirectory.absolutePathString())
val downloadFile = downloadDirectory.resolve("client-$version.jar") val downloadFile = downloadDirectory.resolve("client-$version.jar")
if (downloadFile.exists()) { if (downloadFile.exists()) {
@ -78,31 +75,17 @@ object VersionChecker {
fun downloadAssetIndex(version: String, path: Path) { fun downloadAssetIndex(version: String, path: Path) {
val url = fetchVersionData(version).assetIndex.url val url = fetchVersionData(version).assetIndex.url
URI.create(url).toURL().openStream().use { CachingHttpClient.get(URI.create(url)).use {
Files.copy(it, path) Files.copy(it, path)
} }
} }
private fun rawDownload(url: String): ByteArray { private fun rawDownload(url: String): ByteArray {
val client = HttpClient.newHttpClient() return CachingHttpClient.getUncached(URI.create(url)).readAllBytes()
val request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build()
val response = client.send(request, HttpResponse.BodyHandlers.ofByteArray())
return response.body()
} }
private fun getUrl(url: String): String { private fun getUrl(url: String): String {
val client = HttpClient.newHttpClient() return CachingHttpClient.getString(URI.create(url))
val request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build()
val response = client.send(request, HttpResponse.BodyHandlers.ofString())
return response.body()
} }
internal fun getValidVersions() { internal fun getValidVersions() {

View file

@ -0,0 +1,48 @@
package org.ecorous.esnesnon.gradle.common
import org.ecorous.esnesnon.gradle.NonsenseGradlePlugin
import java.io.InputStream
import java.net.URI
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.util.stream.Collectors
import kotlin.io.path.createParentDirectories
import kotlin.io.path.inputStream
import kotlin.io.path.notExists
object CachingHttpClient {
fun downloadTo(uri: URI, path: Path) {
if (path.notExists()) {
Files.copy(getUncached(uri), path)
}
}
fun get(uri: URI): InputStream {
val cache = getCacheFile(uri)
if (cache.notExists()) {
getUncached(uri).use {
Files.copy(it, cache)
}
}
return cache.inputStream()
}
fun getString(uri: URI): String {
return get(uri).bufferedReader(StandardCharsets.UTF_8).lines().collect(Collectors.joining("\n"))
}
fun getUncached(uri: URI): InputStream {
return uri.toURL().openStream()
}
private fun getCacheFile(uri: URI): Path {
val path = NonsenseGradlePlugin.nonsenseCacheDir.resolve("httpCache")
.resolve(uri.toString().substring(uri.scheme.length + 2))
path.createParentDirectories()
return path
}
}

View file

@ -23,7 +23,7 @@ fun Project.minecraft(
println("Client data: ${clientData.url}") println("Client data: ${clientData.url}")
// download client data // download client data
println("Downloading client...") println("Downloading client...")
val clientJar = VersionChecker.downloadClient(version, gradle.gradleUserHomeDir) val clientJar = VersionChecker.downloadClient(version)
println("Downloaded client!") println("Downloaded client!")
val remappedJar = clientJar.resolveSibling("client-$version-remapped.jar") val remappedJar = clientJar.resolveSibling("client-$version-remapped.jar")
NonsenseGradlePlugin.remappedGameJarPath = remappedJar NonsenseGradlePlugin.remappedGameJarPath = remappedJar
@ -33,8 +33,7 @@ fun Project.minecraft(
val mapper = Mapper(ProguardParser.read(MojmapProvider.get(version).orElseThrow()).reverse()) val mapper = Mapper(ProguardParser.read(MojmapProvider.get(version).orElseThrow()).reverse())
val paramMappings = ParchmentProvider.getParchment( val paramMappings = ParchmentProvider.getParchment(
version, parchmentVersion, version, parchmentVersion,
gradle.gradleUserHomeDir.resolve("nonsense-gradle/org/parchmentmc/parchment/$version/$parchmentVersion") NonsenseGradlePlugin.nonsenseCacheDir.resolve("org/parchmentmc/parchment/$version/$parchmentVersion")
.toPath()
) )
NonsenseRemapper.remap(mapper, clientJar, remappedJar, true, paramMappings) NonsenseRemapper.remap(mapper, clientJar, remappedJar, true, paramMappings)
} }
@ -56,5 +55,5 @@ fun Project.minecraft(
fun Project.loader(version: String){ fun Project.loader(version: String){
dependencies.add("implementation", "org.ecorous.esnesnon:nonsense-loader:$version") dependencies.add("implementation", "org.ecorous.esnesnon:nonsense-loader:$version")
dependencies.add("annotationProcessor", "net.fabricmc:sponge-mixin:0.13.4-mixin.0.8.5") dependencies.add("annotationProcessor", "net.fabricmc:sponge-mixin:0.13.4+mixin.0.8.5")
} }

View file

@ -13,7 +13,7 @@ import kotlin.io.path.notExists
object RunConfigGenerator { object RunConfigGenerator {
private const val LOG4J_CONFIG_PATH = ".gradle/nonsense/log4j.xml" private const val LOG4J_CONFIG_PATH = ".gradle/nonsense/log4j.xml"
private const val ASSET_DIR = "caches/nonsense-gradle/assets" private const val ASSET_DIR = "assets"
private val ADAPTERS = arrayOf(EclipseAdapter(), IdeaAdapter()) private val ADAPTERS = arrayOf(EclipseAdapter(), IdeaAdapter())
fun generate(project: Project) { fun generate(project: Project) {
@ -25,7 +25,7 @@ object RunConfigGenerator {
} }
} }
val assetPath = project.gradle.gradleUserHomeDir.resolve(ASSET_DIR).toPath().absolute() val assetPath = NonsenseGradlePlugin.nonsenseCacheDir.resolve(ASSET_DIR).absolute()
val assetIndexPath = assetPath.resolve("indexes").resolve(NonsenseGradlePlugin.minecraftVersion + ".json") val assetIndexPath = assetPath.resolve("indexes").resolve(NonsenseGradlePlugin.minecraftVersion + ".json")
if (assetIndexPath.notExists()) { if (assetIndexPath.notExists()) {
assetIndexPath.createParentDirectories() assetIndexPath.createParentDirectories()

View file

@ -36,7 +36,7 @@ class EclipseAdapter : RunConfigAdapter {
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="${formatXmlList(programArgs)}"/> <stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="${formatXmlList(programArgs)}"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="${sanitizeXmlValue(projectName)}"/> <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="${sanitizeXmlValue(projectName)}"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="${formatXmlList(vmArgs)}"/> <stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="${formatXmlList(vmArgs)}"/>
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${'$'}{workspace_loc:${sanitizeXmlValue(projectName)}}/%RUN_DIRECTORY%"/> <stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${'$'}{workspace_loc:${sanitizeXmlValue(projectName)}}/run"/>
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="true"/> <booleanAttribute key="org.eclipse.jdt.launching.ATTR_ATTR_USE_ARGFILE" value="true"/>
</launchConfiguration> </launchConfiguration>
""".trimIndent() """.trimIndent()

View file

@ -4,6 +4,8 @@ import org.ecorous.esnesnon.gradle.common.formatXmlList
import org.ecorous.esnesnon.gradle.common.sanitizeXmlValue import org.ecorous.esnesnon.gradle.common.sanitizeXmlValue
import org.ecorous.esnesnon.gradle.run.RunConfigAdapter import org.ecorous.esnesnon.gradle.run.RunConfigAdapter
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.tasks.SourceSet
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import kotlin.io.path.createDirectories import kotlin.io.path.createDirectories
import kotlin.io.path.isDirectory import kotlin.io.path.isDirectory
@ -23,16 +25,28 @@ class IdeaAdapter : RunConfigAdapter {
val file = folder.resolve("$name.xml") val file = folder.resolve("$name.xml")
var module = project.name + "." + project.extensions.getByType(JavaPluginExtension::class.java).sourceSets.getByName(
SourceSet.MAIN_SOURCE_SET_NAME
).name
var parent: Project? = project
while ((parent?.parent.also { parent = it }) != null) {
module = parent!!.name + "." + module
}
val runDir = project.projectDir.resolve("run")
runDir.mkdirs()
file.writer(StandardCharsets.UTF_8).use { file.writer(StandardCharsets.UTF_8).use {
it.write( it.write(
""" """
<component name="ProjectRunConfigurationManager"> <component name="ProjectRunConfigurationManager">
<configuration default="false" name="%NAME%" type="Application" factoryName="Application"> <configuration default="false" name="$name" type="Application" factoryName="Application">
<option name="MAIN_CLASS_NAME" value="${sanitizeXmlValue(mainClass)}"/> <option name="MAIN_CLASS_NAME" value="${sanitizeXmlValue(mainClass)}"/>
<module name="%IDEA_MODULE%"/> <module name="${module.replace(" ", "_")}"/>
<option name="PROGRAM_PARAMETERS" value="${formatXmlList(programArgs)}"/> <option name="PROGRAM_PARAMETERS" value="${formatXmlList(programArgs)}"/>
<option name="VM_PARAMETERS" value="${formatXmlList(vmArgs)}"/> <option name="VM_PARAMETERS" value="${formatXmlList(vmArgs)}"/>
<option name="WORKING_DIRECTORY" value="${'$'}PROJECT_DIR${'$'}/%RUN_DIRECTORY%/"/> <option name="WORKING_DIRECTORY" value="$runDir"/>
<method v="2"> <method v="2">
<option name="Make" enabled="true"/> <option name="Make" enabled="true"/>
</method> </method>