ExposableConfigWriter.java
7.73 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
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
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.modconfig;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.InstanceCreator;
/**
* Manages serialisation of exposable properties to a JSON config file via Gson
*
* @author Adam Mummery-Smith
*/
public final class ExposableConfigWriter implements InstanceCreator<Exposable>
{
/**
* Minimum number of milliseconds that must elapse between writes
*/
private static final long ANTI_HAMMER_DELAY = 1000L;
/**
* Exposable instance which we will serialise exposed properties
*/
private final Exposable exposable;
/**
* JSON file to write to
*/
private final File configFile;
/**
* True if this is a versioned config strategy
*/
private final boolean versioned;
/**
* Disable anti-hammer and always save when requested
*/
private final boolean aggressive;
/**
* Gson instance
*/
private final Gson gson;
/**
* True if a config write has been requested but anti-hammer has prevented
* the write from occurring.
*/
private volatile boolean dirty = false;
/**
* Last time the config was written, used for anti-hammer
*/
private volatile long lastWrite = 0L;
/**
* It's possible that writes may be requested from different threads, lock
* object to prevent cross-thread derp.
*/
private Object readWriteLock = new Object();
/**
* @param exposable
* @param configFile
*/
private ExposableConfigWriter(Exposable exposable, File configFile, boolean versioned, boolean aggressive)
{
this.exposable = exposable;
this.configFile = configFile;
this.versioned = versioned;
this.aggressive = aggressive;
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.serializeNulls();
gsonBuilder.excludeFieldsWithoutExposeAnnotation();
gsonBuilder.registerTypeAdapter(exposable.getClass(), this);
if (this.exposable instanceof AdvancedExposable)
{
((AdvancedExposable)this.exposable).setupGsonSerialiser(gsonBuilder);
}
this.gson = gsonBuilder.create();
}
/**
* Get the config file underlying this writer
*/
File getConfigFile()
{
return this.configFile;
}
/**
* Returns true if this writer is using a versioned strategy
*/
boolean isVersioned()
{
return this.versioned;
}
/**
* Returns true if anti-hammer is disabled for this writer
*/
public boolean isAggressive()
{
return this.aggressive;
}
/**
* Returns true if this writer has been invalidated but not yet been flushed
* to disk.
*/
boolean isDirty()
{
return this.dirty;
}
/* (non-Javadoc)
* @see com.google.gson.InstanceCreator
* #createInstance(java.lang.reflect.Type)
*/
@Override
public Exposable createInstance(Type type)
{
return this.exposable;
}
/**
* Initialise the config, reads from file and writes the initial config file
* if not present.
*/
void init()
{
// Read the config
this.read();
// If the config doesn't exist yet, seed the config
if (!this.configFile.exists())
{
this.write();
}
}
/**
* Read the config from the file
*/
void read()
{
synchronized (this.readWriteLock)
{
if (this.configFile.exists())
{
FileReader reader = null;
try
{
reader = new FileReader(this.configFile);
// Normally GSON would produce a new object by calling the default constructor, but we
// trick it into deserialising properties on the existing object instance by implementing
// an InstanceCreator which just returns the object instance which we already have
this.gson.fromJson(reader, this.exposable.getClass());
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (reader != null)
{
reader.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
}
/**
* Write the config to the file
*/
void write()
{
synchronized (this.readWriteLock)
{
FileWriter writer = null;
try
{
writer = new FileWriter(this.configFile);
this.gson.toJson(this.exposable, writer);
this.dirty = false;
this.lastWrite = System.currentTimeMillis();
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
if (writer != null)
{
writer.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}
/**
* Write the config to file, respecting anti-hammer and queuing the write if
* not enough time has elapsed.
*/
void invalidate()
{
if (!this.aggressive)
{
long sinceLastWrite = System.currentTimeMillis() - this.lastWrite;
if (sinceLastWrite < ANTI_HAMMER_DELAY)
{
this.dirty = true;
return;
}
}
this.write();
}
/**
* Handle latent writes if the config was previously invalidated
*/
void onTick()
{
if (!this.aggressive && this.dirty)
{
long sinceLastWrite = System.currentTimeMillis() - this.lastWrite;
if (sinceLastWrite >= ANTI_HAMMER_DELAY)
{
this.write();
}
}
}
/**
* Force a write if dirty
*/
void sync()
{
if (this.dirty || this.aggressive)
{
this.write();
}
}
/**
* Factory method which creates and intialises a new ExposableConfigWriter
* for the specified exposable object and strategy.
*
* @param exposable
* @param strategy
* @param fileName
*/
static ExposableConfigWriter create(Exposable exposable, ConfigStrategy strategy, String fileName, boolean aggressive)
{
if (!fileName.toLowerCase().endsWith(".json"))
{
fileName = fileName + ".json";
}
File configFile = strategy.getFileForStrategy(fileName);
if (exposable instanceof AdvancedExposable)
{
File customConfigFile = ((AdvancedExposable)exposable).getConfigFile(configFile, configFile.getParentFile(), fileName);
if (customConfigFile != null)
{
configFile = customConfigFile;
}
}
ExposableConfigWriter writer = new ExposableConfigWriter(exposable, configFile, strategy == ConfigStrategy.Versioned, aggressive);
return writer;
}
}