|
| 1 | +package net.cryptic_game.backend.processor.config; |
| 2 | + |
| 3 | +import com.google.auto.service.AutoService; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.Reader; |
| 9 | +import java.lang.reflect.InvocationTargetException; |
| 10 | +import java.lang.reflect.Method; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.LinkedList; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Locale; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Queue; |
| 17 | +import java.util.Set; |
| 18 | +import javax.annotation.processing.AbstractProcessor; |
| 19 | +import javax.annotation.processing.Processor; |
| 20 | +import javax.annotation.processing.RoundEnvironment; |
| 21 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 22 | +import javax.annotation.processing.SupportedSourceVersion; |
| 23 | +import javax.lang.model.SourceVersion; |
| 24 | +import javax.lang.model.element.TypeElement; |
| 25 | +import javax.lang.model.element.VariableElement; |
| 26 | +import javax.lang.model.util.ElementFilter; |
| 27 | + |
| 28 | +import org.yaml.snakeyaml.Yaml; |
| 29 | + |
| 30 | +@AutoService(Processor.class) |
| 31 | +@SupportedSourceVersion(SourceVersion.RELEASE_11) |
| 32 | +@SupportedAnnotationTypes("net.cryptic_game.backend.processor.config.ConfigValue") |
| 33 | +public class ConfigProcessor extends AbstractProcessor { |
| 34 | + |
| 35 | + private final Map<String, Object> config; |
| 36 | + |
| 37 | + public ConfigProcessor() { |
| 38 | + final File workingDir = new File(System.getProperty("user.dir"), "config/gamedesign/parameter"); |
| 39 | + this.config = this.loadConfig(workingDir); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) { |
| 44 | + annotations.stream() |
| 45 | + .flatMap(annotation -> ElementFilter.fieldsIn(roundEnv.getElementsAnnotatedWith(annotation)).stream()) |
| 46 | + .forEach(this::process); |
| 47 | + |
| 48 | + return true; |
| 49 | + } |
| 50 | + |
| 51 | + private void process(final VariableElement element) { |
| 52 | + final ConfigValue configValue = element.getAnnotation(ConfigValue.class); |
| 53 | + final String name = configValue.value().isEmpty() ? element.getSimpleName().toString() : configValue.value(); |
| 54 | + |
| 55 | + final Object value = this.getValue(this.config, new LinkedList<>(List.of(name.toLowerCase(Locale.ROOT).split("_")))); |
| 56 | + |
| 57 | + if (value == null) { |
| 58 | + throw new RuntimeException(String.format("Unable to find config value \"%s\".", name)); |
| 59 | + } |
| 60 | + |
| 61 | + if (!element.asType().toString().equals(value.getClass().getName())) { |
| 62 | + throw new RuntimeException(String.format( |
| 63 | + "Config value \"%s\" has wrong type. (Required: %s, Provided: %s)", |
| 64 | + name, |
| 65 | + element.asType().toString(), |
| 66 | + value.getClass().getName() |
| 67 | + )); |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + final Method method = element.getClass() |
| 72 | + .getMethod("setData", Object.class); |
| 73 | + |
| 74 | + method.setAccessible(true); |
| 75 | + method.invoke(element, value); |
| 76 | + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { |
| 77 | + throw new RuntimeException(e); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private Map<String, Object> loadConfig(final File configDir) { |
| 82 | + final File[] files = configDir.listFiles(); |
| 83 | + if (files == null) { |
| 84 | + return Collections.emptyMap(); |
| 85 | + } |
| 86 | + |
| 87 | + Map<String, Object> config = null; |
| 88 | + |
| 89 | + for (final File file : files) { |
| 90 | + final String name = file.getName().toLowerCase(Locale.ENGLISH); |
| 91 | + if (file.isDirectory() || !(name.endsWith(".yaml") || name.endsWith(".yml"))) continue; |
| 92 | + |
| 93 | + final Map<String, Object> data; |
| 94 | + |
| 95 | + try (Reader reader = new FileReader(file)) { |
| 96 | + data = new Yaml().load(reader); |
| 97 | + } catch (IOException e) { |
| 98 | + throw new RuntimeException(e); |
| 99 | + } |
| 100 | + |
| 101 | + if (config == null) config = data; |
| 102 | + else this.merge(config, data); |
| 103 | + } |
| 104 | + |
| 105 | + return config; |
| 106 | + } |
| 107 | + |
| 108 | + private void merge(final Map<String, Object> first, final Map<String, Object> second) { |
| 109 | + second.forEach((secondKey, secondValue) -> { |
| 110 | + final Object firstValue = first.get(secondKey); |
| 111 | + |
| 112 | + if (firstValue instanceof Map && secondValue instanceof Map) { |
| 113 | + this.merge((Map<String, Object>) firstValue, (Map<String, Object>) secondValue); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + first.put(secondKey, secondValue); |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + private Object getValue(final Map<String, Object> config, final Queue<String> key) { |
| 122 | + if (config == null) return null; |
| 123 | + |
| 124 | + if (key.isEmpty()) throw new IllegalArgumentException(); |
| 125 | + |
| 126 | + return key.size() == 1 |
| 127 | + ? config.get(key.poll()) |
| 128 | + : this.getValue((Map<String, Object>) config.get(key.poll()), key); |
| 129 | + } |
| 130 | +} |
0 commit comments