-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackageForRelease.java
148 lines (138 loc) · 4.33 KB
/
PackageForRelease.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.lang.ProcessBuilder.Redirect;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* A simple utility that handles packaging the game and it's assets, so it's easier to publish them on github.
* It also does some code optimizations:
* - remove assert statements (they can prevent inlining when left in the code)
*/
public class PackageForRelease {
public static void copy(Path source, Path dest) throws Exception {
Files.copy(source, dest);
}
public static String filterAssertion(String text) {
char[] array = text.toCharArray();
char[] result = new char[array.length];
// Assertions start with a single "assert" keyword that's outside a string or variable name.
// Assertions may contain pieces of code that may contain semicolons.
// Assertions end with a semicolon.
boolean isString = false;
boolean isAssert = false;
int curlyBracketDepth = 0;
int normalBracketDepth = 0;
int squareBracketDepth = 0;
int curlyBracketDepthOfLastAssert = -1;
int normalBracketDepthOfLastAssert = -1;
int squareBracketDepthOfLastAssert = -1;
int resultSize = 0;
for(int i = 0; i < array.length; i++) {
switch(array[i]) {
case '"':
isString = !isString;
break;
case '{':
if(!isString)
curlyBracketDepth++;
break;
case '(':
if(!isString)
normalBracketDepth++;
break;
case '[':
if(!isString)
squareBracketDepth++;
break;
case '}':
if(!isString)
curlyBracketDepth--;
break;
case ')':
if(!isString)
normalBracketDepth--;
break;
case ']':
if(!isString)
squareBracketDepth--;
break;
case 'a':
// Start the assertion:
if((i == 0 || !Character.isAlphabetic(array[i-1])) // Make sure the assert isn't in the middle of a variable or function name.
&& array[i+1] == 's'
&& array[i+2] == 's'
&& array[i+3] == 'e'
&& array[i+4] == 'r'
&& array[i+5] == 't'
&& !Character.isAlphabetic(array[i+6])) { // Make sure the assert isn't in the middle of a variable or function name.
if(!isString && !isAssert) {
isAssert = true;
curlyBracketDepthOfLastAssert = curlyBracketDepth;
normalBracketDepthOfLastAssert = normalBracketDepth;
squareBracketDepthOfLastAssert = squareBracketDepth;
}
}
break;
case ';':
if(isAssert) {
if(curlyBracketDepthOfLastAssert == curlyBracketDepth
&& normalBracketDepthOfLastAssert == normalBracketDepth
&& squareBracketDepthOfLastAssert == squareBracketDepth) {
isAssert = false;
continue;
}
}
break;
default:
break;
}
if(!isAssert)
result[resultSize++] = array[i];
}
return new String(result, 0, resultSize);
}
public static void copyAndRemoveAssertions(File src, File destSrc) throws Exception {
if (Files.isDirectory(src.toPath())) {
Files.walk(src.toPath()).filter(path -> !Files.isDirectory(path)).forEach(path -> {
try {
String text = Files.readString(path);
text = filterAssertion(text);
Path outPath = destSrc.toPath().resolve(src.toPath().relativize(path));
Files.createDirectories(outPath.getParent());
Files.writeString(outPath, text);
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public static void deleteDir(File file) {
File[] contents = file.listFiles();
if (contents != null) {
for (File f : contents) {
deleteDir(f);
}
}
file.delete();
}
public static void mavenCompilePackage(File dir) throws Exception {
ProcessBuilder pb = new ProcessBuilder("mvn", "compile", "package");
pb.directory(dir);
pb.redirectOutput(Redirect.INHERIT);
Process p = pb.start();
p.waitFor();
}
public static void main(String[] args) throws Exception {
File output = new File("release");
deleteDir(output);
output.mkdirs();
copy(Path.of("pom.xml"), Path.of("release/pom.xml"));
copyAndRemoveAssertions(new File("src"), new File("release/src")); // Removes assertions from release code because they may cause performance problems.
mavenCompilePackage(output);
copy(Path.of("release/target/Cubyz.jar"), Path.of("release/Cubyz.jar"));
}
}