From d69b634615134e294c4eee45827adc1eb73514b9 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Tue, 24 May 2022 18:15:49 +0200 Subject: OC-23 finish migration system --- .../oneconfig/config/migration/Migrator.java | 14 ++++ .../config/migration/VigilanceMigrator.java | 84 ++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java (limited to 'src/main/java/cc/polyfrost/oneconfig/config/migration') diff --git a/src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java b/src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java new file mode 100644 index 0000000..abfb2a0 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java @@ -0,0 +1,14 @@ +package cc.polyfrost.oneconfig.config.migration; + +import java.lang.reflect.Field; + +public interface Migrator { + /** + * @param field The field of the option + * @param name The name of the option + * @param category The category of the option + * @param subcategory The subcategory of the option + * @return Value of the option, null if not found + */ + Object getValue(Field field, String name, String category, String subcategory); +} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java b/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java new file mode 100644 index 0000000..5573a3e --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java @@ -0,0 +1,84 @@ +package cc.polyfrost.oneconfig.config.migration; + +import cc.polyfrost.oneconfig.config.annotations.VigilanceName; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class VigilanceMigrator implements Migrator { + private static final Pattern categoryPattern = Pattern.compile("\\[\"?(?[^.\\[\\]\"]+)\"?\\.\"?(?[^.\\[\\]\"]+)\"?]"); + private static final Pattern booleanPattern = Pattern.compile("\"?(?[^\\s\"]+)\"? = (?true|false)"); + private static final Pattern numberPattern = Pattern.compile("\"?(?[^\\s\"]+)\"? = (?[\\d.]+)"); + private static final Pattern stringPattern = Pattern.compile("\"?(?[^\\s\"]+)\"? = \"(?.+)\""); + protected HashMap>> values = null; + protected final String filePath; + + public VigilanceMigrator(String filePath) { + this.filePath = filePath; + } + + @Override + public Object getValue(Field field, String name, String category, String subcategory) { + if (values == null) getOptions(); + if (field.isAnnotationPresent(VigilanceName.class)) { + VigilanceName annotation = field.getAnnotation(VigilanceName.class); + name = annotation.name(); + category = annotation.category(); + subcategory = annotation.subcategory(); + } + name = parse(name); + category = parse(category); + subcategory = parse(subcategory); + if (values.containsKey(category) && values.get(category).containsKey(subcategory) && values.get(category).get(subcategory).containsKey(name)) + return values.get(category).get(subcategory).get(name); + return null; + } + + protected String parse(String value) { + return value.toLowerCase().replace(" ", "_"); + } + + protected void getOptions() { + if (values == null) values = new HashMap<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { + String currentCategory = null; + String currentSubcategory = null; + String line; + while ((line = reader.readLine()) != null) { + Matcher categoryMatcher = categoryPattern.matcher(line); + if (categoryMatcher.find()) { + currentCategory = categoryMatcher.group("category"); + currentSubcategory = categoryMatcher.group("subcategory"); + if (!values.containsKey(currentCategory)) values.put(currentCategory, new HashMap<>()); + if (!values.get(currentCategory).containsKey(currentSubcategory)) + values.get(currentCategory).put(currentSubcategory, new HashMap<>()); + continue; + } + if (currentCategory == null) continue; + HashMap options = values.get(currentCategory).get(currentSubcategory); + Matcher booleanMatcher = booleanPattern.matcher(line); + if (booleanMatcher.find()) { + options.put(booleanMatcher.group("name"), Boolean.parseBoolean(booleanMatcher.group("value"))); + continue; + } + Matcher numberMatcher = numberPattern.matcher(line); + if (numberMatcher.find()) { + String value = numberMatcher.group("value"); + if (value.contains(".")) options.put(numberMatcher.group("name"), Float.parseFloat(value)); + else options.put(numberMatcher.group("name"), Integer.parseInt(value)); + continue; + } + Matcher stringMatcher = stringPattern.matcher(line); + if (stringMatcher.find()) { + options.put(stringMatcher.group("name"), stringMatcher.group("value")); + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} -- cgit