Browse Source

Make loader-specific code in Mixin config work

malte0811 4 years ago
parent
commit
8d0ee4e977

+ 5 - 35
common/src/main/java/malte0811/ferritecore/mixin/config/FerriteConfig.java

@@ -1,15 +1,12 @@
 package malte0811.ferritecore.mixin.config;
 
-import com.electronwill.nightconfig.core.ConfigSpec;
-import com.electronwill.nightconfig.core.file.CommentedFileConfig;
-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 java.io.IOException;
+import java.lang.reflect.Method;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -65,37 +62,10 @@ public class FerriteConfig {
 
         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) {
+                Class<?> handler = Class.forName("malte0811.ferritecore.mixin.config.ConfigFileHandler");
+                Method finish = handler.getMethod("finish", List.class);
+                finish.invoke(null, options);
+            } catch (Exception e) {
                 throw new RuntimeException(e);
             }
         }

+ 1 - 1
common/src/main/resources/ferritecore.blockstatecache.mixin.json

@@ -2,7 +2,7 @@
   "required": true,
   "package": "malte0811.ferritecore.mixin.blockstatecache",
   "compatibilityLevel": "JAVA_8",
-  "refmap": "ferritecore.refmap.json",
+  "refmap": "ferritecore-common-refmap.json",
   "client": [
   ],
   "injectors": {

+ 1 - 1
common/src/main/resources/ferritecore.dedupmultipart.mixin.json

@@ -2,7 +2,7 @@
   "required": true,
   "package": "malte0811.ferritecore.mixin.dedupmultipart",
   "compatibilityLevel": "JAVA_8",
-  "refmap": "ferritecore.refmap.json",
+  "refmap": "ferritecore-common-refmap.json",
   "injectors": {
     "defaultRequire": 1
   },

+ 1 - 1
common/src/main/resources/ferritecore.fastmap.mixin.json

@@ -2,7 +2,7 @@
   "required": true,
   "package": "malte0811.ferritecore.mixin.fastmap",
   "compatibilityLevel": "JAVA_8",
-  "refmap": "ferritecore.refmap.json",
+  "refmap": "ferritecore-common-refmap.json",
   "mixins": [
     "FastMapStateHolderMixin"
   ],

+ 1 - 1
common/src/main/resources/ferritecore.mrl.mixin.json

@@ -2,7 +2,7 @@
   "required": true,
   "package": "malte0811.ferritecore.mixin.mrl",
   "compatibilityLevel": "JAVA_8",
-  "refmap": "ferritecore.refmap.json",
+  "refmap": "ferritecore-common-refmap.json",
   "client": [
     "ModelResourceLocationMixin",
     "ResourceLocationAccess"

+ 1 - 2
common/src/main/resources/ferritecore.nopropertymap.mixin.json

@@ -2,9 +2,8 @@
   "required": true,
   "package": "malte0811.ferritecore.mixin.nopropertymap",
   "compatibilityLevel": "JAVA_8",
-  "refmap": "ferritecore.refmap.json",
+  "refmap": "ferritecore-common-refmap.json",
   "mixins": [
-    "DummyFerriteValuesMixin",
     "NoPropertyStateHolderMixin"
   ],
   "injectors": {

+ 1 - 1
common/src/main/resources/ferritecore.predicates.mixin.json

@@ -2,7 +2,7 @@
   "required": true,
   "package": "malte0811.ferritecore.mixin.predicates",
   "compatibilityLevel": "JAVA_8",
-  "refmap": "ferritecore.refmap.json",
+  "refmap": "ferritecore-common-refmap.json",
   "client": [
     "AndConditionMixin",
     "OrConditionMixin",

+ 41 - 0
fabric/src/main/java/malte0811/ferritecore/mixin/config/ConfigFileHandler.java

@@ -0,0 +1,41 @@
+package malte0811.ferritecore.mixin.config;
+
+import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
+import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
+import malte0811.ferritecore.ModMain;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+public class ConfigFileHandler {
+    // Called reflectively from FerriteConfig
+    public static void finish(List<FerriteConfig.Option> options) throws IOException {
+        Path config = Paths.get("config", ModMain.MODID + ".mixin.properties");
+        if (!Files.exists(config))
+            Files.createFile(config);
+        Properties propsInFile = new Properties();
+        propsInFile.load(Files.newInputStream(config));
+        Object2BooleanMap<String> existingOptions = new Object2BooleanOpenHashMap<>();
+        for (String key : propsInFile.stringPropertyNames()) {
+            existingOptions.put(key, Boolean.parseBoolean(propsInFile.getProperty(key)));
+        }
+        List<String> newLines = new ArrayList<>();
+        Object2BooleanMap<String> actualOptions = new Object2BooleanOpenHashMap<>();
+        // Write data back manually, we can't put comments on individual values using Properties
+        for (FerriteConfig.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 (FerriteConfig.Option o : options) {
+            o.set(actualOptions::getBoolean);
+        }
+        Files.write(config, newLines);
+    }
+}

+ 11 - 0
forge/build.gradle

@@ -1,3 +1,5 @@
+import java.util.stream.Collectors
+
 plugins {
     id "com.github.johnrengelman.shadow" version "5.0.0"
 }
@@ -50,3 +52,12 @@ remapJar {
     input.set(shadowJar.archivePath)
     classifier "forge"
 }
+
+//TODO this is also in the other build.gradle…
+def fcMixinConfigs = ["predicates", "fastmap", "nopropertymap", "mrl", "dedupmultipart", "blockstatecache"].stream()
+        .map({s -> archivesBaseName+"."+s+".mixin.json"})
+        .collect(Collectors.toList())
+
+loom {
+    mixinConfigs = fcMixinConfigs
+}

+ 39 - 0
forge/src/main/java/malte0811/ferritecore/mixin/config/ConfigFileHandler.java

@@ -0,0 +1,39 @@
+package malte0811.ferritecore.mixin.config;
+
+import com.electronwill.nightconfig.core.ConfigSpec;
+import com.electronwill.nightconfig.core.file.CommentedFileConfig;
+import com.electronwill.nightconfig.core.io.ParsingException;
+import malte0811.ferritecore.ModMain;
+import malte0811.ferritecore.mixin.config.FerriteConfig.Option;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+
+public class ConfigFileHandler {
+    // Called reflectively from FerriteConfig
+    public static void finish(List<Option> options) {
+        ConfigSpec spec = new ConfigSpec();
+        for (Option o : options) {
+            spec.define(o.getName(), true);
+        }
+        CommentedFileConfig configData = read(Paths.get("config", ModMain.MODID+"-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 static CommentedFileConfig read(Path configPath) {
+        final CommentedFileConfig configData = CommentedFileConfig.builder(configPath)
+                .sync()
+                .preserveInsertionOrder()
+                .build();
+        configData.load();
+        return configData;
+    }
+}