diff --git a/core/src/main/java/com/boydti/fawe/config/Settings.java b/core/src/main/java/com/boydti/fawe/config/Settings.java index 71485a8b..f35b0673 100644 --- a/core/src/main/java/com/boydti/fawe/config/Settings.java +++ b/core/src/main/java/com/boydti/fawe/config/Settings.java @@ -167,7 +167,8 @@ public class Settings extends Config { public int COMPRESSION_LEVEL = 3; @Comment({ "The buffer size for compression:", - " - Larger = better ratio but uses more upfront memory" + " - Larger = better ratio but uses more upfront memory", + " - Must be in the range [64, 33554432]", }) public int BUFFER_SIZE = 531441; diff --git a/core/src/main/java/com/boydti/fawe/object/FaweInputStream.java b/core/src/main/java/com/boydti/fawe/object/FaweInputStream.java index b16dc4f6..788e7d71 100644 --- a/core/src/main/java/com/boydti/fawe/object/FaweInputStream.java +++ b/core/src/main/java/com/boydti/fawe/object/FaweInputStream.java @@ -28,6 +28,13 @@ public class FaweInputStream extends DataInputStream { private NBTInputStream nbtIn; + public void skipFully(int num) throws IOException { + long skipped = skip(num); + while (skipped != num) { + skipped += skip(num - skipped); + } + } + public NamedTag readNBT() throws IOException { if (nbtIn == null) { nbtIn = new NBTInputStream(parent); diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/AbstractDelegateChangeSet.java b/core/src/main/java/com/boydti/fawe/object/changeset/AbstractDelegateChangeSet.java index e600ece5..8bcb393e 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/AbstractDelegateChangeSet.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/AbstractDelegateChangeSet.java @@ -21,6 +21,8 @@ public class AbstractDelegateChangeSet extends FaweChangeSet { public AbstractDelegateChangeSet(FaweChangeSet parent) { super(parent.getWorld()); this.parent = parent; + this.waitingCombined = parent.waitingCombined; + this.waitingAsync = parent.waitingAsync; } @Override @@ -33,6 +35,11 @@ public class AbstractDelegateChangeSet extends FaweChangeSet { return super.closeAsync(); } + @Override + public boolean flush() { + return parent.flush(); + } + @Override public boolean close() { return super.close() && parent.close(); @@ -52,6 +59,12 @@ public class AbstractDelegateChangeSet extends FaweChangeSet { return parent.getWorld(); } + @Override + @Deprecated + public boolean flushAsync() { + return parent.flushAsync(); + } + @Override public void add(int x, int y, int z, int combinedFrom, int combinedTo) { parent.add(x, y, z, combinedFrom, combinedTo); diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java b/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java index cbf860fb..1b11f3fc 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/DiskStorageHistory.java @@ -18,7 +18,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.util.Map; import java.util.UUID; @@ -316,21 +315,21 @@ public class DiskStorageHistory extends FaweStreamChangeSet { } @Override - public InputStream getBlockIS() throws IOException { + public FaweInputStream getBlockIS() throws IOException { if (!bdFile.exists()) { return null; } - InputStream is = MainUtil.getCompressedIS(new FileInputStream(bdFile)); + FaweInputStream is = MainUtil.getCompressedIS(new FileInputStream(bdFile)); readHeader(is); return is; } @Override - public InputStream getBiomeIS() throws IOException { + public FaweInputStream getBiomeIS() throws IOException { if (!bioFile.exists()) { return null; } - InputStream is = MainUtil.getCompressedIS(new FileInputStream(bioFile)); + FaweInputStream is = MainUtil.getCompressedIS(new FileInputStream(bioFile)); return is; } @@ -376,7 +375,7 @@ public class DiskStorageHistory extends FaweStreamChangeSet { try (FileInputStream fis = new FileInputStream(bdFile)) { FaweInputStream gis = MainUtil.getCompressedIS(fis); // skip mode - gis.skip(1); + gis.skipFully(1); // origin ox = ((gis.read() << 24) + (gis.read() << 16) + (gis.read() << 8) + (gis.read() << 0)); oz = ((gis.read() << 24) + (gis.read() << 16) + (gis.read() << 8) + (gis.read() << 0)); @@ -415,9 +414,9 @@ public class DiskStorageHistory extends FaweStreamChangeSet { int oz = getOriginZ(); if (ox == 0 && oz == 0 && bdFile.exists()) { try (FileInputStream fis = new FileInputStream(bdFile)) { - final InputStream gis = MainUtil.getCompressedIS(fis); + final FaweInputStream gis = MainUtil.getCompressedIS(fis); // skip mode - gis.skip(1); + gis.skipFully(1); // origin ox = ((gis.read() << 24) + (gis.read() << 16) + (gis.read() << 8) + (gis.read() << 0)); oz = ((gis.read() << 24) + (gis.read() << 16) + (gis.read() << 8) + (gis.read() << 0)); diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java b/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java index 9d84c618..f446627a 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/FaweChangeSet.java @@ -207,7 +207,7 @@ public abstract class FaweChangeSet implements ChangeSet { } public boolean isEmpty() { - return size() == 0; + return waitingCombined.get() == 0 && waitingAsync.get() == 0 && size() == 0; } public void add(int x, int y, int z, int combinedFrom, BaseBlock to) { diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java b/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java index b68ebc38..cfe9af8c 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/FaweStreamChangeSet.java @@ -70,15 +70,15 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { public interface FaweStreamPositionDelegate { void write(OutputStream out, int x, int y, int z) throws IOException; - int readX(InputStream in) throws IOException; - int readY(InputStream in) throws IOException; - int readZ(InputStream in) throws IOException; + int readX(FaweInputStream in) throws IOException; + int readY(FaweInputStream in) throws IOException; + int readZ(FaweInputStream in) throws IOException; } public interface FaweStreamIdDelegate { void writeChange(OutputStream out, int from, int to) throws IOException; - void readCombined(InputStream in, MutableBlockChange change, boolean dir) throws IOException; - void readCombined(InputStream in, MutableFullBlockChange change, boolean dir) throws IOException; + void readCombined(FaweInputStream in, MutableBlockChange change, boolean dir) throws IOException; + void readCombined(FaweInputStream in, MutableFullBlockChange change, boolean dir) throws IOException; } private void setupStreamDelegates(int mode) { @@ -94,9 +94,9 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { } @Override - public void readCombined(InputStream is, MutableBlockChange change, boolean dir) throws IOException { + public void readCombined(FaweInputStream is, MutableBlockChange change, boolean dir) throws IOException { if (dir) { - is.skip(2); + is.skipFully(2); int to1 = is.read(); int to2 = is.read(); change.id = (short) ((to2 << 4) + (to1 >> 4)); @@ -104,14 +104,14 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { } else { int from1 = is.read(); int from2 = is.read(); - is.skip(2); + is.skipFully(2); change.id = (short) ((from2 << 4) + (from1 >> 4)); change.data = (byte) (from1 & 0xf); } } @Override - public void readCombined(InputStream is, MutableFullBlockChange change, boolean dir) throws IOException { + public void readCombined(FaweInputStream is, MutableFullBlockChange change, boolean dir) throws IOException { change.from = ((byte) is.read() & 0xFF) + ((byte) is.read() << 8); change.to = ((byte) is.read() & 0xFF) + ((byte) is.read() << 8); } @@ -125,7 +125,7 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { } @Override - public void readCombined(InputStream in, MutableBlockChange change, boolean dir) throws IOException { + public void readCombined(FaweInputStream in, MutableBlockChange change, boolean dir) throws IOException { int from1 = in.read(); int from2 = in.read(); change.id = (short) ((from2 << 4) + (from1 >> 4)); @@ -133,7 +133,7 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { } @Override - public void readCombined(InputStream is, MutableFullBlockChange change, boolean dir) throws IOException { + public void readCombined(FaweInputStream is, MutableFullBlockChange change, boolean dir) throws IOException { change.from = ((byte) is.read() & 0xFF) + ((byte) is.read() << 8); change.to = 0; } @@ -162,20 +162,18 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { byte[] buffer = new byte[4]; @Override - public int readX(InputStream in) throws IOException { - if (in.read(buffer) == -1) { - throw new EOFException(); - } + public int readX(FaweInputStream in) throws IOException { + in.readFully(buffer); return lx = lx + ((((buffer[1] & 0xFF) + ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20); } @Override - public int readY(InputStream in) { + public int readY(FaweInputStream in) { return (ly = ly + buffer[0]) & 0xFF; } @Override - public int readZ(InputStream in) throws IOException { + public int readZ(FaweInputStream in) throws IOException { return lz = lz + ((((buffer[2] & 0xFF) + ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20); } }; @@ -196,20 +194,18 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { } @Override - public int readX(InputStream is) throws IOException { - if (is.read(buffer) == -1) { - throw new EOFException(); - } + public int readX(FaweInputStream is) throws IOException { + is.readFully(buffer); return lx = (lx + (buffer[0] & 0xFF) + (buffer[1] << 8)); } @Override - public int readY(InputStream is) throws IOException { + public int readY(FaweInputStream is) throws IOException { return (ly = (ly + (buffer[4]))) & 0xFF; } @Override - public int readZ(InputStream is) throws IOException { + public int readZ(FaweInputStream is) throws IOException { return lz = (lz + (buffer[2] & 0xFF) + (buffer[3] << 8)); } }; @@ -249,6 +245,9 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { if (blockSize > 0) { return false; } + if (waitingCombined.get() != 0 || waitingAsync.get() != 0) { + return false; + } flush(); return blockSize == 0; } @@ -275,8 +274,8 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { public abstract NBTOutputStream getTileCreateOS() throws IOException; public abstract NBTOutputStream getTileRemoveOS() throws IOException; - public abstract InputStream getBlockIS() throws IOException; - public abstract InputStream getBiomeIS() throws IOException; + public abstract FaweInputStream getBlockIS() throws IOException; + public abstract FaweInputStream getBiomeIS() throws IOException; public abstract NBTInputStream getEntityCreateIS() throws IOException; public abstract NBTInputStream getEntityRemoveIS() throws IOException; public abstract NBTInputStream getTileCreateIS() throws IOException; @@ -391,7 +390,7 @@ public abstract class FaweStreamChangeSet extends FaweChangeSet { } public Iterator getBlockIterator(final boolean dir) throws IOException { - final InputStream is = getBlockIS(); + final FaweInputStream is = getBlockIS(); if (is == null) { return new ArrayList().iterator(); } diff --git a/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java b/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java index 0691ea6f..b5dda36e 100644 --- a/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java +++ b/core/src/main/java/com/boydti/fawe/object/changeset/MemoryOptimizedHistory.java @@ -10,7 +10,6 @@ import com.sk89q.jnbt.NBTInputStream; import com.sk89q.jnbt.NBTOutputStream; import com.sk89q.worldedit.world.World; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; /** @@ -153,7 +152,7 @@ public class MemoryOptimizedHistory extends FaweStreamChangeSet { } @Override - public InputStream getBiomeIS() throws IOException { + public FaweInputStream getBiomeIS() throws IOException { if (biomes == null) { return null; } @@ -174,7 +173,7 @@ public class MemoryOptimizedHistory extends FaweStreamChangeSet { } @Override - public InputStream getBlockIS() throws IOException { + public FaweInputStream getBlockIS() throws IOException { if (ids == null) { return null; } diff --git a/core/src/main/java/com/boydti/fawe/util/MainUtil.java b/core/src/main/java/com/boydti/fawe/util/MainUtil.java index 6a3d5751..22c47c45 100644 --- a/core/src/main/java/com/boydti/fawe/util/MainUtil.java +++ b/core/src/main/java/com/boydti/fawe/util/MainUtil.java @@ -66,11 +66,11 @@ import java.util.zip.Inflater; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import net.jpountz.lz4.LZ4BlockInputStream; +import net.jpountz.lz4.LZ4BlockOutputStream; import net.jpountz.lz4.LZ4Compressor; import net.jpountz.lz4.LZ4Factory; import net.jpountz.lz4.LZ4FastDecompressor; import net.jpountz.lz4.LZ4InputStream; -import net.jpountz.lz4.LZ4OutputStream; import net.jpountz.lz4.LZ4Utils; public class MainUtil { @@ -312,8 +312,7 @@ public class MainUtil { } public static FaweOutputStream getCompressedOS(OutputStream os, int amount, int buffer) throws IOException { -// os.write((byte) 10 + amount); - os.write((byte) -amount); + os.write((byte) 10 + amount); os = new BufferedOutputStream(os, buffer); if (amount == 0) { return new FaweOutputStream(os); @@ -326,14 +325,14 @@ public class MainUtil { LZ4Factory factory = LZ4Factory.fastestInstance(); int fastAmount = 1 + ((amount - 1) % 3); for (int i = 0; i < fastAmount; i++) { - os = new LZ4OutputStream(os, buffer, factory.fastCompressor()); + os = new LZ4BlockOutputStream(os, buffer, factory.fastCompressor()); } int highAmount = amount > 3 ? 1 : 0; for (int i = 0; i < highAmount; i++) { if (amount == 9) { - os = new LZ4OutputStream(os, buffer, factory.highCompressor(17)); + os = new LZ4BlockOutputStream(os, buffer, factory.highCompressor(17)); } else { - os = new LZ4OutputStream(os, buffer, factory.highCompressor()); + os = new LZ4BlockOutputStream(os, buffer, factory.highCompressor()); } } return new FaweOutputStream(os); @@ -352,7 +351,7 @@ public class MainUtil { boolean legacy; if (mode > 10) { legacy = false; - mode = -mode + 9; + mode = -mode + 10; } else { legacy = true; } diff --git a/core/src/main/java/com/sk89q/worldedit/LocalSession.java b/core/src/main/java/com/sk89q/worldedit/LocalSession.java index 5e4a1e23..e3edc8de 100644 --- a/core/src/main/java/com/sk89q/worldedit/LocalSession.java +++ b/core/src/main/java/com/sk89q/worldedit/LocalSession.java @@ -31,6 +31,7 @@ import com.boydti.fawe.object.changeset.FaweChangeSet; import com.boydti.fawe.object.extent.ResettableExtent; import com.boydti.fawe.util.EditSessionBuilder; import com.boydti.fawe.util.MainUtil; +import com.boydti.fawe.util.TaskManager; import com.boydti.fawe.wrappers.WorldWrapper; import com.sk89q.jchronic.Chronic; import com.sk89q.jchronic.Options; @@ -101,14 +102,6 @@ public class LocalSession { private transient RegionSelector selector = new CuboidRegionSelector(); private transient boolean placeAtPos1 = false; private transient List history = Collections.synchronizedList(new LinkedList() { - @Override - public void add(int index, Object element) { // Integer = Lazy evaluated FaweChangeSet - if (element instanceof Integer || element instanceof FaweChangeSet) { - super.add(index, element); - } else { - throw new ClassCastException("Must add either Integer (index) or FaweChangeSet"); - } - } @Override public Object get(int index) { Object value = super.get(index); @@ -478,6 +471,7 @@ public class LocalSession { } } } + historySize += MainUtil.getSize(changeSet); if (append) { history.add(changeSet); diff --git a/core/src/main/java/net/jpountz/lz4/LZ4BlockOutputStream.java b/core/src/main/java/net/jpountz/lz4/LZ4BlockOutputStream.java index ee36034b..626ae35d 100644 --- a/core/src/main/java/net/jpountz/lz4/LZ4BlockOutputStream.java +++ b/core/src/main/java/net/jpountz/lz4/LZ4BlockOutputStream.java @@ -182,7 +182,7 @@ public final class LZ4BlockOutputStream extends FilterOutputStream { checksum.update(buffer, 0, o); check = (int) checksum.getValue(); } else { - check = 0; + check = 1; } int compressedLength = compressor.compress(buffer, 0, o, compressedBuffer, HEADER_LENGTH); final int compressMethod;