| 1 | package net.minecraft.world.chunk.storage;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import java.io.BufferedInputStream;
|
| 5 | import java.io.ByteArrayInputStream;
|
| 6 | import java.io.ByteArrayOutputStream;
|
| 7 | import java.io.DataInputStream;
|
| 8 | import java.io.DataOutputStream;
|
| 9 | import java.io.File;
|
| 10 | import java.io.IOException;
|
| 11 | import java.io.RandomAccessFile;
|
| 12 | import java.util.List;
|
| 13 | import java.util.zip.DeflaterOutputStream;
|
| 14 | import java.util.zip.GZIPInputStream;
|
| 15 | import java.util.zip.InflaterInputStream;
|
| 16 | import net.minecraft.server.MinecraftServer;
|
| 17 |
|
| 18 | public class RegionFile
|
| 19 | {
|
| 20 | private static final byte[] emptySector = new byte[4096];
|
| 21 | private final File fileName;
|
| 22 | private RandomAccessFile dataFile;
|
| 23 | private final int[] offsets = new int[1024];
|
| 24 | private final int[] chunkTimestamps = new int[1024];
|
| 25 | private List<Boolean> sectorFree;
|
| 26 |
|
| 27 | /** McRegion sizeDelta */
|
| 28 | private int sizeDelta;
|
| 29 | private long lastModified;
|
| 30 |
|
| 31 | public RegionFile(File fileNameIn)
|
| 32 | {
|
| 33 | this.fileName = fileNameIn;
|
| 34 | this.sizeDelta = 0;
|
| 35 |
|
| 36 | try
|
| 37 | {
|
| 38 | if (fileNameIn.exists())
|
| 39 | {
|
| 40 | this.lastModified = fileNameIn.lastModified();
|
| 41 | }
|
| 42 |
|
| 43 | this.dataFile = new RandomAccessFile(fileNameIn, "rw");
|
| 44 |
|
| 45 | if (this.dataFile.length() < 4096L)
|
| 46 | {
|
| 47 | for (int i = 0; i < 1024; ++i)
|
| 48 | {
|
| 49 | this.dataFile.writeInt(0);
|
| 50 | }
|
| 51 |
|
| 52 | for (int i1 = 0; i1 < 1024; ++i1)
|
| 53 | {
|
| 54 | this.dataFile.writeInt(0);
|
| 55 | }
|
| 56 |
|
| 57 | this.sizeDelta += 8192;
|
| 58 | }
|
| 59 |
|
| 60 | if ((this.dataFile.length() & 4095L) != 0L)
|
| 61 | {
|
| 62 | for (int j1 = 0; (long)j1 < (this.dataFile.length() & 4095L); ++j1)
|
| 63 | {
|
| 64 | this.dataFile.write(0);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | int k1 = (int)this.dataFile.length() / 4096;
|
| 69 | this.sectorFree = Lists.<Boolean>newArrayListWithCapacity(k1);
|
| 70 |
|
| 71 | for (int j = 0; j < k1; ++j)
|
| 72 | {
|
| 73 | this.sectorFree.add(Boolean.valueOf(true));
|
| 74 | }
|
| 75 |
|
| 76 | this.sectorFree.set(0, Boolean.valueOf(false));
|
| 77 | this.sectorFree.set(1, Boolean.valueOf(false));
|
| 78 | this.dataFile.seek(0L);
|
| 79 |
|
| 80 | for (int l1 = 0; l1 < 1024; ++l1)
|
| 81 | {
|
| 82 | int k = this.dataFile.readInt();
|
| 83 | this.offsets[l1] = k;
|
| 84 |
|
| 85 | if (k != 0 && (k >> 8) + (k & 255) <= this.sectorFree.size())
|
| 86 | {
|
| 87 | for (int l = 0; l < (k & 255); ++l)
|
| 88 | {
|
| 89 | this.sectorFree.set((k >> 8) + l, Boolean.valueOf(false));
|
| 90 | }
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | for (int i2 = 0; i2 < 1024; ++i2)
|
| 95 | {
|
| 96 | int j2 = this.dataFile.readInt();
|
| 97 | this.chunkTimestamps[i2] = j2;
|
| 98 | }
|
| 99 | }
|
| 100 | catch (IOException ioexception)
|
| 101 | {
|
| 102 | ioexception.printStackTrace();
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | /**
|
| 107 | * Returns an uncompressed chunk stream from the region file.
|
| 108 | */
|
| 109 | public synchronized DataInputStream getChunkDataInputStream(int x, int z)
|
| 110 | {
|
| 111 | if (this.outOfBounds(x, z))
|
| 112 | {
|
| 113 | return null;
|
| 114 | }
|
| 115 | else
|
| 116 | {
|
| 117 | try
|
| 118 | {
|
| 119 | int i = this.getOffset(x, z);
|
| 120 |
|
| 121 | if (i == 0)
|
| 122 | {
|
| 123 | return null;
|
| 124 | }
|
| 125 | else
|
| 126 | {
|
| 127 | int j = i >> 8;
|
| 128 | int k = i & 255;
|
| 129 |
|
| 130 | if (j + k > this.sectorFree.size())
|
| 131 | {
|
| 132 | return null;
|
| 133 | }
|
| 134 | else
|
| 135 | {
|
| 136 | this.dataFile.seek((long)(j * 4096));
|
| 137 | int l = this.dataFile.readInt();
|
| 138 |
|
| 139 | if (l > 4096 * k)
|
| 140 | {
|
| 141 | return null;
|
| 142 | }
|
| 143 | else if (l <= 0)
|
| 144 | {
|
| 145 | return null;
|
| 146 | }
|
| 147 | else
|
| 148 | {
|
| 149 | byte b0 = this.dataFile.readByte();
|
| 150 |
|
| 151 | if (b0 == 1)
|
| 152 | {
|
| 153 | byte[] abyte1 = new byte[l - 1];
|
| 154 | this.dataFile.read(abyte1);
|
| 155 | return new DataInputStream(new BufferedInputStream(new GZIPInputStream(new ByteArrayInputStream(abyte1))));
|
| 156 | }
|
| 157 | else if (b0 == 2)
|
| 158 | {
|
| 159 | byte[] abyte = new byte[l - 1];
|
| 160 | this.dataFile.read(abyte);
|
| 161 | return new DataInputStream(new BufferedInputStream(new InflaterInputStream(new ByteArrayInputStream(abyte))));
|
| 162 | }
|
| 163 | else
|
| 164 | {
|
| 165 | return null;
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 | catch (IOException var9)
|
| 172 | {
|
| 173 | return null;
|
| 174 | }
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | /**
|
| 179 | * Returns an output stream used to write chunk data. Data is on disk when the returned stream is closed.
|
| 180 | */
|
| 181 | public DataOutputStream getChunkDataOutputStream(int x, int z)
|
| 182 | {
|
| 183 | return this.outOfBounds(x, z) ? null : new DataOutputStream(new DeflaterOutputStream(new RegionFile.ChunkBuffer(x, z)));
|
| 184 | }
|
| 185 |
|
| 186 | /**
|
| 187 | * args: x, z, data, length - write chunk data at (x, z) to disk
|
| 188 | */
|
| 189 | protected synchronized void write(int x, int z, byte[] data, int length)
|
| 190 | {
|
| 191 | try
|
| 192 | {
|
| 193 | int i = this.getOffset(x, z);
|
| 194 | int j = i >> 8;
|
| 195 | int k = i & 255;
|
| 196 | int l = (length + 5) / 4096 + 1;
|
| 197 |
|
| 198 | if (l >= 256)
|
| 199 | {
|
| 200 | return;
|
| 201 | }
|
| 202 |
|
| 203 | if (j != 0 && k == l)
|
| 204 | {
|
| 205 | this.write(j, data, length);
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
| 209 | for (int i1 = 0; i1 < k; ++i1)
|
| 210 | {
|
| 211 | this.sectorFree.set(j + i1, Boolean.valueOf(true));
|
| 212 | }
|
| 213 |
|
| 214 | int l1 = this.sectorFree.indexOf(Boolean.valueOf(true));
|
| 215 | int j1 = 0;
|
| 216 |
|
| 217 | if (l1 != -1)
|
| 218 | {
|
| 219 | for (int k1 = l1; k1 < this.sectorFree.size(); ++k1)
|
| 220 | {
|
| 221 | if (j1 != 0)
|
| 222 | {
|
| 223 | if (((Boolean)this.sectorFree.get(k1)).booleanValue())
|
| 224 | {
|
| 225 | ++j1;
|
| 226 | }
|
| 227 | else
|
| 228 | {
|
| 229 | j1 = 0;
|
| 230 | }
|
| 231 | }
|
| 232 | else if (((Boolean)this.sectorFree.get(k1)).booleanValue())
|
| 233 | {
|
| 234 | l1 = k1;
|
| 235 | j1 = 1;
|
| 236 | }
|
| 237 |
|
| 238 | if (j1 >= l)
|
| 239 | {
|
| 240 | break;
|
| 241 | }
|
| 242 | }
|
| 243 | }
|
| 244 |
|
| 245 | if (j1 >= l)
|
| 246 | {
|
| 247 | j = l1;
|
| 248 | this.setOffset(x, z, l1 << 8 | l);
|
| 249 |
|
| 250 | for (int j2 = 0; j2 < l; ++j2)
|
| 251 | {
|
| 252 | this.sectorFree.set(j + j2, Boolean.valueOf(false));
|
| 253 | }
|
| 254 |
|
| 255 | this.write(j, data, length);
|
| 256 | }
|
| 257 | else
|
| 258 | {
|
| 259 | this.dataFile.seek(this.dataFile.length());
|
| 260 | j = this.sectorFree.size();
|
| 261 |
|
| 262 | for (int i2 = 0; i2 < l; ++i2)
|
| 263 | {
|
| 264 | this.dataFile.write(emptySector);
|
| 265 | this.sectorFree.add(Boolean.valueOf(false));
|
| 266 | }
|
| 267 |
|
| 268 | this.sizeDelta += 4096 * l;
|
| 269 | this.write(j, data, length);
|
| 270 | this.setOffset(x, z, j << 8 | l);
|
| 271 | }
|
| 272 | }
|
| 273 |
|
| 274 | this.setChunkTimestamp(x, z, (int)(MinecraftServer.getCurrentTimeMillis() / 1000L));
|
| 275 | }
|
| 276 | catch (IOException ioexception)
|
| 277 | {
|
| 278 | ioexception.printStackTrace();
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | /**
|
| 283 | * args: sectorNumber, data, length - write the chunk data to this RegionFile
|
| 284 | */
|
| 285 | private void write(int sectorNumber, byte[] data, int length) throws IOException
|
| 286 | {
|
| 287 | this.dataFile.seek((long)(sectorNumber * 4096));
|
| 288 | this.dataFile.writeInt(length + 1);
|
| 289 | this.dataFile.writeByte(2);
|
| 290 | this.dataFile.write(data, 0, length);
|
| 291 | }
|
| 292 |
|
| 293 | /**
|
| 294 | * args: x, z - check region bounds
|
| 295 | */
|
| 296 | private boolean outOfBounds(int x, int z)
|
| 297 | {
|
| 298 | return x < 0 || x >= 32 || z < 0 || z >= 32;
|
| 299 | }
|
| 300 |
|
| 301 | /**
|
| 302 | * args: x, z - get chunk's offset in region file
|
| 303 | */
|
| 304 | private int getOffset(int x, int z)
|
| 305 | {
|
| 306 | return this.offsets[x + z * 32];
|
| 307 | }
|
| 308 |
|
| 309 | /**
|
| 310 | * args: x, z, - true if chunk has been saved / converted
|
| 311 | */
|
| 312 | public boolean isChunkSaved(int x, int z)
|
| 313 | {
|
| 314 | return this.getOffset(x, z) != 0;
|
| 315 | }
|
| 316 |
|
| 317 | /**
|
| 318 | * args: x, z, offset - sets the chunk's offset in the region file
|
| 319 | */
|
| 320 | private void setOffset(int x, int z, int offset) throws IOException
|
| 321 | {
|
| 322 | this.offsets[x + z * 32] = offset;
|
| 323 | this.dataFile.seek((long)((x + z * 32) * 4));
|
| 324 | this.dataFile.writeInt(offset);
|
| 325 | }
|
| 326 |
|
| 327 | /**
|
| 328 | * args: x, z, timestamp - sets the chunk's write timestamp
|
| 329 | */
|
| 330 | private void setChunkTimestamp(int x, int z, int timestamp) throws IOException
|
| 331 | {
|
| 332 | this.chunkTimestamps[x + z * 32] = timestamp;
|
| 333 | this.dataFile.seek((long)(4096 + (x + z * 32) * 4));
|
| 334 | this.dataFile.writeInt(timestamp);
|
| 335 | }
|
| 336 |
|
| 337 | /**
|
| 338 | * close this RegionFile and prevent further writes
|
| 339 | */
|
| 340 | public void close() throws IOException
|
| 341 | {
|
| 342 | if (this.dataFile != null)
|
| 343 | {
|
| 344 | this.dataFile.close();
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | class ChunkBuffer extends ByteArrayOutputStream
|
| 349 | {
|
| 350 | private int chunkX;
|
| 351 | private int chunkZ;
|
| 352 |
|
| 353 | public ChunkBuffer(int x, int z)
|
| 354 | {
|
| 355 | super(8096);
|
| 356 | this.chunkX = x;
|
| 357 | this.chunkZ = z;
|
| 358 | }
|
| 359 |
|
| 360 | public void close() throws IOException
|
| 361 | {
|
| 362 | RegionFile.this.write(this.chunkX, this.chunkZ, this.buf, this.count);
|
| 363 | }
|
| 364 | }
|
| 365 | }
|
| 366 |
|