Skip to content

Commit f0e93ce

Browse files
committed
Improve config parser resiliency, closes #216
1 parent 0ecd21d commit f0e93ce

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

cardinal-components-base/src/main/java/org/ladysnake/cca/internal/base/ComponentsInternals.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@ public static void init() {
5454
try(Reader reader = Files.newBufferedReader(path)) {
5555
Properties cfg = new Properties();
5656
cfg.load(reader);
57-
if (Integer.parseInt(cfg.getProperty("config-version")) < configVersion) {
57+
try {
58+
if (Integer.parseInt(cfg.getProperty("config-version")) < configVersion) {
59+
writeConfigFile(path);
60+
cfg.clear();
61+
}
62+
logDeserializationWarnings = Boolean.parseBoolean(cfg.getProperty("log-deserialization-warnings", String.valueOf(DEFAULT_LOG_DESERIALIZATION_WARNINGS)));
63+
maxWarningsPerComponent = Integer.parseInt(cfg.getProperty("max-deserialization-warnings", String.valueOf(DEFAULT_MAX_WARNINGS_PER_COMPONENT)));
64+
} catch (NumberFormatException e) {
65+
LOGGER.error("Config file at {} contains invalid values, resetting", path, e);
5866
writeConfigFile(path);
59-
cfg.clear();
6067
}
61-
logDeserializationWarnings = Boolean.parseBoolean(cfg.getProperty("log-deserialization-warnings", String.valueOf(DEFAULT_LOG_DESERIALIZATION_WARNINGS)));
62-
maxWarningsPerComponent = Integer.parseInt(cfg.getProperty("max-deserialization-warnings", String.valueOf(DEFAULT_MAX_WARNINGS_PER_COMPONENT)));
63-
} catch (IOException e) {
68+
} catch (IOException | IllegalArgumentException e) {
69+
LOGGER.error("Invalid config file at {}, resetting", path, e);
6470
writeConfigFile(path);
6571
}
6672
}

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Starting with alpha-5, world data from previous minecraft versions should be mig
1818
- Components attached to a `LevelData` object (using the `cardinal-components-leveldata` module) can no longer use dynamic registries in serialization (vanilla limitation)
1919
- `ComponentContainer.Builder#build(String)` (had been deprecated for removal for a couple years now)
2020

21+
**Fixes**
22+
- Fixed game crashing when CCA's config file was invalid
23+
2124
------------------------------------------------------
2225
Version 7.3.1
2326
------------------------------------------------------

0 commit comments

Comments
 (0)