From dad6b3c1b61c7d15679fa84fce69342f4224ae0f Mon Sep 17 00:00:00 2001 From: deirn Date: Mon, 23 Sep 2024 20:59:45 +0700 Subject: [PATCH] neoforge support --- build.gradle.kts | 4 +- .../lol/bai/explosion/gradle/ExplosionExt.kt | 1 + .../explosion/gradle/internal/Explosion.kt | 1 + .../explosion/gradle/internal/ResolverTask.kt | 2 + explosion-resolver-neoforge/build.gradle.kts | 42 +++++++++++++ .../resolver/neoforge/FakeLaunchContext.kt | 55 ++++++++++++++++ .../bai/explosion/resolver/neoforge/Main.kt | 62 +++++++++++++++++++ explosion-test-neoforge/build.gradle | 21 +++++++ explosion-test-neoforge/settings.gradle | 9 +++ settings.gradle.kts | 1 + 10 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 explosion-resolver-neoforge/build.gradle.kts create mode 100644 explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/FakeLaunchContext.kt create mode 100644 explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/Main.kt create mode 100644 explosion-test-neoforge/build.gradle create mode 100644 explosion-test-neoforge/settings.gradle diff --git a/build.gradle.kts b/build.gradle.kts index d08b320..832ad1d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,13 +2,13 @@ plugins { `maven-publish` kotlin("jvm") version "1.9.22" - id("com.github.johnrengelman.shadow") version "8.1.1" + id("com.gradleup.shadow") version "8.3.2" } allprojects { apply(plugin = "maven-publish") apply(plugin = "org.jetbrains.kotlin.jvm") - apply(plugin = "com.github.johnrengelman.shadow") + apply(plugin = "com.gradleup.shadow") version = providers.environmentVariable("VERSION").getOrElse("9999-local") group = "lol.bai.explosion" diff --git a/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/ExplosionExt.kt b/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/ExplosionExt.kt index 14b3a7f..e761113 100644 --- a/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/ExplosionExt.kt +++ b/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/ExplosionExt.kt @@ -4,5 +4,6 @@ interface ExplosionExt { val fabric: ExplosionPlatform val forge: ExplosionPlatform + val neoforge: ExplosionPlatform } \ No newline at end of file diff --git a/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/Explosion.kt b/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/Explosion.kt index 12b5c93..8f05303 100644 --- a/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/Explosion.kt +++ b/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/Explosion.kt @@ -10,5 +10,6 @@ open class Explosion(project: Project, outputDir: Path) : ExplosionExt { override val fabric = Platform(project, "fabric", outputDir, null) override val forge = Platform(project, "forge", outputDir, null) + override val neoforge = Platform(project, "neoforge", outputDir, null) } \ No newline at end of file diff --git a/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/ResolverTask.kt b/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/ResolverTask.kt index dd7e1e7..fbceb16 100644 --- a/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/ResolverTask.kt +++ b/explosion-gradle-plugin/src/main/kotlin/lol/bai/explosion/gradle/internal/ResolverTask.kt @@ -27,6 +27,8 @@ abstract class ResolverTask : JavaExec() { outputDir.asFile.get().absolutePath, ) + jvmArgs("--add-opens=java.base/java.lang.invoke=ALL-UNNAMED") + super.exec() } diff --git a/explosion-resolver-neoforge/build.gradle.kts b/explosion-resolver-neoforge/build.gradle.kts new file mode 100644 index 0000000..d9ce8c7 --- /dev/null +++ b/explosion-resolver-neoforge/build.gradle.kts @@ -0,0 +1,42 @@ +kotlin { + jvmToolchain(21) +} + +repositories { + maven("https://maven.neoforged.net/releases/") + maven("https://libraries.minecraft.net/") +} + +dependencies { + shade("net.neoforged.fancymodloader:loader:4.0.29") { isTransitive = false } + + implementation("com.google.guava:guava:32.1.2-jre") + implementation("org.apache.maven:maven-artifact:3.8.1") + implementation("com.electronwill.night-config:core:3.7.3") + implementation("com.electronwill.night-config:toml:3.7.3") + implementation("org.apache.logging.log4j:log4j-api:2.22.1") + implementation("org.apache.logging.log4j:log4j-core:2.22.1") + implementation("org.slf4j:slf4j-api:2.0.16") + implementation("com.google.code.gson:gson:2.11.0") + + implementation("com.mojang:logging:1.2.7") { isTransitive = false } + implementation("cpw.mods:modlauncher:11.0.3") { isTransitive = false } + implementation("cpw.mods:securejarhandler:3.0.7") { isTransitive = false } + implementation("net.neoforged:JarJarFileSystems:0.4.1") { isTransitive = false } + implementation("net.neoforged:JarJarMetadata:0.4.1") { isTransitive = false } + implementation("net.neoforged:JarJarSelector:0.4.1") { isTransitive = false } + implementation("net.neoforged:mergetool:2.0.0:api") +} + +tasks.shadowJar { + exclude("log4j2*") + exclude("META-INF/org/apache/logging/**") +} + +publishing { + publications { + create("maven") { + from(components["java"]) + } + } +} \ No newline at end of file diff --git a/explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/FakeLaunchContext.kt b/explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/FakeLaunchContext.kt new file mode 100644 index 0000000..ebe4bd5 --- /dev/null +++ b/explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/FakeLaunchContext.kt @@ -0,0 +1,55 @@ +package lol.bai.explosion.resolver.neoforge + +import net.neoforged.fml.loading.moddiscovery.locators.JarInJarDependencyLocator +import net.neoforged.fml.loading.moddiscovery.locators.ModsFolderLocator +import net.neoforged.fml.loading.moddiscovery.readers.JarModsDotTomlModFileReader +import net.neoforged.fml.loading.moddiscovery.readers.NestedLibraryModReader +import net.neoforged.neoforgespi.ILaunchContext +import net.neoforged.neoforgespi.locating.IDependencyLocator +import net.neoforged.neoforgespi.locating.IModFileCandidateLocator +import net.neoforged.neoforgespi.locating.IModFileReader +import java.nio.file.Path +import java.util.* +import java.util.stream.Stream + +class FakeLaunchContext( + private val modsFolder: Path +) : ILaunchContext { + @Suppress("NOTHING_TO_INLINE") + private inline fun unsupported(): Nothing { + throw UnsupportedOperationException() + } + + override fun environment() = unsupported() + override fun modLists() = unsupported() + override fun mods() = unsupported() + override fun mavenRoots() = unsupported() + + override fun isLocated(p0: Path) = false + override fun addLocated(p0: Path) = true + + override fun loadServices(service: Class): Stream> { + return when (service) { + IModFileCandidateLocator::class.java -> Stream.of( + Fake(ModsFolderLocator(modsFolder, "explosion!!!")) + ) + + IModFileReader::class.java -> Stream.of( + Fake(JarModsDotTomlModFileReader()), + Fake(NestedLibraryModReader()) + ) + + IDependencyLocator::class.java -> Stream.of( + Fake(JarInJarDependencyLocator()) + ) + + else -> unsupported() + } + } +} + +@Suppress("UNCHECKED_CAST") +private class Fake(val instance: Any) : ServiceLoader.Provider { + override fun get() = instance as T + override fun type() = instance::class.java as Class +} \ No newline at end of file diff --git a/explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/Main.kt b/explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/Main.kt new file mode 100644 index 0000000..f0dd504 --- /dev/null +++ b/explosion-resolver-neoforge/src/main/kotlin/lol/bai/explosion/resolver/neoforge/Main.kt @@ -0,0 +1,62 @@ +package lol.bai.explosion.resolver.neoforge + +import net.neoforged.fml.loading.ImmediateWindowHandler +import net.neoforged.fml.loading.moddiscovery.ModDiscoverer +import net.neoforged.fml.loading.moddiscovery.ModFile +import net.neoforged.fml.loading.moddiscovery.ModValidator +import net.neoforged.fml.loading.progress.ProgressMeter +import net.neoforged.fml.loading.progress.StartupNotificationManager +import java.lang.invoke.MethodHandles +import java.nio.file.Files +import java.nio.file.StandardCopyOption +import kotlin.io.path.Path +import kotlin.io.path.inputStream +import kotlin.io.path.name +import kotlin.io.path.writeText + +@Suppress("UnstableApiUsage") +fun main(args: Array) { + val (inputDirStr, outputDirStr) = args + val inputDir = Path(inputDirStr) + val outputDir = Path(outputDirStr) + + @Suppress("LocalVariableName") + run { + val ImmediateWindowHandler = ImmediateWindowHandler::class.java + val ProgressMeter = ProgressMeter::class.java + + MethodHandles + .privateLookupIn(ImmediateWindowHandler, MethodHandles.lookup()) + .findStaticVarHandle(ImmediateWindowHandler, "earlyProgress", ProgressMeter) + .set(StartupNotificationManager.addProgressBar("explosion!!!", 0)) + } + + val discoverer = ModDiscoverer(FakeLaunchContext(inputDir)) + val validator = discoverer.discoverMods() + + @Suppress("LocalVariableName", "UNCHECKED_CAST") + val candidateMods = validator.run { + val ModValidator = ModValidator::class.java + val List = List::class.java + + MethodHandles + .privateLookupIn(ModValidator, MethodHandles.lookup()) + .findVarHandle(ModValidator, "candidateMods", List) + .get(this) as List + } + + val meta = StringBuilder() + for (modFile in candidateMods) { + val mod = modFile.modInfos.firstOrNull() ?: continue + val path = outputDir.resolve("${mod.modId}-${mod.version}") + Files.copy(modFile.filePath.inputStream(), path, StandardCopyOption.REPLACE_EXISTING) + meta.append(path.name) + .append("\t") + .append(mod.modId) + .append("\t") + .append(mod.version) + .append("\n") + } + + outputDir.resolve("__meta.txt").writeText(meta.toString()) +} \ No newline at end of file diff --git a/explosion-test-neoforge/build.gradle b/explosion-test-neoforge/build.gradle new file mode 100644 index 0000000..2c5cc80 --- /dev/null +++ b/explosion-test-neoforge/build.gradle @@ -0,0 +1,21 @@ +plugins { + id 'net.neoforged.moddev' version '1.0.17' + id 'lol.bai.explosion' +} + +neoForge { + version = "21.1.58" +} + +repositories { + maven { + url "https://cursemaven.com" + content { + includeGroup "curse.maven" + } + } +} + +dependencies { + implementation explosion.neoforge("curse.maven:ender-io-64578:5720393") +} diff --git a/explosion-test-neoforge/settings.gradle b/explosion-test-neoforge/settings.gradle new file mode 100644 index 0000000..662edc9 --- /dev/null +++ b/explosion-test-neoforge/settings.gradle @@ -0,0 +1,9 @@ +pluginManagement { + repositories { + mavenLocal() + gradlePluginPortal() + maven { url = 'https://maven.neoforged.net/releases' } + } +} + +includeBuild ".." \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index b6012ff..67e8116 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -10,3 +10,4 @@ rootProject.name = "explosion" include("explosion-gradle-plugin") include("explosion-resolver-fabric") include("explosion-resolver-forge") +include("explosion-resolver-neoforge")