66package net .minecraftforge .gradle .common .legacy ;
77
88import groovy .lang .GroovyObjectSupport ;
9+ import net .minecraftforge .gradle .common .tasks .ExtractMCPData ;
910import net .minecraftforge .gradle .common .tasks .ExtractZip ;
1011import net .minecraftforge .gradle .common .util .MinecraftExtension ;
12+ import net .minecraftforge .srgutils .IMappingFile ;
1113import net .minecraftforge .srgutils .MinecraftVersion ;
1214import org .gradle .api .Project ;
1315import org .gradle .api .artifacts .Dependency ;
1416import org .gradle .api .file .FileCollection ;
1517import org .gradle .api .provider .Property ;
1618import org .gradle .api .provider .Provider ;
19+ import org .gradle .api .tasks .TaskProvider ;
1720
1821import java .io .File ;
1922
@@ -46,8 +49,13 @@ public abstract class LegacyExtension extends GroovyObjectSupport {
4649 * that points to a directory containing CSV mappings files. This is used by LegacyDev to remap dependencies' AT modifiers.
4750 * We replicate the FG2 behavior by extracting the mappings to a folder in the build directory
4851 * and setting the property to point to it.
52+ * - SRG Mappings Runtime Property;
53+ * FG2 GradleStart exposes a <code>net.minecraftforge.gradle.GradleStart.srg.notch-srg</code> property
54+ * that points to a NOTCH -> SRG mapping file. This can be consumed by mods to access obfuscation mappings at runtime.
55+ * We replicate the FG2 behavior by attaching the property to each run configuration and pointing it to
56+ * the output of the extractSrg task, which is the srg mapping file extracted from mcp config.
4957 *
50- * This is called from {@link Utils. createRunConfigTasks) }
58+ * This is called from {@link net.minecraftforge.gradle.common.util.Utils# createRunConfigTasks}
5159 *
5260 * In other words, it's a containment zone for version-specific hacks.
5361 * For issues you think are caused by this function, contact Curle or any other Retrogradle maintainer.
@@ -58,6 +66,7 @@ public static void runRetrogradleFixes(Project project) {
5866 final MinecraftExtension minecraft = project .getExtensions ().getByType (MinecraftExtension .class );
5967 final boolean shouldFixClasspath = config .getFixClasspath ().get ();
6068 final boolean shouldExtractMappings = config .getExtractMappings ().get ();
69+ final boolean shouldAttachMappings = config .getAttachMappings ().get ();
6170
6271 if (shouldFixClasspath ) {
6372 project .getLogger ().info ("LegacyExtension: Fixing classpath" );
@@ -97,6 +106,26 @@ public static void runRetrogradleFixes(Project project) {
97106 // execute extractMappings before each run task
98107 project .getTasks ().named ("prepareRuns" ).configure (t -> t .dependsOn (extractMappingsTask ));
99108 }
109+
110+ if (shouldAttachMappings ) {
111+ project .getLogger ().info ("LegacyExtension: Attaching mappings path to runs" );
112+ // Get the existing extractSrg task
113+ final TaskProvider <ExtractMCPData > extractSrg = project .getTasks ().named ("extractSrg" , ExtractMCPData .class );
114+ // Convert the mappings file to the desired format
115+ final TaskProvider <FormatSRG > createLegacyObf2Srg = project .getTasks ().register ("createLegacyObf2Srg" , FormatSRG .class , t -> {
116+ // Set the input SRG to the extract task's output
117+ t .getSrg ().set (extractSrg .flatMap (ExtractMCPData ::getOutput ));
118+ // Mods expect the classic SRG format
119+ t .getFormat ().set (IMappingFile .Format .SRG );
120+ });
121+ // Get the task's output file as a provider
122+ final Provider <File > mappingsFile = createLegacyObf2Srg .flatMap (t -> t .getOutput ().getAsFile ());
123+ // Attach the property along with the file path to each run configuration
124+ minecraft .getRuns ().configureEach (run -> run .property ("net.minecraftforge.gradle.GradleStart.srg.notch-srg" , mappingsFile .get ()));
125+
126+ // execute attachMappings before each run task
127+ project .getTasks ().named ("prepareRuns" ).configure (t -> t .dependsOn (createLegacyObf2Srg ));
128+ }
100129 }
101130
102131 public LegacyExtension (Project project ) {
@@ -122,6 +151,7 @@ public LegacyExtension(Project project) {
122151
123152 getFixClasspath ().convention (isLegacy );
124153 getExtractMappings ().convention (isLegacy );
154+ getAttachMappings ().convention (isLegacy );
125155 }
126156
127157 /**
@@ -140,8 +170,19 @@ public LegacyExtension(Project project) {
140170 * that points to a directory containing CSV mappings files. This is used by LegacyDev to remap dependencies' AT modifiers.
141171 * We replicate the FG2 behavior by extracting the mappings to a folder in the build directory
142172 * and setting the property to point to it.
143- * <p>
173+ *
144174 * Takes a boolean - true for apply fix, false for no fix.
145175 */
146176 public abstract Property <Boolean > getExtractMappings ();
177+
178+ /**
179+ * attachMappings;
180+ * FG2 GradleStart exposes a <code>net.minecraftforge.gradle.GradleStart.srg.notch-srg</code> property
181+ * that points to a NOTCH -> SRG mapping file. This can be consumed by mods to access obfuscation mappings at runtime.
182+ * We replicate the FG2 behavior by attaching the property to each run configuration and pointing it to
183+ * the output of the extractSrg task, which is the srg mapping file extracted from mcp config.
184+ *
185+ * Takes a boolean - true for apply fix, false for no fix.
186+ */
187+ public abstract Property <Boolean > getAttachMappings ();
147188}
0 commit comments