2017-05-06 08:10:31 +05:30
|
|
|
#include "oopless-parser.hpp"
|
|
|
|
#include "brushdef.hpp"
|
|
|
|
#include <sstream>
|
|
|
|
#include <iostream>
|
2017-06-17 16:04:19 +05:30
|
|
|
#include <iterator>
|
2017-05-06 08:10:31 +05:30
|
|
|
using namespace std;
|
|
|
|
|
2017-06-15 19:39:38 +05:30
|
|
|
bool is_ebrush(std::vector<std::string> input) {
|
|
|
|
#define KEYWORD_ENTBRUSH1 "type Teleporter"
|
|
|
|
#define KEYWORD_ENTBRUSH2 "type JumpPad"
|
|
|
|
for (const string &str : input) {
|
2017-06-17 01:38:10 +05:30
|
|
|
if (str.find(KEYWORD_ENTBRUSH1) != string::npos ||
|
|
|
|
str.find(KEYWORD_ENTBRUSH2) != string::npos) {
|
2017-06-15 19:39:38 +05:30
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-06 08:10:31 +05:30
|
|
|
|
|
|
|
// place holding functions to test proper control flow.
|
|
|
|
bool write(const struct TBrush &geometry,
|
|
|
|
ofstream &fout,
|
|
|
|
void (*b) (stringstream &, const vector<TPlanePoints> &)) {
|
|
|
|
bool ok = true;
|
|
|
|
if (geometry.m_Vertices.size() < 1) {
|
|
|
|
cerr << "error: geometry has no vertices" << endl;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
if (geometry.m_Faces.size() < 1) {
|
|
|
|
cerr << "error: geometry has no faces"<< endl;
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
if (ok) {
|
|
|
|
fout << GetBrushString(geometry, b);
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2017-06-17 16:04:19 +05:30
|
|
|
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;
|
2017-05-06 08:10:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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)) {
|
2017-06-15 19:39:38 +05:30
|
|
|
// 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))) {
|
2017-05-06 08:10:31 +05:30
|
|
|
f.seekg(pos);
|
|
|
|
return output;
|
|
|
|
} else {
|
|
|
|
output.push_back(line);
|
|
|
|
}
|
|
|
|
pos = f.tellg();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_prefab(ifstream &f,
|
|
|
|
ofstream &out,
|
2017-06-17 16:04:19 +05:30
|
|
|
void (*b) (stringstream &, const vector<TPlanePoints> &),
|
|
|
|
EntityConverter &e) {
|
2017-05-06 08:10:31 +05:30
|
|
|
string l;
|
|
|
|
getline(f, l);
|
|
|
|
if (l.find(KEYWORD_BRUSH) != string::npos) {
|
2017-06-17 01:38:10 +05:30
|
|
|
write(parse_brush<ifstream>(f), out, b);
|
2017-05-06 08:10:31 +05:30
|
|
|
} else if (l.find(KEYWORD_ENTITY) != string::npos) {
|
2017-06-17 16:04:19 +05:30
|
|
|
write(get_entity(f), out, b, e);
|
2017-05-06 08:10:31 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool convertmap(const char * const infile,
|
|
|
|
const char * const outfile,
|
2017-06-17 16:04:19 +05:30
|
|
|
void (*brushdef) (stringstream &, const vector<TPlanePoints> &),
|
|
|
|
const char * const entfile) {
|
2017-05-06 08:10:31 +05:30
|
|
|
ifstream fin;
|
|
|
|
fin.open(infile);
|
|
|
|
if (!fin.good()){
|
|
|
|
cerr << "error: failed to open input file" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
2017-06-17 16:04:19 +05:30
|
|
|
EntityConverter ec;
|
|
|
|
if (entfile != NULL) {
|
|
|
|
string ef(entfile);
|
|
|
|
string in(infile);
|
|
|
|
ec = EntityConverter(ef, in);
|
|
|
|
}
|
2017-05-06 08:10:31 +05:30
|
|
|
ofstream fout;
|
|
|
|
fout.open(outfile);
|
|
|
|
if (!fout.good()) {
|
|
|
|
cerr << "error: failed to open output file" << endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fout << "{\n"
|
|
|
|
<< "\"classname\" \"worldspawn\"" << endl;
|
|
|
|
string line;
|
|
|
|
while (getline(fin, line)) {
|
|
|
|
if (line.find(KEYWORD_PREFAB) != string::npos ||
|
|
|
|
line.find(KEYWORD_GLOBAL) != string::npos) {
|
2017-06-17 16:04:19 +05:30
|
|
|
parse_prefab(fin, fout, brushdef, ec);
|
2017-05-06 08:10:31 +05:30
|
|
|
} else if (line.find(KEYWORD_ENTITY) != string::npos) {
|
2017-06-17 16:04:19 +05:30
|
|
|
write(get_entity(fin), fout, brushdef, ec);
|
2017-05-06 08:10:31 +05:30
|
|
|
} else if (line.find(KEYWORD_BRUSH) != string::npos) {
|
2017-06-17 01:38:10 +05:30
|
|
|
write(parse_brush<ifstream>(fin), fout, brushdef);
|
2017-05-06 08:10:31 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
fout << "}" << endl;
|
|
|
|
fin.close();
|
|
|
|
fout.close();
|
|
|
|
return true;
|
|
|
|
}
|