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