-
Notifications
You must be signed in to change notification settings - Fork 16
/
xcom2json.cpp
627 lines (550 loc) · 16.8 KB
/
xcom2json.cpp
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include "xcom.h"
#include "util.h"
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <locale>
#include <cassert>
using namespace xcom;
static std::string escape(const std::string& str) {
std::string ret;
for (size_t i = 0; i < str.length(); ++i) {
switch (str[i])
{
case '"':
ret += "\\\"";
break;
case '\\':
ret += "\\\\";
break;
case '\n':
ret += "\\n";
break;
case '\r':
ret += "\\r";
break;
case '\t':
ret += "\\t";
break;
default:
if (str[i] > 0 && str[i] < ' ') {
std::string hex =
util::to_hex(reinterpret_cast<const unsigned char *>(&str[i]), 1);
ret += "\\u00";
ret += hex;
}
else {
ret += str[i];
}
}
}
return ret;
}
struct json_writer
{
json_writer(const std::string& filename) :
out(filename), indent_level(0), skip_indent(true), needs_comma(false)
{
out.setf(std::ofstream::boolalpha);
}
void indent()
{
if (needs_comma) {
out << ", ";
}
if (!skip_indent) {
out << std::endl;
std::string ind(2 * indent_level, ' ');
out << ind;
}
}
void begin_object(bool omit_newline = false)
{
indent();
out << "{ ";
++indent_level;
needs_comma = false;
skip_indent = omit_newline;
}
void end_object()
{
--indent_level;
if (needs_comma) {
out << " ";
}
needs_comma = false;
indent();
out << "}";
needs_comma = true;
skip_indent = false;
}
void begin_array(bool omit_newline = false)
{
indent();
out << "[ ";
++indent_level;
needs_comma = false;
skip_indent = omit_newline;
}
void end_array()
{
--indent_level;
if (needs_comma) {
out << " ";
}
needs_comma = false;
indent();
out << "]";
needs_comma = true;
skip_indent = false;
}
void end_item(bool omit_newline)
{
if (!omit_newline) {
skip_indent = false;
}
else {
skip_indent = true;
}
needs_comma = true;
}
void write_key(const std::string &name)
{
indent();
out << "\"" << name << "\": ";
skip_indent = true;
needs_comma = false;
}
void write_int(const std::string &name, int32_t val, bool omit_newline = false)
{
write_key(name);
out << val;
end_item(omit_newline);
}
void write_raw_int(int val, bool omit_newline = false)
{
indent();
out << val;
end_item(omit_newline);
}
void write_float(const std::string &name, float val, bool omit_newline = false)
{
write_key(name);
out << (val + 0.0f);
end_item(omit_newline);
}
void write_raw_float(float val, bool omit_newline = false)
{
indent();
out << val;
end_item(omit_newline);
}
void write_string(const std::string &name, const std::string &val,
bool omit_newline = false)
{
write_key(name);
out << "\"" << escape(val) << "\"";
end_item(omit_newline);
}
void write_unicode_string(const std::string& name, const xcom_string& str)
{
write_key(name);
begin_object(true);
write_string("str", str.str, true);
write_bool("is_wide", str.is_wide, true);
end_object();
}
void write_raw_unicode_string(const xcom_string& str)
{
begin_object(true);
write_string("str", str.str, true);
write_bool("is_wide", str.is_wide, true);
end_object();
}
void write_raw_string(const std::string& val, bool omit_newline = false)
{
indent();
out << "\"" << escape(val) << "\"";
end_item(omit_newline);
}
void write_bool(const std::string &name, bool val, bool omit_newline = false)
{
write_key(name);
out << val;
end_item(omit_newline);
}
private:
std::ofstream out;
size_t indent_level;
bool skip_indent;
bool needs_comma;
};
struct json_property_visitor : public property_visitor
{
json_property_visitor(json_writer &writer, const actor_table &ga,
const actor_table &la) :
w(writer), global_actors(ga), local_actors(la) {}
void write_common(property* prop, bool omit_newline = false)
{
w.write_string("name", prop->name, omit_newline);
w.write_string("kind", prop->kind_string(), omit_newline);
}
virtual void visit(int_property *prop) override
{
w.begin_object(true);
write_common(prop, true);
w.write_int("value", prop->value, true);
w.end_object();
}
virtual void visit(float_property *prop) override
{
w.begin_object(true);
write_common(prop, true);
w.write_float("value", prop->value, true);
w.end_object();
}
virtual void visit(bool_property *prop) override
{
w.begin_object(true);
write_common(prop, true);
w.write_bool("value", prop->value, true);
w.end_object();
}
virtual void visit(string_property *prop) override
{
w.begin_object(true);
write_common(prop, true);
w.write_unicode_string("value", prop->str);
w.end_object();
}
virtual void visit(name_property *prop) override
{
w.begin_object(true);
write_common(prop, true);
w.write_string("string", prop->str, true);
w.write_int("number", prop->number, true);
w.end_object();
}
virtual void visit(object_property *prop) override
{
w.begin_object(true);
write_common(prop, true);
w.write_int("actor", prop->actor, true);
w.end_object();
}
virtual void visit(enum_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_string("type", prop->type);
w.write_string("value", prop->value.name);
w.write_int("number", prop->value.number);
w.end_object();
}
virtual void visit(struct_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_string("struct_name", prop->struct_name);
if (prop->native_data_length > 0) {
w.write_string("native_data",
util::to_hex(prop->native_data.get(), prop->native_data_length));
w.write_key("properties");
w.begin_array(true);
w.end_array();
}
else {
w.write_string("native_data", "");
w.write_key("properties");
w.begin_array();
std::for_each(prop->properties.begin(), prop->properties.end(),
[this](const property_ptr& v) {
json_property_visitor visitor(*this);
v->accept(&visitor);
});
w.end_array();
}
w.end_object();
}
virtual void visit(array_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_int("data_length", prop->data_length);
w.write_int("array_bound", prop->array_bound);
std::string data_str = (prop->array_bound > 0) ?
util::to_hex(prop->data.get(), prop->data_length) : "";
w.write_string("data", data_str);
w.end_object();
}
virtual void visit(object_array_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_key("actors");
w.begin_array(true);
for (unsigned int i = 0; i < prop->elements.size(); ++i) {
w.write_raw_int(prop->elements[i], true);
}
w.end_array();
w.end_object();
}
virtual void visit(number_array_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_key("elements");
w.begin_array(true);
for (int32_t v : prop->elements) {
w.write_raw_int(v, true);
}
w.end_array();
w.end_object();
}
virtual void visit(string_array_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_key("strings");
w.begin_array();
for (const xcom_string& s : prop->elements) {
w.write_raw_unicode_string(s);
}
w.end_array();
w.end_object();
}
virtual void visit(enum_array_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_key("enum_values");
w.begin_array();
for (const enum_value& s : prop->elements) {
w.begin_object(true);
w.write_string("value", s.name, true);
w.write_int("number", s.number, true);
w.end_object();
}
w.end_array();
w.end_object();
}
virtual void visit(struct_array_property *prop) override
{
w.begin_object();
write_common(prop);
w.write_key("structs");
w.begin_array();
std::for_each(prop->elements.begin(), prop->elements.end(),
[this](const property_list& proplist) {
w.begin_array();
std::for_each(proplist.begin(), proplist.end(),
[this](const property_ptr& p) {
p->accept(this);
});
w.end_array();
});
w.end_array();
w.end_object();
}
// Return true if we can condense a static array of strings into just a series of string literals in an array instead of having
// a big array of nested string properties. This is the case as long as none of the strings in question are wide.
// TODO: Probably need to revisit this for non-INT games where it's possibly a lot more likely that all the strings will be UTF-16.
bool can_condense_string_array(const static_array_property& static_array)
{
for (const property_ptr& prop : static_array.properties) {
const string_property *string_prop = dynamic_cast<const string_property*>(prop.get());
assert(string_prop != nullptr);
if (string_prop->str.is_wide) {
return false;
}
}
return true;
}
virtual void visit(static_array_property *prop) override
{
w.begin_object();
write_common(prop);
// If this array holds well-known subtypes (int), write them right in this node instead of repeating it
if (prop->properties.size() > 0 && prop->properties[0]->kind == property::kind_t::int_property) {
w.write_key("int_values");
w.begin_array(true);
for (const property_ptr& v : prop->properties) {
const int_property *int_prop = dynamic_cast<const int_property*>(v.get());
w.write_raw_int(int_prop->value, true);
}
w.end_array();
}
else if (prop->properties.size() > 0 && prop->properties[0]->kind == property::kind_t::string_property && can_condense_string_array(*prop)) {
w.write_key("string_values");
w.begin_array(true);
for (const property_ptr& v : prop->properties) {
const string_property *string_prop = dynamic_cast<const string_property*>(v.get());
w.write_raw_string(string_prop->str.str, true);
}
w.end_array();
}
else {
w.write_key("properties");
w.begin_array();
for (const property_ptr& v : prop->properties) {
v->accept(this);
}
w.end_array();
}
w.end_object();
}
json_writer& w;
const actor_table &global_actors;
const actor_table &local_actors;
};
static void checkpoint_to_json(const checkpoint & chk, json_writer& w,
const actor_table& global_actors, const actor_table& local_actors)
{
w.begin_object();
w.write_string("name", chk.name);
w.write_string("instance_name", chk.instance_name);
w.write_string("class_name", chk.class_name);
w.write_key("vector");
w.begin_array(true);
for (const auto& i : chk.vector) {
w.write_raw_float(i, true);
}
w.end_array();
w.write_key("rotator");
w.begin_array(true);
for (const auto& i : chk.rotator) {
w.write_raw_int(i, true);
}
w.end_array();
w.write_key("properties");
w.begin_array();
std::for_each(chk.properties.begin(), chk.properties.end(),
[&w, &global_actors, &local_actors](const property_ptr& v) {
json_property_visitor visitor{ w, global_actors, local_actors };
v->accept(&visitor);
});
w.end_array();
w.write_int("template_index", chk.template_index);
w.write_int("pad_size", chk.pad_size);
w.end_object();
}
static void checkpoint_chunk_to_json(const checkpoint_chunk& chk,
json_writer &w, const saved_game& save)
{
w.begin_object();
w.write_int("unknown_int1", chk.unknown_int1);
w.write_string("game_type", chk.game_type);
w.write_key("checkpoint_table");
w.begin_array();
std::for_each(chk.checkpoints.begin(), chk.checkpoints.end(),
[&w, &save, &chk](const checkpoint& v) {
checkpoint_to_json(v, w, save.actors, chk.actors);
});
w.end_array();
w.write_int("unknown_int2", chk.unknown_int2);
w.write_string("class_name", chk.class_name);
w.write_key("actor_table");
w.begin_array();
std::for_each(chk.actors.begin(), chk.actors.end(),
[&w](const std::string& a) { w.write_raw_string(a); }
);
w.end_array();
w.write_int("unknown_int3", chk.unknown_int3);
w.write_string("display_name", chk.display_name);
w.write_string("map_name", chk.map_name);
w.write_int("unknown_int4", chk.unknown_int4);
w.end_object();
}
void buildJson(const saved_game& save, json_writer& w)
{
w.begin_object();
// Write the header
const header &hdr = save.hdr;
w.write_key("header");
w.begin_object();
w.write_int("version", static_cast<uint32_t>(hdr.version));
w.write_int("uncompressed_size", hdr.uncompressed_size);
w.write_int("game_number", hdr.game_number);
w.write_int("save_number", hdr.save_number);
w.write_unicode_string("save_description", hdr.save_description);
w.write_unicode_string("time", hdr.time);
w.write_string("map_command", hdr.map_command);
w.write_bool("tactical_save", hdr.tactical_save);
w.write_bool("ironman", hdr.ironman);
w.write_bool("autosave", hdr.autosave);
w.write_string("dlc", hdr.dlc);
w.write_string("language", hdr.language);
if (hdr.version == xcom_version::enemy_within_android) {
w.write_int("profile_number", hdr.profile_number);
w.write_unicode_string("profile_date", hdr.profile_date);
}
w.end_object();
w.write_key("actor_table");
w.begin_array();
std::for_each(save.actors.begin(), save.actors.end(),
[&w](const std::string& a) { w.write_raw_string(a); }
);
w.end_array();
w.write_key("checkpoints");
w.begin_array();
std::for_each(save.checkpoints.begin(), save.checkpoints.end(),
[&w, &save](const checkpoint_chunk& v) {
checkpoint_chunk_to_json(v, w, save); w.end_item(false);
});
w.end_array();
w.end_object();
}
void usage(const char * name)
{
printf("Usage: %s [-o <out_file>] <in_file>\n", name);
printf("-o -- Specify output file name, defaults to <in_file>.json\n");
}
int main(int argc, char *argv[])
{
std::string infile;
std::string outfile;
std::string tmpfile;
if (argc <= 1) {
usage(argv[0]);
return 1;
}
setlocale(LC_ALL, "en_US.utf8");
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-o") == 0) {
if (argc <= (i+1)) {
usage(argv[0]);
return 1;
}
outfile = argv[++i];
}
else {
if (!infile.empty()) {
usage(argv[0]);
return 1;
}
infile = argv[i];
}
}
if (infile.empty()) {
usage(argv[0]);
return 1;
}
if (outfile.empty()) {
outfile = infile + ".json";
}
try {
saved_game save = read_xcom_save(infile);
json_writer w{ outfile };
buildJson(save, w);
return 0;
}
catch (const error::xcom_exception& e) {
fprintf(stderr, "%s", e.what().c_str());
return 1;
}
fprintf(stderr, "Error: unknown error.\n");
return 1;
}