Refactor config parsing to only write if config is dirty

This commit is contained in:
makamys committed 2023-02-17 01:38:13 +01:00
1 parent ddb02e692f
commit ceba1109ea
13 files changed
+244 -155

No files matched your search

@@ -1,146 +0,0 @@
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<String, PropertyToken> propertyTokens = readPropertyTokens(file);
try {
for (Field f : cls.getFields()) {
if (f.isAnnotationPresent(ConfigString.class)) {
ConfigString ann = f.getAnnotation(ConfigString.class);
List<String> 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);
}
prop.defaultValue = def;
prop.comment.removeIf(s -> s.startsWith("[default:"));
prop.comment.add("[default: " + prop.defaultValue + "]");
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<String, PropertyToken> propertyTokens) {
List<String> 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<String, PropertyToken> readPropertyTokens(File file) {
List<String> comment = new ArrayList<>();
String prop = "";
Map<String, PropertyToken> 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);
comment.clear();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return tokens;
}
private static class PropertyToken {
public List<String> comment;
public String key;
public String value;
public String defaultValue;
public PropertyToken(List<String> comment, String propertyString) {
this.comment = new ArrayList<>(comment);
String[] pair = propertyString.split("=");
if(pair.length != 2) {
throw new RuntimeException();
} else {
key = pair[0];
value = pair[1];
}
}
public PropertyToken(List<String> comment, String key, String value) {
this.comment = new ArrayList<>(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();
}
}
@@ -0,0 +1,68 @@
package io.github.legacymoddingmc.unimixins.common.config;
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) {
PropertyCollection properties = PropertyCollection.fromFile(file);
try {
for (Field f : cls.getFields()) {
if (f.isAnnotationPresent(ConfigString.class)) {
ConfigString ann = f.getAnnotation(ConfigString.class);
List<String> comment = new ArrayList<>(Arrays.asList(ann.com().split("\n")));
String def = ann.def();
String cat = ann.cat();
comment.removeIf(s -> s.startsWith("[default:"));
comment.add("[default: " + def + "]");
String key = cat + "." + f.getName();
PropertyToken prop = properties.get(key);
if (prop == null) {
prop = new PropertyToken(comment, key, def);
prop.setDirty();
} else {
prop.setComment(comment);
}
setFieldValue(f, prop.getValue());
properties.put(key, prop);
}
}
} catch(Exception e) {
e.printStackTrace();
}
properties.writeToFile(file);
}
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();
}
}
@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();
}
}
@@ -1,4 +1,4 @@
package io.github.legacymoddingmc.unimixins.common;
package io.github.legacymoddingmc.unimixins.common.config;
import net.minecraft.launchwrapper.Launch;
@@ -0,0 +1,86 @@
package io.github.legacymoddingmc.unimixins.common.config;
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
class PropertyCollection {
// We use a TreeMap so keys are automatically sorted
private Map<String, PropertyToken> properties = new TreeMap<>();
public static PropertyCollection fromFile(File file) {
PropertyCollection coll = new PropertyCollection();
coll.readPropertyTokens(file);
return coll;
}
private void readPropertyTokens(File file) {
List<String> comment = new ArrayList<>();
String prop = "";
List<String> keyOrder = new ArrayList<>();
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);
properties.put(token.getKey(), token);
keyOrder.add(token.getKey());
comment.clear();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void writeToFile(File file) {
writePropertyTokens(file);
}
private void writePropertyTokens(File file) {
boolean dirty = properties.values().stream().anyMatch(PropertyToken::isDirty);
if(dirty) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
for (Map.Entry<String, PropertyToken> e : properties.entrySet()) {
PropertyToken token = e.getValue();
for (String commentLine : token.getComment()) {
bw.write("# " + commentLine + "\n");
}
bw.write(token.getKey() + "=" + token.getValue() + "\n\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public PropertyToken get(String key) {
return properties.get(key);
}
public void put(String key, PropertyToken token) {
PropertyToken old = properties.get(key);
if (!token.equals(old)) {
properties.put(key, token);
token.setDirty();
}
}
}
@@ -0,0 +1,81 @@
package io.github.legacymoddingmc.unimixins.common.config;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.List;
public class PropertyToken {
private List<String> comment;
private String key;
private String value;
private boolean dirty;
public PropertyToken(List<String> comment, String propertyString) {
this.comment = ImmutableList.copyOf(comment);
String[] pair = propertyString.split("=");
if (pair.length != 2) {
throw new RuntimeException();
} else {
key = pair[0];
value = pair[1];
}
}
public PropertyToken(List<String> comment, String key, String value) {
this.comment = new ArrayList<>(comment);
this.key = key;
this.value = value;
}
public List<String> getComment() {
return comment;
}
public void setComment(List<String> comment) {
if(!comment.equals(this.comment)) {
this.comment = ImmutableList.copyOf(comment);
this.dirty = true;
}
}
public String getKey() {
return key;
}
public void setKey(String key) {
if(!key.equals(this.key)) {
this.key = key;
this.dirty = true;
}
}
public String getValue() {
return value;
}
public void setValue(String value) {
if(!value.equals(this.value)) {
this.value = value;
this.dirty = true;
}
}
public boolean isDirty() {
return dirty;
}
public void setDirty() {
dirty = true;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof PropertyToken) {
PropertyToken o = (PropertyToken) obj;
return comment.equals(o.comment) && key.equals(o.key) && value.equals(o.value);
}
return super.equals(obj);
}
}