43 lines
1.7 KiB
Java
43 lines
1.7 KiB
Java
package io.gitlab.jfronny.resclone.util.config;
|
|
|
|
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
|
|
import io.gitlab.jfronny.resclone.Resclone;
|
|
import io.gitlab.jfronny.resclone.api.RescloneApi;
|
|
import io.gitlab.jfronny.resclone.data.PackMetaUnloaded;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
public class ConfigLoader {
|
|
public static void load(RescloneApi api) {
|
|
Path configPath = api.getConfigPath().resolve("config.json");
|
|
if (!Files.exists(configPath)) {
|
|
save(api, new HashSet<>(), true);
|
|
}
|
|
try (BufferedReader reader = Files.newBufferedReader(configPath)) {
|
|
RescloneConfig data = GsonHolders.CONFIG.getGson().fromJson(reader, RescloneConfig.class);
|
|
api.setPruneUnused(data.pruneUnused());
|
|
for (PackMetaUnloaded meta : data.packs()) {
|
|
api.addPack(meta.fetcher, meta.source, meta.name, meta.forceDownload, meta.forceEnable);
|
|
}
|
|
if (data.updateRequired()) {
|
|
save(api, data.packs(), data.pruneUnused());
|
|
}
|
|
} catch (IOException e) {
|
|
Resclone.LOGGER.error("Could not load config", e);
|
|
}
|
|
}
|
|
|
|
public static void save(RescloneApi api, Set<PackMetaUnloaded> packs, boolean pruneUnused) {
|
|
try (var writer = Files.newBufferedWriter(api.getConfigPath().resolve("config.json"))) {
|
|
GsonHolders.CONFIG.getGson().toJson(new RescloneConfig(packs, pruneUnused), writer);
|
|
} catch (IOException e) {
|
|
Resclone.LOGGER.error("Could not write config", e);
|
|
}
|
|
}
|
|
}
|