Search in sources :

Example 1 with ChunkStore

use of org.terasology.persistence.ChunkStore in project Terasology by MovingBlocks.

the class LocalChunkProvider method createOrLoadChunk.

private void createOrLoadChunk(Vector3i chunkPos) {
    Chunk chunk = chunkCache.get(chunkPos);
    if (chunk == null && !preparingChunks.contains(chunkPos)) {
        preparingChunks.add(chunkPos);
        pipeline.doTask(new AbstractChunkTask(chunkPos) {

            @Override
            public String getName() {
                return "Create or Load Chunk";
            }

            @Override
            public void run() {
                ChunkStore chunkStore = storageManager.loadChunkStore(getPosition());
                Chunk chunk;
                EntityBufferImpl buffer = new EntityBufferImpl();
                if (chunkStore == null) {
                    chunk = new ChunkImpl(getPosition(), blockManager, biomeManager);
                    generator.createChunk(chunk, buffer);
                } else {
                    chunk = chunkStore.getChunk();
                }
                InternalLightProcessor.generateInternalLighting(chunk);
                chunk.deflate();
                TShortObjectMap<TIntList> mappings = createBatchBlockEventMappings(chunk);
                readyChunks.offer(new ReadyChunkInfo(chunk, mappings, chunkStore, buffer.getAll()));
            }
        });
    }
}
Also used : ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) AbstractChunkTask(org.terasology.world.chunks.pipeline.AbstractChunkTask) ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) EntityBufferImpl(org.terasology.world.generation.impl.EntityBufferImpl) TShortObjectMap(gnu.trove.map.TShortObjectMap) Chunk(org.terasology.world.chunks.Chunk) ManagedChunk(org.terasology.world.chunks.ManagedChunk) ChunkStore(org.terasology.persistence.ChunkStore)

Example 2 with ChunkStore

use of org.terasology.persistence.ChunkStore in project Terasology by MovingBlocks.

the class LocalChunkProviderTest method testCompleteUpdateRestoresEntitiesForRestoredChunks.

@Test
public void testCompleteUpdateRestoresEntitiesForRestoredChunks() throws Exception {
    final Chunk chunk = mockChunkAt(0, 0, 0);
    final ChunkStore chunkStore = mock(ChunkStore.class);
    final ReadyChunkInfo readyChunkInfo = ReadyChunkInfo.createForRestoredChunk(chunk, new TShortObjectHashMap<>(), chunkStore, Collections.emptyList());
    when(chunkFinalizer.completeFinalization()).thenReturn(readyChunkInfo);
    chunkProvider.completeUpdate();
    verify(chunkStore).restoreEntities();
}
Also used : ReadyChunkInfo(org.terasology.world.chunks.internal.ReadyChunkInfo) Chunk(org.terasology.world.chunks.Chunk) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Example 3 with ChunkStore

use of org.terasology.persistence.ChunkStore in project Terasology by MovingBlocks.

the class StorageManagerTest method testEntitySurvivesStorageInChunkStore.

@Test
public void testEntitySurvivesStorageInChunkStore() throws Exception {
    Chunk chunk = new ChunkImpl(CHUNK_POS, blockManager, biomeManager);
    chunk.setBlock(0, 0, 0, testBlock);
    chunk.markReady();
    ChunkProvider chunkProvider = mock(ChunkProvider.class);
    when(chunkProvider.getAllChunks()).thenReturn(Arrays.asList(chunk));
    CoreRegistry.put(ChunkProvider.class, chunkProvider);
    EntityRef entity = entityManager.create();
    long id = entity.getId();
    LocationComponent locationComponent = new LocationComponent();
    Vector3f positionInChunk = new Vector3f(chunk.getAABB().getMin());
    positionInChunk.x += 1;
    positionInChunk.y += 1;
    positionInChunk.z += 1;
    locationComponent.setWorldPosition(positionInChunk);
    entity.addComponent(locationComponent);
    esm.waitForCompletionOfPreviousSaveAndStartSaving();
    esm.finishSavingAndShutdown();
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    EngineEntityManager newEntityManager = context.get(EngineEntityManager.class);
    StorageManager newSM = new ReadWriteStorageManager(savePath, moduleEnvironment, newEntityManager, blockManager, biomeManager, false);
    newSM.loadGlobalStore();
    ChunkStore restored = newSM.loadChunkStore(CHUNK_POS);
    restored.restoreEntities();
    EntityRef ref = newEntityManager.getEntity(id);
    assertTrue(ref.exists());
    assertTrue(ref.isActive());
}
Also used : EngineEntityManager(org.terasology.entitySystem.entity.internal.EngineEntityManager) ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3f(org.terasology.math.geom.Vector3f) StorageManager(org.terasology.persistence.StorageManager) Chunk(org.terasology.world.chunks.Chunk) ChunkProvider(org.terasology.world.chunks.ChunkProvider) EntityRef(org.terasology.entitySystem.entity.EntityRef) LocationComponent(org.terasology.logic.location.LocationComponent) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Example 4 with ChunkStore

