/
NBTStorage.java
400 lines (353 loc) · 12.6 KB
/
NBTStorage.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
package net.citizensnpcs.api.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Queue;
import java.util.zip.GZIPInputStream;
import net.citizensnpcs.api.jnbt.ByteTag;
import net.citizensnpcs.api.jnbt.CompoundTag;
import net.citizensnpcs.api.jnbt.DoubleTag;
import net.citizensnpcs.api.jnbt.FloatTag;
import net.citizensnpcs.api.jnbt.IntTag;
import net.citizensnpcs.api.jnbt.LongTag;
import net.citizensnpcs.api.jnbt.NBTInputStream;
import net.citizensnpcs.api.jnbt.NBTOutputStream;
import net.citizensnpcs.api.jnbt.NBTUtils;
import net.citizensnpcs.api.jnbt.ShortTag;
import net.citizensnpcs.api.jnbt.StringTag;
import net.citizensnpcs.api.jnbt.Tag;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
public class NBTStorage implements FileStorage {
private final File file;
private final String name;
private final Map<String, Tag> root = Maps.newHashMap();
public NBTStorage(File file) {
this(file, "root");
}
public NBTStorage(File file, String name) {
this.file = file;
if (!this.file.exists()) {
create();
}
this.name = name;
}
public NBTStorage(String file) {
this(new File(file), "root");
}
private void create() {
try {
Messaging.log("Creating file: " + file.getName());
Files.createParentDirs(file);
file.createNewFile();
} catch (IOException ex) {
Messaging.severe("Could not create file: " + file.getName());
ex.printStackTrace();
}
}
@Override
public File getFile() {
return file;
}
@Override
public NBTKey getKey(String root) {
return new NBTKey(root);
}
@Override
public boolean load() {
NBTInputStream stream = null;
try {
stream = new NBTInputStream(new GZIPInputStream(new FileInputStream(file)));
Tag tag = stream.readTag();
if (tag == null || !(tag instanceof CompoundTag)) {
return false;
} else {
root.clear();
root.putAll(((CompoundTag) tag).getValue());
}
} catch (IOException ex) {
ex.printStackTrace();
return false;
} finally {
try {
if (stream != null) {
stream.close();
}
}
catch (Exception e) {
e.getCause(); // Do nothing
}
}
return true;
}
@Override
public void save() {
NBTOutputStream stream = null;
try {
Files.createParentDirs(file);
File temporaryFile = File.createTempFile(file.getName(), null, file.getParentFile());
temporaryFile.deleteOnExit();
stream = new NBTOutputStream(new FileOutputStream(temporaryFile));
stream.writeTag(new CompoundTag(name, root));
stream.close();
file.delete();
temporaryFile.renameTo(file);
temporaryFile.delete();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
}
catch (Exception e) {
e.getCause(); // Do nothing
}
}
}
@Override
public String toString() {
return "NBTStorage {file=" + file + "}";
}
public class NBTKey extends DataKey {
public NBTKey(String root) {
super(root);
}
private String createRelativeKey(String parent, String sub) {
if (sub.isEmpty())
return parent;
if (sub.charAt(0) == '.')
return parent.isEmpty() ? sub.substring(1, sub.length()) : parent + sub;
return parent.isEmpty() ? sub : parent + "." + sub;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
NBTKey other = (NBTKey) obj;
if (!getOuterType().equals(other.getOuterType())) {
return false;
}
if (path == null) {
if (other.path != null) {
return false;
}
} else if (!path.equals(other.path)) {
return false;
}
return true;
}
private Number extractNumber(Tag tag) {
if (tag == null)
return null;
if (tag instanceof DoubleTag) {
return ((DoubleTag) tag).getValue();
} else if (tag instanceof IntTag) {
return ((IntTag) tag).getValue();
} else if (tag instanceof ShortTag) {
return ((ShortTag) tag).getValue();
} else if (tag instanceof ByteTag) {
return ((ByteTag) tag).getValue();
} else if (tag instanceof FloatTag) {
return ((FloatTag) tag).getValue();
} else if (tag instanceof LongTag) {
return ((LongTag) tag).getValue();
}
return null;
}
private Map<String, Tag> findLastParent(String[] parts) {
Map<String, Tag> map = root;
for (int i = 0; i < parts.length - 1; ++i) {
if (!map.containsKey(parts[i]) || !(map.get(parts[i]) instanceof CompoundTag))
return Collections.emptyMap();
map = ((CompoundTag) map.get(parts[i])).getValue();
}
return map;
}
private Tag findLastTag(String key) {
return findLastTag(key, true);
}
private Tag findLastTag(String key, boolean relative) {
String[] parts = Iterables.toArray(
Splitter.on('.').omitEmptyStrings().split(relative ? createRelativeKey(key) : key), String.class);
if (parts.length == 0)
return new CompoundTag(name, root);
Map<String, Tag> map = findLastParent(parts);
if (!map.containsKey(parts[parts.length - 1]))
return null;
return map.get(parts[parts.length - 1]);
}
@Override
public boolean getBoolean(String key) {
Number number = extractNumber(findLastTag(key));
if (number == null)
return false;
return number.byteValue() >= 1 ? true : false;
}
@Override
public double getDouble(String key) {
Number number = extractNumber(findLastTag(key));
if (number == null)
return 0;
return number.doubleValue();
}
@Override
public int getInt(String key) {
Number number = extractNumber(findLastTag(key));
if (number == null)
return 0;
return number.intValue();
}
@Override
public long getLong(String key) {
Number number = extractNumber(findLastTag(key));
if (number == null)
return 0;
return number.longValue();
}
private String getNameFor(String key) {
String[] parts = Iterables.toArray(Splitter.on('.').split(createRelativeKey(key)), String.class);
return parts[parts.length - 1];
}
private NBTStorage getOuterType() {
return NBTStorage.this;
}
@Override
public Object getRaw(String key) {
Tag tag = findLastTag(key);
if (tag == null)
return null;
return tag.getValue();
}
@Override
public DataKey getRelative(String relative) {
return new NBTKey(createRelativeKey(relative));
}
@Override
public String getString(String key) {
Tag tag = findLastTag(key);
if (tag == null || !(tag instanceof StringTag))
return "";
return ((StringTag) tag).getValue();
}
@Override
public Iterable<DataKey> getSubKeys() {
Tag tag = findLastTag(path, false);
if (!(tag instanceof CompoundTag))
return Collections.emptyList();
List<DataKey> subKeys = Lists.newArrayList();
for (String name : ((CompoundTag) tag).getValue().keySet()) {
subKeys.add(new NBTKey(createRelativeKey(name)));
}
return subKeys;
}
@Override
public Map<String, Object> getValuesDeep() {
Tag tag = findLastTag(path, false);
if (!(tag instanceof CompoundTag))
return Collections.emptyMap();
Queue<Node> node = new ArrayDeque<Node>(ImmutableList.of(new Node(tag)));
Map<String, Object> values = Maps.newHashMap();
while (!node.isEmpty()) {
Node root = node.poll();
for (Entry<String, Tag> entry : root.values.entrySet()) {
String key = createRelativeKey(root.parent, entry.getKey());
if (entry.getValue() instanceof CompoundTag) {
node.add(new Node(key, entry.getValue()));
continue;
}
values.put(key, entry.getValue().getValue());
}
}
return values;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
}
@Override
public boolean keyExists(String key) {
return findLastTag(createRelativeKey(key)) != null;
}
@Override
public String name() {
int last = path.lastIndexOf('.');
return path.substring(last == 0 ? 0 : last + 1);
}
private void putTag(String key, Tag tag) {
String[] parts = Iterables.toArray(Splitter.on('.').split(createRelativeKey(key)), String.class);
Map<String, Tag> parent = root;
for (int i = 0; i < parts.length - 1; ++i) {
if (!parent.containsKey(parts[i]) || !(parent.get(parts[i]) instanceof CompoundTag)) {
parent.put(parts[i], new CompoundTag(parts[i]));
}
parent = ((CompoundTag) parent.get(parts[i])).getValue();
}
parent.put(tag.getName(), tag);
}
@Override
public void removeKey(String key) {
String[] parts = Iterables.toArray(Splitter.on('.').split(createRelativeKey(key)), String.class);
Map<String, Tag> parent = findLastParent(parts);
parent.remove(parts[parts.length - 1]);
}
@Override
public void setBoolean(String key, boolean value) {
putTag(key, new ByteTag(getNameFor(key), (byte) (value ? 1 : 0)));
}
@Override
public void setDouble(String key, double value) {
putTag(key, new DoubleTag(getNameFor(key), value));
}
@Override
public void setInt(String key, int value) {
putTag(key, new IntTag(getNameFor(key), value));
}
@Override
public void setLong(String key, long value) {
putTag(key, new LongTag(getNameFor(key), value));
}
@Override
public void setRaw(String key, Object value) {
Tag tag = NBTUtils.createTag(getNameFor(key), value);
if (tag == null)
throw new IllegalArgumentException("could not convert value to tag");
putTag(key, tag);
}
@Override
public void setString(String key, String value) {
putTag(key, new StringTag(getNameFor(key), value));
}
}
private static class Node {
final String parent;
final Map<String, Tag> values;
public Node(String parent, Tag tag) {
this.parent = parent;
values = ((CompoundTag) tag).getValue();
}
public Node(Tag tag) {
this("", tag);
}
}
}