Skip to content

Commit ecc87e0

Browse files
committed
More Math constants. Better constant replacement. Removed Test
1 parent 7b45a94 commit ecc87e0

5 files changed

Lines changed: 57 additions & 46 deletions

File tree

src/main/java/Test.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/main/java/org/mcphackers/mcp/tasks/TaskDecompile.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.mcphackers.mcp.tools.ProgressInfo;
88
import org.mcphackers.mcp.tools.Util;
99
import org.mcphackers.mcp.tools.constants.GLConstants;
10+
import org.mcphackers.mcp.tools.constants.MathConstants;
1011
import org.mcphackers.mcp.tools.fernflower.Decompiler;
1112
import org.mcphackers.mcp.tools.mcinjector.MCInjector;
1213

@@ -110,8 +111,9 @@ public void doTask() throws Exception {
110111
}
111112
break;
112113
case CONSTS:
114+
new MathConstants().replace(ffOut);
113115
if(hasLWJGL) {
114-
GLConstants.replace(ffOut);
116+
new GLConstants().replace(ffOut);
115117
}
116118
break;
117119
case COPYSRC:
@@ -181,7 +183,7 @@ public ProgressInfo getProgress() {
181183
return new ProgressInfo("Applying patches...", current, total);
182184
case CONSTS:
183185
current = 86;
184-
return new ProgressInfo("Replacing LWJGL constants...", current, total);
186+
return new ProgressInfo("Replacing constants...", current, total);
185187
case COPYSRC:
186188
current = 87;
187189
return new ProgressInfo("Copying sources...", current, total);

src/main/java/org/mcphackers/mcp/tools/constants/Constants.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
public abstract class Constants {
1616

17-
public static void replace(Path src) throws IOException {
17+
public void replace(Path src) throws IOException {
1818
Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
1919
@Override
2020
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
@@ -26,11 +26,9 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) thro
2626
});
2727
}
2828

29-
protected static String replace_constants(String code) {
30-
return code;
31-
}
29+
protected abstract String replace_constants(String code);
3230

