-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
common/src/main/java/org/popcraft/chunky/util/RegionFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package org.popcraft.chunky.util; | ||
|
||
import com.google.gson.reflect.TypeToken; | ||
import de.bluecolored.bluenbt.BlueNBT; | ||
import de.bluecolored.bluenbt.NBTName; | ||
import org.popcraft.chunky.nbt.util.ChunkPos; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.DataInputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.RandomAccessFile; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.zip.InflaterInputStream; | ||
|
||
public final class RegionFile { | ||
private static final int ENTRIES = 1024; | ||
private static final int SECTOR_SIZE = 4096; | ||
private final Set<Chunk> chunks = new HashSet<>(); | ||
private final Map<ChunkPos, Chunk> chunkMap = new HashMap<>(); | ||
|
||
public RegionFile(final File file) { | ||
final BlueNBT blueNBT = new BlueNBT(); | ||
try (final RandomAccessFile region = new RandomAccessFile(file, "r")) { | ||
if (region.length() < 4096) { | ||
return; | ||
} | ||
final int[] offsetTable = new int[ENTRIES]; | ||
final int[] sizeTable = new int[ENTRIES]; | ||
for (int i = 0; i < ENTRIES; ++i) { | ||
final int location = region.readInt(); | ||
offsetTable[i] = (location >> 8) & 0xFFFFFF; | ||
sizeTable[i] = location & 0xFF; | ||
} | ||
final int[] timestampTable = new int[ENTRIES]; | ||
for (int i = 0; i < ENTRIES; ++i) { | ||
timestampTable[i] = region.readInt(); | ||
} | ||
for (int i = 0; i < ENTRIES; ++i) { | ||
final int offset = offsetTable[i] * SECTOR_SIZE; | ||
final int size = sizeTable[i] * SECTOR_SIZE; | ||
if (offset == 0 && size == 0) { | ||
continue; | ||
} | ||
region.seek(offset); | ||
final int length = region.readInt(); | ||
final byte compressionType = region.readByte(); | ||
if (compressionType != 2) { | ||
throw new UnsupportedOperationException("Not in zlib format"); | ||
} | ||
final byte[] compressed = new byte[length - 1]; | ||
region.readFully(compressed); | ||
final DataInputStream input = new DataInputStream(new InflaterInputStream(new ByteArrayInputStream(compressed))); | ||
final Chunk chunk = blueNBT.read(input, TypeToken.get(Chunk.class)); | ||
chunks.add(chunk); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
for (final Chunk chunk : chunks) { | ||
chunkMap.put(ChunkPos.of(chunk.x, chunk.z), chunk); | ||
} | ||
} | ||
|
||
public Collection<Chunk> getChunks() { | ||
return chunks; | ||
} | ||
|
||
public Optional<Chunk> getChunk(final int x, final int z) { | ||
return Optional.ofNullable(chunkMap.get(ChunkPos.of(x, z))); | ||
} | ||
|
||
public static class Chunk { | ||
@NBTName("xPos") | ||
public int x; | ||
@NBTName("zPos") | ||
public int z; | ||
@NBTName("InhabitedTime") | ||
public long inhabitedTime = -1; | ||
} | ||
} |