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())
2017-03-25 17:18:31 -04:00
{
std::string model = entity["model"];
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();)
2017-03-25 17:18:31 -04:00
{
if (i->find("classname") != i->end())
2017-03-25 17:18:31 -04:00
{
std::string classname = (*i)["classname"];
if (Utils::String::StartsWith(classname, "trigger_"))
2017-03-25 17:18:31 -04:00
{
i = this->entities.erase(i);
continue;
}
}
++i;
}
}
2017-03-25 17:18:31 -04:00
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)
2017-03-25 17:18:31 -04:00
{
char character = buffer[i];
if (character == '{')
{
entity.clear();
}
switch (character)
{
case '{':
{
entity.clear();
break;
}
case '}':
2017-03-25 17:18:31 -04:00
{
this->entities.push_back(entity);
2017-03-25 17:18:31 -04:00
entity.clear();
break;
2017-03-25 17:18:31 -04:00
}
case '"':
2017-03-25 17:18:31 -04:00
{
if (parseState == PARSE_AWAIT_KEY)
2017-03-25 17:18:31 -04:00
{
key.clear();
parseState = PARSE_READ_KEY;
2017-03-25 17:18:31 -04:00
}
else if (parseState == PARSE_READ_KEY)
2017-03-25 17:18:31 -04:00
{
parseState = PARSE_AWAIT_VALUE;
2017-03-25 17:18:31 -04:00
}
else if (parseState == PARSE_AWAIT_VALUE)
2017-03-25 17:18:31 -04:00
{
value.clear();
parseState = PARSE_READ_VALUE;
2017-03-25 17:18:31 -04:00
}
else if (parseState == PARSE_READ_VALUE)
2017-03-25 17:18:31 -04:00
{
entity[Utils::String::ToLower(key)] = value;
parseState = PARSE_AWAIT_KEY;
}
else
{
throw std::runtime_error("Parsing error!");
2017-03-25 17:18:31 -04:00
}
break;
}
default:
{
if (parseState == PARSE_READ_KEY) key.push_back(character);
else if (parseState == PARSE_READ_VALUE) value.push_back(character);
break;
}
2017-03-25 17:18:31 -04:00
}
}
}
}