Search in sources :

Example 1 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class ComponentSerializer method deserializeOnto.

private <T extends Component> Component deserializeOnto(Component targetComponent, EntityData.Component componentData, ComponentMetadata<T> componentMetadata, FieldSerializeCheck<Component> fieldCheck) {
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    DeserializationContext context = new ProtobufDeserializationContext(typeSerializationLibrary);
    Map<FieldMetadata<?, ?>, PersistedData> dataMap = Maps.newHashMapWithExpectedSize(componentData.getFieldCount());
    for (EntityData.NameValue field : componentData.getFieldList()) {
        FieldMetadata<?, ?> fieldInfo = null;
        if (field.hasNameIndex()) {
            fieldInfo = componentMetadata.getField(field.getNameIndex());
        } else if (field.hasName()) {
            fieldInfo = componentMetadata.getField(field.getName());
        }
        if (fieldInfo != null) {
            dataMap.put(fieldInfo, new ProtobufPersistedData(field.getValue()));
        } else if (field.hasName()) {
            logger.warn("Cannot deserialize unknown field '{}' onto '{}'", field.getName(), componentMetadata.getUri());
        }
    }
    serializer.deserializeOnto(targetComponent, dataMap, context, fieldCheck);
    return targetComponent;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) DeserializationContext(org.terasology.persistence.typeHandling.DeserializationContext) ProtobufDeserializationContext(org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext) ProtobufDeserializationContext(org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 2 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class NetworkEntitySerializer method serializeComponentDelta.

private void serializeComponentDelta(Component oldComponent, Component newComponent, FieldSerializeCheck<Component> fieldCheck, EntityData.PackedEntity.Builder entityData, ByteString.Output entityFieldIds, ByteString.Output componentFieldCounts, boolean componentInitial) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(oldComponent.getClass());
    if (componentMetadata == null) {
        logger.error("Unregistered component type: {}", oldComponent.getClass());
        return;
    }
    byte fieldCount = 0;
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
        if (fieldCheck.shouldSerializeField(field, newComponent, componentInitial)) {
            Object oldValue = field.getValue(oldComponent);
            Object newValue = field.getValue(newComponent);
            if (!Objects.equal(oldValue, newValue)) {
                PersistedData data = serializer.serializeValue(field, newValue, serializationContext);
                if (!data.isNull()) {
                    entityFieldIds.write(field.getId());
                    entityData.addFieldValue(((ProtobufPersistedData) data).getValue());
                    fieldCount++;
                } else {
                    logger.error("Exception serializing component type: {}, field: {} - returned null", componentMetadata, field);
                }
            }
        }
    }
    if (fieldCount > 0) {
        entityData.addComponentId(idTable.get(newComponent.getClass()));
        componentFieldCounts.write(fieldCount);
    }
}
Also used : ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 3 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class NetworkEntitySerializer method serializeComponentFull.

private void serializeComponentFull(Component component, boolean ignoreIfNoFields, FieldSerializeCheck<Component> fieldCheck, EntityData.PackedEntity.Builder entityData, ByteString.Output entityFieldIds, ByteString.Output componentFieldCounts, boolean componentInitial) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(component.getClass());
    if (componentMetadata == null) {
        logger.error("Unregistered component type: {}", component.getClass());
        return;
    }
    Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
    byte fieldCount = 0;
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
        if (fieldCheck.shouldSerializeField(field, component, componentInitial)) {
            PersistedData fieldValue = serializer.serialize(field, component, serializationContext);
            entityFieldIds.write(field.getId());
            entityData.addFieldValue(((ProtobufPersistedData) fieldValue).getValue());
            fieldCount++;
        }
    }
    if (fieldCount != 0 || !ignoreIfNoFields) {
        entityData.addComponentId(idTable.get(component.getClass()));
        componentFieldCounts.write(fieldCount);
    }
}
Also used : ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 4 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class BooleanTypeHandler method deserializeCollection.

@Override
public List<Boolean> deserializeCollection(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        List<Boolean> result = Lists.newArrayListWithCapacity(array.size());
        for (PersistedData item : array) {
            if (item.isBoolean()) {
                result.add(item.getAsBoolean());
            } else {
                result.add(null);
            }
        }
        return result;
    }
    return Lists.newArrayList();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedData(org.terasology.persistence.typeHandling.PersistedData)

Example 5 with PersistedData

use of org.terasology.persistence.typeHandling.PersistedData in project Terasology by MovingBlocks.

the class DoubleTypeHandler method deserializeCollection.

@Override
public List<Double> deserializeCollection(PersistedData data, DeserializationContext context) {
    if (data.isArray()) {
        PersistedDataArray array = data.getAsArray();
        List<Double> result = Lists.newArrayListWithCapacity(array.size());
        for (PersistedData item : array) {
            if (item.isNumber()) {
                result.add(item.getAsDouble());
            } else {
                result.add(null);
            }
        }
        return result;
    }
    return Lists.newArrayList();
}
Also used : PersistedDataArray(org.terasology.persistence.typeHandling.PersistedDataArray) PersistedData(org.terasology.persistence.typeHandling.PersistedData)

Aggregations

PersistedData (org.terasology.persistence.typeHandling.PersistedData)17 PersistedDataArray (org.terasology.persistence.typeHandling.PersistedDataArray)6 Test (org.junit.Test)4 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)4 Serializer (org.terasology.persistence.typeHandling.Serializer)4 ProtobufPersistedData (org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData)4 DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)3 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)3 Map (java.util.Map)2 SerializationContext (org.terasology.persistence.typeHandling.SerializationContext)2 SimpleTypeHandler (org.terasology.persistence.typeHandling.SimpleTypeHandler)2 TypeHandler (org.terasology.persistence.typeHandling.TypeHandler)2 EntityData (org.terasology.protobuf.EntityData)2 Color (org.terasology.rendering.nui.Color)2 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 Component (org.terasology.entitySystem.Component)1 EntityRef (org.terasology.entitySystem.entity.EntityRef)1 Vector3i (org.terasology.math.geom.Vector3i)1 NetworkComponent (org.terasology.network.NetworkComponent)1