Search in sources :

Example 1 with EntitySerializer

use of org.terasology.engine.persistence.serializers.EntitySerializer in project Terasology by MovingBlocks.

the class EntitySerializerTest method setup.

@BeforeEach
public void setup() {
    NetworkSystem networkSystem = mock(NetworkSystem.class);
    when(networkSystem.getMode()).thenReturn(NetworkMode.NONE);
    context.put(NetworkSystem.class, networkSystem);
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    entityManager = context.get(EngineEntityManager.class);
    entityManager.getComponentLibrary().register(new ResourceUrn("test", "gettersetter"), GetterSetterComponent.class);
    entityManager.getComponentLibrary().register(new ResourceUrn("test", "string"), StringComponent.class);
    entityManager.getComponentLibrary().register(new ResourceUrn("test", "integer"), IntegerComponent.class);
    entitySerializer = new EntitySerializer(entityManager);
    componentLibrary = entityManager.getComponentLibrary();
    PrefabData prefabData = new PrefabData();
    prefabData.addComponent(new StringComponent("Value"));
    prefab = Assets.generateAsset(new ResourceUrn("test:Test"), prefabData, Prefab.class);
}
Also used : EngineEntityManager(org.terasology.engine.entitySystem.entity.internal.EngineEntityManager) PrefabData(org.terasology.engine.entitySystem.prefab.PrefabData) StringComponent(org.terasology.unittest.stubs.StringComponent) NetworkSystem(org.terasology.engine.network.NetworkSystem) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) PojoPrefab(org.terasology.engine.entitySystem.prefab.internal.PojoPrefab) EntitySerializer(org.terasology.engine.persistence.serializers.EntitySerializer) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with EntitySerializer

use of org.terasology.engine.persistence.serializers.EntitySerializer 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 3 with EntitySerializer

use of org.terasology.engine.persistence.serializers.EntitySerializer in project Terasology by MovingBlocks.

the class BaseEntityRef method toFullDescription.

@Override
public String toFullDescription() {
    EntitySerializer serializer = new EntitySerializer((EngineEntityManager) entityManager);
    serializer.setUsingFieldIds(false);
    return AccessController.doPrivileged((PrivilegedAction<String>) () -> EntityDataJSONFormat.write(serializer.serialize(this)));
}
Also used : EntitySerializer(org.terasology.engine.persistence.serializers.EntitySerializer)

Example 4 with EntitySerializer

use of org.terasology.engine.persistence.serializers.EntitySerializer 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

EntitySerializer (org.terasology.engine.persistence.serializers.EntitySerializer)4 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)2 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)2 PersistenceComponentSerializeCheck (org.terasology.engine.persistence.serializers.PersistenceComponentSerializeCheck)2 Component (org.terasology.gestalt.entitysystem.component.Component)2 HashSet (java.util.HashSet)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 EngineEntityManager (org.terasology.engine.entitySystem.entity.internal.EngineEntityManager)1 PrefabData (org.terasology.engine.entitySystem.prefab.PrefabData)1 PojoPrefab (org.terasology.engine.entitySystem.prefab.internal.PojoPrefab)1 NetworkSystem (org.terasology.engine.network.NetworkSystem)1 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)1 StringComponent (org.terasology.unittest.stubs.StringComponent)1