From 0e214e595546453c55804d0b27dbe38f892a2a2b Mon Sep 17 00:00:00 2001 From: <> Date: Sat, 17 Jun 2017 03:34:19 -0700 Subject: [PATCH] Integrated entity converter; missing brush component. --- ReflexToQ3/includes/oopless-parser.cpp | 65 +++++++++++++++++++++----- ReflexToQ3/includes/oopless-parser.hpp | 28 ++++++----- ReflexToQ3/main.cpp | 17 ++++--- 3 files changed, 82 insertions(+), 28 deletions(-) diff --git a/ReflexToQ3/includes/oopless-parser.cpp b/ReflexToQ3/includes/oopless-parser.cpp index 2377b38..54b0abb 100644 --- a/ReflexToQ3/includes/oopless-parser.cpp +++ b/ReflexToQ3/includes/oopless-parser.cpp @@ -1,13 +1,11 @@ - #include "oopless-parser.hpp" #include "brushdef.hpp" -#include "EntityConverter.hpp" #include #include +#include using namespace std; bool is_ebrush(std::vector input) { -// defines are only used in this function. #define KEYWORD_ENTBRUSH1 "type Teleporter" #define KEYWORD_ENTBRUSH2 "type JumpPad" for (const string &str : input) { @@ -38,8 +36,46 @@ bool write(const struct TBrush &geometry, return ok; } -void write(const vector &entity, ofstream &fo) { - ; +void write(const vector &entity, + ofstream &fo, + void (*b) (stringstream &, const vector &), + EntityConverter &e) { +/* Likely inefficient. + Current process for the entities: + 1. It acquires the lines. + 2. Searches through and splits the vector. + 3. Recombines the brush vector. + (4. Sends the vector to parse the brush through again.)*/ + bool brushent = false; + unsigned int i; + for (i = 0; i < entity.size(); ++i) { + if (entity[i].find(KEYWORD_BRUSH) != string::npos) { + brushent = true; + break; + } + } + vector brush(entity.size() - i); + if (brushent) { + // entity.begin() + i = vector that begins with KEYWORD_BRUSH + // parsing functions will expect only the contents underneath the keyword. + move((entity.begin() + (i + 1)), entity.end(), brush.begin()); + } + vector converted = e.convert(entity); + if (converted.empty()) { + return; + } + fo << "{" << endl; + for (const string &s : converted) { + fo << s; + } + if (brushent) { + stringstream ss; + string line; + copy(brush.begin(), brush.end(), ostream_iterator(ss, "\n")); + getline(ss, line); + write(parse_brush(ss), fo, b); + } + fo << "}" << endl; } // entities will vary widely depending on their type and available flags. @@ -66,26 +102,33 @@ vector get_entity(ifstream &f) { void parse_prefab(ifstream &f, ofstream &out, - void (*b) (stringstream &, const vector &)) { + void (*b) (stringstream &, const vector &), + EntityConverter &e) { string l; getline(f, l); if (l.find(KEYWORD_BRUSH) != string::npos) { write(parse_brush(f), out, b); } else if (l.find(KEYWORD_ENTITY) != string::npos) { - write(get_entity(f), out); + write(get_entity(f), out, b, e); } } -// note: can't use a single function pointer for variadic overloaded write function? bool convertmap(const char * const infile, const char * const outfile, - void (*brushdef) (stringstream &, const vector &)) { + void (*brushdef) (stringstream &, const vector &), + const char * const entfile) { ifstream fin; fin.open(infile); if (!fin.good()){ cerr << "error: failed to open input file" << endl; return false; } + EntityConverter ec; + if (entfile != NULL) { + string ef(entfile); + string in(infile); + ec = EntityConverter(ef, in); + } ofstream fout; fout.open(outfile); if (!fout.good()) { @@ -98,9 +141,9 @@ bool convertmap(const char * const infile, while (getline(fin, line)) { if (line.find(KEYWORD_PREFAB) != string::npos || line.find(KEYWORD_GLOBAL) != string::npos) { - parse_prefab(fin, fout, brushdef); + parse_prefab(fin, fout, brushdef, ec); } else if (line.find(KEYWORD_ENTITY) != string::npos) { - write(get_entity(fin), fout); + write(get_entity(fin), fout, brushdef, ec); } else if (line.find(KEYWORD_BRUSH) != string::npos) { write(parse_brush(fin), fout, brushdef); } diff --git a/ReflexToQ3/includes/oopless-parser.hpp b/ReflexToQ3/includes/oopless-parser.hpp index ab1f6e3..3508f98 100644 --- a/ReflexToQ3/includes/oopless-parser.hpp +++ b/ReflexToQ3/includes/oopless-parser.hpp @@ -7,6 +7,7 @@ #include #include #include +#include "EntityConverter.hpp" bool is_ebrush(std::vector); @@ -14,17 +15,22 @@ bool write(const struct TBrush &, std::ofstream &, void (*f) (std::stringstream &, const std::vector &)); -void write(const std::vector &, std::ofstream &); +void write(const std::vector &, + std::ofstream &, + void (*f) (std::stringstream &, const std::vector &), + EntityConverter &); std::vector get_entity(std::ifstream &); void parse_prefab(std::ifstream &, std::ofstream &, - void (*f) (std::stringstream &, const std::vector &)); + void (*f) (std::stringstream &, const std::vector &), + EntityConverter &); bool convertmap(const char * const, const char * const, - void (*f) (std::stringstream &, const std::vector &)); + void (*f) (std::stringstream &, const std::vector &), + const char * const); #define KEYWORD_ENTITY "entity" #define KEYWORD_BRUSH "brush" @@ -33,8 +39,8 @@ bool convertmap(const char * const, #define KEYWORD_GLOBAL "global" #define KEYWORD_PREFAB "prefab" -template -std::vector parse_vertices(T &f) { +template +std::vector parse_vertices(STREAMOBJ &f) { using namespace std; #define FIRSTCH(x) x[0] vector output; @@ -69,8 +75,8 @@ using namespace std; return output; } -template -std::vector parse_face(T &f) { +template +std::vector parse_face(STREAMOBJ &f) { using namespace std; #define FIRSTCH(x) x[0] #define SECONDCH(x) x[1] @@ -132,18 +138,18 @@ using namespace std; return output; } -template -struct TBrush parse_brush(T &f) { +template +struct TBrush parse_brush(STREAMOBJ &f) { using namespace std; struct TBrush output; string l; getline(f, l); if (l.find(KEYWORD_VERTICES) != string::npos) { - output.m_Vertices = parse_vertices(f); + output.m_Vertices = parse_vertices(f); } getline(f, l); if (l.find(KEYWORD_FACES) != string::npos) { - output.m_Faces = parse_face(f); + output.m_Faces = parse_face(f); } return output; } diff --git a/ReflexToQ3/main.cpp b/ReflexToQ3/main.cpp index 4fa2d4c..2cd121a 100644 --- a/ReflexToQ3/main.cpp +++ b/ReflexToQ3/main.cpp @@ -24,17 +24,22 @@ int main(const int _kiArgC, const char** _kppcArgv) { return(3); } -/* // placeholder argument parsing until something more robust is decided upon - // optional arg #1: brush definition - gtkradiant or netradiant style + // placeholder argument parsing until something more robust is decided upon +/* // optional arg #1: brush definition - gtkradiant or netradiant style void (*brushdef) (std::stringstream &, const std::vector &); brushdef = ((string(_kppcArgv[ARG_POS_BRUSHDEF - 1])).compare("-gtk") == 0) ? &brushdef_gtk: &brushdef_net; - + */ // optional arg #2: entity mapping file - enables entity conversion - const char * const entitymap = (_kiArgC < ARG_POS_ENTITYCONV) ? - _kppcArgv[ARG_POS_ENTITYCONV - 1]: NULL; */ + const char * const entitymap = (_kiArgC >= ARG_POS_ENTITYCONV) ? + _kppcArgv[ARG_POS_ENTITYCONV - 1]: NULL; - bool status = convertmap(_kppcArgv[1], _kppcArgv[2], &brushdef_net); + bool status; + try { + status = convertmap(_kppcArgv[1], _kppcArgv[2], &brushdef_net, entitymap); + } catch (exception &e) { + cout << e.what() << endl; + } if (status) { cout << "Successfully converted map " << _kppcArgv[1]