LibJF/libjf-config-core-v1/src/main/java/io/gitlab/jfronny/libjf/config/impl/dsl/DefaultConfigIO.java

89 lines
3.9 KiB
Java

package io.gitlab.jfronny.libjf.config.impl.dsl;
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
import io.gitlab.jfronny.gson.*;
import io.gitlab.jfronny.gson.stream.JsonWriter;
import io.gitlab.jfronny.libjf.LibJf;
import io.gitlab.jfronny.libjf.config.api.v1.*;
import io.gitlab.jfronny.libjf.config.impl.entrypoint.JfConfigSafe;
import io.gitlab.jfronny.libjf.config.impl.watch.JfConfigWatchService;
import java.io.*;
import java.nio.file.Files;
import java.util.Map;
import java.util.function.Consumer;
public class DefaultConfigIO {
public static Consumer<ConfigInstance> loader(String id) {
return c -> c.getFilePath().ifPresent(path -> {
if (Files.exists(path)) {
try (BufferedReader br = Files.newBufferedReader(path)) {
JsonElement element = JsonParser.parseReader(br);
if (element.isJsonObject()) loadFrom(element.getAsJsonObject(), c);
else LibJf.LOGGER.error("Invalid config: Not a JSON object for " + id);
} catch (Exception e) {
LibJf.LOGGER.error("Could not read config for " + id, e);
}
}
c.write();
});
}
private static void loadFrom(JsonObject source, ConfigCategory category) {
for (EntryInfo<?> entry : category.getEntries()) {
if (source.has(entry.getName())) {
try {
entry.loadFromJson(source.get(entry.getName()));
} catch (IllegalAccessException e) {
LibJf.LOGGER.error("Could not set config entry value of " + entry.getName(), e);
}
} else LibJf.LOGGER.error("Config does not contain entry for " + entry.getName());
}
for (Map.Entry<String, ConfigCategory> entry : category.getCategories().entrySet()) {
if (source.has(entry.getKey())) {
JsonElement el = source.get(entry.getKey());
if (el.isJsonObject()) loadFrom(el.getAsJsonObject(), entry.getValue());
else LibJf.LOGGER.error("Config category is not a JSON object, skipping");
} else LibJf.LOGGER.error("Config does not contain entry for subcategory " + entry.getKey());
}
if (category instanceof DslConfigCategory cat) {
for (Map.Entry<String, Consumer<JsonElement>> entry : cat.migrations.entrySet()) {
if (source.has(entry.getKey())) {
entry.getValue().accept(source.get(entry.getKey()));
}
}
}
}
public static Consumer<ConfigInstance> writer(String id) {
return c -> c.getFilePath().ifPresent(path -> JfConfigWatchService.lock(path, () -> {
try (BufferedWriter bw = Files.newBufferedWriter(path);
JsonWriter jw = GsonHolders.CONFIG.getGson().newJsonWriter(bw)) {
writeTo(jw, c);
} catch (Exception e) {
LibJf.LOGGER.error("Could not write config for " + id, e);
}
}));
}
private static void writeTo(JsonWriter writer, ConfigCategory category) throws IOException {
category.fix();
writer.beginObject();
String val;
for (EntryInfo<?> entry : category.getEntries()) {
try {
entry.writeTo(writer, category.getTranslationPrefix());
} catch (IllegalAccessException e) {
LibJf.LOGGER.error("Could not write entry", e);
}
}
for (Map.Entry<String, ConfigCategory> entry : category.getCategories().entrySet()) {
if ((val = JfConfigSafe.TRANSLATION_SUPPLIER.apply(category.getTranslationPrefix() + entry.getKey() + ".title")) != null)
writer.comment(val);
writer.name(entry.getKey());
writeTo(writer, entry.getValue());
}
writer.endObject();
}
}