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.config.profiles.Profiles;
import io.polyfrost.oneconfig.gui.elements.config.*;
import io.polyfrost.oneconfig.hud.HudCore;
import io.polyfrost.oneconfig.hud.interfaces.BasicHud;
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.Arrays;
import java.util.Map;
import java.util.Optional;
public class Config {
protected final String configFile;
protected 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, String configFile) {
this.configFile = configFile;
init(modData);
}
public void init(ModData modData) {
if (Profiles.getProfileFile(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(Profiles.getProfileFile(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(Profiles.getProfileFile(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
*/
protected ArrayList