|
@@ -3,8 +3,14 @@ package malte0811.ferritecore.mixin.config;
|
|
import com.electronwill.nightconfig.core.ConfigSpec;
|
|
import com.electronwill.nightconfig.core.ConfigSpec;
|
|
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
|
import com.electronwill.nightconfig.core.io.ParsingException;
|
|
import com.electronwill.nightconfig.core.io.ParsingException;
|
|
|
|
+import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
|
|
|
+import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
|
|
|
+import malte0811.ferritecore.ModMain;
|
|
|
|
+import me.shedaniel.architectury.annotations.ExpectPlatform;
|
|
|
|
|
|
import javax.annotation.Nullable;
|
|
import javax.annotation.Nullable;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.Paths;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
@@ -48,19 +54,6 @@ public class FerriteConfig {
|
|
builder.finish();
|
|
builder.finish();
|
|
}
|
|
}
|
|
|
|
|
|
- private static CommentedFileConfig read(Path configPath) {
|
|
|
|
- final CommentedFileConfig configData = CommentedFileConfig.builder(configPath)
|
|
|
|
- .sync()
|
|
|
|
- .preserveInsertionOrder()
|
|
|
|
- .build();
|
|
|
|
- try {
|
|
|
|
- configData.load();
|
|
|
|
- } catch (ParsingException ex) {
|
|
|
|
- throw new RuntimeException("Failed to load config " + configPath, ex);
|
|
|
|
- }
|
|
|
|
- return configData;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
public static class ConfigBuilder {
|
|
public static class ConfigBuilder {
|
|
private final List<Option> options = new ArrayList<>();
|
|
private final List<Option> options = new ArrayList<>();
|
|
|
|
|
|
@@ -70,19 +63,40 @@ public class FerriteConfig {
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
- public void finish() {
|
|
|
|
- ConfigSpec spec = new ConfigSpec();
|
|
|
|
- for (Option o : options) {
|
|
|
|
- spec.define(o.getName(), true);
|
|
|
|
- }
|
|
|
|
- CommentedFileConfig configData = read(Paths.get("config", "ferritecore-mixin.toml"));
|
|
|
|
- for (Option o : options) {
|
|
|
|
- configData.setComment(o.getName(), o.getComment());
|
|
|
|
- }
|
|
|
|
- spec.correct(configData);
|
|
|
|
- configData.save();
|
|
|
|
- for (Option o : options) {
|
|
|
|
- o.set(configData::get);
|
|
|
|
|
|
+ private void finish() {
|
|
|
|
+ try {
|
|
|
|
+ Path config = Paths.get("config", ModMain.MODID+".mixin.properties");
|
|
|
|
+ if (!Files.exists(config))
|
|
|
|
+ Files.createFile(config);
|
|
|
|
+ List<String> lines = Files.readAllLines(config);
|
|
|
|
+ Object2BooleanMap<String> existingOptions = new Object2BooleanOpenHashMap<>();
|
|
|
|
+ for (String line : lines) {
|
|
|
|
+ line = line.trim();
|
|
|
|
+ if (line.isEmpty() || line.charAt(0) == '#') {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ final int indexOfEq = line.indexOf('=');
|
|
|
|
+ if (indexOfEq < 0) {
|
|
|
|
+ throw new RuntimeException("Invalid line "+line);
|
|
|
|
+ }
|
|
|
|
+ final String key = line.substring(0, indexOfEq).trim();
|
|
|
|
+ final String value = line.substring(indexOfEq + 1).trim();
|
|
|
|
+ existingOptions.put(key, Boolean.parseBoolean(value));
|
|
|
|
+ }
|
|
|
|
+ List<String> newLines = new ArrayList<>();
|
|
|
|
+ Object2BooleanMap<String> actualOptions = new Object2BooleanOpenHashMap<>();
|
|
|
|
+ for (Option o : options) {
|
|
|
|
+ final boolean value = existingOptions.getOrDefault(o.getName(), true);
|
|
|
|
+ actualOptions.put(o.getName(), value);
|
|
|
|
+ newLines.add("# "+o.getComment());
|
|
|
|
+ newLines.add(o.getName()+" = "+value);
|
|
|
|
+ }
|
|
|
|
+ for (Option o : options) {
|
|
|
|
+ o.set(actualOptions::getBoolean);
|
|
|
|
+ }
|
|
|
|
+ Files.write(config, newLines);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -100,7 +114,7 @@ public class FerriteConfig {
|
|
this.dependencies = Arrays.asList(dependencies);
|
|
this.dependencies = Arrays.asList(dependencies);
|
|
}
|
|
}
|
|
|
|
|
|
- void set(Predicate<String> isEnabled) {
|
|
|
|
|
|
+ public void set(Predicate<String> isEnabled) {
|
|
final boolean enabled = isEnabled.test(getName());
|
|
final boolean enabled = isEnabled.test(getName());
|
|
if (enabled) {
|
|
if (enabled) {
|
|
for (Option dep : dependencies) {
|
|
for (Option dep : dependencies) {
|