-
Notifications
You must be signed in to change notification settings - Fork 2
/
aof-file-parser.js
278 lines (229 loc) · 9.69 KB
/
aof-file-parser.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
'use strict';
let fs = require('fs');
let save = (game, filename, cb) => {
// Validate that all the necessary parameters are present
let error = '';
if (!game.regionId) error = 'regionId missing';
if (!game.gameId) error = 'gameId missing';
if (!game.riotVersion) error = 'riotVersion missing';
if (!game.key) error = 'encryption key missing';
if (!game.endStartupChunkId) error = 'endStartupChunkId missing';
if (!game.startGameChunkId) error = 'startGameChunkId missing';
if (error) {
cb({ success: false, error: new Error(error), warnings: [] });
return;
}
let warnings = [];
// Check players array, should contain at least 1 player
if (game.players && game.players.length === 0) warnings.push('No players');
if (!game.players) game.players = [];
// Check keyframes
if (!game.keyframes || !game.keyframes.length) {
cb({ success: false, error: new Error('No keyframes'), warnings: [] });
return;
}
// Check chunks
if (!game.chunks || !game.chunks.length) {
cb({ success: false, error: new Error('No chunks'), warnings: [] });
return;
}
// Start counting keyframes/chunks
let dataLength = 0; // Used to calculate the length of the buffer
let totalKeyframes = 0; // Count total keyframes
let totalChunks = 0; // Count total chunks
game.keyframes.forEach((data) => {
dataLength += data.data.length;
totalKeyframes++;
});
game.chunks.forEach((data) => {
dataLength += data.data.length;
totalChunks++;
});
// Compare count with array length to check if we have all keyframes / chunks
let complete = (totalKeyframes === game.keyframes.length - 1) && (totalChunks === game.chunks.length - 1) ? 1 : 0;
// Create a replay file
let c = 0;
let keyLen = Buffer.byteLength(game.key, 'base64');
let buff = new Buffer(18 + keyLen);
// Splits gameId into low and high 32bit numbers
let high = Math.floor(game.gameId / 4294967296); // right shift by 32 bits (js doesn't support '>> 32')
let low = game.gameId - high * 4294967296; // extract lower 32 bits
// File version
buff.writeUInt8(12, c); c += 1;
// Extract bytes for riot version
let splits = game.riotVersion.split('.');
// Basic game info
buff.writeUInt8(game.regionId, c); c += 1;
buff.writeUInt32BE(high, c); c += 4;
buff.writeUInt32BE(low, c); c += 4;
buff.writeUInt8(splits[0], c); c += 1;
buff.writeUInt8(splits[1], c); c += 1;
buff.writeUInt8(splits[2], c); c += 1;
buff.writeUInt8(keyLen, c); c += 1;
buff.write(game.key, c, keyLen, 'base64'); c += keyLen;
buff.writeUInt8(complete ? 1 : 0, c); c += 1;
buff.writeUInt8(game.endStartupChunkId, c); c += 1;
buff.writeUInt8(game.startGameChunkId, c); c += 1;
// Players
buff.writeUInt8(game.players.length, c); c += 1;
for (let i = 0; i < game.players.length; i++) {
let p = game.players[i];
let len = Buffer.byteLength(p.name, 'utf8');
let tempBuff = new Buffer(20 + len);
let d = 0;
tempBuff.writeInt32BE(p.id, d); d += 4;
tempBuff.writeUInt8(len, d); d += 1;
tempBuff.write(p.name, d, len, 'utf8'); d += len;
tempBuff.writeUInt8(p.teamNr, d); d += 1;
tempBuff.writeUInt8(p.leagueId, d); d += 1;
tempBuff.writeUInt8(p.leagueRank, d); d += 1;
tempBuff.writeInt32BE(p.championId, d); d += 4;
tempBuff.writeInt32BE(p.spell1Id, d); d += 4;
tempBuff.writeInt32BE(p.spell2Id, d); d += 4;
buff = Buffer.concat([ buff, tempBuff ]); c += tempBuff.length;
}
// Extend buffer
buff = Buffer.concat([ buff, new Buffer(4 + dataLength + (totalKeyframes + totalChunks) * 6) ]);
// Keyframes
buff.writeUInt16BE(totalKeyframes, c); c += 2;
game.keyframes.forEach(function(keyframe, index) {
if (!keyframe) return;
buff.writeUInt16BE(keyframe.id, c); c += 2;
buff.writeInt32BE(keyframe.data.length, c); c += 4;
keyframe.data.copy(buff, c);
c += keyframe.data.length;
});
// Chunks
buff.writeUInt16BE(totalChunks, c); c += 2;
game.chunks.forEach(function(chunk, index) {
if (!chunk) return;
buff.writeUInt16BE(chunk.id, c); c += 2;
buff.writeInt32BE(chunk.data.length, c); c += 4; // length of chunk
chunk.data.copy(buff, c);
c += chunk.data.length;
});
let file = filename + '.aof';
fs.writeFile(file, buff, (err) => {
if(err) {
cb({ success: false, error: err, warnings: warnings });
return;
}
cb({ success: true, error: null, warnings: warnings });
});
};
let load = (file, cb) => {
let replayMetadata = {};
let replayData = {};
let buff;
try {
buff = fs.readFileSync(file);
} catch (e) {
cb({ success: false, error: e }); // Will abort if there is any error reading the file.
return;
}
let c = 0;
// Read file version
replayMetadata.fileVersion = buff.readUInt8(c); c += 1;
if (replayMetadata.fileVersion < 8) {
cb({ success: false, error: new Error('The file is using an old data format') });
return;
} else if (replayMetadata.fileVersion == 9) {
cb({ success: false, error: new Error('This file is using a corrupted data format. Please report this to an administrator of aof.gg') });
return;
}
// Read the region id
replayMetadata.regionId = buff.readUInt8(c); c += 1;
// Read the game id
if (replayMetadata.fileVersion == 8) {
replayMetadata.gameId = buff.readUInt32BE(c); c += 4;
} else {
let high = buff.readUInt32BE(c); c += 4;
let low = buff.readUInt32BE(c); c += 4;
replayMetadata.gameId = high * 4294967296 + low;
}
// Read the riot version
replayMetadata.riotVersion = buff.readUInt8(c); c += 1;
replayMetadata.riotVersion += '.' + buff.readUInt8(c); c += 1;
replayMetadata.riotVersion += '.' + buff.readUInt8(c); c += 1;
// Read additional data for the replay
let len = buff.readUInt8(c); c += 1;
replayMetadata.key = buff.toString('base64', c, c + len); c += len;
replayMetadata.complete = buff.readUInt8(c); c += 1;
replayMetadata.endStartupChunkId = buff.readUInt8(c); c += 1;
replayMetadata.startGameChunkId = buff.readUInt8(c); c += 1;
// Read the player data
replayMetadata.players = [];
let num = buff.readUInt8(c); c += 1;
for (let i = 0; i < num; i++) {
let p = {};
p.id = buff.readInt32BE(c); c += 4;
len = buff.readUInt8(c); c += 1;
p.name = buff.toString('utf8', c, c + len); c += len;
p.teamNr = buff.readUInt8(c); c += 1;
p.leagueId = buff.readUInt8(c); c += 1;
p.leagueRank = buff.readUInt8(c); c += 1;
p.championId = buff.readInt32BE(c); c += 4;
p.spell1Id = buff.readInt32BE(c); c += 4;
p.spell2Id = buff.readInt32BE(c); c += 4;
replayMetadata.players.push(p);
}
// Read the keyframes
replayData.keyframes = [];
if (replayMetadata.fileVersion < 11) {
num = buff.readUInt8(c); c += 1;
} else {
num = buff.readUInt16BE(c); c += 2;
}
for (let i = 0; i < num; i++) {
let keyframe = {};
if (replayMetadata.fileVersion < 11) {
keyframe.id = buff.readUInt8(c); c += 1;
} else if (replayMetadata.fileVersion == 11) {
keyframe.id = i + 1; c += 1;
} else {
keyframe.id = buff.readUInt16BE(c); c += 2;
}
len = buff.readInt32BE(c); c += 4;
keyframe.data = new Buffer(len);
buff.copy(keyframe.data, 0, c, c + len); c += len;
replayData.keyframes[keyframe.id] = keyframe;
}
// We need at least one keyframe
if (replayData.keyframes.length === 0) {
cb({ success: false, error: new Error('No keyframes') });
return;
}
// Read the chunks
replayData.chunks = [];
if (replayMetadata.fileVersion < 11) {
num = buff.readUInt8(c); c += 1;
} else {
num = buff.readUInt16BE(c); c += 2;
}
for (let i = 0; i < num; i++) {
let chunk = {};
if (replayMetadata.fileVersion < 11) {
chunk.id = buff.readUInt8(c); c += 1;
} else if (replayMetadata.fileVersion == 11) {
chunk.id = i + 1; c += 1;
} else {
chunk.id = buff.readUInt16BE(c); c += 2;
}
len = buff.readInt32BE(c); c += 4;
chunk.data = new Buffer(len);
buff.copy(chunk.data, 0, c, c + len); c += len;
replayData.chunks[chunk.id] = chunk;
}
// Calculate the last chunk id
if (replayData.chunks.length > 0) {
replayMetadata.endGameChunkId = replayData.chunks[replayData.chunks.length - 1].id
} else {
cb({ success: false, error: new Error('No chunks') });
return;
}
cb({ success: true, error: null }, replayMetadata, replayData);
};
module.exports = {
save: save,
load: load
};