use of org.terasology.persistence.ChunkStore in project Terasology by MovingBlocks.

the class AbstractStorageManager method loadChunkStore.

@Override
public ChunkStore loadChunkStore(Vector3i chunkPos) {
    byte[] chunkData = loadCompressedChunk(chunkPos);
    ChunkStore store = null;
    if (chunkData != null) {
        ByteArrayInputStream bais = new ByteArrayInputStream(chunkData);
        try (GZIPInputStream gzipIn = new GZIPInputStream(bais)) {
            EntityData.ChunkStore storeData = EntityData.ChunkStore.parseFrom(gzipIn);
            store = new ChunkStoreInternal(storeData, entityManager, blockManager, biomeManager);
        } catch (IOException e) {
            logger.error("Failed to read existing saved chunk {}", chunkPos);
        }
    }
    return store;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityData(org.terasology.protobuf.EntityData) IOException(java.io.IOException) ChunkStore(org.terasology.persistence.ChunkStore)

Example 5 with ChunkStore

use of org.terasology.persistence.ChunkStore in project Terasology by MovingBlocks.

the class StorageManagerTest method testChunkSurvivesStorageSaveAndRestore.

@Test
public void testChunkSurvivesStorageSaveAndRestore() throws Exception {
    Chunk chunk = new ChunkImpl(CHUNK_POS, blockManager, biomeManager);
    chunk.setBlock(0, 0, 0, testBlock);
    chunk.setBlock(0, 4, 2, testBlock2);
    chunk.markReady();
    ChunkProvider chunkProvider = mock(ChunkProvider.class);
    when(chunkProvider.getAllChunks()).thenReturn(Arrays.asList(chunk));
    when(chunkProvider.getChunk(Matchers.any(Vector3i.class))).thenReturn(chunk);
    CoreRegistry.put(ChunkProvider.class, chunkProvider);
    boolean storeChunkInZips = true;
    esm.setStoreChunksInZips(storeChunkInZips);
    esm.waitForCompletionOfPreviousSaveAndStartSaving();
    esm.finishSavingAndShutdown();
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    EngineEntityManager newEntityManager = context.get(EngineEntityManager.class);
    StorageManager newSM = new ReadWriteStorageManager(savePath, moduleEnvironment, newEntityManager, blockManager, biomeManager, storeChunkInZips);
    newSM.loadGlobalStore();
    ChunkStore restored = newSM.loadChunkStore(CHUNK_POS);
    assertNotNull(restored);
    assertEquals(CHUNK_POS, restored.getChunkPosition());
    assertNotNull(restored.getChunk());
    assertEquals(testBlock, restored.getChunk().getBlock(0, 0, 0));
    assertEquals(testBlock2, restored.getChunk().getBlock(0, 4, 2));
}
Also used : EngineEntityManager(org.terasology.entitySystem.entity.internal.EngineEntityManager) ChunkImpl(org.terasology.world.chunks.internal.ChunkImpl) Vector3i(org.terasology.math.geom.Vector3i) StorageManager(org.terasology.persistence.StorageManager) Chunk(org.terasology.world.chunks.Chunk) ChunkProvider(org.terasology.world.chunks.ChunkProvider) ChunkStore(org.terasology.persistence.ChunkStore) Test(org.junit.Test)

Aggregations

ChunkStore (org.terasology.persistence.ChunkStore)6 Chunk (org.terasology.world.chunks.Chunk)5 Test (org.junit.Test)4 ChunkImpl (org.terasology.world.chunks.internal.ChunkImpl)4 ChunkProvider (org.terasology.world.chunks.ChunkProvider)3 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)2 StorageManager (org.terasology.persistence.StorageManager)2 ReadyChunkInfo (org.terasology.world.chunks.internal.ReadyChunkInfo)2 TShortObjectMap (gnu.trove.map.TShortObjectMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 Vector3f (org.terasology.math.geom.Vector3f)1 Vector3i (org.terasology.math.geom.Vector3i)1 EntityData (org.terasology.protobuf.EntityData)1 ManagedChunk (org.terasology.world.chunks.ManagedChunk)1 AbstractChunkTask (org.terasology.world.chunks.pipeline.AbstractChunkTask)1 EntityBufferImpl (org.terasology.world.generation.impl.EntityBufferImpl)1