this repo has no description
at fixPythonPipStalling 174 lines 5.5 kB view raw
1#include <iostream> 2#include "boost/program_options.hpp" 3#include "boost/filesystem.hpp" 4#include "boost/filesystem/fstream.hpp" 5 6#include "RezParser.generated.hh" 7#include "RezLexer.h" 8#include "RezWorld.h" 9 10#include "ResourceFork.h" 11#include "BinaryIO.h" 12#include "ResourceFile.h" 13#include "Diagnostic.h" 14 15namespace po = boost::program_options; 16namespace fs = boost::filesystem; 17 18static po::options_description desc; 19 20static void usage() 21{ 22 std::cerr << "Usage: " << "Rez [options] input-file\n"; 23 std::cerr << desc << std::endl; 24} 25 26static void CopyBinaryResources(RezWorld& world, const std::string& fn) 27{ 28 ResourceFile copyRsrc; 29 if(!copyRsrc.read(fn)) 30 { 31 world.problem(Diagnostic(Diagnostic::error, "Could not read binary resource file " + fn, yy::location())); 32 } 33 else if(world.verboseFlag) 34 { 35 std::cerr << "Read " << copyRsrc.resources.countResources() << " resources from " << fn << "\n"; 36 } 37 38 world.getResources().addResources(copyRsrc.resources); 39} 40 41int main(int argc, const char *argv[]) 42{ 43 desc.add_options() 44 ("help,h", "show this help message") 45 ("output,o", po::value<std::string>()->default_value("rez.output.rsrc"), "output file") 46 ("append,a", "append to existing output file") 47 ("type,t", po::value<std::string>()->default_value("rsrc"), "output file finder type code") 48 ("creator,c", po::value<std::string>()->default_value("RSED"), "output file finder creator code") 49 ("define,D", po::value<std::vector<std::string>>(), "predefine preprocessor symbol") 50 ("include,I", po::value<std::vector<std::string>>(), "add include file path") 51 ("copy", po::value<std::vector<std::string>>(), "copy resources from other resource file") 52 ("cc", po::value<std::vector<std::string>>(), "also write output to another file") 53 ("debug,d", "debug logging") 54 ("data", po::value<std::string>(), "copy data fork from another file") 55 ; 56 po::options_description hidden, alldesc; 57 hidden.add_options() 58 ("input", po::value<std::vector<std::string>>(), "input file" ) 59 ; 60 alldesc.add(desc).add(hidden); 61 62 po::variables_map options; 63 try 64 { 65 auto parsed = po::command_line_parser(argc, argv) 66 .options(alldesc) 67 .positional(po::positional_options_description().add("input", -1)) 68 .style(po::command_line_style::default_style) 69 .run(); 70 71 po::store(parsed, options); 72 } 73 catch(po::error& e) 74 { 75 std::cerr << "ERROR: " << e.what() << std::endl << std::endl; 76 usage(); 77 return 1; 78 } 79 80 po::notify(options); 81 82 if(options.count("help") 83 || (!options.count("input") && !options.count("copy") 84 && !options.count("output"))) 85 { 86 usage(); 87 return 0; 88 } 89 90 RezWorld world; 91 92 if(options.count("debug")) 93 world.verboseFlag = true; 94 95 std::string outfile = options["output"].as<std::string>(); 96 ResourceFile rsrcFile; 97 98 if(options.count("append")) 99 { 100 rsrcFile.read(outfile); 101 102 world.getResources().addResources(rsrcFile.resources); 103 } 104 105 if(options.count("data")) 106 { 107 std::string fn = options["data"].as<std::string>(); 108 ResourceFile dataFile; 109 if(!dataFile.read(fn)) 110 world.problem(Diagnostic(Diagnostic::error, "Could not read dataresource file " + fn, yy::location())); 111 rsrcFile.data = dataFile.data; 112 } 113 114 if(options.count("copy")) 115 for(std::string fn : options["copy"].as<std::vector<std::string>>()) 116 CopyBinaryResources(world, fn); 117 118 if(options.count("input")) 119 for(std::string fn : options["input"].as<std::vector<std::string>>()) 120 { 121 fs::path path(fn); 122 if(path.extension() == ".rsrc" || path.extension() == ".bin") 123 { 124 CopyBinaryResources(world, fn); 125 } 126 else 127 { 128 try 129 { 130 RezLexer lexer(world, fn); 131 132 if(options.count("define")) 133 for(std::string define : options["define"].as<std::vector<std::string>>()) 134 lexer.addDefine(define); 135 if(options.count("include")) 136 for(std::string path : options["include"].as<std::vector<std::string>>()) 137 lexer.addIncludePath(path); 138 139 if(world.verboseFlag) 140 { 141 std::cerr << "Compiling " << fn << "...\n"; 142 } 143 144 RezParser parser(lexer, world); 145 parser.parse(); 146 } 147 catch(...) 148 { 149 world.problem(Diagnostic(Diagnostic::fatalError,"unknown error",yy::location(&fn))); 150 } 151 } 152 } 153 154 if(world.hadErrors) 155 return 1; 156 157 rsrcFile.resources = world.getResources(); 158 rsrcFile.creator = options["creator"].as<std::string>(); 159 rsrcFile.type = options["type"].as<std::string>(); 160 161 if(world.verboseFlag) 162 { 163 std::cerr << "Writing " << rsrcFile.resources.countResources() << " resources.\n"; 164 } 165 rsrcFile.write(outfile); 166 167 if(options.count("cc")) 168 for(std::string ccFile : options["cc"].as<std::vector<std::string>>()) 169 { 170 rsrcFile.write(ccFile); 171 } 172 173 return 0; 174}