Resclone/src/main/java/io/gitlab/jfronny/resclone/RescloneConfig.java

147 lines
6.8 KiB
Java

package io.gitlab.jfronny.resclone;
import com.google.gson.reflect.TypeToken;
import io.gitlab.jfronny.commons.serialize.gson.api.v2.GsonHolders;
import io.gitlab.jfronny.gson.JsonParseException;
import io.gitlab.jfronny.gson.stream.*;
import io.gitlab.jfronny.libjf.config.api.v2.JfCustomConfig;
import io.gitlab.jfronny.libjf.config.api.v2.dsl.DSL;
import io.gitlab.jfronny.resclone.data.PackMetaUnloaded;
import java.io.*;
import java.lang.reflect.Type;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
public class RescloneConfig implements JfCustomConfig {
public static Set<PackMetaUnloaded> packs;
public static boolean pruneUnused;
public static boolean filterPacks;
public static boolean logProcessing;
private static final String ERR_DUPLICATE = "Unexpected duplicate \"%s\" in Resclone config";
private static final String PACKS = "packs";
private static final String PRUNE_UNUSED = "pruneUnused";
private static final String FILTER_PACKS = "filterPacks";
private static final String LOG_PROCESSING = "logProcessing";
private static final Type META_SET = new TypeToken<Set<PackMetaUnloaded>>(){}.getType();
private static void load(Path path) throws IOException {
if (!Files.exists(path)) {
packs = new HashSet<>();
pruneUnused = true;
filterPacks = true;
logProcessing = false;
write(path);
return;
}
boolean updateRequired = false;
try (BufferedReader br = Files.newBufferedReader(path);
JsonReader reader = GsonHolders.CONFIG.getGson().newJsonReader(br)) {
if (reader.peek() == JsonToken.BEGIN_ARRAY) {
// Legacy format compatibility
packs = GsonHolders.CONFIG.getGson().fromJson(reader, META_SET);
updateRequired = true;
} else if (reader.peek() == JsonToken.BEGIN_OBJECT) {
// New format
reader.beginObject();
Set<PackMetaUnloaded> packs = null;
Boolean pruneUnused = null;
Boolean filterPacks = null;
Boolean logProcessing = null;
while (reader.peek() != JsonToken.END_OBJECT) {
final String name = reader.nextName();
switch (name) {
case PACKS -> {
if (packs != null) throw new JsonParseException(ERR_DUPLICATE.formatted(PACKS));
packs = GsonHolders.CONFIG.getGson().fromJson(reader, META_SET);
}
case PRUNE_UNUSED -> {
if (pruneUnused != null) throw new JsonParseException(ERR_DUPLICATE.formatted(PRUNE_UNUSED));
pruneUnused = reader.nextBoolean();
}
case FILTER_PACKS -> {
if (filterPacks != null) throw new JsonParseException(ERR_DUPLICATE.formatted(FILTER_PACKS));
filterPacks = reader.nextBoolean();
}
case LOG_PROCESSING -> {
if (logProcessing != null) throw new JsonParseException(ERR_DUPLICATE.formatted(LOG_PROCESSING));
logProcessing = reader.nextBoolean();
}
default -> throw new JsonParseException("Unexpected element: \"" + name + "\" in Resclone config");
}
}
reader.endObject();
if (packs == null) throw new JsonParseException("Expected Resclone config object to contain packs");
if (pruneUnused == null) {
pruneUnused = true;
updateRequired = true;
}
if (filterPacks == null) {
filterPacks = true;
updateRequired = true;
}
if (logProcessing == null) {
logProcessing = false;
updateRequired = true;
}
RescloneConfig.packs = packs;
RescloneConfig.pruneUnused = pruneUnused;
RescloneConfig.filterPacks = filterPacks;
RescloneConfig.logProcessing = logProcessing;
} else throw new JsonParseException("Expected Resclone config to be an object or array");
}
if (updateRequired) write(path);
}
private static void write(Path path) throws IOException {
try (BufferedWriter bw = Files.newBufferedWriter(path);
JsonWriter writer = GsonHolders.CONFIG.getGson().newJsonWriter(bw)) {
writer.beginObject()
.comment("The packs to be loaded by resclone")
.name(PACKS);
GsonHolders.CONFIG.getGson().toJson(packs, META_SET, writer);
writer.comment("Automatically remove all downloaded packs that are not in the config to free up unneeded space")
.name(PRUNE_UNUSED)
.value(pruneUnused)
.comment("Whether to filter packs to remove files unchanged from vanilla and empty directories")
.name(FILTER_PACKS)
.value(filterPacks)
.comment("Log automatic processing steps applied to downloaded packs")
.name(LOG_PROCESSING)
.value(logProcessing)
.endObject();
}
}
static {
Path path = Resclone.getConfigPath().resolve("config.json");
DSL.create(Resclone.MOD_ID).register(builder ->
builder.setLoadMethod(configInstance -> {
try {
load(path);
} catch (IOException e) {
Resclone.LOGGER.error("Could not load config", e);
}
}).setWriteMethod(configInstance -> {
try {
write(path);
} catch (IOException e) {
Resclone.LOGGER.error("Could not write config", e);
}
}).setPath(path)
.<Set<PackMetaUnloaded>>value(PACKS, new HashSet<>(), Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, io.gitlab.jfronny.libjf.config.api.v2.type.Type.ofClass(META_SET), 100, () -> packs, p -> packs = p)
.value(PRUNE_UNUSED, pruneUnused, () -> pruneUnused, p -> pruneUnused = p)
.value(FILTER_PACKS, filterPacks, () -> filterPacks, p -> filterPacks = p)
.value(LOG_PROCESSING, logProcessing, () -> logProcessing, p -> logProcessing = p)
).load();
}
@Override
public void register(DSL.Defaulted dsl) {
}
}