Skip to content

Commit 1409326

Browse files
committed
A whole lot of more constants
1 parent ecc87e0 commit 1409326

3 files changed

Lines changed: 72 additions & 40 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public TaskRecompile(int side, TaskInfo info) {
3131
@Override
3232
public void doTask() throws Exception {
3333
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
34+
if(compiler == null) {
35+
throw new RuntimeException("Could not find compiling API");
36+
}
3437
DiagnosticCollector<JavaFileObject> ds = new DiagnosticCollector<>();
3538

3639
Path binPath = Paths.get(chooseFromSide(MCPConfig.CLIENT_BIN, MCPConfig.SERVER_BIN));

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Map;
55
import java.util.Map.Entry;
66
import java.util.Set;
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
79
import java.util.HashSet;
810
import java.util.List;
911
import java.util.regex.Matcher;
@@ -15,26 +17,21 @@
1517

1618
public class GLConstants extends Constants {
1719

18-
private final JSONObject json;
20+
private static final JSONObject json = getJson();
21+
private static Exception cause;
1922

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;
23+
private static final List _PACKAGES = json == null ? new ArrayList() : json.getJSONArray("PACKAGES").toList();
24+
private static final List _CONSTANTS = json == null ? new ArrayList() : Util.jsonToList(json.getJSONArray("CONSTANTS"));
25+
private static final Map _CONSTANTS_KEYBOARD = json == null ? new HashMap() : Util.jsonToMap(json.getJSONObject("CONSTANTS_KEYBOARD"));
26+
private static final Pattern _CALL_REGEX = Pattern.compile("(" + String.join("|", _PACKAGES) + ")\\.([\\w]+)\\(.+?\\)");
27+
private static final Pattern _CONSTANT_REGEX = Pattern.compile("(?<![-.\\w])\\d+(?![.\\w])");
28+
private static final Pattern _INPUT_REGEX = Pattern.compile("((Keyboard)\\.((getKeyName|isKeyDown)\\(.+?\\)|getEventKey\\(\\) == .+?(?=[\\);]))|new KeyBinding\\([ \\w\\\"]+, .+?\\))");
29+
private static final Pattern _IMPORT = Pattern.compile("import [.*\\w]+;");
2730

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"));
31+
public GLConstants() throws Exception {
32+
if (cause != null) {
33+
throw new Exception("Could not initialize constants", cause);
34+
}
3835
}
3936

4037
private String updateImport(String code, String imp) {
@@ -89,7 +86,14 @@ protected String replace_constants(String code) {
8986
return code;
9087
}
9188

92-
private static JSONObject getJson() throws JSONException, IOException {
93-
return Util.parseJSONFile(GLConstants.class.getClassLoader().getResourceAsStream("gl_constants.json"));
89+
private static JSONObject getJson() {
90+
Exception ex = null;
91+
try {
92+
return Util.parseJSONFile(GLConstants.class.getClassLoader().getResourceAsStream("gl_constants.json"));
93+
} catch (JSONException | IOException e) {
94+
ex = e;
95+
}
96+
cause = ex;
97+
return null;
9498
}
9599
}
Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,49 @@
11
package org.mcphackers.mcp.tools.constants;
22

3+
import java.util.regex.Pattern;
4+
35
public class MathConstants extends Constants {
46

7+
// Used to prevent strings from being captured, such as "2.0D"
8+
private static final Pattern _CONSTANT_REGEX = Pattern.compile("(?![\\\"\\'][.\\w\\s]*)-*\\d+\\.*\\w*(?![.\\w\\s]*[\\\"\\'])");
9+
510
protected String replace_constants(String code) {
11+
return replaceTextOfMatchGroup(code, _CONSTANT_REGEX, match1 -> {
12+
String constant = match1.group(0);
613

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");
14-
for (int i = 1; i <= 100; i++) {
15-
double d = i * 0.01D;
16-
if(d != (double)(float)d) { // if imprecise
17-
code = floatCastedToDouble(code, d);
14+
constant = replaceValue(constant, Math.PI, "Math.PI");
15+
constant = replaceValue(constant, (float)Math.PI, "(float)Math.PI");
16+
constant = replaceValue(constant, (float)Math.PI / 2F, "(float)Math.PI / 2F");
17+
constant = replaceValue(constant, (float)Math.PI / 4.5F, "(float)Math.PI / 4.5F");
18+
constant = replaceValue(constant, (double)(float)Math.PI, "(double)(float)Math.PI");
19+
constant = replaceValue(constant, Math.PI * 2D, "Math.PI * 2D");
20+
constant = replaceValue(constant, Math.PI / 2D, "Math.PI / 2D");
21+
for (int i = 1; i <= 100; i++) {
22+
double d = i * 0.01D;
23+
if(d != (double)(float)d) { // if imprecise
24+
constant = floatCastedToDouble(constant, (float)d);
25+
}
1826
}
19-
}
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");
25-
return code;
27+
constant = floatCastedToDouble(constant, 0.0075F);
28+
constant = floatCastedToDouble(constant, 0.999F);
29+
constant = floatCastedToDouble(constant, 0.997F);
30+
constant = floatCastedToDouble(constant, 1.62F);
31+
constant = replaceValue(constant, 0xFFFFFF, "0xFFFFFF"); // TODO Might do this in fernflower at some point
32+
constant = replaceValue(constant, 0x20200000, "0x20200000");
33+
constant = replaceValue(constant, 0x20400000, "0x20400000");
34+
constant = replaceValue(constant, 0xFF000000, "0xFF000000");
35+
//brugh
36+
constant = replaceValue(constant, 2.0D / 256D, "2.0D / 256D");
37+
constant = replaceValue(constant, 6.0D / 256D, "6.0D / 256D");
38+
constant = replaceValue(constant, 7.0D / 256D, "7.0D / 256D");
39+
constant = replaceValue(constant, 8.0D / 256D, "8.0D / 256D");
40+
constant = replaceValue(constant, 9.0D / 256D, "9.0D / 256D");
41+
return constant;
42+
});
2643
}
2744

28-
private String floatCastedToDouble(String code, double value) {
29-
return code.replace((double)(float)value + "D", "(double)" + (float)value + "F");
45+
private String floatCastedToDouble(String code, float value) {
46+
return code.replace((double)value + "D", "(double)" + value + "F");
3047
}
3148

3249
private String replaceValue(String code, double value, String replace) {
@@ -36,4 +53,12 @@ private String replaceValue(String code, double value, String replace) {
3653
private String replaceValue(String code, float value, String replace) {
3754
return code.replace(value + "F", replace);
3855
}
56+
57+
private String replaceValue(String code, int value, String replace) {
58+
return code.replace(value + "", replace);
59+
}
60+
61+
private String replaceValue(String code, long value, String replace) {
62+
return code.replace(value + "L", replace);
63+
}
3964
}

0 commit comments

Comments
 (0)