Search in sources :

Example 6 with ProtobufPersistedData

use of org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData in project Terasology by MovingBlocks.

the class ComponentSerializer method serialize.

/**
 * Serializes the differences between two components.
 *
 * @param base  The base component to compare against.
 * @param delta The component whose differences will be serialized
 * @param check A check to use to see if each field should be serialized.
 * @return The serialized component, or null if it could not be serialized
 */
public EntityData.Component serialize(Component base, Component delta, FieldSerializeCheck<Component> check) {
    ComponentMetadata<?> componentMetadata = componentLibrary.getMetadata(base.getClass());
    if (componentMetadata == null) {
        logger.error("Unregistered component type: {}", base.getClass());
        return null;
    }
    EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
    serializeComponentType(componentMetadata, componentMessage);
    Serializer serializer = typeHandlerLibrary.getSerializerFor(componentMetadata);
    boolean changed = false;
    for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
        if (check.shouldSerializeField(field, delta) && serializer.getHandlerFor(field) != null) {
            Object origValue = field.getValue(base);
            Object deltaValue = field.getValue(delta);
            if (!Objects.equal(origValue, deltaValue)) {
                PersistedData value = serializer.serializeValue(field, deltaValue, serializationContext);
                if (!value.isNull()) {
                    EntityData.Value dataValue = ((ProtobufPersistedData) value).getValue();
                    if (usingFieldIds) {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setNameIndex(field.getId()).setValue(dataValue).build());
                    } else {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setName(field.getName()).setValue(dataValue).build());
                    }
                    changed = true;
                }
            }
        }
    }
    if (changed) {
        return componentMessage.build();
    }
    return null;
}
Also used : ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) ReplicatedFieldMetadata(org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) Component(org.terasology.gestalt.entitysystem.component.Component) Serializer(org.terasology.persistence.typeHandling.Serializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

Example 7 with ProtobufPersistedData

use of org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData 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 = typeHandlerLibrary.getSerializerFor(componentMetadata);
    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.getId());
        }
    }
    serializer.deserializeOnto(targetComponent, dataMap, fieldCheck);
    return targetComponent;
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) ReplicatedFieldMetadata(org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) EntityData(org.terasology.protobuf.EntityData) PersistedData(org.terasology.persistence.typeHandling.PersistedData) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

Aggregations

ProtobufPersistedData (org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData)7 ProtobufPersistedDataSerializer (org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)7 Serializer (org.terasology.persistence.typeHandling.Serializer)7 ReplicatedFieldMetadata (org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata)6 PersistedData (org.terasology.persistence.typeHandling.PersistedData)4 EntityData (org.terasology.protobuf.EntityData)3 Component (org.terasology.gestalt.entitysystem.component.Component)2 PersistedDataSerializer (org.terasology.persistence.typeHandling.PersistedDataSerializer)2 ByteString (com.google.protobuf.ByteString)1 Event (org.terasology.engine.entitySystem.event.Event)1 SerializationException (org.terasology.persistence.typeHandling.SerializationException)1 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)1