reworked saving

This commit is contained in:
Leijurv 2018-08-21 13:06:36 -07:00
parent 5bc8adc777
commit 7623b8b386
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A
3 changed files with 41 additions and 85 deletions

View File

@ -40,11 +40,6 @@ public final class CachedChunk implements IBlockTypeAccess {
*/ */
public static final int SIZE_IN_BYTES = SIZE / 8; 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 * The chunk x coordinate
*/ */

View File

@ -17,18 +17,15 @@
package baritone.bot.chunk; package baritone.bot.chunk;
import baritone.bot.utils.GZIPUtils;
import baritone.bot.utils.pathing.PathingBlockType; import baritone.bot.utils.pathing.PathingBlockType;
import java.io.FileInputStream; import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays;
import java.util.BitSet; import java.util.BitSet;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; import java.util.zip.GZIPOutputStream;
/** /**
@ -36,6 +33,13 @@ import java.util.zip.GZIPOutputStream;
* @since 8/3/2018 9:35 PM * @since 8/3/2018 9:35 PM
*/ */
public final class CachedRegion implements ICachedChunkAccess { 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. * 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); Path regionFile = getRegionFile(path, this.x, this.z);
if (!Files.exists(regionFile)) if (!Files.exists(regionFile))
Files.createFile(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 z = 0; z < 32; z++) {
for (int x = 0; x < 32; x++) { for (int x = 0; x < 32; x++) {
CachedChunk chunk = this.chunks[x][z]; CachedChunk chunk = this.chunks[x][z];
if (chunk == null) { if (chunk == null) {
out.write(CachedChunk.EMPTY_CHUNK); out.write(CHUNK_NOT_PRESENT);
} else { } else {
out.write(CHUNK_PRESENT);
byte[] chunkBytes = chunk.toByteArray(); byte[] chunkBytes = chunk.toByteArray();
out.write(chunkBytes); out.write(chunkBytes);
// Messy, but fills the empty 0s that should be trailing to fill up the space. // 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"); System.out.println("Loading region " + x + "," + z + " from disk");
byte[] decompressed; try (
try (FileInputStream in = new FileInputStream(regionFile.toFile())) { FileInputStream fileIn = new FileInputStream(regionFile.toFile());
decompressed = GZIPUtils.decompress(in); GZIPInputStream gzipIn = new GZIPInputStream(fileIn);
} DataInputStream in = new DataInputStream(gzipIn);
) {
if (decompressed == null) int magic = in.readInt();
return; 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++) { for (int z = 0; z < 32; z++) {
int index = (x + (z << 5)) * CachedChunk.SIZE_IN_BYTES; for (int x = 0; x < 32; x++) {
byte[] bytes = Arrays.copyOfRange(decompressed, index, index + CachedChunk.SIZE_IN_BYTES); int isChunkPresent = in.read();
if (isAllZeros(bytes)) { switch (isChunkPresent) {
this.chunks[x][z] = null; case CHUNK_PRESENT:
} else { byte[] bytes = new byte[CachedChunk.SIZE_IN_BYTES];
BitSet bits = BitSet.valueOf(bytes); in.readFully(bytes);
updateCachedChunk(x, z, bits); 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");
}
} }
} }
} }

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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();
}
}