Skip to content

Commit 20f1238

Browse files
Su5eDSizableShrimp
authored andcommitted
Add LegacyExtension attachMappings patch
1 parent f93c005 commit 20f1238

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) Forge Development LLC and contributors
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
package net.minecraftforge.gradle.common.legacy;
7+
8+
import net.minecraftforge.srgutils.IMappingFile;
9+
import org.gradle.api.DefaultTask;
10+
import org.gradle.api.file.RegularFileProperty;
11+
import org.gradle.api.provider.Property;
12+
import org.gradle.api.tasks.Input;
13+
import org.gradle.api.tasks.InputFile;
14+
import org.gradle.api.tasks.OutputFile;
15+
import org.gradle.api.tasks.TaskAction;
16+
17+
import java.io.IOException;
18+
19+
public abstract class FormatSRG extends DefaultTask {
20+
private final Property<IMappingFile.Format> format;
21+
22+
public FormatSRG() {
23+
this.format = getProject().getObjects().property(IMappingFile.Format.class)
24+
.convention(IMappingFile.Format.SRG);
25+
getOutput().convention(getProject().getLayout().getBuildDirectory().dir(getName()).map(f -> f.file("output.srg")));
26+
}
27+
28+
@TaskAction
29+
public void apply() throws IOException {
30+
IMappingFile input = IMappingFile.load(getSrg().get().getAsFile());
31+
input.write(getOutput().get().getAsFile().toPath(), getFormat().get(), false);
32+
}
33+
34+
@InputFile
35+
public abstract RegularFileProperty getSrg();
36+
37+
@Input
38+
public Property<IMappingFile.Format> getFormat() {
39+
return this.format;
40+
}
41+
42+
public void setFormat(String value) {
43+
this.getFormat().set(IMappingFile.Format.valueOf(value));
44+
}
45+
46+
@OutputFile
47+
public abstract RegularFileProperty getOutput();
48+
}

src/common/java/net/minecraftforge/gradle/common/legacy/LegacyExtension.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
package net.minecraftforge.gradle.common.legacy;
77

88
import groovy.lang.GroovyObjectSupport;
9+
import net.minecraftforge.gradle.common.tasks.ExtractMCPData;
910
import net.minecraftforge.gradle.common.tasks.ExtractZip;
1011
import net.minecraftforge.gradle.common.util.MinecraftExtension;
12+
import net.minecraftforge.srgutils.IMappingFile;
1113
import net.minecraftforge.srgutils.MinecraftVersion;
1214
import org.gradle.api.Project;
1315
import org.gradle.api.artifacts.Dependency;
1416
import org.gradle.api.file.FileCollection;
1517
import org.gradle.api.provider.Property;
1618
import org.gradle.api.provider.Provider;
19+
import org.gradle.api.tasks.TaskProvider;
1720

1821
import 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

Comments
 (0)