Skip to content

Commit 3bb06c7

Browse files
committed
Support special case in mcp renaming
1 parent b17c95c commit 3bb06c7

File tree

2 files changed

+19
-48
lines changed

2 files changed

+19
-48
lines changed

src/common/java/net/minecraftforge/gradle/common/task/TaskApplyMappings.java

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,7 @@
2323
import java.io.File;
2424
import java.io.FileOutputStream;
2525
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.io.InputStreamReader;
28-
import java.io.StringWriter;
2926
import java.nio.charset.StandardCharsets;
30-
import java.util.HashMap;
31-
import java.util.Map;
32-
import java.util.regex.Matcher;
33-
import java.util.regex.Pattern;
3427
import java.util.zip.ZipFile;
3528
import java.util.zip.ZipOutputStream;
3629

@@ -40,39 +33,17 @@
4033
import org.gradle.api.tasks.OutputFile;
4134
import org.gradle.api.tasks.TaskAction;
4235

43-
import de.siegmar.fastcsv.reader.CsvContainer;
44-
import de.siegmar.fastcsv.reader.CsvReader;
45-
import de.siegmar.fastcsv.reader.CsvRow;
36+
import net.minecraftforge.gradle.common.util.McpNames;
4637
import net.minecraftforge.gradle.common.util.Utils;
4738

4839
public class TaskApplyMappings extends DefaultTask {
49-
private static final Pattern SRG = Pattern.compile("func_[0-9]+_[a-zA-Z_]+|field_[0-9]+_[a-zA-Z_]+|p_[\\w]+_\\d+_\\b");
50-
5140
private File mappings;
5241
private File input;
5342
private File output = getProject().file("build/" + getName() + "/output.zip");
5443

5544
@TaskAction
5645
public void apply() throws IOException {
57-
Map<String, String> names = new HashMap<>();
58-
try (ZipFile zip = new ZipFile(getMappings())) {
59-
zip.stream().filter(e -> e.getName().endsWith(".csv")).forEach(e -> {
60-
CsvReader reader = new CsvReader();
61-
reader.setContainsHeader(true);
62-
try {
63-
CsvContainer csv = reader.read(new InputStreamReader(zip.getInputStream(e)));
64-
for (CsvRow row : csv.getRows()) {
65-
if (row.getField("searge") != null) {
66-
names.put(row.getField("searge"), row.getField("name"));
67-
} else {
68-
names.put(row.getField("param"), row.getField("name"));
69-
}
70-
}
71-
} catch (IOException e1) {
72-
throw new RuntimeException(e1);
73-
}
74-
});
75-
}
46+
McpNames names = McpNames.load(getMappings());
7647

7748
try (ZipFile zin = new ZipFile(getInput())) {
7849
try (FileOutputStream fos = new FileOutputStream(getOutput());
@@ -83,7 +54,7 @@ public void apply() throws IOException {
8354
if (!e.getName().endsWith(".java")) {
8455
IOUtils.copy(zin.getInputStream(e), out);
8556
} else {
86-
out.write(replace(zin.getInputStream(e), names).getBytes(StandardCharsets.UTF_8));
57+
out.write(names.rename(zin.getInputStream(e), false).getBytes(StandardCharsets.UTF_8));
8758
}
8859
out.closeEntry();
8960
} catch (IOException e1) {
@@ -94,20 +65,6 @@ public void apply() throws IOException {
9465
}
9566
}
9667

97-
private String replace(InputStream stream, Map<String, String> names) throws IOException {
98-
StringWriter writer = new StringWriter();
99-
IOUtils.copy(stream, writer, StandardCharsets.UTF_8);
100-
StringBuffer buf = new StringBuffer();
101-
Matcher matcher = SRG.matcher(writer.toString());
102-
while (matcher.find()) {
103-
String name = names.get(matcher.group());
104-
matcher.appendReplacement(buf, name == null ? matcher.group() : name);
105-
}
106-
matcher.appendTail(buf);
107-
return buf.toString();
108-
}
109-
110-
11168
@InputFile
11269
public File getInput() {
11370
return input;

src/common/java/net/minecraftforge/gradle/common/util/McpNames.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.HashMap;
3030
import java.util.LinkedList;
3131
import java.util.List;
32+
import java.util.Locale;
3233
import java.util.Map;
3334
import java.util.regex.Matcher;
3435
import java.util.regex.Pattern;
@@ -47,7 +48,7 @@
4748

4849
public class McpNames {
4950
private static final String NEWLINE = System.getProperty("line.separator");
50-
private static final Pattern SRG_FINDER = Pattern.compile("func_[0-9]+_[a-zA-Z_]+|field_[0-9]+_[a-zA-Z_]+|p_[\\w]+_\\d+_\\b");
51+
private static final Pattern SRG_FINDER = Pattern.compile("[fF]unc_[0-9]+_[a-zA-Z_]+|[fF]ield_[0-9]+_[a-zA-Z_]+|p_[\\w]+_\\d+_\\b");
5152
private static final Pattern METHOD_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: {3})+|\\t+)(?!return)(?:\\w+\\s+)*(?<generic><[\\w\\W]*>\\s+)?(?<return>\\w+[\\w$.]*(?:<[\\w\\W]*>)?[\\[\\]]*)\\s+(?<name>func_[0-9]+_[a-zA-Z_]+)\\(");
5253
private static final Pattern FIELD_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: {3})+|\\t+)(?!return)(?:\\w+\\s+)*(?:\\w+[\\w$.]*(?:<[\\w\\W]*>)?[\\[\\]]*)\\s+(?<name>field_[0-9]+_[a-zA-Z_]+) *(?:=|;)");
5354
private static final Pattern CLASS_JAVADOC_PATTERN = Pattern.compile("^(?<indent>(?: )*|\\t*)([\\w|@]*\\s)*(class|interface|@interface|enum) (?<name>[\\w]+)");
@@ -180,11 +181,24 @@ private static void insertAboveAnnotations(List<String> list, String line) {
180181
list.add(list.size() - back, line);
181182
}
182183

184+
/*
185+
* There are certain times, such as Mixin Accessors that we wish to have the name of this method with the first character upper case.
186+
*/
187+
private String getMapped(String srg) {
188+
boolean cap = srg.charAt(0) == 'F';
189+
if (cap)
190+
srg = 'f' + srg.substring(1);
191+
String ret = names.getOrDefault(srg, srg);
192+
if (cap)
193+
ret = ret.substring(0, 1).toUpperCase(Locale.ENGLISH) + ret.substring(1);
194+
return ret;
195+
}
196+
183197
private String replaceInLine(String line) {
184198
StringBuffer buf = new StringBuffer();
185199
Matcher matcher = SRG_FINDER.matcher(line);
186200
while (matcher.find())
187-
matcher.appendReplacement(buf, names.getOrDefault(matcher.group(), matcher.group()));
201+
matcher.appendReplacement(buf, getMapped(matcher.group()));
188202
matcher.appendTail(buf);
189203
return buf.toString();
190204
}

0 commit comments

Comments
 (0)