33-
public static String replaceTextOfMatchGroup(String sourceString, Pattern pattern, Function<MatchResult,String> replaceStrategy) {
31+
public String replaceTextOfMatchGroup(String sourceString, Pattern pattern, Function<MatchResult,String> replaceStrategy) {
3432
Stack<MatchResult> startPositions = new Stack<>();
3533
Matcher matcher = pattern.matcher(sourceString);
3634

src/main/java/org/mcphackers/mcp/tools/constants/GLConstants.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,29 @@
1515

1616
public class GLConstants extends Constants {
1717

18-
private static final JSONObject json = getJson();
18+
private final JSONObject json;
1919

20-
private static final List _PACKAGES = json.getJSONArray("PACKAGES").toList();
21-
private static final Pattern _CALL_REGEX = Pattern.compile("(" + String.join("|", _PACKAGES) + ")\\.([\\w]+)\\(.+?\\)");
22-
private static final Pattern _CONSTANT_REGEX = Pattern.compile("(?<![-.\\w])\\d+(?![.\\w])");
23-
private static final Pattern _INPUT_REGEX = Pattern.compile("((Keyboard)\\.((getKeyName|isKeyDown)\\(.+?\\)|getEventKey\\(\\) == .+?(?=[\\);]))|new KeyBinding\\([ \\w\\\"]+, .+?\\))");
24-
private static final Pattern _IMPORT = Pattern.compile("import [.*\\w]+;");
25-
private static final Map _CONSTANTS_KEYBOARD = Util.jsonToMap(json.getJSONObject("CONSTANTS_KEYBOARD"));
26-
private static final List _CONSTANTS = Util.jsonToList(json.getJSONArray("CONSTANTS"));
20+
private final List _PACKAGES;
21+
private final Pattern _CALL_REGEX;
22+
private final Pattern _CONSTANT_REGEX;
23+
private final Pattern _INPUT_REGEX;
24+
private final Pattern _IMPORT;
25+
private final Map _CONSTANTS_KEYBOARD;
26+
private final List _CONSTANTS;
2727

28-
private static String updateImport(String code, String imp) {
28+
public GLConstants() throws JSONException, IOException {
29+
json = getJson();
30+
31+
_PACKAGES = json.getJSONArray("PACKAGES").toList();
32+
_CALL_REGEX = Pattern.compile("(" + String.join("|", _PACKAGES) + ")\\.([\\w]+)\\(.+?\\)");
33+
_CONSTANT_REGEX = Pattern.compile("(?<![-.\\w])\\d+(?![.\\w])");
34+
_INPUT_REGEX = Pattern.compile("((Keyboard)\\.((getKeyName|isKeyDown)\\(.+?\\)|getEventKey\\(\\) == .+?(?=[\\);]))|new KeyBinding\\([ \\w\\\"]+, .+?\\))");
35+
_IMPORT = Pattern.compile("import [.*\\w]+;");
36+
_CONSTANTS_KEYBOARD = Util.jsonToMap(json.getJSONObject("CONSTANTS_KEYBOARD"));
37+
_CONSTANTS = Util.jsonToList(json.getJSONArray("CONSTANTS"));
38+
}
39+
40+
private String updateImport(String code, String imp) {
2941
Matcher matcher = _IMPORT.matcher(code);
3042
int lastIndex = -1;
3143
while (matcher.find()) {
@@ -38,7 +50,7 @@ private static String updateImport(String code, String imp) {
3850
return code;
3951
}
4052

41-
protected static String replace_constants(String code) {
53+
protected String replace_constants(String code) {
4254
Set<String> imports = new HashSet<String>();
4355
code = replaceTextOfMatchGroup(code, _INPUT_REGEX, match1 -> {
4456
String full_call = match1.group(0);
@@ -77,12 +89,7 @@ protected static String replace_constants(String code) {
7789
return code;
7890
}
7991

80-
private static JSONObject getJson() {
81-
try {
82-
return Util.parseJSONFile(GLConstants.class.getClassLoader().getResourceAsStream("gl_constants.json"));
83-
} catch (JSONException | IOException e) {
84-
e.printStackTrace();
85-
return null;
86-
}
92+
private static JSONObject getJson() throws JSONException, IOException {
93+
return Util.parseJSONFile(GLConstants.class.getClassLoader().getResourceAsStream("gl_constants.json"));
8794
}
8895
}

src/main/java/org/mcphackers/mcp/tools/constants/MathConstants.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,38 @@
22

33
public class MathConstants extends Constants {
44

5-
protected static String replace_constants(String code) {
5+
protected String replace_constants(String code) {
66

7-
code = code.replace(Math.PI + "D", "Math.PI");
8-
code = code.replace((float)Math.PI + "F", "(float)Math.PI");
9-
code = code.replace((float)Math.PI / 2F + "F", "(float)Math.PI / 2F");
10-
code = code.replace((float)Math.PI / 4.5F + "F", "(float)Math.PI / 4.5F");
11-
7+
code = replaceValue(code, Math.PI, "Math.PI");
8+
code = replaceValue(code, (float)Math.PI, "(float)Math.PI");
9+
code = replaceValue(code, (float)Math.PI / 2F, "(float)Math.PI / 2F");
10+
code = replaceValue(code, (float)Math.PI / 4.5F, "(float)Math.PI / 4.5F");
11+
code = replaceValue(code, (double)(float)Math.PI, "(double)(float)Math.PI");
12+
code = replaceValue(code, Math.PI * 2D, "Math.PI * 2D");
13+
code = replaceValue(code, Math.PI / 2D, "Math.PI / 2D");
1214
for (int i = 1; i <= 100; i++) {
1315
double d = i * 0.01D;
1416
if(d != (double)(float)d) { // if imprecise
15-
code = code.replace((double)(float)d + "D", "(double)" + (float)d + "F");
17+
code = floatCastedToDouble(code, d);
1618
}
1719
}
20+
code = floatCastedToDouble(code, 0.0075D);
21+
code = replaceValue(code, (double)0.999F, "(double)0.999F");
22+
code = replaceValue(code, 1.0D / 128, "1.0D / 128");
23+
code = replaceValue(code, (double)0.997F, "(double)0.997F");
24+
code = replaceValue(code, (double)1.62F, "(double)1.62F");
1825
return code;
1926
}
27+
28+
private String floatCastedToDouble(String code, double value) {
29+
return code.replace((double)(float)value + "D", "(double)" + (float)value + "F");
30+
}
31+
32+
private String replaceValue(String code, double value, String replace) {
33+
return code.replace(value + "D", replace);
34+
}
35+
36+
private String replaceValue(String code, float value, String replace) {
37+
return code.replace(value + "F", replace);
38+
}
2039
}

0 commit comments

Comments
 (0)