aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/net/elytrium/limboauth/config/Config.java
blob: 48912c610a0587b2a2eb0d7566281cdb7a32ca64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * Copyright (C) 2021 - 2022 Elytrium
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.elytrium.limboauth.config;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

public class Config {

  private final Logger logger = LoggerFactory.getLogger(Config.class);

  private String oldPrefix = "";
  private String currentPrefix = "";

  /**
   * Set the value of a specific node. Probably throws some error if you supply non-existing keys or invalid values.
   *
   * @param key   config node
   * @param value value
   */
  private void set(String key, Object value, Class<?> root) {
    String[] split = key.split("\\.");
    Object instance = this.getInstance(split, root);
    if (instance != null) {
      Field field = this.getField(split, instance);
      if (field != null) {
        try {
          if (field.getAnnotation(Final.class) != null) {
            return;
          }
          if (field.getType() == String.class && !(value instanceof String)) {
            value = value + "";
          }
          field.set(instance, value);
          return;
        } catch (Throwable e) {
          e.printStackTrace();
        }
      }
    }

    this.logger.debug("Failed to set config option: " + key + ": " + value + " | " + instance + " | " + root.getSimpleName() + ".yml");
  }

  @SuppressWarnings("unchecked")
  public void set(Map<String, Object> input, String oldPath) {
    for (Map.Entry<String, Object> entry : input.entrySet()) {
      String key = oldPath + (oldPath.isEmpty() ? "" : ".") + entry.getKey();
      Object value = entry.getValue();

      if (value instanceof Map) {
        this.set((Map<String, Object>) value, key);
      } else if (value instanceof String) {
        if (key.equalsIgnoreCase("prefix") && !this.currentPrefix.equals(value)) {
          this.currentPrefix = (String) value;
        }

        this.set(key, ((String) value).replace("{NL}", "\n").replace("{PRFX}", this.currentPrefix), this.getClass());
      } else {
        this.set(key, value, this.getClass());
      }
    }
  }

  public boolean load(File file, String prefix) {
    this.oldPrefix = this.currentPrefix.isEmpty() ? prefix : this.currentPrefix;
    this.currentPrefix = prefix;
    if (!file.exists()) {
      return false;
    }

    try (InputStreamReader reader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
      this.set(new Yaml().load(reader), "");
    } catch (IOException e) {
      this.logger.warn("Unable to load config", e);
      return false;
    }

    return true;
  }

