fork/slippc

git clone https://git.julia.blue/fork/slippc

Patrick CrainAdded documentation to replay.h; updated readme and changelog; minor tweaks to output of parser and analyzer, including original input filename; bumped parser and analyzer versions to 0.7.07d49c2f

0.7.1
27.0 KiB701 linesraw
1#include "parser.h"
2
3//Debug output convenience macros
4#define DOUT1(s) if (_debug >= 1) { std::cout << s; }
5#define DOUT2(s) if (_debug >= 2) { std::cout << s; }
6#define FAIL(e) std::cerr << "ERROR: " << e << std::endl
7#define FAIL_CORRUPT(e) std::cerr << "ERROR: " << e << "; replay may be corrupt" << std::endl
8
9namespace slip {
10
11  Parser::Parser(int debug_level) {
12    _debug = debug_level;
13    _bp    = 0;
14  }
15
16  Parser::~Parser() {
17    if (_rb != nullptr) {
18      delete [] _rb;
19    }
20    _cleanup();
21  }
22
23  bool Parser::load(const char* replayfilename) {
24    DOUT1("Loading " << replayfilename << std::endl);
25    _replay.original_file = std::string(replayfilename);
26    std::ifstream myfile;
27    myfile.open(replayfilename,std::ios::binary | std::ios::in);
28    if (myfile.fail()) {
29      FAIL("File " << replayfilename << " could not be opened or does not exist");
30      return false;
31    }
32
33    myfile.seekg(0, myfile.end);
34    _file_size = myfile.tellg();
35    if (_file_size < MIN_REPLAY_LENGTH) {
36      FAIL("File " << replayfilename << " is too short to be a valid Slippi replay");
37      return false;
38    }
39    DOUT1("  File Size: " << +_file_size << std::endl);
40    myfile.seekg(0, myfile.beg);
41
42    _rb = new char[_file_size];
43    myfile.read(_rb,_file_size);
44    myfile.close();
45
46    // Check if we have a compressed .zlp file
47    bool is_compressed = same4(&_rb[0],LZMA_HEADER);
48    if (is_compressed) {
49      DOUT1("  Decompressing .zlp" << std::endl);
50      // Copy the buffer to a string
51      std::string rs(_rb, _file_size);
52      // Decompress the string
53      std::string decomp = decompressWithLzma(rs);
54      // Get the new file size
55      _file_size    = decomp.size();
56      // Delete the old read buffer
57      delete [] _rb;
58      // Reallocate it with more spce
59      _rb = new char[_file_size];
60      // Copy buffer from the decompressed string
61      memcpy(_rb,decomp.c_str(),_file_size);
62      // Create a Compressor object
63      Compressor *d  = new slip::Compressor(0);
64      // Decompress the buffer
65      d->loadFromBuff(&_rb,_file_size);
66      // Save it back to the original buffer
67      d->saveToBuff(&_rb);
68      DOUT1("  Decompressed File Size: " << +_file_size << std::endl);
69    }
70
71    return this->_parse();
72  }
73
74  bool Parser::_parse() {
75    _bp = 0; //Start reading from byte 0
76    if (not this->_parseHeader()) {
77      std::cerr << "Failed to parse header" << std::endl;
78      return false;
79    }
80    if (not this->_parseEventDescriptions()) {
81      std::cerr << "Failed to parse event descriptions" << std::endl;
82      return false;
83    }
84    if (not this->_parseEvents()) {
85      std::cerr << "Failed to parse events proper" << std::endl;
86      return false;
87    }
88    if (not this->_parseMetadata()) {
89      std::cerr << "Warning: failed to parse metadata" << std::endl;
90      //Non-fatal if we can't parse metadata, so don't need to return false
91    }
92    DOUT1("Successfully parsed replay!" << std::endl);
93    return true;
94  }
95
96  bool Parser::_parseHeader() {
97    DOUT1("Parsing header" << std::endl);
98
99    //First 15 bytes contain header information
100    if (same8(&_rb[_bp],SLP_HEADER)) {
101      DOUT1("  Slippi Header Matched" << std::endl);
102    } else {
103      FAIL_CORRUPT("Header did not match expected Slippi file header");
104      return false;
105    }
106    _length_raw_start = readBE4U(&_rb[_bp+11]);
107    if(_length_raw_start == 0) {  //TODO: this is /technically/ recoverable
108      FAIL_CORRUPT("0-byte raw data detected");
109      return false;
110    }
111    DOUT1("  Raw portion = " << _length_raw_start << " bytes" << std::endl);
112    if (_length_raw_start > _file_size) {
113      FAIL_CORRUPT("Raw data size exceeds file size of " << _file_size << " bytes");
114      return false;
115    }
116    _length_raw = _length_raw_start;
117    _bp += 15;
118    return true;
119  }
120
121  bool Parser::_parseEventDescriptions() {
122    DOUT1("Parsing event descriptions" << std::endl);
123
124    //Next 2 bytes should be 0x35
125    if (_rb[_bp] != Event::EV_PAYLOADS) {
126      FAIL_CORRUPT("Expected Event 0x" << std::hex
127        << Event::EV_PAYLOADS << std::dec << " (Event Payloads)");
128      return false;
129    }
130    uint8_t ev_bytes = _rb[_bp+1]-1; //Subtract 1 because the last byte we read counted as part of the payload
131    _payload_sizes[Event::EV_PAYLOADS] = int32_t(ev_bytes+1);
132    DOUT1("  Event description length = " << int32_t(ev_bytes+1) << " bytes" << std::endl);
133    _bp += 2;
134
135    //Next ev_bytes bytes describe events
136    for(unsigned i = 0; i < ev_bytes; i+=3) {
137      unsigned ev_code = uint8_t(_rb[_bp+i]);
138      if (_payload_sizes[ev_code] > 0) {
139        if (ev_code >= Event::EV_PAYLOADS && ev_code <= Event::GAME_END) {
140          FAIL_CORRUPT("Event " << Event::name[ev_code-Event::EV_PAYLOADS] << " payload size set multiple times");
141        } else {
142          FAIL_CORRUPT("Event " << hex(ev_code) << std::dec << " payload size set multiple times");
143        }
144        return false;
145      }
146      _payload_sizes[ev_code] = readBE2U(&_rb[_bp+i+1])+1;  //Add one for the event code itself
147      DOUT1("  Payload size for event "
148        << hex(ev_code) << std::dec << ": " << _payload_sizes[ev_code]
149        << " bytes" << std::endl);
150    }
151
152    //Sanity checks to verify we at least have Payload Sizes, Game Start, Pre Frame, Post Frame, and Game End Events
153    for(unsigned i = Event::EV_PAYLOADS; i <= Event::GAME_END; ++i) {
154      if (_payload_sizes[i] == 0) {
155        FAIL_CORRUPT("Event " << Event::name[i-Event::EV_PAYLOADS] << " payload size not set");
156        return false;
157      }
158    }
159
160    //Update the remaining length of the raw data to sift through
161    _bp += ev_bytes;
162    _length_raw -= (2+ev_bytes);
163    return true;
164  }
165
166  bool Parser::_parseEvents() {
167    DOUT1("Parsing events proper" << std::endl);
168
169    bool success = true;
170    for( ; _length_raw > 0; ) {
171      unsigned ev_code = uint8_t(_rb[_bp]);
172      unsigned shift   = _payload_sizes[ev_code];
173      if (shift > _length_raw) {
174        FAIL_CORRUPT("Event byte offset exceeds raw data length");
175        return false;
176      }
177      switch(ev_code) { //Determine the event code
178        case Event::GAME_START:  success = _parseGameStart();  break;
179        case Event::PRE_FRAME:   success = _parsePreFrame();   break;
180        case Event::POST_FRAME:  success = _parsePostFrame();  break;
181        case Event::GAME_END:    success = _parseGameEnd();    break;
182        case Event::ITEM_UPDATE: success = _parseItemUpdate(); break;
183
184        case Event::SPLIT_MSG:   success = true;               break;
185        case Event::FRAME_START: success = true;               break;
186        case Event::BOOKEND:     success = true;               break;
187
188        default:
189          DOUT1("  Warning: unknown event code " << hex(ev_code) << " encountered; skipping" << std::endl);
190          break;
191      }
192      if (not success) {
193        return false;
194      }
195      if (_payload_sizes[ev_code] == 0) {
196        FAIL_CORRUPT("Uninitialized event " << hex(ev_code) << " encountered");
197        return false;
198      }
199      _length_raw    -= shift;
200      _bp            += shift;
201      DOUT2("  Raw bytes remaining: " << +_length_raw << std::endl);
202    }
203
204    return true;
205  }
206
207  bool Parser::_parseGameStart() {
208    DOUT1("  Parsing game start event at byte " << +_bp << std::endl);
209
210    if (_slippi_maj > 0) {
211      FAIL_CORRUPT("Duplicate game start event");
212      return false;
213    }
214
215    //Get Slippi version
216    _slippi_maj = uint8_t(_rb[_bp+O_SLP_MAJ]); //Major version
217    _slippi_min = uint8_t(_rb[_bp+O_SLP_MIN]); //Minor version
218    _slippi_rev = uint8_t(_rb[_bp+O_SLP_REV]); //Build version (4th char unused)
219    _replay.slippi_version_raw = readBE4U(&_rb[_bp+O_SLP_MAJ]);
220
221    if (_slippi_maj == 0) {
222      FAIL("Replays from Slippi 0.x.x are not supported");
223      return false;
224    }
225
226    std::stringstream ss;
227    ss << +_slippi_maj << "." << +_slippi_min << "." << +_slippi_rev;
228    _slippi_version = ss.str();
229    DOUT1("    Slippi Version: " << _slippi_version << std::endl);
230
231    //Get player info
232    for(unsigned p = 0; p < 4; ++p) {
233      unsigned i                     = O_PLAYERDATA + 0x24*p; //Beginning of player info block
234      unsigned m                     = O_DASHBACK   + 0x8*p;
235      unsigned k                     = O_NAMETAG    + 0x10*p;
236      std::string ps                 = std::to_string(p+1);
237
238      _replay.player[p].ext_char_id  = uint8_t(_rb[_bp+i+O_PLAYER_ID]);
239      if (_replay.player[p].ext_char_id >= CharExt::__LAST) {
240        FAIL_CORRUPT("External character ID " << +_replay.player[p].ext_char_id << " is invalid");
241        return false;
242      }
243      _replay.player[p].player_type  = uint8_t(_rb[_bp+i+O_PLAYER_TYPE]);
244      _replay.player[p].start_stocks = uint8_t(_rb[_bp+i+O_START_STOCKS]);
245      _replay.player[p].color        = uint8_t(_rb[_bp+i+O_COLOR]);
246      _replay.player[p].shade        = uint8_t(_rb[_bp+i+O_SHADE]);
247      _replay.player[p].handicap     = uint8_t(_rb[_bp+i+O_HANDICAP]);
248      _replay.player[p].team_id      = uint8_t(_rb[_bp+i+O_TEAM_ID]);
249      _replay.player[p].cpu_level    = uint8_t(_rb[_bp+i+O_CPU_LEVEL]);
250      _replay.player[p].offense      = readBE4F(&_rb[_bp+i+O_OFFENSE]);
251      _replay.player[p].defense      = readBE4F(&_rb[_bp+i+O_DEFENSE]);
252      _replay.player[p].scale        = readBE4F(&_rb[_bp+i+O_SCALE]);
253      _replay.player[p].dash_back    = readBE4U(&_rb[_bp+m]);
254      _replay.player[p].shield_drop  = readBE4U(&_rb[_bp+m+0x4]);
255      _replay.player[p].stamina      = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x01;
256      _replay.player[p].silent       = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x02;
257      _replay.player[p].low_gravity  = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x04;
258      _replay.player[p].invisible    = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x08;
259      _replay.player[p].black_stock  = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x10;
260      _replay.player[p].metal        = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x20;
261      _replay.player[p].warp_in      = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x40;
262      _replay.player[p].rumble       = uint8_t(_rb[_bp+i+O_PLAYER_BITS]) & 0x80;
263
264      //Decode Melee's internal tag as a proper UTF-8 string
265      if(MIN_VERSION(1,3,0)) {
266        _replay.player[p].tag_css = decode_shift_jis(_rb+_bp+k);
267      }
268    }
269
270    //Write to replay data structure
271    _replay.parser_version = PARSER_VERSION;
272    _replay.slippi_version = std::string(_slippi_version);
273    _replay.game_start_raw = std::string(base64_encode(reinterpret_cast<const unsigned char *>(&_rb[_bp+O_GAMEBITS_1]),312));
274    _replay.metadata       = "";
275
276    _replay.sudden_death   = bool(_rb[_bp+O_SUDDEN_DEATH]);
277    _replay.teams          = bool(_rb[_bp+O_IS_TEAMS]);
278    _replay.items_on       = int8_t(_rb[_bp+O_ITEM_SPAWN]);
279    _replay.sd_score       = int8_t(_rb[_bp+O_SD_SCORE]);
280    _replay.stage          = readBE2U(&_rb[_bp+O_STAGE]);
281    _replay.timer          = readBE4U(&_rb[_bp+O_TIMER])/60;
282
283    _replay.timer_behav    = uint8_t(_rb[_bp+O_GAMEBITS_1]) & 0x03;
284    _replay.ui_chars       = (uint8_t(_rb[_bp+O_GAMEBITS_1]) & 0x1C) >> 2;
285    _replay.game_mode      = (uint8_t(_rb[_bp+O_GAMEBITS_1]) & 0xE0) >> 5;
286    _replay.friendly_fire  = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x01;
287    _replay.demo_mode      = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x02;
288    _replay.classic_adv    = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x04;
289    _replay.hrc_event      = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x08;
290    _replay.allstar_wait1  = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x10;
291    _replay.allstar_wait2  = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x20;
292    _replay.allstar_game1  = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x40;
293    _replay.allstar_game2  = uint8_t(_rb[_bp+O_GAMEBITS_2]) & 0x80;
294    _replay.single_button  = uint8_t(_rb[_bp+O_GAMEBITS_3]) & 0x10;
295    _replay.pause_timer    = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x01;
296    _replay.pause_nohud    = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x02;
297    _replay.pause_lras     = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x04;
298    _replay.pause_off      = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x08;
299    _replay.pause_zretry   = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x10;
300    _replay.pause_analog   = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x40;
301    _replay.pause_score    = uint8_t(_rb[_bp+O_GAMEBITS_4]) & 0x80;
302    _replay.items1         = uint8_t(_rb[_bp+ITEMBITS_1]);
303    _replay.items2         = uint8_t(_rb[_bp+ITEMBITS_2]);
304    _replay.items3         = uint8_t(_rb[_bp+ITEMBITS_3]);
305    _replay.items4         = uint8_t(_rb[_bp+ITEMBITS_4]);
306    _replay.items5         = uint8_t(_rb[_bp+ITEMBITS_5]);
307    if (_replay.stage >= Stage::__LAST) {
308      FAIL_CORRUPT("Stage ID " << +_replay.stage << " is invalid");
309      return false;
310    }
311    _replay.seed           = readBE4U(&_rb[_bp+O_RNG_GAME_START]);
312
313    if(MIN_VERSION(1,5,0)) {
314      _replay.pal            = bool(_rb[_bp+O_IS_PAL]);
315    }
316    if(MIN_VERSION(2,0,0)) {
317      _replay.frozen_stadium = bool(_rb[_bp+O_PS_FROZEN]);
318    }
319    if(MIN_VERSION(3,7,0)) {
320      _replay.scene_min      = uint8_t(_rb[_bp+O_SCENE_MIN]);
321      _replay.scene_maj      = uint8_t(_rb[_bp+O_SCENE_MAJ]);
322    }
323
324    _max_frames = getMaxNumFrames();
325    _replay.setFrames(_max_frames);
326    DOUT1("    Estimated " << _max_frames << " gameplay frames (" << (_replay.frame_count) << " total frames)" << std::endl);
327    return true;
328  }
329
330  bool Parser::_parsePreFrame() {
331    DOUT2("  Parsing pre frame event at byte " << +_bp << std::endl);
332    int32_t fnum = readBE4S(&_rb[_bp+O_FRAME]);
333    int32_t f    = fnum-LOAD_FRAME;
334
335    if (fnum < LOAD_FRAME) {
336      FAIL_CORRUPT("Frame index " << fnum << " less than " << +LOAD_FRAME);
337      return false;
338    }
339    if (fnum >= _max_frames) {
340      FAIL_CORRUPT("Frame index " << fnum
341        << " greater than max frames computed from reported raw size (" << _max_frames << ")");
342      return false;
343    }
344
345    uint8_t p    = uint8_t(_rb[_bp+O_PLAYER])+4*uint8_t(_rb[_bp+O_FOLLOWER]); //Includes follower
346    if (p > 7 || _replay.player[p].frame == nullptr) {
347      FAIL_CORRUPT("Invalid player index " << +p);
348      return false;
349    }
350
351    _replay.last_frame                      = fnum;
352    _replay.frame_count                     = f+1; //Update the last frame we actually read
353    _replay.player[p].frame[f].frame        = fnum;
354    _replay.player[p].frame[f].player       = p%4;
355    _replay.player[p].frame[f].follower     = (p>3);
356    _replay.player[p].frame[f].alive        = 1;
357    _replay.player[p].frame[f].seed         = readBE4U(&_rb[_bp+O_RNG_PRE]);
358    _replay.player[p].frame[f].action_pre   = readBE2U(&_rb[_bp+O_ACTION_PRE]);
359    _replay.player[p].frame[f].pos_x_pre    = readBE4F(&_rb[_bp+O_XPOS_PRE]);
360    _replay.player[p].frame[f].pos_y_pre    = readBE4F(&_rb[_bp+O_YPOS_PRE]);
361    _replay.player[p].frame[f].face_dir_pre = readBE4F(&_rb[_bp+O_FACING_PRE]);
362    _replay.player[p].frame[f].joy_x        = readBE4F(&_rb[_bp+O_JOY_X]);
363    _replay.player[p].frame[f].joy_y        = readBE4F(&_rb[_bp+O_JOY_Y]);
364    _replay.player[p].frame[f].c_x          = readBE4F(&_rb[_bp+O_CX]);
365    _replay.player[p].frame[f].c_y          = readBE4F(&_rb[_bp+O_CY]);
366    _replay.player[p].frame[f].trigger      = readBE4F(&_rb[_bp+O_TRIGGER]);
367    _replay.player[p].frame[f].buttons      = readBE2U(&_rb[_bp+O_BUTTONS]);
368    _replay.player[p].frame[f].phys_l       = readBE4F(&_rb[_bp+O_PHYS_L]);
369    _replay.player[p].frame[f].phys_r       = readBE4F(&_rb[_bp+O_PHYS_R]);
370
371    if(MIN_VERSION(1,2,0)) {
372      _replay.player[p].frame[f].ucf_x        = uint8_t(_rb[_bp+O_UCF_ANALOG]);
373      if(MIN_VERSION(1,4,0)) {
374        _replay.player[p].frame[f].percent_pre  = readBE4F(&_rb[_bp+O_DAMAGE_PRE]);
375      }
376    }
377
378    return true;
379  }
380
381  bool Parser::_parsePostFrame() {
382    DOUT2("  Parsing post frame event at byte " << +_bp << std::endl);
383    int32_t fnum = readBE4S(&_rb[_bp+O_FRAME]);
384    int32_t f    = fnum-LOAD_FRAME;
385
386    if (fnum < LOAD_FRAME) {
387      FAIL_CORRUPT("Frame index " << fnum << " less than " << +LOAD_FRAME);
388      return false;
389    }
390    if (fnum >= _max_frames) {
391      FAIL_CORRUPT("Frame index " << fnum << " greater than max frames computed from reported raw size ("
392        << _max_frames << ")");
393      return false;
394    }
395
396    uint8_t p    = uint8_t(_rb[_bp+O_PLAYER])+4*uint8_t(_rb[_bp+O_FOLLOWER]); //Includes follower
397    if (p > 7 || _replay.player[p].frame == nullptr) {
398      FAIL_CORRUPT("Invalid player index " << +p);
399      return false;
400    }
401
402    _replay.player[p].frame[f].char_id       = uint8_t(_rb[_bp+O_INT_CHAR_ID]);
403    if (_replay.player[p].frame[f].char_id >= CharInt::__LAST) {
404      FAIL_CORRUPT("Internal characater ID " << +_replay.player[p].frame[f].char_id << " is invalid");
405      return false;
406    }
407
408    _replay.player[p].frame[f].action_post   = readBE2U(&_rb[_bp+O_ACTION_POST]);
409    _replay.player[p].frame[f].pos_x_post    = readBE4F(&_rb[_bp+O_XPOS_POST]);
410    _replay.player[p].frame[f].pos_y_post    = readBE4F(&_rb[_bp+O_YPOS_POST]);
411    _replay.player[p].frame[f].face_dir_post = readBE4F(&_rb[_bp+O_FACING_POST]);
412    _replay.player[p].frame[f].percent_post  = readBE4F(&_rb[_bp+O_DAMAGE_POST]);
413    _replay.player[p].frame[f].shield        = readBE4F(&_rb[_bp+O_SHIELD]);
414    _replay.player[p].frame[f].hit_with      = uint8_t(_rb[_bp+O_LAST_HIT_ID]);
415    _replay.player[p].frame[f].combo         = uint8_t(_rb[_bp+O_COMBO]);
416    _replay.player[p].frame[f].hurt_by       = uint8_t(_rb[_bp+O_LAST_HIT_BY]);
417    _replay.player[p].frame[f].stocks        = uint8_t(_rb[_bp+O_STOCKS]);
418    _replay.player[p].frame[f].action_fc     = readBE4F(&_rb[_bp+O_ACTION_FRAMES]);
419
420    if(MIN_VERSION(2,0,0)) {
421      _replay.player[p].frame[f].flags_1       = uint8_t(_rb[_bp+O_STATE_BITS_1]);
422      _replay.player[p].frame[f].flags_2       = uint8_t(_rb[_bp+O_STATE_BITS_2]);
423      _replay.player[p].frame[f].flags_3       = uint8_t(_rb[_bp+O_STATE_BITS_3]);
424      _replay.player[p].frame[f].flags_4       = uint8_t(_rb[_bp+O_STATE_BITS_4]);
425      _replay.player[p].frame[f].flags_5       = uint8_t(_rb[_bp+O_STATE_BITS_5]);
426      _replay.player[p].frame[f].hitstun       = readBE4F(&_rb[_bp+O_HITSTUN]);
427      _replay.player[p].frame[f].airborne      = bool(_rb[_bp+O_AIRBORNE]);
428      _replay.player[p].frame[f].ground_id     = readBE2U(&_rb[_bp+O_GROUND_ID]);
429      _replay.player[p].frame[f].jumps         = uint8_t(_rb[_bp+O_JUMPS]);
430      _replay.player[p].frame[f].l_cancel      = uint8_t(_rb[_bp+O_LCANCEL]);
431    }
432
433    if(MIN_VERSION(2,1,0)) {
434      _replay.player[p].frame[f].hurtbox       = uint8_t(_rb[_bp+O_HURTBOX]);
435    }
436
437    if(MIN_VERSION(3,5,0)) {
438      _replay.player[p].frame[f].self_air_x    = readBE4F(&_rb[_bp+O_SELF_AIR_X]);
439      _replay.player[p].frame[f].self_air_y    = readBE4F(&_rb[_bp+O_SELF_AIR_Y]);
440      _replay.player[p].frame[f].attack_x      = readBE4F(&_rb[_bp+O_ATTACK_X]);
441      _replay.player[p].frame[f].attack_y      = readBE4F(&_rb[_bp+O_ATTACK_Y]);
442      _replay.player[p].frame[f].self_grd_x    = readBE4F(&_rb[_bp+O_SELF_GROUND_X]);
443    }
444
445    if(MIN_VERSION(3,8,0)) {
446      _replay.player[p].frame[f].hitlag        = readBE4F(&_rb[_bp+O_HITLAG]);
447    }
448
449    return true;
450  }
451
452  bool Parser::_parseItemUpdate() {
453    DOUT2("  Parsing item frame event at byte " << +_bp << std::endl);
454    int32_t fnum = readBE4S(&_rb[_bp+O_FRAME]);
455
456    if (fnum < LOAD_FRAME) {
457      FAIL_CORRUPT("Frame index " << fnum << " less than " << +LOAD_FRAME);
458      return false;
459    }
460    if (fnum >= _max_frames) {
461      FAIL_CORRUPT("Frame index " << fnum << " greater than max frames computed from reported raw size ("
462        << _max_frames << ")");
463      return false;
464    }
465
466    uint32_t id    = readBE4U(&_rb[_bp+O_ITEM_ID]);
467
468    if (id > MAX_ITEMS) {
469      return true;  //We can't output this many items (TODO: what's with item ID 3039053192???)
470    }
471
472    int32_t f    = _replay.item[id].num_frames;
473
474    _replay.item[id].spawn_id          = id;
475    if (id >= _replay.num_items) {
476      for(unsigned i = _replay.num_items; i <= id; ++i) {
477        _replay.item[i].frame = new SlippiItemFrame[MAX_ITEM_LIFE];
478      }
479      _replay.num_items = id + 1;
480    }
481
482    if (f < MAX_ITEM_LIFE) {
483      _replay.item[id].num_frames       += 1;
484      _replay.item[id].type              = readBE2U(&_rb[_bp+O_ITEM_TYPE]);
485      _replay.item[id].frame[f].frame    = fnum;
486      _replay.item[id].frame[f].state    = uint8_t(_rb[_bp+O_ITEM_STATE]);
487      _replay.item[id].frame[f].face_dir = readBE4F(&_rb[_bp+O_ITEM_FACING]);
488      _replay.item[id].frame[f].xvel     = readBE4F(&_rb[_bp+O_ITEM_XVEL]);
489      _replay.item[id].frame[f].yvel     = readBE4F(&_rb[_bp+O_ITEM_YVEL]);
490      _replay.item[id].frame[f].xpos     = readBE4F(&_rb[_bp+O_ITEM_XPOS]);
491      _replay.item[id].frame[f].ypos     = readBE4F(&_rb[_bp+O_ITEM_YPOS]);
492      _replay.item[id].frame[f].damage   = readBE2U(&_rb[_bp+O_ITEM_DAMAGE]);
493      _replay.item[id].frame[f].expire   = readBE4F(&_rb[_bp+O_ITEM_EXPIRE]);
494      if(MIN_VERSION(3,2,0)) {
495        _replay.item[id].frame[f].flags_1  = uint8_t(_rb[_bp+O_ITEM_MISC]);
496        _replay.item[id].frame[f].flags_2  = uint8_t(_rb[_bp+O_ITEM_MISC+1]);
497        _replay.item[id].frame[f].flags_3  = uint8_t(_rb[_bp+O_ITEM_MISC+2]);
498        _replay.item[id].frame[f].flags_4  = uint8_t(_rb[_bp+O_ITEM_MISC+3]);
499        if(MIN_VERSION(3,6,0)) {
500          _replay.item[id].frame[f].owner    = int8_t(_rb[_bp+O_ITEM_OWNER]);
501        }
502      }
503    } else {
504      DOUT1("Item " << +id << " was alive longer than expected " << std::endl);
505    }
506
507    return true;
508  }
509
510  bool Parser::_parseGameEnd() {
511    DOUT1("  Parsing game end event at byte " << +_bp << std::endl);
512    _replay.end_type       = uint8_t(_rb[_bp+O_END_METHOD]);
513
514    if(MIN_VERSION(2,0,0)) {
515      _replay.lras           = int8_t(_rb[_bp+O_LRAS]);
516    }
517
518    // Determine game winner
519    int   winner_stocks = 0;
520    float winner_damage = 0;
521    for(unsigned p = 0; p < 4; ++p) {
522      if (_replay.player[p].player_type == 3) {
523        continue;  //If we're not playing, we probably didn't win
524      }
525      int   end_stocks = _replay.player[p].frame[_replay.frame_count-1].stocks;
526      _replay.player[p].end_stocks = end_stocks;
527      float end_damage = _replay.player[p].frame[_replay.frame_count-1].percent_post;
528      if ((end_stocks > winner_stocks) || (end_stocks == winner_stocks && end_damage < winner_damage)) {
529        winner_stocks = end_stocks;
530        winner_damage = end_damage;
531        _replay.winner_id = p;  //Tentatively set this person as the winner
532      }
533    }
534    return true;
535  }
536
537  bool Parser::_parseMetadata() {
538    DOUT1("Parsing metadata" << std::endl);
539
540    //Parse metadata from UBJSON as regular JSON
541    std::stringstream ss;
542
543    std::string indent  = " ";
544    std::string key     = "";
545    std::string val     = "";
546    std::string keypath = "";  //Flattened representation of current JSON key
547    bool        done    = false;
548    bool        fail    = false;
549    int32_t     n       = 0;
550
551    std::regex comma_killer("(,)(\\s*\\})");
552
553    uint8_t strlen = 0;
554    for(unsigned i = 0; _bp+i < _file_size;) {
555      //Get next key
556      switch(_rb[_bp+i]) {
557        case 0x55: //U -> Length upcoming
558          if (_bp+i+1 >= _file_size) {
559            fail = true; break;
560          }
561          strlen = _rb[_bp+i+1];
562          if (strlen > 0) {
563            if (_bp+i+1+strlen >= _file_size) {
564              fail = true; break;
565            }
566            key.assign(&_rb[_bp+i+2],strlen);
567            // std::cout << keypath << std::endl;
568          } else {
569            key = "";  //Not sure if empty strings are actually valid, but just in case
570          }
571          keypath += ","+key;
572          if (key.compare("metadata") != 0) {
573            ss << indent << "\"" << key << "\" : ";
574          }
575          i = i+2+strlen;
576          break;
577        case 0x7d: //} -> Object ending
578          keypath = keypath.substr(0,keypath.find_last_of(","));
579          indent = indent.substr(1);
580          if (indent.length() == 0) {
581            done = true;
582            break;
583          }
584          ss << indent << "}," << std::endl;
585          i = i+1;
586          continue;
587        default:
588          std::cerr << "Warning: don't know what's happening; expected key" << std::endl;
589          return false;
590      }
591      if (done) {
592        break;
593      } else if (fail || _bp+i >= _file_size) {
594        std::cerr << "Warning: metadata shorter than expected" << std::endl;
595        return false;
596      }
597      //Get next value
598      switch(_rb[_bp+i]) {
599        case 0x7b: //{ -> Object upcoming
600          ss << "{" << std::endl;
601          if (key.compare("metadata") != 0) {
602            indent = indent+" ";
603          }
604          i = i+1;
605          break;
606        case 0x53: //S -> string upcoming
607          if (_bp+i+2 >= _file_size) {
608            fail = true; break;
609          }
610          ss << "\"";
611          if (_rb[_bp+i+1] != 0x55) {  //If the string is not of length U
612            std::cerr << "Warning: found a long string we can't parse yet:" << std::endl;
613            std::cerr << "  " << ss.str() << std::endl;
614            return false;
615          }
616          strlen = _rb[_bp+i+2];
617          if (_bp+i+2+strlen >= _file_size) {
618            fail = true; break;
619          }
620          val.assign(&_rb[_bp+i+3],strlen);
621          ss << val << "\"," << std::endl;
622          if (key.compare("startAt") == 0) {
623            _replay.start_time = val;
624          } else if (key.compare("playedOn") == 0) {
625            _replay.played_on = val;
626          } else if (key.compare("netplay") == 0) {
627            size_t portpos = keypath.find("players,");
628            if (portpos != std::string::npos) {
629              int port = keypath.substr(portpos+8,1).c_str()[0] - '0';
630              _replay.player[port].tag = val;
631            }
632          } else if (key.compare("code") == 0) {
633            size_t portpos = keypath.find("players,");
634            if (portpos != std::string::npos) {
635              int port = keypath.substr(portpos+8,1).c_str()[0] - '0';
636              _replay.player[port].tag_code = val;
637            }
638          }
639          i = i+3+strlen;
640          keypath = keypath.substr(0,keypath.find_last_of(","));
641          break;
642        case 0x6c: //l -> 32-bit signed int upcoming
643          if (_bp+i+4 >= _file_size) {
644            fail = true; break;
645          }
646          n = readBE4S(&_rb[_bp+i+1]);
647          ss << std::dec << n << "," << std::endl;
648          i = i+5;
649          keypath = keypath.substr(0,keypath.find_last_of(","));
650          break;
651        default:
652          std::cerr << "Warning: don't know what's happening; expected value" << std::endl;
653          return false;
654      }
655      if (fail) {
656        std::cerr << "Warning: metadata shorter than expected" << std::endl;
657        return false;
658      }
659      continue;
660    }
661
662    std::string metadata = ss.str();
663    metadata = metadata.substr(0,metadata.length()-2);  //Remove trailing comma
664    std::string mjson;
665    std::regex_replace(  //Get rid of extraneous commas in our otherwise valid JSON
666      std::back_inserter(mjson),
667      metadata.begin(),
668      metadata.end(),
669      comma_killer,
670      "$2"
671      );
672
673    _replay.metadata = mjson;
674
675    return true;
676  }
677
678  Analysis* Parser::analyze() {
679    Analyzer a(_debug);
680    return a.analyze(_replay);
681  }
682
683  void Parser::_cleanup() {
684    _replay.cleanup();
685  }
686
687  std::string Parser::asJson(bool delta) {
688    return _replay.replayAsJson(delta);
689  }
690
691  void Parser::save(const char* outfilename,bool delta) {
692    DOUT1("Saving JSON" << std::endl);
693    std::ofstream ofile2;
694    ofile2.open(outfilename);
695    ofile2 << asJson(delta) << std::endl;
696    ofile2.close();
697    DOUT1("Saved to " << outfilename << "!" << std::endl);
698  }
699
700
701}