191 lines
4.6 KiB
Java
191 lines
4.6 KiB
Java
package pm.j4.petroleum.util.config;
|
|
|
|
import com.google.common.reflect.TypeToken;
|
|
import com.google.gson.*;
|
|
import java.io.*;
|
|
import java.lang.reflect.Type;
|
|
import java.util.*;
|
|
import net.fabricmc.loader.api.FabricLoader;
|
|
import pm.j4.petroleum.PetroleumMod;
|
|
import pm.j4.petroleum.modules.bindings.BindingInfo;
|
|
|
|
/**
|
|
* The type Config manager.
|
|
*/
|
|
public class ConfigManager {
|
|
/**
|
|
* The constant GSON.
|
|
*/
|
|
public static final Gson GSON = new GsonBuilder()
|
|
.registerTypeAdapter(GlobalConfig.class, SerializationHelper.getSerializer())
|
|
.registerTypeAdapter(GlobalConfig.class, SerializationHelper.getDeserializer())
|
|
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create();
|
|
/**
|
|
* The constant file.
|
|
*/
|
|
private static File file;
|
|
/**
|
|
* The constant config.
|
|
*/
|
|
private static ConfigHolder config;
|
|
|
|
/**
|
|
* Prepare config file.
|
|
*/
|
|
private static void prepareConfigFile() {
|
|
if (file != null) {
|
|
return;
|
|
}
|
|
file = new File(FabricLoader.getInstance().getConfigDir().toString(), "petroleum.json");
|
|
}
|
|
|
|
/**
|
|
* Init config.
|
|
*/
|
|
public static void initConfig() {
|
|
if (config != null) {
|
|
return;
|
|
}
|
|
|
|
config = new ConfigHolder();
|
|
config.globalConfig = new DefaultConfig();
|
|
config.serverConfigs = new HashMap<>();
|
|
|
|
load();
|
|
}
|
|
|
|
/**
|
|
* Load.
|
|
*/
|
|
private static void load() {
|
|
prepareConfigFile();
|
|
|
|
try {
|
|
if (!file.exists()) {
|
|
save();
|
|
}
|
|
if (file.exists()) {
|
|
BufferedReader reader = new BufferedReader(new FileReader(file));
|
|
ConfigHolder parsedConfig = null;
|
|
try {
|
|
parsedConfig = GSON.fromJson(reader, ConfigHolder.class);
|
|
} catch (Exception e) {
|
|
System.out.println("Couldn't parse config file");
|
|
e.printStackTrace();
|
|
}
|
|
if (parsedConfig != null) {
|
|
config = parsedConfig;
|
|
}
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
System.out.println("Couldn't load Petroleum configuration");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Save.
|
|
*/
|
|
public static void save() {
|
|
prepareConfigFile();
|
|
|
|
String json = GSON.toJson(config);
|
|
|
|
try (FileWriter fileWriter = new FileWriter(file)) {
|
|
fileWriter.write(json);
|
|
} catch (IOException e) {
|
|
System.out.println("Couldn't save Petroleum configuration");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets config.
|
|
*
|
|
* @return the config
|
|
*/
|
|
public static Optional<ConfigHolder> getConfig() {
|
|
if (config == null) {
|
|
return Optional.empty();
|
|
}
|
|
return Optional.of(config);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The type Serialization helper.
|
|
*/
|
|
class SerializationHelper {
|
|
|
|
/**
|
|
* The constant s.
|
|
*/
|
|
private static final JsonSerializer<GlobalConfig> s = (src, typeOfSrc, ctx) -> {
|
|
JsonObject jsonConfig = new JsonObject();
|
|
|
|
JsonArray bindings = ctx.serialize(src.serializeBindings()).getAsJsonArray();
|
|
jsonConfig.add("bindings", bindings);
|
|
|
|
JsonArray modules = ctx.serialize(src.enabledModules).getAsJsonArray();
|
|
jsonConfig.add("enabled_modules", modules);
|
|
|
|
JsonObject moduleConfigs = new JsonObject();
|
|
PetroleumMod.getActiveMods().forEach(module -> {
|
|
moduleConfigs.add(module.getModuleName(), ctx.serialize(src.serializeModuleConfiguration(module)));
|
|
});
|
|
jsonConfig.add("module_configuration", moduleConfigs);
|
|
|
|
return jsonConfig;
|
|
};
|
|
|
|
/**
|
|
* The constant ds.
|
|
*/
|
|
private static final JsonDeserializer<GlobalConfig> ds = ((json, typeOfT, ctx) -> {
|
|
JsonObject obj = json.getAsJsonObject();
|
|
|
|
List<BindingInfo> bindings = new ArrayList<>();
|
|
if (obj.has("bindings")) {
|
|
obj.get("bindings").getAsJsonArray().forEach(b -> bindings.add(ctx.deserialize(b, BindingInfo.class)));
|
|
}
|
|
List<String> modules = new ArrayList<>();
|
|
if (obj.has("enabled_modules")) {
|
|
obj.get("enabled_modules").getAsJsonArray().forEach(m -> modules.add(m.getAsString()));
|
|
}
|
|
GlobalConfig cfg = new GlobalConfig();
|
|
Map<String, List<OptionSerializiable>> options;
|
|
Type type = new TypeToken<Map<String, List<OptionSerializiable>>>() {
|
|
}.getType();
|
|
if (obj.has("module_configuration")) {
|
|
options = ctx.deserialize(obj.get("module_configuration"), type);
|
|
} else {
|
|
options = new HashMap<>();
|
|
}
|
|
PetroleumMod.getActiveMods().forEach(module -> {
|
|
if (options.containsKey(module.getModuleName())) {
|
|
cfg.deserializeModuleConfiguration(options.get(module.getModuleName()), module);
|
|
}
|
|
});
|
|
cfg.deserializeBindings(bindings);
|
|
cfg.enabledModules = modules;
|
|
return cfg;
|
|
});
|
|
|
|
/**
|
|
* Gets serializer.
|
|
*
|
|
* @return the serializer
|
|
*/
|
|
public static JsonSerializer<GlobalConfig> getSerializer() {
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* Gets deserializer.
|
|
*
|
|
* @return the deserializer
|
|
*/
|
|
public static JsonDeserializer<GlobalConfig> getDeserializer() {
|
|
return ds;
|
|
}
|
|
} |