-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
SavableMapFlagTracker.java
230 lines (204 loc) · 8.02 KB
/
SavableMapFlagTracker.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
package com.denizenscript.denizencore.flags;
import com.denizenscript.denizencore.objects.ObjectFetcher;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.core.TimeTag;
import com.denizenscript.denizencore.utilities.AsciiMatcher;
import com.denizenscript.denizencore.utilities.CoreConfiguration;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class SavableMapFlagTracker extends MapTagBasedFlagTracker {
public static class SaveOptimizedFlag {
public MapTag map;
public String string;
public boolean canExpire;
public MapTag getMap() {
if (map == null) {
if (string.startsWith("map@")) {
map = MapTag.valueOf(string, CoreUtilities.noDebugContext);
}
else {
map = new MapTag();
map.map.put(valueString, ObjectFetcher.pickObjectFor(string, CoreUtilities.noDebugContext));
}
}
return map;
}
public String getString() {
if (string == null) {
if (map.map.containsKey(expirationString) || map.map.get(valueString) instanceof MapTag) {
string = map.savable();
}
else {
string = map.map.get(valueString).savable();
}
}
return string;
}
}
public HashMap<StringHolder, SaveOptimizedFlag> map;
public boolean modified;
public SavableMapFlagTracker() {
map = new HashMap<>();
}
public SavableMapFlagTracker(String input) {
map = new HashMap<>(input.length() / 50);
int eol = input.indexOf('\n');
int startOfLine = 0;
while (eol != -1) {
int colon = input.indexOf(':', startOfLine);
if (colon != -1) {
String key = input.substring(startOfLine, colon);
boolean expirable = key.startsWith("\\ex");
if (expirable) {
key = key.substring("\\ex".length());
}
key = unescapeKey(key);
String value = unescapeValue(input.substring(colon + 1, eol));
SaveOptimizedFlag flag = new SaveOptimizedFlag();
flag.canExpire = expirable;
flag.string = value;
map.put(new StringHolder(key), flag);
}
startOfLine = eol + 1;
eol = input.indexOf('\n', eol + 1);
}
doTotalClean();
}
public void doTotalClean() {
if (CoreConfiguration.skipAllFlagCleanings) {
return;
}
ArrayList<StringHolder> toRemove = new ArrayList<>();
for (Map.Entry<StringHolder, SaveOptimizedFlag> entry : map.entrySet()) {
SaveOptimizedFlag val = entry.getValue();
if (!val.canExpire) {
continue;
}
TimeTag expireTime = null;
boolean hasSubMap = false;
if (val.map != null) {
expireTime = (TimeTag) val.map.map.get(expirationString);
hasSubMap = val.map.map.get(valueString).canBeType(MapTag.class);
}
else if (val.string.startsWith("map@")) {
MapTag quickMap = MapTag.valueOf(val.string, CoreUtilities.noDebugContext, false);
ObjectTag time = quickMap.map.get(expirationString);
if (time != null) {
expireTime = TimeTag.valueOf(time.toString(), CoreUtilities.noDebugContext);
}
hasSubMap = quickMap.map.get(valueString).canBeType(MapTag.class);
}
if (isExpired(expireTime)) {
toRemove.add(entry.getKey());
modified = true;
}
else if (hasSubMap) {
ObjectTag subValue = val.getMap().map.get(valueString);
if (subValue instanceof MapTag) {
if (doClean((MapTag) subValue)) {
val.string = null;
modified = true;
}
}
}
}
for (StringHolder str : toRemove) {
map.remove(str);
}
}
@Override
public MapTag getRootMap(String key) {
SaveOptimizedFlag flag = map.get(new StringHolder(key));
if (flag == null) {
return null;
}
return flag.getMap();
}
@Override
public void setRootMap(String key, MapTag value) {
modified = true;
if (value == null) {
map.remove(new StringHolder(key));
return;
}
SaveOptimizedFlag flag = new SaveOptimizedFlag();
flag.map = value;
flag.string = null;
if (value.map.containsKey(expirationString) || value.map.get(valueString) instanceof MapTag) {
flag.canExpire = true;
}
map.put(new StringHolder(key), flag);
}
@Override
public Collection<String> listAllFlags() {
ArrayList<String> keys = new ArrayList<>(map.size());
for (StringHolder string : map.keySet()) {
keys.add(string.str);
}
return keys;
}
public static AsciiMatcher valueEscapeNeededMatcher = new AsciiMatcher("\0\n\\");
public static String unescapeValue(String key) {
if (!CoreUtilities.contains(key, '\\')) {
return key;
}
key = CoreUtilities.replace(key, "\\nl", "\n");
key = CoreUtilities.replace(key, "\\bs", "\\");
return key;
}
public static String escapeValue(String key) {
if (!valueEscapeNeededMatcher.containsAnyMatch(key)) {
return key;
}
key = CoreUtilities.replace(key, "\\", "\\bs");
key = CoreUtilities.replace(key, "\n", "\\nl");
key = CoreUtilities.replace(key, "\0", "");
return key;
}
public static AsciiMatcher keyEscapeNeededMatcher = new AsciiMatcher("\0:\n\\");
public static String unescapeKey(String key) {
if (!CoreUtilities.contains(key, '\\')) {
return key;
}
key = CoreUtilities.replace(key, "\\co", ":");
key = CoreUtilities.replace(key, "\\nl", "\n");
key = CoreUtilities.replace(key, "\\bs", "\\");
return key;
}
public static String escapeKey(String key) {
if (!keyEscapeNeededMatcher.containsAnyMatch(key)) {
return key;
}
key = CoreUtilities.replace(key, "\\", "\\bs");
key = CoreUtilities.replace(key, ":", "\\co");
key = CoreUtilities.replace(key, "\n", "\\nl");
key = CoreUtilities.replace(key, "\0", "");
return key;
}
@Override
public String toString() {
StringBuilder toOutput = new StringBuilder(map.size() * 100);
for (Map.Entry<StringHolder, SavableMapFlagTracker.SaveOptimizedFlag> flag : map.entrySet()) {
if (flag.getValue().canExpire) {
toOutput.append("\\ex");
}
toOutput.append(escapeKey(flag.getKey().str)).append(":").append(escapeValue(flag.getValue().getString())).append('\n');
}
return toOutput.toString();
}
public static SavableMapFlagTracker loadFlagFile(String filePath) {
String content = CoreUtilities.journallingLoadFile(filePath + ".dat");
if (content == null) {
return new SavableMapFlagTracker();
}
return new SavableMapFlagTracker(content);
}
public void saveToFile(String filePath) {
CoreUtilities.journallingFileSave(filePath + ".dat", toString());
}
}