Search in sources :

Example 1 with ProtobufPersistedData

use of org.terasology.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 = 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 ProtobufPersistedData

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

the class EventSerializer method deserializeOnto.

private Event deserializeOnto(Event targetEvent, EntityData.Event eventData, EventMetadata<? extends Event> eventMetadata) {
    Serializer serializer = typeSerializationLibrary.getSerializerFor(eventMetadata);
    for (int i = 0; i < eventData.getFieldIds().size(); ++i) {
        byte fieldId = eventData.getFieldIds().byteAt(i);
        ReplicatedFieldMetadata<?, ?> fieldInfo = eventMetadata.getField(fieldId);
        if (fieldInfo == null) {
            logger.error("Unable to serialize field {}, out of bounds", fieldId);
            continue;
        }
        if (fieldInfo.isReplicated()) {
            serializer.deserializeOnto(targetEvent, fieldInfo, new ProtobufPersistedData(eventData.getFieldValue(i)), deserializationContext);
        }
    }
    return targetEvent;
}
Also used : ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 3 with ProtobufPersistedData

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

the class NetworkEntitySerializer method deserializeOnto.

public void deserializeOnto(MutableComponentContainer entity, EntityData.PackedEntity entityData, FieldSerializeCheck<Component> fieldCheck) {
    int fieldPos = 0;
    for (int componentIndex = 0; componentIndex < entityData.getComponentIdCount(); ++componentIndex) {
        Integer componentId = entityData.getComponentId(componentIndex);
        Class<? extends Component> componentClass = idTable.inverse().get(componentId);
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(componentClass);
        if (metadata == null) {
            logger.warn("Skipping unknown component {}", componentId);
            fieldPos += UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex));
            continue;
        }
        if (!componentSerializeCheck.serialize(metadata)) {
            fieldPos += UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex));
            continue;
        }
        Component component = entity.getComponent(metadata.getType());
        boolean createdNewComponent = false;
        if (component == null) {
            createdNewComponent = true;
            component = metadata.newInstance();
        }
        Serializer serializer = typeSerializationLibrary.getSerializerFor(metadata);
        for (int fieldIndex = 0; fieldIndex < UnsignedBytes.toInt(entityData.getComponentFieldCounts().byteAt(componentIndex)); ++fieldIndex) {
            byte fieldId = entityData.getFieldIds().byteAt(fieldPos);
            ReplicatedFieldMetadata fieldMetadata = metadata.getField(fieldId);
            if (fieldMetadata != null && fieldCheck.shouldDeserialize(metadata, fieldMetadata)) {
                logger.trace("Deserializing field {} of component {} as value {}", fieldMetadata, metadata, entityData.getFieldValue(fieldPos));
                serializer.deserializeOnto(component, fieldMetadata, new ProtobufPersistedData(entityData.getFieldValue(fieldPos)), deserializationContext);
            }
            fieldPos++;
        }
        if (createdNewComponent) {
            entity.addComponent(component);
        } else {
            entity.saveComponent(component);
        }
    }
    for (int componentId : entityData.getRemovedComponentList()) {
        Class<? extends Component> componentClass = idTable.inverse().get(componentId);
        ComponentMetadata<?> metadata = componentLibrary.getMetadata(componentClass);
        if (componentSerializeCheck.serialize(metadata)) {
            entity.removeComponent(metadata.getType());
        }
    }
}
Also used : ProtobufPersistedData(org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData) ReplicatedFieldMetadata(org.terasology.entitySystem.metadata.ReplicatedFieldMetadata) Component(org.terasology.entitySystem.Component) Serializer(org.terasology.persistence.typeHandling.Serializer)

Example 4 with ProtobufPersistedData

use of org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData 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 5 with ProtobufPersistedData

use of org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData 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)

Aggregations

Serializer (org.terasology.persistence.typeHandling.Serializer)7 ProtobufPersistedData (org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData)7 ReplicatedFieldMetadata (org.terasology.entitySystem.metadata.ReplicatedFieldMetadata)6 PersistedData (org.terasology.persistence.typeHandling.PersistedData)4 EntityData (org.terasology.protobuf.EntityData)3 Component (org.terasology.entitySystem.Component)2 ByteString (com.google.protobuf.ByteString)1 Event (org.terasology.entitySystem.event.Event)1 DeserializationContext (org.terasology.persistence.typeHandling.DeserializationContext)1 SerializationException (org.terasology.persistence.typeHandling.SerializationException)1 ProtobufDeserializationContext (org.terasology.persistence.typeHandling.protobuf.ProtobufDeserializationContext)1 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)1