Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for partial chunk/region load #49

Merged
merged 7 commits into from
Jun 8, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions src/main/java/net/querz/mca/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import static net.querz.mca.LoadFlags.*;

public class Chunk {

Expand Down Expand Up @@ -49,10 +50,10 @@ public class Chunk {
*/
public Chunk(CompoundTag data) {
this.data = data;
initReferences();
initReferences(ALL_DATA);
}

private void initReferences() {
private void initReferences(long loadFlags) {
if (data == null) {
throw new NullPointerException("data cannot be null");
}
Expand All @@ -63,17 +64,39 @@ private void initReferences() {
this.dataVersion = data.getInt("DataVersion");
this.inhabitedTime = level.getLong("InhabitedTime");
this.lastUpdate = level.getLong("LastUpdate");
this.biomes = level.getIntArray("Biomes");
this.heightMaps = level.getCompoundTag("Heightmaps");
this.carvingMasks = level.getCompoundTag("CarvingMasks");
this.entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null;
this.tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null;
this.tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null;
this.liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null;
this.lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null;
this.liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null;
this.toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null;
this.postProcessing = level.containsKey("PostProcessing") ? level.getListTag("PostProcessing").asListTagList() : null;
if((loadFlags | BIOMES) != 0) {
this.biomes = level.getIntArray("Biomes");
}
if((loadFlags | HEIGHTMAPS) != 0) {
this.heightMaps = level.getCompoundTag("Heightmaps");
}
if((loadFlags | CARVING_MARKS) != 0) {
this.carvingMasks = level.getCompoundTag("CarvingMasks");
}
if((loadFlags | ENTITIES) != 0) {
this.entities = level.containsKey("Entities") ? level.getListTag("Entities").asCompoundTagList() : null;
}
if((loadFlags | TILE_ENTITIES) != 0) {
this.tileEntities = level.containsKey("TileEntities") ? level.getListTag("TileEntities").asCompoundTagList() : null;
}
if((loadFlags | TILE_TICKS) != 0) {
this.tileTicks = level.containsKey("TileTicks") ? level.getListTag("TileTicks").asCompoundTagList() : null;
}
if((loadFlags | LIQUID_TILE_TICKS) != 0) {
this.liquidTicks = level.containsKey("LiquidTicks") ? level.getListTag("LiquidTicks").asCompoundTagList() : null;
}
if((loadFlags | LIGHTS) != 0) {
this.lights = level.containsKey("Lights") ? level.getListTag("Lights").asListTagList() : null;
}
if((loadFlags | LIQUIDS_TO_BE_TICKED) != 0) {
this.liquidsToBeTicked = level.containsKey("LiquidsToBeTicked") ? level.getListTag("LiquidsToBeTicked").asListTagList() : null;
}
if((loadFlags | TO_BE_TICKED) != 0) {
this.toBeTicked = level.containsKey("ToBeTicked") ? level.getListTag("ToBeTicked").asListTagList() : null;
}
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")) {
Expand All @@ -86,10 +109,14 @@ private void initReferences() {
if (newSection.isEmpty()) {
continue;
}

this.sections[sectionIndex] = newSection;
}
}

// 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;
}
}

/**
Expand Down Expand Up @@ -118,6 +145,16 @@ public int serialize(RandomAccessFile raf, int xPos, int zPos) throws IOExceptio
* @throws IOException When something went wrong during reading.
*/
public void deserialize(RandomAccessFile raf) throws IOException {
deserialize(raf, ALL_DATA);
}

/**
* Reads chunk data from a RandomAccessFile. The RandomAccessFile must already be at the correct position.
* @param raf The RandomAccessFile to read the chunk data from.
* @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded
* @throws IOException When something went wrong during reading.
*/
public void deserialize(RandomAccessFile raf, long loadFlags) throws IOException {
byte compressionTypeByte = raf.readByte();
CompressionType compressionType = CompressionType.getFromID(compressionTypeByte);
if (compressionType == null) {
Expand All @@ -127,7 +164,7 @@ public void deserialize(RandomAccessFile raf) throws IOException {
NamedTag tag = new NBTDeserializer(false).fromStream(dis);
if (tag != null && tag.getTag() instanceof CompoundTag) {
data = (CompoundTag) tag.getTag();
initReferences();
initReferences(loadFlags);
} else {
throw new IOException("invalid data tag: " + (tag == null ? "null" : tag.getClass().getName()));
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/querz/mca/LoadFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.querz.mca;

public class LoadFlags {
public static long BIOMES = 0x0001;
public static long HEIGHTMAPS = 0x0002;
public static long CARVINGMARKS = 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LIQUID_TILE_TICKS should not be the same as TILE_TICKS

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LIQUIDS_TO_BE_TICKED should not be the same as LIGHTS


public static long ALL_DATA = 0xffffffffffffffffL;


}
13 changes: 12 additions & 1 deletion src/main/java/net/querz/mca/MCAFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ public MCAFile(int regionX, int regionZ) {
* @throws IOException If something went wrong during deserialization.
* */
public void deserialize(RandomAccessFile raf) throws IOException {
deserialize(raf, LoadFlags.ALL_DATA);
}

/**
* Reads an .mca file from a {@code RandomAccessFile} into this object.
* This method does not perform any cleanups on the data.
* @param raf The {@code RandomAccessFile} to read from.
* @param loadFlags A logical or of {@link LoadFlags} constants indicating what data should be loaded
* @throws IOException If something went wrong during deserialization.
* */
public void deserialize(RandomAccessFile raf, long loadFlags) throws IOException {
chunks = new Chunk[1024];
for (int i = 0; i < 1024; i++) {
raf.seek(i * 4);
Expand All @@ -47,7 +58,7 @@ public void deserialize(RandomAccessFile raf) throws IOException {
int timestamp = raf.readInt();
Chunk chunk = new Chunk(timestamp);
raf.seek(4096 * offset + 4); //+4: skip data size
chunk.deserialize(raf);
chunk.deserialize(raf, loadFlags);
chunks[i] = chunk;
}
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/net/querz/mca/Section.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.querz.mca;

import static net.querz.mca.LoadFlags.*;

import net.querz.nbt.tag.ByteArrayTag;
import net.querz.nbt.tag.CompoundTag;
import net.querz.nbt.tag.ListTag;
Expand All @@ -21,6 +23,10 @@ public class Section {
private int dataVersion;

public Section(CompoundTag sectionRoot, int dataVersion) {
this(sectionRoot, dataVersion, ALL_DATA);
}

public Section(CompoundTag sectionRoot, int dataVersion, long loadFlags) {
data = sectionRoot;
this.dataVersion = dataVersion;
ListTag<?> rawPalette = sectionRoot.getListTag("Palette");
Expand All @@ -37,9 +43,15 @@ public Section(CompoundTag sectionRoot, int dataVersion) {
LongArrayTag blockStates = sectionRoot.getLongArrayTag("BlockStates");
ByteArrayTag skyLight = sectionRoot.getByteArrayTag("SkyLight");

this.blockLight = blockLight != null ? blockLight.getValue() : null;
this.blockStates = blockStates != null ? blockStates.getValue() : null;
this.skyLight = skyLight != null ? skyLight.getValue() : null;
if((loadFlags | BLOCK_LIGHTS) != 0) {
this.blockLight = blockLight != null ? blockLight.getValue() : null;
}
if((loadFlags | BLOCK_STATES) != 0) {
this.blockStates = blockStates != null ? blockStates.getValue() : null;
}
if((loadFlags | SKY_LIGHT) != 0) {
this.skyLight = skyLight != null ? skyLight.getValue() : null;
}
}

Section() {}
Expand Down