Integrated entity converter; missing brush component.

This commit is contained in:
2017-06-17 03:34:19 -07:00
parent 29d8fd4c02
commit 0e214e5955
3 changed files with 82 additions and 28 deletions

View File

@ -1,13 +1,11 @@
#include "oopless-parser.hpp" #include "oopless-parser.hpp"
#include "brushdef.hpp" #include "brushdef.hpp"
#include "EntityConverter.hpp"
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <iterator>
using namespace std; using namespace std;
bool is_ebrush(std::vector<std::string> input) { bool is_ebrush(std::vector<std::string> input) {
// defines are only used in this function.
#define KEYWORD_ENTBRUSH1 "type Teleporter" #define KEYWORD_ENTBRUSH1 "type Teleporter"
#define KEYWORD_ENTBRUSH2 "type JumpPad" #define KEYWORD_ENTBRUSH2 "type JumpPad"
for (const string &str : input) { for (const string &str : input) {
@ -38,8 +36,46 @@ bool write(const struct TBrush &geometry,
return ok; return ok;
} }
void write(const vector<string> &entity, ofstream &fo) { void write(const vector<string> &entity,
; ofstream &fo,
void (*b) (stringstream &, const vector<TPlanePoints> &),
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<string> 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<string> 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<string>(ss, "\n"));
getline(ss, line);
write(parse_brush<stringstream>(ss), fo, b);
}
fo << "}" << endl;
} }
// entities will vary widely depending on their type and available flags. // entities will vary widely depending on their type and available flags.
@ -66,26 +102,33 @@ 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) {
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); 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, 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) {
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()) {
@ -98,9 +141,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); parse_prefab(fin, fout, brushdef, ec);
} else if (line.find(KEYWORD_ENTITY) != string::npos) { } 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) { } 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 "EntityConverter.hpp"
bool is_ebrush(std::vector<std::string>); bool is_ebrush(std::vector<std::string>);
@ -14,17 +15,22 @@ bool write(const struct TBrush &,
std::ofstream &, std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &)); void (*f) (std::stringstream &, const std::vector<TPlanePoints> &));
void write(const std::vector<std::string> &, std::ofstream &); void write(const std::vector<std::string> &,
std::ofstream &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
EntityConverter &);
std::vector<std::string> get_entity(std::ifstream &); 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 &);
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);
#define KEYWORD_ENTITY "entity" #define KEYWORD_ENTITY "entity"
#define KEYWORD_BRUSH "brush" #define KEYWORD_BRUSH "brush"
@ -33,8 +39,8 @@ bool convertmap(const char * const,
#define KEYWORD_GLOBAL "global" #define KEYWORD_GLOBAL "global"
#define KEYWORD_PREFAB "prefab" #define KEYWORD_PREFAB "prefab"
template <class T> template <class STREAMOBJ>
std::vector<Eigen::Vector3f> parse_vertices(T &f) { std::vector<Eigen::Vector3f> parse_vertices(STREAMOBJ &f) {
using namespace std; using namespace std;
#define FIRSTCH(x) x[0] #define FIRSTCH(x) x[0]
vector<Eigen::Vector3f> output; vector<Eigen::Vector3f> output;
@ -69,8 +75,8 @@ using namespace std;
return output; return output;
} }
template <class T> template <class STREAMOBJ>
std::vector<struct TFace> parse_face(T &f) { std::vector<struct TFace> parse_face(STREAMOBJ &f) {
using namespace std; using namespace std;
#define FIRSTCH(x) x[0] #define FIRSTCH(x) x[0]
#define SECONDCH(x) x[1] #define SECONDCH(x) x[1]
@ -132,18 +138,18 @@ using namespace std;
return output; return output;
} }
template <class T> template <class STREAMOBJ>
struct TBrush parse_brush(T &f) { struct TBrush parse_brush(STREAMOBJ &f) {
using namespace std; using namespace std;
struct TBrush output; struct TBrush output;
string l; string l;
getline(f, l); getline(f, l);
if (l.find(KEYWORD_VERTICES) != string::npos) { if (l.find(KEYWORD_VERTICES) != string::npos) {
output.m_Vertices = parse_vertices<T>(f); output.m_Vertices = parse_vertices<STREAMOBJ>(f);
} }
getline(f, l); getline(f, l);
if (l.find(KEYWORD_FACES) != string::npos) { if (l.find(KEYWORD_FACES) != string::npos) {
output.m_Faces = parse_face<T>(f); output.m_Faces = parse_face<STREAMOBJ>(f);
} }
return output; return output;
} }

View File

@ -24,17 +24,22 @@ int main(const int _kiArgC, const char** _kppcArgv)
{ {
return(3); return(3);
} }
/* // placeholder argument parsing until something more robust is decided upon // placeholder argument parsing until something more robust is decided upon
// optional arg #1: brush definition - gtkradiant or netradiant style /* // optional arg #1: brush definition - gtkradiant or netradiant style
void (*brushdef) (std::stringstream &, const std::vector<TPlanePoints> &); void (*brushdef) (std::stringstream &, const std::vector<TPlanePoints> &);
brushdef = ((string(_kppcArgv[ARG_POS_BRUSHDEF - 1])).compare("-gtk") == 0) ? brushdef = ((string(_kppcArgv[ARG_POS_BRUSHDEF - 1])).compare("-gtk") == 0) ?
&brushdef_gtk: &brushdef_net; &brushdef_gtk: &brushdef_net;
*/
// optional arg #2: entity mapping file - enables entity conversion // optional arg #2: entity mapping file - enables entity conversion
const char * const entitymap = (_kiArgC < ARG_POS_ENTITYCONV) ? const char * const entitymap = (_kiArgC >= ARG_POS_ENTITYCONV) ?
_kppcArgv[ARG_POS_ENTITYCONV - 1]: NULL; */ _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) { if (status) {
cout << "Successfully converted map " cout << "Successfully converted map "
<< _kppcArgv[1] << _kppcArgv[1]