diff --git a/src/main/java/baritone/bot/chunk/CachedChunk.java b/src/main/java/baritone/bot/chunk/CachedChunk.java index c85cc88c..8316345f 100644 --- a/src/main/java/baritone/bot/chunk/CachedChunk.java +++ b/src/main/java/baritone/bot/chunk/CachedChunk.java @@ -40,11 +40,6 @@ public final class CachedChunk implements IBlockTypeAccess { */ public static final int SIZE_IN_BYTES = SIZE / 8; - /** - * An array of just 0s with the length of {@link CachedChunk#SIZE_IN_BYTES} - */ - public static final byte[] EMPTY_CHUNK = new byte[SIZE_IN_BYTES]; - /** * The chunk x coordinate */ diff --git a/src/main/java/baritone/bot/chunk/CachedRegion.java b/src/main/java/baritone/bot/chunk/CachedRegion.java index 1853bf44..cbdcc9ed 100644 --- a/src/main/java/baritone/bot/chunk/CachedRegion.java +++ b/src/main/java/baritone/bot/chunk/CachedRegion.java @@ -17,18 +17,15 @@ package baritone.bot.chunk; -import baritone.bot.utils.GZIPUtils; import baritone.bot.utils.pathing.PathingBlockType; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Arrays; import java.util.BitSet; import java.util.function.Consumer; +import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** @@ -36,6 +33,13 @@ import java.util.zip.GZIPOutputStream; * @since 8/3/2018 9:35 PM */ public final class CachedRegion implements ICachedChunkAccess { + private static final byte CHUNK_NOT_PRESENT = 0; + private static final byte CHUNK_PRESENT = 1; + + /** + * Magic value to detect invalid cache files, or incompatible cache files saved in an old version of Baritone + */ + private static final int CACHED_REGION_MAGIC = 456022910; /** * All of the chunks in this region: A 32x32 array of them. @@ -111,13 +115,19 @@ public final class CachedRegion implements ICachedChunkAccess { Path regionFile = getRegionFile(path, this.x, this.z); if (!Files.exists(regionFile)) Files.createFile(regionFile); - try (FileOutputStream fileOut = new FileOutputStream(regionFile.toFile()); GZIPOutputStream out = new GZIPOutputStream(fileOut)) { + try ( + FileOutputStream fileOut = new FileOutputStream(regionFile.toFile()); + GZIPOutputStream gzipOut = new GZIPOutputStream(fileOut); + DataOutputStream out = new DataOutputStream(gzipOut); + ) { + out.writeInt(CACHED_REGION_MAGIC); for (int z = 0; z < 32; z++) { for (int x = 0; x < 32; x++) { CachedChunk chunk = this.chunks[x][z]; if (chunk == null) { - out.write(CachedChunk.EMPTY_CHUNK); + out.write(CHUNK_NOT_PRESENT); } else { + out.write(CHUNK_PRESENT); byte[] chunkBytes = chunk.toByteArray(); out.write(chunkBytes); // Messy, but fills the empty 0s that should be trailing to fill up the space. @@ -142,23 +152,30 @@ public final class CachedRegion implements ICachedChunkAccess { System.out.println("Loading region " + x + "," + z + " from disk"); - byte[] decompressed; - try (FileInputStream in = new FileInputStream(regionFile.toFile())) { - decompressed = GZIPUtils.decompress(in); - } - - if (decompressed == null) - return; - - for (int z = 0; z < 32; z++) { - for (int x = 0; x < 32; x++) { - int index = (x + (z << 5)) * CachedChunk.SIZE_IN_BYTES; - byte[] bytes = Arrays.copyOfRange(decompressed, index, index + CachedChunk.SIZE_IN_BYTES); - if (isAllZeros(bytes)) { - this.chunks[x][z] = null; - } else { - BitSet bits = BitSet.valueOf(bytes); - updateCachedChunk(x, z, bits); + try ( + FileInputStream fileIn = new FileInputStream(regionFile.toFile()); + GZIPInputStream gzipIn = new GZIPInputStream(fileIn); + DataInputStream in = new DataInputStream(gzipIn); + ) { + int magic = in.readInt(); + if (magic != CACHED_REGION_MAGIC) { + throw new IOException("Bad magic value " + magic); + } + for (int z = 0; z < 32; z++) { + for (int x = 0; x < 32; x++) { + int isChunkPresent = in.read(); + switch (isChunkPresent) { + case CHUNK_PRESENT: + byte[] bytes = new byte[CachedChunk.SIZE_IN_BYTES]; + in.readFully(bytes); + BitSet bits = BitSet.valueOf(bytes); + updateCachedChunk(x, z, bits); + case CHUNK_NOT_PRESENT: + this.chunks[x][z] = null; + break; + default: + throw new IOException("Malformed stream"); + } } } } diff --git a/src/main/java/baritone/bot/utils/GZIPUtils.java b/src/main/java/baritone/bot/utils/GZIPUtils.java deleted file mode 100644 index 7b15e580..00000000 --- a/src/main/java/baritone/bot/utils/GZIPUtils.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * This file is part of Baritone. - * - * Baritone is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Baritone is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Baritone. If not, see . - */ - -package baritone.bot.utils; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.GZIPInputStream; -import java.util.zip.GZIPOutputStream; - -/** - * @author Brady - * @since 8/3/2018 10:18 PM - */ -public final class GZIPUtils { - - private GZIPUtils() { - } - - public static byte[] compress(byte[] in) throws IOException { - ByteArrayOutputStream byteStream = new ByteArrayOutputStream(in.length); - - try (GZIPOutputStream gzipStream = new GZIPOutputStream(byteStream)) { - gzipStream.write(in); - } - return byteStream.toByteArray(); - } - - public static byte[] decompress(InputStream in) throws IOException { - ByteArrayOutputStream outStream = new ByteArrayOutputStream(); - - try (GZIPInputStream gzipStream = new GZIPInputStream(in)) { - byte[] buffer = new byte[1024]; - int len; - while ((len = gzipStream.read(buffer)) > 0) { - outStream.write(buffer, 0, len); - } - } - return outStream.toByteArray(); - } -}