Skip to content

Commit 45b0165

Browse files
committed
Optimized constant replacing
1 parent 1409326 commit 45b0165

4 files changed

Lines changed: 58 additions & 58 deletions

File tree

src/main/java/org/mcphackers/mcp/tools/Util.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,13 @@ public static void copyDirectory(Path sourceFolder, Path targetFolder, String[]
219219
break;
220220
}
221221
}
222-
if(doCopy)
223-
try {
224-
Files.createDirectories(destination.getParent());
222+
if(doCopy) {
223+
try {
224+
Files.createDirectories(destination.getParent());
225225
Files.copy(source, destination);
226-
} catch (IOException e) {
227-
e.printStackTrace();
228-
}
226+
} catch (IOException e) {
227+
}
228+
}
229229
});
230230
}
231231

@@ -257,16 +257,12 @@ public static void compress(Path sourceDir, Path target) throws IOException {
257257
final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(target.toFile()));
258258
Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
259259
@Override
260-
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
261-
try {
262-
Path targetFile = sourceDir.relativize(file);
263-
outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
264-
byte[] bytes = Files.readAllBytes(file);
265-
outputStream.write(bytes, 0, bytes.length);
266-
outputStream.closeEntry();
267-
} catch (IOException e) {
268-
e.printStackTrace();
269-
}
260+
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
261+
Path targetFile = sourceDir.relativize(file);
262+
outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
263+
byte[] bytes = Files.readAllBytes(file);
264+
outputStream.write(bytes, 0, bytes.length);
265+
outputStream.closeEntry();
270266
return FileVisitResult.CONTINUE;
271267
}
272268
});
Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,68 @@
11
package org.mcphackers.mcp.tools.constants;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
35
import java.util.regex.Pattern;
46

57
public class MathConstants extends Constants {
68

79
// Used to prevent strings from being captured, such as "2.0D"
810
private static final Pattern _CONSTANT_REGEX = Pattern.compile("(?![\\\"\\'][.\\w\\s]*)-*\\d+\\.*\\w*(?![.\\w\\s]*[\\\"\\'])");
11+
private static final Map<String, String> _CONSTANTS = new HashMap<String, String>();
12+
13+
static {
14+
for (int i = 1; i <= 100; i++) {
15+
double d = i * 0.01D;
16+
if(d != (double)(float)d) { // if imprecise
17+
floatCastedToDouble((float)d);
18+
}
19+
}
20+
floatCastedToDouble(0.0075F);
21+
floatCastedToDouble(0.999F);
22+
floatCastedToDouble(0.997F);
23+
floatCastedToDouble(1.62F);
24+
replaceValue(Math.PI, "Math.PI");
25+
replaceValue((float)Math.PI, "(float)Math.PI");
26+
replaceValue((float)Math.PI / 2F, "(float)Math.PI / 2F");
27+
replaceValue((float)Math.PI / 4.5F, "(float)Math.PI / 4.5F");
28+
replaceValue((double)(float)Math.PI, "(double)(float)Math.PI");
29+
replaceValue(Math.PI * 2D, "Math.PI * 2D");
30+
replaceValue(Math.PI / 2D, "Math.PI / 2D");
31+
replaceValue(0xFFFFFF, "0xFFFFFF"); // TODO Might do this in fernflower at some point
32+
replaceValue(0x20200000, "0x20200000");
33+
replaceValue(0x20400000, "0x20400000");
34+
replaceValue(0xFF000000, "0xFF000000");
35+
replaceValue(2.0D / 256D, "2.0D / 256D");
36+
replaceValue(6.0D / 256D, "6.0D / 256D");
37+
replaceValue(7.0D / 256D, "7.0D / 256D");
38+
replaceValue(8.0D / 256D, "8.0D / 256D");
39+
replaceValue(9.0D / 256D, "9.0D / 256D");
40+
}
941

1042
protected String replace_constants(String code) {
1143
return replaceTextOfMatchGroup(code, _CONSTANT_REGEX, match1 -> {
1244
String constant = match1.group(0);
13-
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-
}
26-
}
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;
45+
return _CONSTANTS.containsKey(constant) ? _CONSTANTS.get(constant) : constant;
4246
});
4347
}
4448

45-
private String floatCastedToDouble(String code, float value) {
46-
return code.replace((double)value + "D", "(double)" + value + "F");
49+
private static String floatCastedToDouble(float value) {
50+
return _CONSTANTS.put((double)value + "D", "(double)" + value + "F");
4751
}
4852

49-
private String replaceValue(String code, double value, String replace) {
50-
return code.replace(value + "D", replace);
53+
private static String replaceValue(double value, String replace) {
54+
return _CONSTANTS.put(value + "D", replace);
5155
}
5256

53-
private String replaceValue(String code, float value, String replace) {
54-
return code.replace(value + "F", replace);
57+
private static String replaceValue(float value, String replace) {
58+
return _CONSTANTS.put(value + "F", replace);
5559
}
5660

57-
private String replaceValue(String code, int value, String replace) {
58-
return code.replace(value + "", replace);
61+
private static String replaceValue(int value, String replace) {
62+
return _CONSTANTS.put(String.valueOf(value), replace);
5963
}
6064

61-
private String replaceValue(String code, long value, String replace) {
62-
return code.replace(value + "L", replace);
65+
private static String replaceValue(long value, String replace) {
66+
return _CONSTANTS.put(value + "L", replace);
6367
}
6468
}

src/main/java/org/mcphackers/mcp/tools/fernflower/SRGJavadocProvider.java renamed to src/main/java/org/mcphackers/mcp/tools/fernflower/SimpleJavadocProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import java.util.HashMap;
1313
import java.util.Map;
1414

15-
public class SRGJavadocProvider implements IJavadocProvider {
15+
public class SimpleJavadocProvider implements IJavadocProvider {
1616

1717
private static final Map<String, String> classes = new HashMap<>();
1818
private static final Map<String, String> methods = new HashMap<>();
1919
private static final Map<String, String> fields = new HashMap<>();
2020

21-
public SRGJavadocProvider(File javadocFile) throws IOException {
21+
public SimpleJavadocProvider(File javadocFile) throws IOException {
2222
readMappings(javadocFile);
2323
}
2424

src/main/java/org/mcphackers/mcp/tools/fernflower/TinyJavadocProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public String getMethodDoc(StructClass structClass, StructMethod structMethod) {
101101
return null;
102102
}
103103

104-
private static MappingTree readMappings(File input) {
104+
private static MappingTree readMappings(File input) throws RuntimeException {
105105
try (BufferedReader reader = Files.newBufferedReader(input.toPath())) {
106106
MemoryMappingTree mappingTree = new MemoryMappingTree();
107107
MappingSourceNsSwitch nsSwitch = new MappingSourceNsSwitch(mappingTree, "named");

0 commit comments

Comments
 (0)