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()));
}
});
}
}
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();
}
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());
}
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;
}
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));
}
Aggregations