diff options
| author | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-24 18:15:49 +0200 |
|---|---|---|
| committer | DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> | 2022-05-24 18:15:49 +0200 |
| commit | d69b634615134e294c4eee45827adc1eb73514b9 (patch) | |
| tree | 8f3f6059d423a24fd25643c7cdb975ddafda1d12 /src/main/java/cc/polyfrost/oneconfig/config/migration | |
| parent | 4b0c40c93658fd871876effa371ad9159845293d (diff) | |
| download | OneConfig-d69b634615134e294c4eee45827adc1eb73514b9.tar.gz OneConfig-d69b634615134e294c4eee45827adc1eb73514b9.tar.bz2 OneConfig-d69b634615134e294c4eee45827adc1eb73514b9.zip | |
OC-23 finish migration system
Diffstat (limited to 'src/main/java/cc/polyfrost/oneconfig/config/migration')
| -rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java | 14 | ||||
| -rw-r--r-- | src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java | 84 |
2 files changed, 98 insertions, 0 deletions
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("\\[\"?(?<category>[^.\\[\\]\"]+)\"?\\.\"?(?<subcategory>[^.\\[\\]\"]+)\"?]"); + private static final Pattern booleanPattern = Pattern.compile("\"?(?<name>[^\\s\"]+)\"? = (?<value>true|false)"); + private static final Pattern numberPattern = Pattern.compile("\"?(?<name>[^\\s\"]+)\"? = (?<value>[\\d.]+)"); + private static final Pattern stringPattern = Pattern.compile("\"?(?<name>[^\\s\"]+)\"? = \"(?<value>.+)\""); + protected HashMap<String, HashMap<String, HashMap<String, Object>>> 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<String, Object> 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(); + } + } +} |
