Search in sources :

Example 1 with DataTypeSerializerContext

use of org.lanternpowered.server.data.persistence.DataTypeSerializerContext in project LanternServer by LanternPowered.

the class CompositeValueContainerStore method deserialize.

@SuppressWarnings("unchecked")
@Override
public void deserialize(T object, DataView dataView) {
    if (object instanceof IValueContainer) {
        final IValueContainer<S> valueContainer = (IValueContainer<S>) object;
        final SimpleValueContainer simpleValueContainer = new SimpleValueContainer(new HashMap<>());
        deserializeValues(object, simpleValueContainer, dataView);
        final Optional<DataView> optDataView = dataView.getView(VALUES);
        if (optDataView.isPresent()) {
            final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
            final DataView valuesView = optDataView.get();
            for (Map.Entry<DataQuery, Object> entry : valuesView.getValues(false).entrySet()) {
                final Key key = KeyRegistryModule.get().getById(entry.getKey().toString()).orElse(null);
                if (key == null) {
                    Lantern.getLogger().warn("Unable to deserialize the data value with key: {} because it doesn't exist.", entry.getKey().toString());
                } else {
                    final TypeToken<?> typeToken = key.getElementToken();
                    final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
                    if (dataTypeSerializer == null) {
                        Lantern.getLogger().warn("Unable to deserialize the data key value: {}, " + "no supported deserializer exists.", key.getId());
                    } else {
                        if (simpleValueContainer.get(key).isPresent()) {
                            Lantern.getLogger().warn("Duplicate usage of the key: {}", key.getId());
                        } else {
                            simpleValueContainer.set(key, dataTypeSerializer.deserialize(typeToken, context, entry.getValue()));
                        }
                    }
                }
            }
        }
        for (Map.Entry<Key<?>, Object> entry : simpleValueContainer.getValues().entrySet()) {
            final Element element = (Element) valueContainer.getValueCollection().getElement((Key) entry.getKey()).orElse(null);
            if (element != null) {
                element.set(entry.getValue());
            } else {
                Lantern.getLogger().warn("Attempted to offer a unsupported value with key \"{}\" to the object {}", entry.getKey().toString(), object.toString());
            }
        }
        dataView.getView(DataQueries.SPONGE_DATA).ifPresent(view -> DataHelper.deserializeRawContainerData(dataView, valueContainer, DataQueries.CUSTOM_MANIPULATORS));
    } else {
    // Not sure what to do
    }
}
Also used : IValueContainer(org.lanternpowered.server.data.IValueContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Example 2 with DataTypeSerializerContext

use of org.lanternpowered.server.data.persistence.DataTypeSerializerContext in project LanternServer by LanternPowered.

the class CompositeValueContainerStore method serialize.

@SuppressWarnings("unchecked")
@Override
public void serialize(T object, DataView dataView) {
    if (object instanceof IValueContainer) {
        final IValueContainer<S> valueContainer = (IValueContainer<S>) object;
        final SimpleValueContainer simpleValueContainer = new SimpleValueContainer(new HashMap<>());
        for (KeyRegistration<?, ?> registration : valueContainer.getValueCollection().getAll()) {
            if (!(registration instanceof Element)) {
                continue;
            }
            final Key key = registration.getKey();
            final Object element = ((Element) registration).get();
            if (element != null) {
                simpleValueContainer.set(key, element);
            }
        }
        // Serialize the values, all written values will be removed from
        // the simple value container
        serializeValues(object, simpleValueContainer, dataView);
        // Write the rest to the Values tag
        final Map<Key<?>, Object> values = simpleValueContainer.getValues();
        if (!values.isEmpty()) {
            final DataView valuesView = dataView.createView(VALUES);
            final DataTypeSerializerContext context = Lantern.getGame().getDataManager().getTypeSerializerContext();
            for (Map.Entry<Key<?>, Object> entry : values.entrySet()) {
                final TypeToken<?> typeToken = entry.getKey().getElementToken();
                final DataTypeSerializer dataTypeSerializer = Lantern.getGame().getDataManager().getTypeSerializer(typeToken).orElse(null);
                if (dataTypeSerializer == null) {
                    Lantern.getLogger().warn("Unable to serialize the data key value: " + entry.getKey());
                } else {
                    valuesView.set(DataQuery.of(entry.getKey().getId()), dataTypeSerializer.serialize(typeToken, context, entry.getValue()));
                }
            }
            if (valuesView.isEmpty()) {
                dataView.remove(VALUES);
            }
        }
        DataHelper.serializeRawContainerData(getOrCreateView(dataView, DataQueries.SPONGE_DATA), valueContainer, DataQueries.CUSTOM_MANIPULATORS);
    } else {
    // Not sure what to do
    }
}
Also used : IValueContainer(org.lanternpowered.server.data.IValueContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) SimpleValueContainer(org.lanternpowered.server.data.io.store.SimpleValueContainer) DataView(org.spongepowered.api.data.DataView) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) HashMap(java.util.HashMap) Map(java.util.Map) Key(org.spongepowered.api.data.key.Key)

