fork/slippc

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

Patrick CrainUnify version strings between parser, analyzer, and compressor into SLIPPC_VERSION46ef8c0

0.8.1
3.6 KiB89 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
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  bool            _game_end_found = false;   //Whether we've found the game end event
30  bool            _is_encoded     = false;   //Whether this file is encoded by the compressor
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  //Getter function for exposing read-only access to underlying replay
57  inline const SlippiReplay* replay() const {
58    return &_replay;
59  };
60
61  //Estimate the maximum number of frames stored in the file
62  //  -> Assumes only two people are alive for the whole match / one ice climber
63  inline int32_t getMaxNumFrames() {
64    //Get the base size for the file
65    unsigned base_size = 0;
66    base_size += _payload_sizes[Event::EV_PAYLOADS];  //One meta event
67    base_size += _payload_sizes[Event::GAME_START];   //One start event
68    base_size += _payload_sizes[Event::GAME_END];     //One end event
69
70    unsigned num_players = 2;
71
72    unsigned frame_size = num_players*(
73      _payload_sizes[Event::PRE_FRAME]+
74      _payload_sizes[Event::POST_FRAME]
75      );
76
77    if (_length_raw_start < base_size) {
78      return 0;  //There's a problem
79    }
80
81    // std::cerr << "((" << _length_raw_start << "-" << base_size << ")/" << frame_size << ")-123" << std::endl;
82    int32_t maxframes = ((_length_raw_start-base_size)/frame_size)+LOAD_FRAME;
83    return maxframes+1;  //TODO: the above doesn't compute an exact number sometimes and we need the +1; figure out why
84  }
85};
86
87}
88
89#endif /* PARSER_H_ */