From 8cd1f1a9804f980c1666079c99bafb6330c77723 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 7 Mar 2019 22:21:06 +0800 Subject: Config with comments --- .../me/shedaniel/rei/client/ConfigManager.java | 111 +++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/main/java/me/shedaniel/rei/client/ConfigManager.java (limited to 'src/main/java/me/shedaniel/rei/client/ConfigManager.java') diff --git a/src/main/java/me/shedaniel/rei/client/ConfigManager.java b/src/main/java/me/shedaniel/rei/client/ConfigManager.java new file mode 100644 index 000000000..c05442b41 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/client/ConfigManager.java @@ -0,0 +1,111 @@ +package me.shedaniel.rei.client; + +import blue.endless.jankson.Jankson; +import blue.endless.jankson.JsonObject; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import me.shedaniel.rei.RoughlyEnoughItemsCore; +import net.fabricmc.loader.api.FabricLoader; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; + +public class ConfigManager implements me.shedaniel.rei.api.ConfigManager { + + private static final Gson GSON = new GsonBuilder().create(); + private static final Jankson JANKSON = Jankson.builder().build(); + private final File configFile, oldConfigFile; + private ConfigObject config; + private boolean craftableOnly; + + public ConfigManager() { + this.oldConfigFile = new File(FabricLoader.getInstance().getConfigDirectory(), "rei.json"); + this.configFile = new File(FabricLoader.getInstance().getConfigDirectory(), "roughlyenoughitems/config.json"); + this.craftableOnly = false; + try { + loadConfig(); + RoughlyEnoughItemsCore.LOGGER.info("REI: Config is loaded."); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void saveConfig() throws IOException { + configFile.getParentFile().mkdirs(); + if (!configFile.exists() && !configFile.createNewFile()) { + RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to save config! Overwriting with default config."); + config = new ConfigObject(); + return; + } + try { + String result = JANKSON.toJson(config).toJson(true, true, 0); + if (!configFile.exists()) + configFile.createNewFile(); + FileOutputStream out = new FileOutputStream(configFile, false); + + out.write(result.getBytes()); + out.flush(); + out.close(); + } catch (Exception e) { + e.printStackTrace(); + RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to save config! Overwriting with default config."); + config = new ConfigObject(); + return; + } + } + + @Override + public void loadConfig() throws IOException { + configFile.getParentFile().mkdirs(); + if (!configFile.exists() && oldConfigFile.exists()) { + RoughlyEnoughItemsCore.LOGGER.info("REI: Detected old config file, trying to move it."); + try { + Files.move(oldConfigFile.toPath(), configFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + } catch (Exception e) { + e.printStackTrace(); + RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to move config file."); + } + } + if (!configFile.exists() || !configFile.canRead()) { + RoughlyEnoughItemsCore.LOGGER.warn("REI: Config not found! Creating one."); + config = new ConfigObject(); + saveConfig(); + return; + } + boolean failed = false; + try { + JsonObject configJson = JANKSON.load(configFile); + String regularized = configJson.toJson(false, false, 0); + + config = GSON.fromJson(regularized, ConfigObject.class); + } catch (Exception e) { + e.printStackTrace(); + failed = true; + } + if (failed || config == null) { + RoughlyEnoughItemsCore.LOGGER.error("REI: Failed to load config! Overwriting with default config."); + config = new ConfigObject(); + } + saveConfig(); + } + + @Override + public ConfigObject getConfig() { + return config; + } + + @Override + public boolean isCraftableOnlyEnabled() { + return craftableOnly; + } + + @Override + public void toggleCraftableOnly() { + craftableOnly = !craftableOnly; + } + +} -- cgit