2014-10-05 21:52:34 +00:00
|
|
|
#include <iostream>
|
2014-10-09 20:15:13 +00:00
|
|
|
#include "boost/program_options.hpp"
|
|
|
|
#include "boost/filesystem.hpp"
|
|
|
|
#include "boost/filesystem/fstream.hpp"
|
2014-10-05 21:52:34 +00:00
|
|
|
|
|
|
|
#include "RezParser.generated.hh"
|
|
|
|
#include "RezLexer.h"
|
2014-10-07 18:15:46 +00:00
|
|
|
#include "RezWorld.h"
|
2014-10-05 21:52:34 +00:00
|
|
|
|
2014-10-21 20:37:43 +00:00
|
|
|
#include "ResourceFork.h"
|
2014-10-08 00:52:34 +00:00
|
|
|
#include "BinaryIO.h"
|
2014-10-31 00:16:47 +00:00
|
|
|
#include "ResourceFile.h"
|
2015-07-18 22:59:46 +00:00
|
|
|
#include "Diagnostic.h"
|
2014-10-08 00:52:34 +00:00
|
|
|
|
2014-10-09 20:15:13 +00:00
|
|
|
namespace po = boost::program_options;
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
static po::options_description desc;
|
|
|
|
|
|
|
|
static void usage()
|
|
|
|
{
|
|
|
|
std::cerr << "Usage: " << "Rez [options] input-file\n";
|
|
|
|
std::cerr << desc << std::endl;
|
|
|
|
}
|
|
|
|
|
2015-08-27 18:49:55 +00:00
|
|
|
static void CopyBinaryResources(RezWorld& world, const std::string& fn)
|
|
|
|
{
|
|
|
|
ResourceFile copyRsrc(fn);
|
|
|
|
if(!copyRsrc.read())
|
|
|
|
{
|
|
|
|
world.problem(Diagnostic(Diagnostic::error, "Could not read binary resource file " + fn, yy::location()));
|
|
|
|
}
|
|
|
|
else if(world.verboseFlag)
|
|
|
|
{
|
|
|
|
std::cerr << "Read " << copyRsrc.resources.countResources() << " resources from " << fn << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
world.getResources().addResources(copyRsrc.resources);
|
|
|
|
}
|
|
|
|
|
2014-10-09 20:15:13 +00:00
|
|
|
int main(int argc, const char *argv[])
|
2014-10-05 21:52:34 +00:00
|
|
|
{
|
2014-10-09 20:15:13 +00:00
|
|
|
desc.add_options()
|
|
|
|
("help,h", "show this help message")
|
|
|
|
("output,o", po::value<std::string>()->default_value("rez.output.rsrc"), "output file")
|
2014-10-16 00:27:12 +00:00
|
|
|
("append,a", "append to existing output file")
|
2014-10-09 20:15:13 +00:00
|
|
|
("type,t", po::value<std::string>()->default_value("rsrc"), "output file finder type code")
|
|
|
|
("creator,c", po::value<std::string>()->default_value("RSED"), "output file finder creator code")
|
2014-10-16 00:26:41 +00:00
|
|
|
("define,D", po::value<std::vector<std::string>>(), "predefine preprocessor symbol")
|
|
|
|
("include,I", po::value<std::vector<std::string>>(), "add include file path")
|
2014-11-07 23:53:58 +00:00
|
|
|
("copy", po::value<std::vector<std::string>>(), "copy resources from other resource file")
|
2015-07-20 01:31:14 +00:00
|
|
|
("cc", po::value<std::vector<std::string>>(), "also write output to another file")
|
2014-10-09 20:15:13 +00:00
|
|
|
("debug,d", "debug logging")
|
2015-08-31 21:47:25 +00:00
|
|
|
("data", po::value<std::string>(), "copy data fork from another file")
|
2014-10-09 20:15:13 +00:00
|
|
|
;
|
|
|
|
po::options_description hidden, alldesc;
|
|
|
|
hidden.add_options()
|
|
|
|
("input", po::value<std::vector<std::string>>(), "input file" )
|
|
|
|
;
|
|
|
|
alldesc.add(desc).add(hidden);
|
|
|
|
|
|
|
|
po::variables_map options;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
auto parsed = po::command_line_parser(argc, argv)
|
|
|
|
.options(alldesc)
|
|
|
|
.positional(po::positional_options_description().add("input", -1))
|
2015-07-20 01:31:14 +00:00
|
|
|
.style(po::command_line_style::default_style)
|
2014-10-09 20:15:13 +00:00
|
|
|
.run();
|
|
|
|
|
|
|
|
po::store(parsed, options);
|
|
|
|
}
|
|
|
|
catch(po::error& e)
|
|
|
|
{
|
|
|
|
std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
po::notify(options);
|
|
|
|
|
2015-07-17 23:09:18 +00:00
|
|
|
if(options.count("help") || (!options.count("input") && !options.count("copy")))
|
2014-10-09 20:15:13 +00:00
|
|
|
{
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-07 18:15:46 +00:00
|
|
|
RezWorld world;
|
2014-10-05 21:52:34 +00:00
|
|
|
|
2014-10-09 20:15:13 +00:00
|
|
|
if(options.count("debug"))
|
|
|
|
world.verboseFlag = true;
|
|
|
|
|
2014-10-16 00:27:12 +00:00
|
|
|
std::string outfile = options["output"].as<std::string>();
|
2014-10-31 00:16:47 +00:00
|
|
|
ResourceFile rsrcFile(outfile);
|
2014-10-16 00:27:12 +00:00
|
|
|
|
|
|
|
if(options.count("append"))
|
|
|
|
{
|
2014-10-31 00:16:47 +00:00
|
|
|
rsrcFile.read();
|
|
|
|
|
|
|
|
world.getResources().addResources(rsrcFile.resources);
|
2014-10-16 00:27:12 +00:00
|
|
|
}
|
2015-08-31 21:47:25 +00:00
|
|
|
|
|
|
|
if(options.count("data"))
|
|
|
|
{
|
|
|
|
std::string fn = options["data"].as<std::string>();
|
|
|
|
ResourceFile dataFile(fn);
|
|
|
|
if(!dataFile.read())
|
|
|
|
world.problem(Diagnostic(Diagnostic::error, "Could not read dataresource file " + fn, yy::location()));
|
|
|
|
rsrcFile.data = dataFile.data;
|
|
|
|
}
|
|
|
|
|
2014-11-07 23:53:58 +00:00
|
|
|
if(options.count("copy"))
|
2015-08-27 18:49:55 +00:00
|
|
|
for(std::string fn : options["copy"].as<std::vector<std::string>>())
|
|
|
|
CopyBinaryResources(world, fn);
|
2014-10-16 00:27:12 +00:00
|
|
|
|
2015-07-17 23:09:18 +00:00
|
|
|
if(options.count("input"))
|
|
|
|
for(std::string fn : options["input"].as<std::vector<std::string>>())
|
|
|
|
{
|
2015-07-20 01:31:14 +00:00
|
|
|
fs::path path(fn);
|
|
|
|
if(path.extension() == ".rsrc" || path.extension() == ".bin")
|
2015-07-18 22:59:46 +00:00
|
|
|
{
|
2015-08-27 18:49:55 +00:00
|
|
|
CopyBinaryResources(world, fn);
|
2015-07-18 22:59:46 +00:00
|
|
|
}
|
2015-07-20 01:31:14 +00:00
|
|
|
else
|
2015-07-18 22:59:46 +00:00
|
|
|
{
|
2015-07-20 01:31:14 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
RezLexer lexer(world, fn);
|
|
|
|
|
|
|
|
if(options.count("define"))
|
|
|
|
for(std::string define : options["define"].as<std::vector<std::string>>())
|
|
|
|
lexer.addDefine(define);
|
|
|
|
if(options.count("include"))
|
|
|
|
for(std::string path : options["include"].as<std::vector<std::string>>())
|
|
|
|
lexer.addIncludePath(path);
|
|
|
|
|
2015-08-27 18:49:55 +00:00
|
|
|
if(world.verboseFlag)
|
|
|
|
{
|
|
|
|
std::cerr << "Compiling " << fn << "...\n";
|
|
|
|
}
|
2015-07-20 01:31:14 +00:00
|
|
|
|
|
|
|
RezParser parser(lexer, world);
|
|
|
|
parser.parse();
|
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
world.problem(Diagnostic(Diagnostic::fatalError,"unknown error",yy::location(&fn)));
|
|
|
|
}
|
2015-07-18 22:59:46 +00:00
|
|
|
}
|
2015-07-17 23:09:18 +00:00
|
|
|
}
|
2014-10-09 20:15:13 +00:00
|
|
|
|
2015-07-18 22:59:46 +00:00
|
|
|
if(world.hadErrors)
|
|
|
|
return 1;
|
|
|
|
|
2014-10-31 00:16:47 +00:00
|
|
|
rsrcFile.resources = world.getResources();
|
|
|
|
rsrcFile.creator = options["creator"].as<std::string>();
|
|
|
|
rsrcFile.type = options["type"].as<std::string>();
|
2015-08-27 18:49:55 +00:00
|
|
|
|
|
|
|
if(world.verboseFlag)
|
|
|
|
{
|
|
|
|
std::cerr << "Writing " << rsrcFile.resources.countResources() << " resources.\n";
|
|
|
|
}
|
2014-10-31 00:16:47 +00:00
|
|
|
rsrcFile.write();
|
2015-07-18 22:59:46 +00:00
|
|
|
|
2015-07-20 01:31:14 +00:00
|
|
|
if(options.count("cc"))
|
|
|
|
for(std::string ccFile : options["cc"].as<std::vector<std::string>>())
|
|
|
|
{
|
|
|
|
rsrcFile.assign(ccFile);
|
|
|
|
rsrcFile.write();
|
|
|
|
}
|
|
|
|
|
2014-10-05 21:52:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|