fork/slippc

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

Patrick CrainAdded portable-file-dialogs and tentative GUI solution for slippc output7abc388

0.7.0
28.5 KiB721 linesraw
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
63  int FRAME_ENC_DELTA = 0;
64
65  int             _debug;                         //Current debug level
66  uint16_t        _payload_sizes[256] = {0};      //Size of payload for each event
67  uint8_t         _slippi_maj         =  0;       //Major version number of replay being parsed
68  uint8_t         _slippi_min         =  0;       //Minor version number of replay being parsed
69  uint8_t         _slippi_rev         =  0;       //Revision number of replay being parsed
70  uint8_t         _is_encoded         =  0;       //Encryption status of replay being parsed
71  int32_t         _max_frames         =  0;       //Maximum number of frames that there will be in the replay file
72  std::string*    _outfilename        =  nullptr; //Name of the file to write
73
74  //Variables needed for mapping floats to ints and vice versa
75  std::map<unsigned,unsigned> float_to_int;  //Map of floats to ints
76  std::map<unsigned,unsigned> int_to_float;  //Reverse map of ints back to floats
77  std::map<unsigned,unsigned> int_to_float_c;//Counter for reverse map of ints back to floats
78  unsigned            num_floats = 0;        //Total number of floats in float map
79
80  uint32_t        _preds = 0;
81  uint32_t        _fails = 0;
82
83  uint32_t        _rng;                                //Current RNG seed we're working with
84  uint32_t        _rng_start;                          //Starting RNG seed
85  char            _x_pre_frame[8][256]       = {0};    //Delta for pre-frames
86  char            _x_pre_frame_2[8][256]     = {0};    //Delta for 2 pre-frames ago
87  char            _x_post_frame[8][256]      = {0};    //Delta for post-frames
88  char            _x_post_frame_2[8][256]    = {0};    //Delta for 2 post-frames ago
89  char            _x_post_frame_3[8][256]    = {0};    //Delta for 3 post-frames ago
90  char            _x_item[ITEM_SLOTS][256]   = {0};    //Delta for item updates
91  char            _x_item_2[ITEM_SLOTS][256] = {0};    //Delta for item updates 2 frames ago
92  char            _x_item_3[ITEM_SLOTS][256] = {0};    //Delta for item updates 3 frames ago
93  char            _x_item_p[ITEM_SLOTS][256] = {0};    //Delta for item position updates
94  int32_t         laststartframe             = -123;   //Last frame used in frame start event, encoding
95  int32_t         lastitemstartframe         = -123;   //Last frame used in item event, encoding
96  int32_t         lastshuffleframe           = -123;   //Last frame used in frame start event, shuffling
97  int32_t         lastitemshuffleframe       = -123;   //Last frame used in item event, shuffling
98  int32_t         lastpreframe[8]            = {-123}; //Last frame used in pre frame event, encoding
99  int32_t         lastshufflepreframe[8]     = {-123}; //Last frame used in pre frame event, shuffling
100  int32_t         lastpostframe[8]           = {-123}; //Last frame used in post frame event, encoding
101  int32_t         lastshufflepostframe[8]    = {-123}; //Last frame used in post frame event, shuffling
102
103  char*           _rb = nullptr;        //Read buffer
104  char*           _wb = nullptr;        //Write buffer
105  unsigned        _bp;                  //Current position in buffer
106  uint32_t        _length_raw;          //Remaining length of raw payload
107  uint32_t        _length_raw_start;    //Total length of raw payload
108  uint32_t        _game_loop_start = 0; //First byte AFTER game start event
109  uint32_t        _game_loop_end = 0;   //First byte OF game end event
110  uint32_t        _file_size;           //Total size of the replay file on disk
111  bool            _parse();             //Internal main parsing funnction
112  bool            _parseHeader();
113  bool            _parseEventDescriptions();
114  bool            _parseEvents();
115  bool            _parseGameStart();
116  bool            _parseGeckoCodes();
117  bool            _parsePreFrame();
118  bool            _parsePostFrame();
119  bool            _parseFrameStart();
120  bool            _parseItemUpdate();
121  bool            _parseBookend();
122  bool            _shuffleEvents(bool unshuffle = false);
123  bool            _unshuffleEvents();
124public:
125  Compressor(int debug_level);                     //Instantiate the parser (possibly in debug mode)
126  ~Compressor();                                   //Destroy the parser
127  bool loadFromFile(const char* replayfilename);   //Load a replay file
128  void saveToFile(bool rawencode);              //Save an encoded replay file
129  bool setOutputFilename(const char* fname);       //Set output file name
130  bool loadFromBuff(char** buffer, unsigned size); //Load a replay from a buffer
131  unsigned saveToBuff(char** buffer);              //Save an encoded replay buffer
132  bool validate();                                 //Validate the encoding
133
134  //https://www.reddit.com/r/SSBM/comments/71gn1d/the_basics_of_rng_in_melee/
135  inline int32_t rollRNGLegacy(int32_t seed) const {
136    int64_t bigseed = seed;  //Cast from 32-bit to 64-bit int
137    return ((bigseed * 214013) + 2531011) % 4294967296;
138  }
139
140  //Given the current frame's RNG value, just add 65536 to get the next frame's
141  inline int32_t rollRNGRollback(int32_t seed) const {
142    return (seed + 65536) % 4294967296;
143  }
144
145  //Per Fizzi and Nikki, RNG is just the starting RNG + 65536*<0-indexed frame>
146  inline int32_t computeRNGRollback(int32_t framenum) const {
147    return (((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
151  inline bool checkRawRNG(int32_t &frame) const {
152    //If first and second bits of frame are different, rng is stored raw
153    uint8_t bit1 = (frame >> 31) & 0x01;
154    uint8_t bit2 = (frame >> 30) & 0x01;
155    uint8_t rng_is_raw = bit1 ^ bit2;
156    return rng_is_raw > 0;
157  }
158
159  //Predict the next frame given a reference frame and delta encode the difference
160  inline int32_t predictFrame(int32_t frame, int32_t ref_frame, char* write_addr = nullptr) const {
161    //Remove RNG bit from frame
162    bool rng_is_raw  = checkRawRNG(frame);
163    if (rng_is_raw) {
164      frame ^= RAW_RNG_MASK;
165    }
166    //Flip 2nd bit in _wb output if raw RNG flag is set
167    uint32_t bitmask = rng_is_raw ? RAW_RNG_MASK : 0x00000000;
168
169    int32_t frame_delta_pred;
170    if (_is_encoded) { //Decode
171      frame_delta_pred = frame + (ref_frame + FRAME_ENC_DELTA);
172      if (write_addr) {
173        writeBE4S(frame_delta_pred ^ bitmask,write_addr);
174      }
175    } else {           //Encode
176      frame_delta_pred = frame - (ref_frame + FRAME_ENC_DELTA);
177      if (write_addr) {
178        writeBE4S(frame_delta_pred,write_addr);
179      }
180    }
181    return frame_delta_pred;
182  }
183
184  //Version of predictFrame used for _unshuffleEvents
185  inline int32_t decodeFrame(int32_t frame, int32_t ref_frame) const {
186    return 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
191  inline void buildFloatMap(unsigned off) {
192    uint32_t enc_float = readBE4U(&_rb[_bp+off]);
193    if (_is_encoded) {                  //Decode
194      if ((enc_float & MAGIC_FLOAT) != MAGIC_FLOAT) {
195        //If it's our first encounter with a float, add it to our maps
196        float_to_int[enc_float]  = num_floats;
197        int_to_float[num_floats] = enc_float;
198        ++num_floats;
199      } else {
200        //Decode our int back to a float
201        unsigned as_unsigned = int_to_float[enc_float & (MAGIC_FLOAT ^ 0xFFFFFFFF)];
202        writeBE4U(as_unsigned, &_wb[_bp+off]);
203      }
204    } else {                            //Encode
205      if (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
207        float_to_int[enc_float]    = num_floats;
208        int_to_float[num_floats]   = enc_float;
209        int_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]];
214        writeBE4U(float_to_int[enc_float] | MAGIC_FLOAT, &_wb[_bp+off]);
215      }
216    }
217  }
218
219  //Compress analog float values by converting them to ints
220  inline void encodeAnalog(unsigned off, float mult) {
221    union { 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
224    char* main_buf = _is_encoded ? _wb : _rb;
225    float_true.u   = readBE4U(&main_buf[_bp+off]);
226    if ((float_true.u & 0x7f800000) && _is_encoded) {
227      return;  //If we have a float in the encoded state, nothing to do
228    }
229
230    float_pred.u = 0;  //Initialize all bytes to zero
231    if (_is_encoded) {
232      if (mult > 127) {
233        float_pred.f = float(float_true.c[0])/mult;
234      } else {
235        float_pred.f = float(float_true.i[0])/mult;
236      }
237      writeBE4F(float_pred.f,&_wb[_bp+off]);  //Write our decoded float
238    } else {
239      if (mult > 127) {
240        float_pred.c[0] = uint8_t(round(float(float_true.f*mult)));
241        float_rest.f = float(float_pred.c[0])/mult;
242      } else {
243        float_pred.i[0] = int8_t(round(float(float_true.f*mult)));
244        float_rest.f = float(float_pred.i[0])/mult;
245      }
246      if (float_rest.u == float_true.u) {  //Verify we can properly decode
247        writeBE4U(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)
256  inline void predictAsDifference(unsigned off, unsigned buffoff, char* buff) {
257    union { 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
259    uint32_t MAXDIFF = 0x03FF;
260
261    char* main_buf = _is_encoded ? _wb : _rb;
262
263    float_true.f = readBE4F(&main_buf[_bp+off]);
264    float_pred.f = readBE4F(&main_buf[_bp+buffoff]) - readBE4F(&buff[buffoff]);
265    float_temp.u = float_pred.u ^ float_true.u;
266
267    if (_is_encoded) {
268      uint32_t diff = float_true.u ^ MAGIC_FLOAT;
269      if (diff <= MAXDIFF) {  //If our prediction was accurate to at least the last few bits
270        float_pred.u ^= diff;
271        writeBE4F(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
275      if (float_temp.u <= MAXDIFF) {
276        writeBE4U(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
282    memcpy(&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
288  inline void predictAccel(unsigned p, unsigned off, char* buff1, char* buff2, char* buff3) {
289    union { 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
291    const uint32_t MAXDIFF = 0xFF;
292
293    float_true.f = readBE4F(&_rb[_bp+off]);
294    float pos1   = readBE4F(&buff1[off]);
295    float pos2   = readBE4F(&buff2[off]);
296    float pos3   = readBE4F(&buff3[off]);
297    float vel1   = pos1 - pos2;
298    float vel2   = pos2 - pos3;
299    float acc1   = vel1 - vel2;
300    float_pred.f = pos1 + vel1 + acc1;
301    float_temp.u = float_pred.u ^ float_true.u;
302
303    if (_is_encoded) {
304      uint32_t diff = float_true.u ^ MAGIC_FLOAT;
305      if (diff <= MAXDIFF) {  //If our prediction was accurate to at least the last few bits
306        float_pred.u ^= diff;
307        writeBE4F(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
311      if (float_temp.u <= MAXDIFF) {
312        writeBE4U(MAGIC_FLOAT ^ float_temp.u,&_wb[_bp+off]);  //Write an impossible float
313      }
314    }
315
316    memcpy(&buff3[off], &buff2[off],4);
317    memcpy(&buff2[off], &buff1[off],4);
318    memcpy(&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
324  inline void predictVeloc(unsigned p, unsigned off, char* buff1, char* buff2) {
325    union { float f; uint32_t u; } float_true, float_pred, float_temp;
326
327    float_true.f = readBE4F(&_rb[_bp+off]);
328    float_pred.f = 2*readBE4F(&buff1[off]) - readBE4F(&buff2[off]);
329    float_temp.u = float_pred.u ^ float_true.u;
330    if (_is_encoded) {
331      if (float_true.u == MAGIC_FLOAT) {  //If our prediction was exactly accurate
332        writeBE4F(float_pred.f,&_wb[_bp+off]);  //Write our predicted float
333      }
334    } else {
335      if (float_temp.u == 0) {  //If our prediction was exactly accurate
336        writeBE4U(MAGIC_FLOAT,&_wb[_bp+off]);  //Write an impossible float
337      }
338    }
339
340    memcpy(&buff2[off], &buff1[off],4);
341    memcpy(&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
346  inline void predictAccelPost(unsigned p, unsigned off) {
347    predictAccel(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
352  inline void predictAccelItem(unsigned p, unsigned off) {
353    predictAccel(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
358  inline void predictVelocPre(unsigned p, unsigned off) {
359    predictVeloc(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
364  inline void predictVelocPost(unsigned p, unsigned off) {
365    predictVeloc(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
370  inline void predictVelocItem(unsigned p, unsigned off) {
371    predictVeloc(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
376  inline void predictRNG(unsigned frameoff, unsigned rngoff) {
377    // return;
378    const uint32_t MAX_ROLLS = 128;
379
380    //Read true frame from write buffer
381    int32_t frame;
382    if (_is_encoded) {  //Read from write buffer
383      frame = readBE4S(&_wb[_bp+frameoff]);
384    } else {            //Read from read buffer
385      frame = readBE4S(&_rb[_bp+frameoff]);
386    }
387    bool rng_is_raw = checkRawRNG(frame);
388    if (rng_is_raw) {
389      frame ^= RAW_RNG_MASK;
390    }
391
392    //Get RNG seed and record number of rolls it takes to get to that point
393    if (MIN_VERSION(3,7,0)) {  //New RNG
394      _rng = computeRNGRollback(frame);
395      if (_is_encoded) {  //Decode
396        if (rng_is_raw == 0) { //Roll RNG a few times until we get to the desired value
397          unsigned rolls = readBE4U(&_rb[_bp+rngoff]);
398          if (rolls < MAX_ROLLS) {
399            for(unsigned i = 0; i < rolls; ++i) {
400              _rng = rollRNGRollback(_rng);
401            }
402          } else {  //Fallback on old RNG rolling method
403            for(unsigned i = MAX_ROLLS; i < rolls; ++i) {
404              _rng = rollRNGLegacy(_rng);
405            }
406          }
407          writeBE4U(_rng,&_wb[_bp+rngoff]);
408        } else {
409          writeBE4U(frame,&_wb[_bp+frameoff]);
410        }
411      } else {  //Encode
412        unsigned rolls  = 0;
413        unsigned target = readBE4U(&_rb[_bp+rngoff]);
414        for(uint32_t start_rng = _rng; (start_rng != target) && (rolls < MAX_ROLLS); ++rolls) {
415          start_rng = rollRNGRollback(start_rng);
416        }
417        if (rolls < MAX_ROLLS) {  //If we can encode RNG as < 256 rolls, do it
418          writeBE4U(rolls,&_wb[_bp+rngoff]);
419          _rng = target;
420        }
421        else {
422          rolls  = 0;
423          for(uint32_t start_rng = _rng; (start_rng != target) && (rolls < MAX_ROLLS); ++rolls) {
424            start_rng = rollRNGLegacy(start_rng);
425          }
426          if (rolls < MAX_ROLLS) {
427            writeBE4U(rolls+MAX_ROLLS,&_wb[_bp+rngoff]);
428          } else { //Store the raw RNG value
429            //Load the predicted frame value
430            int32_t predicted_frame = readBE4S(&_wb[_bp+frameoff]);
431            //Flip second bit of cached frame number to signal raw rng
432            writeBE4S(predicted_frame ^ RAW_RNG_MASK,&_wb[_bp+frameoff]);
433          }
434        }
435      }
436    } else { //Old RNG
437      if (_is_encoded) {
438        unsigned rolls = readBE4U(&_rb[_bp+rngoff]);
439        for(unsigned i = 0; i < rolls; ++i) {
440          _rng = rollRNGLegacy(_rng);
441        }
442        writeBE4U(_rng,&_wb[_bp+rngoff]);
443      } else { //Roll RNG until we hit the target, write the # of rolls
444        unsigned rolls, seed = readBE4U(&_rb[_bp+rngoff]);
445        for(rolls = 0;_rng != seed; ++rolls) {
446          _rng = rollRNGLegacy(_rng);
447        }
448        writeBE4U(rolls,&_wb[_bp+rngoff]);
449      }
450    }
451  }
452
453  //XOR bytes in the current read buffer with bytes from another buffer
454  inline void xorEncodeRange(unsigned start, unsigned stop, char* buffer) {
455    for(unsigned i = start; i < stop; ++i) {
456      _wb[_bp+i] = _rb[_bp+i] ^ buffer[i];
457      buffer[i]  = _is_encoded ? _wb[_bp+i] : _rb[_bp+i];
458    }
459  }
460
461  inline bool _shuffleColumns(unsigned *offset) {
462      truncateColumnWidthsToVersion();
463      char* main_buf = _wb;
464
465      // Track the starting position of the buffer
466      unsigned s = _game_loop_start;
467      unsigned *mem_size = new unsigned[1];
468
469      // Shuffle message columns
470      if (main_buf[s] == Event::SPLIT_MSG) {
471        *mem_size = offset[19];
472        _transposeEventColumns(main_buf,s,mem_size,_debug ? dw_mesg : cw_mesg,false);
473        s += *mem_size;
474      }
475
476      // Shuffle frame start columns
477      if (main_buf[s] == Event::FRAME_START) {
478        *mem_size = offset[0];
479        _transposeEventColumns(main_buf,s,mem_size,_debug ? dw_start : cw_start,false);
480        s += *mem_size;
481      }
482
483      // Shuffle pre frame columns
484      for(unsigned p = 0; p < 8; ++p) {
485          // True player order: 0,4,1,5,2,6,3,7
486          unsigned i = (4*(p%2)) + unsigned(p/2);
487          if (main_buf[s] != Event::PRE_FRAME) {
488              break;
489          }
490          *mem_size = offset[1+i];
491          if (*mem_size == 0) {
492            continue;
493          }
494          _transposeEventColumns(main_buf,s,mem_size,_debug ? dw_pre : cw_pre,false);
495          s += *mem_size;
496      }
497
498      // Shuffle item columns
499      if (main_buf[s] == Event::ITEM_UPDATE) {
500        *mem_size = offset[9];
501        _transposeEventColumns(main_buf,s,mem_size,_debug ? dw_item : cw_item,false);
502        s += *mem_size;
503      }
504
505      // Shuffle post frame columns
506      for(unsigned p = 0; p < 8; ++p) {
507          // True player order: 0,4,1,5,2,6,3,7
508          unsigned i = (4*(p%2)) + unsigned(p/2);
509          if (main_buf[s] != Event::POST_FRAME) {
510              break;
511          }
512          *mem_size = offset[10+i];
513          if (*mem_size == 0) {
514            continue;
515          }
516          _transposeEventColumns(main_buf,s,mem_size,_debug ? dw_post : cw_post,false);
517          s += *mem_size;
518      }
519
520      // Shuffle frame end columns
521      if (main_buf[s] == Event::BOOKEND) {
522        *mem_size = offset[18];
523        _transposeEventColumns(main_buf,s,mem_size,_debug ? dw_end : cw_end,false);
524        s += *mem_size;
525      }
526
527      delete mem_size;
528
529      return true;
530  }
531
532  inline bool _unshuffleColumns(char* main_buf) {
533      truncateColumnWidthsToVersion();
534      // We need to unshuffle event columns before doing anything else
535      // Track the starting position of the buffer
536      unsigned s         = _game_loop_start;
537      unsigned *mem_size = new unsigned[1]{0};
538
539      // Unshuffle message columns
540      if (main_buf[s] == Event::SPLIT_MSG) {
541        _revertEventColumns(main_buf,s,mem_size,_debug ? dw_mesg : cw_mesg);
542        s += *mem_size;
543      }
544
545      // Unshuffle frame start columns
546      if (main_buf[s] == Event::FRAME_START) {
547        _revertEventColumns(main_buf,s,mem_size,_debug ? dw_start : cw_start);
548        s += *mem_size;
549      }
550
551      // Unshuffle pre frame columns
552      for(unsigned i = 0; i < 8; ++i) {
553          if (main_buf[s] != Event::PRE_FRAME) {
554              break;
555          }
556          _revertEventColumns(main_buf,s,mem_size,_debug ? dw_pre : cw_pre);
557          s += *mem_size;
558      }
559
560      // Unshuffle item columns
561      if (main_buf[s] == Event::ITEM_UPDATE) {
562        _revertEventColumns(main_buf,s,mem_size,_debug ? dw_item : cw_item);
563        s += *mem_size;
564      }
565
566      // Unshuffle post frame columns
567      for(unsigned i = 0; i < 8; ++i) {
568          if (main_buf[s] != Event::POST_FRAME) {
569              break;
570          }
571          _revertEventColumns(main_buf,s,mem_size,_debug ? dw_post : cw_post);
572          s += *mem_size;
573      }
574
575      // Unshuffle frame end columns
576      if (main_buf[s] == Event::BOOKEND) {
577        _revertEventColumns(main_buf,s,mem_size,_debug ? dw_end : cw_end);
578        s += *mem_size;
579      }
580
581      // Reset _game_loop_start
582      delete mem_size;
583
584      // All done!
585      return true;
586  }
587
588  inline bool _transposeEventColumns(char* mem_buf, unsigned mem_off, unsigned* mem_size, const int col_widths[], bool unshuffle=false) {
589    bool shuffle    = !unshuffle;        //Convenience reference to whether we're shuffled
590    char* mem_start = &mem_buf[mem_off]; //Convenience reference to proper memory offset
591    uint8_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
594    unsigned struct_size = 0;
595    unsigned col_offsets[1024] = {0};
596    for(unsigned i = 0; col_widths[i] != 0; ++i) {
597        col_offsets[i] = struct_size;
598        if (col_widths[i] > 0) {
599          struct_size += col_widths[i];
600        } else {  //Bit shuffling columns are always one byte
601          struct_size += 1;
602        }
603    }
604
605    // (Unshuffle only) Compute mem_size from consecutive event codes if necessary
606    if (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
610      while(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
618    char* 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
621    unsigned num_entries = (*mem_size) / struct_size;
622    // std::cout << "Shuffling " << num_entries << " entries" << std::endl;
623
624    // Transpose the columns!
625    unsigned b = 0;
626    for(unsigned i = 0; col_widths[i] != 0; ++i) {
627      DOUT3("SHUFFLE " << hex(ev_code) << " column " << i << " at " << mem_off+b);
628      if (col_widths[i] > 0) {  //Normal column shuffling
629        for (unsigned e = 0; e < num_entries; ++e) {
630          unsigned mempos = (e*struct_size+col_offsets[i]);
631          memcpy(
632              &buff[     unshuffle ? mempos : b],
633              &mem_start[unshuffle ? b : mempos],
634              sizeof(char)*col_widths[i]
635              );
636          b += col_widths[i];
637        }
638      } else {  //If col_widths[i] < 0, then use bitwise column shuffling
639        for (int bit = 7, ib = 7; ib >= 0; --ib) {
640          for (unsigned e = 0; e < num_entries; ++e) {
641            unsigned mempos = (e*struct_size+col_offsets[i]);
642            char r_byte     = mem_start[shuffle ? mempos : b];
643            char r_bit      = (r_byte >> (shuffle ? ib : bit)) & 0x01;
644            char w_bit      = r_bit << (shuffle ? bit : ib);
645            buff[shuffle ? b : mempos] ^= w_bit;
646            if ((--bit) < 0) {
647              bit   = 7;
648              b    += 1;
649            }
650          }
651        }
652      }
653      DOUT3(" to " << mem_off+b << std::endl);
654    }
655
656    // Copy back the shuffled columns
657    memcpy(&mem_start[0], &buff[0], *mem_size);
658
659    // Clear the memory buffer
660    delete buff;
661
662    // All done!
663    return true;
664  }
665
666  inline bool _revertEventColumns(char* mem_buf, unsigned mem_off, unsigned* mem_size, const int col_widths[]) {
667    return _transposeEventColumns(mem_buf,mem_off,mem_size,col_widths,true);
668  }
669
670  inline unsigned encodeFrameIntoItemId(unsigned item_id, int32_t frame) const {
671    return item_id ^ (((frame+256) % 256) << 24);
672  }
673
674  inline unsigned getFrameModFromItemId(unsigned item_id) const {
675    return (item_id >> 24);
676  }
677
678  inline int32_t lookAheadToFinalizedFrame(char* mem_start, int32_t ref_frame=0) const {
679    unsigned mem_off = 0;
680    while(mem_start[mem_off] != Event::BOOKEND) {
681      mem_off += _payload_sizes[uint8_t(mem_start[mem_off])];
682    }
683    return readBE4S(&mem_start[mem_off+O_ROLLBACK_FRAME]);
684  }
685
686  static inline void readDefaultGeckoCodes(char** buff) {
687    static bool  _codes_read = false;
688    static char* _codes;
689    if (! _codes_read) {
690      _codes = new char[CODE_SIZE] {0};
691      std::string fname = "/home/pretzel/workspace/slippc/data/gecko.dat";
692      std::fstream fin;
693      fin.open(fname, std::ios::in | std::ios::binary);
694      fin.read(_codes, CODE_SIZE * sizeof(char));
695      fin.close();
696      _codes_read = true;
697    }
698    *buff = new char[CODE_SIZE];
699    memcpy(*buff,_codes,CODE_SIZE);
700  }
701
702  inline void truncateColumnWidthsToVersion() {
703    if (MAX_VERSION(3,5,0)) {
704      cw_post[27] = 0; //Self-induced Air x Speed and onward are invalid
705      dw_post[27] = 0;
706    }
707    if (MAX_VERSION(2,1,0)) {
708      cw_post[26] = 0; //Hurtbox Collision State and onward are invalid
709      dw_post[26] = 0;
710    }
711    if (MAX_VERSION(2,0,0)) {
712      cw_post[16] = 0; //State bit flags 1 and onward are invalid
713      dw_post[16] = 0;
714    }
715  }
716
717};
718
719}
720
721#endif /* COMPRESSOR_H_ */