Skip to content

Commit 3dd7c42

Browse files
committed
FG 1.0 variant of FmlCleanup
1 parent d76007d commit 3dd7c42

4 files changed

Lines changed: 292 additions & 29 deletions

File tree

src/main/java/net/minecraftforge/mcpcleanup/FFPatcher.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,13 @@ public static String processSimple(String text) {
5050
}
5151

5252
public static String processFile(String name, String text, boolean enumArgs, boolean enumCtr) {
53-
StringBuffer out = new StringBuffer();
54-
Matcher m = SYNTHETICS.matcher(text);
55-
while(m.find())
56-
m.appendReplacement(out, synthetic_replacement(m).replace("$", "\\$"));
57-
m.appendTail(out);
58-
text = out.toString();
59-
53+
text = removeSynthetics(text);
6054
text = replaceAll(text, TRAILING, "");
6155
text = replaceAll(text, TRAILINGZERO, "$1$2");
6256
text = processClass(name, text, enumArgs, enumCtr);
6357
text = replaceAll(text, NEWLINES, NEWLINE);
64-
text = replaceAll(text, EMPTY_SUPER, "");
65-
66-
// fix interfaces (added 1.7.10+)
67-
out = new StringBuffer();
68-
m = ABSTRACT.matcher(text);
69-
while (m.find())
70-
m.appendReplacement(out, abstract_replacement(m).replace("$", "\\$"));
71-
m.appendTail(out);
72-
73-
return out.toString();
58+
text = replaceAll(text, EMPTY_SUPER, "");;
59+
return text;
7460
}
7561

7662
static String processClass(String name, String text, boolean enumArgs, boolean enumCtr) {
@@ -218,6 +204,16 @@ private static void processEnum(String name, List<String> lines, String indent,
218204
}
219205
}
220206

