Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jul 11, 2026. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPackageForRelease.java
More file actions
Latest commit
148 lines (138 loc) · 4.33 KB
/
Copy pathPackageForRelease.java
File metadata and controls
148 lines (138 loc) · 4.33 KB
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
importjava.io.BufferedInputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.lang.ProcessBuilder.Redirect;
importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.nio.file.Files;
importjava.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)
*/
publicclassPackageForRelease {
publicstaticvoidcopy(Pathsource, Pathdest) throwsException {
Files.copy(source, dest);
}
publicstaticStringfilterAssertion(Stringtext) {
char[] array = text.toCharArray();
char[] result = newchar[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.
booleanisString = false;
booleanisAssert = false;
intcurlyBracketDepth = 0;
intnormalBracketDepth = 0;
intsquareBracketDepth = 0;
intcurlyBracketDepthOfLastAssert = -1;
intnormalBracketDepthOfLastAssert = -1;
intsquareBracketDepthOfLastAssert = -1;
intresultSize = 0;
for(inti = 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];
}
returnnewString(result, 0, resultSize);
}
publicstaticvoidcopyAndRemoveAssertions(Filesrc, FiledestSrc) throwsException {
if (Files.isDirectory(src.toPath())) {
Files.walk(src.toPath()).filter(path -> !Files.isDirectory(path)).forEach(path -> {
try {
Stringtext = Files.readString(path);
text = filterAssertion(text);
PathoutPath = destSrc.toPath().resolve(src.toPath().relativize(path));
Files.createDirectories(outPath.getParent());
Files.writeString(outPath, text);
} catch (Exceptione) {
e.printStackTrace();
}
});
}
}
publicstaticvoiddeleteDir(Filefile) {
File[] contents = file.listFiles();
if (contents != null) {
for (Filef : contents) {
deleteDir(f);
}
}
file.delete();
}
publicstaticvoidmavenCompilePackage(Filedir) throwsException {
ProcessBuilderpb = newProcessBuilder("mvn", "compile", "package");
pb.directory(dir);
pb.redirectOutput(Redirect.INHERIT);
Processp = pb.start();
p.waitFor();
}
publicstaticvoidmain(String[] args) throwsException {
Fileoutput = newFile("release");
deleteDir(output);
output.mkdirs();
copy(Path.of("pom.xml"), Path.of("release/pom.xml"));
copyAndRemoveAssertions(newFile("src"), newFile("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"));
}
}