fork/slippc

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

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

0.6.1
6.6 KiB206 linesraw
1#include <algorithm>
2
3#include "util.h"
4#include "parser.h"
5#include "analyzer.h"
6#include "compressor.h"
7
8#if GUI_ENABLED == 1
9  #include "portable-file-dialogs.h"
10#endif
11
12namespace slip {
13
14typedef std::vector<std::__cxx11::basic_string<char> > str_vec;
15
16// https://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c
17char* getCmdOption(char ** begin, char ** end, const std::string & option) {
18  char ** itr = std::find(begin, end, option);
19  if (itr != end && ++itr != end) {
20    return *itr;
21  }
22  return 0;
23}
24
25bool cmdOptionExists(char** begin, char** end, const std::string& option) {
26  return std::find(begin, end, option) != end;
27}
28
29#if GUI_ENABLED == 1
30  bool askYesNo(std::string title, std::string question) {
31    return pfd::message(title, question, pfd::choice::yes_no, pfd::icon::question).result()==pfd::button::yes;
32  }
33#endif
34
35void printUsage() {
36  std::cout
37    << "Usage: slippc -i <infile> [-x | -X <zlpfle>] [-j <jsonfile>] [-a <analysisfile>] [-f] [-d <debuglevel>] [-h]:" << std::endl
38    << "  -i        Parse and analyze <infile> (not very useful on its own)" << std::endl
39    << "  -j        Output <infile> in .json format to <jsonfile> (use \"-\" for stdout)" << std::endl
40    << "  -a        Output an analysis of <infile> in .json format to <analysisfile> (use \"-\" for stdout)" << std::endl
41    << "  -f        When used with -j <jsonfile>, write full frame info (instead of just frame deltas)" << std::endl
42    << "  -x        Compress or decompress a replay" << std::endl
43    << "  -X        Set output file name for compression" << std::endl
44    << std::endl
45    << "Debug options:" << std::endl
46    << "  -d        Run at debug level <debuglevel> (show debug output)" << std::endl
47    << "  --raw-enc Output raw encodes with -x (DANGEROUS, debug only)" << std::endl
48    << "  -h        Show this help message" << std::endl
49    ;
50}
51
52int run(int argc, char** argv) {
53  if (cmdOptionExists(argv, argv+argc, "-h")) {
54    printUsage();
55    return 0;
56  }
57
58  int debug          = 0;
59  char* dlevel       = getCmdOption(   argv, argv+argc, "-d");
60  char* infile       = getCmdOption(   argv, argv+argc, "-i");
61  char* cfile        = getCmdOption(   argv, argv+argc, "-X");
62  char* outfile      = getCmdOption(   argv, argv+argc, "-j");
63  char* analysisfile = getCmdOption(   argv, argv+argc, "-a");
64  bool  nodelta      = cmdOptionExists(argv, argv+argc, "-f");
65  bool  encode       = cmdOptionExists(argv, argv+argc, "-x");
66  bool  rawencode    = cmdOptionExists(argv, argv+argc, "--raw-enc");
67
68  if (dlevel) {
69    if (dlevel[0] >= '0' && dlevel[0] <= '9') {
70      debug = dlevel[0]-'0';
71    } else {
72      std::cerr << "Warning: invalid debug level" << std::endl;
73    }
74  }
75  if (debug) {
76    std::cout << "Running at debug level " << +debug << std::endl;
77  }
78
79  if (not infile) { //Running in GUI dialog mode
80    std::string save, saveext;
81    #if GUI_ENABLED == 1
82      debug = 1;  //Force some debug output in GUI mode
83      str_vec file_res = pfd::open_file(
84        "Select an input File", ".", {"Slippi Files", "*.slp *.zlp"}).result();
85      if(file_res.empty()) {
86        printUsage();
87        return -1;
88      }
89      stringtoChars(file_res[0],&infile);
90
91      std::string inbase = getFileBase(file_res[0]);
92      std::string inext  = getFileExt(file_res[0]);
93
94      if (inext.compare("slp") == 0) {
95        if (askYesNo("Compress?","Output compressed file (yes) or JSON (no)?")) {
96          std::cout << "GUI mode, compressed output" << std::endl;
97          save = pfd::save_file("Select an Output file", inbase+".zlp", { "Zlippi Files", "*.zlp"}).result();
98          stringtoChars(save,&cfile);
99        } else if (askYesNo("Analysis?", "Output analysis JSON (yes) or regular JSON (no)?")) {
100          std::cout << "GUI mode, analysis output" << std::endl;
101          save = pfd::save_file("Select an Output file", inbase+".json", { "JSON Files", "*.json"}).result();
102          stringtoChars(save,&analysisfile);
103        } else {
104          std::cout << "GUI mode, JSON output" << std::endl;
105          save = pfd::save_file("Select an Output file", inbase+".json", { "JSON Files", "*.json"}).result();
106          stringtoChars(save,&outfile);
107        }
108      } else if (inext.compare("zlp") == 0) {
109        std::cout << "GUI mode, decompressed output" << std::endl;
110        save = pfd::save_file("Select an Output file", inbase+".slp", { "Slippi Files", "*.slp"}).result();
111        stringtoChars(save,&cfile);
112      }
113    #endif
114    if(save.empty()) {
115      std::cerr << "No output file selected" << std::endl;
116      printUsage();
117      return -1;
118    }
119  }
120
121  if(cfile || encode) {
122    slip::Compressor *c = new slip::Compressor(debug);
123
124    if (cfile) {
125      if (!(c->setOutputFilename(cfile))) {
126        std::cerr << "File " << cfile << " already exists or is invalid" << std::endl;
127        return 4;
128      }
129    }
130
131    std::cerr << "Encoding / decoding replay" << std::endl;
132    if (not c->loadFromFile(infile)) {
133      std::cerr << "Failed to encode input; exiting" << std::endl;
134      delete c;
135      return 2;
136    }
137    if (rawencode) {
138        std::cerr << "Skipping validation" << std::endl;
139    } else {
140      std::cerr << "Validating encoding" << std::endl;
141      if (!c->validate()) {
142        std::cerr << "Validation failed; exiting" << std::endl;
143        return 3;
144      }
145    }
146    std::cerr << "Saving encoded / decoded replay" << std::endl;
147    c->saveToFile(rawencode);
148    std::cerr << "Saved!" << std::endl;
149    return 0;
150  }
151
152  slip::Parser *p = new slip::Parser(debug);
153  if (not p->load(infile)) {
154    std::cerr << "Could not load input; exiting" << std::endl;
155    delete p;
156    return 2;
157  }
158
159  if (outfile) {
160    if (outfile[0] == '-' && outfile[1] == '\0') {
161      if (debug) {
162        std::cerr << "Writing Slippi JSON data to stdout" << std::endl;
163      }
164      std::cout << p->asJson(!nodelta) << std::endl;
165    } else {
166      if (debug) {
167        std::cout << "Saving Slippi JSON data to file" << std::endl;
168      }
169      p->save(outfile,!nodelta);
170    }
171  }
172
173  if (analysisfile) {
174    slip::Analysis *a  = p->analyze();
175    if (a->success) {
176      if (analysisfile[0] == '-' && analysisfile[1] == '\0') {
177        if (debug) {
178          std::cout << "Writing analysis to stdout" << std::endl;
179        }
180        std::cout << a->asJson() << std::endl;
181      } else {
182        if (debug) {
183          std::cout << "Saving analysis to file" << std::endl;
184        }
185        a->save(analysisfile);
186      }
187    }
188    if (debug) {
189      std::cout << "Deleting analysis" << std::endl;
190    }
191
192    delete a;
193  }
194
195  if (debug) {
196    std::cout << "Cleaning up" << std::endl;
197  }
198  delete p;
199  return 0;
200}
201
202}
203
204int main(int argc, char** argv) {
205  return slip::run(argc,argv);
206}