fork/slippc
git clone https://git.julia.blue/fork/slippc
7abc388
0.6.1
1#ifndef COMPRESSOR_H_ 2#define COMPRESSOR_H_ 3 4//Debug output convenience macros 5#define DOUT1 (s ) if (_debug >= 1) { std::cout << s; } 6#define DOUT2 (s ) if (_debug >= 2) { std::cout << s; } 7#define DOUT3 (s ) if (_debug >= 3) { std::cout << s; } 8#define FAIL (e ) std::cerr << "ERROR: " << e << std::endl 9#define FAIL_CORRUPT (e ) std::cerr << "ERROR: " << e << "; replay may be corrupt" << std::endl 10 11#include <iostream> 12#include <fstream> 13#include <sstream> 14#include <regex> 15#include <map> 16#include <cmath> 17#include <limits> 18#include <cstdio> 19#include <filesystem> 20 21#include "util.h" 22#include "enums.h" 23#include "schema.h" 24 25// Replay File (.slp) Spec: https://github.com/project-slippi/slippi-wiki/blob/master/SPEC.md 26 27const std ::string COMPRESSOR_VERSION = "0.3.0" ; 28const uint32_t RAW_RNG_MASK = 0x40000000 ;//Second bit of unsigned int 29const uint32_t MAGIC_FLOAT = 0xFF000000 ;//First 8 bits of float 30 31const uint32_t ITEM_SLOTS = 256 ;//Max number of items we expect to track at once 32const uint32_t MESSAGE_SIZE = 517 ;//Size of Message Splitter event 33const uint32_t CODE_SIZE = 32571 ;//Size of gecko.dat file 34 35// Frame event column byte widths (negative numbers denote bit shuffling) 36static int32_t cw_start []= {1 ,4 ,4 ,0 }; 37static int32_t cw_mesg []= {1 ,512 ,2 ,1 ,1 ,0 }; 38static int32_t cw_pre []= {1 ,4 ,1 ,1 ,4 ,2 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,2 ,4 ,4 ,1 ,4 ,0 }; 39static int32_t cw_item []= {1 ,4 ,2 ,1 ,4 ,4 ,4 ,4 ,4 ,2 ,4 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 };// Shuffle bytes of item id 40static int32_t cw_post []= {1 ,4 ,1 ,1 ,1 ,2 ,4 ,4 ,4 ,4 ,4 ,1 ,1 ,1 ,1 ,4 ,1 ,1 ,1 ,1 ,1 ,4 ,1 ,2 ,1 ,1 ,1 ,4 ,4 ,4 ,4 ,4 ,0 }; 41static int32_t cw_end []= {1 ,4 ,4 ,0 }; 42 43// Debug Frame event column byte widths (negative numbers denote bit shuffling) 44static int32_t dw_start []= {1 ,4 ,4 ,0 }; 45static int32_t dw_mesg []= {1 ,512 ,2 ,1 ,1 ,0 }; 46static int32_t dw_pre []= {1 ,4 ,1 ,1 ,4 ,2 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,4 ,2 ,4 ,4 ,1 ,4 ,0 }; 47static int32_t dw_item []= {1 ,4 ,2 ,1 ,4 ,4 ,4 ,4 ,4 ,2 ,4 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,1 ,0 };// Shuffle bytes of item id 48static int32_t dw_post []= {1 ,4 ,1 ,1 ,1 ,2 ,4 ,4 ,4 ,4 ,4 ,1 ,1 ,1 ,1 ,4 ,1 ,1 ,1 ,1 ,1 ,4 ,1 ,2 ,1 ,1 ,1 ,4 ,4 ,4 ,4 ,4 ,0 }; 49static int32_t dw_end []= {1 ,4 ,4 ,0 }; 50 51// static int32_t dw_start[] = {1,4,4,0}; 52// static int32_t dw_mesg[] = {1,512,2,1,1,0}; 53// static int32_t dw_pre[] = {1,4,1,1,4,2,4,4,4,4,4,4,4,4,4,2,4,4,1,4,0}; 54// static int32_t dw_item[] = {1,4,2,1,4,4,4,4,4,2,4, 1,1,1,1 ,1,1,1,1,1,0}; // Shuffle bytes of item id 55// static int32_t dw_post[] = {1,4,1,1,1,2,4,4,4,4,4,-1,-1,-1,-1,4,-1,-1,-1,-1,-1,4,-1,2,-1,-1,-1,4,4,4,4,4,0}; 56// static int32_t dw_end[] = {1,4,4,0}; 57 58namespace slip { 59 60class Compressor { 61private : 62// TODO: this caused problems as a magic number, so leaving it here for now 63int FRAME_ENC_DELTA = 0 ; 64 65int _debug ;//Current debug level 66uint16_t _payload_sizes [256 ]= {0 };//Size of payload for each event 67uint8_t _slippi_maj = 0 ;//Major version number of replay being parsed 68uint8_t _slippi_min = 0 ;//Minor version number of replay being parsed 69uint8_t _slippi_rev = 0 ;//Revision number of replay being parsed 70uint8_t _is_encoded = 0 ;//Encryption status of replay being parsed 71int32_t _max_frames = 0 ;//Maximum number of frames that there will be in the replay file 72std ::string * _outfilename = nullptr ;//Name of the file to write 73 74//Variables needed for mapping floats to ints and vice versa 75std ::map < unsigned ,unsigned > float_to_int ;//Map of floats to ints 76std ::map < unsigned ,unsigned > int_to_float ;//Reverse map of ints back to floats 77std ::map < unsigned ,unsigned > int_to_float_c ;//Counter for reverse map of ints back to floats 78unsigned num_floats = 0 ;//Total number of floats in float map 79 80uint32_t _preds = 0 ; 81uint32_t _fails = 0 ; 82 83uint32_t _rng ;//Current RNG seed we're working with 84uint32_t _rng_start ;//Starting RNG seed 85char _x_pre_frame [8 ][256 ]= {0 };//Delta for pre-frames 86char _x_pre_frame_2 [8 ][256 ]= {0 };//Delta for 2 pre-frames ago 87char _x_post_frame [8 ][256 ]= {0 };//Delta for post-frames 88char _x_post_frame_2 [8 ][256 ]= {0 };//Delta for 2 post-frames ago 89char _x_post_frame_3 [8 ][256 ]= {0 };//Delta for 3 post-frames ago 90char _x_item [ITEM_SLOTS ][256 ]= {0 };//Delta for item updates 91char _x_item_2 [ITEM_SLOTS ][256 ]= {0 };//Delta for item updates 2 frames ago 92char _x_item_3 [ITEM_SLOTS ][256 ]= {0 };//Delta for item updates 3 frames ago 93char _x_item_p [ITEM_SLOTS ][256 ]= {0 };//Delta for item position updates 94int32_t laststartframe = -123 ;//Last frame used in frame start event, encoding 95int32_t lastitemstartframe = -123 ;//Last frame used in item event, encoding 96int32_t lastshuffleframe = -123 ;//Last frame used in frame start event, shuffling 97int32_t lastitemshuffleframe = -123 ;//Last frame used in item event, shuffling 98int32_t lastpreframe [8 ]= {-123 };//Last frame used in pre frame event, encoding 99int32_t lastshufflepreframe [8 ]= {-123 };//Last frame used in pre frame event, shuffling 100int32_t lastpostframe [8 ]= {-123 };//Last frame used in post frame event, encoding 101int32_t lastshufflepostframe [8 ]= {-123 };//Last frame used in post frame event, shuffling 102 103char * _rb = nullptr ;//Read buffer 104char * _wb = nullptr ;//Write buffer 105unsigned _bp ;//Current position in buffer 106uint32_t _length_raw ;//Remaining length of raw payload 107uint32_t _length_raw_start ;//Total length of raw payload 108uint32_t _game_loop_start = 0 ;//First byte AFTER game start event 109uint32_t _game_loop_end = 0 ;//First byte OF game end event 110uint32_t _file_size ;//Total size of the replay file on disk 111bool _parse ();//Internal main parsing funnction 112bool _parseHeader (); 113bool _parseEventDescriptions (); 114bool _parseEvents (); 115bool _parseGameStart (); 116bool _parseGeckoCodes (); 117bool _parsePreFrame (); 118bool _parsePostFrame (); 119bool _parseFrameStart (); 120bool _parseItemUpdate (); 121bool _parseBookend (); 122bool _shuffleEvents (bool unshuffle = false); 123bool _unshuffleEvents (); 124public : 125Compressor (int debug_level ); //Instantiate the parser (possibly in debug mode) 126~ Compressor (); //Destroy the parser 127bool loadFromFile ( const char * replayfilename); //Load a replay file 128void saveToFile ( bool rawencode); //Save an encoded replay file 129bool setOutputFilename ( const char * fname); //Set output file name 130bool loadFromBuff ( char ** buffer, unsigned size); //Load a replay from a buffer 131unsigned saveToBuff ( char ** buffer); //Save an encoded replay buffer 132bool validate (); //Validate the encoding 133 134//https://www.reddit.com/r/SSBM/comments/71gn1d/the_basics_of_rng_in_melee/ 135inline int32_t rollRNGLegacy ( int32_t seed) const { 136int64_t bigseed = seed; //Cast from 32-bit to 64-bit int 137return ((bigseed * 214013 ) + 2531011 ) % 4294967296 ; 138} 139 140//Given the current frame's RNG value, just add 65536 to get the next frame's 141inline int32_t rollRNGRollback ( int32_t seed) const { 142return (seed + 65536 ) % 4294967296 ; 143} 144 145//Per Fizzi and Nikki, RNG is just the starting RNG + 65536*<0-indexed frame> 146inline int32_t computeRNGRollback ( int32_t framenum) const { 147return (((framenum + 123 ) * 65536 ) + _rng_start) % 4294967296 ; 148} 149 150//Check the RAW_RNG_MASK bit from a frame, and return whether that bit was flipped 151inline bool checkRawRNG ( int32_t & frame) const { 152//If first and second bits of frame are different, rng is stored raw 153uint8_t bit1 = (frame >> 31 ) & 0x01 ; 154uint8_t bit2 = (frame >> 30 ) & 0x01 ; 155uint8_t rng_is_raw = bit1 ^ bit2; 156return rng_is_raw > 0 ; 157} 158 159//Predict the next frame given a reference frame and delta encode the difference 160inline int32_t predictFrame ( int32_t frame, int32_t ref_frame, char * write_addr = nullptr ) const { 161//Remove RNG bit from frame 162bool rng_is_raw = checkRawRNG (frame); 163if (rng_is_raw) { 164frame ^= RAW_RNG_MASK ; 165} 166//Flip 2nd bit in _wb output if raw RNG flag is set 167uint32_t bitmask = rng_is_raw ? RAW_RNG_MASK : 0x00000000 ; 168 169int32_t frame_delta_pred; 170if (_is_encoded) { //Decode 171frame_delta_pred = frame + (ref_frame + FRAME_ENC_DELTA ); 172if (write_addr) { 173writeBE4S (frame_delta_pred ^ bitmask,write_addr); 174} 175} else { //Encode 176frame_delta_pred = frame - (ref_frame + FRAME_ENC_DELTA ); 177if (write_addr) { 178writeBE4S (frame_delta_pred,write_addr); 179} 180} 181return frame_delta_pred; 182} 183 184//Version of predictFrame used for _unshuffleEvents 185inline int32_t decodeFrame ( int32_t frame, int32_t ref_frame) const { 186return predictFrame (frame, ref_frame, nullptr ); 187} 188 189//Create a mapping of floats we've already seen by treating them as unsigned integer indices 190// to an index build on-the-fly 191inline void buildFloatMap ( unsigned off) { 192uint32_t enc_float = readBE4U ( & _rb[_bp + off]); 193if (_is_encoded) { //Decode 194if ((enc_float & MAGIC_FLOAT ) != MAGIC_FLOAT ) { 195//If it's our first encounter with a float, add it to our maps 196float_to_int[enc_float] = num_floats; 197int_to_float[num_floats] = enc_float; 198++ num_floats; 199} else { 200//Decode our int back to a float 201unsigned as_unsigned = int_to_float[enc_float & ( MAGIC_FLOAT ^ 0xFFFFFFFF )]; 202writeBE4U (as_unsigned, & _wb[_bp + off]); 203} 204} else { //Encode 205if (float_to_int. find (enc_float) == float_to_int. end ()) { 206//If it's our first encounter with a float, add it to our maps 207float_to_int[enc_float] = num_floats; 208int_to_float[num_floats] = enc_float; 209int_to_float_c[num_floats] = 1 ; 210++ num_floats; 211} else { 212//Set the 2nd and 3rd bit, since they will never be set simultaneously in real floats 213++ int_to_float_c[float_to_int[enc_float]]; 214writeBE4U (float_to_int[enc_float] | MAGIC_FLOAT , & _wb[_bp + off]); 215} 216} 217} 218 219//Compress analog float values by converting them to ints 220inline void encodeAnalog ( unsigned off, float mult) { 221union { float f ; uint32_t u ; uint8_t c [ 4 ]; int8_t i [ 4 ]; } float_true, float_pred, float_rest; 222 223// If any of exponent bits are set, we still have a float 224char * main_buf = _is_encoded ? _wb : _rb; 225float_true. u = readBE4U ( & main_buf[_bp + off]); 226if ((float_true. u & 0x7f800000 ) && _is_encoded) { 227return ; //If we have a float in the encoded state, nothing to do 228} 229 230float_pred. u = 0 ; //Initialize all bytes to zero 231if (_is_encoded) { 232if (mult > 127 ) { 233float_pred. f = float (float_true. c [ 0 ])/mult; 234} else { 235float_pred. f = float (float_true. i [ 0 ])/mult; 236} 237writeBE4F (float_pred. f , & _wb[_bp + off]); //Write our decoded float 238} else { 239if (mult > 127 ) { 240float_pred. c [ 0 ] = uint8_t ( round ( float (float_true. f * mult))); 241float_rest. f = float (float_pred. c [ 0 ])/mult; 242} else { 243float_pred. i [ 0 ] = int8_t ( round ( float (float_true. f * mult))); 244float_rest. f = float (float_pred. i [ 0 ])/mult; 245} 246if (float_rest. u == float_true. u ) { //Verify we can properly decode 247writeBE4U (float_pred. u , & _wb[_bp + off]); //Write our encoded int 248} 249} 250} 251 252//General purpose function for encoding the difference 253// between floats in two buffers, using a magic float value 254// to flag whether such an encoded value is present 255// (useful for computing item velocities in terms of positions) 256inline void predictAsDifference ( unsigned off, unsigned buffoff, char * buff) { 257union { float f ; uint32_t u ; } float_true, float_pred, float_temp; 258//If our prediction is accurate to at least the last 10 bits, it's close enough 259uint32_t MAXDIFF = 0x03FF ; 260 261char * main_buf = _is_encoded ? _wb : _rb; 262 263float_true. f = readBE4F ( & main_buf[_bp + off]); 264float_pred. f = readBE4F ( & main_buf[_bp + buffoff]) - readBE4F ( & buff[buffoff]); 265float_temp. u = float_pred. u ^ float_true. u ; 266 267if (_is_encoded) { 268uint32_t diff = float_true. u ^ MAGIC_FLOAT ; 269if (diff <= MAXDIFF ) { //If our prediction was accurate to at least the last few bits 270float_pred. u ^= diff; 271writeBE4F (float_pred. f , & _wb[_bp + off]); //Write our predicted float 272} 273} else { 274//If at most the last few bits are off, we succeeded 275if (float_temp. u <= MAXDIFF ) { 276writeBE4U ( MAGIC_FLOAT ^ float_temp. u , & _wb[_bp + off]); //Write an impossible float 277} else { 278// std::cout << "DIFF =" << float_temp.u << std::endl; 279} 280} 281 282memcpy ( & buff[buffoff], & main_buf[_bp + buffoff], 4 ); 283} 284 285//General purpose function for predicting acceleration based 286// on float data in three buffers, using a magic float value 287// to flag whether such an encoded value is present 288inline void predictAccel ( unsigned p, unsigned off, char * buff1, char * buff2, char * buff3) { 289union { float f ; uint32_t u ; } float_true, float_pred, float_temp; 290//If our prediction is accurate to at least the last 8 bits, it's close enough 291const uint32_t MAXDIFF = 0xFF ; 292 293float_true. f = readBE4F ( & _rb[_bp + off]); 294float pos1 = readBE4F ( & buff1[off]); 295float pos2 = readBE4F ( & buff2[off]); 296float pos3 = readBE4F ( & buff3[off]); 297float vel1 = pos1 - pos2; 298float vel2 = pos2 - pos3; 299float acc1 = vel1 - vel2; 300float_pred. f = pos1 + vel1 + acc1; 301float_temp. u = float_pred. u ^ float_true. u ; 302 303if (_is_encoded) { 304uint32_t diff = float_true. u ^ MAGIC_FLOAT ; 305if (diff <= MAXDIFF ) { //If our prediction was accurate to at least the last few bits 306float_pred. u ^= diff; 307writeBE4F (float_pred. f , & _wb[_bp + off]); //Write our predicted float 308} 309} else { 310//If at most the last few bits are off, we succeeded 311if (float_temp. u <= MAXDIFF ) { 312writeBE4U ( MAGIC_FLOAT ^ float_temp. u , & _wb[_bp + off]); //Write an impossible float 313} 314} 315 316memcpy ( & buff3[off], & buff2[off], 4 ); 317memcpy ( & buff2[off], & buff1[off], 4 ); 318memcpy ( & buff1[off],_is_encoded ? & _wb[_bp + off] : & _rb[_bp + off], 4 ); 319} 320 321//General purpose function for predicting velocity based 322// on float data in two buffers, using a magic float value 323// to flag whether such an encoded value is present 324inline void predictVeloc ( unsigned p, unsigned off, char * buff1, char * buff2) { 325union { float f ; uint32_t u ; } float_true, float_pred, float_temp; 326 327float_true. f = readBE4F ( & _rb[_bp + off]); 328float_pred. f = 2 * readBE4F ( & buff1[off]) - readBE4F ( & buff2[off]); 329float_temp. u = float_pred. u ^ float_true. u ; 330if (_is_encoded) { 331if (float_true. u == MAGIC_FLOAT ) { //If our prediction was exactly accurate 332writeBE4F (float_pred. f , & _wb[_bp + off]); //Write our predicted float 333} 334} else { 335if (float_temp. u == 0 ) { //If our prediction was exactly accurate 336writeBE4U ( MAGIC_FLOAT , & _wb[_bp + off]); //Write an impossible float 337} 338} 339 340memcpy ( & buff2[off], & buff1[off], 4 ); 341memcpy ( & buff1[off],_is_encoded ? & _wb[_bp + off] : & _rb[_bp + off], 4 ); 342} 343 344//Using the previous three post-frame events as reference, predict a floating point value, and store 345// an otherwise-impossible float if our prediction was correct 346inline void predictAccelPost ( unsigned p, unsigned off) { 347predictAccel (p,off,_x_post_frame[p],_x_post_frame_2[p],_x_post_frame_3[p]); 348} 349 350//Using the previous three item events as reference, predict a floating point value, and store 351// an otherwise-impossible float if our prediction was correct 352inline void predictAccelItem ( unsigned p, unsigned off) { 353predictAccel (p,off,_x_item[p],_x_item_2[p],_x_item_3[p]); 354} 355 356//Using the previous two pre-frame events as reference, predict a floating point value, and store 357// an otherwise-impossible float if our prediction was correct 358inline void predictVelocPre ( unsigned p, unsigned off) { 359predictVeloc (p,off,_x_pre_frame[p],_x_pre_frame_2[p]); 360} 361 362//Using the previous two post-frame events as reference, predict a floating point value, and store 363// an otherwise-impossible float if our prediction was correct 364inline void predictVelocPost ( unsigned p, unsigned off) { 365predictVeloc (p,off,_x_post_frame[p],_x_post_frame_2[p]); 366} 367 368//Using the previous two item events with slot id as reference, predict a floating point value, and store 369// an otherwise-impossible float if our prediction was correct 370inline void predictVelocItem ( unsigned p, unsigned off) { 371predictVeloc (p,off,_x_item[p],_x_item_2[p]); 372} 373 374//Predict the RNG by reading a full (not delta-encoded) frame and 375// counting the number of rolls it takes to arrive at the correct seed 376inline void predictRNG ( unsigned frameoff, unsigned rngoff) { 377// return; 378const uint32_t MAX_ROLLS = 128 ; 379 380//Read true frame from write buffer 381int32_t frame; 382if (_is_encoded) { //Read from write buffer 383frame = readBE4S ( & _wb[_bp + frameoff]); 384} else { //Read from read buffer 385frame = readBE4S ( & _rb[_bp + frameoff]); 386} 387bool rng_is_raw = checkRawRNG (frame); 388if (rng_is_raw) { 389frame ^= RAW_RNG_MASK ; 390} 391 392//Get RNG seed and record number of rolls it takes to get to that point 393if ( MIN_VERSION ( 3 , 7 , 0 )) { //New RNG 394_rng = computeRNGRollback (frame); 395if (_is_encoded) { //Decode 396if (rng_is_raw == 0 ) { //Roll RNG a few times until we get to the desired value 397unsigned rolls = readBE4U ( & _rb[_bp + rngoff]); 398if (rolls < MAX_ROLLS ) { 399for ( unsigned i = 0 ; i < rolls; ++ i) { 400_rng = rollRNGRollback (_rng); 401} 402} else { //Fallback on old RNG rolling method 403for ( unsigned i = MAX_ROLLS ; i < rolls; ++ i) { 404_rng = rollRNGLegacy (_rng); 405} 406} 407writeBE4U (_rng, & _wb[_bp + rngoff]); 408} else { 409writeBE4U (frame, & _wb[_bp + frameoff]); 410} 411} else { //Encode 412unsigned rolls = 0 ; 413unsigned target = readBE4U ( & _rb[_bp + rngoff]); 414for ( uint32_t start_rng = _rng; (start_rng != target) && (rolls < MAX_ROLLS ); ++ rolls) { 415start_rng = rollRNGRollback (start_rng); 416} 417if (rolls < MAX_ROLLS ) { //If we can encode RNG as < 256 rolls, do it 418writeBE4U (rolls, & _wb[_bp + rngoff]); 419_rng = target; 420} 421else { 422rolls = 0 ; 423for ( uint32_t start_rng = _rng; (start_rng != target) && (rolls < MAX_ROLLS ); ++ rolls) { 424start_rng = rollRNGLegacy (start_rng); 425} 426if (rolls < MAX_ROLLS ) { 427writeBE4U (rolls + MAX_ROLLS , & _wb[_bp + rngoff]); 428} else { //Store the raw RNG value 429//Load the predicted frame value 430int32_t predicted_frame = readBE4S ( & _wb[_bp + frameoff]); 431//Flip second bit of cached frame number to signal raw rng 432writeBE4S (predicted_frame ^ RAW_RNG_MASK , & _wb[_bp + frameoff]); 433} 434} 435} 436} else { //Old RNG 437if (_is_encoded) { 438unsigned rolls = readBE4U ( & _rb[_bp + rngoff]); 439for ( unsigned i = 0 ; i < rolls; ++ i) { 440_rng = rollRNGLegacy (_rng); 441} 442writeBE4U (_rng, & _wb[_bp + rngoff]); 443} else { //Roll RNG until we hit the target, write the # of rolls 444unsigned rolls, seed = readBE4U ( & _rb[_bp + rngoff]); 445for (rolls = 0 ;_rng != seed; ++ rolls) { 446_rng = rollRNGLegacy (_rng); 447} 448writeBE4U (rolls, & _wb[_bp + rngoff]); 449} 450} 451} 452 453//XOR bytes in the current read buffer with bytes from another buffer 454inline void xorEncodeRange ( unsigned start, unsigned stop, char * buffer) { 455for ( unsigned i = start; i < stop; ++ i) { 456_wb[_bp + i] = _rb[_bp + i] ^ buffer[i]; 457buffer[i] = _is_encoded ? _wb[_bp + i] : _rb[_bp + i]; 458} 459} 460 461inline bool _shuffleColumns ( unsigned * offset) { 462truncateColumnWidthsToVersion (); 463char * main_buf = _wb; 464 465// Track the starting position of the buffer 466unsigned s = _game_loop_start; 467unsigned * mem_size = new unsigned[ 1 ]; 468 469// Shuffle message columns 470if (main_buf[s] == Event:: SPLIT_MSG ) { 471* mem_size = offset[ 19 ]; 472_transposeEventColumns (main_buf,s,mem_size,_debug ? dw_mesg : cw_mesg,false); 473s += * mem_size; 474} 475 476// Shuffle frame start columns 477if (main_buf[s] == Event:: FRAME_START ) { 478* mem_size = offset[ 0 ]; 479_transposeEventColumns (main_buf,s,mem_size,_debug ? dw_start : cw_start,false); 480s += * mem_size; 481} 482 483// Shuffle pre frame columns 484for ( unsigned p = 0 ; p < 8 ; ++ p) { 485// True player order: 0,4,1,5,2,6,3,7 486unsigned i = ( 4 * (p% 2 )) + unsigned (p/ 2 ); 487if (main_buf[s] != Event:: PRE_FRAME ) { 488break ; 489} 490* mem_size = offset[ 1 + i]; 491if ( * mem_size == 0 ) { 492continue ; 493} 494_transposeEventColumns (main_buf,s,mem_size,_debug ? dw_pre : cw_pre,false); 495s += * mem_size; 496} 497 498// Shuffle item columns 499if (main_buf[s] == Event:: ITEM_UPDATE ) { 500* mem_size = offset[ 9 ]; 501_transposeEventColumns (main_buf,s,mem_size,_debug ? dw_item : cw_item,false); 502s += * mem_size; 503} 504 505// Shuffle post frame columns 506for ( unsigned p = 0 ; p < 8 ; ++ p) { 507// True player order: 0,4,1,5,2,6,3,7 508unsigned i = ( 4 * (p% 2 )) + unsigned (p/ 2 ); 509if (main_buf[s] != Event:: POST_FRAME ) { 510break ; 511} 512* mem_size = offset[ 10 + i]; 513if ( * mem_size == 0 ) { 514continue ; 515} 516_transposeEventColumns (main_buf,s,mem_size,_debug ? dw_post : cw_post,false); 517s += * mem_size; 518} 519 520// Shuffle frame end columns 521if (main_buf[s] == Event:: BOOKEND ) { 522* mem_size = offset[ 18 ]; 523_transposeEventColumns (main_buf,s,mem_size,_debug ? dw_end : cw_end,false); 524s += * mem_size; 525} 526 527delete mem_size; 528 529return true; 530} 531 532inline bool _unshuffleColumns ( char * main_buf) { 533truncateColumnWidthsToVersion (); 534// We need to unshuffle event columns before doing anything else 535// Track the starting position of the buffer 536unsigned s = _game_loop_start; 537unsigned * mem_size = new unsigned[ 1 ]{ 0 }; 538 539// Unshuffle message columns 540if (main_buf[s] == Event:: SPLIT_MSG ) { 541_revertEventColumns (main_buf,s,mem_size,_debug ? dw_mesg : cw_mesg); 542s += * mem_size; 543} 544 545// Unshuffle frame start columns 546if (main_buf[s] == Event:: FRAME_START ) { 547_revertEventColumns (main_buf,s,mem_size,_debug ? dw_start : cw_start); 548s += * mem_size; 549} 550 551// Unshuffle pre frame columns 552for ( unsigned i = 0 ; i < 8 ; ++ i) { 553if (main_buf[s] != Event:: PRE_FRAME ) { 554break ; 555} 556_revertEventColumns (main_buf,s,mem_size,_debug ? dw_pre : cw_pre); 557s += * mem_size; 558} 559 560// Unshuffle item columns 561if (main_buf[s] == Event:: ITEM_UPDATE ) { 562_revertEventColumns (main_buf,s,mem_size,_debug ? dw_item : cw_item); 563s += * mem_size; 564} 565 566// Unshuffle post frame columns 567for ( unsigned i = 0 ; i < 8 ; ++ i) { 568if (main_buf[s] != Event:: POST_FRAME ) { 569break ; 570} 571_revertEventColumns (main_buf,s,mem_size,_debug ? dw_post : cw_post); 572s += * mem_size; 573} 574 575// Unshuffle frame end columns 576if (main_buf[s] == Event:: BOOKEND ) { 577_revertEventColumns (main_buf,s,mem_size,_debug ? dw_end : cw_end); 578s += * mem_size; 579} 580 581// Reset _game_loop_start 582delete mem_size; 583 584// All done! 585return true; 586} 587 588inline bool _transposeEventColumns ( char * mem_buf, unsigned mem_off, unsigned * mem_size, const int col_widths[], bool unshuffle = false) { 589bool shuffle = !unshuffle; //Convenience reference to whether we're shuffled 590char * mem_start = & mem_buf[mem_off]; //Convenience reference to proper memory offset 591uint8_t ev_code = mem_start[ 0 ]; //Always an event code regardless of shuffling 592 593// Compute the total size of all columns in the event struct 594unsigned struct_size = 0 ; 595unsigned col_offsets[ 1024 ] = { 0 }; 596for ( unsigned i = 0 ; col_widths[i] != 0 ; ++ i) { 597col_offsets[i] = struct_size; 598if (col_widths[i] > 0 ) { 599struct_size += col_widths[i]; 600} else { //Bit shuffling columns are always one byte 601struct_size += 1 ; 602} 603} 604 605// (Unshuffle only) Compute mem_size from consecutive event codes if necessary 606if (unshuffle) { 607// Ignore the existing mem_size, since it's invalid anyway 608* mem_size = 0 ; 609// While the current byte is still the event code, increment the memory size 610while (mem_start[ * mem_size] == ev_code) { 611++ ( * mem_size); 612} 613// Multiple the memory size by the size of the struct to get the true memory size 614* mem_size *= struct_size; 615} 616 617// Create a new buffer for storing all intermediate data 618char * buff = new char[ * mem_size] { 0 }; //Make sure it's initialized to zero 619 620// Use struct size to get the total number of entries in the event array 621unsigned num_entries = ( * mem_size) / struct_size; 622// std::cout << "Shuffling " << num_entries << " entries" << std::endl; 623 624// Transpose the columns! 625unsigned b = 0 ; 626for ( unsigned i = 0 ; col_widths[i] != 0 ; ++ i) { 627DOUT3 ( "SHUFFLE " << hex (ev_code) << " column " << i << " at " << mem_off + b); 628if (col_widths[i] > 0 ) { //Normal column shuffling 629for ( unsigned e = 0 ; e < num_entries; ++ e) { 630unsigned mempos = (e * struct_size + col_offsets[i]); 631memcpy ( 632& buff[ unshuffle ? mempos : b], 633& mem_start[unshuffle ? b : mempos], 634sizeof ( char ) * col_widths[i] 635); 636b += col_widths[i]; 637} 638} else { //If col_widths[i] < 0, then use bitwise column shuffling 639for ( int bit = 7 , ib = 7 ; ib >= 0 ; -- ib) { 640for ( unsigned e = 0 ; e < num_entries; ++ e) { 641unsigned mempos = (e * struct_size + col_offsets[i]); 642char r_byte = mem_start[shuffle ? mempos : b]; 643char r_bit = (r_byte >> (shuffle ? ib : bit)) & 0x01 ; 644char w_bit = r_bit << (shuffle ? bit : ib); 645buff[shuffle ? b : mempos] ^= w_bit; 646if (( -- bit) < 0 ) { 647bit = 7 ; 648b += 1 ; 649} 650} 651} 652} 653DOUT3 ( " to " << mem_off + b << std::endl); 654} 655 656// Copy back the shuffled columns 657memcpy ( & mem_start[ 0 ], & buff[ 0 ], * mem_size); 658 659// Clear the memory buffer 660delete buff; 661 662// All done! 663return true; 664} 665 666inline bool _revertEventColumns ( char * mem_buf, unsigned mem_off, unsigned * mem_size, const int col_widths[]) { 667return _transposeEventColumns (mem_buf,mem_off,mem_size,col_widths,true); 668} 669 670inline unsigned encodeFrameIntoItemId ( unsigned item_id, int32_t frame) const { 671return item_id ^ (((frame + 256 ) % 256 ) << 24 ); 672} 673 674inline unsigned getFrameModFromItemId ( unsigned item_id) const { 675return (item_id >> 24 ); 676} 677 678inline int32_t lookAheadToFinalizedFrame ( char * mem_start, int32_t ref_frame = 0 ) const { 679unsigned mem_off = 0 ; 680while (mem_start[mem_off] != Event:: BOOKEND ) { 681mem_off += _payload_sizes[ uint8_t (mem_start[mem_off])]; 682} 683return readBE4S ( & mem_start[mem_off + O_ROLLBACK_FRAME ]); 684} 685 686static inline void readDefaultGeckoCodes ( char ** buff) { 687static bool _codes_read = false; 688static char * _codes; 689if (! _codes_read) { 690_codes = new char[ CODE_SIZE ] { 0 }; 691std :: string fname = "/home/pretzel/workspace/slippc/data/gecko.dat" ; 692std :: fstream fin; 693fin. open (fname, std::ios::in | std::ios::binary); 694fin. read (_codes, CODE_SIZE * sizeof ( char )); 695fin. close (); 696_codes_read = true; 697} 698* buff = new char[ CODE_SIZE ]; 699memcpy ( * buff,_codes, CODE_SIZE ); 700} 701 702inline void truncateColumnWidthsToVersion () { 703if ( MAX_VERSION ( 3 , 5 , 0 )) { 704cw_post[ 27 ] = 0 ; //Self-induced Air x Speed and onward are invalid 705dw_post[ 27 ] = 0 ; 706} 707if ( MAX_VERSION ( 2 , 1 , 0 )) { 708cw_post[ 26 ] = 0 ; //Hurtbox Collision State and onward are invalid 709dw_post[ 26 ] = 0 ; 710} 711if ( MAX_VERSION ( 2 , 0 , 0 )) { 712cw_post[ 16 ] = 0 ; //State bit flags 1 and onward are invalid 713dw_post[ 16 ] = 0 ; 714} 715} 716 717}; 718 719} 720 721#endif /* COMPRESSOR_H_ */