-
Notifications
You must be signed in to change notification settings - Fork 226
/
MCAWriter.java
221 lines (205 loc) · 8.8 KB
/
MCAWriter.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
package com.boydti.fawe.jnbt.anvil;
import com.boydti.fawe.object.collection.IterableThreadLocal;
import com.boydti.fawe.object.io.BufferedRandomAccessFile;
import com.boydti.fawe.util.MainUtil;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;
import java.util.zip.Deflater;
public abstract class MCAWriter {
private File folder;
private final int length;
private final int width;
private final int area;
private int OX, OZ;
public MCAWriter(int width, int length, File regionFolder) {
this.folder = regionFolder;
this.width = width;
this.length = length;
this.area = width * length;
}
public final File getFolder() {
return folder;
}
public void setFolder(File folder) {
this.folder = folder;
}
public final int getWidth() {
return width;
}
public final int getLength() {
return length;
}
/**
* Set the MCA file offset (each mca file is 512 blocks)
* - A negative value will shift the map negative
* - This only applies to generation, not block get/set
*
* @param mcaOX
* @param mcaOZ
*/
public void setMCAOffset(int mcaOX, int mcaOZ) {
OX = mcaOX << 9;
OZ = mcaOZ << 9;
}
public int getOffsetX() {
return OX;
}
public int getOffsetZ() {
return OZ;
}
public final int getArea() {
return area;
}
public abstract boolean shouldWrite(int chunkX, int chunkZ);
public abstract MCAChunk write(MCAChunk input, int startX, int endX, int startZ, int endZ);
public void generate() throws IOException {
if (!folder.exists()) {
folder.mkdirs();
}
final ForkJoinPool pool = new ForkJoinPool();
int bcx = 0;
int bcz = 0;
int tcx = (width - 1) >> 4;
int tcz = (length - 1) >> 4;
final ThreadLocal<MCAChunk> chunkStore = new ThreadLocal<MCAChunk>() {
@Override
protected MCAChunk initialValue() {
MCAChunk chunk = new MCAChunk(null, 0, 0);
chunk.biomes = new byte[256];
return chunk;
}
};
final ThreadLocal<byte[]> byteStore1 = new ThreadLocal<byte[]>() {
@Override
protected byte[] initialValue() {
return new byte[500000];
}
};
final ThreadLocal<byte[]> byteStore2 = new ThreadLocal<byte[]>() {
@Override
protected byte[] initialValue() {
return new byte[500000];
}
};
final ThreadLocal<Deflater> deflateStore = new ThreadLocal<Deflater>() {
@Override
protected Deflater initialValue() {
Deflater deflater = new Deflater(Deflater.BEST_SPEED, false);
return deflater;
}
};
byte[] fileBuf = new byte[1 << 16];
int mcaXMin = 0;
int mcaZMin = 0;
int mcaXMax = mcaXMin + ((width - 1) >> 9);
int mcaZMax = mcaZMin + ((length - 1) >> 9);
for (int mcaZ = mcaXMin; mcaZ <= mcaZMax; mcaZ++) {
for (int mcaX = mcaXMin; mcaX <= mcaXMax; mcaX++) {
final int fmcaX = mcaX;
final int fmcaZ = mcaZ;
File file = new File(folder, "r." + (mcaX + (getOffsetX() >> 9)) + "." + (mcaZ + (getOffsetZ() >> 9)) + ".mca");
if (!file.exists()) {
file.createNewFile();
}
final BufferedRandomAccessFile raf = new BufferedRandomAccessFile(file, "rw", fileBuf);
final byte[] header = new byte[4096];
final byte[][] compressed = new byte[1024][];
int bx = mcaX << 9;
int bz = mcaZ << 9;
int scx = bx >> 4;
int ecx = Math.min(scx + 31, tcx);
int scz = bz >> 4;
int ecz = Math.min(scz + 31, tcz);
for (int cz = scz; cz <= ecz; cz++) {
final int csz = cz << 4;
final int cez = Math.min(csz + 15, length - 1);
for (int cx = scx; cx <= ecx; cx++) {
final int csx = cx << 4;
final int cex = Math.min(csx + 15, width - 1);
final int fcx = cx;
final int fcz = cz;
if (shouldWrite(cx, cz)) {
pool.submit(new Runnable() {
@Override
public void run() {
try {
MCAChunk chunk = chunkStore.get();
chunk.setLoc(null, fcx, fcz);
chunk = write(chunk, csx, cex, csz, cez);
if (chunk != null) {
// Generation offset
chunk.setLoc(null, fcx + (getOffsetX() >> 4), fcz + (getOffsetZ() >> 4));
// Compress
byte[] bytes = chunk.toBytes(byteStore1.get());
byte[] compressedBytes = MainUtil.compress(bytes, byteStore2.get(), deflateStore.get());
int blocks = (compressed.length + 4095) >> 12;
compressed[((fcx & 31)) + ((fcz & 31) << 5)] = compressedBytes.clone();
}
} catch (Throwable e) {
e.printStackTrace();
}
}
});
}
}
}
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
pool.submit(new Runnable() {
@Override
public void run() {
try {
int totalLength = 8192;
for (int i = 0; i < compressed.length; i++) {
byte[] compressedBytes = compressed[i];
if (compressedBytes != null) {
int blocks = ((4095 + compressedBytes.length + 5) / 4096) * 4096;
totalLength += blocks;
}
}
raf.setLength(totalLength);
int offset = 8192;
for (int i = 0; i < compressed.length; i++) {
byte[] compressedBytes = compressed[i];
if (compressedBytes != null) {
// Set header
int index = i << 2;
int offsetMedium = offset >> 12;
int blocks = ((4095 + compressedBytes.length + 5) / 4096);
header[index] = (byte) (offsetMedium >> 16);
header[index + 1] = (byte) ((offsetMedium >> 8));
header[index + 2] = (byte) ((offsetMedium >> 0));
header[index + 3] = (byte) (blocks);
// Write bytes
int cx = (fmcaX << 5) + (i & 31);
int cz = (fmcaZ << 5) + (i >> 5);
raf.seek(offset);
raf.writeInt(compressedBytes.length + 1);
raf.write(2);
raf.write(compressedBytes);
offset += blocks * 4096;
}
}
raf.seek(0);
raf.write(header);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
raf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
pool.awaitQuiescence(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
pool.shutdown();
IterableThreadLocal.clean(byteStore1);
IterableThreadLocal.clean(byteStore2);
IterableThreadLocal.clean(deflateStore);
}
}