mirror of
https://github.com/Shiewk/ViewServerResources.git
synced 2026-04-28 03:44:17 +02:00
Improve config saving & loading
This commit is contained in:
@@ -1,40 +1,33 @@
|
||||
package de.shiewk.viewserverresources.client;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.internal.Streams;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import de.shiewk.viewserverresources.config.ViewServerResourcesConfig;
|
||||
import de.shiewk.viewserverresources.event.ChatAnnouncer;
|
||||
import de.shiewk.viewserverresources.event.ScreenListener;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
|
||||
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static de.shiewk.viewserverresources.ViewServerResourcesMod.LOGGER;
|
||||
|
||||
public class ViewServerResourcesClient implements ClientModInitializer {
|
||||
|
||||
private static final ObjectArrayList<String> whitelistedURLs = new ObjectArrayList<>();
|
||||
private static final ObjectArrayList<String> whitelistedHosts = new ObjectArrayList<>();
|
||||
private static boolean broadcastDownloads = true;
|
||||
private static ViewServerResourcesConfig config = new ViewServerResourcesConfig();
|
||||
private static File whitelistFile;
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
public static boolean allowedURL(URL uRL) {
|
||||
if (whitelistedURLs.contains(uRL.toString())){
|
||||
if (config.whitelist.urls.contains(uRL.toString())){
|
||||
if (LOGGER.isDebugEnabled())
|
||||
LOGGER.debug("URL {} is whitelisted", uRL);
|
||||
return true;
|
||||
} else if (whitelistedHosts.contains(uRL.getHost())){
|
||||
} else if (config.whitelist.hosts.contains(uRL.getHost())){
|
||||
if (LOGGER.isDebugEnabled())
|
||||
LOGGER.debug("Host {} is whitelisted", uRL.getHost());
|
||||
return true;
|
||||
@@ -45,36 +38,25 @@ public class ViewServerResourcesClient implements ClientModInitializer {
|
||||
public static void addWhitelistURL(URL url){
|
||||
final String urls = url.toString();
|
||||
LOGGER.info("Whitelisting url {}", urls);
|
||||
if (!whitelistedURLs.contains(urls)){
|
||||
whitelistedURLs.add(urls);
|
||||
if (!config.whitelist.urls.contains(urls)){
|
||||
config.whitelist.urls.add(urls);
|
||||
}
|
||||
}
|
||||
|
||||
public static void addWhitelistHost(URL url){
|
||||
final String h = url.getHost();
|
||||
LOGGER.info("Whitelisting host {}", h);
|
||||
if (!whitelistedHosts.contains(h)){
|
||||
whitelistedHosts.add(h);
|
||||
if (!config.whitelist.hosts.contains(h)){
|
||||
config.whitelist.hosts.add(h);
|
||||
}
|
||||
}
|
||||
|
||||
public static void loadConfig(){
|
||||
public synchronized static void loadConfig(){
|
||||
LOGGER.info("Loading config");
|
||||
try (FileReader fr = new FileReader(whitelistFile)){
|
||||
final JsonObject cfg = gson.fromJson(fr, JsonObject.class);
|
||||
final JsonObject whitelist = cfg.get("whitelist").getAsJsonObject();
|
||||
final JsonArray whitelistHosts = whitelist.getAsJsonArray("hosts");
|
||||
whitelistedHosts.clear();
|
||||
for (JsonElement whitelistHost : whitelistHosts) {
|
||||
whitelistedHosts.add(whitelistHost.getAsString());
|
||||
}
|
||||
final JsonArray whitelistURLs = whitelist.getAsJsonArray("urls");
|
||||
whitelistedURLs.clear();
|
||||
for (JsonElement whitelistURL : whitelistURLs) {
|
||||
whitelistedURLs.add(whitelistURL.getAsString());
|
||||
}
|
||||
final JsonElement bdl = cfg.get("broadcastDownloads");
|
||||
broadcastDownloads = bdl == null || !bdl.isJsonPrimitive() || bdl.getAsBoolean();
|
||||
ViewServerResourcesConfig cfg = gson.fromJson(fr, ViewServerResourcesConfig.class);
|
||||
Objects.requireNonNull(cfg, "Configuration");
|
||||
config = cfg;
|
||||
} catch (FileNotFoundException e) {
|
||||
LOGGER.warn("Config file not found");
|
||||
} catch (IOException e) {
|
||||
@@ -82,55 +64,29 @@ public class ViewServerResourcesClient implements ClientModInitializer {
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveConfig() {
|
||||
public synchronized static void saveConfig() {
|
||||
LOGGER.info("Saving config");
|
||||
try (FileWriter fw = new FileWriter(whitelistFile)) {
|
||||
final JsonObject cfg = getConfigObject();
|
||||
|
||||
try (JsonWriter jsonWriter = new JsonWriter(fw)) {
|
||||
Streams.write(cfg, jsonWriter);
|
||||
}
|
||||
|
||||
gson.toJson(config, fw);
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Error saving config", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static @NotNull JsonObject getConfigObject() {
|
||||
JsonObject cfg = new JsonObject();
|
||||
JsonObject whitelist = new JsonObject();
|
||||
|
||||
JsonArray hosts = new JsonArray();
|
||||
for (String whitelistedHost : whitelistedHosts) {
|
||||
hosts.add(whitelistedHost);
|
||||
}
|
||||
whitelist.add("hosts", hosts);
|
||||
|
||||
JsonArray urls = new JsonArray();
|
||||
for (String whitelistedURL : whitelistedURLs) {
|
||||
urls.add(whitelistedURL);
|
||||
}
|
||||
whitelist.add("urls", urls);
|
||||
|
||||
cfg.add("whitelist", whitelist);
|
||||
cfg.addProperty("broadcastDownloads", broadcastDownloads);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
public static List<String> getWhitelistedURLs() {
|
||||
return whitelistedURLs;
|
||||
return config.whitelist.urls;
|
||||
}
|
||||
|
||||
public static List<String> getWhitelistedHosts() {
|
||||
return whitelistedHosts;
|
||||
return config.whitelist.hosts;
|
||||
}
|
||||
|
||||
public static boolean isBroadcastDownloads() {
|
||||
return broadcastDownloads;
|
||||
return config.broadcastDownloads;
|
||||
}
|
||||
|
||||
public static void setBroadcastDownloads(boolean broadcastDownloads) {
|
||||
ViewServerResourcesClient.broadcastDownloads = broadcastDownloads;
|
||||
config.broadcastDownloads = broadcastDownloads;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package de.shiewk.viewserverresources.config;
|
||||
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
public class ViewServerResourcesConfig {
|
||||
|
||||
public Whitelists whitelist = new Whitelists();
|
||||
public boolean broadcastDownloads = true;
|
||||
|
||||
public static class Whitelists {
|
||||
public CopyOnWriteArrayList<String> urls = new CopyOnWriteArrayList<>();
|
||||
public CopyOnWriteArrayList<String> hosts = new CopyOnWriteArrayList<>();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user