diff options
17 files changed, 83 insertions, 1254 deletions
diff --git a/api/OneConfig.api b/api/OneConfig.api index 48f4bc2..f99cbdc 100644 --- a/api/OneConfig.api +++ b/api/OneConfig.api @@ -1,6 +1,5 @@ public class cc/polyfrost/oneconfig/config/Config { protected final field configFile Ljava/lang/String; - protected final field defaults Ljava/util/HashMap; public field enabled Z protected final field gson Lcom/google/gson/Gson; public field mod Lcc/polyfrost/oneconfig/config/data/Mod; @@ -8,23 +7,21 @@ public class cc/polyfrost/oneconfig/config/Config { public final field optionNames Ljava/util/HashMap; public fun <init> (Lcc/polyfrost/oneconfig/config/data/Mod;Ljava/lang/String;)V public fun <init> (Lcc/polyfrost/oneconfig/config/data/Mod;Ljava/lang/String;Z)V - protected fun addDependency (Ljava/lang/String;Ljava/lang/String;)V - protected fun addDependency (Ljava/lang/String;Ljava/util/function/Supplier;)V - protected fun addDependency (Ljava/lang/String;Z)V - protected fun addListener (Ljava/lang/String;Ljava/lang/Runnable;)V - protected fun deserializePart (Lcom/google/gson/JsonObject;Ljava/lang/Object;)V - protected fun generateOptionList (Ljava/lang/Object;Lcc/polyfrost/oneconfig/config/elements/OptionPage;Lcc/polyfrost/oneconfig/config/data/Mod;Z)V + protected final fun addDependency (Ljava/lang/String;Ljava/lang/String;)V + protected final fun addDependency (Ljava/lang/String;Ljava/util/function/Supplier;)V + protected final fun addDependency (Ljava/lang/String;Z)V + protected fun addGsonOptions (Lcom/google/gson/GsonBuilder;)Lcom/google/gson/GsonBuilder; + protected final fun addListener (Ljava/lang/String;Ljava/lang/Runnable;)V + protected final fun generateOptionList (Ljava/lang/Object;Lcc/polyfrost/oneconfig/config/elements/OptionPage;Lcc/polyfrost/oneconfig/config/data/Mod;Z)V protected fun getCustomOption (Ljava/lang/reflect/Field;Lcc/polyfrost/oneconfig/config/annotations/CustomOption;Lcc/polyfrost/oneconfig/config/elements/OptionPage;Lcc/polyfrost/oneconfig/config/data/Mod;Z)Lcc/polyfrost/oneconfig/config/elements/BasicOption; - public fun getDefault (Ljava/lang/reflect/Field;)Ljava/lang/Object; - protected fun hideIf (Ljava/lang/String;Ljava/lang/String;)V - protected fun hideIf (Ljava/lang/String;Ljava/util/function/Supplier;)V - protected fun hideIf (Ljava/lang/String;Z)V + protected final fun hideIf (Ljava/lang/String;Ljava/lang/String;)V + protected final fun hideIf (Ljava/lang/String;Ljava/util/function/Supplier;)V + protected final fun hideIf (Ljava/lang/String;Z)V public fun initialize ()V public fun load ()V public fun openGui ()V public fun reInitialize ()V - protected fun registerKeyBind (Lcc/polyfrost/oneconfig/config/core/OneKeyBind;Ljava/lang/Runnable;)V - public fun reset ()V + protected final fun registerKeyBind (Lcc/polyfrost/oneconfig/config/core/OneKeyBind;Ljava/lang/Runnable;)V public fun save ()V public fun supportsProfiles ()Z } @@ -302,7 +299,6 @@ public abstract class cc/polyfrost/oneconfig/config/elements/BasicOption { public fun isEnabled ()Z public fun isHidden ()Z public fun keyTyped (CI)V - public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V protected fun set (Ljava/lang/Object;)V public fun setParent (Ljava/lang/Object;)V } @@ -310,7 +306,6 @@ public abstract class cc/polyfrost/oneconfig/config/elements/BasicOption { public class cc/polyfrost/oneconfig/config/elements/OptionCategory { public final field subcategories Ljava/util/ArrayList; public fun <init> ()V - public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V } public class cc/polyfrost/oneconfig/config/elements/OptionPage { @@ -318,7 +313,6 @@ public class cc/polyfrost/oneconfig/config/elements/OptionPage { public final field mod Lcc/polyfrost/oneconfig/config/data/Mod; public final field name Ljava/lang/String; public fun <init> (Ljava/lang/String;Lcc/polyfrost/oneconfig/config/data/Mod;)V - public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V } public class cc/polyfrost/oneconfig/config/elements/OptionSubcategory { @@ -329,7 +323,11 @@ public class cc/polyfrost/oneconfig/config/elements/OptionSubcategory { public fun draw (JIILcc/polyfrost/oneconfig/utils/InputHandler;)I public fun drawLast (JILcc/polyfrost/oneconfig/utils/InputHandler;)V public fun getName ()Ljava/lang/String; - public fun reset (Lcc/polyfrost/oneconfig/config/Config;)V +} + +public class cc/polyfrost/oneconfig/config/gson/InstanceSupplier : com/google/gson/InstanceCreator { + public fun <init> (Ljava/lang/Object;)V + public fun createInstance (Ljava/lang/reflect/Type;)Ljava/lang/Object; } public class cc/polyfrost/oneconfig/config/gson/exclusion/ExclusionUtils { @@ -349,56 +347,6 @@ public class cc/polyfrost/oneconfig/config/gson/exclusion/ProfileExclusionStrate public fun shouldSkipField (Lcom/google/gson/FieldAttributes;)Z } -public class cc/polyfrost/oneconfig/config/gson/gsoninterface/GsonContext { - public fun <init> (Lcom/google/gson/Gson;Lcc/polyfrost/oneconfig/config/gson/gsoninterface/InterfaceAdapterFactory$InterfaceTypeAdapter;)V - public fun fromJsonTree (Lcom/google/gson/JsonElement;Ljava/lang/Class;)Ljava/lang/Object; - public fun fromJsonTree (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;)Ljava/lang/Object; - public fun thisFromJsonTree (Lcom/google/gson/JsonElement;)Ljava/lang/Object; - public fun thisFromJsonTree (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;)Ljava/lang/Object; - public fun thisToJsonTree (Ljava/lang/Object;)Lcom/google/gson/JsonElement; - public fun toJsonTree (Ljava/lang/Object;)Lcom/google/gson/JsonElement; -} - -public class cc/polyfrost/oneconfig/config/gson/gsoninterface/InterfaceAdapterFactory : com/google/gson/TypeAdapterFactory { - public fun <init> ()V - public fun create (Lcom/google/gson/Gson;Lcom/google/gson/reflect/TypeToken;)Lcom/google/gson/TypeAdapter; -} - -public class cc/polyfrost/oneconfig/config/gson/gsoninterface/InterfaceAdapterFactory$InterfaceTypeAdapter : com/google/gson/TypeAdapter { - public fun read (Lcom/google/gson/stream/JsonReader;)Ljava/lang/Object; - public fun write (Lcom/google/gson/stream/JsonWriter;Ljava/lang/Object;)V -} - -public abstract interface class cc/polyfrost/oneconfig/config/gson/gsoninterface/JsonDeserialization { -} - -public abstract interface class cc/polyfrost/oneconfig/config/gson/gsoninterface/JsonDeserializes { - public abstract fun fromJsonTree (Lcom/google/gson/JsonElement;Ljava/lang/reflect/Type;Lcc/polyfrost/oneconfig/config/gson/gsoninterface/GsonContext;)Ljava/lang/Object; -} - -public abstract interface class cc/polyfrost/oneconfig/config/gson/gsoninterface/JsonSerialization { - public abstract fun toJsonTree (Lcc/polyfrost/oneconfig/config/gson/gsoninterface/GsonContext;)Lcom/google/gson/JsonElement; -} - -public class cc/polyfrost/oneconfig/config/gson/gsoninterface/Reflection { - public fun <init> ()V - public static fun classOfType (Ljava/lang/reflect/Type;)Ljava/lang/Class; - public static fun constructAnyway (Ljava/lang/reflect/Constructor;[Ljava/lang/Object;)Ljava/lang/Object; - public static fun getAccessibleField (Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field; - public static fun getConstructor (Ljava/lang/Class;[Ljava/lang/Class;)Ljava/lang/reflect/Constructor; - public static fun getFieldValue (Ljava/lang/reflect/Field;Ljava/lang/Object;)Ljava/lang/Object; - public static fun getParameterBounds (Ljava/lang/Class;)[Ljava/lang/Class; - public static fun getTypeParameters (Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class; - public static fun isAbstract (Ljava/lang/Class;)Z - public static fun newInstance (Ljava/lang/Class;)Ljava/lang/Object; -} - -public class cc/polyfrost/oneconfig/config/gson/gsoninterface/Reflection$Stack : java/util/ArrayList { - public fun <init> ()V - public fun pop ()Ljava/lang/Object; - public fun push (Ljava/lang/Object;)Lcc/polyfrost/oneconfig/config/gson/gsoninterface/Reflection$Stack; -} - public class cc/polyfrost/oneconfig/config/migration/CfgMigrator : cc/polyfrost/oneconfig/config/migration/Migrator { public fun <init> (Ljava/lang/String;)V protected fun generateValues ()V diff --git a/src/main/java/cc/polyfrost/oneconfig/config/Config.java b/src/main/java/cc/polyfrost/oneconfig/config/Config.java index 87ade90..fe7d7f7 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/Config.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/Config.java @@ -34,9 +34,9 @@ import cc.polyfrost.oneconfig.config.data.PageLocation; import cc.polyfrost.oneconfig.config.elements.BasicOption; import cc.polyfrost.oneconfig.config.elements.OptionPage; import cc.polyfrost.oneconfig.config.elements.OptionSubcategory; +import cc.polyfrost.oneconfig.config.gson.InstanceSupplier; import cc.polyfrost.oneconfig.config.gson.exclusion.NonProfileSpecificExclusionStrategy; import cc.polyfrost.oneconfig.config.gson.exclusion.ProfileExclusionStrategy; -import cc.polyfrost.oneconfig.config.gson.gsoninterface.InterfaceAdapterFactory; import cc.polyfrost.oneconfig.gui.elements.config.ConfigKeyBind; import cc.polyfrost.oneconfig.gui.OneConfigGui; import cc.polyfrost.oneconfig.gui.elements.config.ConfigPageButton; @@ -45,14 +45,10 @@ import cc.polyfrost.oneconfig.hud.HUDUtils; import cc.polyfrost.oneconfig.internal.config.annotations.Option; import cc.polyfrost.oneconfig.internal.config.core.ConfigCore; import cc.polyfrost.oneconfig.internal.config.core.KeyBindHandler; -import cc.polyfrost.oneconfig.utils.JsonUtils; import cc.polyfrost.oneconfig.utils.gui.GuiUtils; import com.google.gson.*; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; +import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.nio.charset.StandardCharsets; @@ -64,19 +60,12 @@ import java.util.function.Supplier; public class Config { public final transient HashMap<String, BasicOption> optionNames = new HashMap<>(); transient protected final String configFile; - transient protected final Gson gson = new GsonBuilder() - .setExclusionStrategies(new ProfileExclusionStrategy()) - .registerTypeAdapterFactory(new InterfaceAdapterFactory()) - .excludeFieldsWithModifiers(Modifier.TRANSIENT) - .setPrettyPrinting() + transient protected final Gson gson = addGsonOptions(new GsonBuilder() + .setExclusionStrategies(new ProfileExclusionStrategy())) .create(); - transient protected final Gson nonProfileSpecificGson = new GsonBuilder() - .setExclusionStrategies(new NonProfileSpecificExclusionStrategy()) - .registerTypeAdapterFactory(new InterfaceAdapterFactory()) - .excludeFieldsWithModifiers(Modifier.TRANSIENT) - .setPrettyPrinting() + transient protected final Gson nonProfileSpecificGson = addGsonOptions(new GsonBuilder() + .setExclusionStrategies(new NonProfileSpecificExclusionStrategy())) .create(); - transient protected final HashMap<Field, Object> defaults = new HashMap<>(); transient public Mod mod; public boolean enabled; @@ -138,12 +127,12 @@ public class Config { */ public void load() { try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(ConfigUtils.getProfileFile(configFile).toPath()), StandardCharsets.UTF_8))) { - deserializePart(JsonUtils.PARSER.parse(reader).getAsJsonObject(), this); + gson.fromJson(reader, this.getClass()); } catch (Exception e) { e.printStackTrace(); } try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(ConfigUtils.getNonProfileSpecificFile(configFile).toPath()), StandardCharsets.UTF_8))) { - deserializePart(JsonUtils.PARSER.parse(reader).getAsJsonObject(), this); + nonProfileSpecificGson.fromJson(reader, this.getClass()); } catch (Exception e) { e.printStackTrace(); } @@ -157,7 +146,7 @@ public class Config { * @param mod data about the mod * @param migrate whether the migrator should be run */ - protected void generateOptionList(Object instance, OptionPage page, Mod mod, boolean migrate) { + protected final void generateOptionList(Object instance, OptionPage page, Mod mod, boolean migrate) { String pagePath = page.equals(mod.defaultPage) ? "" : page.name + "."; for (Field field : instance.getClass().getDeclaredFields()) { Option option = ConfigUtils.findAnnotation(field, Option.class); @@ -214,45 +203,11 @@ public class Config { return null; } - /** - * Deserialize part of config and load values - * - * @param json json to deserialize - * @param instance instance of target class - */ - protected void deserializePart(JsonObject json, Object instance) { - Class<?> clazz = instance.getClass(); - ArrayList<Field> fields = ConfigUtils.getClassFields(clazz); - for (Map.Entry<String, JsonElement> element : json.entrySet()) { - String name = element.getKey(); - JsonElement value = element.getValue(); - if (value.isJsonObject()) { - Optional<Class<?>> innerClass = Arrays.stream(clazz.getClasses()).filter(aClass -> aClass.getSimpleName().equals(name)).findFirst(); - if (innerClass.isPresent()) { - deserializePart(value.getAsJsonObject(), innerClass.get()); - continue; - } - } - try { - Field field = null; - for (Field f : fields) { - if (f.getName().equals(name)) { - field = f; - break; - } - } - if (field != null) { - TypeAdapter<?> adapter = gson.getAdapter(field.getType()); - Object object = adapter.fromJsonTree(value); - field.setAccessible(true); - field.set(instance, object); - } else { - System.out.println("Could not deserialize " + name + " in class " + clazz.getSimpleName()); - } - } catch (Exception ignored) { - System.out.println("Could not deserialize " + name + " in class " + clazz.getSimpleName()); - } - } + protected GsonBuilder addGsonOptions(GsonBuilder builder) { + return builder + .registerTypeAdapter(this.getClass(), new InstanceSupplier<>(this)) + .excludeFieldsWithModifiers(Modifier.TRANSIENT) + .setPrettyPrinting(); } /** @@ -269,7 +224,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param condition The condition that has to be met for the option to be enabled */ - protected void addDependency(String option, Supplier<Boolean> condition) { + protected final void addDependency(String option, Supplier<Boolean> condition) { if (!optionNames.containsKey(option)) return; optionNames.get(option).addDependency(condition); } @@ -280,7 +235,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param dependentOption The option that has to be enabled */ - protected void addDependency(String option, String dependentOption) { + protected final void addDependency(String option, String dependentOption) { if (!optionNames.containsKey(option) || !optionNames.containsKey(dependentOption)) return; optionNames.get(option).addDependency(() -> { try { @@ -297,7 +252,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param value The value of the dependency */ - protected void addDependency(String option, boolean value) { + protected final void addDependency(String option, boolean value) { if (!optionNames.containsKey(option)) return; optionNames.get(option).addDependency(() -> value); } @@ -308,7 +263,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param condition The condition that has to be met for the option to be hidden */ - protected void hideIf(String option, Supplier<Boolean> condition) { + protected final void hideIf(String option, Supplier<Boolean> condition) { if (!optionNames.containsKey(option)) return; optionNames.get(option).addHideCondition(condition); } @@ -319,7 +274,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param dependentOption The option that has to be hidden */ - protected void hideIf(String option, String dependentOption) { + protected final void hideIf(String option, String dependentOption) { if (!optionNames.containsKey(option) || !optionNames.containsKey(dependentOption)) return; optionNames.get(option).addHideCondition(() -> { try { @@ -336,7 +291,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param value The value of the condition */ - protected void hideIf(String option, boolean value) { + protected final void hideIf(String option, boolean value) { if (!optionNames.containsKey(option)) return; optionNames.get(option).addHideCondition(() -> value); } @@ -347,7 +302,7 @@ public class Config { * @param option The name of the field, or if the field is in a page "pageName.fieldName" * @param runnable What should be executed after the option is changed */ - protected void addListener(String option, Runnable runnable) { + protected final void addListener(String option, Runnable runnable) { if (!optionNames.containsKey(option)) return; optionNames.get(option).addListener(runnable); } @@ -358,7 +313,7 @@ public class Config { * @param keyBind The keybind * @param runnable The code to be executed */ - protected void registerKeyBind(OneKeyBind keyBind, Runnable runnable) { + protected final void registerKeyBind(OneKeyBind keyBind, Runnable runnable) { Field field = null; Object instance = null; for (BasicOption option : optionNames.values()) { @@ -379,23 +334,6 @@ public class Config { } /** - * @param field The field to get the default value from - * @return The default value of the given field - */ - public Object getDefault(Field field) { - return defaults.get(field); - } - - /** - * Reset this config file to its defaults. - */ - public void reset() { - for (BasicOption option : optionNames.values()) { - option.reset(this); - } - } - - /** * @return If this mod supports profiles, false for compatibility mode */ public boolean supportsProfiles() { diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java index 82d8b31..3394c52 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/BasicOption.java @@ -102,7 +102,7 @@ public abstract class BasicOption { * @param x x position * @param y y position */ - public void drawLast(long vg, int x, int y , InputHandler inputHandler) { + public void drawLast(long vg, int x, int y, InputHandler inputHandler) { } /** @@ -115,20 +115,6 @@ public abstract class BasicOption { } /** - * Reset the field to its default value - * - * @param config The config the field is in - */ - public void reset(Config config) { - Object object = config.getDefault(field); - if (object == null) return; - try { - set(object); - } catch (IllegalAccessException ignored) { - } - } - - /** * @return If the option is enabled, based on the dependencies */ public boolean isEnabled() { diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java index d3da143..4daefca 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionCategory.java @@ -32,10 +32,4 @@ import java.util.ArrayList; public class OptionCategory { public final ArrayList<OptionSubcategory> subcategories = new ArrayList<>(); - - public void reset(Config config) { - for (OptionSubcategory subcategory : subcategories) { - subcategory.reset(config); - } - } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java index 9ac7c14..7a1dfb0 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionPage.java @@ -40,10 +40,4 @@ public class OptionPage { this.name = name; this.mod = mod; } - - public void reset(Config config) { - for (OptionCategory subcategory : categories.values()) { - subcategory.reset(config); - } - } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java index 282f30f..08c59b8 100644 --- a/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java +++ b/src/main/java/cc/polyfrost/oneconfig/config/elements/OptionSubcategory.java @@ -132,10 +132,4 @@ public class OptionSubcategory { public String getName() { return name; } - - public void reset(Config config) { - for (BasicOption option : options) { - options.remove(config); - } - } } diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/InstanceSupplier.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/InstanceSupplier.java new file mode 100644 index 0000000..f20b527 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/gson/InstanceSupplier.java @@ -0,0 +1,44 @@ +/* + * This file is part of OneConfig. + * OneConfig - Next Generation Config Library for Minecraft: Java Edition + * Copyright (C) 2021, 2022 Polyfrost. + * <https://polyfrost.cc> <https://github.com/Polyfrost/> + * + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * OneConfig is licensed under the terms of version 3 of the GNU Lesser + * General Public License as published by the Free Software Foundation, AND + * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, + * either version 1.0 of the Additional Terms, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License. If not, see <https://www.gnu.org/licenses/>. You should + * have also received a copy of the Additional Terms Applicable + * to OneConfig, as published by Polyfrost. If not, see + * <https://polyfrost.cc/legal/oneconfig/additional-terms> + */ + +package cc.polyfrost.oneconfig.config.gson; + +import com.google.gson.InstanceCreator; + +import java.lang.reflect.Type; + +public class InstanceSupplier<T> implements InstanceCreator<T> { + private final T instance; + + public InstanceSupplier(T instance) { + this.instance = instance; + } + + @Override + public T createInstance(Type type) { + return instance; + } +}
\ No newline at end of file diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/gsoninterface/GsonContext.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/gsoninterface/GsonContext.java deleted file mode 100644 index e27a362..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/gson/gsoninterface/GsonContext.java +++ /dev/null @@ -1,123 +0,0 @@ -package cc.polyfrost.oneconfig.config.gson.gsoninterface; - -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021, 2022 Polyfrost. - * - * <https://polyfrost.cc> <https://github.com/Polyfrost/> - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * OneConfig is licensed under the terms of version 3 of the GNU Lesser - * General Public License as published by the Free Software Foundation, AND - * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, - * either version 1.0 of the Additional Terms, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License. If not, see <https://www.gnu.org/licenses/>. You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * <https://polyfrost.cc/legal/oneconfig/additional-terms> - - * This file contains an adaptation of code from gson-interface - * Project found at <https://github.com/mintern/gson-interface> - * For the avoidance of doubt, this file is still licensed under the terms - * of OneConfig's Licensing. - * - * LICENSE NOTICE FOR ADAPTED CODE - * - * Copyright (C) 2012, Brandon Mintern, EasyESI, Berkeley, CA - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither gson-interface nor the names of its contributors may be used - * to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRANDON MINTERN OR EASYESI BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - */ - -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonIOException; -import com.google.gson.TypeAdapter; -import com.google.gson.internal.bind.JsonTreeReader; -import com.google.gson.internal.bind.JsonTreeWriter; - -import java.io.IOException; -import java.lang.reflect.Type; - -/** - * @author mintern - */ -public class GsonContext<T> { - private final Gson gson; - private final InterfaceAdapterFactory.InterfaceTypeAdapter<T> constructingAdapter; - - public GsonContext(Gson g, InterfaceAdapterFactory.InterfaceTypeAdapter<T> ita) { - gson = g; - constructingAdapter = ita; - } - - public JsonElement toJsonTree(Object obj) { - return gson.toJsonTree(obj); - } - - public JsonElement thisToJsonTree(T obj) throws JsonIOException { - JsonTreeWriter writer = new JsonTreeWriter(); - try { - constructingAdapter.getDelegate().write(writer, obj); - } catch (IOException e) { - throw new JsonIOException(e); - } - return writer.get(); - } - - public <C> C fromJsonTree(JsonElement json, Class<C> type) { - return gson.fromJson(json, type); - } - - public <C> C fromJsonTree(JsonElement json, Type typeOfC) { - return (C) gson.fromJson(json, typeOfC); - } - - public T thisFromJsonTree(JsonElement json) throws JsonIOException { - try { - return constructingAdapter.getDelegate().read(new JsonTreeReader(json)); - } catch (IOException e) { - throw new JsonIOException(e); - } - } - - public <C extends T> C thisFromJsonTree(JsonElement json, Type typeOfC) { - TypeAdapter<C> adapter = constructingAdapter.getNextAdapter(typeOfC); - try { - return adapter.read(new JsonTreeReader(json)); - } catch (IOException e) { - throw new JsonIOException(e); - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/gson/gsoninterface/InterfaceAdapterFactory.java b/src/main/java/cc/polyfrost/oneconfig/config/gson/gsoninterface/InterfaceAdapterFactory.java deleted file mode 100644 index 8f7d9d6..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/gson/gsoninterface/InterfaceAdapterFactory.java +++ /dev/null @@ -1,192 +0,0 @@ -package cc.polyfrost.oneconfig.config.gson.gsoninterface; - -/* - * This file is part of OneConfig. - * OneConfig - Next Generation Config Library for Minecraft: Java Edition - * Copyright (C) 2021, 2022 Polyfrost. - * - * <https://polyfrost.cc> <https://github.com/Polyfrost/> - * - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * OneConfig is licensed under the terms of version 3 of the GNU Lesser - * General Public License as published by the Free Software Foundation, AND - * under the Additional Terms Applicable to OneConfig, as published by Polyfrost, - * either version 1.0 of the Additional Terms, or (at your option) any later - * version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License. If not, see <https://www.gnu.org/licenses/>. You should - * have also received a copy of the Additional Terms Applicable - * to OneConfig, as published by Polyfrost. If not, see - * <https://polyfrost.cc/legal/oneconfig/additional-terms> - - * This file contains an adaptation of code from gson-interface - * Project found at <https://github.com/mintern/gson-interface> - * For the avoidance of doubt, this file is still licensed under the terms - * of OneConfig's Licensing. - * - * LICENSE NOTICE FOR ADAPTED CODE - * - * Copyright (C) 2012, Brandon Mintern, EasyESI, Berkeley, CA - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * * Neither gson-interface nor the names of its contributors may be used - * to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS - * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BRANDON MINTERN OR EASYESI BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF - * THE POSSIBILITY OF SUCH DAMAGE. - */ - -import com.google.gson.*; -import com.google.gson.internal.Streams; -import com.google.gson.reflect.TypeToken; -import com.google.gson.stream.JsonReader; -import com.google.gson.stream.JsonWriter; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.Type; -import java.util.HashMap; -import java.util.Map; - -/** - * @author mintern - */ -public class InterfaceAdapterFactory implements TypeAdapterFactory { - @Override - public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> tt) { - Class<T> rawType = Reflection.classOfType(tt.getRawType()); - boolean serializes = JsonSerialization.class.isAssignableFrom(rawType); - Constructor<JsonDeserializes<T>> deserializerConstructor = null; - Class<JsonDeserializes<T>>[] typeParameters = Reflection.getTypeParameters(rawType, JsonDeserialization.class); - if (typeParameters != null) { - deserializerConstructor = Reflection.getConstructor(typeParameters[0]); - } - if (serializes || deserializerConstructor != null) { - return new InterfaceTypeAdapter(serializes, deserializerConstructor, gson, tt, this); - } - return null; - } - |
