Skip to content

Commit

Permalink
neoforge support
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Sep 23, 2024
1 parent a9b3321 commit dad6b3c
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 2 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ interface ExplosionExt {

val fabric: ExplosionPlatform
val forge: ExplosionPlatform
val neoforge: ExplosionPlatform

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ abstract class ResolverTask : JavaExec() {
outputDir.asFile.get().absolutePath,
)

jvmArgs("--add-opens=java.base/java.lang.invoke=ALL-UNNAMED")

super.exec()
}

Expand Down
42 changes: 42 additions & 0 deletions explosion-resolver-neoforge/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<MavenPublication>("maven") {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -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 <T : Any?> loadServices(service: Class<T>): Stream<ServiceLoader.Provider<T>> {
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<T>(val instance: Any) : ServiceLoader.Provider<T> {
override fun get() = instance as T
override fun type() = instance::class.java as Class<T>
}
Original file line number Diff line number Diff line change
@@ -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<String>) {
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<ModFile>
}

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())
}
21 changes: 21 additions & 0 deletions explosion-test-neoforge/build.gradle
Original file line number Diff line number Diff line change
@@ -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")
}
9 changes: 9 additions & 0 deletions explosion-test-neoforge/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
maven { url = 'https://maven.neoforged.net/releases' }
}
}

includeBuild ".."
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ rootProject.name = "explosion"
include("explosion-gradle-plugin")
include("explosion-resolver-fabric")
include("explosion-resolver-forge")
include("explosion-resolver-neoforge")

0 comments on commit dad6b3c

Please sign in to comment.