From b5ed3b16c8ec7bc3fe6147e67b59dc7ffb64e88c Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 18 Apr 2020 20:33:55 +0200 Subject: [PATCH] Improvements to the EssResourceDB's api, also add a missing bind to the ESS singleton. --- database/ess_resource_db.cpp | 47 +++++++++++++++++++++++++ database/ess_resource_db.h | 4 +++ database/ess_resource_db_static.cpp | 53 +++++++++++++++++++++-------- database/ess_resource_db_static.h | 25 +++++++++----- singletons/ess.cpp | 1 + 5 files changed, 106 insertions(+), 24 deletions(-) diff --git a/database/ess_resource_db.cpp b/database/ess_resource_db.cpp index 2f45bdd..21a5cdd 100644 --- a/database/ess_resource_db.cpp +++ b/database/ess_resource_db.cpp @@ -48,6 +48,50 @@ void ESSResourceDB::set_xp_data(const Ref &data) { _xp_data = data; } +void ESSResourceDB::clear() { + _xp_data.unref(); +} + +void ESSResourceDB::add_entity_resource_db(Ref other) { + if (!other.is_valid()) + return; + + if (!_xp_data.is_valid() && other->_xp_data.is_valid()) + _xp_data = other->_xp_data; + + for (int i = 0; i < other->get_entity_resource_count(); ++i) { + add_entity_resource(other->get_entity_resource_index(i)); + } + + for (int i = 0; i < other->get_entity_skill_count(); ++i) { + add_entity_skill(other->get_entity_skill_index(i)); + } + + for (int i = 0; i < other->get_entity_data_count(); ++i) { + add_entity_data(other->get_entity_data_index(i)); + } + + for (int i = 0; i < other->get_spell_count(); ++i) { + add_spell(other->get_spell_index(i)); + } + + for (int i = 0; i < other->get_aura_count(); ++i) { + add_aura(other->get_aura_index(i)); + } + + for (int i = 0; i < other->get_craft_recipe_count(); ++i) { + add_craft_recipe(other->get_craft_recipe_index(i)); + } + + for (int i = 0; i < other->get_item_template_count(); ++i) { + add_item_template(other->get_item_template_index(i)); + } + + for (int i = 0; i < other->get_entity_species_data_count(); ++i) { + add_entity_species_data(other->get_entity_species_data_index(i)); + } +} + void ESSResourceDB::initialize() { if (has_method("_initialize")) call("_initialize"); @@ -137,6 +181,9 @@ void ESSResourceDB::_bind_methods() { 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("clear"), &ESSResourceDB::clear); + ClassDB::bind_method(D_METHOD("add_entity_resource_db", "other"), &ESSResourceDB::add_entity_resource_db); + BIND_VMETHOD(MethodInfo("_initialize")); ClassDB::bind_method(D_METHOD("initialize"), &ESSResourceDB::initialize); } diff --git a/database/ess_resource_db.h b/database/ess_resource_db.h index 7c03b5b..62149a7 100644 --- a/database/ess_resource_db.h +++ b/database/ess_resource_db.h @@ -113,6 +113,10 @@ public: virtual Vector get_entity_species_datas() const = 0; virtual void set_entity_species_datas(const Vector &data) = 0; + virtual void clear(); + + void add_entity_resource_db(Ref other); + void initialize(); ESSResourceDB(); diff --git a/database/ess_resource_db_static.cpp b/database/ess_resource_db_static.cpp index 7c94a6c..adc2ccf 100644 --- a/database/ess_resource_db_static.cpp +++ b/database/ess_resource_db_static.cpp @@ -30,8 +30,15 @@ SOFTWARE. #include "../entities/resources/entity_resource_data.h" #include "../entities/skills/entity_skill_data.h" +bool ESSResourceDBStatic::get_remap_ids() const { + return _remap_ids; +} +void ESSResourceDBStatic::set_remap_ids(const bool value) { + _remap_ids = value; +} + Ref ESSResourceDBStatic::get_entity_resource(int id) { - if (id < 0 || id > _entity_resources.size()) + if (id < 0 || id >= _entity_resources.size()) return Ref(); return _entity_resources.get(id); @@ -45,7 +52,7 @@ int ESSResourceDBStatic::get_entity_resource_count() { return _entity_resources.size(); } void ESSResourceDBStatic::add_entity_resource(Ref cls) { - if (cls.is_valid()) + if (_remap_ids && cls.is_valid()) cls->set_id(_entity_resources.size()); _entity_resources.push_back(cls); @@ -71,7 +78,7 @@ void ESSResourceDBStatic::set_entity_resources(const Vector &data) { } Ref ESSResourceDBStatic::get_entity_skill(int id) { - if (id < 0 || id > _entity_skills.size()) + if (id < 0 || id >= _entity_skills.size()) return Ref(); return _entity_skills.get(id); @@ -85,7 +92,7 @@ int ESSResourceDBStatic::get_entity_skill_count() { return _entity_skills.size(); } void ESSResourceDBStatic::add_entity_skill(Ref cls) { - if (cls.is_valid()) + if (_remap_ids && cls.is_valid()) cls->set_id(_entity_skills.size()); _entity_skills.push_back(cls); @@ -111,7 +118,7 @@ void ESSResourceDBStatic::set_entity_skills(const Vector &data) { } Ref ESSResourceDBStatic::get_entity_data(int id) { - if (id < 0 || id > _entity_datas.size()) + if (id < 0 || id >= _entity_datas.size()) return Ref(); return _entity_datas.get(id); @@ -125,7 +132,7 @@ int ESSResourceDBStatic::get_entity_data_count() { return _entity_datas.size(); } void ESSResourceDBStatic::add_entity_data(Ref cls) { - if (cls.is_valid()) + if (_remap_ids && cls.is_valid()) cls->set_id(_entity_datas.size()); _entity_datas.push_back(cls); @@ -151,7 +158,7 @@ void ESSResourceDBStatic::set_entity_datas(const Vector &data) { } Ref ESSResourceDBStatic::get_spell(int id) { - if (id < 0 || id > _spells.size()) + if (id < 0 || id >= _spells.size()) return Ref(); return _spells.get(id); @@ -166,7 +173,7 @@ int ESSResourceDBStatic::get_spell_count() { return _spells.size(); } void ESSResourceDBStatic::add_spell(Ref spell) { - if (spell.is_valid()) + if (_remap_ids && spell.is_valid()) spell->set_id(_spells.size()); _spells.push_back(spell); @@ -192,14 +199,14 @@ void ESSResourceDBStatic::set_spells(const Vector &data) { } void ESSResourceDBStatic::add_aura(Ref aura) { - if (aura.is_valid()) + if (_remap_ids && aura.is_valid()) aura->set_id(_auras.size()); _auras.push_back(aura); } Ref ESSResourceDBStatic::get_aura(int id) { - if (id < 0 || id > _auras.size()) + if (id < 0 || id >= _auras.size()) return Ref(); return _auras.get(id); @@ -237,14 +244,14 @@ void ESSResourceDBStatic::set_auras(const Vector &data) { //Craft Data void ESSResourceDBStatic::add_craft_recipe(Ref cda) { - if (cda.is_valid()) + if (_remap_ids && cda.is_valid()) cda->set_id(_craft_recipes.size()); _craft_recipes.push_back(cda); } Ref ESSResourceDBStatic::get_craft_recipe(int id) { - if (id < 0 || id > _craft_recipes.size()) + if (id < 0 || id >= _craft_recipes.size()) return Ref(); return _craft_recipes.get(id); @@ -281,14 +288,14 @@ void ESSResourceDBStatic::set_craft_recipes(const Vector &data) { } void ESSResourceDBStatic::add_item_template(Ref cda) { - if (cda.is_valid()) + if (_remap_ids && cda.is_valid()) cda->set_id(_item_templates.size()); _item_templates.push_back(cda); } Ref ESSResourceDBStatic::get_item_template(int item_id) { - if (item_id < 0 || item_id > _item_templates.size()) + if (item_id < 0 || item_id >= _item_templates.size()) return Ref(); return _item_templates.get(item_id); @@ -324,7 +331,7 @@ void ESSResourceDBStatic::set_item_templates(const Vector &data) { } void ESSResourceDBStatic::add_entity_species_data(Ref cda) { - if (cda.is_valid()) + if (_remap_ids && cda.is_valid()) cda->set_id(_entity_species_datas.size()); _entity_species_datas.push_back(cda); @@ -363,7 +370,19 @@ void ESSResourceDBStatic::set_entity_species_datas(const Vector &data) } } +void ESSResourceDBStatic::clear() { + _entity_resources.clear(); + _entity_skills.clear(); + _entity_datas.clear(); + _spells.clear(); + _auras.clear(); + _craft_recipes.clear(); + _item_templates.clear(); + _entity_species_datas.clear(); +} + ESSResourceDBStatic::ESSResourceDBStatic() { + _remap_ids = false; } ESSResourceDBStatic::~ESSResourceDBStatic() { @@ -378,6 +397,10 @@ ESSResourceDBStatic::~ESSResourceDBStatic() { } void ESSResourceDBStatic::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_remap_ids"), &ESSResourceDBStatic::get_remap_ids); + ClassDB::bind_method(D_METHOD("set_remap_ids", "value"), &ESSResourceDBStatic::set_remap_ids); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "remap_ids"), "set_remap_ids", "get_remap_ids"); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entity_resources", PROPERTY_HINT_NONE, "17/17:EntityResourceData", PROPERTY_USAGE_DEFAULT, "EntityResourceData"), "set_entity_resources", "get_entity_resources"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entity_skills", PROPERTY_HINT_NONE, "17/17:EntitySkillData", PROPERTY_USAGE_DEFAULT, "EntitySkillData"), "set_entity_skills", "get_entity_skills"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "entity_datas", PROPERTY_HINT_NONE, "17/17:EntityData", PROPERTY_USAGE_DEFAULT, "EntityData"), "set_entity_datas", "get_entity_datas"); diff --git a/database/ess_resource_db_static.h b/database/ess_resource_db_static.h index c1be77c..5cbc62b 100644 --- a/database/ess_resource_db_static.h +++ b/database/ess_resource_db_static.h @@ -54,6 +54,9 @@ class ESSResourceDBStatic : public ESSResourceDB { GDCLASS(ESSResourceDBStatic, ESSResourceDB); public: + bool get_remap_ids() const; + void set_remap_ids(const bool value); + String get_xp_data_path(); void set_xp_data_path(String path); @@ -91,7 +94,7 @@ public: Ref get_aura(int aura_id); Ref get_aura_index(int index); int get_aura_count(); - void add_aura( Ref aura); + void add_aura(Ref aura); Vector get_auras() const; void set_auras(const Vector &data); @@ -116,6 +119,8 @@ public: Vector get_entity_species_datas() const; void set_entity_species_datas(const Vector &data); + void clear(); + ESSResourceDBStatic(); ~ESSResourceDBStatic(); @@ -123,14 +128,16 @@ protected: static void _bind_methods(); private: - Vector> _entity_resources; - Vector> _entity_skills; - Vector> _entity_datas; - Vector> _spells; - Vector> _auras; - Vector> _craft_recipes; - Vector> _item_templates; - Vector> _entity_species_datas; + bool _remap_ids; + + Vector > _entity_resources; + Vector > _entity_skills; + Vector > _entity_datas; + Vector > _spells; + Vector > _auras; + Vector > _craft_recipes; + Vector > _item_templates; + Vector > _entity_species_datas; }; #endif diff --git a/singletons/ess.cpp b/singletons/ess.cpp index e8f6579..6c430b7 100644 --- a/singletons/ess.cpp +++ b/singletons/ess.cpp @@ -228,6 +228,7 @@ void ESS::_bind_methods() { //load ClassDB::bind_method(D_METHOD("load_resource_db"), &ESS::load_resource_db); + ClassDB::bind_method(D_METHOD("load_entity_spawner"), &ESS::load_entity_spawner); ClassDB::bind_method(D_METHOD("load_resource", "path", "type_hint"), &ESS::load_resource, DEFVAL(""));