From ab7256dff5d6d37488081ba7a01b36d3ee9ef563 Mon Sep 17 00:00:00 2001 From: DeDiamondPro <67508414+DeDiamondPro@users.noreply.github.com> Date: Sun, 5 Jun 2022 17:43:23 +0200 Subject: refactor (#36) * refactor * fix vig compat * fix nanovg thingy * e * finalize * gui utils package thingy --- .../oneconfig/config/migration/Migrator.java | 14 ---- .../config/migration/VigilanceMigrator.java | 84 ---------------------- .../config/migration/vigilance/Migrator.java | 14 ++++ .../migration/vigilance/VigilanceMigrator.java | 84 ++++++++++++++++++++++ 4 files changed, 98 insertions(+), 98 deletions(-) delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java delete mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/Migrator.java create mode 100644 src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/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 deleted file mode 100644 index abfb2a0..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/migration/Migrator.java +++ /dev/null @@ -1,14 +0,0 @@ -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 deleted file mode 100644 index b45aeba..0000000 --- a/src/main/java/cc/polyfrost/oneconfig/config/migration/VigilanceMigrator.java +++ /dev/null @@ -1,84 +0,0 @@ -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 final String filePath; - protected HashMap>> values = null; - - 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(); - } - } -} diff --git a/src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/Migrator.java b/src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/Migrator.java new file mode 100644 index 0000000..f3f60b7 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/Migrator.java @@ -0,0 +1,14 @@ +package cc.polyfrost.oneconfig.config.migration.vigilance; + +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/vigilance/VigilanceMigrator.java b/src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/VigilanceMigrator.java new file mode 100644 index 0000000..2870e03 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/config/migration/vigilance/VigilanceMigrator.java @@ -0,0 +1,84 @@ +package cc.polyfrost.oneconfig.config.migration.vigilance; + +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 final String filePath; + protected HashMap>> values = null; + + 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