-
Notifications
You must be signed in to change notification settings - Fork 0
/
Uristmaps.java
391 lines (331 loc) · 12.5 KB
/
Uristmaps.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
package org.uristmaps;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.minlog.Log;
import org.ini4j.Wini;
import org.uristmaps.data.*;
import org.uristmaps.tasks.*;
import org.uristmaps.util.BuildFiles;
import org.uristmaps.util.ExportFiles;
import org.uristmaps.util.FileWatcher;
import org.uristmaps.util.OutputFiles;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
import static org.uristmaps.util.Util.ANSI_RED;
import static org.uristmaps.util.Util.ANSI_RESET;
/**
* Entry point for uristmaps application
*/
public class Uristmaps {
public static final String VERSION = "0.3.1";
/**
* The global configuration object.
*/
public static Wini conf;
/**
* The global kryo object.
*/
public static Kryo kryo;
/**
* The global file watcher object.
*/
public static FileWatcher files;
/**
* Entry point of the application.
*
* Runs all available tasks.
* @param args
*/
public static void main(String[] args) {
// Load configuration file
Log.info("Uristmaps " + VERSION);
loadConfig(args);
initKryo();
initLogger();
initDirectories();
initFileInfo();
// Set logger to debug if flag is set in config
if (conf.get("App", "debug", Boolean.class)) {
Log.DEBUG();
Log.info("Enabled Debug Logging");
}
// Fill the executor with all available tasks.
TaskExecutor executor = new TaskExecutor();
// No file management for this task until subtasks are possible.
executor.addTaskGroup(new TilesetsTaskGroup());
executor.addTask("BmpConvertTask", () -> BmpConverter.convert());
executor.addTask("SitesGeojson",
new File[]{BuildFiles.getSitesFile(),
BuildFiles.getWorldFile(),
BuildFiles.getSitemapsIndex()},
OutputFiles.getSitesGeojson(),
() -> WorldSites.geoJson());
executor.addTask("Sites",
new File[]{ExportFiles.getLegendsXML(),
ExportFiles.getPopulationFile(),
BuildFiles.getStructureGroups(),
BuildFiles.getStructureGroupsDefinitions()},
BuildFiles.getSitesFile(),
() -> WorldSites.load());
executor.addTask("CompileUristJs",
OutputFiles.getPopulationJs(),
OutputFiles.getUristJs(),
() -> TemplateRenderer.compileUristJs());
executor.addTask(new CompileIndexTask());
executor.addTask("DistResources", () -> FileCopier.distResources());
executor.addTask("PopulationHeatmaps",
new File[0],
OutputFiles.getPopulationJs(),
() -> Heatmaps.writePopulationsJS());
executor.addTask("WorldInfo",
new File[] {ExportFiles.getWorldHistory(),
ExportFiles.getBiomeMap()},
BuildFiles.getWorldFile(),
() -> WorldInfo.load());
executor.addTask("BiomeInfoTask",
ExportFiles.getBiomeMap(),
BuildFiles.getBiomeInfo(),
() -> BiomeInfo.load());
executor.addTaskGroup(new SatRendererTaskGroup());
List<File> dependantForSitemaps = new LinkedList<>();
dependantForSitemaps.add(BuildFiles.getSitemapsIndex());
dependantForSitemaps.addAll(Arrays.asList(OutputFiles.getAllSiteMaps()));
executor.addTask("LoadSitemaps",
ExportFiles.getAllSitemaps(),
dependantForSitemaps.toArray(new File[0]),
() -> Sitemaps.load());
executor.addTask("CopySiteMaps",
ExportFiles.getAllSitemaps(),
OutputFiles.getAllSiteMaps(),
() -> Sitemaps.copy());
executor.addTask("GroupStructures",
new File[] {ExportFiles.getStructuresMap(),
BuildFiles.getWorldFile()},
new File[]{BuildFiles.getStructureGroups(),
BuildFiles.getStructureGroupsDefinitions()},
() -> StructureGroups.load());
executor.addTask(new FullBuildMetaTask());
executor.addTask("Host", () -> WebServer.start());
// Parse more parameters
for (String arg : args) {
if (arg.equalsIgnoreCase("tasks") ||arg.equalsIgnoreCase("list")) {
echoTasks(executor);
return;
} else if (arg.equalsIgnoreCase("forget")) {
forgetInputFiles();
return;
} else if (arg.equalsIgnoreCase("help")) {
usage();
return;
}
}
// Check if we can stop initializing here and start the web server.
for (String arg : args) {
if (arg.equalsIgnoreCase("host")) {
executor.exec("Host");
return;
}
}
// Run the default task or the requested task.
Log.info("Starting full build");
executor.exec("BmpConvertTask", "FullBuild");
}
/**
* Print some help text.
*/
private static void usage() {
System.out.println("Usage: ");
System.out.println("\tlist\tTo list all available tasks.");
System.out.println("\tforget\tTo forget file states and not skip any tasks.");
System.out.println("\thost\tTo start the local webserver.");
}
/**
* Remove the file state store so all tasks will run as if its their first time, in the next run.
*/
private static void forgetInputFiles() {
System.out.println("Forgetting all file states.");
files.forget();
}
/**
* Print all public tasks.
* @param executor
*/
private static void echoTasks(TaskExecutor executor) {
// List all available tasks
System.out.println("Available tasks:");
TreeSet<String> taskNames = new TreeSet<>(executor.getTasks());
taskNames.stream().filter(name -> executor.getTask(name).isPublic()).forEach(System.out::println);
}
public static void Old() {
// TODO: Load regions info
// TODO: Render region labels
// TODO: Place region labels
}
private static void initFileInfo() {
files = new FileWatcher();
}
/**
* Make sure the output directories exist.
*/
private static void initDirectories() {
new File(conf.fetch("Paths", "output")).mkdirs();
new File(conf.fetch("Paths", "build")).mkdirs();
new File(conf.fetch("Paths", "tilesets")).mkdirs();
}
/**
* Configure the logger used for output.
*/
private static void initLogger() {
Log.setLogger(new FilteringLogger(conf.get("App", "log_blacklist").split(",")));
}
/**
* Configure the kryo instance used for reading/writing objects.
*/
private static void initKryo() {
kryo = new Kryo();
kryo.register(Coord2.class);
kryo.register(Site.class);
kryo.register(FileInfo.class);
kryo.register(WorldInfo.class);
kryo.register(StructureGroup.class);
kryo.register(SitemapInfo.class);
}
/**
* Load the config ini-style file.
*/
private static void loadConfig(String[] args) {
File targetFile = new File("config.cfg"); // Default config location
// Read config file path from ARGS if provided
for (int i= 0; i < args.length; i++) {
if (args[i].equals("-c")) {
if (i + 1 < args.length) {
targetFile = new File(args[i+1]);
break;
}
}
}
try {
conf = new Wini(targetFile);
} catch (IOException e) {
if (!targetFile.exists()){
Log.error("Could not find the config file: " + targetFile);
}
System.exit(1);
}
// Validate config
List<String> missing = new LinkedList<>();
validateEntry("Paths", "export", missing);
validateEntry("Paths", "region_name", missing);
validateEntry("Paths", "build", missing);
validateEntry("Paths", "output", missing);
validateEntry("Paths", "tiles", missing);
validateEntry("Paths", "tilesets", missing);
validateEntry("App", "processes", missing);
validateEntry("App", "debug", missing);
validateEntry("App", "log_blacklist", missing);
validateEntry("Map", "max_zoom", missing);
validateEntry("Map", "min_zoom", missing);
validateEntry("Map", "max_cluster_radius", missing);
validateEntry("Map", "show_spoilers", missing);
validateEntry("Map", "map_font", missing);
validateEntry("Output", "footer", missing);
validateEntry("Web", "port", missing);
if (!missing.isEmpty()) {
for (String msg : missing) {
Log.error(String.format(ANSI_RED + "Entry missing in config file: %s" + ANSI_RESET, msg));
}
System.exit(1);
}
}
/**
* Make sure the provided entry is in the given category of the config. If not, add
* an entry to the list.
* @param category
* @param entry
* @param missing
*/
private static void validateEntry(String category, String entry, List<String> missing) {
if (conf.get(category, entry) == null) {
missing.add(String.format("%s->%s", category, entry));
}
}
/**
* A logger that can ignore items belonging to a set category.
*/
static public class FilteringLogger extends Log.Logger {
private final Set<String> blacklist;
private long startUpTime = new Date().getTime();
/**
* Create a new filtering logger with the provided blacklist.
* @param blacklist
*/
public FilteringLogger(String[] blacklist) {
if (blacklist == null) {
this.blacklist = new HashSet<>();
} else {
this.blacklist = new HashSet<>();
for (String item : blacklist) {
this.blacklist.add(item.toLowerCase().trim());
}
}
}
/**
* Drop log messages from blacklisted categories.
* @param level
* @param category
* @param message
* @param ex
*/
public void log (int level, String category, String message, Throwable ex) {
if (level == Log.LEVEL_DEBUG && category != null && blacklist.contains(category.toLowerCase())) {
return;
}
logMsg(level, category, message, ex);
}
private void logMsg(int level, String category, String message, Throwable ex) {
StringBuilder builder = new StringBuilder(256);
long time = new Date().getTime() - startUpTime;
long minutes = time / (1000 * 60);
long seconds = time / (1000) % 60;
if (minutes <= 9) builder.append('0');
builder.append(minutes);
builder.append(':');
if (seconds <= 9) builder.append('0');
builder.append(seconds);
switch (level) {
case Log.LEVEL_ERROR:
//builder.append(Util.ANSI_RED);
builder.append(" ERROR: ");
break;
case Log.LEVEL_WARN:
//builder.append(Util.ANSI_YELLOW);
builder.append(" WARN: ");
break;
case Log.LEVEL_INFO:
builder.append(" INFO: ");
break;
case Log.LEVEL_DEBUG:
//builder.append(Util.ANSI_GREEN);
builder.append(" DEBUG: ");
break;
case Log.LEVEL_TRACE:
builder.append(" TRACE: ");
break;
}
if (category != null) {
builder.append('[').append(category).append("] ");
}
builder.append(message);
if (ex != null) {
StringWriter writer = new StringWriter(256);
ex.printStackTrace(new PrintWriter(writer));
builder.append('\n');
builder.append(writer.toString().trim());
}
//builder.append(Util.ANSI_RESET);
print(builder.toString());
}
}
}