iw4x-client/src/Utils/Entities.cpp

170 lines
3.2 KiB
C++
Raw Normal View History

2017-03-25 17:18:31 -04:00
#include "STDInclude.hpp"
namespace Utils
{
std::string Entities::build()
{
std::string entityString;
for (auto& entity : this->entities)
{
entityString.append("{\n");
for (auto& property : entity)
{
entityString.push_back('"');
entityString.append(property.first);
entityString.append("\" \"");
entityString.append(property.second);
entityString.append("\"\n");
}
entityString.append("}\n");
}
return entityString;
}
std::vector<std::string> Entities::getModels()
{
std::vector<std::string> models;
for (auto& entity : this->entities)
{
if(entity.find("model") != entity.end())
{
std::string model = entity["model"];
2017-03-27 08:15:09 -04:00
if(!model.empty() && model[0] != '*' && model[0] != '?') // Skip brushmodels
2017-03-25 17:18:31 -04:00
{
if (std::find(models.begin(), models.end(), model) == models.end())
{
models.push_back(model);
}
}
}
}
return models;
}
void Entities::deleteTriggers()
{
for(auto i = this->entities.begin(); i != this->entities.end();)
{
if(i->find("classname") != i->end())
{
std::string classname = (*i)["classname"];
if(Utils::String::StartsWith(classname, "trigger_"))
{
i = this->entities.erase(i);
continue;
}
}
++i;
}
}
void Entities::convertTurrets()
{
for (auto& entity : this->entities)
{
if (entity.find("classname") != entity.end())
{
if (entity["classname"] == "misc_turret"s)
{
entity["weaponinfo"] = "turret_minigun_mp";
entity["model"] = "weapon_minigun";
}
}
}
}
void Entities::deleteWeapons(bool keepTurrets)
{
for (auto i = this->entities.begin(); i != this->entities.end();)
{
2017-03-30 11:56:06 -04:00
if (i->find("weaponinfo") != i->end() || (i->find("targetname") != i->end() && (*i)["targetname"] == "oldschool_pickup"s))
2017-03-25 17:18:31 -04:00
{
if (!keepTurrets || i->find("classname") == i->end() || (*i)["classname"] != "misc_turret"s)
{
i = this->entities.erase(i);
continue;
}
}
++i;
}
}
void Entities::parse(std::string buffer)
{
int parseState = 0;
std::string key;
std::string value;
std::unordered_map<std::string, std::string> entity;
for(unsigned int i = 0; i < buffer.size(); ++i)
{
char character = buffer[i];
if(character == '{')
{
entity.clear();
}
switch(character)
{
case '{':
{
entity.clear();
break;
}
case '}':
{
this->entities.push_back(entity);
entity.clear();
break;
}
case '"':
{
if (parseState == PARSE_AWAIT_KEY)
{
key.clear();
parseState = PARSE_READ_KEY;
}
else if (parseState == PARSE_READ_KEY)
{
parseState = PARSE_AWAIT_VALUE;
}
else if (parseState == PARSE_AWAIT_VALUE)
{
value.clear();
parseState = PARSE_READ_VALUE;
}
else if (parseState == PARSE_READ_VALUE)
{
entity[Utils::String::ToLower(key)] = value;
parseState = PARSE_AWAIT_KEY;
}
else
{
throw std::runtime_error("Parsing error!");
}
break;
}
default:
{
if(parseState == PARSE_READ_KEY) key.push_back(character);
else if (parseState == PARSE_READ_VALUE) value.push_back(character);
break;
}
}
}
}
}