fork/slippc

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

Patrick Crainbundled test replays in repository; completely refactored test code to use compressed replays for testing2589656

compression
3.6 KiB91 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.8.0";
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  bool            _game_end_found = false;   //Whether we've found the game end event
32  bool            _is_encoded     = false;   //Whether this file is encoded by the compressor
33
34  char*           _rb = nullptr; //Read buffer
35  unsigned        _bp; //Current position in buffer
36  uint32_t        _length_raw; //Remaining length of raw payload
37  uint32_t        _length_raw_start; //Total length of raw payload
38  uint32_t        _file_size; //Total size of the replay file on disk
39  bool            _parse(); //Internal main parsing funnction
40  bool            _parseHeader();
41  bool            _parseEventDescriptions();
42  bool            _parseEvents();
43  bool            _parseGameStart();
44  bool            _parsePreFrame();
45  bool            _parsePostFrame();
46  bool            _parseGameEnd();
47  bool            _parseItemUpdate();
48  bool            _parseMetadata();
49  void            _cleanup(); //Cleanup replay data
50public:
51  Parser(int debug_level);               //Instantiate the parser (possibly in debug mode)
52  ~Parser();                             //Destroy the parser
53  bool load(const char* replayfilename); //Load a replay file
54  Analysis* analyze();                   //Analyze the loaded replay file
55  std::string asJson(bool delta);        //Convert the parsed replay structure to a JSON
56  void save(const char* outfilename,bool delta); //Save a replay file
57
58  //Getter function for exposing read-only access to underlying replay
59  inline const SlippiReplay* replay() const {
60    return &_replay;
61  };
62
63  //Estimate the maximum number of frames stored in the file
64  //  -> Assumes only two people are alive for the whole match / one ice climber
65  inline int32_t getMaxNumFrames() {
66    //Get the base size for the file
67    unsigned base_size = 0;
68    base_size += _payload_sizes[Event::EV_PAYLOADS];  //One meta event
69    base_size += _payload_sizes[Event::GAME_START];   //One start event
70    base_size += _payload_sizes[Event::GAME_END];     //One end event
71
72    unsigned num_players = 2;
73
74    unsigned frame_size = num_players*(
75      _payload_sizes[Event::PRE_FRAME]+
76      _payload_sizes[Event::POST_FRAME]
77      );
78
79    if (_length_raw_start < base_size) {
80      return 0;  //There's a problem
81    }
82
83    // std::cerr << "((" << _length_raw_start << "-" << base_size << ")/" << frame_size << ")-123" << std::endl;
84    int32_t maxframes = ((_length_raw_start-base_size)/frame_size)+LOAD_FRAME;
85    return maxframes+1;  //TODO: the above doesn't compute an exact number sometimes and we need the +1; figure out why
86  }
87};
88
89}
90
91#endif /* PARSER_H_ */