use of org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck in project Terasology by MovingBlocks.
the class EntityRestorer method restore.
public Map<String, EntityRef> restore(EntityData.EntityStore store) {
EntitySerializer serializer = new EntitySerializer(entityManager);
serializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
Map<Class<? extends Component>, Integer> idMap = Maps.newHashMap();
for (int i = 0; i < store.getComponentClassCount(); ++i) {
ComponentMetadata<?> metadata = entityManager.getComponentLibrary().resolve(store.getComponentClass(i));
if (metadata != null) {
idMap.put(metadata.getType(), i);
}
}
serializer.setComponentIdMapping(idMap);
store.getEntityList().forEach(serializer::deserialize);
Map<String, EntityRef> namedEntities = Maps.newHashMap();
for (int i = 0; i < store.getEntityNameCount() && i < store.getEntityNamedCount(); ++i) {
namedEntities.put(store.getEntityName(i), entityManager.getEntity(store.getEntityNamed(i)));
}
return namedEntities;
}
use of org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck in project Terasology by MovingBlocks.
the class EntitySerializerTest method testComponentWithDoNotPersistIsNotPersisted.
@Test
public void testComponentWithDoNotPersistIsNotPersisted() {
componentLibrary.register(new ResourceUrn("test", "nonpersisted"), NonpersistedComponent.class);
EntityRef entity = entityManager.create();
entity.addComponent(new NonpersistedComponent());
entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
EntityData.Entity entityData = entitySerializer.serialize(entity);
long nextId = entityManager.getNextId();
entityManager.clear();
entityManager.setNextId(nextId);
EntityRef loadedEntity = entitySerializer.deserialize(entityData);
assertTrue(loadedEntity.exists());
assertFalse(loadedEntity.hasComponent(NonpersistedComponent.class));
}
use of org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck in project Terasology by MovingBlocks.
the class EntitySerializerTest method testComponentWithDoNotPersistIsIgnoredUponDeserialization.
@Test
public void testComponentWithDoNotPersistIsIgnoredUponDeserialization() {
componentLibrary.register(new ResourceUrn("test", "nonpersisted"), NonpersistedComponent.class);
EntityRef entity = entityManager.create();
entity.addComponent(new NonpersistedComponent());
entitySerializer.setComponentSerializeCheck(metadata -> true);
EntityData.Entity entityData = entitySerializer.serialize(entity);
long nextId = entityManager.getNextId();
entityManager.clear();
entityManager.setNextId(nextId);
entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
EntityRef loadedEntity = entitySerializer.deserialize(entityData);
assertTrue(loadedEntity.exists());
assertFalse(loadedEntity.hasComponent(NonpersistedComponent.class));
}
use of org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck in project Terasology by MovingBlocks.
the class GlobalStoreBuilder method build.
public EntityData.GlobalStore build(EngineEntityManager entityManager, Iterable<EntityRef> entities) {
EntityData.GlobalStore.Builder store = EntityData.GlobalStore.newBuilder();
Map<Class<? extends Component>, Integer> componentIdTable = Maps.newHashMap();
for (ComponentMetadata<?> componentMetadata : entityManager.getComponentLibrary().iterateComponentMetadata()) {
store.addComponentClass(componentMetadata.getId().toString());
componentIdTable.put(componentMetadata.getType(), componentIdTable.size());
}
prefabSerializer.setComponentIdMapping(componentIdTable);
/*
* The prefabs can't be obtained from entityManager.getPrefabManager().listPrefabs() as that might not
* be thread save.
*/
Set<Prefab> prefabsRequiredForEntityStorage = new HashSet<>();
for (EntityRef entityRef : entityManager.getAllEntities()) {
Prefab prefab = entityRef.getParentPrefab();
if (prefab != null) {
prefabsRequiredForEntityStorage.add(prefab);
}
}
for (Prefab prefab : prefabsRequiredForEntityStorage) {
store.addPrefab(prefabSerializer.serialize(prefab));
}
EntitySerializer entitySerializer = new EntitySerializer(entityManager);
entitySerializer.setComponentSerializeCheck(new PersistenceComponentSerializeCheck());
entitySerializer.setComponentIdMapping(componentIdTable);
for (EntityRef entity : entities) {
if (entity.isPersistent()) {
store.addEntity(entitySerializer.serialize(entity));
}
}
store.setNextEntityId(nextEntityId);
return store.build();
}
Aggregations