diff options
author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-02-12 13:25:04 +0100 |
---|---|---|
committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-02-12 13:25:04 +0100 |
commit | 49c5129697b599955341221818e6a5c6df70e3fa (patch) | |
tree | d06d7d6aa2a6b8a69c6f44737e5355371f2b9810 /src/main/java/io/polyfrost/oneconfig | |
parent | 86fed12ac48f750c16efd1311dfae980371725f9 (diff) | |
download | OneConfig-49c5129697b599955341221818e6a5c6df70e3fa.tar.gz OneConfig-49c5129697b599955341221818e6a5c6df70e3fa.tar.bz2 OneConfig-49c5129697b599955341221818e6a5c6df70e3fa.zip |
config thingies
Diffstat (limited to 'src/main/java/io/polyfrost/oneconfig')
5 files changed, 173 insertions, 2 deletions
diff --git a/src/main/java/io/polyfrost/oneconfig/OneConfig.java b/src/main/java/io/polyfrost/oneconfig/OneConfig.java index 6f58f52..a025984 100644 --- a/src/main/java/io/polyfrost/oneconfig/OneConfig.java +++ b/src/main/java/io/polyfrost/oneconfig/OneConfig.java @@ -1,6 +1,7 @@ package io.polyfrost.oneconfig; import io.polyfrost.oneconfig.command.OneConfigCommand; +import io.polyfrost.oneconfig.test.TestConfig; import net.minecraft.client.Minecraft; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; @@ -12,11 +13,11 @@ import java.io.File; @Mod(modid = "oneconfig", name = "OneConfig", version = "${version}") public class OneConfig { - private static Minecraft mc = Minecraft.getMinecraft(); + private static final Minecraft mc = Minecraft.getMinecraft(); public static File jarFile; public static File oneConfigDir = new File(mc.mcDataDir, "OneConfig/"); public static File themesDir = new File(oneConfigDir, "themes/"); - + public static TestConfig config = new TestConfig(); @Mod.EventHandler public void onPreFMLInit(FMLPreInitializationEvent event) { diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java new file mode 100644 index 0000000..48abf1c --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/Config.java @@ -0,0 +1,63 @@ +package io.polyfrost.oneconfig.interfaces; + +import com.google.gson.*; + +import java.io.*; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.charset.StandardCharsets; +import java.util.Map; + +public class Config { + private final File configFile; + + public Config(File configFile) { + this.configFile = configFile; + if (configFile.exists()) + load(); + else + save(); + } + + Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting() + .registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create(); + + public void save() { + try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) { + writer.write(gson.toJson(this.getClass())); + } catch (IOException ignored) { + } + } + + public void load() { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) { + processPart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass()); + } catch (IOException ignored) { + } + } + + private void processPart(JsonObject json, Class<?> clazz) { + for (Map.Entry<String, JsonElement> element : json.entrySet()) { + String name = element.getKey(); + JsonElement value = element.getValue(); + if (value.isJsonObject()) { + for (Class<?> innerClass : clazz.getClasses()) { + if (innerClass.getSimpleName().equals(name)) { + processPart(value.getAsJsonObject(), innerClass); + break; + } + } + } else { + try { + Field field = clazz.getField(name); + TypeAdapter<?> adapter = gson.getAdapter(field.getType()); + Object object = adapter.fromJsonTree(value); + field.setAccessible(true); + field.set(null, object); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java new file mode 100644 index 0000000..95a558d --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapter.java @@ -0,0 +1,53 @@ +package io.polyfrost.oneconfig.interfaces; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.IOException; +import java.lang.reflect.Field; + +final class OneConfigTypeAdapter<T> extends TypeAdapter<Class<T>> { + + private final Gson gson; + private final JsonParser parser = new JsonParser(); + + private OneConfigTypeAdapter(final Gson gson) { + this.gson = gson; + } + + static <T> TypeAdapter<Class<T>> getStaticTypeAdapter(final Gson gson) { + return new OneConfigTypeAdapter<>(gson); + } + + @Override + public void write(final JsonWriter out, final Class<T> value) throws IOException { + try { + out.beginObject(); + for (Field field : value.getFields()) { + out.name(field.getName()); + field.setAccessible(true); + final Object fieldValue = field.get(null); + final TypeAdapter<Object> adapter = (TypeAdapter) gson.getAdapter(field.getType()); + adapter.write(out, fieldValue); + } + for (Class<?> clazz : value.getClasses()) { + System.out.println(clazz.toString()); + out.name(clazz.getSimpleName()); + final TypeAdapter<JsonElement> adapter = gson.getAdapter(JsonElement.class); + adapter.write(out, parser.parse(gson.toJson(clazz))); + } + out.endObject(); + } catch (final IllegalAccessException ex) { + throw new IOException(ex); + } + } + + @Override + public Class<T> read(final JsonReader in) throws IOException { + return null; + } +} diff --git a/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java new file mode 100644 index 0000000..6491ba2 --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/interfaces/OneConfigTypeAdapterFactory.java @@ -0,0 +1,30 @@ +package io.polyfrost.oneconfig.interfaces; + +import com.google.gson.Gson; +import com.google.gson.TypeAdapter; +import com.google.gson.TypeAdapterFactory; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; + +import static io.polyfrost.oneconfig.interfaces.OneConfigTypeAdapter.getStaticTypeAdapter; + +public class OneConfigTypeAdapterFactory implements TypeAdapterFactory { + + private static final TypeAdapterFactory staticTypeAdapterFactory = new OneConfigTypeAdapterFactory(); + + public static TypeAdapterFactory getStaticTypeAdapterFactory() { + return staticTypeAdapterFactory; + } + + @Override + public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> typeToken) { + final Type type = typeToken.getType(); + if (type.equals(Class.class)) { + @SuppressWarnings("unchecked") final TypeAdapter<T> castStaticTypeAdapter = (TypeAdapter<T>) getStaticTypeAdapter(gson); + return castStaticTypeAdapter; + } + return null; + } + +} diff --git a/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java b/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java new file mode 100644 index 0000000..21259ac --- /dev/null +++ b/src/main/java/io/polyfrost/oneconfig/test/TestConfig.java @@ -0,0 +1,24 @@ +package io.polyfrost.oneconfig.test; + +import io.polyfrost.oneconfig.annotations.Category; +import io.polyfrost.oneconfig.annotations.Switch; +import io.polyfrost.oneconfig.annotations.TextField; +import io.polyfrost.oneconfig.interfaces.Config; + +import java.io.File; + +public class TestConfig extends Config { + + @Switch(name = "Cool Switch") + public static boolean toggle = false; + + @Category(name = "Cool Category") + public static class category { + @TextField(name = "Cool text field") + public static String text = "e"; + } + + public TestConfig() { + super(new File("./config/testConfig.json")); + } +} |