Resclone/src/main/java/io/gitlab/jfronny/resclone/util/config/ConfigLoader.java

43 lines
1.7 KiB
Java
Raw Normal View History

2022-07-31 14:18:49 +02:00
package io.gitlab.jfronny.resclone.util.config;
2020-12-29 16:14:53 +01:00
2023-02-26 14:00:07 +01:00
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolders;
2020-12-29 16:14:53 +01:00
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.api.RescloneApi;
import io.gitlab.jfronny.resclone.data.PackMetaUnloaded;
2022-07-31 14:18:49 +02:00
import java.io.BufferedReader;
2020-12-29 16:14:53 +01:00
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)) {
2022-07-31 14:18:49 +02:00
save(api, new HashSet<>(), true);
2020-12-29 16:14:53 +01:00
}
2022-07-31 14:18:49 +02:00
try (BufferedReader reader = Files.newBufferedReader(configPath)) {
2023-02-26 14:00:07 +01:00
RescloneConfig data = GsonHolders.CONFIG.getGson().fromJson(reader, RescloneConfig.class);
2022-07-31 14:18:49 +02:00
api.setPruneUnused(data.pruneUnused());
for (PackMetaUnloaded meta : data.packs()) {
api.addPack(meta.fetcher, meta.source, meta.name, meta.forceDownload, meta.forceEnable);
2020-12-29 16:14:53 +01:00
}
2022-07-31 14:18:49 +02:00
if (data.updateRequired()) {
save(api, data.packs(), data.pruneUnused());
}
2020-12-29 16:14:53 +01:00
} catch (IOException e) {
Resclone.LOGGER.error("Could not load config", e);
2020-12-29 16:14:53 +01:00
}
}
2022-07-31 14:18:49 +02:00
public static void save(RescloneApi api, Set<PackMetaUnloaded> packs, boolean pruneUnused) {
try (var writer = Files.newBufferedWriter(api.getConfigPath().resolve("config.json"))) {
2023-02-26 14:00:07 +01:00
GsonHolders.CONFIG.getGson().toJson(new RescloneConfig(packs, pruneUnused), writer);
2020-12-29 16:14:53 +01:00
} catch (IOException e) {
Resclone.LOGGER.error("Could not write config", e);
2020-12-29 16:14:53 +01:00
}
}
}