Search in sources :

Example 1 with PersistenceComponentSerializeCheck

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;
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) Component(org.terasology.gestalt.entitysystem.component.Component) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) EntitySerializer(org.terasology.engine.persistence.serializers.EntitySerializer)

Example 2 with PersistenceComponentSerializeCheck

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));
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) EntityData(org.terasology.protobuf.EntityData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 3 with PersistenceComponentSerializeCheck

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));
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) EntityData(org.terasology.protobuf.EntityData) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Test(org.junit.jupiter.api.Test)

Example 4 with PersistenceComponentSerializeCheck

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();
}
Also used : PersistenceComponentSerializeCheck(org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck) EntitySerializer(org.terasology.engine.persistence.serializers.EntitySerializer) Component(org.terasology.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) HashSet(java.util.HashSet)

Aggregations

EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)4 PersistenceComponentSerializeCheck (org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck)4 Test (org.junit.jupiter.api.Test)2 EntitySerializer (org.terasology.engine.persistence.serializers.EntitySerializer)2 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)2 Component (org.terasology.gestalt.entitysystem.component.Component)2 EntityData (org.terasology.protobuf.EntityData)2 HashSet (java.util.HashSet)1 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)1