From 7a43d1aaaae20857113dff5c881f3545d50c5d65 Mon Sep 17 00:00:00 2001 From: aromaa Date: Thu, 14 Nov 2024 19:08:07 +0200 Subject: [PATCH] Implement DataTranslatable TypeSerializer --- .../DataTranslatableTypeSerializer.java | 73 +++++++++++++++++++ .../common/config/PluginConfigManager.java | 3 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/spongepowered/common/config/DataTranslatableTypeSerializer.java diff --git a/src/main/java/org/spongepowered/common/config/DataTranslatableTypeSerializer.java b/src/main/java/org/spongepowered/common/config/DataTranslatableTypeSerializer.java new file mode 100644 index 00000000000..9ff9379395f --- /dev/null +++ b/src/main/java/org/spongepowered/common/config/DataTranslatableTypeSerializer.java @@ -0,0 +1,73 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.common.config; + +import io.leangen.geantyref.GenericTypeReflector; +import org.spongepowered.api.data.DataManager; +import org.spongepowered.api.data.persistence.DataTranslator; +import org.spongepowered.common.data.persistence.ConfigurateTranslator; +import org.spongepowered.configurate.ConfigurationNode; +import org.spongepowered.configurate.serialize.SerializationException; +import org.spongepowered.configurate.serialize.TypeSerializer; + +import java.lang.reflect.Type; + +import javax.inject.Inject; + +public final class DataTranslatableTypeSerializer implements TypeSerializer { + + private final DataManager dataManager; + + @Inject + DataTranslatableTypeSerializer(final DataManager dataManager) { + this.dataManager = dataManager; + } + + public boolean accepts(final Type x) { + return this.dataManager.translator(GenericTypeReflector.erase(x)).isPresent(); + } + + @SuppressWarnings("unchecked") + @Override + public Object deserialize(final Type type, final ConfigurationNode value) throws SerializationException { + final Class clazz = (Class) GenericTypeReflector.erase(type); + final DataTranslator translator = this.dataManager.translator(clazz) + .orElseThrow(() -> new SerializationException("Could not translate of DataTranslatable type: " + clazz.getName())); + return translator.translate(ConfigurateTranslator.instance().translate(value)); + } + + @SuppressWarnings("unchecked") + @Override + public void serialize(final Type type, final Object obj, final ConfigurationNode value) throws SerializationException { + if (obj == null) { + value.raw(null); + } else { + final Class clazz = (Class) GenericTypeReflector.erase(type); + final DataTranslator translator = this.dataManager.translator(clazz) + .orElseThrow(() -> new SerializationException("Could not translate of DataTranslatable type: " + clazz.getName())); + ConfigurateTranslator.instance().translateDataToNode(value, translator.translate(obj)); + } + } +} diff --git a/src/main/java/org/spongepowered/common/config/PluginConfigManager.java b/src/main/java/org/spongepowered/common/config/PluginConfigManager.java index 60389388981..3fb5af7a96c 100644 --- a/src/main/java/org/spongepowered/common/config/PluginConfigManager.java +++ b/src/main/java/org/spongepowered/common/config/PluginConfigManager.java @@ -49,7 +49,7 @@ public final class PluginConfigManager implements ConfigManager { private final WatchServiceListener listener; @Inject - PluginConfigManager(final DataSerializableTypeSerializer dataSerializableSerializer) throws IOException { + PluginConfigManager(final DataSerializableTypeSerializer dataSerializableSerializer, final DataTranslatableTypeSerializer dataTranslatableSerializer) throws IOException { // TODO: Move this onto the async scheduler, rather than shared FJ pool? this.listener = WatchServiceListener.builder() .threadFactory(new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Sponge-WatchService-%d").build()) @@ -60,6 +60,7 @@ public final class PluginConfigManager implements ConfigManager { // See https://github.com/SpongePowered/SpongeCommon/issues/1348 .register(DataSerializableTypeSerializer::accepts, dataSerializableSerializer) .registerAll(SpongeAdventure.CONFIGURATE.serializers()) + .register(dataTranslatableSerializer::accepts, dataTranslatableSerializer) .build(); }