Performance improvements for dealing with json

This commit is contained in:
Deukhoofd 2021-08-28 15:56:08 +02:00
parent 9dafa51e9d
commit 01b2f29db6
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 39 additions and 36 deletions

View File

@ -5,10 +5,11 @@
using json = nlohmann::json;
#define GET(o, objectKey, key) \
auto _##objectKey = o[#objectKey]; \
auto& _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with value '" << key << "' in file '" \
<< path << "'\n"; \
auto errorKeyFunc = [=]() { return key; }; \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() << path \
<< "'\n"; \
return nullptr; \
}
@ -45,7 +46,7 @@ PkmnLib::Library::ItemLibrary* BuildItems::Build(const std::string& path) {
if (i.key().starts_with("$")) {
continue;
}
auto val = i.value();
auto& val = i.value();
GET(val, name, i);
GET(val, itemType, i);
GET(val, flags, i);
@ -57,15 +58,15 @@ PkmnLib::Library::ItemLibrary* BuildItems::Build(const std::string& path) {
return nullptr;
auto flags = std::unordered_set<uint>();
for (auto flagIndex : _flags.items()) {
for (auto& flagIndex : _flags.items()) {
flags.insert(ArbUt::StringView(flagIndex.value().get<std::string>().c_str()));
}
CreatureLib::Library::SecondaryEffect* effect = nullptr;
auto effectJson = val["effect"];
auto& effectJson = val["effect"];
if (effectJson != nullptr) {
auto effectName = effectJson["name"];
auto parametersJson = effectJson["parameters"];
auto& effectName = effectJson["name"];
auto& parametersJson = effectJson["parameters"];
ArbUt::List<CreatureLib::Library::EffectParameter*> parameters;
if (parametersJson != nullptr) {
for (auto& kv : parametersJson.items()) {

View File

@ -5,9 +5,10 @@
using json = nlohmann::json;
#define GET(o, objectKey, key) \
auto _##objectKey = o[#objectKey]; \
auto& _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with value '" << key << "' in file '" \
auto errorKeyFunc = [=]() { return key; }; \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() \
<< path << "'\n"; \
return nullptr; \
}
@ -34,7 +35,7 @@ PkmnLib::Library::MoveLibrary* BuildMoves::Build(const std::string& path,
json j;
fileStream >> j;
for (const auto& i : j["data"].items()) {
auto val = i.value();
auto& val = i.value();
GET(val, name, i);
GET(val, type, i);
GET(val, power, i);
@ -57,15 +58,15 @@ PkmnLib::Library::MoveLibrary* BuildMoves::Build(const std::string& path,
return nullptr;
}
auto flags = std::unordered_set<uint32_t>();
for (auto flagIndex : _flags.items()) {
for (auto& flagIndex : _flags.items()) {
flags.insert(ArbUt::StringView(flagIndex.value().get<std::string>().c_str()));
}
CreatureLib::Library::SecondaryEffect* effect = nullptr;
auto jsonEffect = val["effect"];
auto& jsonEffect = val["effect"];
if (jsonEffect != nullptr) {
auto name = jsonEffect["name"];
auto chanceJson = jsonEffect["chance"];
auto parametersJson = jsonEffect["parameters"];
auto& name = jsonEffect["name"];
auto& chanceJson = jsonEffect["chance"];
auto& parametersJson = jsonEffect["parameters"];
if (name != nullptr) {
ArbUt::List<CreatureLib::Library::EffectParameter*> parameters;
auto chance = -1.0f;

View File

@ -3,10 +3,11 @@
#include <iostream>
#define GET(o, objectKey, key) \
auto _##objectKey = o[#objectKey]; \
auto& _##objectKey = o[#objectKey]; \
if (_##objectKey.is_null()) { \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << key << "' in file '" \
<< path << "'\n"; \
auto errorKeyFunc = [=]() { return key; }; \
std::cout << "Failed to retrieve key '" << #objectKey << "' for object with key '" << errorKeyFunc() \
<< "' in file '" << path << "'\n"; \
return nullptr; \
}
@ -22,11 +23,11 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
json j;
fileStream >> j;
for (json::iterator it = j.begin(); it != j.end(); ++it) {
for (const auto& it : j.items()) {
if (it.key().starts_with("$")) {
continue;
}
auto val = it.value();
auto& val = it.value();
GET(val, id, it.key());
GET(val, species, it.key());
GET(val, genderRatio, it.key());
@ -47,7 +48,7 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
eggGroups.Append(eg.get<std::string>().c_str());
}
auto defaultForme = _formes["default"];
auto& defaultForme = _formes["default"];
if (!defaultForme.is_null()) {
auto forme = BuildForme("default", defaultForme, it.key(), path, types, moveLibrary);
species = new PkmnLib::Library::PokemonSpecies(
@ -57,7 +58,7 @@ PkmnLib::Library::SpeciesLibrary* BuildSpecies::BuildLibrary(const std::string&
_baseHappiness.get<uint8_t>(), eggGroups);
}
for (json::iterator formeIt = _formes.begin(); formeIt != _formes.end(); ++formeIt) {
for (const auto& formeIt : _formes.items()) {
if (formeIt.key() == "default") {
continue;
}
@ -99,15 +100,15 @@ const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string
const std::string& baseKeyName, const std::string& path,
const CreatureLib::Library::TypeLibrary* typeLibrary,
const PkmnLib::Library::MoveLibrary* moveLibrary) {
GET(forme, abilities, baseKeyName << " -> " << name);
GET(forme, hiddenAbilities, baseKeyName << " -> " << name);
GET(forme, baseStats, baseKeyName << " -> " << name);
GET(forme, evReward, baseKeyName << " -> " << name);
GET(forme, types, baseKeyName << " -> " << name);
GET(forme, height, baseKeyName << " -> " << name);
GET(forme, weight, baseKeyName << " -> " << name);
GET(forme, baseExp, baseKeyName << " -> " << name);
GET(forme, moves, baseKeyName << " -> " << name);
GET(forme, abilities, baseKeyName + " -> " + name);
GET(forme, hiddenAbilities, baseKeyName + " -> " + name);
GET(forme, baseStats, baseKeyName + " -> " + name);
GET(forme, evReward, baseKeyName + " -> " + name);
GET(forme, types, baseKeyName + " -> " + name);
GET(forme, height, baseKeyName + " -> " + name);
GET(forme, weight, baseKeyName + " -> " + name);
GET(forme, baseExp, baseKeyName + " -> " + name);
GET(forme, moves, baseKeyName + " -> " + name);
auto typeStrings = _types.get<std::vector<std::string>>();
auto types = ArbUt::List<uint8_t>(typeStrings.size());
@ -121,15 +122,15 @@ const PkmnLib::Library::PokemonForme* BuildSpecies::BuildForme(const std::string
for (size_t i = 0; i < abilityStrings.size(); i++) {
abilities.Append(ArbUt::StringView(abilityStrings[i].c_str()));
}
auto hiddenAbilityStrings = _abilities.get<std::vector<std::string>>();
auto hiddenAbilityStrings = _hiddenAbilities.get<std::vector<std::string>>();
auto hiddenAbilities = ArbUt::List<ArbUt::StringView>(hiddenAbilityStrings.size());
for (size_t i = 0; i < hiddenAbilityStrings.size(); i++) {
hiddenAbilities.Append(ArbUt::StringView(abilityStrings[i].c_str()));
hiddenAbilities.Append(ArbUt::StringView(hiddenAbilityStrings[i].c_str()));
}
auto moves = new PkmnLib::Library::LearnableMoves(100);
auto movesJson = forme["moves"];
auto levelMovesJson = movesJson["levelMoves"];
auto& movesJson = forme["moves"];
auto& levelMovesJson = movesJson["levelMoves"];
for (auto& levelMoveObj : levelMovesJson) {
auto levelMoveName = levelMoveObj["name"].get<std::string>();
auto level = levelMoveObj["level"].get<u8>();