diff --git a/NBT.iml b/NBT.iml new file mode 100644 index 00000000..065a1192 --- /dev/null +++ b/NBT.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index ae6575fa..cc4fdc29 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 77a041f6..622ab64a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,5 @@ -#Wed Mar 29 16:28:10 CEST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/gradlew b/gradlew index 4453ccea..2fe81a7d 100755 --- a/gradlew +++ b/gradlew @@ -1,5 +1,21 @@ #!/usr/bin/env sh +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + ############################################################################## ## ## Gradle start up script for UN*X @@ -28,16 +44,16 @@ APP_NAME="Gradle" APP_BASE_NAME=`basename "$0"` # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS="" +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -109,8 +125,8 @@ if $darwin; then GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" fi -# For Cygwin, switch paths to Windows format before running java -if $cygwin ; then +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then APP_HOME=`cygpath --path --mixed "$APP_HOME"` CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` JAVACMD=`cygpath --unix "$JAVACMD"` @@ -138,35 +154,30 @@ if $cygwin ; then else eval `echo args$i`="\"$arg\"" fi - i=$((i+1)) + i=`expr $i + 1` done case $i in - (0) set -- ;; - (1) set -- "$args0" ;; - (2) set -- "$args0" "$args1" ;; - (3) set -- "$args0" "$args1" "$args2" ;; - (4) set -- "$args0" "$args1" "$args2" "$args3" ;; - (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; - (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; - (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; - (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; - (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; esac fi # Escape application args -save ( ) { +save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } -APP_ARGS=$(save "$@") +APP_ARGS=`save "$@"` # Collect all arguments for the java command, following the shell quoting and substitution rules eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" -# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong -if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then - cd "$(dirname "$0")" -fi - exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat index e95643d6..24467a14 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,3 +1,19 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + @if "%DEBUG%" == "" @echo off @rem ########################################################################## @rem @@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0 set APP_HOME=%DIRNAME% @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" @rem Find java.exe if defined JAVA_HOME goto findJavaFromJavaHome diff --git a/src/main/java/net/querz/mca/Chunk.java b/src/main/java/net/querz/mca/Chunk.java index 89a712b4..59c45b07 100644 --- a/src/main/java/net/querz/mca/Chunk.java +++ b/src/main/java/net/querz/mca/Chunk.java @@ -1,5 +1,6 @@ package net.querz.mca; +import java.rmi.UnexpectedException; import net.querz.nbt.tag.CompoundTag; import net.querz.nbt.tag.ListTag; import net.querz.nbt.io.NamedTag; @@ -18,6 +19,8 @@ public class Chunk { public static final int DEFAULT_DATA_VERSION = 1628; + private boolean partial; + private int lastMCAUpdate; private CompoundTag data; @@ -64,48 +67,50 @@ private void initReferences(long loadFlags) { this.dataVersion = data.getInt("DataVersion"); this.inhabitedTime = level.getLong("InhabitedTime"); this.lastUpdate = level.getLong("LastUpdate"); - if((loadFlags | BIOMES) != 0) { + if((loadFlags & BIOMES) != 0) { this.biomes = level.getIntArray("Biomes"); } - if((loadFlags | HEIGHTMAPS) != 0) { + if((loadFlags & HEIGHTMAPS) != 0) { this.heightMaps = level.getCompoundTag("Heightmaps"); } - if((loadFlags | CARVING_MARKS) != 0) { + if((loadFlags & CARVINGMASKS) != 0) { this.carvingMasks = level.getCompoundTag("CarvingMasks"); } - if((loadFlags | ENTITIES) != 0) { + if((loadFlags & ENTITIES) != 0) { this.entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null; } - if((loadFlags | TILE_ENTITIES) != 0) { + if((loadFlags & TILE_ENTITIES) != 0) { this.tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null; } - if((loadFlags | TILE_TICKS) != 0) { + if((loadFlags & TILE_TICKS) != 0) { this.tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null; } - if((loadFlags | LIQUID_TILE_TICKS) != 0) { + if((loadFlags & LIQUID_TICKS) != 0) { this.liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null; } - if((loadFlags | LIGHTS) != 0) { + if((loadFlags & LIGHTS) != 0) { this.lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null; } - if((loadFlags | LIQUIDS_TO_BE_TICKED) != 0) { + if((loadFlags & LIQUIDS_TO_BE_TICKED) != 0) { this.liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null; } - if((loadFlags | TO_BE_TICKED) != 0) { + if((loadFlags & TO_BE_TICKED) != 0) { this.toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null; } - if((loadFlags | POST_PROCESSING) != 0) { + if((loadFlags & POST_PROCESSING) != 0) { this.postProcessing = level.containsKey("PostProcessing") ? level.getListTag("PostProcessing").asListTagList() : null; } this.status = level.getString("Status"); - this.structures = level.getCompoundTag("Structures"); - if (level.containsKey("Sections")) { + if((loadFlags & STRUCTURES) != 0) { + this.structures = level.getCompoundTag("Structures"); + } + if ((loadFlags & (BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT)) != 0 && level.containsKey("Sections")) { for (CompoundTag section : level.getListTag("Sections").asCompoundTagList()) { int sectionIndex = section.getByte("Y"); if (sectionIndex > 15 || sectionIndex < 0) { continue; } - Section newSection = new Section(section, dataVersion); + Section newSection = new Section(section, dataVersion, loadFlags); if (newSection.isEmpty()) { continue; } @@ -116,6 +121,9 @@ private void initReferences(long loadFlags) { // If we haven't requested the full set of data we can drop the underlying raw data to let the GC handle it. if(loadFlags != ALL_DATA) { this.data = null; + this.partial = true; + } else { + partial = false; } } @@ -125,9 +133,13 @@ private void initReferences(long loadFlags) { * @param xPos The x-coordinate of the chunk. * @param zPos The z-coodrinate of the chunk. * @return The amount of bytes written to the RandomAccessFile. + * @throws UnsupportedOperationException When something went wrong during writing. * @throws IOException When something went wrong during writing. */ public int serialize(RandomAccessFile raf, int xPos, int zPos) throws IOException { + if(this.partial) { + throw new UnsupportedOperationException("Partially loaded chunks cannot be serialized"); + } ByteArrayOutputStream baos = new ByteArrayOutputStream(4096); try (BufferedOutputStream nbtOut = new BufferedOutputStream(CompressionType.ZLIB.compress(baos))) { new NBTSerializer(false).toStream(new NamedTag(null, updateHandle(xPos, zPos)), nbtOut); @@ -608,7 +620,7 @@ public CompoundTag updateHandle(int xPos, int zPos) { } else { if (biomes != null && biomes.length == 1024) level.putIntArray("Biomes", biomes); } - if (heightMaps != null) level.put("HeightMaps", heightMaps); + if (heightMaps != null) level.put("Heightmaps", heightMaps); if (carvingMasks != null) level.put("CarvingMasks", carvingMasks); if (entities != null) level.put("Entities", entities); if (tileEntities != null) level.put("TileEntities", tileEntities); diff --git a/src/main/java/net/querz/mca/LoadFlags.java b/src/main/java/net/querz/mca/LoadFlags.java index c2e89ff0..b9db8460 100644 --- a/src/main/java/net/querz/mca/LoadFlags.java +++ b/src/main/java/net/querz/mca/LoadFlags.java @@ -3,20 +3,19 @@ public class LoadFlags { public static long BIOMES = 0x0001; public static long HEIGHTMAPS = 0x0002; - public static long CARVINGMARKS = 0x0004; + public static long CARVINGMASKS = 0x0004; public static long ENTITIES = 0x0008; public static long TILE_ENTITIES = 0x0010; - public static long CARVING_MARKS = 0x0020; public static long TILE_TICKS = 0x0040; - public static long LIQUID_TILE_TICKS = 0x0040; - public static long TO_BE_TICKED = 0x0080; - public static long POST_PROCESSING = 0x0100; - public static long STRUCTURES = 0x0200; - public static long BLOCK_LIGHTS = 0x0400; - public static long BLOCK_STATES = 0x0800; - public static long SKY_LIGHT = 0x1000; - public static long LIGHTS = 0x2000; - public static long LIQUIDS_TO_BE_TICKED = 0x2000; + public static long LIQUID_TICKS = 0x0080; + public static long TO_BE_TICKED = 0x0100; + public static long POST_PROCESSING = 0x0200; + public static long STRUCTURES = 0x0400; + public static long BLOCK_LIGHTS = 0x0800; + public static long BLOCK_STATES = 0x1000; + public static long SKY_LIGHT = 0x2000; + public static long LIGHTS = 0x4000; + public static long LIQUIDS_TO_BE_TICKED = 0x8000; public static long ALL_DATA = 0xffffffffffffffffL; diff --git a/src/main/java/net/querz/mca/MCAUtil.java b/src/main/java/net/querz/mca/MCAUtil.java index b12304a2..b7a28ab3 100644 --- a/src/main/java/net/querz/mca/MCAUtil.java +++ b/src/main/java/net/querz/mca/MCAUtil.java @@ -40,6 +40,33 @@ public static MCAFile read(File file) throws IOException { } } + + /** + * @see MCAUtil#read(File) + * @param file The file to read the data from. + * @return An in-memory representation of the MCA file with decompressed chunk data. + * @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded + * @throws IOException if something during deserialization goes wrong. + * */ + public static MCAFile read(String file, long loadFlags) throws IOException { + return read(new File(file), loadFlags); + } + + /** + * Reads an MCA file and loads all of its chunks. + * @param file The file to read the data from. + * @return An in-memory representation of the MCA file with decompressed chunk data + * @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded + * @throws IOException if something during deserialization goes wrong. + * */ + public static MCAFile read(File file, long loadFlags) throws IOException { + MCAFile mcaFile = newMCAFile(file); + try (RandomAccessFile raf = new RandomAccessFile(file, "r")) { + mcaFile.deserialize(raf, loadFlags); + return mcaFile; + } + } + /** * Calls {@link MCAUtil#write(MCAFile, File, boolean)} without changing the timestamps. * @see MCAUtil#write(MCAFile, File, boolean) diff --git a/src/main/java/net/querz/mca/Section.java b/src/main/java/net/querz/mca/Section.java index ea4887f4..e6b25327 100644 --- a/src/main/java/net/querz/mca/Section.java +++ b/src/main/java/net/querz/mca/Section.java @@ -43,13 +43,13 @@ public Section(CompoundTag sectionRoot, int dataVersion, long loadFlags) { LongArrayTag blockStates = sectionRoot.getLongArrayTag("BlockStates"); ByteArrayTag skyLight = sectionRoot.getByteArrayTag("SkyLight"); - if((loadFlags | BLOCK_LIGHTS) != 0) { + if((loadFlags & BLOCK_LIGHTS) != 0) { this.blockLight = blockLight != null ? blockLight.getValue() : null; } - if((loadFlags | BLOCK_STATES) != 0) { + if((loadFlags & BLOCK_STATES) != 0) { this.blockStates = blockStates != null ? blockStates.getValue() : null; } - if((loadFlags | SKY_LIGHT) != 0) { + if((loadFlags & SKY_LIGHT) != 0) { this.skyLight = skyLight != null ? skyLight.getValue() : null; } } diff --git a/src/test/java/net/querz/mca/MCAFileTest.java b/src/test/java/net/querz/mca/MCAFileTest.java index 794489bf..2ebb4f11 100644 --- a/src/test/java/net/querz/mca/MCAFileTest.java +++ b/src/test/java/net/querz/mca/MCAFileTest.java @@ -2,6 +2,7 @@ import net.querz.nbt.tag.CompoundTag; import net.querz.nbt.tag.ListTag; +import static net.querz.mca.LoadFlags.*; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; @@ -343,6 +344,79 @@ public void testChunkInvalidDataTag() { }, IOException.class); } + private void assertLoadFLag(Object field, long flags, long wantedFlag) { + if((flags & wantedFlag) != 0) { + assertNotNull(String.format("Should not be null. Flags=%08x, Wanted flag=%08x", flags, wantedFlag), field); + } else { + assertNull(String.format("Should be null. Flags=%08x, Wanted flag=%08x", flags, wantedFlag), field); + } + } + + private void assertPartialChunk(Chunk c, long loadFlags) { + assertLoadFLag(c.getBiomes(), loadFlags, BIOMES); + assertLoadFLag(c.getHeightMaps(), loadFlags, HEIGHTMAPS); + assertLoadFLag(c.getEntities(), loadFlags, ENTITIES); + assertLoadFLag(c.getCarvingMasks(), loadFlags, CARVINGMASKS); + assertLoadFLag(c.getLights(), loadFlags, LIGHTS); + assertLoadFLag(c.getPostProcessing(), loadFlags, POST_PROCESSING); + assertLoadFLag(c.getLiquidTicks(), loadFlags, LIQUID_TICKS); + assertLoadFLag(c.getLiquidsToBeTicked(), loadFlags, LIQUIDS_TO_BE_TICKED); + assertLoadFLag(c.getTileTicks(), loadFlags, TILE_TICKS); + assertLoadFLag(c.getTileEntities(), loadFlags, TILE_ENTITIES); + assertLoadFLag(c.getToBeTicked(), loadFlags, TO_BE_TICKED); + assertLoadFLag(c.getSection(0), loadFlags, BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT); + if((loadFlags & (BLOCK_LIGHTS|BLOCK_STATES|SKY_LIGHT)) != 0) { + Section s = c.getSection(0); + assertNotNull(String.format("Section is null. Flags=%08x", loadFlags), s); + assertLoadFLag(s.getBlockStates(), loadFlags, BLOCK_STATES); + assertLoadFLag(s.getBlockLight(), loadFlags, BLOCK_LIGHTS); + assertLoadFLag(s.getSkyLight(), loadFlags, SKY_LIGHT); + } + } + + public void testPartialLoad() { + long[] flags = new long[] { + BIOMES, + HEIGHTMAPS, + ENTITIES, + CARVINGMASKS, + LIGHTS, + POST_PROCESSING, + LIQUID_TICKS, + LIQUIDS_TO_BE_TICKED, + TILE_TICKS, + TILE_ENTITIES, + TO_BE_TICKED, + BLOCK_STATES, + BLOCK_LIGHTS, + SKY_LIGHT + }; + + MCAFile f = assertThrowsNoException(() -> MCAUtil.read(copyResourceToTmp("r.2.2.mca"))); + Chunk c = f.getChunk(0); + c.setCarvingMasks(getSomeCompoundTag()); + c.setEntities(getSomeCompoundTagList()); + c.setLights(getSomeListTagList()); + c.setTileEntities(getSomeCompoundTagList()); + c.setTileTicks(getSomeCompoundTagList()); + c.setLiquidTicks(getSomeCompoundTagList()); + c.setToBeTicked(getSomeListTagList()); + c.setLiquidsToBeTicked(getSomeListTagList()); + c.setHeightMaps(getSomeCompoundTag()); + c.setPostProcessing(getSomeListTagList()); + c.getSection(0).setBlockLight(new byte[2048]); + File tmp = this.getNewTmpFile("r.2.2.mca"); + assertThrowsNoException(() -> MCAUtil.write(f, tmp)); + + for(long flag : flags) { + MCAFile mcaFile = assertThrowsNoException(() -> MCAUtil.read(tmp, flag)); + c = mcaFile.getChunk(0, 0); + assertPartialChunk(c, flag); + assertThrowsException(() -> MCAUtil.write(mcaFile, getNewTmpFile("r.12.34.mca")), UnsupportedOperationException.class); + } + tmp.delete(); + } + public void test1_15GetBiomeAt() throws IOException { MCAFile f = assertThrowsNoException(() -> MCAUtil.read(copyResourceToTmp("r.0.0.mca"))); assertEquals(162, f.getBiomeAt(31, 0, 63));