|
@@ -1,94 +1,38 @@
|
|
package me.lortseam.completeconfig;
|
|
package me.lortseam.completeconfig;
|
|
|
|
|
|
-import me.lortseam.completeconfig.api.ConfigGroup;
|
|
|
|
-import me.lortseam.completeconfig.api.ConfigOwner;
|
|
|
|
import me.lortseam.completeconfig.data.Config;
|
|
import me.lortseam.completeconfig.data.Config;
|
|
|
|
+import me.lortseam.completeconfig.api.ConfigGroup;
|
|
import me.lortseam.completeconfig.gui.GuiBuilder;
|
|
import me.lortseam.completeconfig.gui.GuiBuilder;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.fabricmc.api.Environment;
|
|
-import net.fabricmc.loader.api.FabricLoader;
|
|
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
import net.minecraft.client.gui.screen.Screen;
|
|
-import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
-import org.apache.logging.log4j.LogManager;
|
|
|
|
-import org.apache.logging.log4j.Logger;
|
|
|
|
-import org.spongepowered.configurate.CommentedConfigurationNode;
|
|
|
|
-import org.spongepowered.configurate.ConfigurateException;
|
|
|
|
-import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
|
|
|
-import org.spongepowered.configurate.serialize.TypeSerializerCollection;
|
|
|
|
|
|
|
|
-import java.nio.file.Path;
|
|
|
|
-import java.nio.file.Paths;
|
|
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Set;
|
|
|
|
|
|
public final class ConfigHandler {
|
|
public final class ConfigHandler {
|
|
|
|
|
|
- private static final Logger LOGGER = LogManager.getLogger();
|
|
|
|
- private static final Map<Class<? extends ConfigOwner>, ConfigHandler> HANDLERS = new HashMap<>();
|
|
|
|
- private static final Map<String, List<String[]>> MOD_BRANCHES = new HashMap<>();
|
|
|
|
|
|
+ private static final Set<ConfigHandler> HANDLERS = new HashSet<>();
|
|
|
|
|
|
static {
|
|
static {
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
- for (ConfigHandler handler : HANDLERS.values()) {
|
|
|
|
|
|
+ for (ConfigHandler handler : HANDLERS) {
|
|
handler.save();
|
|
handler.save();
|
|
}
|
|
}
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
|
|
|
|
- static ConfigHandler buildConfig(String modID, String[] branch, Class<? extends ConfigOwner> owner, List<ConfigGroup> topLevelGroups, TypeSerializerCollection typeSerializers, GuiBuilder guiBuilder) {
|
|
|
|
- if (HANDLERS.containsKey(owner)) {
|
|
|
|
- throw new IllegalArgumentException("The specified owner " + owner + " already created a config!");
|
|
|
|
- }
|
|
|
|
- if (topLevelGroups.isEmpty()) {
|
|
|
|
- LOGGER.warn("[CompleteConfig] Owner " + owner + " of mod " + modID + " tried to create an empty config!");
|
|
|
|
- return null;
|
|
|
|
- }
|
|
|
|
- List<String[]> branches = MOD_BRANCHES.computeIfAbsent(modID, key -> new ArrayList<>());
|
|
|
|
- if (branches.stream().anyMatch(presentBranch -> Arrays.equals(branch, presentBranch))) {
|
|
|
|
- throw new IllegalArgumentException("A config of the mod " + modID + " with the specified branch " + Arrays.toString(branch) + " already exists!");
|
|
|
|
- }
|
|
|
|
- branches.add(branch);
|
|
|
|
- String[] subPath = ArrayUtils.add(branch, 0, modID);
|
|
|
|
- subPath[subPath.length - 1] = subPath[subPath.length - 1] + ".conf";
|
|
|
|
- Path filePath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), subPath);
|
|
|
|
- ConfigHandler handler = new ConfigHandler(modID, filePath, topLevelGroups, typeSerializers, guiBuilder);
|
|
|
|
- HANDLERS.put(owner, handler);
|
|
|
|
- return handler;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * Gets the {@link ConfigHandler} for the specified owner if that owner created a config before.
|
|
|
|
- *
|
|
|
|
- * @param owner The owner class of the config
|
|
|
|
- * @return The handler if one was found or else an empty result
|
|
|
|
- */
|
|
|
|
- public static Optional<ConfigHandler> of(Class<? extends ConfigOwner> owner) {
|
|
|
|
- return Optional.ofNullable(HANDLERS.get(owner));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private final HoconConfigurationLoader loader;
|
|
|
|
|
|
+ private final ConfigSource source;
|
|
private final Config config;
|
|
private final Config config;
|
|
private GuiBuilder guiBuilder;
|
|
private GuiBuilder guiBuilder;
|
|
|
|
|
|
- private ConfigHandler(String modID, Path filePath, List<ConfigGroup> topLevelGroups, TypeSerializerCollection typeSerializers, GuiBuilder guiBuilder) {
|
|
|
|
- loader = HoconConfigurationLoader.builder()
|
|
|
|
- .path(filePath)
|
|
|
|
- .defaultOptions(options -> options.serializers(builder -> builder.registerAll(typeSerializers)))
|
|
|
|
- .build();
|
|
|
|
- config = new Config(modID, topLevelGroups);
|
|
|
|
- CommentedConfigurationNode root = load();
|
|
|
|
- if (!root.virtual()) {
|
|
|
|
- config.apply(root);
|
|
|
|
- }
|
|
|
|
|
|
+ ConfigHandler(ConfigSource source, List<ConfigGroup> topLevelGroups, GuiBuilder guiBuilder) {
|
|
|
|
+ this.source = source;
|
|
|
|
+ config = new Config(source.getModID(), topLevelGroups);
|
|
this.guiBuilder = guiBuilder;
|
|
this.guiBuilder = guiBuilder;
|
|
- }
|
|
|
|
-
|
|
|
|
- private CommentedConfigurationNode load() {
|
|
|
|
- try {
|
|
|
|
- return loader.load();
|
|
|
|
- } catch (ConfigurateException e) {
|
|
|
|
- LOGGER.error("[CompleteConfig] Failed to load config from file!", e);
|
|
|
|
- }
|
|
|
|
- return CommentedConfigurationNode.root();
|
|
|
|
|
|
+ HANDLERS.add(this);
|
|
|
|
+ source.load(config);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -113,13 +57,7 @@ public final class ConfigHandler {
|
|
* Saves the config to a save file.
|
|
* Saves the config to a save file.
|
|
*/
|
|
*/
|
|
public void save() {
|
|
public void save() {
|
|
- CommentedConfigurationNode root = loader.createNode();
|
|
|
|
- config.fetch(root);
|
|
|
|
- try {
|
|
|
|
- loader.save(root);
|
|
|
|
- } catch (ConfigurateException e) {
|
|
|
|
- LOGGER.error("[CompleteConfig] Failed to save config to file!", e);
|
|
|
|
- }
|
|
|
|
|
|
+ source.save(config);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|