fork/slippc

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

Patrick Crainadded proper dynamic array resizing to handle event shuffling in very large replaysedc0808

master
10.9 KiB361 linesraw
1#include <algorithm>
2#include <sys/stat.h>
3#include <filesystem>
4
5#include "util.h"
6#include "parser.h"
7#include "analyzer.h"
8#include "compressor.h"
9
10// #define GUI_ENABLED 1  //debug, normally enable this from the makefile
11
12#if GUI_ENABLED == 1
13  #include "portable-file-dialogs.h"
14#endif
15
16typedef std::filesystem::directory_iterator            f_iter;
17typedef std::filesystem::directory_entry               f_entry;
18typedef std::vector<std::__cxx11::basic_string<char> > str_vec;
19
20namespace slip {
21
22int _debug = 0;  //used to conform to macro in Util.h
23
24
25// https://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c
26char* getCmdOption(char ** begin, char ** end, const std::string & option) {
27  char ** itr = std::find(begin, end, option);
28  if (itr != end && ++itr != end) {
29    return *itr;
30  }
31  return 0;
32}
33
34bool cmdOptionExists(char** begin, char** end, const std::string& option) {
35  return std::find(begin, end, option) != end;
36}
37
38#if GUI_ENABLED == 1
39  bool askYesNo(std::string title, std::string question) {
40    return pfd::message(title, question, pfd::choice::yes_no, pfd::icon::question).result()==pfd::button::yes;
41  }
42#endif
43
44void printUsage() {
45  std::cout
46    << "Usage: slippc -i <infile> [-x | -X <zlpfle>] [-j <jsonfile>] [-a <analysisfile>] [-f] [-d <debuglevel>] [-h]:" << std::endl
47    << "  -i        Set input file (can be .slp, .zlp, or a whole directory)" << std::endl
48    << "  -j        Output <infile> in .json format to <jsonfile> (use \"-\" for stdout)" << std::endl
49    << "  -a        Output an analysis of <infile> in .json format to <analysisfile> (use \"-\" for stdout)" << std::endl
50    << "  -f        When used with -j <jsonfile>, write full frame info (instead of just frame deltas)" << std::endl
51    << "  -x        Compress or decompress a replay" << std::endl
52    << "  -X        Set output file name for compression" << std::endl
53    << std::endl
54    << "Debug options:" << std::endl
55    << "  -d           Run at debug level <debuglevel> (show debug output)" << std::endl
56    << "  --skip-save  Skip saving compressed replay, validate only" << std::endl
57    << "  --raw-enc    Output raw encodes with -x (DANGEROUS, debug only)" << std::endl
58    << "  --dump-gecko Dump gecko codes to <inputfilename>.dat" << std::endl
59    << "  -h           Show this help message" << std::endl
60    ;
61}
62
63typedef struct _cmdoptions {
64  char* dlevel       = nullptr;
65  char* infile       = nullptr;
66  char* cfile        = nullptr;
67  char* outfile      = nullptr;
68  char* analysisfile = nullptr;
69  bool  nodelta      = false;
70  bool  encode       = false;
71  bool  rawencode    = false;
72  bool  skipsave     = false;
73  bool  dumpgecko    = false;
74  bool  dirmode      = false;
75  int   debug        = 0;
76} cmdoptions;
77
78cmdoptions getCommandLineOptions(int argc, char** argv) {
79  _cmdoptions c;
80  c.dlevel       = getCmdOption(   argv, argv+argc, "-d");
81  c.infile       = getCmdOption(   argv, argv+argc, "-i");
82  c.cfile        = getCmdOption(   argv, argv+argc, "-X");
83  c.outfile      = getCmdOption(   argv, argv+argc, "-j");
84  c.analysisfile = getCmdOption(   argv, argv+argc, "-a");
85  c.nodelta      = cmdOptionExists(argv, argv+argc, "-f");
86  c.encode       = cmdOptionExists(argv, argv+argc, "-x");
87  c.rawencode    = cmdOptionExists(argv, argv+argc, "--raw-enc");
88  c.skipsave     = cmdOptionExists(argv, argv+argc, "--skip-save");
89  c.dumpgecko    = cmdOptionExists(argv, argv+argc, "--dump-gecko");
90  c.dirmode      = isDirectory(c.infile);
91
92  if (c.dlevel) {
93    if (c.dlevel[0] >= '0' && c.dlevel[0] <= '9') {
94      c.debug = c.dlevel[0]-'0';
95      _debug  = c.debug;
96    } else {
97      c.debug = 1;
98      _debug  = c.debug;
99      std::cerr << "Warning: invalid debug level" << std::endl;
100    }
101  }
102
103  if (c.debug) {
104    DOUT1("Running at debug level " << +c.debug);
105  }
106
107  return c;
108}
109
110#if GUI_ENABLED == 1
111  void getGUIOptions(cmdoptions &c) {
112    if (_debug < 1) {
113      _debug = 1;  //force some debug output
114    }
115    std::string save, saveext;
116    str_vec file_res = pfd::open_file(
117      "Select an input File", ".", {"Slippi Files", "*.slp *.zlp"}).result();
118    if(file_res.empty()) {
119      printUsage();
120      return "";
121    }
122    stringtoChars(file_res[0],&c.infile);
123
124    std::string inbase = getFileBase(file_res[0]);
125    std::string inext  = getFileExt(file_res[0]);
126
127    if (inext.compare("slp") == 0) {
128      if (askYesNo("Compress?","Output compressed file (yes) or JSON (no)?")) {
129        DOUT1("GUI mode, compressed output");
130        save = pfd::save_file("Select an Output file", inbase+".zlp", { "Zlippi Files", "*.zlp"}).result();
131        stringtoChars(save,&c.cfile);
132      } else if (askYesNo("Analysis?", "Output analysis JSON (yes) or regular JSON (no)?")) {
133        DOUT1("GUI mode, analysis output");
134        save = pfd::save_file("Select an Output file", inbase+".json", { "JSON Files", "*.json"}).result();
135        stringtoChars(save,&c.analysisfile);
136      } else {
137        DOUT1("GUI mode, JSON output");
138        save = pfd::save_file("Select an Output file", inbase+".json", { "JSON Files", "*.json"}).result();
139        stringtoChars(save,&c.outfile);
140      }
141    } else if (inext.compare("zlp") == 0) {
142      DOUT1("GUI mode, decompressed output");
143      save = pfd::save_file("Select an Output file", inbase+".slp", { "Slippi Files", "*.slp"}).result();
144      stringtoChars(save,&c.cfile);
145    }
146  }
147#endif
148
149inline void copyCommandOptions(const cmdoptions &c, cmdoptions &c2) {
150  memcpy(&c2,&c,sizeof(cmdoptions));
151}
152
153inline void cleanupCommandOptions(cmdoptions &c) {
154  if(c.infile) {
155    delete[] c.infile;
156  }
157  if(c.cfile) {
158    delete[] c.cfile;
159  }
160  if(c.outfile) {
161    delete[] c.outfile;
162  }
163  if(c.analysisfile) {
164    delete[] c.analysisfile;
165  }
166}
167
168int handleCompression(const cmdoptions &c, const int debug) {
169  slip::Compressor cmp(debug);
170
171  if (c.cfile) {
172    if (!(cmp.setOutputFilename(c.cfile))) {
173      FAIL("  File " << c.cfile << " already exists or is invalid");
174      return 4;
175    }
176  }
177
178  if (c.dumpgecko) {
179    DOUT1("  Setting gecko output filename");
180    cmp.setGeckoOutputFilename(c.infile);
181  }
182
183  DOUT1("  Encoding / decoding replay");
184  if (not cmp.loadFromFile(c.infile)) {
185    FAIL("  Failed to encode input; exiting");
186    return 2;
187  }
188
189  DOUT1("  Validating encoding");
190  if (!cmp.validate()) {
191    FAIL("  Validation failed; exiting");
192    return 3;
193  }
194
195  if (c.skipsave) {
196    DOUT1("  Skipping saving");
197  } else {
198    DOUT1("  Saving encoded / decoded replay");
199    cmp.saveToFile(c.rawencode);
200  }
201
202  return 0;
203}
204
205int handleAnalysis(const cmdoptions &c, const int debug, slip::Parser &p) {
206  DOUT1(" Analyzing");
207  slip::Analysis *a  = p.analyze();
208
209  if (a->success) {
210    if (c.analysisfile[0] == '-' && c.analysisfile[1] == '\0') {
211      if (debug) {
212        DOUT1("  Writing analysis to stdout");
213      }
214      std::cout << a->asJson() << std::endl;
215    } else {
216      if (debug) {
217        DOUT1("  Saving analysis to file");
218      }
219      a->save(c.analysisfile);
220    }
221  }
222
223  delete a;
224  return 0;
225}
226
227int handleJson(const cmdoptions &c, const int debug, slip::Parser &p) {
228  DOUT1(" Writing JSON");
229  if (c.outfile[0] == '-' && c.outfile[1] == '\0') {
230    if (debug) {
231      DOUT1("  Writing Slippi JSON data to stdout");
232    }
233    std::cout << p.asJson(!c.nodelta) << std::endl;
234  } else {
235    if (debug) {
236      DOUT1("  Saving Slippi JSON data to file");
237    }
238    p.save(c.outfile,!c.nodelta);
239  }
240  return 0;
241}
242
243int handleSingleFile(const cmdoptions &c, const int debug) {
244  int retc = 0;  //return value from compression phase
245  int reta = 0;  //return value from analysis phase
246  int retj = 0;  //return value from jsonoutput phase
247
248  if (c.outfile || c.analysisfile) {
249    DOUT1(" Parsing");
250    slip::Parser p(debug);
251    if (not p.load(c.infile)) {
252      FAIL("    Could not load input; exiting");
253      return 2;
254    }
255
256    if (c.outfile) {
257      retj = handleJson(c,debug,p);
258    }
259
260    if (c.analysisfile) {
261      reta = handleAnalysis(c,debug,p);
262    }
263  }
264
265  if (c.cfile || c.encode || c.skipsave) {
266    DOUT1(" Compressing ");
267    retc = handleCompression(c,debug);
268    if ((!c.skipsave) && c.dirmode && c.cfile && (!fileExists(c.cfile))) {
269      FAIL("  Failed to compress file, logging error");
270      ERRLOG(PATH(c.cfile).parent_path(),c.infile << " could not be compressed");
271    }
272  }
273
274  if (debug) {
275    DOUT1(" Cleaning up");
276  }
277  return retc+reta+retj;
278}
279
280int handleDirectory(const cmdoptions &c, const int debug) {
281  // verify all of our input and output directories are valid (not files + proper write permissions)
282  if (!(c.cfile || c.outfile || c.analysisfile)) {
283    FAIL("No output directories specified with -j, -a, or -X");
284    return -2;
285  }
286  if (c.outfile && (!makeDirectoryIfNotExists(c.outfile))) {
287    FAIL("JSON output directory '" << c.outfile << "' is not a valid directory");
288    return -2;
289  }
290  if (c.analysisfile && (!makeDirectoryIfNotExists(c.analysisfile))) {
291    FAIL("Analysis output directory '" << c.analysisfile << "' is not a valid directory");
292    return -2;
293  }
294  if (c.cfile && (!makeDirectoryIfNotExists(c.cfile))) {
295    FAIL("Compression output directory '" << c.cfile << "' is not a valid directory");
296    return -2;
297  }
298
299  // find all slippi files in a directory
300  for (const f_entry & entry : f_iter(std::string(c.infile))) {
301    std::string base  = entry.path().filename();
302    std::string noext = entry.path().stem();
303    if (getFileExt(base).compare("slp") == 0) {
304      cmdoptions c2;
305      copyCommandOptions(c,c2);
306      stringtoChars((entry.path()).string(),&(c2.infile));
307      if(c2.cfile) {
308        stringtoChars((PATH(c.cfile) / PATH(noext+".zlp")).string(),&(c2.cfile));
309      }
310      if(c2.outfile) {
311        stringtoChars((PATH(c.outfile) / PATH(base+".json")).string(),&(c2.outfile));
312      }
313      if(c2.analysisfile) {
314        stringtoChars((PATH(c.analysisfile) / PATH(noext+"-analysis.json")).string(),&(c2.analysisfile));
315      }
316      // std::cout << "  -i "   << c2.infile << std::endl;
317      // std::cout << "    -X " << c2.cfile << std::endl;
318      // std::cout << "    -j " << c2.outfile << std::endl;
319      // std::cout << "    -a " << c2.analysisfile << std::endl;
320      INFO("Processing file " << CYN << c2.infile << BLN);
321      int ret = handleSingleFile(c2,debug);
322      if (ret != 0) {
323        WARN("  Encountered errors processing input file " << RED << c2.infile << BLN);
324      }
325      cleanupCommandOptions(c2);
326    }
327  }
328  return 0;
329}
330
331int run(int argc, char** argv) {
332  if (cmdOptionExists(argv, argv+argc, "-h")) {
333    printUsage();
334    return 0;
335  }
336
337  cmdoptions c = getCommandLineOptions(argc,argv);
338
339  #if GUI_ENABLED == 1
340    if (not c.infile) { //if we don't have an input file, open file selector
341      getGUIOptions(c);
342    }
343  #endif
344
345  if (not c.infile) { //if we still don't have an input file, exit
346    WARN("No input selected");
347    printUsage();
348    return -1;
349  }
350
351  if(isDirectory(c.infile)) {
352    return handleDirectory(c,c.debug);
353  }
354  return handleSingleFile(c,c.debug);
355}
356
357}
358
359int main(int argc, char** argv) {
360  return slip::run(argc,argv);
361}