Search in sources :

Example 1 with McRegionReader

use of com.sk89q.worldedit.world.storage.McRegionReader in project FastAsyncWorldEdit by IntellectualSites.

the class FolderSnapshot method getChunkTag.

@Override
public CompoundTag getChunkTag(BlockVector3 position) throws DataException, IOException {
    BlockVector2 pos = position.toBlockVector2();
    Optional<Path> regFolder = getRegionFolder();
    if (!regFolder.isPresent()) {
        Path chunkFile = getFolder().resolve(LegacyChunkStore.getFilename(pos, "/"));
        if (!Files.exists(chunkFile)) {
            throw new MissingChunkException();
        }
        return ChunkStoreHelper.readCompoundTag(() -> new GZIPInputStream(Files.newInputStream(chunkFile)));
    }
    Path regionFile = regFolder.get().resolve(McRegionChunkStore.getFilename(pos));
    if (!Files.exists(regionFile)) {
        // Try mcr as well
        regionFile = regionFile.resolveSibling(regionFile.getFileName().toString().replace(".mca", ".mcr"));
        if (!Files.exists(regionFile)) {
            throw new MissingChunkException();
        }
    }
    try (InputStream stream = Files.newInputStream(regionFile)) {
        McRegionReader regionReader = new McRegionReader(stream);
        return ChunkStoreHelper.readCompoundTag(() -> regionReader.getChunkInputStream(pos));
    }
}
Also used : Path(java.nio.file.Path) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) McRegionReader(com.sk89q.worldedit.world.storage.McRegionReader) BlockVector2(com.sk89q.worldedit.math.BlockVector2) MissingChunkException(com.sk89q.worldedit.world.storage.MissingChunkException)

Example 2 with McRegionReader

use of com.sk89q.worldedit.world.storage.McRegionReader in project FastAsyncWorldEdit by IntellectualSites.

the class FileSystemSnapshotDatabaseTest method setUpStatic.

@BeforeAll
static void setUpStatic() throws IOException, DataException {
    try (InputStream in = Resources.getResource("world_region.mca.gzip").openStream();
        GZIPInputStream gzIn = new GZIPInputStream(in)) {
        REGION_DATA = ByteStreams.toByteArray(gzIn);
    }
    McRegionReader reader = new McRegionReader(new ByteArrayInputStream(REGION_DATA));
    try {
        // Find the single chunk
        BlockVector2 chunkPos = IntStream.range(0, 32).mapToObj(x -> IntStream.range(0, 32).filter(z -> reader.hasChunk(x, z)).mapToObj(z -> BlockVector2.at(x, z))).flatMap(Function.identity()).findAny().orElseThrow(() -> new AssertionError("No chunk in region file."));
        ByteArrayOutputStream cap = new ByteArrayOutputStream();
        try (InputStream in = reader.getChunkInputStream(chunkPos);
            GZIPOutputStream gzOut = new GZIPOutputStream(cap)) {
            ByteStreams.copy(in, gzOut);
        }
        CHUNK_DATA = cap.toByteArray();
        CHUNK_TAG = ChunkStoreHelper.readCompoundTag(() -> new GZIPInputStream(new ByteArrayInputStream(CHUNK_DATA)));
        CHUNK_POS = chunkPos.toBlockVector3();
    } finally {
        reader.close();
    }
    TEMP_DIR = Files.createTempDirectory("worldedit-fs-snap-dbs").toRealPath();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) IntStream(java.util.stream.IntStream) ChunkStoreHelper(com.sk89q.worldedit.world.storage.ChunkStoreHelper) GZIPInputStream(java.util.zip.GZIPInputStream) BlockVector2(com.sk89q.worldedit.math.BlockVector2) TestFactory(org.junit.jupiter.api.TestFactory) BlockVector3(com.sk89q.worldedit.math.BlockVector3) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ZonedDateTime(java.time.ZonedDateTime) DynamicNode(org.junit.jupiter.api.DynamicNode) Function(java.util.function.Function) AfterAll(org.junit.jupiter.api.AfterAll) ByteArrayInputStream(java.io.ByteArrayInputStream) BeforeAll(org.junit.jupiter.api.BeforeAll) DynamicContainer.dynamicContainer(org.junit.jupiter.api.DynamicContainer.dynamicContainer) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Path(java.nio.file.Path) DataException(com.sk89q.worldedit.world.DataException) McRegionReader(com.sk89q.worldedit.world.storage.McRegionReader) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ArchiveNioSupport(com.sk89q.worldedit.util.io.file.ArchiveNioSupport) Resources(com.google.common.io.Resources) Files(java.nio.file.Files) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Instant(java.time.Instant) ZoneId(java.time.ZoneId) DisplayName(org.junit.jupiter.api.DisplayName) Test(org.junit.jupiter.api.Test) UncheckedIOException(java.io.UncheckedIOException) ArchiveNioSupports(com.sk89q.worldedit.util.io.file.ArchiveNioSupports) FileVisitResult(java.nio.file.FileVisitResult) Stream(java.util.stream.Stream) DateTimeFormatter(java.time.format.DateTimeFormatter) ByteStreams(com.google.common.io.ByteStreams) CompoundTag(com.sk89q.jnbt.CompoundTag) GZIPOutputStream(java.util.zip.GZIPOutputStream) ZipArchiveNioSupport(com.sk89q.worldedit.util.io.file.ZipArchiveNioSupport) InputStream(java.io.InputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) McRegionReader(com.sk89q.worldedit.world.storage.McRegionReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BlockVector2(com.sk89q.worldedit.math.BlockVector2) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

BlockVector2 (com.sk89q.worldedit.math.BlockVector2)2 McRegionReader (com.sk89q.worldedit.world.storage.McRegionReader)2 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 ByteStreams (com.google.common.io.ByteStreams)1 Resources (com.google.common.io.Resources)1 CompoundTag (com.sk89q.jnbt.CompoundTag)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 ArchiveNioSupport (com.sk89q.worldedit.util.io.file.ArchiveNioSupport)1 ArchiveNioSupports (com.sk89q.worldedit.util.io.file.ArchiveNioSupports)1 ZipArchiveNioSupport (com.sk89q.worldedit.util.io.file.ZipArchiveNioSupport)1 DataException (com.sk89q.worldedit.world.DataException)1 ChunkStoreHelper (com.sk89q.worldedit.world.storage.ChunkStoreHelper)1 MissingChunkException (com.sk89q.worldedit.world.storage.MissingChunkException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 FileVisitResult (java.nio.file.FileVisitResult)1