/
YamlStorageWithLists.java
439 lines (385 loc) · 14.1 KB
/
YamlStorageWithLists.java
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package net.citizensnpcs.api.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import com.google.common.io.Files;
import com.google.common.primitives.Ints;
public class YamlStorageWithLists implements FileStorage {
private final FileConfiguration config;
private final File file;
public YamlStorageWithLists(File file) {
this(file, null);
}
public YamlStorageWithLists(File file, String header) {
config = new YamlConfiguration();
tryIncreaseMaxCodepoints(config);
this.file = file;
if (!file.exists()) {
create();
if (header != null) {
config.options().header(header);
}
save();
}
}
private void create() {
try {
Bukkit.getLogger().log(Level.INFO, "Creating file: " + file.getName());
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Could not create file: " + file.getName());
}
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
YamlStorageWithLists other = (YamlStorageWithLists) obj;
return Objects.equals(file, other.file);
}
@Override
public File getFile() {
return file;
}
@Override
public YamlKey getKey(String root) {
return new YamlKey(root);
}
@Override
public int hashCode() {
return 31 + (file == null ? 0 : file.hashCode());
}
@Override
public boolean load() {
try {
config.load(file);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
}
private boolean pathExists(String key) {
return config.get(key) != null;
}
@Override
public void save() {
try {
Files.createParentDirs(file);
File temporaryFile = File.createTempFile(file.getName(), null, file.getParentFile());
temporaryFile.deleteOnExit();
config.save(temporaryFile);
file.delete();
temporaryFile.renameTo(file);
temporaryFile.delete();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public String toString() {
return "YamlStorage {file=" + file + "}";
}
private void tryIncreaseMaxCodepoints(FileConfiguration config) {
if (SET_CODEPOINT_LIMIT == null || LOADER_OPTIONS == null)
return;
try {
SET_CODEPOINT_LIMIT.invoke(LOADER_OPTIONS.get(config), 67108864 /* ~64MB, Paper's limit */);
} catch (Exception e) {
e.printStackTrace();
}
}
public class YamlKey extends DataKey {
public YamlKey(String root) {
super(root);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj) || getClass() != obj.getClass())
return false;
YamlKey other = (YamlKey) obj;
return getOuterType().equals(other.getOuterType());
}
private Object get(String path, Object def) {
int i1 = -1, i2;
Function<String, Object> next = k -> {
Object res = config.getConfigurationSection(k);
return res == null ? config.getList(k) : res;
};
Object relative = config;
while ((i1 = path.indexOf('.', i2 = i1 + 1)) != -1) {
String curr = path.substring(i2, i1);
relative = next.apply(curr);
if (relative == null)
return def;
if (relative instanceof List) {
List<Object> list = (List<Object>) relative;
next = k -> {
if (Ints.tryParse(k) >= list.size())
return def;
return list.get(Ints.tryParse(k));
};
} else if (relative instanceof ConfigurationSection) {
ConfigurationSection section = (ConfigurationSection) relative;
next = k -> {
Object res = section.getConfigurationSection(k);
return res == null ? section.getList(k) : res;
};
} else if (relative instanceof Map) {
Map map = (Map) relative;
next = k -> map.get(k);
}
}
if (relative == null)
return def;
if (relative instanceof ConfigurationSection)
return ((ConfigurationSection) relative).get(path.substring(i2));
else if (relative instanceof List)
return ((List) relative).get(Ints.tryParse(path.substring(i2)));
else if (relative instanceof Map)
return ((Map) relative).get(path.substring(i2));
throw new RuntimeException();
}
@Override
public boolean getBoolean(String key) {
String path = createRelativeKey(key);
Object val = get(path, null);
if (val != null)
return Boolean.parseBoolean(val.toString());
return false;
}
@Override
public boolean getBoolean(String key, boolean def) {
return (boolean) get(createRelativeKey(key), def);
}
@Override
public double getDouble(String key) {
return getDouble(key, 0);
}
@Override
public double getDouble(String key, double def) {
String path = createRelativeKey(key);
Object val = get(path, def);
if (val != null) {
if (val instanceof Number)
return ((Number) val).doubleValue();
String str = val.toString();
return str.isEmpty() ? def : Double.parseDouble(str);
}
return def;
}
@Override
public DataKey getFromRoot(String path) {
return new YamlKey(path);
}
@Override
public int getInt(String key) {
return getInt(key, 0);
}
@Override
public int getInt(String key, int def) {
String path = createRelativeKey(key);
Object val = get(path, def);
if (val != null) {
if (val instanceof Number)
return ((Number) val).intValue();
String str = val.toString();
return str.isEmpty() ? def : Integer.parseInt(str);
}
return def;
}
@Override
public Iterable<DataKey> getIntegerSubKeys() {
return super.getIntegerSubKeys();
}
@Override
public long getLong(String key) {
return getLong(key, 0L);
}
@Override
public long getLong(String key, long def) {
String path = createRelativeKey(key);
Object val = get(path, def);
if (val != null) {
if (val instanceof Number)
return ((Number) val).longValue();
String str = val.toString();
return str.isEmpty() ? def : Long.parseLong(str);
}
return def;
}
private YamlStorageWithLists getOuterType() {
return YamlStorageWithLists.this;
}
@Override
public Object getRaw(String key) {
return get(createRelativeKey(key), null);
}
@Override
public YamlKey getRelative(String relative) {
if (relative == null || relative.isEmpty())
return this;
return new YamlKey(createRelativeKey(relative));
}
public YamlStorageWithLists getStorage() {
return YamlStorageWithLists.this;
}
@Override
public String getString(String key) {
String path = createRelativeKey(key);
Object val = get(path, null);
if (val != null)
return val.toString();
return "";
}
@Override
public Iterable<DataKey> getSubKeys() {
Object value = get(path, null);
if (value == null)
return Collections.emptyList();
if (value instanceof List)
return IntStream.range(0, ((List) value).size()).mapToObj(this::getRelative)
.collect(Collectors.toList());
else if (value instanceof Map)
return ((Map<String, Object>) value).keySet().stream().map(this::getRelative)
.collect(Collectors.toList());
else {
ConfigurationSection section = (ConfigurationSection) value;
return section.getKeys(false).stream().map(this::getRelative).collect(Collectors.toList());
}
}
@Override
public Map<String, Object> getValuesDeep() {
return sectionToValues(config.getConfigurationSection(path));
}
@Override
public int hashCode() {
final int prime = 31;
int result = prime * super.hashCode() + getOuterType().hashCode();
return result;
}
@Override
public boolean keyExists(String key) {
return get(createRelativeKey(key), null) != null;
}
@Override
public String name() {
int last = path.lastIndexOf('.');
return path.substring(last == 0 ? 0 : last + 1);
}
@Override
public void removeKey(String key) {
config.set(createRelativeKey(key), null);
}
private void set(String path, Object value) {
int i1 = -1, i2;
Object prev = config;
while ((i1 = path.indexOf('.', i2 = i1 + 1)) != -1) {
String curr = path.substring(i2, i1);
Object next = null;
if (prev instanceof ConfigurationSection) {
next = ((ConfigurationSection) prev).get(curr);
} else if (prev instanceof Map) {
next = ((Map) prev).get(curr);
} else if (prev instanceof List) {
Integer idx = Ints.tryParse(curr);
if (idx != null) {
List list = (List) prev;
while (list.size() <= idx) {
list.add(new HashMap());
}
next = list.get(idx);
}
}
int nextSegment = path.indexOf('.', i1 + 1);
if (nextSegment != -1 && Ints.tryParse(path.substring(i1 + 1, nextSegment)) != null) {
if (!(next instanceof List)) {
next = new ArrayList<>();
if (prev instanceof ConfigurationSection) {
((ConfigurationSection) prev).set(curr, next);
} else if (prev instanceof Map) {
((Map) prev).put(curr, next);
}
}
} else if (next == null) {
if (prev instanceof ConfigurationSection) {
next = ((ConfigurationSection) prev).createSection(curr);
} else if (prev instanceof Map) {
next = new HashMap();
((Map) prev).put(curr, next);
}
}
prev = next;
}
if (prev == null)
throw new RuntimeException();
if (prev instanceof ConfigurationSection) {
((ConfigurationSection) prev).set(path.substring(i2), value);
} else if (prev instanceof List) {
((List) prev).set(Ints.tryParse(path.substring(i2)), value);
} else if (prev instanceof Map) {
((Map) prev).put(path.substring(i2), value);
}
}
@Override
public void setBoolean(String key, boolean value) {
set(createRelativeKey(key), value);
}
@Override
public void setDouble(String key, double value) {
set(createRelativeKey(key), String.valueOf(value));
}
@Override
public void setInt(String key, int value) {
set(createRelativeKey(key), value);
}
@Override
public void setLong(String key, long value) {
set(createRelativeKey(key), value);
}
@Override
public void setRaw(String key, Object value) {
set(createRelativeKey(key), value);
}
@Override
public void setString(String key, String value) {
set(createRelativeKey(key), value);
}
@Override
public String toString() {
return "YamlKey [path=" + path + "]";
}
}
private static Field LOADER_OPTIONS;
private static Method SET_CODEPOINT_LIMIT;
static {
try {
LOADER_OPTIONS = YamlConfiguration.class.getDeclaredField("yamlLoaderOptions");
LOADER_OPTIONS.setAccessible(true);
SET_CODEPOINT_LIMIT = Class.forName("org.yaml.snakeyaml.LoaderOptions").getMethod("setCodepointLimit",
int.class);
SET_CODEPOINT_LIMIT.setAccessible(true);
} catch (Exception e) {
}
}
}