From 246f8178eb1a59126c6a6ff3d2e9a26f01281026 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 19 Apr 2020 12:26:27 +0200 Subject: [PATCH] Added hashmaps to ESSResoruceDB to easilz convert between ids and resource paths. --- database/ess_resource_db.cpp | 192 ++++++++++++++++++++++++++++ database/ess_resource_db.h | 59 +++++++-- database/ess_resource_db_map.cpp | 16 +++ database/ess_resource_db_static.cpp | 16 +++ 4 files changed, 274 insertions(+), 9 deletions(-) diff --git a/database/ess_resource_db.cpp b/database/ess_resource_db.cpp index 21a5cdd..be0765b 100644 --- a/database/ess_resource_db.cpp +++ b/database/ess_resource_db.cpp @@ -48,6 +48,158 @@ void ESSResourceDB::set_xp_data(const Ref &data) { _xp_data = data; } +void ESSResourceDB::add_entity_resource(Ref cls) { + if (!cls.is_valid()) + return; + + _entity_resources_id_to_path.set(cls->get_id(), cls->get_path()); + _entity_resources_path_to_id.set(cls->get_path(), cls->get_id()); +} + +void ESSResourceDB::add_entity_skill(Ref cls) { + if (!cls.is_valid()) + return; + + _entity_skill_id_to_path.set(cls->get_id(), cls->get_path()); + _entity_skill_path_to_id.set(cls->get_path(), cls->get_id()); +} + +void ESSResourceDB::add_entity_data(Ref cls) { + if (!cls.is_valid()) + return; + + _entity_data_id_to_path.set(cls->get_id(), cls->get_path()); + _entity_data_path_to_id.set(cls->get_path(), cls->get_id()); +} + +void ESSResourceDB::add_spell(Ref spell) { + if (!spell.is_valid()) + return; + + _spell_id_to_path.set(spell->get_id(), spell->get_path()); + _spell_path_to_id.set(spell->get_path(), spell->get_id()); +} + +void ESSResourceDB::add_aura(Ref aura) { + if (!aura.is_valid()) + return; + + _aura_id_to_path.set(aura->get_id(), aura->get_path()); + _aura_path_to_id.set(aura->get_path(), aura->get_id()); +} + +void ESSResourceDB::add_craft_recipe(Ref cda) { + if (!cda.is_valid()) + return; + + _craft_recipe_id_to_path.set(cda->get_id(), cda->get_path()); + _craft_recipe_path_to_id.set(cda->get_path(), cda->get_id()); +} + +void ESSResourceDB::add_item_template(Ref cda) { + if (!cda.is_valid()) + return; + + _item_template_id_to_path.set(cda->get_id(), cda->get_path()); + _item_template_path_to_id.set(cda->get_path(), cda->get_id()); +} + +void ESSResourceDB::add_entity_species_data(Ref cda) { + if (!cda.is_valid()) + return; + + _entity_species_id_to_path.set(cda->get_id(), cda->get_path()); + _entity_species_path_to_id.set(cda->get_path(), cda->get_id()); +} + +StringName ESSResourceDB::entity_resource_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_entity_resources_id_to_path.has(id), StringName()); + + return _entity_resources_id_to_path[id]; +} +int ESSResourceDB::entity_resource_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_entity_resources_path_to_id.has(path), 0); + + return _entity_resources_path_to_id[path]; +} + +StringName ESSResourceDB::entity_skill_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_entity_skill_id_to_path.has(id), StringName()); + + return _entity_skill_id_to_path[id]; +} +int ESSResourceDB::entity_skill_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_entity_skill_path_to_id.has(path), 0); + + return _entity_skill_path_to_id[path]; +} + +StringName ESSResourceDB::entity_data_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_entity_data_id_to_path.has(id), StringName()); + + return _entity_data_id_to_path[id]; +} +int ESSResourceDB::entity_data_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_entity_data_path_to_id.has(path), 0); + + return _entity_data_path_to_id[path]; +} + +StringName ESSResourceDB::spell_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_spell_id_to_path.has(id), StringName()); + + return _spell_id_to_path[id]; +} +int ESSResourceDB::spell_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_spell_path_to_id.has(path), 0); + + return _spell_path_to_id[path]; +} + +StringName ESSResourceDB::aura_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_aura_id_to_path.has(id), StringName()); + + return _aura_id_to_path[id]; +} +int ESSResourceDB::aura_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_aura_path_to_id.has(path), 0); + + return _aura_path_to_id[path]; +} + +StringName ESSResourceDB::craft_recipe_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_craft_recipe_id_to_path.has(id), StringName()); + + return _craft_recipe_id_to_path[id]; +} +int ESSResourceDB::craft_recipe_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_craft_recipe_path_to_id.has(path), 0); + + return _craft_recipe_path_to_id[path]; +} + +StringName ESSResourceDB::item_template_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_item_template_id_to_path.has(id), StringName()); + + return _item_template_id_to_path[id]; +} +int ESSResourceDB::item_template_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_item_template_path_to_id.has(path), 0); + + return _item_template_path_to_id[path]; +} + +StringName ESSResourceDB::entity_species_id_to_path(const int id) const { + ERR_FAIL_COND_V(!_entity_species_id_to_path.has(id), StringName()); + + return _entity_species_id_to_path[id]; +} +int ESSResourceDB::entity_species_path_to_id(const StringName &path) const { + ERR_FAIL_COND_V(!_entity_species_path_to_id.has(path), 0); + + return _entity_species_path_to_id[path]; +} + void ESSResourceDB::clear() { _xp_data.unref(); } @@ -102,6 +254,30 @@ ESSResourceDB::ESSResourceDB() { ESSResourceDB::~ESSResourceDB() { _xp_data.unref(); + + _entity_resources_path_to_id.clear(); + _entity_resources_id_to_path.clear(); + + _entity_skill_path_to_id.clear(); + _entity_skill_id_to_path.clear(); + + _entity_data_path_to_id.clear(); + _entity_data_id_to_path.clear(); + + _spell_path_to_id.clear(); + _spell_id_to_path.clear(); + + _aura_path_to_id.clear(); + _aura_id_to_path.clear(); + + _craft_recipe_path_to_id.clear(); + _craft_recipe_id_to_path.clear(); + + _item_template_path_to_id.clear(); + _item_template_id_to_path.clear(); + + _entity_species_path_to_id.clear(); + _entity_species_id_to_path.clear(); } void ESSResourceDB::_bind_methods() { @@ -124,6 +300,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_entity_resource_count"), &ESSResourceDB::get_entity_resource_count); ClassDB::bind_method(D_METHOD("get_entity_resources"), &ESSResourceDB::get_entity_resources); ClassDB::bind_method(D_METHOD("set_entity_resources", "recipe"), &ESSResourceDB::set_entity_resources); + ClassDB::bind_method(D_METHOD("entity_resource_id_to_path", "id"), &ESSResourceDB::entity_resource_id_to_path); + ClassDB::bind_method(D_METHOD("entity_resource_path_to_id", "path"), &ESSResourceDB::entity_resource_path_to_id); //EntitySkills ClassDB::bind_method(D_METHOD("add_entity_skill", "cls"), &ESSResourceDB::add_entity_skill); @@ -132,6 +310,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_entity_skill_count"), &ESSResourceDB::get_entity_skill_count); ClassDB::bind_method(D_METHOD("get_entity_skills"), &ESSResourceDB::get_entity_skills); ClassDB::bind_method(D_METHOD("set_entity_skills", "recipe"), &ESSResourceDB::set_entity_skills); + ClassDB::bind_method(D_METHOD("entity_skill_id_to_path", "id"), &ESSResourceDB::entity_skill_id_to_path); + ClassDB::bind_method(D_METHOD("entity_skill_path_to_id", "path"), &ESSResourceDB::entity_skill_path_to_id); //EntityData ClassDB::bind_method(D_METHOD("add_entity_data", "cls"), &ESSResourceDB::add_entity_data); @@ -140,6 +320,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_entity_data_count"), &ESSResourceDB::get_entity_data_count); ClassDB::bind_method(D_METHOD("get_entity_datas"), &ESSResourceDB::get_entity_datas); ClassDB::bind_method(D_METHOD("set_entity_datas", "recipe"), &ESSResourceDB::set_entity_skills); + ClassDB::bind_method(D_METHOD("entity_data_id_to_path", "id"), &ESSResourceDB::entity_data_id_to_path); + ClassDB::bind_method(D_METHOD("entity_data_path_to_id", "path"), &ESSResourceDB::entity_data_path_to_id); //Spell ClassDB::bind_method(D_METHOD("add_spell", "spell"), &ESSResourceDB::add_spell); @@ -148,6 +330,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_spell_count"), &ESSResourceDB::get_spell_count); ClassDB::bind_method(D_METHOD("get_spells"), &ESSResourceDB::get_spells); ClassDB::bind_method(D_METHOD("set_spells", "recipe"), &ESSResourceDB::set_spells); + ClassDB::bind_method(D_METHOD("spell_id_to_path", "id"), &ESSResourceDB::spell_id_to_path); + ClassDB::bind_method(D_METHOD("spell_path_to_id", "path"), &ESSResourceDB::spell_path_to_id); //Aura ClassDB::bind_method(D_METHOD("add_aura", "spell"), &ESSResourceDB::add_aura); @@ -156,6 +340,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_aura_count"), &ESSResourceDB::get_aura_count); ClassDB::bind_method(D_METHOD("get_auras"), &ESSResourceDB::get_auras); ClassDB::bind_method(D_METHOD("set_auras", "recipe"), &ESSResourceDB::set_auras); + ClassDB::bind_method(D_METHOD("aura_id_to_path", "id"), &ESSResourceDB::aura_id_to_path); + ClassDB::bind_method(D_METHOD("aura_path_to_id", "path"), &ESSResourceDB::aura_path_to_id); //Craft Data ClassDB::bind_method(D_METHOD("add_craft_recipe", "craft_recipe"), &ESSResourceDB::add_craft_recipe); @@ -164,6 +350,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_craft_recipe_count"), &ESSResourceDB::get_craft_recipe_count); ClassDB::bind_method(D_METHOD("get_craft_recipes"), &ESSResourceDB::get_craft_recipes); ClassDB::bind_method(D_METHOD("set_craft_recipes", "recipe"), &ESSResourceDB::set_craft_recipes); + ClassDB::bind_method(D_METHOD("craft_recipe_id_to_path", "id"), &ESSResourceDB::craft_recipe_id_to_path); + ClassDB::bind_method(D_METHOD("craft_recipe_path_to_id", "path"), &ESSResourceDB::craft_recipe_path_to_id); //Item Templates ClassDB::bind_method(D_METHOD("add_item_template", "item_template"), &ESSResourceDB::add_item_template); @@ -172,6 +360,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_item_template_count"), &ESSResourceDB::get_item_template_count); ClassDB::bind_method(D_METHOD("get_item_templates"), &ESSResourceDB::get_item_templates); ClassDB::bind_method(D_METHOD("set_item_templates", "recipe"), &ESSResourceDB::set_item_templates); + ClassDB::bind_method(D_METHOD("item_template_id_to_path", "id"), &ESSResourceDB::item_template_id_to_path); + ClassDB::bind_method(D_METHOD("item_template_path_to_id", "path"), &ESSResourceDB::item_template_path_to_id); //Player Character Data ClassDB::bind_method(D_METHOD("add_entity_species_data", "pcd"), &ESSResourceDB::add_entity_species_data); @@ -180,6 +370,8 @@ void ESSResourceDB::_bind_methods() { ClassDB::bind_method(D_METHOD("get_entity_species_data_count"), &ESSResourceDB::get_entity_species_data_count); ClassDB::bind_method(D_METHOD("get_entity_species_datas"), &ESSResourceDB::get_entity_species_datas); ClassDB::bind_method(D_METHOD("set_entity_species_datas", "recipe"), &ESSResourceDB::set_entity_species_datas); + ClassDB::bind_method(D_METHOD("entity_species_id_to_path", "id"), &ESSResourceDB::entity_species_id_to_path); + ClassDB::bind_method(D_METHOD("entity_species_path_to_id", "path"), &ESSResourceDB::entity_species_path_to_id); ClassDB::bind_method(D_METHOD("clear"), &ESSResourceDB::clear); ClassDB::bind_method(D_METHOD("add_entity_resource_db", "other"), &ESSResourceDB::add_entity_resource_db); diff --git a/database/ess_resource_db.h b/database/ess_resource_db.h index 62149a7..9544598 100644 --- a/database/ess_resource_db.h +++ b/database/ess_resource_db.h @@ -36,6 +36,8 @@ SOFTWARE. #include "../data/entities/xp_data.h" +#include "core/hash_map.h" + class Aura; class Spell; class EntityData; @@ -60,58 +62,74 @@ public: virtual Ref get_entity_resource(int class_id) = 0; virtual Ref get_entity_resource_index(int index) = 0; virtual int get_entity_resource_count() = 0; - virtual void add_entity_resource(Ref cls) = 0; + virtual void add_entity_resource(Ref cls); virtual Vector get_entity_resources() const = 0; virtual void set_entity_resources(const Vector &data) = 0; + StringName entity_resource_id_to_path(const int id) const; + int entity_resource_path_to_id(const StringName &path) const; virtual Ref get_entity_skill(int class_id) = 0; virtual Ref get_entity_skill_index(int index) = 0; virtual int get_entity_skill_count() = 0; - virtual void add_entity_skill(Ref cls) = 0; + virtual void add_entity_skill(Ref cls); virtual Vector get_entity_skills() const = 0; virtual void set_entity_skills(const Vector &data) = 0; + StringName entity_skill_id_to_path(const int id) const; + int entity_skill_path_to_id(const StringName &path) const; virtual Ref get_entity_data(int class_id) = 0; virtual Ref get_entity_data_index(int index) = 0; virtual int get_entity_data_count() = 0; - virtual void add_entity_data(Ref cls) = 0; + virtual void add_entity_data(Ref cls); virtual Vector get_entity_datas() const = 0; virtual void set_entity_datas(const Vector &data) = 0; + StringName entity_data_id_to_path(const int id) const; + int entity_data_path_to_id(const StringName &path) const; virtual Ref get_spell(int spell_id) = 0; virtual Ref get_spell_index(int index) = 0; virtual int get_spell_count() = 0; - virtual void add_spell(Ref spell) = 0; + virtual void add_spell(Ref spell); virtual Vector get_spells() const = 0; virtual void set_spells(const Vector &data) = 0; + StringName spell_id_to_path(const int id) const; + int spell_path_to_id(const StringName &path) const; virtual Ref get_aura(int aura_id) = 0; virtual Ref get_aura_index(int index) = 0; virtual int get_aura_count() = 0; - virtual void add_aura(Ref aura) = 0; + virtual void add_aura(Ref aura); virtual Vector get_auras() const = 0; virtual void set_auras(const Vector &data) = 0; + StringName aura_id_to_path(const int id) const; + int aura_path_to_id(const StringName &path) const; virtual Ref get_craft_recipe(int craft_id) = 0; virtual Ref get_craft_recipe_index(int index) = 0; virtual int get_craft_recipe_count() = 0; - virtual void add_craft_recipe(Ref aura) = 0; + virtual void add_craft_recipe(Ref aura); virtual Vector get_craft_recipes() const = 0; virtual void set_craft_recipes(const Vector &data) = 0; + StringName craft_recipe_id_to_path(const int id) const; + int craft_recipe_path_to_id(const StringName &path) const; - virtual void add_item_template(Ref aura) = 0; + virtual void add_item_template(Ref aura); virtual Ref get_item_template(int item_id) = 0; virtual Ref get_item_template_index(int index) = 0; virtual int get_item_template_count() = 0; virtual Vector get_item_templates() const = 0; virtual void set_item_templates(const Vector &data) = 0; + StringName item_template_id_to_path(const int id) const; + int item_template_path_to_id(const StringName &path) const; - virtual void add_entity_species_data(Ref aura) = 0; + virtual void add_entity_species_data(Ref aura); virtual Ref get_entity_species_data(int item_id) = 0; virtual Ref get_entity_species_data_index(int index) = 0; virtual int get_entity_species_data_count() = 0; virtual Vector get_entity_species_datas() const = 0; virtual void set_entity_species_datas(const Vector &data) = 0; + StringName entity_species_id_to_path(const int id) const; + int entity_species_path_to_id(const StringName &path) const; virtual void clear(); @@ -125,10 +143,33 @@ public: protected: static void _bind_methods(); -private: Ref _xp_data; Ref _armor_type_skills[ItemEnums::ARMOR_TYPE_MAX]; + + HashMap _entity_resources_id_to_path; + HashMap _entity_resources_path_to_id; + + HashMap _entity_skill_id_to_path; + HashMap _entity_skill_path_to_id; + + HashMap _entity_data_id_to_path; + HashMap _entity_data_path_to_id; + + HashMap _spell_id_to_path; + HashMap _spell_path_to_id; + + HashMap _aura_id_to_path; + HashMap _aura_path_to_id; + + HashMap _craft_recipe_id_to_path; + HashMap _craft_recipe_path_to_id; + + HashMap _item_template_id_to_path; + HashMap _item_template_path_to_id; + + HashMap _entity_species_id_to_path; + HashMap _entity_species_path_to_id; }; #endif diff --git a/database/ess_resource_db_map.cpp b/database/ess_resource_db_map.cpp index 63f5798..76b9abd 100644 --- a/database/ess_resource_db_map.cpp +++ b/database/ess_resource_db_map.cpp @@ -52,6 +52,8 @@ void ESSResourceDBMap::add_entity_resource(Ref cls) { _entity_resources.push_back(cls); _entity_resource_map.set(cls->get_id(), cls); + + ESSResourceDB::add_entity_resource(cls); } Vector ESSResourceDBMap::get_entity_resources() const { Vector r; @@ -94,6 +96,8 @@ void ESSResourceDBMap::add_entity_skill(Ref cls) { _entity_skills.push_back(cls); _entity_skill_map.set(cls->get_id(), cls); + + ESSResourceDB::add_entity_skill(cls); } Vector ESSResourceDBMap::get_entity_skills() const { Vector r; @@ -136,6 +140,8 @@ void ESSResourceDBMap::add_entity_data(Ref cls) { _entity_datas.push_back(cls); _entity_data_map.set(cls->get_id(), cls); + + ESSResourceDB::add_entity_data(cls); } Vector ESSResourceDBMap::get_entity_datas() const { Vector r; @@ -179,6 +185,8 @@ void ESSResourceDBMap::add_spell(Ref spell) { _spells.push_back(spell); _spell_map.set(spell->get_id(), spell); + + ESSResourceDB::add_spell(spell); } Vector ESSResourceDBMap::get_spells() const { Vector r; @@ -208,6 +216,8 @@ void ESSResourceDBMap::add_aura(Ref aura) { _auras.push_back(aura); _aura_map.set(aura->get_id(), aura); + _aura_id_to_path.set(aura->get_id(), aura->get_path()); + _aura_path_to_id.set(aura->get_path(), aura->get_id()); } Ref ESSResourceDBMap::get_aura(int aura_id) { @@ -255,6 +265,8 @@ void ESSResourceDBMap::add_craft_recipe(Ref cda) { _craft_recipes.push_back(cda); _craft_recipe_map.set(cda->get_id(), cda); + + ESSResourceDB::add_craft_recipe(cda); } Ref ESSResourceDBMap::get_craft_recipe(int craft_id) { @@ -301,6 +313,8 @@ void ESSResourceDBMap::add_item_template(Ref cda) { _item_templates.push_back(cda); _item_template_map.set(cda->get_id(), cda); + + ESSResourceDB::add_item_template(cda); } Ref ESSResourceDBMap::get_item_template(int item_id) { @@ -346,6 +360,8 @@ void ESSResourceDBMap::add_entity_species_data(Ref cda) { _entity_species_datas.push_back(cda); _entity_species_data_map.set(cda->get_id(), cda); + + ESSResourceDB::add_entity_species_data(cda); } Ref ESSResourceDBMap::get_entity_species_data(int item_id) { if (!_entity_species_data_map.has(item_id)) diff --git a/database/ess_resource_db_static.cpp b/database/ess_resource_db_static.cpp index adc2ccf..75fbc0b 100644 --- a/database/ess_resource_db_static.cpp +++ b/database/ess_resource_db_static.cpp @@ -56,6 +56,8 @@ void ESSResourceDBStatic::add_entity_resource(Ref cls) { cls->set_id(_entity_resources.size()); _entity_resources.push_back(cls); + + ESSResourceDB::add_entity_resource(cls); } Vector ESSResourceDBStatic::get_entity_resources() const { Vector r; @@ -96,6 +98,8 @@ void ESSResourceDBStatic::add_entity_skill(Ref cls) { cls->set_id(_entity_skills.size()); _entity_skills.push_back(cls); + + ESSResourceDB::add_entity_skill(cls); } Vector ESSResourceDBStatic::get_entity_skills() const { Vector r; @@ -136,6 +140,8 @@ void ESSResourceDBStatic::add_entity_data(Ref cls) { cls->set_id(_entity_datas.size()); _entity_datas.push_back(cls); + + ESSResourceDB::add_entity_data(cls); } Vector ESSResourceDBStatic::get_entity_datas() const { Vector r; @@ -177,6 +183,8 @@ void ESSResourceDBStatic::add_spell(Ref spell) { spell->set_id(_spells.size()); _spells.push_back(spell); + + ESSResourceDB::add_spell(spell); } Vector ESSResourceDBStatic::get_spells() const { Vector r; @@ -203,6 +211,8 @@ void ESSResourceDBStatic::add_aura(Ref aura) { aura->set_id(_auras.size()); _auras.push_back(aura); + + ESSResourceDB::add_aura(aura); } Ref ESSResourceDBStatic::get_aura(int id) { @@ -248,6 +258,8 @@ void ESSResourceDBStatic::add_craft_recipe(Ref cda) { cda->set_id(_craft_recipes.size()); _craft_recipes.push_back(cda); + + ESSResourceDB::add_craft_recipe(cda); } Ref ESSResourceDBStatic::get_craft_recipe(int id) { @@ -292,6 +304,8 @@ void ESSResourceDBStatic::add_item_template(Ref cda) { cda->set_id(_item_templates.size()); _item_templates.push_back(cda); + + ESSResourceDB::add_item_template(cda); } Ref ESSResourceDBStatic::get_item_template(int item_id) { @@ -335,6 +349,8 @@ void ESSResourceDBStatic::add_entity_species_data(Ref cda) { cda->set_id(_entity_species_datas.size()); _entity_species_datas.push_back(cda); + + ESSResourceDB::add_entity_species_data(cda); } Ref ESSResourceDBStatic::get_entity_species_data(int id) { if (id < 0 || id > _entity_species_datas.size())