From fee71e8be9e29494c28979c2fe5b044c0b2c949b Mon Sep 17 00:00:00 2001 From: makamys Date: Thu, 16 Feb 2023 02:22:01 +0100 Subject: [PATCH] Refactor config parsing into common module --- module-common/LICENSE | 24 +++ module-common/build.gradle | 1 + .../unimixins/common/AnnotatedProperties.java | 138 ++++++++++++++++++ module-compat/build.gradle | 15 +- .../unimixins/compat/CompatConfig.java | 33 +---- .../unimixins/compat/CompatCore.java | 4 +- settings.gradle | 3 +- 7 files changed, 185 insertions(+), 33 deletions(-) create mode 100644 module-common/LICENSE create mode 100644 module-common/build.gradle create mode 100644 module-common/src/main/java/io/github/legacymoddingmc/unimixins/common/AnnotatedProperties.java diff --git a/module-common/LICENSE b/module-common/LICENSE new file mode 100644 index 0000000..68a49da --- /dev/null +++ b/module-common/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/module-common/build.gradle b/module-common/build.gradle new file mode 100644 index 0000000..fe4ddad --- /dev/null +++ b/module-common/build.gradle @@ -0,0 +1 @@ +archivesBaseName = archivesBaseName.replace("-1.7.10", "-common-1.7.10") diff --git a/module-common/src/main/java/io/github/legacymoddingmc/unimixins/common/AnnotatedProperties.java b/module-common/src/main/java/io/github/legacymoddingmc/unimixins/common/AnnotatedProperties.java new file mode 100644 index 0000000..abccb19 --- /dev/null +++ b/module-common/src/main/java/io/github/legacymoddingmc/unimixins/common/AnnotatedProperties.java @@ -0,0 +1,138 @@ +package io.github.legacymoddingmc.unimixins.common; + +import java.io.*; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.reflect.Field; +import java.util.*; + +public class AnnotatedProperties { + public static void load(File file, Class cls) { + Map propertyTokens = readPropertyTokens(file); + + try { + for (Field f : cls.getFields()) { + if (f.isAnnotationPresent(ConfigString.class)) { + ConfigString ann = f.getAnnotation(ConfigString.class); + List comment = Arrays.asList(ann.com().split("\n")); + String def = ann.def(); + String cat = ann.cat(); + + String key = cat + "." + f.getName(); + + PropertyToken prop = propertyTokens.get(key); + if (prop == null) { + prop = new PropertyToken(comment, key, def); + } + + setFieldValue(f, prop.value); + + propertyTokens.put(key, prop); + } + } + } catch(Exception e) { + e.printStackTrace(); + } + + writePropertyTokens(file, propertyTokens); + } + + private static void setFieldValue(Field f, String value) throws Exception { + if(f.getType() == boolean.class) { + f.set(null, Boolean.parseBoolean(value)); + } else { + throw new UnsupportedOperationException(); + } + } + + private static void writePropertyTokens(File file, Map propertyTokens) { + List keys = new ArrayList<>(); + keys.addAll(propertyTokens.keySet()); + Collections.sort(keys); + + try(BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { + for(String k : keys) { + PropertyToken token = propertyTokens.get(k); + + for(String commentLine : token.comment) { + bw.write("# " + commentLine + "\n"); + } + bw.write(token.key + "=" + token.value + "\n\n"); + } + } catch(Exception e) { + e.printStackTrace(); + } + } + + private static Map readPropertyTokens(File file) { + List comment = new ArrayList<>(); + String prop = ""; + + Map tokens = new HashMap<>(); + + if(file.exists()) { + try (BufferedReader br = new BufferedReader(new FileReader(file))) { + while (true) { + String line = br.readLine(); + if (line == null) break; + if (line.isEmpty()) continue; + + line = line.trim(); + + if (line.startsWith("#")) { + comment.add(line.substring(1).trim()); + } else { + prop = line; + + PropertyToken token = new PropertyToken(comment, prop); + tokens.put(token.key, token); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + return tokens; + } + + private static class PropertyToken { + + public List comment; + public String key; + public String value; + + public PropertyToken(List comment, String propertyString) { + this.comment = comment; + String[] pair = propertyString.split("="); + if(pair.length != 2) { + throw new RuntimeException(); + } else { + key = pair[0]; + value = pair[1]; + } + } + + public PropertyToken(List comment, String key, String value) { + this.comment = comment; + this.key = key; + this.value = value; + } + + } + + @Retention(RetentionPolicy.RUNTIME) + @Target(ElementType.FIELD) + public @interface ConfigString { + + /** Default value */ + String def(); + /** Comment */ + String com() default ""; + /** Category (the property name gets prefixed with this) */ + String cat(); + + } +} diff --git a/module-compat/build.gradle b/module-compat/build.gradle index 8ecaf19..7a37028 100644 --- a/module-compat/build.gradle +++ b/module-compat/build.gradle @@ -1,10 +1,20 @@ dependencies { compileOnly('org.spongepowered:mixin:0.8.5') + shadow(project(":module-common")) { + transitive = false + } } archivesBaseName = archivesBaseName.replace("-1.7.10", "-compat-1.7.10") -jar { +apply plugin: "com.github.johnrengelman.shadow" + + +shadowJar { + configurations = [project.configurations.shadow] + classifier = '' + relocate 'io.github.legacymoddingmc.unimixins.common', 'io.github.legacymoddingmc.unimixins.compat.repackage.common' + manifest { attributes ( 'TweakClass': 'org.spongepowered.asm.launch.MixinTweaker', @@ -14,3 +24,6 @@ jar { ) } } + +jar.dependsOn shadowJar +jar.enabled = false diff --git a/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatConfig.java b/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatConfig.java index e9a385b..e1910ba 100644 --- a/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatConfig.java +++ b/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatConfig.java @@ -1,37 +1,10 @@ package io.github.legacymoddingmc.unimixins.compat; -import net.minecraft.launchwrapper.Launch; - -import java.io.File; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.util.Properties; +import io.github.legacymoddingmc.unimixins.common.AnnotatedProperties; public class CompatConfig { - private static final Properties PROPS = new Properties(); - + @AnnotatedProperties.ConfigString(def = "true", com = "Remap references to Mixin's shaded ASM.", cat = "compat") public static boolean enableRemapper; + @AnnotatedProperties.ConfigString(def = "true", com = "Include Mixin errors in crash reports.", cat = "compat") public static boolean enhanceCrashReports; - - public static void load() { - try(FileReader fr = new FileReader(new File(Launch.minecraftHome, "config/unimixins.properties"))) { - PROPS.load(fr); - } catch(IOException e){ - e.printStackTrace(); - } - - applyProperties(); - - try(FileWriter fw = new FileWriter(new File(Launch.minecraftHome, "config/unimixins.properties"))) { - PROPS.store(fw, ""); - } catch(IOException e){ - e.printStackTrace(); - } - } - - private static void applyProperties() { - enableRemapper = Boolean.parseBoolean(PROPS.getProperty("unimixins.compat.remap", "true")); - enhanceCrashReports = Boolean.parseBoolean(PROPS.getProperty("unimixins.compat.enhanceCrashReports", "true")); - } } diff --git a/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatCore.java b/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatCore.java index 326bb7c..99215ad 100644 --- a/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatCore.java +++ b/module-compat/src/main/java/io/github/legacymoddingmc/unimixins/compat/CompatCore.java @@ -1,9 +1,11 @@ package io.github.legacymoddingmc.unimixins.compat; +import java.io.File; import java.util.*; import cpw.mods.fml.relauncher.IFMLLoadingPlugin; import cpw.mods.fml.relauncher.IFMLLoadingPlugin.MCVersion; +import io.github.legacymoddingmc.unimixins.common.AnnotatedProperties; import net.minecraft.launchwrapper.Launch; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -17,7 +19,7 @@ public class CompatCore implements IFMLLoadingPlugin { public CompatCore() { LOGGER.info("Instantiating CompatCore"); - CompatConfig.load(); + AnnotatedProperties.load(new File(Launch.minecraftHome, "config/unimixins.properties"), CompatConfig.class); if(CompatConfig.enableRemapper) { // We register the transformer this way to register it as early as possible. diff --git a/settings.gradle b/settings.gradle index 41a3ce9..a3696c9 100644 --- a/settings.gradle +++ b/settings.gradle @@ -7,4 +7,5 @@ include 'module-mixinbooterlegacy' include 'module-gasstation' include 'module-gtnhmixins' include 'module-mixinextras' -include 'module-all' \ No newline at end of file +include 'module-all' +include 'module-common'