Example 3 with DataTypeSerializerContext

use of org.lanternpowered.server.data.persistence.DataTypeSerializerContext in project LanternServer by LanternPowered.

the class DataHelper method serializeRawRegisteredKeyData.

public static void serializeRawRegisteredKeyData(DataView dataView, IValueContainer valueContainer, Set<Key> ignoredKeys) {
    DataView view = null;
    final ValueCollection valueCollection = valueContainer.getValueCollection();
    final LanternDataManager dataManager = Lantern.getGame().getDataManager();
    final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
    for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
        final Key<?> key = registration.getKey();
        if (!(registration instanceof Element) || ignoredKeys.contains(key) || key == LanternKeys.FAILED_DATA_MANIPULATORS || key == LanternKeys.FAILED_DATA_VALUES) {
            continue;
        }
        final Element element = (Element) registration;
        final TypeToken<?> typeToken = key.getElementToken();
        final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
        final Object object = element.get();
        if (object == null) {
            continue;
        }
        final Object value = typeSerializer.serialize(typeToken, context, object);
        if (view == null) {
            view = dataView.createView(DataQueries.DATA_VALUES);
        }
        view.set(key.getQuery(), value);
    }
    Element<DataView> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_VALUES).orElse(null);
    if (holder != null) {
        if (view == null) {
            view = dataView.createView(DataQueries.DATA_VALUES);
        }
        for (Map.Entry<DataQuery, Object> entry : holder.get().getValues(false).entrySet()) {
            if (!view.contains(entry.getKey())) {
                view.set(entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) Map(java.util.Map)

Example 4 with DataTypeSerializerContext

use of org.lanternpowered.server.data.persistence.DataTypeSerializerContext in project LanternServer by LanternPowered.

the class DataHelper method deserializeRawRegisteredKeyData.

public static void deserializeRawRegisteredKeyData(DataView dataView, IValueContainer valueContainer) throws InvalidDataException {
    dataView = dataView.getView(DataQueries.DATA_VALUES).orElse(null);
    if (dataView == null) {
        return;
    }
    final ValueCollection valueCollection = valueContainer.getValueCollection();
    final LanternDataManager dataManager = Lantern.getGame().getDataManager();
    final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
    for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
        final Key<?> key = registration.getKey();
        if (!(registration instanceof Element) || key == LanternKeys.FAILED_DATA_MANIPULATORS || key == LanternKeys.FAILED_DATA_VALUES) {
            continue;
        }
        final Optional<Object> data = dataView.get(key.getQuery());
        if (!data.isPresent()) {
            continue;
        }
        dataView.remove(key.getQuery());
        final TypeToken<?> typeToken = key.getElementToken();
        final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
        ((Element) registration).set(typeSerializer.deserialize(typeToken, context, data.get()));
    }
    if (valueContainer instanceof CompositeValueStore) {
        final CompositeValueStore store = (CompositeValueStore) valueContainer;
        for (Map.Entry<DataQuery, Object> entry : dataView.getValues(false).entrySet()) {
            final Key<?> key = KeyRegistryModule.get().getByQuery(entry.getKey()).orElse(null);
            if (key == null) {
                continue;
            }
            final TypeToken<?> typeToken = key.getElementToken();
            final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
            store.offer(key, typeSerializer.deserialize(typeToken, context, entry.getValue()));
            dataView.remove(entry.getKey());
        }
    }
    if (!dataView.isEmpty()) {
        // Should be safe to cast, at least if nobody touches this key
        Element<DataView> holder = valueCollection.getElement(LanternKeys.FAILED_DATA_VALUES).orElse(null);
        if (holder == null) {
            holder = valueCollection.register(LanternKeys.FAILED_DATA_VALUES, null);
        }
        holder.set(dataView);
    }
}
Also used : DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) DataView(org.spongepowered.api.data.DataView) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer) CompositeValueStore(org.spongepowered.api.data.value.mutable.CompositeValueStore) Map(java.util.Map)

