From a535ef393ebee1dc32e14f22ffe0b6adcc1f792c Mon Sep 17 00:00:00 2001 From: shartte Date: Sat, 27 Apr 2024 22:26:08 +0200 Subject: [PATCH 1/6] Fix unused mcp_mappings placeholder causing problems (#153) Previously we set the unused mcp_mappings placeholder to a file that doesn't exist. Gradle now checks the file we try to resolve actually exists and crashes. --- .../runtime/definition/NeoFormRuntimeDefinition.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/definition/NeoFormRuntimeDefinition.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/definition/NeoFormRuntimeDefinition.java index eff575fc9..16976e911 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/definition/NeoFormRuntimeDefinition.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/definition/NeoFormRuntimeDefinition.java @@ -102,9 +102,9 @@ public Map buildRunInterpolationData(RunImpl run) { final Map interpolationData = new HashMap<>(super.buildRunInterpolationData(run)); interpolationData.put("mcp_version", neoform.getVersion()); - interpolationData.put("mcp_mappings", getSpecification().getNeoFormArchive() - .matching(artifact -> artifact.include("config/joined.srg")) - .getSingleFile().getAbsolutePath()); + // NeoForge still references this in the environment variable MCP_MAPPINGS, which is unused since 1.20.2 + // Remove this interpolation placeholder once NeoForge removes the environment variable from its config.json + interpolationData.put("mcp_mappings", "UNUSED_DEPRECATED"); return interpolationData; } From d66f11c56828e084e2b6758f0c6a91dc1c176e1c Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Sat, 27 Apr 2024 22:27:49 +0200 Subject: [PATCH 2/6] Update gradle version and bump wrapper (#152) --- build.gradle | 1 - gradle.properties | 3 --- gradle/wrapper/gradle-wrapper.properties | 2 +- gradlew.bat | 20 ++++++++++---------- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/build.gradle b/build.gradle index a86becc2e..6662bb0b3 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,6 @@ version = gradleutils.version.toString() //We hard coded the version of the gradle wrapper here. tasks.named('wrapper', Wrapper).configure { Wrapper wrapperTask -> - wrapperTask.gradleVersion = project.gradle_version wrapperTask.distributionType = Wrapper.DistributionType.ALL } diff --git a/gradle.properties b/gradle.properties index 9567e1ea6..c693e1993 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,9 +6,6 @@ org.gradle.caching=true # Default project properties. group=net.neoforged.gradle -#Gradle properties -gradle_version=8.0 - #Java properties java_version=8 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 2ea3535dc..e7646dead 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-all.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew.bat b/gradlew.bat index 93e3f59f1..25da30dbd 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail From 08369c97b33337b983d330885446c97493368308 Mon Sep 17 00:00:00 2001 From: shartte Date: Sun, 28 Apr 2024 11:12:00 +0200 Subject: [PATCH 3/6] Merge pull request #154 * Remove MCP_MAPPINGS from the generated Neoforge run types --- .../platform/extensions/DynamicProjectExtension.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/platform/src/main/java/net/neoforged/gradle/platform/extensions/DynamicProjectExtension.java b/platform/src/main/java/net/neoforged/gradle/platform/extensions/DynamicProjectExtension.java index 057e921ad..eeefee207 100644 --- a/platform/src/main/java/net/neoforged/gradle/platform/extensions/DynamicProjectExtension.java +++ b/platform/src/main/java/net/neoforged/gradle/platform/extensions/DynamicProjectExtension.java @@ -538,8 +538,7 @@ public void runtime(final String neoFormVersion, Directory patches, Directory re //Note: We can not use a 'configureEach' here, because this causes issues with the config cache. userdevProfile.runType("client", type -> { type.getEnvironmentVariables().put("MOD_CLASSES", "{source_roots}"); - type.getEnvironmentVariables().put("MCP_MAPPINGS", "{mcp_mappings}"); - + type.getIsClient().set(true); type.getIsGameTest().set(true); type.getSystemProperties().put("neoforge.enableGameTest", "true"); @@ -557,8 +556,7 @@ public void runtime(final String neoFormVersion, Directory patches, Directory re }); userdevProfile.runType("server", type -> { type.getEnvironmentVariables().put("MOD_CLASSES", "{source_roots}"); - type.getEnvironmentVariables().put("MCP_MAPPINGS", "{mcp_mappings}"); - + type.getIsServer().set(true); type.getArguments().add("--launchTarget"); @@ -568,8 +566,7 @@ public void runtime(final String neoFormVersion, Directory patches, Directory re }); userdevProfile.runType("gameTestServer", type -> { type.getEnvironmentVariables().put("MOD_CLASSES", "{source_roots}"); - type.getEnvironmentVariables().put("MCP_MAPPINGS", "{mcp_mappings}"); - + type.getIsServer().set(true); type.getIsGameTest().set(true); type.getSystemProperties().put("neoforge.enableGameTest", "true"); @@ -583,8 +580,7 @@ public void runtime(final String neoFormVersion, Directory patches, Directory re }); userdevProfile.runType("data", type -> { type.getEnvironmentVariables().put("MOD_CLASSES", "{source_roots}"); - type.getEnvironmentVariables().put("MCP_MAPPINGS", "{mcp_mappings}"); - + type.getIsDataGenerator().set(true); type.getArguments().add("--launchTarget"); From f4baaba715b1b546566bd5d8fa2f53767bdd2215 Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Sun, 28 Apr 2024 11:20:07 +0200 Subject: [PATCH 4/6] Fix handling of multiple AccessTransformers in the same project (#155) --- .../runtime/tasks/AccessTransformer.java | 1 - .../common/util/CommonRuntimeTaskUtils.java | 47 +--- .../userdev/AccessTransformerTests.groovy | 222 ++++++++++++++++++ .../gradle/userdev/FunctionalTests.groovy | 4 +- 4 files changed, 233 insertions(+), 41 deletions(-) create mode 100644 userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/AccessTransformerTests.groovy diff --git a/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/AccessTransformer.java b/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/AccessTransformer.java index 38c5637f9..07642dc4b 100644 --- a/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/AccessTransformer.java +++ b/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/AccessTransformer.java @@ -50,7 +50,6 @@ public AccessTransformer() { @PathSensitive(PathSensitivity.NONE) public abstract RegularFileProperty getInputFile(); - @InputFiles @PathSensitive(PathSensitivity.NONE) public abstract ConfigurableFileCollection getTransformers(); diff --git a/common/src/main/java/net/neoforged/gradle/common/util/CommonRuntimeTaskUtils.java b/common/src/main/java/net/neoforged/gradle/common/util/CommonRuntimeTaskUtils.java index bb803e871..42c765443 100644 --- a/common/src/main/java/net/neoforged/gradle/common/util/CommonRuntimeTaskUtils.java +++ b/common/src/main/java/net/neoforged/gradle/common/util/CommonRuntimeTaskUtils.java @@ -9,6 +9,7 @@ import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils; import net.neoforged.gradle.dsl.common.util.GameArtifact; import net.neoforged.gradle.util.StringCapitalizationUtils; +import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.EmptyFileVisitor; import org.gradle.api.file.FileTree; import org.gradle.api.file.FileVisitDetails; @@ -26,52 +27,22 @@ private CommonRuntimeTaskUtils() { } public static TaskProvider createAccessTransformer(Definition definition, String namePreFix, File workspaceDirectory, Consumer> dependentTaskConfigurationHandler, FileTree files, Collection data) { - final Collection> fileProducingTasks = new ArrayList<>(); - final Map indexes = new HashMap<>(); - - files.visit(new EmptyFileVisitor() { - @Override - public void visitFile(@NotNull FileVisitDetails fileDetails) { - indexes.compute(fileDetails.getName(), (s, index) -> index == null ? 0 : index + 1); - final int index = indexes.get(fileDetails.getName()); - - final String name = CommonRuntimeUtils.buildTaskName(definition.getSpecification(), namePreFix + "AccessTransformerProvider" + fileDetails.getName() + (index == 0 ? "" : "_" + index)); - - final TaskProvider provider = definition.getSpecification().getProject().getTasks().register(name, ArtifactProvider.class, task -> { - task.getInputFiles().from( - files.matching(f -> f.include(fileDetails.getPath())) - ); - String outputFileName = fileDetails.getName(); - if (index > 0) { - int extensionDot = outputFileName.lastIndexOf('.'); - if (extensionDot == -1) { - outputFileName += "_" + index; - } else { - outputFileName = outputFileName.substring(0, extensionDot) + "_" + index + outputFileName.substring(extensionDot); - } - } - task.getOutputFileName().set(outputFileName); - task.getOutput().set(new File(workspaceDirectory, "accesstransformers/" + namePreFix + "/" + outputFileName)); - }); - - fileProducingTasks.add(provider); - } - }); - + final TaskProvider generator; if (!data.isEmpty()) { - final TaskProvider generator = definition.getSpecification().getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(definition.getSpecification(), namePreFix + "AccessTransformerGenerator"), AccessTransformerFileGenerator.class, task -> { + generator = definition.getSpecification().getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(definition.getSpecification(), namePreFix + "AccessTransformerGenerator"), AccessTransformerFileGenerator.class, task -> { task.getOutput().set(new File(workspaceDirectory, "accesstransformers/" + namePreFix + "/_script-access-transformer.cfg")); task.getAdditionalTransformers().set(data); }); dependentTaskConfigurationHandler.accept(generator); - - fileProducingTasks.add(generator); + } else { + generator = null; } return definition.getSpecification().getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(definition.getSpecification(), String.format("apply%sAccessTransformer", StringCapitalizationUtils.capitalize(namePreFix))), AccessTransformer.class, task -> { - for (TaskProvider fileRemapTask : fileProducingTasks) { - task.getTransformers().from(fileRemapTask.flatMap(WithOutput::getOutput)); - task.dependsOn(fileRemapTask); + task.getTransformers().from(files); + if (generator != null) { + task.getTransformers().from(generator.flatMap(WithOutput::getOutput)); + task.dependsOn(generator); } }); } diff --git a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/AccessTransformerTests.groovy b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/AccessTransformerTests.groovy new file mode 100644 index 000000000..8ba916fe6 --- /dev/null +++ b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/AccessTransformerTests.groovy @@ -0,0 +1,222 @@ +package net.neoforged.gradle.userdev + +import net.neoforged.trainingwheels.gradle.functional.BuilderBasedTestSpecification +import org.gradle.testkit.runner.TaskOutcome + +class AccessTransformerTests extends BuilderBasedTestSpecification { + + @Override + protected void configurePluginUnderTest() { + pluginUnderTest = "net.neoforged.gradle.userdev"; + injectIntoAllProject = true; + } + + def "the userdev runtime supports loading ats from a file"() { + given: + def project = create("userdev_supports_ats_from_file", { + it.build(""" + java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } + } + + minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') + + dependencies { + implementation 'net.neoforged:neoforge:+' + } + """) + it.file("src/main/resources/META-INF/accesstransformer.cfg", """public-f net.minecraft.client.Minecraft fixerUpper # fixerUpper""") + it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """ + package net.neoforged.gradle.userdev; + + import net.minecraft.client.Minecraft; + + public class FunctionalTests { + public static void main(String[] args) { + System.out.println(Minecraft.getInstance().fixerUpper.getClass().toString()); + } + } + """) + it.withToolchains() + }) + + when: + def initialRun = project.run { + it.tasks('build') + } + + then: + initialRun.task(":neoFormRecompile").outcome == TaskOutcome.SUCCESS + initialRun.task(":build").outcome == TaskOutcome.SUCCESS + } + + def "the userdev runtime supports loading ats from the script"() { + given: + def project = create("userdev_supports_ats_in_scripts", { + it.build(""" + java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } + } + + minecraft.accessTransformers.entry 'public-f net.minecraft.client.Minecraft fixerUpper # fixerUpper' + + dependencies { + implementation 'net.neoforged:neoforge:+' + } + """) + it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """ + package net.neoforged.gradle.userdev; + + import net.minecraft.client.Minecraft; + + public class FunctionalTests { + public static void main(String[] args) { + System.out.println(Minecraft.getInstance().fixerUpper.getClass().toString()); + } + } + """) + it.withToolchains() + }) + + when: + def initialRun = project.run { + it.tasks('build') + } + + then: + initialRun.task(":neoFormRecompile").outcome == TaskOutcome.SUCCESS + initialRun.task(":build").outcome == TaskOutcome.SUCCESS + } + + def "the userdev runtime supports loading ats from the script and the file"() { + given: + def project = create("userdev_supports_ats_in_script_and_file", { + it.build(""" + java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } + } + minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') + minecraft.accessTransformers.entry 'public-f net.minecraft.client.Minecraft LOGGER # LOGGER' + + dependencies { + implementation 'net.neoforged:neoforge:+' + } + """) + it.file("src/main/resources/META-INF/accesstransformer.cfg", """public-f net.minecraft.client.Minecraft fixerUpper # fixerUpper""") + it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """ + package net.neoforged.gradle.userdev; + + import net.minecraft.client.Minecraft; + + public class FunctionalTests { + public static void main(String[] args) { + System.out.println(Minecraft.getInstance().fixerUpper.getClass().toString()); + System.out.println(Minecraft.LOGGER.getClass().toString()); + } + } + """) + it.withToolchains() + }) + + when: + def initialRun = project.run { + it.tasks('build') + } + + then: + initialRun.task(":neoFormRecompile").outcome == TaskOutcome.SUCCESS + initialRun.task(":build").outcome == TaskOutcome.SUCCESS + } + + def "the userdev runtime supports loading ats from multiple files"() { + given: + def project = create("userdev_supports_ats_in_multiple_distinctly_named_files", { + it.build(""" + java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } + } + minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') + minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer2.cfg') + + dependencies { + implementation 'net.neoforged:neoforge:+' + } + """) + it.file("src/main/resources/META-INF/accesstransformer.cfg", """public-f net.minecraft.client.Minecraft fixerUpper # fixerUpper""") + it.file("src/main/resources/META-INF/accesstransformer2.cfg", """public-f net.minecraft.client.Minecraft LOGGER # LOGGER""") + it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """ + package net.neoforged.gradle.userdev; + + import net.minecraft.client.Minecraft; + + public class FunctionalTests { + public static void main(String[] args) { + System.out.println(Minecraft.getInstance().fixerUpper.getClass().toString()); + System.out.println(Minecraft.LOGGER.getClass().toString()); + } + } + """) + it.withToolchains() + }) + + when: + def initialRun = project.run { + it.tasks('build') + } + + then: + initialRun.task(":neoFormRecompile").outcome == TaskOutcome.SUCCESS + initialRun.task(":build").outcome == TaskOutcome.SUCCESS + } + + def "the userdev runtime supports loading ats from multiple files named the same in different directories"() { + given: + def project = create("userdev_supports_ats_in_files_named_the_same", { + it.build(""" + java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } + } + minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg') + minecraft.accessTransformers.file rootProject.file('src/main/resources/accesstransformer.cfg') + + dependencies { + implementation 'net.neoforged:neoforge:+' + } + """) + it.file("src/main/resources/META-INF/accesstransformer.cfg", """public-f net.minecraft.client.Minecraft fixerUpper # fixerUpper""") + it.file("src/main/resources/accesstransformer.cfg", """public-f net.minecraft.client.Minecraft LOGGER # LOGGER""") + it.file("src/main/java/net/neoforged/gradle/userdev/FunctionalTests.java", """ + package net.neoforged.gradle.userdev; + + import net.minecraft.client.Minecraft; + + public class FunctionalTests { + public static void main(String[] args) { + System.out.println(Minecraft.getInstance().fixerUpper.getClass().toString()); + System.out.println(Minecraft.LOGGER.getClass().toString()); + } + } + """) + it.withToolchains() + }) + + when: + def initialRun = project.run { + it.tasks('build') + } + + then: + initialRun.task(":neoFormRecompile").outcome == TaskOutcome.SUCCESS + initialRun.task(":build").outcome == TaskOutcome.SUCCESS + } +} diff --git a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/FunctionalTests.groovy b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/FunctionalTests.groovy index cfd6dcaac..928523e65 100644 --- a/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/FunctionalTests.groovy +++ b/userdev/src/functionalTest/groovy/net/neoforged/gradle/userdev/FunctionalTests.groovy @@ -6,7 +6,6 @@ import org.gradle.testkit.runner.TaskOutcome class FunctionalTests extends BuilderBasedTestSpecification { - @Override protected void configurePluginUnderTest() { pluginUnderTest = "net.neoforged.gradle.userdev"; @@ -117,7 +116,7 @@ class FunctionalTests extends BuilderBasedTestSpecification { def "the userdev runtime by default supports the build cache"() { given: - def project = create("compile_with_gradle_and_official_mappings", { + def project = create("userdev_supports_loading_from_buildcache", { it.build(""" java { toolchain { @@ -165,4 +164,5 @@ class FunctionalTests extends BuilderBasedTestSpecification { secondRun.task(":neoFormRecompile").outcome == TaskOutcome.FROM_CACHE initialRun.task(":build").outcome == TaskOutcome.SUCCESS } + } From b15b5af29377bc4181d2a7f5ff59668f7c92dad1 Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Sun, 28 Apr 2024 14:12:33 +0200 Subject: [PATCH 5/6] Fix injection logic not taking into accounts that trees might need to be offset by a prefix. (#156) --- README.md | 6 +- .../common/runtime/tasks/DefaultRuntime.java | 4 +- .../tasks/ObfuscatedDependencyMarker.java | 63 ------------------- .../extensions/NeoFormRuntimeExtension.java | 1 + .../tasks/InjectFromFileTreeSource.java | 52 +++++++++++++-- .../util/ZipBuildingFileTreeVisitor.java | 12 +++- 6 files changed, 63 insertions(+), 75 deletions(-) delete mode 100644 common/src/main/java/net/neoforged/gradle/common/tasks/ObfuscatedDependencyMarker.java diff --git a/README.md b/README.md index 70c58d6d4..9041a5dc1 100644 --- a/README.md +++ b/README.md @@ -44,13 +44,13 @@ plugins { dependencies { // For depending on a Minecraft JAR-file with both client- and server-classes - implementation "net.minecraft:neoform_joined:' + implementation "net.minecraft:neoform_joined:" // For depending on the Minecraft client JAR-file - implementation "net.minecraft:neoform_client:' + implementation "net.minecraft:neoform_client:" // For depending on the Minecraft dedicated server JAR-file - implementation "net.minecraft:neoform_server:' + implementation "net.minecraft:neoform_server:" } ``` diff --git a/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/DefaultRuntime.java b/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/DefaultRuntime.java index 1688bf347..bbd20a108 100644 --- a/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/DefaultRuntime.java +++ b/common/src/main/java/net/neoforged/gradle/common/runtime/tasks/DefaultRuntime.java @@ -49,8 +49,8 @@ public DefaultRuntime() { Map.Entry::getKey, entry -> getNeoFormArchive() .getAsFileTree() - .matching(archive -> archive.include(entry.getValue()) - ))))); + .matching(archive -> archive.include(entry.getValue())) + )))); getOutputDirectory().finalizeValueOnRead(); } diff --git a/common/src/main/java/net/neoforged/gradle/common/tasks/ObfuscatedDependencyMarker.java b/common/src/main/java/net/neoforged/gradle/common/tasks/ObfuscatedDependencyMarker.java deleted file mode 100644 index 81aa6f1bb..000000000 --- a/common/src/main/java/net/neoforged/gradle/common/tasks/ObfuscatedDependencyMarker.java +++ /dev/null @@ -1,63 +0,0 @@ -package net.neoforged.gradle.common.tasks; - -import net.neoforged.gradle.util.AdaptingZipBuildingFileTreeVisitor; -import net.neoforged.gradle.dsl.common.tasks.WithOutput; -import net.neoforged.gradle.dsl.common.tasks.WithWorkspace; -import org.gradle.api.DefaultTask; -import org.gradle.api.file.RegularFileProperty; -import org.gradle.api.tasks.CacheableTask; -import org.gradle.api.tasks.InputFile; -import org.gradle.api.tasks.PathSensitive; -import org.gradle.api.tasks.PathSensitivity; -import org.gradle.api.tasks.TaskAction; - -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.jar.Attributes; -import java.util.jar.Manifest; -import java.util.zip.ZipOutputStream; - -@CacheableTask -public abstract class ObfuscatedDependencyMarker extends DefaultTask implements WithOutput, WithWorkspace { - - - public ObfuscatedDependencyMarker() { - super(); - getOutput().convention(getProject().getLayout().getBuildDirectory().dir("obfuscated").flatMap(directory -> getObfuscatedJar().map(input -> directory.file(input.getAsFile().getName().substring(0, input.getAsFile().getName().length() - 4) + "-marked.jar")))); - } - - @TaskAction - public void doMark() { - try { - final File outputJar = this.ensureFileWorkspaceReady(getOutput()); - final FileOutputStream fileOutputStream = new FileOutputStream(outputJar); - final ZipOutputStream outputZipStream = new ZipOutputStream(fileOutputStream); - final AdaptingZipBuildingFileTreeVisitor zipBuildingFileTreeVisitor = new AdaptingZipBuildingFileTreeVisitor(outputZipStream, (fileVisitDetails, outputStream) -> { - if (!fileVisitDetails.getRelativePath().getPathString().equals("META-INF/MANIFEST.MF")) { - fileVisitDetails.copyTo(outputStream); - return; - } - try { - Manifest manifest = new Manifest(fileVisitDetails.open()); - Attributes mainAttributes = manifest.getMainAttributes(); - mainAttributes.putValue("Obfuscated", "true"); - mainAttributes.putValue("Obfuscated-By", "NeoGradle"); - - manifest.write(outputStream); - } catch (IOException e) { - throw new RuntimeException("Failed to write manifest", e); - } - }); - getProject().zipTree(getObfuscatedJar().get()).visit(zipBuildingFileTreeVisitor); - outputZipStream.close(); - fileOutputStream.close(); - } catch (IOException e) { - throw new RuntimeException("Failed to create recompiled output jar", e); - } - } - - @InputFile - @PathSensitive(PathSensitivity.NONE) - public abstract RegularFileProperty getObfuscatedJar(); -} diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java index 36101f366..8a5a46bd4 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java @@ -128,6 +128,7 @@ private static TaskProvider createBuiltIn(final NeoFormRun } }) ); + fileTreeSource.getTreePrefix().set(task.getSymbolicDataSources().map(data -> data.get("inject"))); return fileTreeSource; }) ); diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/InjectFromFileTreeSource.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/InjectFromFileTreeSource.java index c9d99c185..e05fe46f7 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/InjectFromFileTreeSource.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/InjectFromFileTreeSource.java @@ -3,13 +3,14 @@ import net.neoforged.gradle.util.ZipBuildingFileTreeVisitor; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.FileTree; -import org.gradle.api.tasks.InputFiles; -import org.gradle.api.tasks.PathSensitive; -import org.gradle.api.tasks.PathSensitivity; +import org.gradle.api.file.FileVisitDetails; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.*; import org.jetbrains.annotations.Nullable; import java.io.IOException; import java.nio.file.Files; +import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** @@ -22,10 +23,15 @@ public abstract class InjectFromFileTreeSource extends AbstractInjectSource { @PathSensitive(PathSensitivity.NONE) public abstract ConfigurableFileCollection getFiles(); + @Optional + @Input + public abstract Property getTreePrefix(); + @Override public byte @Nullable [] tryReadFile(String path) throws IOException { + final String lookupPath = buildTreePrefix() + path; final FileTree matching = getFiles().getAsFileTree().matching(createFilter()) - .matching(pattern -> pattern.include(path)); + .matching(pattern -> pattern.include(lookupPath)); if (matching.isEmpty()) { return null; } @@ -39,6 +45,42 @@ public void copyTo(ZipOutputStream out) throws IOException { } final FileTree source = getFiles().getAsFileTree().matching(createFilter()); - source.visit(new ZipBuildingFileTreeVisitor(out)); + source.visit(new ZipBuildingFileTreeVisitor(out) { + @Override + protected ZipEntry createDirectory(FileVisitDetails fileVisitDetails) { + String path = fileVisitDetails.getRelativePath().getPathString(); + if (path.startsWith(buildTreePrefix())) { + path = path.substring(buildTreePrefix().length()); + } + + if (!path.endsWith("/")) { + path += "/"; + } + + return new ZipEntry(path); + } + + @Override + protected ZipEntry createFile(FileVisitDetails fileVisitDetails) { + String path = fileVisitDetails.getRelativePath().getPathString(); + if (path.startsWith(buildTreePrefix())) { + path = path.substring(buildTreePrefix().length()); + } + + return new ZipEntry(path); + } + }); + } + + private String buildTreePrefix() { + if (getTreePrefix().isPresent()) { + final String prefix = getTreePrefix().get(); + if (!prefix.endsWith("/")) { + return prefix + "/"; + } + return prefix; + } + + return ""; } } diff --git a/utils/src/main/java/net/neoforged/gradle/util/ZipBuildingFileTreeVisitor.java b/utils/src/main/java/net/neoforged/gradle/util/ZipBuildingFileTreeVisitor.java index d0e91d4c1..93e892a01 100644 --- a/utils/src/main/java/net/neoforged/gradle/util/ZipBuildingFileTreeVisitor.java +++ b/utils/src/main/java/net/neoforged/gradle/util/ZipBuildingFileTreeVisitor.java @@ -24,7 +24,7 @@ public ZipBuildingFileTreeVisitor(ZipOutputStream outputZipStream) { @Override public void visitDir(FileVisitDetails fileVisitDetails) { try { - final ZipEntry directoryEntry = new ZipEntry(fileVisitDetails.getRelativePath().getPathString() + "/"); + final ZipEntry directoryEntry = createDirectory(fileVisitDetails); outputZipStream.putNextEntry(directoryEntry); outputZipStream.closeEntry(); } catch (IOException zip) { @@ -33,11 +33,15 @@ public void visitDir(FileVisitDetails fileVisitDetails) { } } } + + protected ZipEntry createDirectory(FileVisitDetails fileVisitDetails) { + return new ZipEntry(fileVisitDetails.getRelativePath().getPathString() + "/"); + } @Override public void visitFile(FileVisitDetails fileVisitDetails) { try { - final ZipEntry fileEntry = new ZipEntry(fileVisitDetails.getRelativePath().getPathString()); + final ZipEntry fileEntry = createFile(fileVisitDetails); outputZipStream.putNextEntry(fileEntry); fileVisitDetails.copyTo(outputZipStream); outputZipStream.closeEntry(); @@ -45,4 +49,8 @@ public void visitFile(FileVisitDetails fileVisitDetails) { throw new RuntimeException("Could not create zip file: " + fileVisitDetails.getRelativePath().getPathString(), e); } } + + protected ZipEntry createFile(FileVisitDetails fileVisitDetails) { + return new ZipEntry(fileVisitDetails.getRelativePath().getPathString()); + } } From b483eb18b0c5456007e4eb48ed24d0e92886ff4a Mon Sep 17 00:00:00 2001 From: shartte Date: Sun, 28 Apr 2024 21:05:00 +0200 Subject: [PATCH 6/6] Fix long-form Vineflower log and thread arguments not getting recognized (#157) --- .../neoform/runtime/extensions/NeoFormRuntimeExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java index 8a5a46bd4..fcf795d59 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java @@ -169,7 +169,7 @@ private static TaskProvider createDecompile(NeoFormRuntimeSpe // Filter out decompiler arguments that aren't related to its output (log-level and thread-count) List decompilerArgs = new ArrayList<>(function.getArgs()); - decompilerArgs.removeIf(arg -> arg.startsWith("-log=") || arg.startsWith("-thr=")); + decompilerArgs.removeIf(arg -> arg.startsWith("--log-level") || arg.startsWith("-log=") || arg.startsWith("--thread-count") || arg.startsWith("-thr=")); // Retrieve the default memory size from the JVM arguments configured in NeoForm String defaultMaxMemory = "4g";