/
SavableMapFlagTracker.java
266 lines (239 loc) · 9.74 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
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
package com.denizenscript.denizencore.flags;
import com.denizenscript.denizencore.DenizenCore;
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.debugging.Debug;
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.putObject(valueString, ObjectFetcher.pickObjectFor(string, CoreUtilities.noDebugContext));
}
}
return map;
}
public String getString() {
if (string == null) {
if (map.containsKey(expirationString) || map.getObject(valueString) instanceof MapTag) {
string = map.savable();
}
else {
string = map.getObject(valueString).savable();
}
}
return string;
}
}
public HashMap<StringHolder, SaveOptimizedFlag> map;
public boolean modified;
public SavableMapFlagTracker() {
map = new HashMap<>();
}
public SavableMapFlagTracker(String input) {
input = input.replace("\r", "");
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);
if (CoreConfiguration.debugVerbose) {
Debug.log("Verbose: MapFlagTracker, loading flag " + key + " as " + value);
}
}
startOfLine = eol + 1;
eol = input.indexOf('\n', eol + 1);
}
}
@Override
public void doTotalClean() {
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Verbose - savable tracker is beginning doTotalClean");
}
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.getObject(expirationString);
hasSubMap = val.map.getObject(valueString).canBeType(MapTag.class);
}
else if (val.string.startsWith("map@")) {
MapTag quickMap = MapTag.valueOf(val.string, CoreUtilities.noDebugContext, false);
if (CoreConfiguration.debugVerbose) {
Debug.log("Verbose: MapFlagTracker, quickMap = " + quickMap.debuggable());
}
ObjectTag time = quickMap.getObject(expirationString);
if (time != null) {
expireTime = TimeTag.valueOf(time.toString(), CoreUtilities.noDebugContext);
}
hasSubMap = quickMap.getObject(valueString).canBeType(MapTag.class);
}
if (isExpired(expireTime)) {
toRemove.add(entry.getKey());
modified = true;
}
else if (hasSubMap) {
ObjectTag subValue = val.getMap().getObject(valueString);
if (subValue instanceof MapTag) {
if (doClean((MapTag) subValue)) {
val.string = null;
modified = true;
}
}
}
}
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Verbose - savable tracker has finished doTotalClean and will remove " + toRemove.size());
}
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.containsKey(expirationString) || value.getObject(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, boolean doClean) {
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Verbose - loading flag file path at " + filePath);
}
String content = CoreUtilities.journallingLoadFile(filePath + ".dat");
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Verbose - loaded flag content for " + filePath + " as " + (content == null ? "null" : content.length()));
}
if (content == null) {
return new SavableMapFlagTracker();
}
SavableMapFlagTracker tracker = new SavableMapFlagTracker(content);
if (CoreConfiguration.debugVerbose) {
Debug.echoError("Verbose - loading flag file path at " + filePath + " to tracker of " + tracker.map.size() + " flags... doClean=" + doClean);
}
if (doClean && !CoreConfiguration.skipAllFlagCleanings) {
tracker.doTotalClean();
}
return tracker;
}
public void saveToFile(String filePath) {
saveToFile(filePath, true);
}
public void saveToFile(String filePath, boolean lockUntilDone) {
String data = toString();
Runnable run = () -> CoreUtilities.journallingFileSave(filePath + ".dat", data);
if (lockUntilDone) {
run.run();
}
else {
DenizenCore.runAsync(run);
}
}
}