add runGame task
Some checks failed
Publish to snapshot maven / build (push) Failing after 17s

This commit is contained in:
moehreag 2024-05-20 02:42:55 +02:00
parent efff2e71d3
commit 44433be9b7
2 changed files with 134 additions and 2 deletions

View file

@ -1,8 +1,10 @@
package org.ecorous.esnesnon.gradle
import net.fabricmc.fernflower.api.IFabricJavadocProvider
import org.ecorous.esnesnon.gradle.common.Env
import org.ecorous.esnesnon.gradle.run.AssetDownloader
import org.ecorous.esnesnon.gradle.run.RunConfigGenerator
import org.ecorous.esnesnon.gradle.run.task.RunGameTask
import org.ecorous.esnesnon.gradle.vineflower.ParchmentJavadocProvider
import org.ecorous.esnesnon.nonsense_remapper.provider.ParchmentProvider
import org.gradle.api.Plugin
@ -101,13 +103,17 @@ class NonsenseGradlePlugin : Plugin<Project> {
}
project.task("genRunConfigs").apply {
group = "nonsense"
doFirst {
group = "nonsense"
RunConfigGenerator.generate(project)
}
}
project.tasks.register("runClient", RunGameTask::class.java, Env.CLIENT)
project.tasks.register("runServer", RunGameTask::class.java, Env.SERVER)
project.task("downloadAssets").apply {
group = "nonsense"
doFirst {
AssetDownloader.download()
}

View file

@ -0,0 +1,126 @@
package org.ecorous.esnesnon.gradle.run.task
import org.ecorous.esnesnon.gradle.NonsenseGradlePlugin
import org.ecorous.esnesnon.gradle.VersionChecker
import org.ecorous.esnesnon.gradle.common.Env
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileCollection
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.tasks.JavaExec
import org.gradle.api.tasks.SourceSet
import org.gradle.process.CommandLineArgumentProvider
import org.jetbrains.annotations.NonNls
import java.io.File
import java.nio.file.Files
import javax.inject.Inject
import kotlin.io.path.*
abstract class RunGameTask @Inject constructor(env: Env) : JavaExec() {
private val classpath: ConfigurableFileCollection = project.objects.fileCollection()
init {
group = "nonsense"
val assetPath = NonsenseGradlePlugin.nonsenseCacheDir.resolve("assets").absolute()
val assetIndexPath = assetPath.resolve("indexes")
if (assetIndexPath.notExists()) {
assetIndexPath.createDirectories()
}
val indexId = VersionChecker.downloadAssetIndex(NonsenseGradlePlugin.minecraftVersion, assetIndexPath)
val log4jPath = project.rootDir.resolve(".gradle/nonsense-gradle/log4j.xml").toPath().absolute()
if (log4jPath.notExists()) {
log4jPath.createParentDirectories()
this::class.java.getResourceAsStream("/log4j.xml").use { input ->
Files.copy(input!!, log4jPath)
}
}
argumentProviders.add(
CommandLineArgumentProvider {
listOf(
"--assetsDir", assetPath.toString(),
"--version", "Nonsense",
"--assetIndex", indexId,
"--accessToken", "0"
)
}
)
classpath.from(project.extensions.getByType(JavaPluginExtension::class.java).sourceSets.getByName(
SourceSet.MAIN_SOURCE_SET_NAME
).runtimeClasspath.filter { it.exists() })
jvmArguments.addAll(
"-Dnonsense.development=true",
"-Dnonsense.plugin.minecraft.gameJar=${NonsenseGradlePlugin.remappedGameJarPath}",
"-Dlog4j.configurationFile=$log4jPath",
writeArgFile()
)
mainClass.set("org.ecorous.esnesnon.nonsense.loader.impl.launch.${env.id}.Nonsense${env.pascalName}")
}
private fun writeArgFile(): String {
val file = Files.createTempFile("nonsense-classpath", ".args")
val content = "-classpath " + classpath.files.stream()
.map { it.absolutePath }
.map { quoteArg(it) }.toList()
.joinToString(File.pathSeparator)
file.writeText(content)
return "@" + file.toAbsolutePath()
}
private fun quoteArg(arg: String): String {
val specials = " #'\"\n\r\t\u000c"
var contains = false
specials.toCharArray().forEach {
if (arg.toCharArray().contains(it)) {
contains = true
}
}
if (!contains) {
return arg
}
val sb: @NonNls StringBuilder = StringBuilder(arg.length * 2)
for (element in arg) {
when (element) {
' ', '#', '\'' -> sb.append('"').append(element).append('"')
'"' -> sb.append("\"\\\"\"")
'\n' -> sb.append("\"\\n\"")
'\r' -> sb.append("\"\\r\"")
'\t' -> sb.append("\"\\t\"")
'\u000c' -> sb.append("\"\\f\"")
else -> sb.append(element)
}
}
return sb.toString()
}
override fun getClasspath(): FileCollection {
return classpath
}
override fun exec() {
super.setClasspath(project.files())
workingDir = project.projectDir.resolve("run")
super.exec()
}
override fun setWorkingDir(dir: File) {
if (!dir.exists()) {
dir.mkdirs()
}
super.setWorkingDir(dir)
}
override fun classpath(vararg paths: Any?): JavaExec {
this.classpath.from(paths)
return this
}
override fun setClasspath(classpath: FileCollection): JavaExec {
this.classpath.setFrom(classpath)
return this
}
}