-
Notifications
You must be signed in to change notification settings - Fork 6
/
XaeroRegionMerger.java
418 lines (385 loc) · 16.3 KB
/
XaeroRegionMerger.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
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
package com.github.entropy5;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
public class XaeroRegionMerger {
public static final HashSet<Integer> GREENS = new HashSet<>(Arrays.asList(2, 161, 49170, 24594, 32929, 8210, 18, 57362, 53409, 4257, 16402, 20641, 49313, 32786, 37025, 16545, 40978));
// static boolean dark = false;
public static void main(String[] args) {
if (args.length != 5) {
System.out.println("Usage: java -jar XaeroRegionMerger.jar <top folder> <bottom folder> <out folder> <cores (int)> <darken (0/1)>");
System.out.println("- This command will merge the zip files from two xaero folders and save them in a third folder");
System.out.println("- The top folder input gets priority over the bottom folder");
System.out.println("- Darkening makes the bottom folder less bright, so you can mark background map data as *undiscovered*");
throw new RuntimeException("Incorrect number of arguments");
}
final Instant before = Instant.now();
// First folder gets pixel priority, put the important stuff here
Path firstFolderIn = new File(args[0]).toPath();
Path secondFolderIn = new File(args[1]).toPath();
Path folderOut = new File(args[2]).toPath();
int parallelism = Integer.parseInt(args[3]);
boolean dark = (Integer.parseInt(args[4]) == 1); // stain the background darker
System.out.println("First folder: " + firstFolderIn);
System.out.println("Second folder: " + secondFolderIn);
System.out.println("Folder out: " + folderOut);
System.out.println("Threads: " + parallelism);
System.out.println("Darkening: " + dark);
HashSet<String> firstSet = getFileNames(firstFolderIn);
HashSet<String> secondSet = getFileNames(secondFolderIn);
HashSet<String> onlyFirst = new HashSet<>(firstSet);
onlyFirst.removeAll(secondSet); // Difference
System.out.println("Only present in first: " + onlyFirst);
copyFull(firstFolderIn, folderOut, onlyFirst);
HashSet<String> onlySecond = new HashSet<>(secondSet);
onlySecond.removeAll(firstSet); // Difference
System.out.println("Only present in second: " + onlySecond);
if (dark) {
deepMerge(secondFolderIn, secondFolderIn, folderOut, onlySecond, false, parallelism, true);
} else {
copyFull(secondFolderIn, folderOut, onlySecond);
}
Instant afterDarken = Instant.now();
long secondsToDarken = afterDarken.getEpochSecond() - before.getEpochSecond();
System.out.println("Completed darkening in: " + (secondsToDarken / 60) + " minutes");
HashSet<String> inter = new HashSet<>(firstSet);
inter.retainAll(secondSet); // Intersection
System.out.println("Need to deep merge: " + inter);
deepMerge(firstFolderIn, secondFolderIn, folderOut, inter, true, parallelism, dark);
Instant afterChunkMerge = Instant.now();
long secondsToDeepMerge = afterChunkMerge.getEpochSecond() - before.getEpochSecond();
System.out.println("Completed deep merge in: " + (secondsToDeepMerge / 60) + " minutes");
}
private static void deepMerge(Path inp1, Path inp2, Path outp, HashSet<String> rNames, boolean first, int parallelism, boolean dark) {
final ExecutorService executorService = Executors.newFixedThreadPool(parallelism);
List<Callable<Object>> tasks = rNames.stream()
.map(rName -> Executors.callable(() -> mergeRegion(inp1, inp2, outp, rName, first, dark)))
.collect(Collectors.toList());
try {
executorService.invokeAll(tasks);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally {
executorService.shutdown();
}
}
private static void mergeRegion(Path inp1, Path inp2, Path outp, String rName, boolean first, boolean dark) {
if (first) { // todo: refactor this scuffed var
System.out.println("Merging " + rName);
} else {
System.out.println("Darkening " + rName);
}
File fileIn1 = inp1.resolve(rName).toFile();
File fileIn2 = inp2.resolve(rName).toFile();
File fileOut = outp.resolve(rName).toFile();
DataInputStream in1 = null;
DataInputStream in2 = null;
DataOutputStream out = null;
int saveVersionA;
int saveVersionB;
// todo: migrate to try with resources sanely
try {
try {
if (first) {
in1 = new DataInputStream(new ByteArrayInputStream(decompressZipToBytes(fileIn1.toPath())));
}
in2 = new DataInputStream(new ByteArrayInputStream(decompressZipToBytes(fileIn2.toPath())));
final ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(fileOut.toPath())));
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
out = new DataOutputStream(byteOut);
final ZipEntry e = new ZipEntry("region.xaero");
zipOut.putNextEntry(e);
int firstByteA = first ? in1.read() : 255;
int firstByteB = in2.read();
if (firstByteA == 255) {
out.write(255);
saveVersionA = first ? in1.readInt() : 4;
saveVersionB = in2.readInt();
if (saveVersionA == 4 && saveVersionB == 4) {
out.writeInt(4);
} else {
throw new RuntimeException(rName + " version problem");
}
firstByteA = -1;
firstByteB = -1;
}
Process tileProc = Process.BOTH;
int tileChunkCoordsA = 0;
int tileChunkCoordsB = 0;
while (true) { // Keeps reading TileChunks (max 8x8) until done
if (tileProc == Process.A || tileProc == Process.BOTH) {
if (firstByteA == -1) {
tileChunkCoordsA = first ? in1.read() : -1; // if first is false then this will make it skip
} else {
tileChunkCoordsA = firstByteA;
}
}
if (tileProc == Process.B || tileProc == Process.BOTH) {
if (firstByteB == -1) {
tileChunkCoordsB = in2.read();
} else {
tileChunkCoordsB = firstByteB;
}
}
if (tileChunkCoordsA == -1 && tileChunkCoordsB == -1) {
tileProc = Process.NONE;
} else if (tileChunkCoordsA == tileChunkCoordsB) {
tileProc = Process.BOTH;
out.write(tileChunkCoordsA);
} else if (tileChunkCoordsB == -1) {
tileProc = Process.A;
out.write(tileChunkCoordsA);
} else if (tileChunkCoordsA == -1) {
tileProc = Process.B;
out.write(tileChunkCoordsB);
} else if (tileChunkCoordsA < tileChunkCoordsB) {
tileProc = Process.A;
out.write(tileChunkCoordsA);
} else {
tileProc = Process.B;
out.write(tileChunkCoordsB);
}
if (tileProc == Process.NONE) { // -1 as chunk coord means its over
zipOut.write(byteOut.toByteArray());
zipOut.closeEntry();
zipOut.close();
break;
}
if (tileProc == Process.A || tileProc == Process.BOTH) {
firstByteA = -1;
}
if (tileProc == Process.B || tileProc == Process.BOTH) {
firstByteB = -1;
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (tileProc == Process.A) {
Integer nextTile = in1.readInt();
passChunk(nextTile, in1, out, true, false, dark); // passChunk handles writing the int
} else if (tileProc == Process.B) {
Integer nextTile = in2.readInt();
passChunk(nextTile, in2, out, true, true, dark);
} else {
Integer nextTileA = in1.readInt();
Integer nextTileB = in2.readInt();
passChunkComplex(nextTileA, nextTileB, in1, in2, out, dark); // Deep merge
}
}
}
}
} finally {
if (in1 != null) {
in1.close();
}
if (in2 != null) {
in2.close();
}
if (out != null) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static void passChunk(Integer nextTile, DataInputStream in, DataOutputStream out, boolean write, boolean darken, boolean dark) throws IOException {
darken = dark && darken;
if (write) {
if (darken) {
out.writeInt(nextTile | 3 << 2); // darken needs color type 3
} else {
out.writeInt(nextTile);
}
}
if (nextTile != -1) { // Skip empty chunk
for (int x = 0; x < 16; ++x) {
for (int z = 0; z < 16; ++z) {
passPixel(nextTile, in, out, write, darken);
nextTile = null;
}
}
int worldInterVersion = in.read();
if (write) {
out.write(worldInterVersion);
}
}
}
private static byte[] decompressZipToBytes(final Path input) {
try {
return toUnzippedByteArray(Files.readAllBytes(input));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static byte[] toUnzippedByteArray(byte[] zippedBytes) throws IOException {
final ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zippedBytes));
final byte[] buff = new byte[1024];
if (zipInputStream.getNextEntry() != null) {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
int l;
while ((l = zipInputStream.read(buff)) > 0) {
outputStream.write(buff, 0, l);
}
return outputStream.toByteArray();
}
return new byte[0];
}
private static void passChunkComplex(Integer nextTileA, Integer nextTileB, DataInputStream in1, DataInputStream in2, DataOutputStream out, boolean dark) throws IOException {
if (nextTileA != -1) { // A gets priority
passChunk(nextTileA, in1, out, true, false, dark);
if (nextTileB != -1) {
passChunk(nextTileB, in2, out, false, false, dark);
}
} else if (nextTileB != -1) {
passChunk(nextTileB, in2, out, true, true, dark);
} else {
out.writeInt(-1); // if both are empty, write -1
}
}
private static void passPixel(Integer next, DataInputStream in, DataOutputStream out, boolean write, boolean darken) throws IOException {
boolean green = false;
int parametres;
if (next != null) {
parametres = next;
} else {
parametres = in.readInt();
if (write) {
if (darken) {
out.writeInt(parametres | 3 << 2); // color type 3
} else {
out.writeInt(parametres);
}
}
}
if ((parametres & 1) != 0) { // likely a grass check
int state = in.readInt();
if (write) {
out.writeInt(state);
if (darken & GREENS.contains(state)) {
green = true;
}
}
} else if (darken) {
green = true;
}
if ((parametres & 64) != 0) {
int height = in.read();
if (write) {
out.write(height);
}
}
if ((parametres & 16777216) != 0) {
int topHeight = in.read();
if (write) {
out.write(topHeight);
}
}
int savedColourType;
int biomeKey;
if ((parametres & 2) != 0) {
savedColourType = in.read();
if (write) {
out.write(savedColourType);
}
for (biomeKey = 0; biomeKey < savedColourType; ++biomeKey) {
passOverlay(in, out, write);
}
}
savedColourType = parametres >> 2 & 3;
if (savedColourType == 3) {
int customColour = in.readInt();
if (write) {
if (darken) {
writeColor(out, green);
} else {
out.writeInt(customColour);
}
}
}
if (darken && write) {
writeColor(out, green);
}
if (savedColourType != 0 && savedColourType != 3 || (parametres & 1048576) != 0) {
int biomeByte = in.read();
if (write) {
out.write(biomeByte);
}
if (biomeByte >= 255) {
biomeKey = in.readInt();
if (write) {
out.writeInt(biomeKey);
}
}
}
}
private static void writeColor(DataOutputStream out, boolean green) throws IOException {
if (green) {
out.writeInt(5732666); // green
} else {
out.writeInt(9868950); // gray
}
}
private static void passOverlay(DataInputStream in, DataOutputStream out, boolean write) throws IOException {
int parametres = in.readInt();
if (write) {
out.writeInt(parametres);
}
int state;
if ((parametres & 1) != 0) {
state = in.readInt();
if (write) {
out.writeInt(state);
}
}
int opacity;
byte savedColourType = (byte) (parametres >> 8 & 3);
if (savedColourType == 2 || (parametres & 4) != 0) {
int biomeBuffer = in.readInt();
if (write) {
out.writeInt(biomeBuffer);
}
}
if ((parametres & 8) != 0) {
opacity = in.readInt();
if (write) {
out.writeInt(opacity);
}
}
}
private static void copyFull(Path folderIn, Path folderOut, HashSet<String> regionSet) {
System.out.println("Copying " + regionSet.size() + " regions");
for (String s : regionSet) {
try {
Files.copy(folderIn.resolve(s), folderOut.resolve(s),
java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static HashSet<String> getFileNames(Path folder) {
HashSet<String> nameSet = new HashSet<>();
Arrays.stream(Objects.requireNonNull(folder.toFile().listFiles()))
.forEach(fileIn -> nameSet.add(fileIn.getName()));
return nameSet;
}
enum Process {
A,
B,
BOTH,
NONE
}
}