-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
170 lines (162 loc) · 5 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
/*jshint esversion: 6 */
const log = require('./pino.js');
const bindings = require('bindings')('storm-replay');
const fs = require('fs');
const version = require('./package.json').version;
/*
* Buffer Helper Functions
*/
function checkOffset (offset, ext, length) {
if ((offset % 1) !== 0 || offset < 0) throw new RangeError('offset is not uint');
if (offset + ext > length) throw new RangeError('Trying to access beyond buffer length');
}
Buffer.prototype.readUInt32LE = function readUInt32LE (offset, noAssert) {
offset = offset >>> 0;
if (!noAssert) checkOffset(offset, 4, this.length);
return ((this[offset]) |
(this[offset + 1] << 8) |
(this[offset + 2] << 16)) +
(this[offset + 3] * 0x1000000);
};
const FILES = [
'header',
'replay.attributes.events',
'replay.details',
'replay.game.events',
'replay.initdata',
'replay.load.info',
'replay.message.events',
'replay.server.battlelobby',
'replay.smartcam.events',
'replay.sync.events',
'replay.sync.history',
'replay.tracker.events'
];
/*
* Exports
*/
module.exports = {
version: version,
extractFile: function(Archive, File) {
log.trace("extractFile('" + Archive + "', '" + File + "')");
if (!fs.existsSync(Archive)) {
return {
success: false,
details: {
reason: Archive + ' not found.',
args: [
{
name: 'Archive',
value: Archive
},
{
name: 'File',
value: File
}
],
}
};
}
if (FILES.indexOf(File) > -1) {
var ret = bindings.extractFile(Archive, File);
if (ret.length === undefined) {
return {
success: false,
details: {
reason: 'Extraction of ' + File + ' failed.',
args: [
{
name: 'Archive',
value: Archive
},
{
name: 'File',
value: File
}
],
}
};
}
return {
success: true,
content: {
data: ret,
size: ret.length
}
};
} else {
return {
success: false,
details: {
reason: 'Extraction of ' + File + ' not permitted.',
args: [
{
name: 'Archive',
value: Archive
},
{
name: 'File',
value: File
}
],
}
};
}
},
getHeader: function(Archive) {
log.trace("getHeader('" + Archive + "')");
if (fs.existsSync(Archive)) {
var ret = bindings.getHeader(Archive);
if (ret.length === undefined) {
return {
success: false,
details: {
reason: Archive + ' is not a valid MPQ archive.',
args: [
{
name: 'Archive',
value: Archive
}
],
}
};
}
return {
success: true,
header: {
data: ret,
size: ret.length
},
content: {
data: ret.slice(16, 16 + ret.readUInt32LE(12)),
size: ret.readUInt32LE(12)
}
};
} else {
return {
success: false,
details: {
reason: Archive + ' not found.',
args: [
{
name: 'Archive',
value: Archive
}
],
}
};
}
},
/**
* Remove replay.message.events from the archive. As some chat messages
* may be toxic or taken out of context, this sanitation method provides
* the ability to ensure a player's actions speak louder than her words.
*
* @summary Remove replay.message.events from the MPQ archive.
*/
removeMessages: function(Archive) {
log.trace("removeMessages('" + Archive + "')");
var ret = bindings.removeMessages(Archive);
return ret;
}
};