fork/slippc
git clone https://git.julia.blue/fork/slippc
edc0808
0.8.1
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 ) { 27char ** itr = std::find (begin ,end ,option ); 28if (itr != end && ++ itr != end ) { 29return * itr ; 30 } 31return 0 ; 32} 33 34bool cmdOptionExists (char ** begin ,char ** end ,const std::string & option ) { 35return std::find (begin ,end ,option )!= end ; 36} 37 38#if GUI_ENABLED == 1 39bool askYesNo (std::string title , std::string question ) { 40return 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 { 64char * dlevel = nullptr ; 65char * infile = nullptr ; 66char * cfile = nullptr ; 67char * outfile = nullptr ; 68char * analysisfile = nullptr ; 69bool nodelta = false; 70bool encode = false; 71bool rawencode = false; 72bool skipsave = false; 73bool dumpgecko = false; 74bool dirmode = false; 75int debug = 0 ; 76}cmdoptions ; 77 78cmdoptions getCommandLineOptions (int argc ,char ** argv ) { 79_cmdoptions c ; 80c .dlevel = getCmdOption (argv ,argv + argc ,"-d" ); 81c .infile = getCmdOption (argv ,argv + argc ,"-i" ); 82c .cfile = getCmdOption (argv ,argv + argc ,"-X" ); 83c .outfile = getCmdOption (argv ,argv + argc ,"-j" ); 84c .analysisfile = getCmdOption (argv ,argv + argc ,"-a" ); 85c .nodelta = cmdOptionExists (argv ,argv + argc ,"-f" ); 86c .encode = cmdOptionExists (argv ,argv + argc ,"-x" ); 87c .rawencode = cmdOptionExists (argv ,argv + argc ,"--raw-enc" ); 88c .skipsave = cmdOptionExists (argv ,argv + argc ,"--skip-save" ); 89c .dumpgecko = cmdOptionExists (argv ,argv + argc ,"--dump-gecko" ); 90c .dirmode = isDirectory (c .infile ); 91 92if (c .dlevel ) { 93if (c .dlevel [0 ] >='0' && c .dlevel [0 ] <='9' ) { 94c .debug = c .dlevel [0 ]- '0' ; 95_debug = c .debug ; 96 }else { 97c .debug = 1 ; 98_debug = c .debug ; 99 std::cerr <<"Warning: invalid debug level" << std::endl ; 100 } 101 } 102 103if (c .debug ) { 104DOUT1 ("Running at debug level " <<+ c .debug ); 105 } 106 107return c ; 108} 109 110#if GUI_ENABLED == 1 111void getGUIOptions (cmdoptions & c ) { 112if (_debug < 1 ) { 113_debug = 1 ;//force some debug output 114 } 115 std::string save ,saveext ; 116str_vec file_res = pfd::open_file ( 117"Select an input File" ,"." , {"Slippi Files" ,"*.slp *.zlp" }).result (); 118if (file_res .empty ()) { 119printUsage (); 120return "" ; 121 } 122stringtoChars (file_res [0 ],& c .infile ); 123 124 std::string inbase = getFileBase (file_res [0 ]); 125 std::string inext = getFileExt (file_res [0 ]); 126 127if (inext .compare ("slp" )== 0 ) { 128if (askYesNo ("Compress?" ,"Output compressed file (yes) or JSON (no)?" )) { 129DOUT1 ("GUI mode, compressed output" ); 130save = pfd::save_file ("Select an Output file" ,inbase + ".zlp" , {"Zlippi Files" ,"*.zlp" }).result (); 131stringtoChars (save ,& c .cfile ); 132 }else if (askYesNo ("Analysis?" ,"Output analysis JSON (yes) or regular JSON (no)?" )) { 133DOUT1 ("GUI mode, analysis output" ); 134save = pfd::save_file ("Select an Output file" ,inbase + ".json" , {"JSON Files" ,"*.json" }).result (); 135stringtoChars (save ,& c .analysisfile ); 136 }else { 137DOUT1 ("GUI mode, JSON output" ); 138save = pfd::save_file ("Select an Output file" ,inbase + ".json" , {"JSON Files" ,"*.json" }).result (); 139stringtoChars (save ,& c .outfile ); 140 } 141 }else if (inext .compare ("zlp" )== 0 ) { 142DOUT1 ("GUI mode, decompressed output" ); 143save = pfd::save_file ("Select an Output file" ,inbase + ".slp" , {"Slippi Files" ,"*.slp" }).result (); 144stringtoChars (save ,& c .cfile ); 145 } 146 } 147#endif 148 149inline void copyCommandOptions (const cmdoptions & c ,cmdoptions & c2 ) { 150memcpy (& c2 ,& c ,sizeof (cmdoptions )); 151} 152 153inline void cleanupCommandOptions (cmdoptions & c ) { 154if (c .infile ) { 155delete []c .infile ; 156 } 157if (c .cfile ) { 158delete []c .cfile ; 159 } 160if (c .outfile ) { 161delete []c .outfile ; 162 } 163if (c .analysisfile ) { 164delete []c .analysisfile ; 165 } 166} 167 168int handleCompression (const cmdoptions & c ,const int debug ) { 169 slip::Compressor cmp (debug ); 170 171if (c .cfile ) { 172if (!(cmp .setOutputFilename (c .cfile ))) { 173FAIL (" File " <<c .cfile <<" already exists or is invalid" ); 174return 4 ; 175 } 176 } 177 178if (c .dumpgecko ) { 179DOUT1 (" Setting gecko output filename" ); 180cmp .setGeckoOutputFilename (c .infile ); 181 } 182 183DOUT1 (" Encoding / decoding replay" ); 184if (notcmp .loadFromFile (c .infile )) { 185FAIL (" Failed to encode input; exiting" ); 186return 2 ; 187 } 188 189DOUT1 (" Validating encoding" ); 190if (!cmp .validate ()) { 191FAIL (" Validation failed; exiting" ); 192return 3 ; 193 } 194 195if (c .skipsave ) { 196DOUT1 (" Skipping saving" ); 197 }else { 198DOUT1 (" Saving encoded / decoded replay" ); 199cmp .saveToFile (c .rawencode ); 200 } 201 202return 0 ; 203} 204 205int handleAnalysis (const cmdoptions & c ,const int debug , slip::Parser & p ) { 206DOUT1 (" Analyzing" ); 207 slip::Analysis * a = p .analyze (); 208 209if (a -> success ) { 210if (c .analysisfile [0 ]== '-' && c .analysisfile [1 ]== '\0' ) { 211if (debug ) { 212DOUT1 (" Writing analysis to stdout" ); 213 } 214 std::cout <<a -> asJson () << std::endl ; 215 }else { 216if (debug ) { 217DOUT1 (" Saving analysis to file" ); 218 } 219a -> save (c .analysisfile ); 220 } 221 } 222 223delete a ; 224return 0 ; 225} 226 227int handleJson (const cmdoptions & c ,const int debug , slip::Parser & p ) { 228DOUT1 (" Writing JSON" ); 229if (c .outfile [0 ]== '-' && c .outfile [1 ]== '\0' ) { 230if (debug ) { 231DOUT1 (" Writing Slippi JSON data to stdout" ); 232 } 233 std::cout <<p .asJson (!c .nodelta ) << std::endl ; 234 }else { 235if (debug ) { 236DOUT1 (" Saving Slippi JSON data to file" ); 237 } 238p .save (c .outfile ,!c .nodelta ); 239 } 240return 0 ; 241} 242 243int handleSingleFile (const cmdoptions & c ,const int debug ) { 244int retc = 0 ;//return value from compression phase 245int reta = 0 ;//return value from analysis phase 246int retj = 0 ;//return value from jsonoutput phase 247 248if (c .outfile || c .analysisfile ) { 249DOUT1 (" Parsing" ); 250 slip::Parser p (debug ); 251if (notp .load (c .infile )) { 252FAIL (" Could not load input; exiting" ); 253return 2 ; 254 } 255 256if (c .outfile ) { 257retj = handleJson (c ,debug ,p ); 258 } 259 260if (c .analysisfile ) { 261reta = handleAnalysis (c ,debug ,p ); 262 } 263 } 264 265if (c .cfile || c .encode || c .skipsave ) { 266DOUT1 (" Compressing " ); 267retc = handleCompression (c ,debug ); 268if ((!c .skipsave )&& c .dirmode && c .cfile && (!fileExists (c .cfile ))) { 269FAIL (" Failed to compress file, logging error" ); 270ERRLOG (PATH (c .cfile ).parent_path (),c .infile <<" could not be compressed" ); 271 } 272 } 273 274if (debug ) { 275DOUT1 (" Cleaning up" ); 276 } 277return 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) 282if (!(c .cfile || c .outfile || c .analysisfile )) { 283FAIL ("No output directories specified with -j, -a, or -X" ); 284return -2 ; 285 } 286if (c .outfile && (!makeDirectoryIfNotExists (c .outfile ))) { 287FAIL ("JSON output directory '" <<c .outfile <<"' is not a valid directory" ); 288return -2 ; 289 } 290if (c .analysisfile && (!makeDirectoryIfNotExists (c .analysisfile ))) { 291FAIL ("Analysis output directory '" <<c .analysisfile <<"' is not a valid directory" ); 292return -2 ; 293 } 294if (c .cfile && (!makeDirectoryIfNotExists (c .cfile ))) { 295FAIL ("Compression output directory '" <<c .cfile <<"' is not a valid directory" ); 296return -2 ; 297 } 298 299// find all slippi files in a directory 300for (const f_entry & entry :f_iter (std::string (c .infile ))) { 301 std::string base = entry .path ().filename (); 302 std::string noext = entry .path ().stem (); 303if (getFileExt (base ).compare ("slp" )== 0 ) { 304cmdoptions c2 ; 305copyCommandOptions (c ,c2 ); 306stringtoChars ((entry .path ()).string (),& (c2 .infile )); 307if (c2 .cfile ) { 308stringtoChars ((PATH (c .cfile ) /PATH (noext + ".zlp" )).string (),& (c2 .cfile )); 309 } 310if (c2 .outfile ) { 311stringtoChars ((PATH (c .outfile ) /PATH (base + ".json" )).string (),& (c2 .outfile )); 312 } 313if (c2 .analysisfile ) { 314stringtoChars ((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; 320INFO ("Processing file " <<CYN <<c2 .infile <<BLN ); 321int ret = handleSingleFile (c2 ,debug ); 322if (ret != 0 ) { 323WARN (" Encountered errors processing input file " <<RED <<c2 .infile <<BLN ); 324 } 325cleanupCommandOptions (c2 ); 326 } 327 } 328return 0 ; 329} 330 331int run (int argc ,char ** argv ) { 332if (cmdOptionExists (argv ,argv + argc ,"-h" )) { 333printUsage (); 334return 0 ; 335 } 336 337cmdoptions c = getCommandLineOptions (argc ,argv ); 338 339#if GUI_ENABLED == 1 340if (notc .infile ) {//if we don't have an input file, open file selector 341getGUIOptions (c ); 342 } 343#endif 344 345if (notc .infile ) {//if we still don't have an input file, exit 346WARN ("No input selected" ); 347printUsage (); 348return -1 ; 349 } 350 351if (isDirectory (c .infile )) { 352return handleDirectory (c ,c .debug ); 353 } 354return handleSingleFile (c ,c .debug ); 355} 356 357} 358 359int main (int argc ,char ** argv ) { 360return slip::run (argc ,argv ); 361}