fork/slippc
git clone https://git.julia.blue/fork/slippc
2589656
compression
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 : 23int _debug ;//Current debug level 24SlippiReplay _replay ;//Internal struct for replay being parsed 25uint16_t _payload_sizes [256 ]= {0 };//Size of payload for each event 26std ::string _slippi_version ;//String representation of the Slippi version of the replay 27uint8_t _slippi_maj = 0 ;//Major version number of replay being parsed 28uint8_t _slippi_min = 0 ;//Minor version number of replay being parsed 29uint8_t _slippi_rev = 0 ;//Revision number of replay being parsed 30int32_t _max_frames = 0 ;//Maximum number of frames that there will be in the replay file 31bool _game_end_found = false;//Whether we've found the game end event 32bool _is_encoded = false;//Whether this file is encoded by the compressor 33 34char * _rb = nullptr ;//Read buffer 35unsigned _bp ;//Current position in buffer 36uint32_t _length_raw ;//Remaining length of raw payload 37uint32_t _length_raw_start ;//Total length of raw payload 38uint32_t _file_size ;//Total size of the replay file on disk 39bool _parse ();//Internal main parsing funnction 40bool _parseHeader (); 41bool _parseEventDescriptions (); 42bool _parseEvents (); 43bool _parseGameStart (); 44bool _parsePreFrame (); 45bool _parsePostFrame (); 46bool _parseGameEnd (); 47bool _parseItemUpdate (); 48bool _parseMetadata (); 49void _cleanup ();//Cleanup replay data 50public : 51Parser (int debug_level ); //Instantiate the parser (possibly in debug mode) 52~ Parser (); //Destroy the parser 53bool load ( const char * replayfilename); //Load a replay file 54Analysis * analyze (); //Analyze the loaded replay file 55std :: string asJson ( bool delta); //Convert the parsed replay structure to a JSON 56void save ( const char * outfilename, bool delta); //Save a replay file 57 58//Getter function for exposing read-only access to underlying replay 59inline const SlippiReplay * replay () const { 60return & _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 65inline int32_t getMaxNumFrames () { 66//Get the base size for the file 67unsigned base_size = 0 ; 68base_size += _payload_sizes[Event:: EV_PAYLOADS ]; //One meta event 69base_size += _payload_sizes[Event:: GAME_START ]; //One start event 70base_size += _payload_sizes[Event:: GAME_END ]; //One end event 71 72unsigned num_players = 2 ; 73 74unsigned frame_size = num_players * ( 75_payload_sizes[Event:: PRE_FRAME ] + 76_payload_sizes[Event:: POST_FRAME ] 77); 78 79if (_length_raw_start < base_size) { 80return 0 ; //There's a problem 81} 82 83// std::cerr << "((" << _length_raw_start << "-" << base_size << ")/" << frame_size << ")-123" << std::endl; 84int32_t maxframes = ((_length_raw_start - base_size)/frame_size) + LOAD_FRAME ; 85return 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_ */