Added robust program options & prepared for entity conversion

This commit is contained in:
2017-06-18 07:44:42 -07:00
parent 728f6fba8b
commit ff1f57fdaa
3 changed files with 86 additions and 38 deletions

View File

@ -103,32 +103,26 @@ vector<string> get_entity(ifstream &f) {
void parse_prefab(ifstream &f, void parse_prefab(ifstream &f,
ofstream &out, ofstream &out,
void (*b) (stringstream &, const vector<TPlanePoints> &), void (*b) (stringstream &, const vector<TPlanePoints> &),
EntityConverter &e) { queue<vector<string> > &entities) {
string l; string l;
getline(f, l); getline(f, l);
if (l.find(KEYWORD_BRUSH) != string::npos) { if (l.find(KEYWORD_BRUSH) != string::npos) {
write(parse_brush<ifstream>(f), out, b); write(parse_brush<ifstream>(f), out, b);
} else if (l.find(KEYWORD_ENTITY) != string::npos) { } else if (l.find(KEYWORD_ENTITY) != string::npos) {
write(get_entity(f), out, b, e); entities.push(get_entity(f));
} }
} }
bool convertmap(const char * const infile, bool convertmap(const char * const infile,
const char * const outfile, const char * const outfile,
void (*brushdef) (stringstream &, const vector<TPlanePoints> &), void (*brushdef) (stringstream &, const vector<TPlanePoints> &),
const char * const entfile) { queue<vector<string> > &entities) {
ifstream fin; ifstream fin;
fin.open(infile); fin.open(infile);
if (!fin.good()){ if (!fin.good()){
cerr << "error: failed to open input file" << endl; cerr << "error: failed to open input file" << endl;
return false; return false;
} }
EntityConverter ec;
if (entfile != NULL) {
string ef(entfile);
string in(infile);
ec = EntityConverter(ef, in);
}
ofstream fout; ofstream fout;
fout.open(outfile); fout.open(outfile);
if (!fout.good()) { if (!fout.good()) {
@ -141,9 +135,9 @@ bool convertmap(const char * const infile,
while (getline(fin, line)) { while (getline(fin, line)) {
if (line.find(KEYWORD_PREFAB) != string::npos || if (line.find(KEYWORD_PREFAB) != string::npos ||
line.find(KEYWORD_GLOBAL) != string::npos) { line.find(KEYWORD_GLOBAL) != string::npos) {
parse_prefab(fin, fout, brushdef, ec); parse_prefab(fin, fout, brushdef, entities);
} else if (line.find(KEYWORD_ENTITY) != string::npos) { } else if (line.find(KEYWORD_ENTITY) != string::npos) {
write(get_entity(fin), fout, brushdef, ec); entities.push(get_entity(fin));
} else if (line.find(KEYWORD_BRUSH) != string::npos) { } else if (line.find(KEYWORD_BRUSH) != string::npos) {
write(parse_brush<ifstream>(fin), fout, brushdef); write(parse_brush<ifstream>(fin), fout, brushdef);
} }

View File

@ -7,6 +7,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include <queue>
#include "EntityConverter.hpp" #include "EntityConverter.hpp"
bool is_ebrush(std::vector<std::string>); bool is_ebrush(std::vector<std::string>);
@ -25,12 +26,12 @@ std::vector<std::string> get_entity(std::ifstream &);
void parse_prefab(std::ifstream &, void parse_prefab(std::ifstream &,
std::ofstream &, std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &), void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
EntityConverter &); std::queue<std::vector<std::string> > &);
bool convertmap(const char * const, bool convertmap(const char * const,
const char * const, const char * const,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &), void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
const char * const); std::queue<std::vector<std::string> > &);
#define KEYWORD_ENTITY "entity" #define KEYWORD_ENTITY "entity"
#define KEYWORD_BRUSH "brush" #define KEYWORD_BRUSH "brush"

View File

@ -2,52 +2,105 @@
// Author: Michael Cameron // Author: Michael Cameron
// Email: chronokun@hotmail.com // Email: chronokun@hotmail.com
// //
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <cxxopts.hpp>
#include "oopless-parser.hpp" #include "oopless-parser.hpp"
#include "brushdef.hpp" #include "brushdef.hpp"
#define ARG_INPUT_SHORTALIAS "i"
#define ARG_OUTPUT_SHORTALIAS "o"
using namespace std; using namespace std;
int main(const int _kiArgC, const char** _kppcArgv) cxxopts::Options arguments(int ac, char ** av) {
{ vector<string> parg = {"input", "output"};
#define ARG_POS_BRUSHDEF 5 cxxopts::Options o("reflex2q3",
#define ARG_POS_ENTITYCONV 4 "A tool to convert Reflex maps to idTech 3 maps.");
// Minimum: program, input file, output file o.add_options()
if(_kiArgC < 3) ("h,help", "displays help")
{ ("i,input",
return(3); "required, input map file, from Reflex",
} cxxopts::value<string>(), "FILE")
// placeholder argument parsing until something more robust is decided upon ("o,output",
/* // optional arg #1: brush definition - gtkradiant or netradiant style "required, output map file, for idTech 3 or similar map editors",
void (*brushdef) (std::stringstream &, const std::vector<TPlanePoints> &); cxxopts::value<string>(), "FILE")
brushdef = ((string(_kppcArgv[ARG_POS_BRUSHDEF - 1])).compare("-gtk") == 0) ? ("gtk",
&brushdef_gtk: &brushdef_net; "optional, output in GTK Radiant's brush definition format")
*/ ("e,ent",
// optional arg #2: entity mapping file - enables entity conversion "optional, convert game entities given a table of equivalents",
const char * const entitymap = (_kiArgC >= ARG_POS_ENTITYCONV) ? cxxopts::value<string>(), "FILE")
_kppcArgv[ARG_POS_ENTITYCONV - 1]: NULL; ;
o.parse_positional(parg);
o.parse(ac, av);
if (o.count("help")) {
cout << o.help() << endl;
}
if (o.count(ARG_INPUT_SHORTALIAS) < 1 ||
o.count(ARG_OUTPUT_SHORTALIAS) < 1) {
cerr << "error: no input or output file given" << endl;
exit(EXIT_FAILURE);
}
return o;
}
bool status; bool convert_worldspawn(const cxxopts::Options &o,
queue<vector<string> > &q) {
//ease of reading
typedef void (*brushdef) (std::stringstream &,
const std::vector<TPlanePoints> &);
bool is_ok = false;
brushdef fn = &brushdef_net;
if (o.count("gtk")) {
fn = &brushdef_gtk;
}
try { try {
status = convertmap(_kppcArgv[1], _kppcArgv[2], &brushdef_net, entitymap); is_ok = convertmap(
o[ARG_INPUT_SHORTALIAS].as<string>().c_str(), // in file
o[ARG_OUTPUT_SHORTALIAS].as<string>().c_str(), // out file
fn, // brush definition
q); // queue of entities
} catch (exception &e) { } catch (exception &e) {
cout << e.what() << endl; cout << e.what() << endl;
cout << "exception encountered while converting map geometry"
" - unrecoverable failure" << endl;
} }
if (status) { if (is_ok) {
cout << "Successfully converted map " cout << "Successfully converted map "
<< _kppcArgv[1] << o[ARG_INPUT_SHORTALIAS].as<string>()
<< " to " << " to "
<< _kppcArgv[2] << o[ARG_OUTPUT_SHORTALIAS].as<string>()
<< endl; << endl;
} else { } else {
cout << "Failed to convert map." << endl; cout << "Failed to convert map." << endl;
} }
return is_ok;
}
// wip: current a print out of all entities
bool convert_entities(const cxxopts::Options &o, queue<vector<string> > &q) {
vector<string> entity;
do {
entity = q.front();
for (const string &s : entity) {
cout << s << endl;
}
q.pop();
} while (!q.empty());
return true;
}
int main(int argc, char** argv)
{
string entityfile;
cxxopts::Options p = arguments(argc, argv);
queue<vector<string> > entities;
bool is_ok = convert_worldspawn(p, entities);
convert_entities(p, entities);
return(0); return(0);
} }