Repository navigation
Expand file tree
/
Copy pathDumpToClient.java
More file actions
447 lines (429 loc) · 19.4 KB
/
Copy pathDumpToClient.java
File metadata and controls
447 lines (429 loc) · 19.4 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
class DumpToClient {
private static final Pattern VAR_DECL = Pattern.compile(
"^\\s+(\\d+) \\| +((?:struct )?BWAPI[^:]*::)?(\\S[^\\[]+)\\s([\\[0-9\\]]+)?\\s?(\\S+)$");
private static final Pattern SIZE_DECL = Pattern.compile(
"^\\s+\\| \\[sizeof=(\\d+), align=\\d+.+");
public static void main(String[] args) throws IOException {
Map<String, Struct> structs = new HashMap<>();
List<String> lines = Files.readAllLines(Paths.get("bwapistructs/dump"));
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
if (line.contains("| struct BWAPI")) {
String structName = line.substring(line.indexOf(':') + 2);
Struct struct = structs.computeIfAbsent(structName, Struct::new);
System.out.println("Parsing: " + structName);
while (++i < lines.size()) {
line = lines.get(i);
if (line.contains(" class ")) {
continue;
}
Matcher matcher = VAR_DECL.matcher(line);
if (!matcher.matches()) {
Matcher sizeMatcher = SIZE_DECL.matcher(line);
if (sizeMatcher.matches()) {
struct.size = Integer.parseInt(sizeMatcher.group(1));
}
break;
}
String varName = matcher.group(5);
String typeName = matcher.group(3);
Variable var;
switch (typeName) {
case "int":
var = new Variable(varName, Type.INT);
break;
case "_Bool":
var = new Variable(varName, Type.BOOLEAN);
break;
case "char":
var = new Variable(varName, Type.CHAR);
break;
case "unsigned short":
var = new Variable(varName, Type.UNSIGNED_SHORT);
break;
case "unsigned int":
var = new Variable(varName, Type.UNSIGNED_INT);
break;
case "double":
var = new Variable(varName, Type.DOUBLE);
break;
default:
if (typeName.endsWith("::Enum")) {
var = new Variable(varName, Type.ENUM);
var.enumName = typeName.substring(0, typeName.length() - 6);
} else {
Struct structRef = structs.computeIfAbsent(typeName, Struct::new);
var = new Variable(varName, Type.STRUCT);
var.structRef = structRef;
}
}
var.offset = Integer.parseInt(matcher.group(1));
if (matcher.group(4) != null) {
var.arraySizes = Arrays.stream(matcher.group(4).split("]\\[|]|\\["))
.filter(s -> !s.isEmpty())
.map(Integer::parseInt).collect(Collectors.toList());
}
struct.variables.add(var);
}
}
}
StringWriter sw = new StringWriter();
try (PrintWriter out = new PrintWriter(sw)) {
out.println("package bwapi;");
out.println("");
out.println("import com.sun.jna.Pointer;");
out.println("");
out.println("class ClientData {");
out.println(" WrappedBuffer buffer;");
out.println(" GameData gameData;");
out.println(" ClientData() {");
out.println(" gameData = createGameData(0);");
out.println(" }");
out.println(" GameData createGameData(int offset) {");
out.println(" return new ClientData.GameData(offset);");
out.println(" }");
out.println(" GameData gameData() {");
out.println(" return gameData;");
out.println(" }");
out.println(" void setBuffer(WrappedBuffer buffer) {");
out.println(" this.buffer = buffer;");
out.println(" }");
out.println(" void setPointer(Pointer pointer) {");
out.println(" setBuffer(new WrappedBuffer(pointer, GameData.SIZE));");
out.println(" }");
out.println(" WrappedBuffer getStaticBuffer() {");
out.println(" return buffer;");
out.println(" }");
out.println("");
structs.values().forEach(s -> {
String bufName = s.name.equals("RegionData") ? "getStaticBuffer()" : "buffer";
out.printf(" class %s {\n", s.name);
out.printf(" static final int SIZE = %d;\n", s.size);
// GameData.myOffset needs to be accessible from AsyncGameData subclass
String access = s.name.equals("GameData") ? "protected" : "private";
out.printf(" %s int myOffset;\n", access);
out.printf(" %s(int myOffset) {\n", s.name);
out.println(" this.myOffset = myOffset;");
out.println(" }");
s.variables.forEach(v -> generateVariable(out, s, v, bufName, null, false));
out.println(" }");
});
out.println("}");
}
String clientDataText = sw.toString().replace("\r\n", "\n").replace("\n", "\r\n");
Files.write(Paths.get("src/main/java/bwapi/ClientData.java"),
clientDataText.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
StringWriter swAsync = new StringWriter();
try (PrintWriter out = new PrintWriter(swAsync)) {
out.println("package bwapi;");
out.println("");
out.println("/**");
out.println(" * Asynchronous client data that routes static information to the main/static shared buffer");
out.println(" * and dynamic information to the packed dynamic buffer.");
out.println(" * Automatically generated by DumpToClient.");
out.println(" */");
out.println("class AsyncClientData extends ClientData {");
out.println(" static final int STATICTILES_START = 3447004;");
out.println(" static final int STATICTILES_END = 4823260;");
out.println(" static final int REGION_START = 5085404;");
out.println(" static final int REGION_END = 10586480;");
out.println(" static final int STRINGSSHAPES_START = 10962632;");
out.println(" static final int STRINGSHAPES_END = 32242636;");
out.println(" static final int UNITFINDER_START = 32962644;");
out.println("");
out.println(" static final int DYNAMIC_SLICE1_SIZE = STATICTILES_START;");
out.println(" static final int DYNAMIC_SLICE2_SIZE = REGION_START - STATICTILES_END;");
out.println(" static final int DYNAMIC_SLICE3_SIZE = STRINGSSHAPES_START - REGION_END;");
out.println(" static final int DYNAMIC_SLICE4_SIZE = UNITFINDER_START - STRINGSHAPES_END;");
out.println("");
out.println(" static final int DEST_SLICE1_OFFSET = 0;");
out.println(" static final int DEST_SLICE2_OFFSET = DYNAMIC_SLICE1_SIZE;");
out.println(" static final int DEST_SLICE3_OFFSET = DEST_SLICE2_OFFSET + DYNAMIC_SLICE2_SIZE;");
out.println(" static final int DEST_SLICE4_OFFSET = DEST_SLICE3_OFFSET + DYNAMIC_SLICE3_SIZE;");
out.println("");
out.println(" static final int BUFFER_SIZE = DEST_SLICE4_OFFSET + DYNAMIC_SLICE4_SIZE;");
out.println("");
out.println(" static final int DELTA_RANGE2 = STATICTILES_END - STATICTILES_START;");
out.println(" static final int DELTA_RANGE3 = DELTA_RANGE2 + (REGION_END - REGION_START);");
out.println(" static final int DELTA_RANGE4 = DELTA_RANGE3 + (STRINGSHAPES_END - STRINGSSHAPES_START);");
out.println("");
out.println(" private WrappedBuffer staticBuffer;");
out.println("");
out.println(" AsyncClientData() {");
out.println(" super();");
out.println(" }");
out.println("");
out.println(" AsyncClientData(WrappedBuffer staticBuffer) {");
out.println(" super();");
out.println(" this.staticBuffer = staticBuffer;");
out.println(" }");
out.println("");
out.println(" void setStaticBuffer(WrappedBuffer staticBuffer) {");
out.println(" this.staticBuffer = staticBuffer;");
out.println(" }");
out.println("");
out.println(" @Override");
out.println(" WrappedBuffer getStaticBuffer() {");
out.println(" return staticBuffer != null ? staticBuffer : buffer;");
out.println(" }");
out.println("");
out.println(" @Override");
out.println(" GameData createGameData(int offset) {");
out.println(" return new AsyncGameData(offset);");
out.println(" }");
out.println("");
out.println(" class AsyncGameData extends GameData {");
out.println(" AsyncGameData(int myOffset) {");
out.println(" super(myOffset);");
out.println(" }");
out.println("");
out.println(" private int deltaRange2() {");
out.println(" return buffer != null && buffer.isPacked() ? DELTA_RANGE2 : 0;");
out.println(" }");
out.println("");
out.println(" private int deltaRange3() {");
out.println(" return buffer != null && buffer.isPacked() ? DELTA_RANGE3 : 0;");
out.println(" }");
out.println("");
out.println(" private int deltaRange4() {");
out.println(" return buffer != null && buffer.isPacked() ? DELTA_RANGE4 : 0;");
out.println(" }");
out.println("");
Struct gameData = structs.get("GameData");
for (Variable v : gameData.variables) {
boolean isStaticTile = v.offset >= 3447004 && v.offset < 4823260;
boolean isDynamicTile = v.offset >= 4823260 && v.offset < 5085404;
boolean isStaticRegion = v.offset >= 5085404 && v.offset < 10586480;
boolean isDynamicGame = v.offset >= 10586480 && v.offset < 10962632;
boolean isDynamicCommand = v.offset >= 32242636 && v.offset < 32962644;
if (isStaticTile || isStaticRegion) {
generateVariable(out, gameData, v, "getStaticBuffer()", null, true);
} else if (isDynamicTile) {
generateVariable(out, gameData, v, "buffer", "deltaRange2()", true);
} else if (isDynamicGame) {
generateVariable(out, gameData, v, "buffer", "deltaRange3()", true);
} else if (isDynamicCommand) {
generateVariable(out, gameData, v, "buffer", "deltaRange4()", true);
}
}
out.println(" }");
out.println("}");
}
String asyncClientDataText = swAsync.toString().replace("\r\n", "\n");
Files.write(Paths.get("src/main/java/bwapi/AsyncClientData.java"),
asyncClientDataText.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
}
private static void generateVariable(PrintWriter out, Struct s, Variable v, String bufName, String delta, boolean isOverride) {
if (isOverride) {
out.println(" @Override");
}
out.print(" ");
switch (v.type) {
case INT:
out.print("int");
break;
case CHAR:
if (v.arraySizes.isEmpty()) {
out.print("char");
} else {
out.print("String");
}
break;
case ENUM:
out.print(v.enumName);
break;
case DOUBLE:
out.print("double");
break;
case STRUCT:
out.print(v.structRef.name);
break;
case BOOLEAN:
out.print("boolean");
break;
case UNSIGNED_INT:
out.print("int");
break;
case UNSIGNED_SHORT:
out.print("short");
break;
}
String camelCaseName =
v.name.substring(0, 1).toUpperCase() + v.name.substring(1);
if (v.name.startsWith("is") || v.name.startsWith("get")) {
out.printf(" %s(", v.name);
} else {
out.printf(" get%s(", camelCaseName);
}
List<String> params = new ArrayList<>();
String offsetString;
int arrayIndices = v.arraySizes.size();
if (v.type == Type.CHAR) {
arrayIndices--;
}
if (arrayIndices > 0) {
List<String> index = new ArrayList<>();
int offset = 1;
for (int i = v.arraySizes.size() - 1; i >= 0; i--) {
int size = 1;
switch (v.type) {
case UNSIGNED_SHORT:
size *= 2;
break;
case UNSIGNED_INT:
case INT:
case ENUM:
size *= 4;
break;
case STRUCT:
size *= v.structRef.size;
break;
}
if (i < arrayIndices) {
params.add("int " + (char) ('i' + arrayIndices - 1 - i));
index.add(
size + " * " + offset + " * " + (char) ('i' + i));
}
offset *= v.arraySizes.get(i);
}
if (delta != null) {
offsetString = "myOffset + " + v.offset + " - " + delta + " + " + String.join(" + ", index);
} else {
offsetString = "myOffset + " + v.offset + " + " + String.join(" + ", index);
}
} else {
if (delta != null) {
offsetString = "myOffset + " + v.offset + " - " + delta;
} else {
offsetString = "myOffset + " + v.offset;
}
}
String paramString = String.join(", ", params);
out.printf("%s) {\n", paramString);
out.printf(" int offset = %s;\n", offsetString);
out.print(" return ");
switch (v.type) {
case UNSIGNED_INT:
case INT:
out.printf("%s.getInt(offset)", bufName);
break;
case CHAR:
out.printf("%s.getString(offset, %d)",
bufName, v.arraySizes.get(v.arraySizes.size() - 1));
break;
case ENUM:
out.printf("%s.idToEnum[%s.getInt(offset)]", v.enumName, bufName);
break;
case DOUBLE:
out.printf("%s.getDouble(offset)", bufName);
break;
case STRUCT:
out.printf("new %s(offset)", v.structRef.name);
break;
case BOOLEAN:
out.printf("%s.getByte(offset) != 0", bufName);
break;
case UNSIGNED_SHORT:
out.printf("%s.getShort(offset)", bufName);
break;
}
out.println(";");
out.println(" }");
if (v.type != Type.STRUCT && (v.type != Type.CHAR || !v.arraySizes.isEmpty())) {
if (isOverride) {
out.println(" @Override");
}
if (paramString.isEmpty()) {
out.printf(" void set%s(", camelCaseName);
} else {
out.printf(" void set%s(%s, ", camelCaseName, paramString);
}
switch (v.type) {
case INT:
case UNSIGNED_INT:
out.println("int value) {");
out.printf(" %s.putInt(%s, value);\n",
bufName, offsetString);
break;
case ENUM:
out.printf("%s value) {\n", v.enumName);
out.printf(" %s.putInt(%s, value.id);\n",
bufName, offsetString);
break;
case UNSIGNED_SHORT:
out.println("short value) {");
out.printf(" %s.putShort(%s, value);\n",
bufName, offsetString);
break;
case DOUBLE:
out.println("double value) {");
out.printf(" %s.putDouble(%s, value);\n",
bufName, offsetString);
break;
case BOOLEAN:
out.println("boolean value) {");
out.printf(
" %s.putByte(%s, (byte) (value ? 1 : 0));\n",
bufName, offsetString);
break;
case CHAR:
out.println("String value) {");
int maxLength = v.arraySizes.get(v.arraySizes.size() - 1);
out.printf(
" %s.putString(%s, %d, value);\n",
bufName, offsetString,
maxLength);
break;
}
out.println(" }");
}
}
enum Type {
STRUCT,
BOOLEAN,
INT,
CHAR,
UNSIGNED_SHORT,
UNSIGNED_INT,
DOUBLE,
ENUM
}
static class Variable {
private final String name;
private final Type type;
int offset;
private Struct structRef;
private String enumName;
private List<Integer> arraySizes = Collections.emptyList();
Variable(String name, Type type) {
this.name = name;
this.type = type;
}
}
static class Struct {
final String name;
int size;
List<Variable> variables = new ArrayList<>();
Struct(String name) {
this.name = name;
}
}
}