From 5c2760da714227e7620174090e4178f880228064 Mon Sep 17 00:00:00 2001 From: Jab125 <67534807+Jab125@users.noreply.github.com> Date: Tue, 9 Jul 2024 20:03:32 +1000 Subject: [PATCH] Remap NeoForge's mixins (#224) --- .../forge/MinecraftPatchedProvider.java | 12 ++- .../AbstractMappedMinecraftProvider.java | 2 + .../neoforge/NeoForge1210Test.groovy | 61 ++++++++++++++ .../projects/neoforge/1210/build.gradle | 83 +++++++++++++++++++ .../projects/neoforge/1210/gradle.properties | 11 +++ .../projects/neoforge/1210/settings.gradle | 2 + 6 files changed, 168 insertions(+), 3 deletions(-) create mode 100644 src/test/groovy/net/fabricmc/loom/test/integration/neoforge/NeoForge1210Test.groovy create mode 100644 src/test/resources/projects/neoforge/1210/build.gradle create mode 100644 src/test/resources/projects/neoforge/1210/gradle.properties create mode 100644 src/test/resources/projects/neoforge/1210/settings.gradle diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java index a058d3197..e2b684bdd 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/forge/MinecraftPatchedProvider.java @@ -89,6 +89,7 @@ import net.fabricmc.loom.util.srg.InnerClassRemapper; import net.fabricmc.mappingio.tree.MappingTree; import net.fabricmc.mappingio.tree.MemoryMappingTree; +import net.fabricmc.tinyremapper.extension.mixin.MixinExtension; import net.fabricmc.tinyremapper.InputTag; import net.fabricmc.tinyremapper.NonClassCopyMode; import net.fabricmc.tinyremapper.OutputConsumerPath; @@ -231,12 +232,17 @@ private TinyRemapper buildRemapper(SharedServiceManager serviceManager, Path inp final String sourceNamespace = IntermediaryNamespaces.intermediary(project); MemoryMappingTree mappings = mappingsService.getMappingTree(); - TinyRemapper remapper = TinyRemapper.newRemapper() + TinyRemapper.Builder builder = TinyRemapper.newRemapper() .withMappings(TinyRemapperHelper.create(mappings, sourceNamespace, "official", true)) .withMappings(InnerClassRemapper.of(InnerClassRemapper.readClassNames(input), mappings, sourceNamespace, "official")) .renameInvalidLocals(true) - .rebuildSourceFilenames(true) - .build(); + .rebuildSourceFilenames(true); + + if (getExtension().isNeoForge()) { + builder.extension(new MixinExtension(inputTag -> true)); + } + + TinyRemapper remapper = builder.build(); if (project.getGradle().getStartParameter().getLogLevel().compareTo(LogLevel.LIFECYCLE) < 0) { MappingsProviderVerbose.saveFile(remapper); diff --git a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java index d860fb3c5..fd432fb6b 100644 --- a/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java +++ b/src/main/java/net/fabricmc/loom/configuration/providers/minecraft/mapped/AbstractMappedMinecraftProvider.java @@ -60,6 +60,7 @@ import net.fabricmc.loom.util.srg.InnerClassRemapper; import net.fabricmc.loom.util.srg.RemapObjectHolderVisitor; import net.fabricmc.mappingio.tree.MemoryMappingTree; +import net.fabricmc.tinyremapper.extension.mixin.MixinExtension; import net.fabricmc.tinyremapper.OutputConsumerPath; import net.fabricmc.tinyremapper.TinyRemapper; @@ -211,6 +212,7 @@ private void remapJar(RemappedJars remappedJars, ConfigContext configContext) th TinyRemapper remapper = TinyRemapperHelper.getTinyRemapper(getProject(), configContext.serviceManager(), fromM, toM, fixRecords, (builder) -> { builder.extraPostApplyVisitor(new SignatureFixerApplyVisitor(remappedSignatures)); + if (extension.isNeoForge()) builder.extension(new MixinExtension(inputTag -> true)); configureRemapper(remappedJars, builder); }, classNames); diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/neoforge/NeoForge1210Test.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/neoforge/NeoForge1210Test.groovy new file mode 100644 index 000000000..c101a85ce --- /dev/null +++ b/src/test/groovy/net/fabricmc/loom/test/integration/neoforge/NeoForge1210Test.groovy @@ -0,0 +1,61 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2024 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.test.integration.neoforge + +import spock.lang.Specification +import spock.lang.Unroll + +import net.fabricmc.loom.test.util.GradleProjectTestTrait + +import static net.fabricmc.loom.test.LoomTestConstants.DEFAULT_GRADLE +import static org.gradle.testkit.runner.TaskOutcome.SUCCESS + +class NeoForge1210Test extends Specification implements GradleProjectTestTrait { + @Unroll + def "build #mcVersion #neoforgeVersion #mappings #patches"() { + if (Integer.valueOf(System.getProperty("java.version").split("\\.")[0]) < 21) { + println("This test requires Java 21. Currently you have Java ${System.getProperty("java.version")}.") + return + } + + setup: + def gradle = gradleProject(project: "neoforge/1210", version: DEFAULT_GRADLE) + gradle.buildGradle.text = gradle.buildGradle.text.replace('@MCVERSION@', mcVersion) + .replace('@NEOFORGEVERSION@', neoforgeVersion) + .replace('MAPPINGS', mappings) // Spotless doesn't like the @'s + .replace('PATCHES', patches) + + when: + def result = gradle.run(task: "build") + + then: + result.task(":build").outcome == SUCCESS + + where: + mcVersion | neoforgeVersion | mappings | patches + '1.21' | '21.0.77-beta' | 'loom.officialMojangMappings()' | '' + '1.21' | '21.0.77-beta' | "'net.fabricmc:yarn:1.21+build.1:v2'" | "'dev.architectury:yarn-mappings-patch-neoforge:1.21+build.4'" + } +} diff --git a/src/test/resources/projects/neoforge/1210/build.gradle b/src/test/resources/projects/neoforge/1210/build.gradle new file mode 100644 index 000000000..5ab5071ca --- /dev/null +++ b/src/test/resources/projects/neoforge/1210/build.gradle @@ -0,0 +1,83 @@ +plugins { + id 'dev.architectury.loom' + id 'maven-publish' +} + +java { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 +} + +base { + archivesName = project.archives_base_name +} + +version = project.mod_version +group = project.maven_group + +def mcVersion = "@MCVERSION@" +def neoforgeVersion = "@NEOFORGEVERSION@" + +repositories { + // Add repositories to retrieve artifacts from in here. + // You should only use this when depending on other mods because + // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. + // See https://docs.gradle.org/current/userguide/declaring_repositories.html + // for more information about repositories. + maven { url "https://maven.neoforged.net/releases/" } +} + +dependencies { + // To change the versions see the gradle.properties file + minecraft "com.mojang:minecraft:$mcVersion" + if ("MAPPINGS".equals("loom.officialMojangMappings()")) { + mappings loom.officialMojangMappings() + } else { + mappings loom.layered { + it.mappings(MAPPINGS) + it.mappings(PATCHES) + } + } + + neoForge "net.neoforged:neoforge:$neoforgeVersion" +} + +tasks.withType(JavaCompile).configureEach { + it.options.release = 21 +} + +java { + // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task + // if it is present. + // If you remove this line, sources will not be generated. + withSourcesJar() +} + +jar { + from("LICENSE") { + rename { "${it}_${project.archivesBaseName}"} + } +} + +// configure the maven publication +publishing { + publications { + mavenJava(MavenPublication) { + // add all the jars that should be included when publishing to maven + artifact(remapJar) { + builtBy remapJar + } + artifact(sourcesJar) { + builtBy remapSourcesJar + } + } + } + + // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. + repositories { + // Add repositories to publish to here. + // Notice: This block does NOT have the same function as the block in the top level. + // The repositories here will be used for publishing your artifact, not for + // retrieving dependencies. + } +} diff --git a/src/test/resources/projects/neoforge/1210/gradle.properties b/src/test/resources/projects/neoforge/1210/gradle.properties new file mode 100644 index 000000000..7fb259a90 --- /dev/null +++ b/src/test/resources/projects/neoforge/1210/gradle.properties @@ -0,0 +1,11 @@ +# Done to increase the memory available to gradle. +org.gradle.jvmargs=-Xmx1G + +# Mod Properties +mod_version = 1.0.0 +maven_group = com.example +archives_base_name = fabric-example-mod + +# Dependencies +# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api +loom.platform = neoforge diff --git a/src/test/resources/projects/neoforge/1210/settings.gradle b/src/test/resources/projects/neoforge/1210/settings.gradle new file mode 100644 index 000000000..c162c363e --- /dev/null +++ b/src/test/resources/projects/neoforge/1210/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = "fabric-example-mod" +