Skip to content

Commit

Permalink
Fix crashing on startup when the config is corrupted
Browse files Browse the repository at this point in the history
  • Loading branch information
xpple committed Aug 19, 2023
1 parent ff666d2 commit 4aa47a6
Showing 1 changed file with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,29 @@
import java.io.IOException;
import java.lang.reflect.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

import static dev.xpple.betterconfig.BetterConfig.LOGGER;

public class BetterConfigInternals {

public static void init(ModConfigImpl modConfig) {
JsonObject root;
JsonObject root = null;
try (BufferedReader reader = Files.newBufferedReader(modConfig.getConfigsPath())) {
root = JsonParser.parseReader(reader).getAsJsonObject();
} catch (IOException e) {
root = new JsonObject();
} catch (IOException ignored) {
} catch (Exception e) {
LOGGER.warn("Could not read config file, default values will be used.");
LOGGER.warn("The old config file will be renamed.");
try {
Files.move(modConfig.getConfigsPath(), modConfig.getConfigsPath().resolveSibling("config_old.json"), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ignored) {
}
} finally {
root = Objects.requireNonNullElse(root, new JsonObject());
}

for (Field field : modConfig.getConfigsClass().getDeclaredFields()) {
Expand Down

0 comments on commit 4aa47a6

Please sign in to comment.