fork/slippc

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

Patrick CrainBumper parser and analyzer versions to synchronize with slip.py81596bb

0.4.0
3.2 KiB83 linesraw
1#ifndef PARSER_H_
2#define PARSER_H_
3
4#include <iostream>
5#include <fstream>
6#include <sstream>
7#include <regex>
8
9#include "util.h"
10#include "replay.h"
11#include "analyzer.h"
12
13// Replay File (.slp) Spec: https://github.com/project-slippi/project-slippi/wiki/Replay-File-Spec
14
15const std::string PARSER_VERSION = "0.4.0";
16
17namespace slip {
18
19class Parser {
20private:
21  int             _debug;                    //Current debug level
22  SlippiReplay    _replay;                   //Internal struct for replay being parsed
23  uint16_t        _payload_sizes[256] = {0}; //Size of payload for each event
24  std::string     _slippi_version;           //String representation of the Slippi version of the replay
25  uint8_t         _slippi_maj = 0;           //Major version number of replay being parsed
26  uint8_t         _slippi_min = 0;           //Minor version number of replay being parsed
27  uint8_t         _slippi_rev = 0;           //Revision number of replay being parsed
28  int32_t         _max_frames = 0;           //Maximum number of frames that there will be in the replay file
29
30  char*           _rb = nullptr; //Read buffer
31  unsigned        _bp; //Current position in buffer
32  uint32_t        _length_raw; //Remaining length of raw payload
33  uint32_t        _length_raw_start; //Total length of raw payload
34  uint32_t        _file_size; //Total size of the replay file on disk
35  bool            _parse(); //Internal main parsing funnction
36  bool            _parseHeader();
37  bool            _parseEventDescriptions();
38  bool            _parseEvents();
39  bool            _parseGameStart();
40  bool            _parsePreFrame();
41  bool            _parsePostFrame();
42  bool            _parseGameEnd();
43  bool            _parseMetadata();
44  void            _cleanup(); //Cleanup replay data
45public:
46  Parser(int debug_level);               //Instantiate the parser (possibly in debug mode)
47  ~Parser();                             //Destroy the parser
48  bool load(const char* replayfilename); //Load a replay file
49  Analysis* analyze();                   //Analyze the loaded replay file
50  std::string asJson(bool delta);        //Convert the parsed replay structure to a JSON
51  void save(const char* outfilename,bool delta); //Save a replay file
52
53  //Estimate the maximum number of frames stored in the file
54  //  -> Assumes only two people are alive for the whole match / one ice climber
55  inline int32_t getMaxNumFrames() {
56    //Get the base size for the file
57    unsigned base_size = 0;
58    base_size += 1+_payload_sizes[Event::EV_PAYLOADS];  //One meta event
59    base_size += 1+_payload_sizes[Event::GAME_START]; //One start event
60    base_size += 1+_payload_sizes[Event::GAME_END]; //One end event
61
62    unsigned num_players = 2;
63
64    unsigned frame_size = num_players*(
65      _payload_sizes[Event::PRE_FRAME]+
66      1+
67      _payload_sizes[Event::POST_FRAME]+
68      1
69      );
70
71    if (_length_raw_start < base_size) {
72      return 0;  //There's a problem
73    }
74
75    // std::cerr << "((" << _length_raw_start << "-" << base_size << ")/" << frame_size << ")-123" << std::endl;
76    int32_t maxframes = ((_length_raw_start-base_size)/frame_size)+LOAD_FRAME;
77    return maxframes+1;  //TODO: the above doesn't compute an exact number sometimes and we need the +1; figure out why
78  }
79};
80
81}
82
83#endif /* PARSER_H_ */