fork/slippc
git clone https://git.julia.blue/fork/slippc
7abc388
0.6.0
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 ) { 18char ** itr = std::find (begin ,end ,option ); 19if (itr != end && ++ itr != end ) { 20return * itr ; 21 } 22return 0 ; 23} 24 25bool cmdOptionExists (char ** begin ,char ** end ,const std::string & option ) { 26return std::find (begin ,end ,option )!= end ; 27} 28 29#if GUI_ENABLED == 1 30bool askYesNo (std::string title , std::string question ) { 31return 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 ) { 53if (cmdOptionExists (argv ,argv + argc ,"-h" )) { 54printUsage (); 55return 0 ; 56 } 57 58int debug = 0 ; 59char * dlevel = getCmdOption (argv ,argv + argc ,"-d" ); 60char * infile = getCmdOption (argv ,argv + argc ,"-i" ); 61char * cfile = getCmdOption (argv ,argv + argc ,"-X" ); 62char * outfile = getCmdOption (argv ,argv + argc ,"-j" ); 63char * analysisfile = getCmdOption (argv ,argv + argc ,"-a" ); 64bool nodelta = cmdOptionExists (argv ,argv + argc ,"-f" ); 65bool encode = cmdOptionExists (argv ,argv + argc ,"-x" ); 66bool rawencode = cmdOptionExists (argv ,argv + argc ,"--raw-enc" ); 67 68if (dlevel ) { 69if (dlevel [0 ] >='0' && dlevel [0 ] <='9' ) { 70debug = dlevel [0 ]- '0' ; 71 }else { 72 std::cerr <<"Warning: invalid debug level" << std::endl ; 73 } 74 } 75if (debug ) { 76 std::cout <<"Running at debug level " <<+ debug << std::endl ; 77 } 78 79if (notinfile ) {//Running in GUI dialog mode 80 std::string save ,saveext ; 81#if GUI_ENABLED == 1 82debug = 1 ;//Force some debug output in GUI mode 83str_vec file_res = pfd::open_file ( 84"Select an input File" ,"." , {"Slippi Files" ,"*.slp *.zlp" }).result (); 85if (file_res .empty ()) { 86printUsage (); 87return -1 ; 88 } 89stringtoChars (file_res [0 ],& infile ); 90 91 std::string inbase = getFileBase (file_res [0 ]); 92 std::string inext = getFileExt (file_res [0 ]); 93 94if (inext .compare ("slp" )== 0 ) { 95if (askYesNo ("Compress?" ,"Output compressed file (yes) or JSON (no)?" )) { 96 std::cout <<"GUI mode, compressed output" << std::endl ; 97save = pfd::save_file ("Select an Output file" ,inbase + ".zlp" , {"Zlippi Files" ,"*.zlp" }).result (); 98stringtoChars (save ,& cfile ); 99 }else if (askYesNo ("Analysis?" ,"Output analysis JSON (yes) or regular JSON (no)?" )) { 100 std::cout <<"GUI mode, analysis output" << std::endl ; 101save = pfd::save_file ("Select an Output file" ,inbase + ".json" , {"JSON Files" ,"*.json" }).result (); 102stringtoChars (save ,& analysisfile ); 103 }else { 104 std::cout <<"GUI mode, JSON output" << std::endl ; 105save = pfd::save_file ("Select an Output file" ,inbase + ".json" , {"JSON Files" ,"*.json" }).result (); 106stringtoChars (save ,& outfile ); 107 } 108 }else if (inext .compare ("zlp" )== 0 ) { 109 std::cout <<"GUI mode, decompressed output" << std::endl ; 110save = pfd::save_file ("Select an Output file" ,inbase + ".slp" , {"Slippi Files" ,"*.slp" }).result (); 111stringtoChars (save ,& cfile ); 112 } 113#endif 114if (save .empty ()) { 115 std::cerr <<"No output file selected" << std::endl ; 116printUsage (); 117return -1 ; 118 } 119 } 120 121if (cfile || encode ) { 122 slip::Compressor * c = new slip::Compressor (debug ); 123 124if (cfile ) { 125if (!(c -> setOutputFilename (cfile ))) { 126 std::cerr <<"File " <<cfile <<" already exists or is invalid" << std::endl ; 127return 4 ; 128 } 129 } 130 131 std::cerr <<"Encoding / decoding replay" << std::endl ; 132if (notc -> loadFromFile (infile )) { 133 std::cerr <<"Failed to encode input; exiting" << std::endl ; 134delete c ; 135return 2 ; 136 } 137if (rawencode ) { 138 std::cerr <<"Skipping validation" << std::endl ; 139 }else { 140 std::cerr <<"Validating encoding" << std::endl ; 141if (!c -> validate ()) { 142 std::cerr <<"Validation failed; exiting" << std::endl ; 143return 3 ; 144 } 145 } 146 std::cerr <<"Saving encoded / decoded replay" << std::endl ; 147c -> saveToFile (rawencode ); 148 std::cerr <<"Saved!" << std::endl ; 149return 0 ; 150 } 151 152 slip::Parser * p = new slip::Parser (debug ); 153if (notp -> load (infile )) { 154 std::cerr <<"Could not load input; exiting" << std::endl ; 155delete p ; 156return 2 ; 157 } 158 159if (outfile ) { 160if (outfile [0 ]== '-' && outfile [1 ]== '\0' ) { 161if (debug ) { 162 std::cerr <<"Writing Slippi JSON data to stdout" << std::endl ; 163 } 164 std::cout <<p -> asJson (!nodelta ) << std::endl ; 165 }else { 166if (debug ) { 167 std::cout <<"Saving Slippi JSON data to file" << std::endl ; 168 } 169p -> save (outfile ,!nodelta ); 170 } 171 } 172 173if (analysisfile ) { 174 slip::Analysis * a = p -> analyze (); 175if (a -> success ) { 176if (analysisfile [0 ]== '-' && analysisfile [1 ]== '\0' ) { 177if (debug ) { 178 std::cout <<"Writing analysis to stdout" << std::endl ; 179 } 180 std::cout <<a -> asJson () << std::endl ; 181 }else { 182if (debug ) { 183 std::cout <<"Saving analysis to file" << std::endl ; 184 } 185a -> save (analysisfile ); 186 } 187 } 188if (debug ) { 189 std::cout <<"Deleting analysis" << std::endl ; 190 } 191 192delete a ; 193 } 194 195if (debug ) { 196 std::cout <<"Cleaning up" << std::endl ; 197 } 198delete p ; 199return 0 ; 200} 201 202} 203 204int main (int argc ,char ** argv ) { 205return slip::run (argc ,argv ); 206}