  /**
   * Indicates that a field should be instantiated / created.
   */
  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD})
  public @interface Create {

  }

  /**
   * Indicates that a field cannot be modified.
   */
  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD})
  public @interface Final {

  }

  /**
   * Creates a comment.
   */
  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD, ElementType.TYPE})
  public @interface Comment {

    String[] value();
  }

  /**
   * Any field or class with is not part of the config.
   */
  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD, ElementType.TYPE})
  public @interface Ignore {

  }

  private String toYamlString(Object value, String spacing, String fieldName) {
    if (value instanceof List) {
      Collection<?> listValue = (Collection<?>) value;
      if (listValue.isEmpty()) {
        return "[]";
      }
      StringBuilder m = new StringBuilder();
      for (Object obj : listValue) {
        m.append(System.lineSeparator()).append(spacing).append("- ").append(this.toYamlString(obj, spacing, fieldName));
      }

      return m.toString();
    }

    if (value instanceof String) {
      String stringValue = (String) value;
      if (stringValue.isEmpty()) {
        return "\"\"";
      }

      String quoted = "\"" + stringValue + "\"";
      if (fieldName.equalsIgnoreCase("prefix")) {
        return quoted;
      } else {
        return quoted.replace("\n", "{NL}").replace(this.currentPrefix.equals(this.oldPrefix) ? this.oldPrefix : this.currentPrefix, "{PRFX}");
      }
    }

    return value != null ? value.toString() : "null";
  }

  /**
   * Set all values in the file (load first to avoid overwriting).
   */
  @SuppressWarnings("ResultOfMethodCallIgnored")
  @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
  public void save(File file) {
    try {
      if (!file.exists()) {
        File parent = file.getParentFile();
        if (parent != null) {
          file.getParentFile().mkdirs();
        }
        file.createNewFile();
      }

      PrintWriter writer = new PrintWriter(file, StandardCharsets.UTF_8);
      Object instance = this;
      this.save(writer, this.getClass(), instance, 0);
      writer.close();
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }

  private void save(PrintWriter writer, Class<?> clazz, Object instance, int indent) {
    try {
      String lineSeparator = System.lineSeparator();
      String spacing = IntStream.range(0, indent).mapToObj(i -> " ").collect(Collectors.joining());

      for (Field field : clazz.getFields()) {
        if (field.getAnnotation(Ignore.class) != null) {
          continue;
        }
        Class<?> current = field.getType();
        if (field.getAnnotation(Ignore.class) != null) {
          continue;
        }

        Comment comment = field.getAnnotation(Comment.class);
        if (comment != null) {
          for (String commentLine : comment.value()) {
            writer.write(spacing + "# " + commentLine + lineSeparator);
          }
        }

        Create create = field.getAnnotation(Create.class);
        if (create != null) {
          Object value = field.get(instance);
          this.setAccessible(field);
          if (indent == 0) {
            writer.write(lineSeparator);
          }
          comment = current.getAnnotation(Comment.class);
          if (comment != null) {
            for (String commentLine : comment.value()) {
              writer.write(spacing + "# " + commentLine + lineSeparator);
            }
          }
          writer.write(spacing + this.toNodeName(current.getSimpleName()) + ":" + lineSeparator);
          if (value == null) {
            field.set(instance, value = current.getDeclaredConstructor().newInstance());
          }
          this.save(writer, current, value, indent + 2);
        } else {
          String value = this.toYamlString(field.get(instance), spacing, field.getName());
          writer.write(spacing + this.toNodeName(field.getName() + ": ") + value + lineSeparator);
        }
      }
    } catch (Throwable e) {
      e.printStackTrace();
    }
  }

  /**
   * Get the field for a specific config node and instance.
   *
   * <p>As expiry can have multiple blocks there will be multiple instances
   *
   * @param split    the node (split by period)
   * @param instance the instance
   */
  private Field getField(String[] split, Object instance) {
    try {
      Field field = instance.getClass().getField(this.toFieldName(split[split.length - 1]));
      this.setAccessible(field);
      return field;
    } catch (Throwable ignored) {
      this.logger.debug("Invalid config field: " + this.join(split, ".") + " for " + this.toNodeName(instance.getClass().getSimpleName()));
      return null;
    }
  }

  /**
   * Get the instance for a specific config node.
   *
   * @param split the node (split by period)
   * @return The instance or null
   */
  private Object getInstance(String[] split, Class<?> root) {
    try {
      Class<?> clazz = root == null ? MethodHandles.lookup().lookupClass() : root;
      Object instance = this;
      while (split.length > 0) {
        if (split.length == 1) {
          return instance;
        } else {
          Class<?> found = null;
          if (clazz == null) {
            return null;
          }

          Class<?>[] classes = clazz.getDeclaredClasses();
          for (Class<?> current : classes) {
            if (Objects.equals(current.getSimpleName(), this.toFieldName(split[0]))) {
              found = current;
              break;
            }
          }

          if (found == null) {
            return null;
          }

          try {
            Field instanceField = clazz.getDeclaredField(this.toFieldName(split[0]));
            this.setAccessible(instanceField);
            Object value = instanceField.get(instance);
            if (value == null) {
              value = found.getDeclaredConstructor().newInstance();
              instanceField.set(instance, value);
            }

            clazz = found;
            instance = value;
            split = Arrays.copyOfRange(split, 1, split.length);
            continue;
          } catch (NoSuchFieldException e) {
            //
          }

          split = Arrays.copyOfRange(split, 1, split.length);
          clazz = found;
          instance = clazz.getDeclaredConstructor().newInstance();
        }
      }
    } catch (Throwable e) {
      e.printStackTrace();
    }

    return null;
  }

  /**
   * Translate a node to a java field name.
   */
  private String toFieldName(String node) {
    return node.toUpperCase(Locale.ROOT).replaceAll("-", "_");
  }

  /**
   * Translate a field to a config node.
   */
  private String toNodeName(String field) {
    return field.toLowerCase(Locale.ROOT).replace("_", "-");
  }

  /**
   * Set some field to be accessible.
   */
  private void setAccessible(Field field) throws NoSuchFieldException, IllegalAccessException {
    field.setAccessible(true);
    if (Modifier.isFinal(field.getModifiers())) {
      if (Runtime.version().feature() < 12) {
        Field modifiersField = Field.class.getDeclaredField("modifiers");
        modifiersField.setAccessible(true);
        modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
      } else {
        // TODO: Maybe use sun.misc.Unsafe?...
        throw new UnsupportedOperationException();
      }
    }
  }

  @SuppressWarnings("SameParameterValue")
  private String join(Object[] array, String delimiter) {
    switch (array.length) {
      case 0: {
        return "";
      }
      case 1: {
        return array[0].toString();
      }
      default: {
        StringBuilder result = new StringBuilder();
        for (int i = 0, j = array.length; i < j; ++i) {
          if (i > 0) {
            result.append(delimiter);
          }
          result.append(array[i]);
        }

        return result.toString();
      }
    }
  }
}