package io.polyfrost.oneconfig.config.interfaces;
import com.google.gson.*;
import io.polyfrost.oneconfig.config.annotations.*;
import io.polyfrost.oneconfig.config.core.ConfigCore;
import io.polyfrost.oneconfig.config.data.ModData;
import io.polyfrost.oneconfig.gui.elements.config.*;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Map;
public class Config {
private final File configFile;
private final Gson gson = new GsonBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setPrettyPrinting()
.registerTypeAdapterFactory(OneConfigTypeAdapterFactory.getStaticTypeAdapterFactory()).create();
/**
* @param modData information about the mod
* @param configFile file where config is stored
*/
public Config(ModData modData, File configFile) {
this.configFile = configFile;
if (configFile.exists())
load();
else
save();
modData.config = this;
ConfigCore.settings.put(modData, generateOptionList(this.getClass()));
}
/**
* Save current config to file
*/
public void save() {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8))) {
writer.write(gson.toJson(this.getClass()));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Load file and overwrite current values
*/
public void load() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8))) {
deserializePart(new JsonParser().parse(reader).getAsJsonObject(), this.getClass());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Generate the option list for internal use only
*
* @param clazz target class
* @return list of options
*/
private ArrayList