Skip to content

Commit ea401b1

Browse files
committed
Improve configuration system
1 parent 180d55b commit ea401b1

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

src/main/java/org/yatopiamc/c2me/common/config/C2MEConfig.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public class C2MEConfig {
2222
.build();
2323
config.load();
2424

25-
asyncIoConfig = new AsyncIoConfig(ConfigUtils.getValue(config, "asyncIO", CommentedConfig::inMemory, "Configuration for async io system"));
26-
threadedWorldGenConfig = new ThreadedWorldGenConfig(ConfigUtils.getValue(config, "threadedWorldGen", CommentedConfig::inMemory, "Configuration for threaded world generation"));
25+
final ConfigUtils.ConfigScope configScope = new ConfigUtils.ConfigScope(config);
26+
asyncIoConfig = new AsyncIoConfig(ConfigUtils.getValue(configScope, "asyncIO", CommentedConfig::inMemory, "Configuration for async io system"));
27+
threadedWorldGenConfig = new ThreadedWorldGenConfig(ConfigUtils.getValue(configScope, "threadedWorldGen", CommentedConfig::inMemory, "Configuration for threaded world generation"));
28+
configScope.removeUnusedKeys();
2729
config.save();
2830
config.close();
2931
C2MEMod.LOGGER.info("Configuration loaded successfully after {}ms", (System.nanoTime() - startTime) / 1_000_000.0);
@@ -35,8 +37,10 @@ public static class AsyncIoConfig {
3537

3638
public AsyncIoConfig(CommentedConfig config) {
3739
Preconditions.checkNotNull(config, "asyncIo config is not present");
38-
this.serializerParallelism = ConfigUtils.getValue(config, "serializerParallelism", () -> Math.min(2, Runtime.getRuntime().availableProcessors()), "IO worker executor parallelism", ConfigUtils.CheckType.THREAD_COUNT);
39-
this.ioWorkerParallelism = ConfigUtils.getValue(config, "ioWorkerParallelism", () -> Math.min(6, Runtime.getRuntime().availableProcessors()), "Serializer executor parallelism", ConfigUtils.CheckType.THREAD_COUNT);
40+
final ConfigUtils.ConfigScope configScope = new ConfigUtils.ConfigScope(config);
41+
this.serializerParallelism = ConfigUtils.getValue(configScope, "serializerParallelism", () -> Math.min(2, Runtime.getRuntime().availableProcessors()), "IO worker executor parallelism", ConfigUtils.CheckType.THREAD_COUNT);
42+
this.ioWorkerParallelism = ConfigUtils.getValue(configScope, "ioWorkerParallelism", () -> Math.min(6, Runtime.getRuntime().availableProcessors()), "Serializer executor parallelism", ConfigUtils.CheckType.THREAD_COUNT);
43+
configScope.removeUnusedKeys();
4044
}
4145
}
4246

@@ -47,9 +51,11 @@ public static class ThreadedWorldGenConfig {
4751

4852
public ThreadedWorldGenConfig(CommentedConfig config) {
4953
Preconditions.checkNotNull(config, "threadedWorldGen config is not present");
50-
this.enabled = ConfigUtils.getValue(config, "enabled", () -> true, "Whether to enable this feature");
51-
this.parallelism = ConfigUtils.getValue(config, "parallelism", () -> Math.min(6, Runtime.getRuntime().availableProcessors()), "World generation worker executor parallelism", ConfigUtils.CheckType.THREAD_COUNT);
52-
this.allowThreadedFeatures = ConfigUtils.getValue(config, "allowThreadedFeatures", () -> false, "Whether to allow feature generation (world decorations like trees, ores and etc.) run in parallel");
54+
final ConfigUtils.ConfigScope configScope = new ConfigUtils.ConfigScope(config);
55+
this.enabled = ConfigUtils.getValue(configScope, "enabled", () -> true, "Whether to enable this feature");
56+
this.parallelism = ConfigUtils.getValue(configScope, "parallelism", () -> Math.min(6, Runtime.getRuntime().availableProcessors()), "World generation worker executor parallelism", ConfigUtils.CheckType.THREAD_COUNT);
57+
this.allowThreadedFeatures = ConfigUtils.getValue(configScope, "allowThreadedFeatures", () -> false, "Whether to allow feature generation (world decorations like trees, ores and etc.) run in parallel");
58+
configScope.removeUnusedKeys();
5359
}
5460
}
5561

src/main/java/org/yatopiamc/c2me/common/config/ConfigUtils.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
11
package org.yatopiamc.c2me.common.config;
22

33
import com.electronwill.nightconfig.core.CommentedConfig;
4+
import com.electronwill.nightconfig.core.Config;
45
import com.google.common.base.Preconditions;
6+
import com.google.common.base.Supplier;
7+
import com.google.common.base.Suppliers;
58

69
import java.util.Arrays;
10+
import java.util.HashSet;
711
import java.util.Objects;
8-
import java.util.function.Supplier;
12+
import java.util.Set;
913

1014
public class ConfigUtils {
1115

12-
public static <T> T getValue(CommentedConfig config, String key, Supplier<T> def, String comment, CheckType... checks) {
16+
public static <T> T getValue(ConfigScope config, String key, Supplier<T> deff, String comment, CheckType... checks) {
1317
Preconditions.checkNotNull(config);
1418
Preconditions.checkNotNull(key);
1519
Preconditions.checkArgument(!key.isEmpty());
16-
Preconditions.checkNotNull(def);
17-
if (!config.contains(key) || (checks.length != 0 && Arrays.stream(checks).anyMatch(checkType -> !checkType.check(config.get(key))))) config.set(key, def.get());
18-
config.setComment(key, " " + comment);
19-
return Objects.requireNonNull(config.get(key));
20+
Preconditions.checkNotNull(deff);
21+
Supplier<T> def = Suppliers.memoize(deff);
22+
config.processedKeys.add(key);
23+
if (!config.config.contains(key) || (checks.length != 0 && Arrays.stream(checks).anyMatch(checkType -> !checkType.check(config.config.get(key)))))
24+
config.config.set(key, def.get());
25+
if (def.get() instanceof Config) config.config.setComment(key, String.format(" %s", comment));
26+
else config.config.setComment(key, String.format(" (Default: %s) %s", def.get(), comment));
27+
return Objects.requireNonNull(config.config.get(key));
28+
}
29+
30+
static class ConfigScope {
31+
final CommentedConfig config;
32+
final Set<String> processedKeys;
33+
34+
ConfigScope(CommentedConfig config) {
35+
this.config = config;
36+
this.processedKeys = new HashSet<>();
37+
}
38+
39+
void removeUnusedKeys() {
40+
config.entrySet().removeIf(entry -> !processedKeys.contains(entry.getKey()));
41+
}
2042
}
2143

2244
public enum CheckType {
23-
THREAD_COUNT {
45+
THREAD_COUNT() {
2446
@Override
2547
public <T> boolean check(T value) {
2648
return value instanceof Number && ((Number) value).intValue() >= 1 && ((Number) value).intValue() <= 0x7fff;

0 commit comments

Comments
 (0)