diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index a5e8bb4..011fe07 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -58,7 +58,7 @@ jobs: run: ./gradlew --build-cache --info --stacktrace "${WORKSPACE_TASK}" - name: Compile the mod - run: ./gradlew --build-cache --info --stacktrace assemble + run: ./gradlew --build-cache --info --stacktrace assemble -PnoSourceInjection=true - name: Attach compilation artifacts uses: actions/upload-artifact@v4 @@ -69,7 +69,7 @@ jobs: - name: Run post-build checks id: build_mod - run: xvfb-run --server-args="-screen 0 1366x768x24" ./gradlew --build-cache --info --stacktrace build + run: xvfb-run --server-args="-screen 0 1366x768x24" ./gradlew --build-cache --info --stacktrace build -PnoSourceInjection=true - name: Attach gradle reports if: failure() && steps.build_mod.conclusion == 'failure' @@ -116,7 +116,7 @@ jobs: echo "eula=true" > run/eula.txt echo "level-seed=-6202107849386030209\nonline-mode=true\n" > run/server.properties echo "stop" > run/stop.txt - timeout "${RUN_SERVER_TIMEOUT}" ./gradlew --build-cache --info --stacktrace runServer 2>&1 < run/stop.txt | tee -a server.log || true + timeout "${RUN_SERVER_TIMEOUT}" ./gradlew --build-cache --info --stacktrace runServer -PnoSourceInjection=true 2>&1 < run/stop.txt | tee -a server.log || true - name: Test no errors reported during server run if: ${{ env.RUN_SERVER_ENABLED == 'true' }} diff --git a/.github/workflows/javadoc.yml b/.github/workflows/javadoc.yml index 86afda0..70f3619 100644 --- a/.github/workflows/javadoc.yml +++ b/.github/workflows/javadoc.yml @@ -182,6 +182,7 @@ jobs: - name: Generate Javadoc run: | ./gradlew javadoc \ + -PnoSourceInjection=true \ -PcnpcDocUrl=${{ steps.cnpc_docs.outputs.url }} \ -PcnpcDocElementList=build/cnpc-element-list diff --git a/CustomNPC-Plus b/CustomNPC-Plus index c6eadbd..5acb371 160000 --- a/CustomNPC-Plus +++ b/CustomNPC-Plus @@ -1 +1 @@ -Subproject commit c6eadbd19398c870240faebd7af4d0cf8fb86d74 +Subproject commit 5acb371fab5142251f82e46e81deb606da797312 diff --git a/build.gradle b/build.gradle index e170fbc..3c9a665 100644 --- a/build.gradle +++ b/build.gradle @@ -7,6 +7,30 @@ plugins { apply from: 'gradle.submodules.gradle' +// Source injection: merges submodule source for IDE hotswap during runClient/runServer. +// Output JAR contains only addon classes. Disable with: -PnoSourceInjection=true +if (!(project.hasProperty("noSourceInjection") && project.noSourceInjection == 'true')) { + ext.sourceInjections = [ + [ + path: 'CustomNPC-Plus', + src: ['src/api/java', 'src/main/java'], + resources: ['src/main/resources'], + compileOnlyFiles: ['tools/standalone-JaninoLoader-1.0.2.jar'], + mixinConfig: 'mixins.customnpcs.json', + ], + [ + path: 'GeckoLib', + src: ['src/main/java'], + resources: ['src/main/resources'], + compileOnlyDeps: [ + 'com.fasterxml.jackson.core:jackson-databind:2.9.0', + 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.0', + ], + ], + ] + apply from: 'gradle.sourceInjection.gradle' +} + version = "1.4-pre" group = "com.goodbird.npcgecko" diff --git a/gradle.properties b/gradle.properties index 6e726ac..7a354b6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -65,7 +65,7 @@ apiPackage = # Specify the configuration file for Forge's access transformers here. It must be placed into /src/main/resources/META-INF/ # There can be multiple files in a space-separated list. # Example value: mymodid_at.cfg nei_at.cfg -accessTransformersFile = npcgecko_at.cfg +accessTransformersFile = npcgecko_at.cfg ../../../../CustomNPC-Plus/src/main/resources/META-INF/customnpcs_at.cfg ../../../../GeckoLib/src/main/resources/META-INF/geckolib_at.cfg # Provides setup for Mixins if enabled. If you don't know what mixins are: Keep it disabled! usesMixins = true diff --git a/gradle.sourceInjection.gradle b/gradle.sourceInjection.gradle new file mode 100644 index 0000000..ec6bad8 --- /dev/null +++ b/gradle.sourceInjection.gradle @@ -0,0 +1,267 @@ +// ============================================================================ +// Reusable Source Injection System +// ============================================================================ +// +// Merges submodule source into compilation for IDE hotswap during dev runs. +// Output JAR contains ONLY the addon's own code. +// +// HOW IT WORKS: +// For each file in the JAR, we check if its source exists in any injected +// directory. If it does, the file came from a submodule and is excluded. +// No hardcoded package names — works with ANY package structure. +// +// DEV JAR vs RELEASE JAR: +// Both exclude ALL injected code. Each submodule's own dev JAR (built by +// gradle.submodules.gradle) serves as the FML mod container so each @Mod +// is discovered in its own JAR. Hotswap works via build/classes/java/main/. +// +// USAGE (in build.gradle): +// +// ext.sourceInjections = [ +// [ +// path: 'CustomNPC-Plus', +// src: ['src/api/java', 'src/main/java'], +// resources: ['src/main/resources'], +// compileOnlyFiles: ['tools/standalone-JaninoLoader-1.0.2.jar'], +// compileOnlyDeps: [], +// mixinConfig: 'mixins.customnpcs.json', +// ], +// ] +// apply from: 'gradle.sourceInjection.gradle' +// +// Disable with: -PnoSourceInjection=true (CI uses JAR-only builds) +// ============================================================================ + +import groovy.json.JsonSlurper +import groovy.json.JsonOutput +import java.util.regex.Pattern +import java.util.regex.Matcher + +if (!project.hasProperty('sourceInjections') || sourceInjections.isEmpty()) { + throw new GradleException("ext.sourceInjections must be defined before applying gradle.sourceInjection.gradle") +} + +println "Source injection enabled for: ${sourceInjections.collect { it.path }.join(', ')}" + +// ============================================================================ +// 1. Merge submodule source directories into compilation +// ============================================================================ +sourceInjections.each { injection -> + sourceSets.main.java.srcDirs += injection.src.collect { "${injection.path}/${it}" } + if (injection.resources) { + sourceSets.main.resources.srcDirs += injection.resources.collect { "${injection.path}/${it}" } + } +} + +// ============================================================================ +// 2. Add dev dependencies needed by submodule source +// ============================================================================ +// Uses devOnlyNonPublishable: available at compile + runtime, NOT in output JAR. +// Needed because source-injected code may reference deps that are shadowed/relocated +// in the submodule's own JAR (e.g., jackson relocated in GeckoLib's shadow build). +sourceInjections.each { injection -> + injection.compileOnlyFiles?.each { dep -> + dependencies.add('devOnlyNonPublishable', files("${injection.path}/${dep}")) + } + injection.compileOnlyDeps?.each { dep -> + dependencies.add('devOnlyNonPublishable', dep) + } +} + +// ============================================================================ +// 3. Merge mcmod.info from all mods for dev runs +// ============================================================================ +tasks.register("mergeMcmodInfo") { + doLast { + def mergedModList = [] + def allResourceDirs = [file("src/main/resources")] + + sourceInjections.each { injection -> + injection.resources?.each { res -> + allResourceDirs.add(file("${injection.path}/${res}")) + } + } + + allResourceDirs.each { resDir -> + def mcmodFile = new File(resDir, "mcmod.info") + if (mcmodFile.exists()) { + // Resolve ${property} placeholders from submodule's gradle.properties + def propsFile = resDir.toPath().resolve("../../../gradle.properties").normalize().toFile() + def props = [:] + if (propsFile.exists()) { + propsFile.withReader("UTF-8") { reader -> + new Properties().with { p -> + p.load(reader) + p.each { k, v -> props[k] = v } + } + } + } + + def text = mcmodFile.text + props.each { key, value -> + text = Pattern.compile('\\$\\{' + Pattern.quote(key) + '\\}') + .matcher(text).replaceAll(Matcher.quoteReplacement(value)) + } + + def json = new JsonSlurper().parseText(text) + // Handle both mcmod.info formats: + // Old format: [{modid: "...", ...}] (plain array) + // New format: {modListVersion: 2, modList: [{...}]} + if (json instanceof List) { + mergedModList.addAll(json) + } else if (json.modList) { + mergedModList.addAll(json.modList) + } + } + } + + def outputDir = file("$buildDir/generatedResources/main") + outputDir.mkdirs() + new File(outputDir, "mcmod.info").text = JsonOutput.prettyPrint( + JsonOutput.toJson([modListVersion: 2, modList: mergedModList]) + ) + println "Merged ${mergedModList.size()} mod entries for dev runtime" + } +} + +tasks.named("processResources") { + dependsOn tasks.named("mergeMcmodInfo") + from(sourceSets.main.resources.srcDirs) { exclude "mcmod.info" } + from("$buildDir/generatedResources/main") { include "mcmod.info" } + duplicatesStrategy = DuplicatesStrategy.EXCLUDE +} + +project.afterEvaluate { + tasks.named("processResources") { + doLast { + def gen = file("$buildDir/generatedResources/main/mcmod.info") + def target = file("$buildDir/resources/main/mcmod.info") + if (gen.exists()) target.text = gen.text + } + } +} + +// ============================================================================ +// 4. JAR filtering helpers +// ============================================================================ +// Pre-resolve all injected source/resource directories for performance. +def injectedSrcDirs = sourceInjections.collectMany { inj -> + (inj.src ?: []).collect { file("${inj.path}/${it}") }.findAll { it.exists() } +} +def injectedResDirs = sourceInjections.collectMany { inj -> + (inj.resources ?: []).collect { file("${inj.path}/${it}") }.findAll { it.exists() } +} + +// Check if a .class file originated from one of the given source directories. +// Handles inner classes (Foo$Bar.class → Foo.java) and package-private classes +// (classes defined in another file within the same package). +def isInjectedClass = { String classPath -> + // Inner classes: com/foo/Bar$Baz.class → com/foo/Bar.java + def javaPath = classPath.replaceFirst('\\$[^/]*\\.class$', '.java') + if (javaPath == classPath) javaPath = classPath.replace('.class', '.java') + + // Direct match: .java file exists in an injected source directory + if (injectedSrcDirs.any { new File(it, javaPath).exists() }) return true + + // Package-private classes: multiple top-level classes in one .java file + // If the package directory exists in an injected src dir with .java files, + // this class came from that submodule. + def pkgDir = javaPath.contains('/') ? javaPath.substring(0, javaPath.lastIndexOf('/')) : '' + if (pkgDir) { + return injectedSrcDirs.any { srcDir -> + def dir = new File(srcDir, pkgDir) + dir.isDirectory() && dir.listFiles()?.any { it.name.endsWith('.java') } + } + } + + return false +} + +def isInjectedResource = { String resPath -> + injectedResDirs.any { new File(it, resPath).exists() } +} + +// ============================================================================ +// 5. Dev JAR: exclude ALL injected code +// ============================================================================ +// Each submodule has its own dev JAR on the classpath (built by gradle.submodules.gradle) +// which serves as its FML mod container. The addon's dev JAR must NOT contain submodule +// @Mod classes, otherwise FML sees duplicate mod IDs. +// Hotswap still works: build/classes/java/main/ is on the classpath separately. +jar { + includeEmptyDirs = false + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + exclude { details -> + def path = details.relativePath.pathString + if (path == 'mcmod.info') return false // keep merged mcmod.info + if (path.endsWith('.class')) return isInjectedClass(path) + return isInjectedResource(path) + } +} + +// ============================================================================ +// 6. Release JAR: also strips injected code (same filter, different mcmod.info) +// ============================================================================ +tasks.register("stripInjectedJar", Zip) { + dependsOn tasks.named("jar") + group = 'build' + description = 'Creates a JAR with only addon classes (all submodule code stripped)' + + from(zipTree(tasks.named("jar", Jar).flatMap { it.archiveFile })) { + exclude { details -> + def path = details.relativePath.pathString + // Replace merged mcmod.info with addon's original + if (path == 'mcmod.info') return true + // Classes and resources already filtered by jar task, but double-check + if (path.endsWith('.class')) return isInjectedClass(path) + return isInjectedResource(path) + } + } + + // Add addon's original mcmod.info (not the merged dev one) + from('src/main/resources') { + include 'mcmod.info' + } + + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + includeEmptyDirs = false + archiveExtension.set('jar') + archiveClassifier.set('stripped') +} + +project.afterEvaluate { + tasks.named("reobfJar").configure { + dependsOn tasks.named("stripInjectedJar") + inputJar.set(tasks.named("stripInjectedJar", Zip).flatMap { it.archiveFile }) + } +} + +// ============================================================================ +// 7. Sources JAR: exclude all injected source +// ============================================================================ +tasks.named("sourcesJar") { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + exclude { details -> + def path = details.relativePath.pathString + if (path.endsWith('.java')) { + return injectedSrcDirs.any { srcDir -> new File(srcDir, path).exists() } + } + return isInjectedResource(path) + } +} + +// ============================================================================ +// 8. Run tasks: aggregate mixin configs from injected submodules +// ============================================================================ +def mixinConfigs = sourceInjections.findAll { it.mixinConfig }.collect { it.mixinConfig } +if (mixinConfigs) { + ['runClient', 'runServer'].each { taskName -> + tasks.named(taskName).configure { + dependsOn(tasks.named("processResources")) + extraArgs.addAll("--tweakClass", "org.spongepowered.asm.launch.MixinTweaker") + mixinConfigs.each { config -> + extraArgs.addAll("--mixin", config) + } + } + } +} diff --git a/gradle.submodules.gradle b/gradle.submodules.gradle index 185a657..ce6829c 100644 --- a/gradle.submodules.gradle +++ b/gradle.submodules.gradle @@ -112,15 +112,15 @@ tasks.register('buildSubmodules') { dependsOn 'buildCNPC', 'buildGeckoLib' } -// ---- Auto-build: submodules compile before this mod ---- +// ---- Auto-build & dependency mode ---- +// Both submodule dev JARs are always built and on the classpath. +// In source injection mode, the dev JARs serve as FML mod containers (so FML +// discovers each @Mod in its own JAR). Hotswap works via build/classes/java/main/. +// In JAR mode (-PnoSourceInjection=true), the dev JARs are the sole source of submodule code. tasks.named('compileJava').configure { dependsOn 'buildCNPC', 'buildGeckoLib' } -// ---- Dependency Declarations ---- -// devOnlyNonPublishable = compile + runtime, but NOT in output JAR and NOT published -// Dev JARs are already deobfuscated (MCP names), so no rfg.deobf() needed -// Using lazy file resolution so JARs are found AFTER submodule build tasks execute dependencies { devOnlyNonPublishable files({ findDevJar('CustomNPC-Plus') }) devOnlyNonPublishable files({ findDevJar('GeckoLib') })