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/interfaces/Config.java | |
| 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/interfaces/Config.java')
| -rw-r--r-- | src/main/java/io/polyfrost/oneconfig/interfaces/Config.java | 63 |
1 files changed, 63 insertions, 0 deletions
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(); + } + } + } + } +} |
