-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
94 lines (84 loc) · 2.64 KB
/
app.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
'use strict';
const processTime = process.hrtime();
const path = require('path');
const config = require('./config');
const logger = config.logger.child({file:__filename});
const XML = require('./lib/parser');
const Local = require('./lib/local');
const Save = require('./lib/save');
const Actions = require('./lib/actions');
const Utils = require('./lib/utils');
const Game = config.Game;
const savePath = path.join(Game.saveDir,Game.saveName);
const writePath = path.join(Game.saveDir,Game.modifiedName);
/**
* Convert the edited save game back to XML and save to file
* @param {Object} editedSave
* @param {Array} modified
*/
function save(editedSave,modified){
logger.info(`Saving local file "${writePath}"`);
Local.save(writePath,XML.compile(editedSave),(err) =>{
if(err){
logger.error('Failed to write save file',err);
throw new Error(err);
} else {
const elapsed = process.hrtime(processTime);
logger.info(`Save written as "${Game.modifiedName}", ready to play!`);
logger.info(`Process took ${(elapsed[0] * 1000 + elapsed[1] / 1000000)}ms`);
modified.forEach((item) =>{
logger.info(`${item.name} modified:`,item.modified);
});
}
});
}
/**
* Process the converted XML string
* @param {Error} err
* @param {Object} result
*/
function processSave(err,result){
if(err){
logger.error(err);
throw new Error(err);
} else {
logger.info('Finished loading save data');
const gameMeta = result.savegame.meta;
const gameSave = result.savegame.game;
if(Save.isSupported(gameMeta)){
config.Game.updateColonyFaction(Utils.findPlayerColony(gameSave));
const result = Actions.doQuickActions(gameSave);
const editedSave = {
savegame:{
meta: [
gameMeta
],
game: [
result.save
]
}
};
logger.info('Finished modifying, writing save file');
save(editedSave,result.modified);
} else {
throw new Error('Unsupported game save version');
}
}
}
/**
* Process the XML loaded
* @param {Error} err
* @param {String} file
*/
function processXml(err,file){
if(err){
logger.error(err);
throw new Error(err);
} else {
logger.info('Loading save data');
XML.parse(file,processSave);
}
}
// Start process by loading in the desired file
logger.info(`Accessing file "${savePath}"`);
Local.get(savePath,processXml);