|
| 1 | +package io.arona74.crlayers; |
| 2 | + |
| 3 | +import com.google.gson.Gson; |
| 4 | +import com.google.gson.GsonBuilder; |
| 5 | +import com.google.gson.JsonObject; |
| 6 | +import net.fabricmc.loader.api.FabricLoader; |
| 7 | + |
| 8 | +import java.io.*; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.nio.file.Path; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | + |
| 14 | +/** |
| 15 | + * Utility class for loading mapping configurations from JSON files |
| 16 | + */ |
| 17 | +public class ConfigLoader { |
| 18 | + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); |
| 19 | + private static final Path CONFIG_DIR = FabricLoader.getInstance().getConfigDir().resolve("crlayers"); |
| 20 | + |
| 21 | + /** |
| 22 | + * Load mappings from a JSON config file |
| 23 | + * First tries to load from the config directory, then falls back to resources |
| 24 | + * @param configFileName The name of the config file (e.g., "block_mappings.json") |
| 25 | + * @return Map of vanilla block IDs to Conquest Reforged block IDs |
| 26 | + */ |
| 27 | + public static Map<String, String> loadMappings(String configFileName) { |
| 28 | + Map<String, String> mappings = new HashMap<>(); |
| 29 | + |
| 30 | + // First try to load from external config directory |
| 31 | + Path externalConfigPath = CONFIG_DIR.resolve(configFileName); |
| 32 | + if (Files.exists(externalConfigPath)) { |
| 33 | + try { |
| 34 | + mappings = loadMappingsFromFile(externalConfigPath); |
| 35 | + CRLayers.LOGGER.info("Loaded mappings from external config: {}", externalConfigPath); |
| 36 | + return mappings; |
| 37 | + } catch (IOException e) { |
| 38 | + CRLayers.LOGGER.error("Failed to load external config file: {}", externalConfigPath, e); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + // Fall back to resource file |
| 43 | + try { |
| 44 | + mappings = loadMappingsFromResource(configFileName); |
| 45 | + CRLayers.LOGGER.info("Loaded mappings from resource: {}", configFileName); |
| 46 | + |
| 47 | + // Create external config file for user customization |
| 48 | + createExternalConfig(configFileName, mappings); |
| 49 | + } catch (IOException e) { |
| 50 | + CRLayers.LOGGER.error("Failed to load resource config file: {}", configFileName, e); |
| 51 | + } |
| 52 | + |
| 53 | + return mappings; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Load mappings from a file path |
| 58 | + */ |
| 59 | + private static Map<String, String> loadMappingsFromFile(Path filePath) throws IOException { |
| 60 | + try (Reader reader = Files.newBufferedReader(filePath)) { |
| 61 | + return parseMappingsJson(reader); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Load mappings from a resource file |
| 67 | + */ |
| 68 | + private static Map<String, String> loadMappingsFromResource(String resourceName) throws IOException { |
| 69 | + InputStream inputStream = ConfigLoader.class.getClassLoader().getResourceAsStream(resourceName); |
| 70 | + if (inputStream == null) { |
| 71 | + throw new IOException("Resource not found: " + resourceName); |
| 72 | + } |
| 73 | + |
| 74 | + try (Reader reader = new InputStreamReader(inputStream)) { |
| 75 | + return parseMappingsJson(reader); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Parse JSON mappings from a reader |
| 81 | + */ |
| 82 | + private static Map<String, String> parseMappingsJson(Reader reader) { |
| 83 | + Map<String, String> mappings = new HashMap<>(); |
| 84 | + |
| 85 | + JsonObject root = GSON.fromJson(reader, JsonObject.class); |
| 86 | + if (root.has("mappings")) { |
| 87 | + JsonObject mappingsObj = root.getAsJsonObject("mappings"); |
| 88 | + for (String key : mappingsObj.keySet()) { |
| 89 | + mappings.put(key, mappingsObj.get(key).getAsString()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return mappings; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Create an external config file for user customization |
| 98 | + */ |
| 99 | + private static void createExternalConfig(String configFileName, Map<String, String> defaultMappings) { |
| 100 | + try { |
| 101 | + // Create config directory if it doesn't exist |
| 102 | + if (!Files.exists(CONFIG_DIR)) { |
| 103 | + Files.createDirectories(CONFIG_DIR); |
| 104 | + CRLayers.LOGGER.info("Created config directory: {}", CONFIG_DIR); |
| 105 | + } |
| 106 | + |
| 107 | + Path configPath = CONFIG_DIR.resolve(configFileName); |
| 108 | + |
| 109 | + // Only create if it doesn't exist |
| 110 | + if (!Files.exists(configPath)) { |
| 111 | + JsonObject root = new JsonObject(); |
| 112 | + root.addProperty("_comment", |
| 113 | + "This file can be edited to customize mappings. Changes will be loaded on next startup."); |
| 114 | + |
| 115 | + JsonObject mappingsObj = new JsonObject(); |
| 116 | + for (Map.Entry<String, String> entry : defaultMappings.entrySet()) { |
| 117 | + mappingsObj.addProperty(entry.getKey(), entry.getValue()); |
| 118 | + } |
| 119 | + root.add("mappings", mappingsObj); |
| 120 | + |
| 121 | + try (Writer writer = Files.newBufferedWriter(configPath)) { |
| 122 | + GSON.toJson(root, writer); |
| 123 | + } |
| 124 | + |
| 125 | + CRLayers.LOGGER.info("Created default config file: {}", configPath); |
| 126 | + } |
| 127 | + } catch (IOException e) { |
| 128 | + CRLayers.LOGGER.error("Failed to create external config file", e); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Save mappings to an external config file |
| 134 | + */ |
| 135 | + public static void saveMappings(String configFileName, Map<String, String> mappings) throws IOException { |
| 136 | + if (!Files.exists(CONFIG_DIR)) { |
| 137 | + Files.createDirectories(CONFIG_DIR); |
| 138 | + } |
| 139 | + |
| 140 | + Path configPath = CONFIG_DIR.resolve(configFileName); |
| 141 | + |
| 142 | + JsonObject root = new JsonObject(); |
| 143 | + root.addProperty("_comment", |
| 144 | + "This file can be edited to customize mappings. Changes will be loaded on next startup."); |
| 145 | + |
| 146 | + JsonObject mappingsObj = new JsonObject(); |
| 147 | + for (Map.Entry<String, String> entry : mappings.entrySet()) { |
| 148 | + mappingsObj.addProperty(entry.getKey(), entry.getValue()); |
| 149 | + } |
| 150 | + root.add("mappings", mappingsObj); |
| 151 | + |
| 152 | + try (Writer writer = Files.newBufferedWriter(configPath)) { |
| 153 | + GSON.toJson(root, writer); |
| 154 | + } |
| 155 | + |
| 156 | + CRLayers.LOGGER.info("Saved mappings to config file: {}", configPath); |
| 157 | + } |
| 158 | +} |
0 commit comments