fork/slippc

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

Patrick CrainFixed escape_json() breaking parsing of original_filename on Windows; bumped version to 0.7.162d6bb0

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