Search in sources :

Example 1 with EntitySerializer

use of org.terasology.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);
    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 : Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntitySerializer(org.terasology.persistence.serializers.EntitySerializer)

Example 2 with EntitySerializer

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

the class EntitySerializerTest method setup.

@Before
public void setup() {
    context.put(NetworkSystem.class, mock(NetworkSystem.class));
    EntitySystemSetupUtil.addReflectionBasedLibraries(context);
    EntitySystemSetupUtil.addEntityManagementRelatedClasses(context);
    entityManager = context.get(EngineEntityManager.class);
    entityManager.getComponentLibrary().register(new SimpleUri("test", "gettersetter"), GetterSetterComponent.class);
    entityManager.getComponentLibrary().register(new SimpleUri("test", "string"), StringComponent.class);
    entityManager.getComponentLibrary().register(new SimpleUri("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.entitySystem.entity.internal.EngineEntityManager) PrefabData(org.terasology.entitySystem.prefab.PrefabData) StringComponent(org.terasology.entitySystem.stubs.StringComponent) NetworkSystem(org.terasology.network.NetworkSystem) SimpleUri(org.terasology.engine.SimpleUri) ResourceUrn(org.terasology.assets.ResourceUrn) Prefab(org.terasology.entitySystem.prefab.Prefab) PojoPrefab(org.terasology.entitySystem.prefab.internal.PojoPrefab) EntitySerializer(org.terasology.persistence.serializers.EntitySerializer) Before(org.junit.Before)

Example 3 with EntitySerializer

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

Example 4 with EntitySerializer

use of org.terasology.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.getUri().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.setComponentIdMapping(componentIdTable);
    for (EntityRef entity : entities) {
        if (entity.isPersistent()) {
            store.addEntity(entitySerializer.serialize(entity));
        }
    }
    store.setNextEntityId(nextEntityId);
    return store.build();
}
Also used : Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab) EntityRef(org.terasology.entitySystem.entity.EntityRef) HashSet(java.util.HashSet) EntitySerializer(org.terasology.persistence.serializers.EntitySerializer)

Aggregations

EntitySerializer (org.terasology.persistence.serializers.EntitySerializer)4 Component (org.terasology.entitySystem.Component)2 EntityRef (org.terasology.entitySystem.entity.EntityRef)2 Prefab (org.terasology.entitySystem.prefab.Prefab)2 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 ResourceUrn (org.terasology.assets.ResourceUrn)1 SimpleUri (org.terasology.engine.SimpleUri)1 EngineEntityManager (org.terasology.entitySystem.entity.internal.EngineEntityManager)1 PrefabData (org.terasology.entitySystem.prefab.PrefabData)1 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)1 StringComponent (org.terasology.entitySystem.stubs.StringComponent)1 NetworkSystem (org.terasology.network.NetworkSystem)1