Procházet zdrojové kódy

Catch crashes due to invalid config file

Lortseam před 4 roky
rodič
revize
42faaa497f

+ 1 - 1
gradle.properties

@@ -7,7 +7,7 @@ org.gradle.jvmargs=-Xmx1G
 	loader_version=0.9.1+build.205
 
 # Mod Properties
-	mod_version = 0.4.1
+	mod_version = 0.4.2
 	maven_group = com.gitlab.Lortseam
 	archives_base_name = completeconfig
 

+ 13 - 4
src/main/java/me/lortseam/completeconfig/Config.java

@@ -2,23 +2,32 @@ package me.lortseam.completeconfig;
 
 import com.google.gson.GsonBuilder;
 import com.google.gson.JsonElement;
+import com.google.gson.JsonSyntaxException;
 import lombok.AccessLevel;
 import lombok.RequiredArgsConstructor;
 import me.lortseam.completeconfig.api.ConfigCategory;
 import me.lortseam.completeconfig.collection.CollectionMap;
 import me.lortseam.completeconfig.serialization.CollectionMapDeserializer;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 @RequiredArgsConstructor(access = AccessLevel.PACKAGE)
 public class Config extends CollectionMap {
 
+    private static final Logger LOGGER = LogManager.getLogger();
+
     private final JsonElement json;
 
     void registerTopLevelCategory(ConfigCategory category) {
         fill(category);
-        new GsonBuilder()
-                .registerTypeAdapter(CollectionMapDeserializer.TYPE, new CollectionMapDeserializer(this, category.getConfigCategoryID()))
-                .create()
-                .fromJson(json, CollectionMapDeserializer.TYPE);
+        try {
+            new GsonBuilder()
+                    .registerTypeAdapter(CollectionMapDeserializer.TYPE, new CollectionMapDeserializer(this, category.getConfigCategoryID()))
+                    .create()
+                    .fromJson(json, CollectionMapDeserializer.TYPE);
+        } catch (JsonSyntaxException e) {
+            LOGGER.warn("An error occurred while trying to load the config for category " + category.getClass());
+        }
     }
 
 }

+ 9 - 4
src/main/java/me/lortseam/completeconfig/ConfigManager.java

@@ -1,9 +1,6 @@
 package me.lortseam.completeconfig;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonNull;
+import com.google.gson.*;
 import me.lortseam.completeconfig.api.ConfigCategory;
 import me.lortseam.completeconfig.gui.GuiBuilder;
 import me.lortseam.completeconfig.gui.GuiRegistry;
@@ -12,6 +9,8 @@ import me.lortseam.completeconfig.serialization.EntrySerializer;
 import me.shedaniel.clothconfig2.api.ConfigBuilder;
 import net.fabricmc.loader.api.FabricLoader;
 import net.minecraft.client.gui.screen.Screen;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.io.*;
 import java.nio.file.Files;
@@ -21,11 +20,15 @@ import java.util.function.Supplier;
 
 public final class ConfigManager {
 
+    private static final Logger LOGGER = LogManager.getLogger();
+
+    private final String modID;
     private final Path jsonPath;
     private final Config config;
     private final GuiBuilder guiBuilder;
 
     ConfigManager(String modID) {
+        this.modID = modID;
         jsonPath = Paths.get(FabricLoader.getInstance().getConfigDir().toString(), modID + ".json");
         config = new Config(load());
         guiBuilder = new GuiBuilder(modID, config);
@@ -37,6 +40,8 @@ public final class ConfigManager {
                 return new Gson().fromJson(new FileReader(jsonPath.toString()), JsonElement.class);
             } catch (FileNotFoundException e) {
                 throw new RuntimeException(e);
+            } catch (JsonSyntaxException e) {
+                LOGGER.warn("An error occurred while trying to load the config for mod " + modID);
             }
         }
         return JsonNull.INSTANCE;

+ 9 - 1
src/main/java/me/lortseam/completeconfig/serialization/EntryDeserializer.java

@@ -6,11 +6,14 @@ import com.google.gson.JsonElement;
 import com.google.gson.JsonParseException;
 import com.google.gson.reflect.TypeToken;
 import me.lortseam.completeconfig.entry.Entry;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.lang.reflect.Type;
 
 public class EntryDeserializer<T> implements JsonDeserializer<Entry<T>> {
 
+    private static final Logger LOGGER = LogManager.getLogger();
     public static final Type TYPE = new TypeToken<Entry<?>>() {}.getType();
 
     private final Entry<T> configEntry;
@@ -21,7 +24,12 @@ public class EntryDeserializer<T> implements JsonDeserializer<Entry<T>> {
 
     @Override
     public Entry<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
-        configEntry.setValue(context.deserialize(json, configEntry.getType()));
+        try {
+            T value = context.deserialize(json, configEntry.getType());
+            configEntry.setValue(value);
+        } catch (JsonParseException e) {
+            LOGGER.warn("An error occurred while trying to load the config entry's value of field " + configEntry.getField() + ": " + e.getMessage());
+        }
         return configEntry;
     }