Package org.terasology.persistence.typeHandling

Examples of org.terasology.persistence.typeHandling.Serializer


    }


    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.hasNameIndex()) {
                logger.warn("Cannot deserialize unknown field '{}' onto '{}'", field.getName(), componentMetadata.getUri());
            }
        }
        serializer.deserializeOnto(targetComponent, dataMap, context, fieldCheck);
        return targetComponent;
    }
View Full Code Here


            return null;
        }
        EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
        serializeComponentType(componentMetadata, componentMessage);

        Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
        for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
            if (check.shouldSerializeField(field, component)) {
                PersistedData result = serializer.serialize(field, component, serializationContext);
                if (!result.isNull()) {
                    EntityData.Value itemValue = ((ProtobufPersistedData) result).getValue();
                    if (usingFieldIds) {
                        componentMessage.addField(EntityData.NameValue.newBuilder().setNameIndex(field.getId()).setValue(itemValue));
                    } else {
View Full Code Here

        }

        EntityData.Component.Builder componentMessage = EntityData.Component.newBuilder();
        serializeComponentType(componentMetadata, componentMessage);

        Serializer serializer = typeSerializationLibrary.getSerializerFor(componentMetadata);
        boolean changed = false;
        for (ReplicatedFieldMetadata field : componentMetadata.getFields()) {
            if (check.shouldSerializeField(field, delta)) {
                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 {
View Full Code Here

        return null;
    }


    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;
    }
View Full Code Here

            return null;
        }
        EntityData.Event.Builder eventData = EntityData.Event.newBuilder();
        serializeEventType(event, eventData);

        Serializer eventSerializer = typeSerializationLibrary.getSerializerFor(eventMetadata);
        ByteString.Output fieldIds = ByteString.newOutput();
        for (ReplicatedFieldMetadata field : eventMetadata.getFields()) {
            if (field.isReplicated()) {
                EntityData.Value serializedValue = ((ProtobufPersistedData) eventSerializer.serialize(field, event, serializationContext)).getValue();
                if (serializedValue != null) {
                    eventData.addFieldValue(serializedValue);
                    fieldIds.write(field.getId());
                }
            }
View Full Code Here

            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 {
View Full Code Here

        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++;
            }
View Full Code Here

            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);
View Full Code Here

TOP

Related Classes of org.terasology.persistence.typeHandling.Serializer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.