fork/slippc

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

Patrick CrainAdded actions per minuet and action state changes per minute stats to analyzer; added total counts for stick movements and button pushes; added max GALINT stat30c55ad

0.6.1
10.8 KiB376 linesraw
1#ifndef UTIL_H_
2#define UTIL_H_
3
4#include <string.h>
5#include <iomanip>
6#include <iostream>
7#include <sstream>
8#include <fstream>
9#include <codecvt>
10#include <algorithm> //std::find
11#include <sys/stat.h> //std::find
12
13#include "lzma.h"
14
15#define BYTE8(b1,b2,b3,b4,b5,b6,b7,b8) (*((uint64_t*)(uint8_t[]){b1,b2,b3,b4,b5,b6,b7,b8}))
16#define BYTE4(b1,b2,b3,b4)             (*((uint32_t*)(uint8_t[]){b1,b2,b3,b4}))
17#define BYTE2(b1,b2)                   (*((uint16_t*)(uint8_t[]){b1,b2}))
18
19const uint64_t SLP_HEADER  = BYTE8(0x7b,0x55,0x03,0x72,0x61,0x77,0x5b,0x24); // {U.raw[$
20const uint32_t LZMA_HEADER = BYTE4(0xfd,0x37,0x7a,0x58);
21// const uint32_t LZMA_HEADER = BYTE4(0x37,0xfd,0x58,0x7a);
22
23
24const unsigned N_HEADER_BYTES      =  15; //Header is always 15 bytes
25const unsigned MIN_EV_PAYLOAD_SIZE =  14; //Payloads, game start, pre frame, post frame, game end always defined
26const unsigned MIN_GAME_START_SIZE = 353; //Minimum size for game start event (necessary for all replays)
27const unsigned MIN_REPLAY_LENGTH   = N_HEADER_BYTES + MIN_EV_PAYLOAD_SIZE + MIN_GAME_START_SIZE;
28
29// Version convenience macros
30#define MIN_VERSION(maj,min,rev) (_slippi_maj > (maj)) || (_slippi_maj == (maj) && ( (_slippi_min > (min)) || (_slippi_min == (min) && _slippi_rev >= (rev)) ))
31#define MAX_VERSION(maj,min,rev) (_slippi_maj < (maj)) || (_slippi_maj == (maj) && ( (_slippi_min < (min)) || (_slippi_min == (min) && _slippi_rev < (rev)) ))
32
33namespace slip {
34
35//Indent Level
36#define ILEV 1
37
38//Strings for various indentation amounts
39const std::string SPACE[10] = {
40  "",
41  " ",
42  "  ",
43  "   ",
44  "    ",
45  "     ",
46  "      ",
47  "       ",
48  "        ",
49  "         ",
50};
51
52static const std::string base64_chars =
53             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
54             "abcdefghijklmnopqrstuvwxyz"
55             "0123456789+/";
56
57
58static inline bool is_base64(unsigned char c) {
59  return (isalnum(c) || (c == '+') || (c == '/'));
60}
61
62//Hex Output stuff
63
64struct HexCharStruct {
65  unsigned char c;
66  HexCharStruct(unsigned char _c) : c(_c) { }
67};
68
69inline std::ostream& operator<<(std::ostream& o, const HexCharStruct& hs) {
70  return (o << "0x" << std::hex << (int)hs.c) << std::dec;
71}
72
73inline HexCharStruct hex(unsigned char _c) {
74  return HexCharStruct(_c);
75}
76
77inline void printBytes(char* buff, unsigned nchars) {
78  for(unsigned i = 0; i < nchars; ++i) {
79    std::cout << hex(buff[i]) << " ";
80  }
81  std::cout << std::dec << std::endl;
82}
83
84//Bitwise comparison of 8-bytes array with uint_64
85inline bool same8(char* array, uint64_t other) {
86  return ((*((uint64_t*)array)) ^ other) == 0;
87}
88//Bitwise comparison of 4-bytes array with uint_32
89inline bool same4(char* array, uint32_t other) {
90  return ((*((uint32_t*)array)) ^ other) == 0;
91}
92//Load a big-endian 32-bit unsigned int from an array
93inline uint32_t readBE4U(char* array) { return __builtin_bswap32(*((uint32_t*)array)); }
94//Load a big-endian 16-bit unsigned int from an array
95inline uint16_t readBE2U(char* array) { return __builtin_bswap16(*((uint16_t*)array)); }
96//Load a big-endian 32-bit int from an array
97inline int32_t  readBE4S(char* array) { return __builtin_bswap32(*((int32_t*)array)); }
98//Load a big-endian 16-bit int from an array
99inline int16_t  readBE2S(char* array) { return __builtin_bswap16(*((int16_t*)array)); }
100//Load a big-endian float from an array
101inline float readBE4F(char* array) {
102   float r;
103   char *rc = ( char* ) & r;
104   rc[0] = array[3];
105   rc[1] = array[2];
106   rc[2] = array[1];
107   rc[3] = array[0];
108   return r;
109}
110
111//Write a big-endian 32-bit unsigned int to an array
112inline void  writeBE4F(float f, char* a) {
113  char *wc = ( char* ) & f;
114  a[3] = wc[0];
115  a[2] = wc[1];
116  a[1] = wc[2];
117  a[0] = wc[3];
118}
119
120//Write a big-endian 16-bit unsigned int to an array
121inline void  writeBE2U(uint16_t i, char* a) {
122  a[1] =  i      & 0xff;
123  a[0] = (i>>8)  & 0xff;
124}
125
126//Write a big-endian 32-bit unsigned int to an array
127inline void  writeBE4U(uint32_t i, char* a) {
128  a[3] =  i      & 0xff;
129  a[2] = (i>>8)  & 0xff;
130  a[1] = (i>>16) & 0xff;
131  a[0] = (i>>24) & 0xff;
132}
133
134//Write a big-endian 32-bit signed int to an array
135inline void  writeBE4S(int32_t i, char* a) {
136  a[3] =  i      & 0xff;
137  a[2] = (i>>8)  & 0xff;
138  a[1] = (i>>16) & 0xff;
139  a[0] = (i>>24) & 0xff;
140}
141
142//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
143inline std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
144  std::string ret;
145  int i = 0;
146  int j = 0;
147  unsigned char char_array_3[3];
148  unsigned char char_array_4[4];
149
150  while (in_len--) {
151    char_array_3[i++] = *(bytes_to_encode++);
152    if (i == 3) {
153      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
154      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
155      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
156      char_array_4[3] = char_array_3[2] & 0x3f;
157
158      for(i = 0; (i <4) ; i++)
159        ret += base64_chars[char_array_4[i]];
160      i = 0;
161    }
162  }
163
164  if (i)
165  {
166    for(j = i; j < 3; j++)
167      char_array_3[j] = '\0';
168
169    char_array_4[0] = ( char_array_3[0] & 0xfc) >> 2;
170    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
171    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
172
173    for (j = 0; (j < i + 1); j++)
174      ret += base64_chars[char_array_4[j]];
175
176    while((i++ < 3))
177      ret += '=';
178
179  }
180
181  return ret;
182
183}
184
185//Base64 encoder / decoder -> https://renenyffenegger.ch/notes/development/Base64/Encoding-and-decoding-base-64-with-cpp
186inline std::string base64_decode(std::string const& encoded_string) {
187  int in_len = encoded_string.size();
188  int i = 0;
189  int j = 0;
190  int in_ = 0;
191  unsigned char char_array_4[4], char_array_3[3];
192  std::string ret;
193
194  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
195    char_array_4[i++] = encoded_string[in_]; in_++;
196    if (i ==4) {
197      for (i = 0; i <4; i++)
198        char_array_4[i] = base64_chars.find(char_array_4[i]);
199
200      char_array_3[0] = ( char_array_4[0] << 2       ) + ((char_array_4[1] & 0x30) >> 4);
201      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
202      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) +   char_array_4[3];
203
204      for (i = 0; (i < 3); i++)
205        ret += char_array_3[i];
206      i = 0;
207    }
208  }
209
210  if (i) {
211    for (j = 0; j < i; j++)
212      char_array_4[j] = base64_chars.find(char_array_4[j]);
213
214    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
215    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
216
217    for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
218  }
219
220  return ret;
221}
222
223// https://stackoverflow.com/questions/7724448/simple-json-string-escape-for-c/33799784#33799784
224inline std::string escape_json(const std::string &s) {
225    std::ostringstream o;
226    for (auto c = s.cbegin(); c != s.cend(); c++) {
227        if (*c == '"' || *c == '\\' || ('\x00' <= *c && *c <= '\x1f')) {
228            o << "\\u"
229              << std::hex << std::setw(4) << std::setfill('0') << (int)*c;
230        } else {
231            o << *c;
232        }
233    }
234    return o.str();
235}
236
237inline std::string to_utf8(const std::u16string &s) {
238  std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> conv;
239  std::string u = conv.to_bytes(s);
240  u.erase(std::find(u.begin(), u.end(), '\0'), u.end());
241  return u;
242}
243
244inline std::string floatToBinary(float f) {
245  union { float f; uint32_t i; } u;
246  u.f = f;
247  std::string s;
248  for (int i = 0; i < 32; i++) {
249    if (u.i % 2)  s.push_back('1');
250    else s.push_back('0');
251    u.i >>= 1;
252  }
253  // Reverse the string since now it's backwards
254  std::string temp(s.rbegin(), s.rend());
255  return temp;
256}
257
258// http://ptspts.blogspot.com/2011/11/how-to-simply-compress-c-string-with.html
259inline std::string compressWithLzma(const std::string& in, int level = 6) {
260  std::string result;
261  result.resize(in.size() + (in.size() >> 2) + 128);
262  size_t out_pos = 0;
263  if (LZMA_OK != lzma_easy_buffer_encode(
264      level, LZMA_CHECK_CRC32, NULL,
265      reinterpret_cast<uint8_t*>(const_cast<char*>(in.data())), in.size(),
266      reinterpret_cast<uint8_t*>(&result[0]), &out_pos, result.size()))
267    abort();
268  result.resize(out_pos);
269  return result;
270}
271
272inline std::string decompressWithLzma(const std::string& in) {
273  static const size_t kMemLimit = 1 << 30;  // 1 GB.
274  lzma_stream strm = LZMA_STREAM_INIT;
275  std::string result;
276  result.resize(8192);
277  size_t result_used = 0;
278  lzma_ret ret;
279  ret = lzma_stream_decoder(&strm, kMemLimit, LZMA_CONCATENATED);
280  if (ret != LZMA_OK)
281    abort();
282  size_t avail0 = result.size();
283  strm.next_in = reinterpret_cast<const uint8_t*>(in.data());
284  strm.avail_in = in.size();
285  strm.next_out = reinterpret_cast<uint8_t*>(&result[0]);
286  strm.avail_out = avail0;
287  while (true) {
288    ret = lzma_code(&strm, strm.avail_in == 0 ? LZMA_FINISH : LZMA_RUN);
289    if (ret == LZMA_STREAM_END) {
290      result_used += avail0 - strm.avail_out;
291      if (0 != strm.avail_in)  // Guaranteed by lzma_stream_decoder().
292        abort();
293      result.resize(result_used);
294      lzma_end(&strm);
295      return result;
296    }
297    if (ret != LZMA_OK)
298      abort();
299    if (strm.avail_out == 0) {
300      result_used += avail0 - strm.avail_out;
301      result.resize(result.size() << 1);
302      strm.next_out = reinterpret_cast<uint8_t*>(&result[0] + result_used);
303      strm.avail_out = avail0 = result.size() - result_used;
304    }
305  }
306}
307
308inline bool fileExists(std::string fname) {
309   std::ifstream i(fname.c_str());
310   return i.good();
311}
312
313// https://stackoverflow.com/questions/667183/padding-stl-strings-in-c
314inline std::string padString(std::string str, const size_t num, const char paddingChar = ' ') {
315    if(num > str.size())
316        str.insert(0, num - str.size(), paddingChar);
317    return str;
318}
319
320inline std::string padString(float f, const size_t num, const char paddingChar = ' ') {
321  return padString(std::to_string(f),num,paddingChar);
322}
323
324inline std::string padString(int i, const size_t num, const char paddingChar = ' ') {
325  return padString(std::to_string(i),num,paddingChar);
326}
327
328
329inline int diffBits(uint16_t u1, uint16_t u2) {
330  int count  = 0;
331  u1        ^= u2;
332  for (int i = 0; i < 16; i++) {
333    if ((u1 >> i) & 1) {
334      ++count;
335    }
336  }
337  return count;
338}
339
340inline int countBits(uint16_t u1) {
341  return diffBits(u1,0);
342}
343
344inline int diffBits(float f1, float f2) {
345  union { float f; uint32_t u; } n1, n2;
346  n1.f       = f1;
347  n2.f       = f2;
348  int count  = 0;
349  n1.u      ^= n2.u;
350  for (int i = 0; i < 32; i++) {
351    if ((n1.u >> i) & 1) {
352      ++count;
353    }
354  }
355  return count;
356}
357
358inline std::string getFileBase(std::string f) {
359  int extpos = f.find_last_of(".");
360  return f.substr(0,extpos);
361}
362
363
364inline std::string getFileExt(std::string f) {
365  int extpos = f.find_last_of(".");
366  return f.substr(extpos+1,f.size()-(extpos+1));
367}
368
369inline void stringtoChars(std::string s, char** c) {
370  *c = new char[s.size()+1];
371  strcpy(*c, s.c_str());
372}
373
374}
375
376#endif /* UTIL_H_ */