-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathLoader.java
More file actions
188 lines (171 loc) · 8.82 KB
/
Loader.java
File metadata and controls
188 lines (171 loc) · 8.82 KB
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
/*
* PlayerVaultsX
* Copyright (C) 2013 Trent Hensler, Laxwashere, CmdrKittens
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.drtshock.playervaults.config;
import com.drtshock.playervaults.PlayerVaults;
import com.drtshock.playervaults.config.annotation.Comment;
import com.drtshock.playervaults.config.annotation.ConfigName;
import com.drtshock.playervaults.config.annotation.WipeOnReload;
import com.drtshock.playervaults.config.file.Translation;
import com.drtshock.playervaults.lib.com.typesafe.config.*;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class Loader {
public static void loadAndSave(@NonNull String fileName, @NonNull Object config) throws IOException, IllegalAccessException {
File file = Loader.getFile(fileName);
Loader.loadAndSave(file, Loader.getConf(file), config);
}
public static @NonNull File getFile(@NonNull String file) {
Path configFolder = PlayerVaults.getInstance().getDataFolder().toPath();
if (!configFolder.toFile().exists()) {
configFolder.toFile().mkdir();
}
Path path = configFolder.resolve(file + ".conf");
return path.toFile();
}
public static @NonNull Config getConf(@NonNull File file) {
return ConfigFactory.parseFile(file);
}
public static void loadAndSave(@NonNull File file, @NonNull Config config, @NonNull Object configObject) throws IOException, IllegalAccessException {
ConfigValue value = Loader.loadNode(config, configObject);
String s = value.render(ConfigRenderOptions.defaults().setOriginComments(false).setComments(true).setJson(false));
Files.write(file.toPath(), s.getBytes(StandardCharsets.UTF_8));
}
public static @NonNull ConfigValue load(Config config, Object configObject) throws IllegalAccessException {
return Loader.loadNode(config, configObject);
}
private static final Set<Class<?>> types = new HashSet<>();
static {
Loader.types.add(Boolean.TYPE);
Loader.types.add(Byte.TYPE);
Loader.types.add(Character.TYPE);
Loader.types.add(Double.TYPE);
Loader.types.add(Float.TYPE);
Loader.types.add(Integer.TYPE);
Loader.types.add(Long.TYPE);
Loader.types.add(Short.TYPE);
Loader.types.add(List.class);
Loader.types.add(Map.class);
Loader.types.add(Set.class);
Loader.types.add(String.class);
Loader.types.add(Translation.TL.class);
}
private static @NonNull ConfigValue loadNode(@NonNull Config config, @NonNull Object object) throws IllegalAccessException {
return loadNode(config, "", object);
}
private static @NonNull ConfigValue loadNode(@NonNull Config config, String path, @NonNull Object object) throws IllegalAccessException {
Map<String, ConfigValue> map = new HashMap<>();
for (Field field : Loader.getFields(object.getClass())) {
if (field.isSynthetic()) {
continue;
}
if ((field.getModifiers() & Modifier.TRANSIENT) != 0) {
if (field.getAnnotation(WipeOnReload.class) != null) {
field.setAccessible(true);
field.set(object, null);
}
continue;
}
field.setAccessible(true);
ConfigName configName = field.getAnnotation(ConfigName.class);
Comment comment = field.getAnnotation(Comment.class);
String confName = configName == null || configName.value().isEmpty() ? field.getName() : configName.value();
String newPath = path.isEmpty() ? confName : (path + '.' + confName);
ConfigValue curValue = Loader.getOrNull(config, newPath);
boolean needsValue = curValue == null;
ConfigValue newValue;
Object defaultValue = field.get(object);
if (Loader.types.contains(field.getType())) {
if (needsValue) {
if (Translation.TL.class.isAssignableFrom(field.getType())) {
Translation.TL tl = (Translation.TL) defaultValue;
newValue = tl.size() == 1 ? ConfigValueFactory.fromAnyRef(tl.get(0)) : ConfigValueFactory.fromAnyRef(tl);
} else {
newValue = ConfigValueFactory.fromAnyRef(defaultValue);
}
} else {
dance:
try {
if (Translation.TL.class.isAssignableFrom(field.getType())) {
Translation.TL tl;
if (curValue.valueType() == ConfigValueType.STRING) {
String s = curValue.unwrapped().toString();
tl = Translation.TL.copyOf(Collections.singletonList(s));
} else if (curValue.valueType() == ConfigValueType.LIST) {
List<String> l = (List<String>) curValue.unwrapped();
tl = Translation.TL.copyOf(l);
} else {
tl = (Translation.TL) defaultValue;
}
newValue = tl.size() == 1 ? ConfigValueFactory.fromAnyRef(tl.get(0)) : ConfigValueFactory.fromAnyRef(tl);
field.set(object, tl);
break dance;
}
if (List.class.isAssignableFrom(field.getType()) && curValue.valueType() == ConfigValueType.STRING) {
List<?> list = Collections.singletonList(curValue.unwrapped());
field.set(object, list);
newValue = ConfigValueFactory.fromAnyRef(list);
break dance;
} else if (Set.class.isAssignableFrom(field.getType()) && curValue.valueType() == ConfigValueType.STRING) {
Set<?> set = Collections.singleton(curValue.unwrapped());
field.set(object, set);
newValue = ConfigValueFactory.fromAnyRef(set);
break dance;
} else if (Set.class.isAssignableFrom(field.getType()) && curValue.valueType() == ConfigValueType.LIST) {
field.set(object, new HashSet<Object>((List<?>) curValue.unwrapped()));
} else {
field.set(object, curValue.unwrapped());
}
newValue = curValue;
} catch (IllegalArgumentException ex) {
PlayerVaults.getInstance().getLogger().warning("Found incorrect type for " + confName + ": Expected " + field.getType() + ", found " + curValue.unwrapped().getClass());
field.set(object, defaultValue);
newValue = ConfigValueFactory.fromAnyRef(defaultValue);
}
}
} else {
newValue = Loader.loadNode(config, newPath, defaultValue);
}
if (comment != null) {
newValue = newValue.withOrigin(newValue.origin().withComments(Arrays.asList(comment.value().split("\n"))));
}
map.put(confName, newValue);
}
return ConfigValueFactory.fromMap(map);
}
private static @Nullable ConfigValue getOrNull(@NonNull Config config, @NonNull String path) {
return config.hasPath(path) ? config.getValue(path) : null;
}
private static @NonNull List<Field> getFields(@NonNull Class<?> clazz) {
return Loader.getFields(new ArrayList<>(), clazz);
}
private static @NonNull List<Field> getFields(@NonNull List<Field> fields, @NonNull Class<?> clazz) {
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
if (clazz.getSuperclass() != null) {
Loader.getFields(fields, clazz.getSuperclass());
}
return fields;
}
}