BuildData/BuildAbilities.cpp

62 lines
2.8 KiB
C++

#include "BuildAbilities.hpp"
#include "json.hpp"
using json = nlohmann::json;
#define GET(o, objectKey, key) \
auto& _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
auto errorKeyFunc = [=]() { return key; }; \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() << path \
<< "'\n"; \
return nullptr; \
}
CreatureLib::Library::TalentLibrary* BuildAbilities::Build(const std::string& path) {
std::ifstream fileStream(path.c_str());
if (fileStream.fail()) {
std::cout << "Failed to load Abilities data file at '" << path << "'\n";
return nullptr;
}
auto lib = new CreatureLib::Library::TalentLibrary();
json j;
fileStream >> j;
for (const auto& i : j.items()) {
if (i.key().starts_with("$")) {
continue;
}
auto& val = i.value();
auto _effect = val["effect"];
auto _parameters = val["parameters"];
ArbUt::StringView effectName = ""_cnc;
if (!_effect.empty()) {
effectName = ArbUt::StringView(_effect.get<std::string>().c_str(), _effect.get<std::string>().length());
}
ArbUt::List<CreatureLib::Library::EffectParameter*> realParameters;
if (!_parameters.empty()) {
for (auto& kv : _parameters.items()) {
auto& p = kv.value();
auto t = p.type();
switch (t) {
case json::value_t::boolean:
realParameters.Append(new CreatureLib::Library::EffectParameter(p.get<bool>()));
break;
case json::value_t::number_integer:
case json::value_t::number_unsigned:
realParameters.Append(new CreatureLib::Library::EffectParameter(p.get<int64_t>()));
break;
case json::value_t::number_float:
realParameters.Append(new CreatureLib::Library::EffectParameter(p.get<float>()));
break;
default: continue;
}
}
}
auto key = ArbUt::StringView(i.key().c_str(), i.key().length());
lib->Insert(key, new CreatureLib::Library::Talent(key, effectName, realParameters));
}
return lib;
}