Search in sources :

Example 1 with ReplicatedFieldMetadata

use of org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata 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 = typeHandlerLibrary.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)));
            }
            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.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) ReplicatedFieldMetadata(org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata) Component(org.terasology.gestalt.entitysystem.component.Component) Serializer(org.terasology.persistence.typeHandling.Serializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

Example 2 with ReplicatedFieldMetadata

use of org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata 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 = typeHandlerLibrary.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.engine.entitySystem.metadata.ReplicatedFieldMetadata) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

Example 3 with ReplicatedFieldMetadata

use of org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata 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 = typeHandlerLibrary.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.engine.entitySystem.metadata.ReplicatedFieldMetadata) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) PersistedData(org.terasology.persistence.typeHandling.PersistedData) Serializer(org.terasology.persistence.typeHandling.Serializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

Example 4 with ReplicatedFieldMetadata

use of org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata 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 5 with ReplicatedFieldMetadata

use of org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata in project Terasology by MovingBlocks.

the class EventSerializer method serialize.

/**
 * Serializes an event.
 *
 * @param event
 * @return The serialized event
 * @throws org.terasology.engine.persistence.typeHandling.SerializationException if an error occurs during serialization
 */
public EntityData.Event serialize(Event event) {
    EventMetadata<?> eventMetadata = eventLibrary.getMetadata(event.getClass());
    if (eventMetadata == null) {
        throw new SerializationException("Unregistered event type: " + event.getClass());
    } else if (!eventMetadata.isConstructable()) {
        throw new SerializationException("Cannot serialize event '" + eventMetadata + "' - lacks default constructor so cannot be deserialized");
    }
    EntityData.Event.Builder eventData = EntityData.Event.newBuilder();
    serializeEventType(event, eventData);
    Serializer eventSerializer = typeHandlerLibrary.getSerializerFor(eventMetadata);
    ByteString.Output fieldIds = ByteString.newOutput();
    for (ReplicatedFieldMetadata field : eventMetadata.getFields()) {
        if (field.isReplicated()) {
            EntityData.Value serializedValue = ((ProtobufPersistedData) eventSerializer.serialize(field, event, persistedDataSerializer)).getValue();
            if (serializedValue != null) {
                eventData.addFieldValue(serializedValue);
                fieldIds.write(field.getId());
            }
        }
    }
    eventData.setFieldIds(fieldIds.toByteString());
    return eventData.build();
}
Also used : SerializationException(org.terasology.persistence.typeHandling.SerializationException) ProtobufPersistedData(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedData) ByteString(com.google.protobuf.ByteString) EntityData(org.terasology.protobuf.EntityData) Event(org.terasology.engine.entitySystem.event.Event) ReplicatedFieldMetadata(org.terasology.engine.entitySystem.metadata.ReplicatedFieldMetadata) Serializer(org.terasology.persistence.typeHandling.Serializer) PersistedDataSerializer(org.terasology.persistence.typeHandling.PersistedDataSerializer) ProtobufPersistedDataSerializer(org.terasology.engine.persistence.typeHandling.protobuf.ProtobufPersistedDataSerializer)

Aggregations

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