From 9f0f5322590698c90a9b496ce3f0ff000fed6f2a Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 14 Apr 2020 00:26:27 +0200 Subject: [PATCH] Centralized the actual resource loading code. --- singletons/entity_data_manager.cpp | 198 ++++------------------------- singletons/entity_data_manager.h | 2 + 2 files changed, 30 insertions(+), 170 deletions(-) diff --git a/singletons/entity_data_manager.cpp b/singletons/entity_data_manager.cpp index c8b69aa..bbdd33a 100644 --- a/singletons/entity_data_manager.cpp +++ b/singletons/entity_data_manager.cpp @@ -414,23 +414,7 @@ void EntityDataManager::load_xp_data() { ERR_FAIL_COND(_xp_data_path == ""); - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(_xp_data_path, "XPData"); - - ERR_FAIL_COND(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(_xp_data_path, "XPData"); -#endif - - ERR_FAIL_COND(!s.is_valid()); - - Ref d = s; + Ref d = load_resource(_xp_data_path, "XPData"); ERR_FAIL_COND(!d.is_valid()); @@ -457,23 +441,7 @@ void EntityDataManager::load_entity_resources() { if (!dir.current_is_dir()) { String path = _entity_resources_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "EntityResourceData"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "EntityResourceData"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref eresd = s; + Ref eresd = load_resource(path, "EntityResourceData"); ERR_CONTINUE(!eresd.is_valid()); @@ -505,23 +473,7 @@ void EntityDataManager::load_entity_skills() { if (!dir.current_is_dir()) { String path = _entity_skills_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "EntitySkillData"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "EntitySkillData"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref eskilld = s; + Ref eskilld = load_resource(path, "EntitySkillData"); ERR_CONTINUE(!eskilld.is_valid()); @@ -553,23 +505,7 @@ void EntityDataManager::load_spells() { if (!dir.current_is_dir()) { String path = _spells_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "Spell"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "Spell"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref spell = s; + Ref spell = load_resource(path, "Spell"); ERR_CONTINUE(!spell.is_valid()); @@ -601,23 +537,7 @@ void EntityDataManager::load_auras() { if (!dir.current_is_dir()) { String path = _auras_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "Aura"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "Aura"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref aura = s; + Ref aura = load_resource(path, "Aura"); ERR_CONTINUE(!aura.is_valid()); @@ -649,23 +569,7 @@ void EntityDataManager::load_characters() { if (!dir.current_is_dir()) { String path = _entity_datas_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "EntityData"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "EntityData"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref cls = s; + Ref cls = load_resource(path, "EntityData"); ERR_CONTINUE(!cls.is_valid()); @@ -697,23 +601,7 @@ void EntityDataManager::load_craft_datas() { if (!dir.current_is_dir()) { String path = _craft_data_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "CraftRecipe"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "CraftRecipe"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref cda = s; + Ref cda = load_resource(path, "CraftRecipe"); ERR_CONTINUE(!cda.is_valid()); @@ -745,23 +633,7 @@ void EntityDataManager::load_item_templates() { if (!dir.current_is_dir()) { String path = _item_template_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "ItemTemplate"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "ItemTemplate"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref it = s; + Ref it = load_resource(path, "ItemTemplate"); ERR_CONTINUE(!it.is_valid()); @@ -793,23 +665,7 @@ void EntityDataManager::load_player_character_datas() { if (!dir.current_is_dir()) { String path = _player_character_data_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "EntityData"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "EntityData"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref pcd = s; + Ref pcd = load_resource(path, "EntityData"); ERR_CONTINUE(!pcd.is_valid()); @@ -841,23 +697,7 @@ void EntityDataManager::load_entity_species_datas() { if (!dir.current_is_dir()) { String path = _entity_species_data_folder + "/" + filename; - _ResourceLoader *rl = _ResourceLoader::get_singleton(); - -#if VERSION_MAJOR < 4 - Ref resl = rl->load_interactive(path, "EntitySpeciesData"); - - ERR_CONTINUE(!resl.is_valid()); - - resl->wait(); - - Ref s = resl->get_resource(); -#else - Ref s = rl->load(path, "EntitySpeciesData"); -#endif - - ERR_CONTINUE(!s.is_valid()); - - Ref pcd = s; + Ref pcd = load_resource(path, "EntitySpeciesData"); ERR_CONTINUE(!pcd.is_valid()); @@ -869,6 +709,22 @@ void EntityDataManager::load_entity_species_datas() { } } +Ref EntityDataManager::load_resource(const String &path, const String &type_hint) { + _ResourceLoader *rl = _ResourceLoader::get_singleton(); + +#if VERSION_MAJOR < 4 + Ref resl = rl->load_interactive(path, type_hint); + + ERR_FAIL_COND_V(!resl.is_valid(), Ref()); + + resl->wait(); + + return resl->get_resource(); +#else + return rl->load(path, type_hint); +#endif +} + void EntityDataManager::request_entity_spawn(const Ref &info) { emit_signal("on_entity_spawn_requested", info); } @@ -1026,6 +882,8 @@ void EntityDataManager::_bind_methods() { ClassDB::bind_method(D_METHOD("load_player_character_datas"), &EntityDataManager::load_player_character_datas); ClassDB::bind_method(D_METHOD("load_entity_species_datas"), &EntityDataManager::load_entity_species_datas); + ClassDB::bind_method(D_METHOD("load_resource", "path", "type_hint"), &EntityDataManager::load_resource); + ADD_SIGNAL(MethodInfo("on_entity_spawn_requested", PropertyInfo(Variant::OBJECT, "info", PROPERTY_HINT_RESOURCE_TYPE, "EntityCreateInfo"))); ClassDB::bind_method(D_METHOD("request_entity_spawn", "info"), &EntityDataManager::request_entity_spawn); diff --git a/singletons/entity_data_manager.h b/singletons/entity_data_manager.h index b7d4346..6c6a0fa 100644 --- a/singletons/entity_data_manager.h +++ b/singletons/entity_data_manager.h @@ -174,6 +174,8 @@ public: void load_player_character_datas(); void load_entity_species_datas(); + Ref load_resource(const String &path, const String &type_hint); + void request_entity_spawn(const Ref &info); void request_entity_spawn_deferred(const Ref &info);