use of org.terasology.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 = typeSerializationLibrary.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;
}
use of org.terasology.persistence.typeHandling.protobuf.ProtobufPersistedData in project Terasology by MovingBlocks.
the class EventSerializer method serialize.
/**
* Serializes an event.
*
* @param event
* @return The serialized event
* @throws org.terasology.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 = 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());
}
}
}
eventData.setFieldIds(fieldIds.toByteString());
return eventData.build();
}
Aggregations