-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
303 lines (262 loc) · 9.19 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
/*jshint esversion: 6 */
/*jslint node: true */
"use strict";
const log = require('./pino.js');
const fs = require('fs');
const path = require('path');
const MPQArchive = exports.MPQArchive = require('empeeku/mpyq').MPQArchive;
const protocol29406 = exports.protocol = require('./lib/protocol29406');
const version = exports.version = require('./package.json').version;
try {
var optional = require('storm-replay');
} catch (err) {
optional = null;
log.warn('heroprotocol.js is using Javascript extraction, which is notably slower and will be re-written in the future. See README.md for more details.');
}
const storm = optional;
// parsable parts
const HEADER = exports.HEADER = 'header';
const DETAILS = exports.DETAILS = 'replay.details';
const INITDATA = exports.INITDATA = 'replay.initdata';
const GAME_EVENTS = exports.GAME_EVENTS = 'replay.game.events';
const MESSAGE_EVENTS = exports.MESSAGE_EVENTS = 'replay.message.events';
const TRACKER_EVENTS = exports.TRACKER_EVENTS = 'replay.tracker.events';
const ATTRIBUTES_EVENTS = exports.ATTRIBUTES_EVENTS = 'replay.attributes.events';
const FILES = exports.FILES = [
HEADER,
DETAILS,
INITDATA,
GAME_EVENTS,
MESSAGE_EVENTS,
TRACKER_EVENTS,
ATTRIBUTES_EVENTS
];
const decoderMap = {
[HEADER]: 'decodeReplayHeader',
[DETAILS]: 'decodeReplayDetails',
[INITDATA]: 'decodeReplayInitdata',
[GAME_EVENTS]: 'decodeReplayGameEvents',
[MESSAGE_EVENTS]: 'decodeReplayMessageEvents',
[TRACKER_EVENTS]: 'decodeReplayTrackerEvents',
[ATTRIBUTES_EVENTS]: 'decodeReplayAttributesEvents'
};
const parseStrings = function parseStrings(data) {
if (!data) return data;
else if (data instanceof Buffer) return data.toString();
else if (Array.isArray(data)) return data.map(item => parseStrings(item));
else if (typeof data === 'object') {
for (let key in data) {
data[key] = parseStrings(data[key]);
}
}
return data;
};
let lastUsed, protocol;
let build = 0;
const openArchive = function (file, noCache) {
log.trace('openArchive() : ' + file + ', ' + noCache);
let archive, header;
if (!lastUsed || !(lastUsed instanceof MPQArchive) || file !== lastUsed.filename || noCache) {
if (typeof file === 'string') {
try {
if (!path.isAbsolute(file)) {
file = path.join(process.cwd(), file);
}
archive = new MPQArchive(file);
archive.filename = file;
} catch (err) {
archive = err;
}
} else if (file instanceof MPQArchive) {
// TODO - need to check what happens when instanciating an MPQArchive with
// invalid path and setup an error accordingly
archive = file;
} else {
archive = new Error('Unsupported parameter: ${file}');
}
if (archive instanceof Error) return archive;
lastUsed = archive;
// parse header
archive.data = {};
header = archive.data[HEADER] = parseStrings(protocol29406.decodeReplayHeader(archive.header.userDataHeader.content));
// The header's baseBuild determines which protocol to use
archive.baseBuild = build = header.m_version.m_baseBuild;
try {
archive.protocol = require(`./lib/protocol${archive.baseBuild}`);
} catch (err) {
archive.error = err;
}
// set header to proper protocol
archive.data[HEADER] = parseStrings(archive.protocol.decodeReplayHeader(archive.header.userDataHeader.content));
archive.get = function (file) {
return exports.get(file, archive);
};
} else {
// load archive from cache
archive = lastUsed;
}
return archive;
};
// ensure non-breaking changes
exports.get = (file, archive) => {
log.debug('get() : ' + file + ', ' + archive);
if (storm) {
return exports.extractFile(file, archive);
} else {
return exports.extractFileJS(file, archive);
}
};
// returns the content of a file in a replay archive
exports.extractFileJS = function (archiveFile, archive, keys) {
log.debug('extractFileJS() : ' + archiveFile + ', ' + archive);
let data;
archive = openArchive(archive);
if (archive instanceof Error) {
return data;
}
if (archive.data[archiveFile] && !keys) {
data = archive.data[archiveFile];
} else {
if (archive.protocol) {
if ([DETAILS, INITDATA, ATTRIBUTES_EVENTS].indexOf(archiveFile) > -1) {
log.trace('extractFileJS() : ' + archiveFile + ' - parsing file');
data = archive.data[archiveFile] =
parseStrings(archive.protocol[decoderMap[archiveFile]](
archive.readFile(archiveFile)
));
} else if ([GAME_EVENTS, MESSAGE_EVENTS, TRACKER_EVENTS].indexOf(archiveFile) > -1) {
log.trace('extractFileJS() : ' + archiveFile + ' - parsing lines iteratively');
if (keys) {
// protocol function to call is a generator
data = [];
for (let event of archive.protocol[decoderMap[archiveFile]](archive.readFile(archiveFile))) {
keyLoop:
// check validity with whitelisted keys
for (var key in keys) {
for (var i = 0, j = keys[key].length; i < j; i++) {
if (parseStrings(event)[key] === keys[key][i]){
data.push(parseStrings(event));
break keyLoop;
}
}
}
}
} else {
data = archive.data[archiveFile] = [];
for (let event of archive.protocol[decoderMap[archiveFile]](archive.readFile(archiveFile))) {
data.push(parseStrings(event));
}
}
} else {
log.trace('extractFileJS() : ' + archiveFile + ' - not parsing');
data = archive.data[archiveFile] = archive.readFile(archiveFile);
}
}
}
return data;
};
/**
* extract all files from archive via cpp binding
* @function
* @param {string} archive - Path of the MPQ archive
* @returns {object} Object of files as buffers
*/
exports.extractFiles = (archive) => {
if (typeof archive === 'string') {
if (!path.isAbsolute(archive)) {
archive = path.join(process.cwd(), archive);
}
}
log.debug('extractFiles() : ' + archive);
let header = exports.parseHeader(storm.getHeader(archive).content.data);
let data = [];
for (var i = FILES.length - 1; i >= 0; i--) {
data[FILES[i]] = exports.extractFile(FILES[i], archive);
}
return data;
};
/**
* extract all files from archive via cpp binding
* @function
* @param {string} file - Filename to extract
* @param {string} archive - Path of the MPQ archive
* @returns {object} Object of files as buffers
*/
exports.extractFile = (file, archive) => {
if (typeof archive === 'string') {
if (!path.isAbsolute(archive)) {
archive = path.join(process.cwd(), archive);
}
}
let build = exports.getVersion(archive);
log.debug('extractFile() : ' + file + ', ' + archive);
if (file === 'header') {
return exports.parseHeader(storm.getHeader(archive).content.data);
}
let result = storm.extractFile(archive, file);
if (result.success == false) {
log.warn(JSON.stringify(result));
}
return exports.parseFile(file, result.content.data, build);
};
/**
* gets the build version of the replay, and preloads the decoding library
* @function
* @param {string} archive - Path of the MPQ archive
* @returns {integer} Build number
*/
exports.getVersion = (archive) => {
if (typeof archive === 'string') {
if (!path.isAbsolute(archive)) {
archive = path.join(process.cwd(), archive);
}
}
let header = exports.parseHeader(storm.getHeader(archive).content.data);
protocol = require(`./lib/protocol${header.m_dataBuildNum}`);
build = header.m_dataBuildNum;
return header.m_dataBuildNum;
};
/**
* parses a basic MPQ header
* @function
* @param {buffer} buffer - Header content from MPQ archive
* @returns {object} Header information from file
*/
exports.parseHeader = function (buffer) {
return parseStrings(protocol29406.decodeReplayHeader(buffer));
};
/**
* parses a buffer based on a given build
* @function
* @param {string} filename - Name of the file to assist in parsing
* @param {buffer} buffer - Binary file contents from MPQ archive
* @param {string} build - Build in which to parse the contents
* @returns {object} File contents
*/
exports.parseFile = function (filename, buffer, build) {
let data, protocol;
try {
protocol = require(`./lib/protocol${build}`);
} catch (err) {
return undefined;
}
if ([DETAILS, INITDATA, ATTRIBUTES_EVENTS].indexOf(filename) > -1) {
log.trace('parseFile() : ' + filename + " (build " + build + ") - parsing entire file");
data = parseStrings(protocol[decoderMap[filename]](buffer));
} else if ([GAME_EVENTS, MESSAGE_EVENTS, TRACKER_EVENTS].indexOf(filename) > -1) {
log.trace('parseFile() : ' + filename + " (build " + build + ") - parsing lines iteratively");
data = [];
for (let event of protocol[decoderMap[filename]](buffer)) {
data.push(parseStrings(event));
}
} else {
log.trace('parseFile() : ' + filename + " (build " + build + ") - not parsing");
data = buffer;
}
return data;
};
if (storm !== null) {
exports.stormVersion = storm.version;
} else {
exports.stormVersion = undefined;
}