Respackopts/src/main/java/io/gitlab/jfronny/respackopts/Respackopts.java

112 lines
4.2 KiB
Java
Raw Normal View History

2020-11-24 22:04:13 +01:00
package io.gitlab.jfronny.respackopts;
import com.google.gson.Gson;
2021-06-10 13:10:12 +02:00
import com.google.gson.GsonBuilder;
2021-06-10 16:11:19 +02:00
import io.gitlab.jfronny.respackopts.data.entry.*;
2021-07-13 16:15:03 +02:00
import io.gitlab.jfronny.respackopts.filters.FilterProvider;
2021-06-10 13:10:12 +02:00
import io.gitlab.jfronny.respackopts.gson.BooleanEntrySerializer;
import io.gitlab.jfronny.respackopts.gson.ConfigBranchSerializer;
import io.gitlab.jfronny.respackopts.gson.EnumEntrySerializer;
import io.gitlab.jfronny.respackopts.gson.NumericEntrySerializer;
2020-11-24 22:04:13 +01:00
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
2021-06-10 13:10:12 +02:00
import net.minecraft.util.Identifier;
2020-12-28 12:12:59 +01:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2020-11-24 22:04:13 +01:00
2021-06-10 16:11:19 +02:00
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
2020-11-24 22:04:13 +01:00
import java.nio.file.Files;
import java.nio.file.Path;
2021-06-10 16:11:19 +02:00
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
2020-11-24 22:04:13 +01:00
@Environment(EnvType.CLIENT)
public class Respackopts implements ClientModInitializer {
public static final Integer META_VERSION = 3;
2021-06-10 13:10:12 +02:00
public static final String ID = "respackopts";
public static final Identifier CONF_ID = new Identifier(ID, "conf.json");
public static final Logger LOGGER = LogManager.getFormatterLogger(ID);
public static final String FILE_EXTENSION = ".rpo";
public static final Set<Runnable> SAVE_ACTIONS = new HashSet<>();
public static Path CONF_DIR;
2021-06-10 13:10:12 +02:00
public static final Map<String, ConfigBranch> CONFIG_BRANCH = new HashMap<>();
2021-06-10 19:26:59 +02:00
public static final Map<String, String> NAME_LOOKUP = new HashMap<>();
2021-06-10 13:10:12 +02:00
public static final Gson GSON;
2020-11-24 22:04:13 +01:00
public static GuiFactory factory = new GuiFactory();
public static boolean forceRespackReload = false;
2021-06-10 13:10:12 +02:00
static {
GSON = new GsonBuilder()
.registerTypeAdapter(ConfigEnumEntry.class, new EnumEntrySerializer())
.registerTypeAdapter(ConfigNumericEntry.class, new NumericEntrySerializer())
.registerTypeAdapter(ConfigBooleanEntry.class, new BooleanEntrySerializer())
.registerTypeAdapter(ConfigBranch.class, new ConfigBranchSerializer())
.setPrettyPrinting()
.create();
try {
CONF_DIR = FabricLoader.getInstance().getConfigDir().resolve(ID);
} catch (Throwable e) {
LOGGER.error("Could not resolve config directory", e);
}
2021-06-10 13:10:12 +02:00
}
2020-11-24 22:04:13 +01:00
@Override
public void onInitializeClient() {
try {
2021-06-10 13:10:12 +02:00
Files.createDirectories(CONF_DIR);
} catch (IOException e) {
Respackopts.LOGGER.error("Could not initialize config directory", e);
}
2020-11-25 21:58:21 +01:00
if (FabricLoader.getInstance().isDevelopmentEnvironment())
2021-06-10 13:10:12 +02:00
SAVE_ACTIONS.add(() -> LOGGER.info("Save"));
2021-07-13 16:15:03 +02:00
FilterProvider.init();
}
public static void save() {
for (Map.Entry<String, ConfigBranch> e : CONFIG_BRANCH.entrySet()) {
try (Writer writer = Files.newBufferedWriter(CONF_DIR.resolve(e.getKey() + ".json"))) {
GSON.toJson(e.getValue(), writer);
writer.flush();
} catch (IOException ex) {
Respackopts.LOGGER.error("Could not save config", ex);
}
}
2021-06-10 13:10:12 +02:00
for (Runnable action : SAVE_ACTIONS) {
action.run();
}
}
public static void load(String id) {
2021-06-10 13:10:12 +02:00
Path q = CONF_DIR.resolve(id + ".json");
if (Files.exists(q)) {
2021-06-10 13:10:12 +02:00
try (Reader reader = Files.newBufferedReader(q)) {
ConfigBranch b = GSON.fromJson(reader, ConfigBranch.class);
if (CONFIG_BRANCH.containsKey(id))
2021-06-10 14:55:03 +02:00
CONFIG_BRANCH.get(id).sync(b, SyncMode.CONF_LOAD);
} catch (IOException e) {
2021-06-10 13:10:12 +02:00
LOGGER.error("Failed to load " + id, e);
}
}
}
public static String sanitizeString(String s) {
// This trims whitespace/underscores and removes non-alphabetical or underscore characters
// ^ = start of string
// $ = end of string
// * = zero or more times
// [\\s_] = whitespace or underscores
// | = or
// [^a-zA-Z_] = not character or underscore
return s.replaceAll("[^a-zA-Z_]|^[\\s_]*|[\\s_]*$", "");
}
2020-11-24 22:04:13 +01:00
}