forked from pydt/civ6-save-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
477 lines (383 loc) · 12.1 KB
/
index.js
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
'use strict';
require('buffer-v6-polyfill');
// Workaround to detect buggy buffer.from support (which exists on lambda's node v4.3.2)
let useNewBuffer = false;
try {
Buffer.from('1337', 'hex');
} catch(e) {
useNewBuffer = true;
}
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const util = require('util');
const zlib = require('zlib');
const iconv = require('iconv-lite');
const diacritics = require('diacritics');
const START_ACTOR = new Buffer([0x58, 0xBA, 0x7F, 0x4C]);
const ZLIB_HEADER = new Buffer([0x78, 0x9C]);
const WEIRD_OUTBACK_TYCOON_MARKER = new Buffer([1, 0xDB, 0x89, 0x32]);
const END_UNCOMPRESSED = new Buffer([0, 0, 1, 0]);
const COMPRESSED_DATA_END = new Buffer([0, 0, 0xFF, 0xFF]);
const GAME_DATA = {
GAME_TURN: new Buffer([0x9D, 0x2C, 0xE6, 0xBD]),
GAME_SPEED: new Buffer([0x99, 0xB0, 0xD9, 0x05]),
MOD_BLOCK_1: new Buffer([0x5C, 0xAE, 0x27, 0x84]),
MOD_BLOCK_2: new Buffer([0xC8, 0xD1, 0x8C, 0x1B]),
MOD_BLOCK_3: new Buffer([0x44, 0x7F, 0xD4, 0xFE]),
MOD_ID: new Buffer([0x54, 0x5F, 0xC4, 0x04]),
MOD_TITLE: new Buffer([0x72, 0xE1, 0x34, 0x30]),
MAP_FILE: new Buffer([0x5A, 0x87, 0xD8, 0x63]),
MAP_SIZE: new Buffer([0x40, 0x5C, 0x83, 0x0B])
};
const SLOT_HEADERS = [
new Buffer([0xC8, 0x9B, 0x5F, 0x65]),
new Buffer([0x5E, 0xAB ,0x58, 0x12]),
new Buffer([0xE4, 0xFA, 0x51, 0x8B]),
new Buffer([0x72, 0xCA, 0x56, 0xFC]),
new Buffer([0xD1, 0x5F, 0x32 ,0x62]),
new Buffer([0x47, 0x6F, 0x35, 0x15]),
new Buffer([0xFD, 0x3E, 0x3C, 0x8C]),
new Buffer([0x6B, 0x0E, 0x3B, 0xFB]),
new Buffer([0xFA, 0x13, 0x84, 0x6B]),
new Buffer([0x6C, 0x23, 0x83, 0x1C]),
new Buffer([0xF4, 0x14, 0x18, 0xAA]),
new Buffer([0x62, 0x24, 0x1F, 0xDD])
];
const ACTOR_DATA = {
ACTOR_NAME: new Buffer([0x2F, 0x5C, 0x5E, 0x9D]),
LEADER_NAME: new Buffer([0x5F, 0x5E, 0xCD, 0xE8]),
ACTOR_TYPE: new Buffer([0xBE, 0xAB, 0x55, 0xCA]),
PLAYER_NAME: new Buffer([0xFD, 0x6B, 0xB9, 0xDA]),
PLAYER_PASSWORD: new Buffer([0x6C, 0xD1, 0x7C, 0x6E]),
PLAYER_ALIVE: new Buffer([0xA6, 0xDF, 0xA7, 0x62]),
IS_CURRENT_TURN: new Buffer([0xCB, 0x21, 0xB0, 0x7A]),
ACTOR_AI_HUMAN: new Buffer([0x95, 0xB9, 0x42, 0xCE]), // 3 = Human, 1 = AI
ACTOR_DESCRIPTION: new Buffer([0x65, 0x19, 0x9B, 0xFF])
};
module.exports.MARKERS = {
START_ACTOR, END_UNCOMPRESSED, COMPRESSED_DATA_END, GAME_DATA, ACTOR_DATA
};
const DATA_TYPES = {
BOOLEAN: 1,
INTEGER: 2,
STRING: 5,
UTF_STRING: 6,
ARRAY_START: 0x0A
};
module.exports.DATA_TYPES = DATA_TYPES;
module.exports.parse = (buffer, options) => {
options = options || {};
let parsed = {
ACTORS: [],
CIVS: []
};
const chunks = [];
let chunkStart = 0;
let curActor;
let state = readState(buffer);
if (state.next4.toString() !== 'CIV6') {
throw new Error('Not a Civilzation 6 save file. :(');
}
while (null !== (state = readState(buffer, state))) {
if (state.next4.equals(GAME_DATA.GAME_SPEED)) {
break;
}
state.pos++;
}
chunks.push(buffer.slice(chunkStart, state.pos));
chunkStart = state.pos;
do {
if (state.next4.equals(END_UNCOMPRESSED)) {
if (options.outputCompressed) {
readCompressedData(buffer, state, path.basename(filename) + '.bin');
}
break;
}
const info = parseEntry(buffer, state);
//console.log(info);
const tryAddActor = (key, marker) => {
if (info.marker.equals(marker)) {
curActor = {};
curActor[key] = info;
parsed.ACTORS.push(curActor);
}
};
for (let marker of SLOT_HEADERS) {
tryAddActor('SLOT_HEADER', marker);
}
if (!curActor && info.marker.equals(START_ACTOR)) {
tryAddActor('START_ACTOR', START_ACTOR);
} else if (info.marker.equals(ACTOR_DATA.ACTOR_DESCRIPTION)) {
curActor = null;
} else {
for (let key in GAME_DATA) {
if (info.marker.equals(GAME_DATA[key])) {
parsed[key] = info;
}
}
if (curActor) {
for (let key in ACTOR_DATA) {
if (info.marker.equals(ACTOR_DATA[key])) {
curActor[key] = info;
}
}
}
}
info.chunk = buffer.slice(chunkStart, state.pos);
chunks.push(info.chunk);
chunkStart = state.pos;
} while (null !== (state = readState(buffer, state)));
if (state) {
chunks.push(buffer.slice(state.pos));
}
for (let curMarker of SLOT_HEADERS) {
const curCiv = _.find(parsed.ACTORS, actor => {
return actor.SLOT_HEADER &&
actor.SLOT_HEADER.marker.equals(curMarker) &&
actor.ACTOR_TYPE &&
actor.ACTOR_TYPE.data === 'CIVILIZATION_LEVEL_FULL_CIV';
});
if (curCiv) {
parsed.CIVS.push(curCiv);
_.pull(parsed.ACTORS, curCiv);
}
}
for (let actor of _.clone(parsed.ACTORS)) {
if (!actor.ACTOR_TYPE) {
_.pull(parsed.ACTORS, actor);
}
}
if (options.simple) {
parsed = simplify(parsed);
}
return {
parsed: parsed,
chunks: chunks
};
};
module.exports.addChunk = (chunks, after, marker, type, value) => {
const newChunk = writeValue(marker, type, value);
const chunkIndex = chunks.indexOf(after.chunk) + 1;
chunks.splice(chunkIndex, 0, newChunk);
};
module.exports.modifyChunk = (chunks, toModify, newValue) => {
const chunkIndex = chunks.indexOf(toModify.chunk);
chunks[chunkIndex] = toModify.chunk = writeValue(toModify.marker, toModify.type, newValue);
};
module.exports.deleteChunk = (chunks, toDelete) => {
_.pull(chunks, toDelete.chunk);
}
if (!module.parent) {
var argv = require('minimist')(process.argv.slice(2));
if (!argv._.length) {
console.log('Please pass the filename as the argument to the script.');
} else {
const buffer = new Buffer(fs.readFileSync(argv._[0]));
const result = module.exports.parse(buffer, argv);
console.log(util.inspect(result.parsed, false, null));
}
}
// Helper functions
function writeValue(marker, type, value) {
switch (type) {
case DATA_TYPES.INTEGER:
return writeInt(marker, value);
case DATA_TYPES.ARRAY_START:
return writeArrayLen(marker, value);
case DATA_TYPES.STRING:
return writeString(marker, value);
default:
throw new Error('I don\'t know how to write type ' + type);
}
}
function simplify(result) {
let mapFn = _.mapValues;
if (_.isArray(result)) {
mapFn = _.map;
}
return mapFn(result, i =>{
if (i.data && !_.isObject(i.data)) {
return i.data;
}
if (i.data === false) {
return false;
}
return simplify(i.data || i);
});
}
function readState(buffer, state) {
if (!state) {
state = {
pos: 0,
next4: buffer.slice(0, 4)
};
} else {
if (state.pos >= buffer.length - 4) {
return null;
}
state.next4 = buffer.slice(state.pos, state.pos + 4);
}
return state;
}
function parseEntry(buffer, state) {
const typeBuffer = buffer.slice(state.pos + 4, state.pos + 8);
const result = {
marker: state.next4,
type: typeBuffer.readUInt32LE()
};
state.pos += 8;
if (result.marker.readUInt32LE() < 256 || result.type === 0) {
result.data = 'SKIP';
} else if (result.marker.equals(WEIRD_OUTBACK_TYCOON_MARKER)) {
// Not sure what this is at the end of outback tycoon files, just quit processing at this point
result.data = 'UNKNOWN DATA AT END OF OUTBACK';
state.pos = buffer.length;
} else if (result.type === 0x18 || typeBuffer.slice(0, 2).equals(ZLIB_HEADER)) {
// compressed data, skip for now...
result.data = 'UNKNOWN COMPRESSED DATA';
state.pos = buffer.indexOf(COMPRESSED_DATA_END, state.pos) + 4;
} else {
switch (result.type) {
case DATA_TYPES.BOOLEAN:
result.data = readBoolean(buffer, state);
break;
case DATA_TYPES.INTEGER:
case DATA_TYPES.ARRAY_START: // 0A is an array, but i really only care about getting the length out, which looks like a normal integer
result.data = readInt(buffer, state);
break;
case 3:
result.data = 'UNKNOWN!';
state.pos += 12;
break;
case 0x15:
result.data = 'UNKNOWN!';
if (buffer.slice(state.pos, state.pos + 4).equals(new Buffer([0, 0, 0, 0x80]))) {
state.pos += 20;
} else {
state.pos += 12;
}
break;
case 4:
case DATA_TYPES.STRING:
result.data = readString(buffer, state);
break;
case DATA_TYPES.UTF_STRING:
result.data = readUtfString(buffer, state);
break;
case 0x14:
case 0x0D:
result.data = 'UNKNOWN!';
state.pos += 16;
break;
case 0x0B:
result.data = readArray(buffer, state);
break;
default:
throw new Error('Error parsing at position ' + state.pos + ': ' + JSON.stringify(result));
}
}
return result;
}
function readString(buffer, state) {
const origState = _.clone(state);
let result = null;
// Length can be up to 3 bytes, but the 4th byte is a marker?
const strLenBuf = Buffer.concat([buffer.slice(state.pos, state.pos + 3), new Buffer([0])]);
const strLen = strLenBuf.readUInt32LE(0);
state.pos += 2;
const strInfo = buffer.slice(state.pos, state.pos + 6);
//new Buffer([0, 0x21, 1, 0, 0, 0]))
if (strInfo[1] === 0 || strInfo[1] === 0x20) {
state.pos += 10;
result = 'Don\'t know what this kind of string is...';
} else if (strInfo[1] === 0x21) {
state.pos += 6;
result = buffer.slice(state.pos, state.pos + strLen - 1).toString(); // Ignore null terminator
state.pos += strLen;
}
if (result === null) {
throw new Error('Error reading string: ' + JSON.stringify(origState));
}
return result;
}
function readArray(buffer, state) {
const origState = _.clone(state);
let result = [];
state.pos += 8;
const arrayLen = buffer.readUInt32LE(state.pos);
state.pos += 4;
for (let i = 0; i < arrayLen; i++) {
if (buffer[state.pos] != 0x0A) {
throw new Error('Error reading array: ' + JSON.stringify(origState));
}
state.pos += 16;
const curData = {};
result.push(curData);
let info;
do {
state = readState(buffer, state);
info = parseEntry(buffer, state);
for (let key in GAME_DATA) {
if (info.marker.equals(GAME_DATA[key])) {
curData[key] = info;
}
}
} while (info.data != "1");
}
return result;
}
function writeString(marker, newValue) {
const safeValue = iconv.encode(diacritics.remove(newValue), 'ascii');
const strLenBuffer = new Buffer([0, 0, 0, 0x21, 1, 0, 0, 0]);
strLenBuffer.writeUInt16LE(safeValue.length + 1, 0);
return Buffer.concat([marker, new Buffer([5, 0, 0, 0]), strLenBuffer, myBufferFrom(safeValue), new Buffer([0])]);
}
function readUtfString(buffer, state) {
const origState = _.clone(state);
let result = null;
const strLen = buffer.readUInt16LE(state.pos) * 2;
state.pos += 2;
if (buffer.slice(state.pos, state.pos + 6).equals(new Buffer([0, 0x21, 2, 0, 0, 0]))) {
state.pos += 6;
result = buffer.slice(state.pos, state.pos + strLen - 2).toString('ucs2'); // Ignore null terminator
state.pos += strLen;
}
if (result === null) {
throw new Error('Error reading string: ' + JSON.stringify(origState));
}
return result;
}
function readBoolean(buffer, state) {
state.pos += 8;
const result = !!buffer[state.pos];
state.pos += 4;
return result;
}
function readInt(buffer, state) {
state.pos += 8;
const result = buffer.readUInt32LE(state.pos);
state.pos += 4;
return result;
}
function writeInt(marker, value) {
const valueBuffer = Buffer.alloc(4);
valueBuffer.writeUInt32LE(value);
return Buffer.concat([marker, new Buffer([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), valueBuffer]);
}
function writeArrayLen(marker, value) {
const valueBuffer = Buffer.alloc(4);
valueBuffer.writeUInt32LE(value);
return Buffer.concat([marker, new Buffer([0x0A, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0]), valueBuffer]);
}
function readCompressedData(buffer, state, filename) {
const compressedData = buffer.slice(state.pos + 4, buffer.indexOf(COMPRESSED_DATA_END, state.pos));
const uncompressedData = zlib.unzipSync(compressedData);
fs.writeFileSync(filename, uncompressedData);
}
function myBufferFrom(source) {
if (useNewBuffer) {
return new Buffer(source);
}
return Buffer.from(source);
}