|
@@ -1,10 +1,6 @@
|
|
package me.lortseam.completeconfig;
|
|
package me.lortseam.completeconfig;
|
|
|
|
|
|
-import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
-import com.fasterxml.jackson.databind.module.SimpleModule;
|
|
|
|
-import com.fasterxml.jackson.databind.node.NullNode;
|
|
|
|
-import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
|
|
|
|
|
|
+import com.google.gson.*;
|
|
import me.lortseam.completeconfig.api.ConfigCategory;
|
|
import me.lortseam.completeconfig.api.ConfigCategory;
|
|
import me.lortseam.completeconfig.api.ConfigOwner;
|
|
import me.lortseam.completeconfig.api.ConfigOwner;
|
|
import me.lortseam.completeconfig.gui.GuiBuilder;
|
|
import me.lortseam.completeconfig.gui.GuiBuilder;
|
|
@@ -18,6 +14,7 @@ import org.apache.commons.lang3.ArrayUtils;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.LogManager;
|
|
import org.apache.logging.log4j.Logger;
|
|
import org.apache.logging.log4j.Logger;
|
|
|
|
|
|
|
|
+import java.io.FileNotFoundException;
|
|
import java.io.FileReader;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.io.Writer;
|
|
import java.io.Writer;
|
|
@@ -28,10 +25,11 @@ import java.util.*;
|
|
|
|
|
|
public final class ConfigHandler {
|
|
public final class ConfigHandler {
|
|
|
|
|
|
- private static final ObjectMapper MAPPER = new YAMLMapper().registerModule(new SimpleModule()
|
|
|
|
- .addSerializer(CollectionSerializer.TYPE, new CollectionSerializer())
|
|
|
|
- .addSerializer(EntrySerializer.TYPE, new EntrySerializer())
|
|
|
|
- );
|
|
|
|
|
|
+ private static final Gson GSON = new GsonBuilder()
|
|
|
|
+ .registerTypeAdapter(CollectionSerializer.TYPE, new CollectionSerializer())
|
|
|
|
+ .registerTypeAdapter(EntrySerializer.TYPE, new EntrySerializer())
|
|
|
|
+ .setPrettyPrinting()
|
|
|
|
+ .create();
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
private static final Logger LOGGER = LogManager.getLogger();
|
|
private static final Map<Class<? extends ConfigOwner>, ConfigHandler> HANDLERS = new HashMap<>();
|
|
private static final Map<Class<? extends ConfigOwner>, ConfigHandler> HANDLERS = new HashMap<>();
|
|
|
|
|
|
@@ -52,7 +50,7 @@ public final class ConfigHandler {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
String[] subPath = ArrayUtils.add(branch, 0, modID);
|
|
String[] subPath = ArrayUtils.add(branch, 0, modID);
|
|
- subPath[subPath.length - 1] = subPath[subPath.length - 1] + ".yaml";
|
|
|
|
|
|
+ subPath[subPath.length - 1] = subPath[subPath.length - 1] + ".json";
|
|
Path jsonPath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), subPath);
|
|
Path jsonPath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), subPath);
|
|
if (HANDLERS.values().stream().anyMatch(handler -> handler.jsonPath.equals(jsonPath))) {
|
|
if (HANDLERS.values().stream().anyMatch(handler -> handler.jsonPath.equals(jsonPath))) {
|
|
throw new IllegalArgumentException("A config of the mod " + modID + " with the specified branch " + Arrays.toString(branch) + " already exists!");
|
|
throw new IllegalArgumentException("A config of the mod " + modID + " with the specified branch " + Arrays.toString(branch) + " already exists!");
|
|
@@ -82,15 +80,17 @@ public final class ConfigHandler {
|
|
this.guiBuilder = guiBuilder;
|
|
this.guiBuilder = guiBuilder;
|
|
}
|
|
}
|
|
|
|
|
|
- private JsonNode load() {
|
|
|
|
|
|
+ private JsonElement load() {
|
|
if(Files.exists(jsonPath)) {
|
|
if(Files.exists(jsonPath)) {
|
|
try {
|
|
try {
|
|
- return MAPPER.readTree(new FileReader(jsonPath.toString()));
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
|
+ return GSON.fromJson(new FileReader(jsonPath.toString()), JsonElement.class);
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } catch (JsonSyntaxException e) {
|
|
LOGGER.warn("[CompleteConfig] An error occurred while trying to load the config " + jsonPath.toString());
|
|
LOGGER.warn("[CompleteConfig] An error occurred while trying to load the config " + jsonPath.toString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return NullNode.instance;
|
|
|
|
|
|
+ return JsonNull.INSTANCE;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -124,7 +124,7 @@ public final class ConfigHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
try(Writer writer = Files.newBufferedWriter(jsonPath)) {
|
|
try(Writer writer = Files.newBufferedWriter(jsonPath)) {
|
|
- MAPPER.writeValue(writer, config);
|
|
|
|
|
|
+ GSON.toJson(config, writer);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|