Templated parse entity

This commit is contained in:
2017-07-18 01:28:15 -07:00
parent 6ef6745d29
commit 0993b17333
2 changed files with 50 additions and 42 deletions

View File

@ -6,6 +6,7 @@
using namespace std; using namespace std;
bool is_ebrush(const std::vector<std::string> &input) { bool is_ebrush(const std::vector<std::string> &input) {
using namespace std;
#define KEYWORD_ENTBRUSH1 "type Teleporter" #define KEYWORD_ENTBRUSH1 "type Teleporter"
#define KEYWORD_ENTBRUSH2 "type JumpPad" #define KEYWORD_ENTBRUSH2 "type JumpPad"
#define KEYWORD_ENTBRUSH3 "type RaceStart" #define KEYWORD_ENTBRUSH3 "type RaceStart"
@ -21,6 +22,25 @@ bool is_ebrush(const std::vector<std::string> &input) {
return false; return false;
} }
vector<string> extract_brush(const vector<string> &entity) {
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> output(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), entity.end(), output.begin());
}
return output;
}
// place holding functions to test proper control flow. // place holding functions to test proper control flow.
bool write(const struct TBrush &geometry, bool write(const struct TBrush &geometry,
ofstream &fout, ofstream &fout,
@ -62,20 +82,7 @@ void write(const vector<string> &entity,
2. Searches through and splits the vector. 2. Searches through and splits the vector.
3. Recombines the brush vector. 3. Recombines the brush vector.
(4. Sends the vector to parse the brush through again.)*/ (4. Sends the vector to parse the brush through again.)*/
bool brushent = false; vector<string> brush = extract_brush(entity);
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), entity.end(), brush.begin());
}
vector<string> converted = e.convert(entity); vector<string> converted = e.convert(entity);
if (converted.empty()) { if (converted.empty()) {
return; return;
@ -84,7 +91,7 @@ void write(const vector<string> &entity,
for (const string &s : converted) { for (const string &s : converted) {
fo << s; fo << s;
} }
if (brushent) { if (brush.size() > 0) {
stringstream ss; stringstream ss;
string line; string line;
copy(brush.begin(), brush.end(), ostream_iterator<string>(ss, "\n")); copy(brush.begin(), brush.end(), ostream_iterator<string>(ss, "\n"));
@ -94,28 +101,6 @@ void write(const vector<string> &entity,
fo << "}" << endl; fo << "}" << endl;
} }
// entities will vary widely depending on their type and available flags.
// for now, acquire entity data, and handle their conversion in the write methods.
vector<string> get_entity(ifstream &f) {
vector<string> output;
string line;
unsigned int pos = f.tellg();
while (getline(f, line)) {
// stop when
// entity or a prefab keyword
// brush keyword and the entity is not of the type teleporter or jumppad.
if ((line.find(KEYWORD_ENTITY) != string::npos ||
line.find(KEYWORD_PREFAB) != string::npos) || (
line.find(KEYWORD_BRUSH) != string::npos && !is_ebrush(output))) {
f.seekg(pos);
return output;
} else {
output.push_back(line);
}
pos = f.tellg();
}
}
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> &),
@ -125,7 +110,7 @@ void parse_prefab(ifstream &f,
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) {
entities.push_back(get_entity(f)); entities.push_back(get_entity<ifstream>(f));
} }
} }
@ -153,7 +138,7 @@ bool convertmap(const char * const infile,
line.find(KEYWORD_GLOBAL) != string::npos) { line.find(KEYWORD_GLOBAL) != string::npos) {
parse_prefab(fin, fout, brushdef, entities); parse_prefab(fin, fout, brushdef, entities);
} else if (line.find(KEYWORD_ENTITY) != string::npos) { } else if (line.find(KEYWORD_ENTITY) != string::npos) {
entities.push_back(get_entity(fin)); entities.push_back(get_entity<ifstream>(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

@ -12,6 +12,8 @@
bool is_ebrush(const std::vector<std::string> &); bool is_ebrush(const std::vector<std::string> &);
std::vector<std::string> extract_brush(const std::vector<std::string> &);
struct TBrush override_textures(struct TBrush, const char*); struct TBrush override_textures(struct TBrush, const char*);
bool write(const struct TBrush &, bool write(const struct TBrush &,
@ -23,8 +25,6 @@ void write(const std::vector<std::string> &,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &), void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
EntityConverter &); EntityConverter &);
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> &),
@ -35,6 +35,7 @@ bool convertmap(const char * const,
void (*f) (std::stringstream &, const std::vector<TPlanePoints> &), void (*f) (std::stringstream &, const std::vector<TPlanePoints> &),
std::vector<std::vector<std::string> > &); std::vector<std::vector<std::string> > &);
#define KEYWORD_ENTITY "entity" #define KEYWORD_ENTITY "entity"
#define KEYWORD_BRUSH "brush" #define KEYWORD_BRUSH "brush"
#define KEYWORD_VERTICES "vertices" #define KEYWORD_VERTICES "vertices"
@ -42,6 +43,28 @@ bool convertmap(const char * const,
#define KEYWORD_GLOBAL "global" #define KEYWORD_GLOBAL "global"
#define KEYWORD_PREFAB "prefab" #define KEYWORD_PREFAB "prefab"
template <class STREAMOBJ>
std::vector<std::string> get_entity(STREAMOBJ &f) {
using namespace std;
vector<string> output;
string line;
unsigned int pos = f.tellg();
while (getline(f, line)) {
// stop when
// entity or a prefab keyword
// brush keyword and the entity is not of the type teleporter or jumppad.
if ((line.find(KEYWORD_ENTITY) != string::npos ||
line.find(KEYWORD_PREFAB) != string::npos) || (
line.find(KEYWORD_BRUSH) != string::npos && !is_ebrush(output))) {
f.seekg(pos);
return output;
} else {
output.push_back(line);
}
pos = f.tellg();
}
}
template <class STREAMOBJ> template <class STREAMOBJ>
std::vector<Eigen::Vector3f> parse_vertices(STREAMOBJ &f) { std::vector<Eigen::Vector3f> parse_vertices(STREAMOBJ &f) {
using namespace std; using namespace std;