Skip to content

Commit

Permalink
handle @include FGD method (fixes #112)
Browse files Browse the repository at this point in the history
also keep classes in alphabetical order within the groups
  • Loading branch information
wootguy committed Apr 23, 2024
1 parent e11e70d commit e6e959f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/editor/Fgd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "util.h"
#include <set>
#include <fstream>
#include "globals.h"
#include <algorithm>

map<string, int> fgdKeyTypes{
{"integer", FGD_KEY_INTEGER},
Expand Down Expand Up @@ -49,6 +51,8 @@ void Fgd::merge(Fgd* other) {
classes.push_back(newClass);
classMap[className] = newClass;
}

sortClasses();
}

bool Fgd::parse() {
Expand Down Expand Up @@ -82,6 +86,24 @@ bool Fgd::parse() {
if (line.empty())
continue;

if (line.find("@include") == 0) {
string baseFgd = getValueInQuotes(line.substr(8));
string basePath = path.substr(0, path.find_last_of("/\\") + 1) + baseFgd;

if (g_parsed_fgds.count(basePath)) {
continue;
}
g_parsed_fgds.insert(basePath);

Fgd* tmp = new Fgd(basePath);
if (tmp->parse()) {
merge(tmp);
}

delete tmp;
continue;
}

if (line[0] == '@') {
if (bracketNestLevel) {
logf("ERROR: New FGD class definition starts before previous one ends (line %d)\n", lineNum);
Expand Down Expand Up @@ -122,6 +144,7 @@ bool Fgd::parse() {
processClassInheritance();
createEntGroups();
setSpawnflagNames();
sortClasses();
return true;
}

Expand Down Expand Up @@ -558,3 +581,14 @@ vector<string> Fgd::splitStringIgnoringQuotes(string s, string delimitter) {

return split;
}

bool sortFgdClasses(FgdClass* a, FgdClass* b) { return a->name < b->name; }

void Fgd::sortClasses() {
for (int i = 0; i < solidEntGroups.size(); i++) {
std::sort(solidEntGroups[i].classes.begin(), solidEntGroups[i].classes.end(), sortFgdClasses);
}
for (int i = 0; i < pointEntGroups.size(); i++) {
std::sort(pointEntGroups[i].classes.begin(), pointEntGroups[i].classes.end(), sortFgdClasses);
}
}
2 changes: 2 additions & 0 deletions src/editor/Fgd.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@ class Fgd {
string getValueInQuotes(string s);

vector<string> splitStringIgnoringQuotes(string s, string delimitter);

void sortClasses();
};
6 changes: 6 additions & 0 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,13 @@ void Renderer::loadSettings() {

void Renderer::loadFgds() {
Fgd* mergedFgd = NULL;

for (int i = 0; i < g_settings.fgdPaths.size(); i++) {
string path = g_settings.fgdPaths[i];

g_parsed_fgds.clear();
g_parsed_fgds.insert(path);

Fgd* tmp = new Fgd(g_settings.fgdPaths[i]);
if (!tmp->parse())
{
Expand Down
1 change: 1 addition & 0 deletions src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ AppSettings g_settings;
string g_config_dir = getConfigDir();
string g_settings_path = g_config_dir + "bspguy.cfg";
Renderer* g_app = NULL;
std::set<std::string> g_parsed_fgds;

MapLimits g_limits;
MapLimits g_engine_limits[ENGINE_TYPES];
4 changes: 4 additions & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <vector>
#include <string>
#include <mutex>
#include <set>
#include "AppSettings.h"

enum engine_types {
Expand Down Expand Up @@ -51,4 +52,7 @@ extern MapLimits g_engine_limits[ENGINE_TYPES];
extern std::string g_config_dir;
extern std::string g_settings_path;

// prevents infinite include loops
extern std::set<std::string> g_parsed_fgds;

extern int g_render_flags;

0 comments on commit e6e959f

Please sign in to comment.