Example 5 with DataTypeSerializerContext

use of org.lanternpowered.server.data.persistence.DataTypeSerializerContext in project LanternServer by LanternPowered.

the class DataHelper method toContainer.

public static DataContainer toContainer(IValueContainer valueContainer) {
    final DataContainer dataContainer = DataContainer.createNew();
    final ValueCollection valueCollection = valueContainer.getValueCollection();
    final LanternDataManager dataManager = Lantern.getGame().getDataManager();
    for (KeyRegistration<?, ?> registration : valueCollection.getAll()) {
        if (!(registration instanceof Element)) {
            continue;
        }
        final Key<?> key = registration.getKey();
        final DataQuery dataQuery = key.getQuery();
        final TypeToken<?> typeToken = key.getElementToken();
        final DataTypeSerializer typeSerializer = dataManager.getTypeSerializer(typeToken).orElseThrow(() -> new IllegalStateException("Wasn't able to find a type serializer for the element type: " + typeToken.toString()));
        final DataTypeSerializerContext context = dataManager.getTypeSerializerContext();
        // The value's shouldn't be null inside a data manipulator,
        // since it doesn't support removal of values
        dataContainer.set(dataQuery, typeSerializer.serialize(typeToken, context, checkNotNull(((Element) registration).get(), "element")));
    }
    return dataContainer;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) DataTypeSerializerContext(org.lanternpowered.server.data.persistence.DataTypeSerializerContext) Element(org.lanternpowered.server.data.element.Element) DataQuery(org.spongepowered.api.data.DataQuery) DataTypeSerializer(org.lanternpowered.server.data.persistence.DataTypeSerializer)

Aggregations

DataTypeSerializer (org.lanternpowered.server.data.persistence.DataTypeSerializer)6 DataTypeSerializerContext (org.lanternpowered.server.data.persistence.DataTypeSerializerContext)6 Element (org.lanternpowered.server.data.element.Element)5 Map (java.util.Map)4 DataQuery (org.spongepowered.api.data.DataQuery)4 DataView (org.spongepowered.api.data.DataView)4 Key (org.spongepowered.api.data.key.Key)3 HashMap (java.util.HashMap)2 IValueContainer (org.lanternpowered.server.data.IValueContainer)2 SimpleValueContainer (org.lanternpowered.server.data.io.store.SimpleValueContainer)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CommandSource (org.spongepowered.api.command.CommandSource)1 CommandArgs (org.spongepowered.api.command.args.CommandArgs)1 CommandContext (org.spongepowered.api.command.args.CommandContext)1 CommandElement (org.spongepowered.api.command.args.CommandElement)1 PatternMatchingCommandElement (org.spongepowered.api.command.args.PatternMatchingCommandElement)1 DataContainer (org.spongepowered.api.data.DataContainer)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 CompositeValueStore (org.spongepowered.api.data.value.mutable.CompositeValueStore)1