207+
208+
private static String removeSynthetics(String text) {
209+
StringBuffer out = new StringBuffer();
210+
Matcher m = SYNTHETICS.matcher(text);
211+
while(m.find())
212+
m.appendReplacement(out, synthetic_replacement(m).replace("$", "\\$"));
213+
m.appendTail(out);
214+
return out.toString();
215+
}
216+
221217
private static String synthetic_replacement(Matcher match) {
222218
//This is designed to remove all the synthetic/bridge methods that the compiler will just generate again
223219
//First off this only works on methods that bounce to methods that are named exactly alike.
@@ -251,6 +247,30 @@ private static String synthetic_replacement(Matcher match) {
251247
return match.group();
252248
}
253249

250+
// Replace parameter names with srg names in abstract methods
251+
public static String renameAbstracts(String name, String text) {
252+
// fix interfaces (added 1.7.10+)
253+
List<String> lines = NewLineDetector.readLines(text);
254+
for (int x = 0; x < lines.size(); x++) {
255+
String line = lines.get(x);
256+
// Quick optimization we only care about abstract methods, which always end with ;
257+
if (!line.endsWith(";"))
258+
continue;
259+
260+
Matcher m = ABSTRACT.matcher(line);
261+
if (m.find()) {
262+
StringBuffer out = new StringBuffer();
263+
do {
264+
m.appendReplacement(out, abstract_replacement(m).replace("$", "\\$"));
265+
} while (m.find());
266+
m.appendTail(out);
267+
lines.set(x, out.toString());
268+
}
269+
270+
}
271+
return String.join(NEWLINE, lines);
272+
}
273+
254274
private static String abstract_replacement(Matcher match) {
255275
String orig = match.group("arguments");
256276
String number = match.group("number");

src/main/java/net/minecraftforge/mcpcleanup/FmlCleanup.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,18 @@ class FmlCleanup {
3131
private static final Pattern BEFORE = Pattern.compile("(?m)((case|default).+(?:\\r\\n|\\r|\\n))(?:\\r\\n|\\r|\\n)");
3232
private static final Pattern AFTER = Pattern.compile("(?m)(?:\\r\\n|\\r|\\n)((?:\\r\\n|\\r|\\n)[ \\t]+(case|default))");
3333

34-
private static final Comparator<String> COMPARATOR = new Comparator<String>() {
35-
@Override
36-
public int compare(String str1, String str2) {
37-
return str2.length() - str1.length();
38-
}
39-
};
34+
private static final Comparator<String> COMPARATOR = (str1, str2) -> str2.length() - str1.length();
4035

41-
public static String cleanup(String text, boolean fixCases) {
36+
public static String cleanup(String name, String text, boolean fixCases) {
4237
if (fixCases) {
4338
text = BEFORE.matcher(text).replaceAll("$1");
4439
text = AFTER.matcher(text).replaceAll("$1");
4540
}
46-
text = FmlCleanup.renameClass(text);
41+
text = FmlCleanup.renameClass(name, text);
4742
return text;
4843
}
4944

50-
private static String renameClass(String text) {
45+
private static String renameClass(String name, String text) {
5146
String[] lines = text.split("(\r\n|\r|\n)");
5247
List<String> output = new ArrayList<String>(lines.length);
5348
MethodInfo method = null;
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
/*
2+
* Copyright (c) Forge Development LLC and contributors
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.mcpcleanup;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.Comparator;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Locale;
13+
import java.util.regex.Matcher;
14+
import java.util.regex.Pattern;
15+
16+
// FG 1.0/Python version of FmlCleanup
17+
// This has the issue of not renaming inner classes
18+
class FmlCleanup1 {
19+
private static final Pattern METHOD_REG = Pattern.compile("^ {4}(\\w+\\s+\\S.*\\(.*|static)$");
20+
private static final Pattern CATCH_REG = Pattern.compile("catch \\((.*)\\)$");
21+
private static final Pattern NESTED_PERINTH = Pattern.compile("\\(.*\\(");
22+
private static final Pattern METHOD_PARAMS = Pattern.compile("\\((.+)\\)");
23+
private static final Pattern METHOD_DEC_END = Pattern.compile("(}|\\);|throws .+?;)$");
24+
private static final Pattern METHOD_END = Pattern.compile("^ {4}\\}$");
25+
private static final Pattern CAPS_START = Pattern.compile("^[A-Z]");
26+
private static final Pattern ARRAY = Pattern.compile("(\\[|\\.\\.\\.)");
27+
private static final Pattern VAR_CALL = Pattern.compile("(?i)[a-z_$][a-z0-9_\\[\\]]+ var\\d+");
28+
private static final Pattern VAR = Pattern.compile("var\\d+");
29+
30+
private static final Comparator<String> COMPARATOR = (str1, str2) -> str2.length() - str1.length();
31+
32+
public static String cleanup(String text) {
33+
String[] lines = text.split("(\r\n|\r|\n)");
34+
String output = "";
35+
36+
boolean insideMethod = false;
37+
String method = "";
38+
ArrayList<String> methodVars = new ArrayList<String>();
39+
boolean skip = false;
40+
41+
for (String line : lines) {
42+
// if re.search(METHOD_REG, line) and not re.search('=', line) and not re.search(r'\(.*\(', line):
43+
if (METHOD_REG.matcher(line).find() && !line.contains("=") && !NESTED_PERINTH.matcher(line).find()) {
44+
// if re.search(r'\(.+\)', line):
45+
Matcher match = METHOD_PARAMS.matcher(line);
46+
if (match.find()) {
47+
// method_variables += [s.strip() for s in re.search(r'\((.+)\)', line).group(1).split(',')]
48+
String[] pts = match.group(1).split(",");
49+
for (String str : pts) {
50+
str = str.trim();
51+
methodVars.add(str);
52+
}
53+
}
54+
55+
method += line + '\n';
56+
// method += line
57+
58+
// single line method?
59+
skip = true;
60+
61+
// if not re.search(r'(}|\);|throws .+?;)$', line):
62+
if (!METHOD_DEC_END.matcher(line).find())
63+
insideMethod = true;
64+
//elif re.search(r'^ {%s}}$' % indent, line):
65+
} else if (METHOD_END.matcher(line).find()) {
66+
//inside_method = False
67+
insideMethod = false;
68+
}
69+
70+
// inside method actions now.
71+
if (insideMethod) {
72+
if (skip) {
73+
skip = false;
74+
continue;
75+
}
76+
77+
method += line + '\n';
78+
79+
Matcher matcher = CATCH_REG.matcher(line);
80+
if (matcher.find())
81+
methodVars.add(matcher.group(1));
82+
else {
83+
Matcher match = VAR_CALL.matcher(line);
84+
while (match.find()) {
85+
if (!match.group().startsWith("return") && !match.group().startsWith("throw"))
86+
methodVars.add(match.group());
87+
}
88+
}
89+
} else {
90+
if (method != null && !method.isEmpty()){
91+
FmlCleanup1 namer = new FmlCleanup1();
92+
HashMap<String, String> todo = new HashMap<String, String>();
93+
94+
for (String var : methodVars) {
95+
String[] split = var.split(" ");
96+
if (split.length > 1)
97+
todo.put(split[1], namer.getName(split[0], split[1]));
98+
else
99+
System.out.printf("Unknown thing : %s (%s)\n", var, method);
100+
}
101+
102+
List<String> sortedKeys = new ArrayList<String>(todo.keySet());
103+
Collections.sort(sortedKeys, COMPARATOR);
104+
105+
// closure changes the sort, to sort by the return value of the closure.
106+
for (String key : sortedKeys) {
107+
if (VAR.matcher(key).matches())
108+
method = method.replace(key, todo.get(key));
109+
}
110+
111+
output += method;
112+
113+
// clear methods.
114+
methodVars.clear();
115+
method = "";
116+
}
117+
118+
if (skip) {
119+
skip = false;
120+
continue;
121+
}
122+
123+
output += line + '\n';
124+
}
125+
}
126+
127+
return output;
128+
}
129+
130+
private final HashMap<String, Holder> last = new HashMap<String, Holder>();
131+
private final HashMap<String, String> remap = new HashMap<String, String>();
132+
133+
private FmlCleanup1() {
134+
last.put("byte", new Holder(0, false, "b"));
135+
last.put("char", new Holder(0, false, "c"));
136+
last.put("short", new Holder(1, false, "short"));
137+
last.put("int", new Holder(0, true, "i", "j", "k", "l"));
138+
last.put("boolean", new Holder(0, true, "flag"));
139+
last.put("double", new Holder(0, false, "d"));
140+
last.put("float", new Holder(0, true, "f"));
141+
last.put("File", new Holder(1, true, "file"));
142+
last.put("String", new Holder(0, true, "s"));
143+
last.put("Class", new Holder(0, true, "oclass"));
144+
last.put("Long", new Holder(0, true, "olong"));
145+
last.put("Byte", new Holder(0, true, "obyte"));
146+
last.put("Short", new Holder(0, true, "oshort"));
147+
last.put("Boolean", new Holder(0, true, "obool"));
148+
last.put("Package", new Holder(0, true, "opackage"));
149+
remap.put("long", "int");
150+
}
151+
152+
private String getName(String type, String var) {
153+
String index = null;
154+
if (last.containsKey(type))
155+
index = type;
156+
else if (remap.containsKey(type))
157+
index = remap.get(type);
158+
159+
if ((index == null || index.isEmpty()) && (CAPS_START.matcher(type).find() || ARRAY.matcher(type).find())) {
160+
// replace multi things with arrays.
161+
type = type.replace("...", "[]");
162+
163+
while (type.contains("[][]"))
164+
type = type.replaceAll("\\[\\]\\[\\]", "[]");
165+
166+
String name = lower(type);
167+
boolean skip_zero = true;
168+
169+
//if (Pattern.compile("\\[").matcher(type).find()) {
170+
if (type.indexOf('[') != -1) {
171+
skip_zero = true;
172+
name = "a" + name;
173+
name = name.replace("[]", "").replace("...", "");
174+
}
175+
176+
last.put(type, new Holder(0, skip_zero, name));
177+
index = type;
178+
}
179+
180+
if (index == null || index.isEmpty()) {
181+
//Debug: System.out.println("NO DATA FOR TYPE " + type + " " + var);
182+
return lower(type);
183+
}
184+
185+
Holder holder = last.get(index);
186+
int id = holder.id;
187+
List<String> data = holder.data;
188+
189+
int ammount = data.size();
190+
191+
String name;
192+
if (ammount == 1) {
193+
name = data.get(0) + (id == 0 && holder.skip_zero ? "" : id);
194+
} else {
195+
int num = id / ammount;
196+
name = data.get(id % ammount) + (id < ammount && holder.skip_zero ? "" : num);
197+
}
198+
199+
holder.id++;
200+
return name;
201+
}
202+
203+
private static String lower(String string) {
204+
return string.toLowerCase(Locale.ENGLISH);
205+
}
206+
207+
private class Holder {
208+
int id;
209+
final public boolean skip_zero;
210+
final ArrayList<String> data = new ArrayList<String>();
211+
212+
public Holder(int t1, boolean skip_zero, String... stuff) {
213+
this.id = t1;
214+
this.skip_zero = skip_zero;
215+
Collections.addAll(this.data, stuff);
216+
}
217+
}
218+
}

0 commit comments

Comments
 (0)