diff options
author | hop311 <hop3114@gmail.com> | 2023-12-28 21:19:12 +0100 |
---|---|---|
committer | hop311 <hop3114@gmail.com> | 2023-12-28 21:22:27 +0100 |
commit | d04d0247479b229407048a37cd0ad60933aa3bf1 (patch) | |
tree | cb3671592c2d1e7697371510f455de7e99f75a12 | |
parent | c21e9b7a529aebcf0148a6671377c3e8478fafdf (diff) |
Use `ERR_FAIL_*` macros where possible
9 files changed, 119 insertions, 203 deletions
diff --git a/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp b/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp index 0ed7755..424be33 100644 --- a/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXMaskedFlagTexture.cpp @@ -76,11 +76,8 @@ Ref<GFXMaskedFlagTexture> GFXMaskedFlagTexture::make_gfx_masked_flag_texture(GFX Ref<GFXMaskedFlagTexture> masked_flag_texture; masked_flag_texture.instantiate(); ERR_FAIL_NULL_V(masked_flag_texture, nullptr); - if (masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag) == OK) { - return masked_flag_texture; - } else { - return nullptr; - } + ERR_FAIL_COND_V(masked_flag_texture->set_gfx_masked_flag(gfx_masked_flag) != OK, nullptr); + return masked_flag_texture; } void GFXMaskedFlagTexture::clear() { diff --git a/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp b/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp index e1bffc5..63deeda 100644 --- a/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp +++ b/extension/src/openvic-extension/classes/GFXPieChartTexture.cpp @@ -107,11 +107,8 @@ Ref<GFXPieChartTexture> GFXPieChartTexture::make_gfx_pie_chart_texture(GFX::PieC Ref<GFXPieChartTexture> pie_chart_texture; pie_chart_texture.instantiate(); ERR_FAIL_NULL_V(pie_chart_texture, nullptr); - if (pie_chart_texture->set_gfx_pie_chart(gfx_pie_chart) == OK) { - return pie_chart_texture; - } else { - return nullptr; - } + ERR_FAIL_COND_V(pie_chart_texture->set_gfx_pie_chart(gfx_pie_chart) != OK, nullptr); + return pie_chart_texture; } void GFXPieChartTexture::clear() { diff --git a/extension/src/openvic-extension/singletons/AssetManager.cpp b/extension/src/openvic-extension/singletons/AssetManager.cpp index 581c7fa..a81a0a2 100644 --- a/extension/src/openvic-extension/singletons/AssetManager.cpp +++ b/extension/src/openvic-extension/singletons/AssetManager.cpp @@ -32,66 +32,50 @@ AssetManager::~AssetManager() { _singleton = nullptr; } -Ref<Image> AssetManager::_load_image(StringName path) { +Ref<Image> AssetManager::_load_image(StringName const& path) { GameSingleton* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, nullptr); const String lookedup_path = std_to_godot_string(game_singleton->get_dataloader().lookup_image_file(godot_to_std_string(path)).string()); - if (lookedup_path.is_empty()) { - UtilityFunctions::push_error("Failed to look up image: ", path); - return nullptr; - } + ERR_FAIL_COND_V_MSG(lookedup_path.is_empty(), nullptr, vformat("Failed to look up image: %s", path)); const Ref<Image> image = Utilities::load_godot_image(lookedup_path); - if (image.is_null() || image->is_empty()) { - UtilityFunctions::push_error("Failed to load image: ", lookedup_path, " (looked up from ", path, ")"); - return nullptr; - } else { - return image; - } + ERR_FAIL_COND_V_MSG( + image.is_null() || image->is_empty(), nullptr, vformat("Failed to load image: %s (looked up: %s)", path, lookedup_path) + ); + return image; } -AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName path) { +AssetManager::image_asset_map_t::iterator AssetManager::_get_image_asset(StringName const& path) { const image_asset_map_t::iterator it = image_assets.find(path); if (it != image_assets.end()) { return it; } const Ref<Image> image = _load_image(path); - if (image.is_valid()) { - return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first; - } else { - return image_assets.end(); - } + ERR_FAIL_NULL_V(image, image_assets.end()); + return image_assets.emplace(std::move(path), AssetManager::image_asset_t { image, nullptr }).first; } -Ref<Image> AssetManager::get_image(StringName path, bool cache) { +Ref<Image> AssetManager::get_image(StringName const& path, bool cache) { if (cache) { const image_asset_map_t::const_iterator it = _get_image_asset(path); - if (it != image_assets.end()) { - return it->second.image; - } else { - return nullptr; - } + ERR_FAIL_COND_V(it == image_assets.end(), nullptr); + return it->second.image; } else { return _load_image(path); } } -Ref<ImageTexture> AssetManager::get_texture(StringName path) { +Ref<ImageTexture> AssetManager::get_texture(StringName const& path) { const image_asset_map_t::iterator it = _get_image_asset(path); - if (it != image_assets.end()) { - if (it->second.texture.is_null()) { - it->second.texture = ImageTexture::create_from_image(it->second.image); - if (it->second.texture.is_null()) { - UtilityFunctions::push_error("Failed to turn image into texture: ", path); - } - } - return it->second.texture; - } else { - return nullptr; + ERR_FAIL_COND_V(it == image_assets.end(), nullptr); + if (it->second.texture.is_null()) { + it->second.texture = ImageTexture::create_from_image(it->second.image); + ERR_FAIL_NULL_V_MSG(it->second.texture, nullptr, vformat("Failed to turn image into texture: %s", path)); } + return it->second.texture; } -Ref<Font> AssetManager::get_font(StringName name) { +Ref<Font> AssetManager::get_font(StringName const& name) { const font_map_t::const_iterator it = fonts.find(name); if (it != fonts.end()) { return it->second; @@ -101,21 +85,19 @@ Ref<Font> AssetManager::get_font(StringName name) { static const String font_ext = ".fnt"; static const String image_ext = ".tga"; - const String image_path = font_dir + name + image_ext; + const StringName image_path = font_dir + name + image_ext; const Ref<Image> image = get_image(image_path); - if (image.is_null()) { - UtilityFunctions::push_error("Failed to load font image: ", image_path, " for the font named ", name); - return nullptr; - } + ERR_FAIL_NULL_V_MSG(image, nullptr, vformat("Failed to load font image %s for the font named %s", image_path, name)); GameSingleton* game_singleton = GameSingleton::get_singleton(); ERR_FAIL_NULL_V(game_singleton, nullptr); + const String font_path = font_dir + name + font_ext; const String lookedup_font_path = - std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_dir + name + font_ext)).string()); + std_to_godot_string(game_singleton->get_dataloader().lookup_file(godot_to_std_string(font_path)).string()); const Ref<Font> font = Utilities::load_godot_font(lookedup_font_path, image); - if (font.is_null()) { - UtilityFunctions::push_error("Failed to load font file ", lookedup_font_path, " for the font named ", name); - return nullptr; - } + ERR_FAIL_NULL_V_MSG( + font, nullptr, + vformat("Failed to load font file %s (looked up: %s) for the font named %s", font_path, lookedup_font_path, name) + ); fonts.emplace(std::move(name), font); return font; } diff --git a/extension/src/openvic-extension/singletons/AssetManager.hpp b/extension/src/openvic-extension/singletons/AssetManager.hpp index e6a7664..40577ad 100644 --- a/extension/src/openvic-extension/singletons/AssetManager.hpp +++ b/extension/src/openvic-extension/singletons/AssetManager.hpp @@ -23,8 +23,8 @@ namespace OpenVic { image_asset_map_t image_assets; font_map_t fonts; - static godot::Ref<godot::Image> _load_image(godot::StringName path); - image_asset_map_t::iterator _get_image_asset(godot::StringName path); + static godot::Ref<godot::Image> _load_image(godot::StringName const& path); + image_asset_map_t::iterator _get_image_asset(godot::StringName const& path); protected: static void _bind_methods(); @@ -37,15 +37,15 @@ namespace OpenVic { /* Search for and load an image at the specified path relative to the game defines, first checking the AssetManager's * image cache (if cache is true) in case it has already been loaded, and returning nullptr if image loading fails. */ - godot::Ref<godot::Image> get_image(godot::StringName path, bool cache = true); + godot::Ref<godot::Image> get_image(godot::StringName const& path, bool cache = true); /* Create a texture from an image found at the specified path relative to the game defines, fist checking * AssetManager's texture cache in case it has already been loaded, and returning nullptr if image loading * or texture creation fails. */ - godot::Ref<godot::ImageTexture> get_texture(godot::StringName path); + godot::Ref<godot::ImageTexture> get_texture(godot::StringName const& path); /* Search for and load a font with the specified name from the game defines' font directory, first checking the * AssetManager's font cache in case it has already been loaded, and returning nullptr if font loading fails. */ - godot::Ref<godot::Font> get_font(godot::StringName name); + godot::Ref<godot::Font> get_font(godot::StringName const& name); }; } diff --git a/extension/src/openvic-extension/singletons/GameSingleton.cpp b/extension/src/openvic-extension/singletons/GameSingleton.cpp index 0e64313..7ad0db0 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.cpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.cpp @@ -118,10 +118,7 @@ Dataloader const& GameSingleton::get_dataloader() const { Error GameSingleton::setup_game(int32_t bookmark_index) { Bookmark const* bookmark = game_manager.get_history_manager().get_bookmark_manager().get_bookmark_by_index(bookmark_index); - if (bookmark == nullptr) { - UtilityFunctions::push_error("Failed to get bookmark with index: ", bookmark_index); - return FAILED; - } + ERR_FAIL_NULL_V_MSG(bookmark, FAILED, vformat("Failed to get bookmark with index: %d", bookmark_index)); bool ret = game_manager.load_bookmark(bookmark); for (Province& province : game_manager.get_map().get_provinces()) { province.set_crime( @@ -272,24 +269,18 @@ Ref<Texture> GameSingleton::get_terrain_texture() const { } Ref<Image> GameSingleton::get_flag_image(Country const* country, StringName const& flag_type) const { - if (country != nullptr) { - const typename decltype(flag_image_map)::const_iterator it = flag_image_map.find(country); - if (it != flag_image_map.end()) { - const typename decltype(it->second)::const_iterator it2 = it->second.find(flag_type); - if (it2 != it->second.end()) { - return it2->second; - } else { - UtilityFunctions::push_error( - "Failed to find ", flag_type, " flag for country: ", std_view_to_godot_string(country->get_identifier()) - ); - } - } else { - UtilityFunctions::push_error( - "Failed to find flags for country: ", std_view_to_godot_string(country->get_identifier()) - ); - } - } - return nullptr; + ERR_FAIL_NULL_V(country, nullptr); + const typename decltype(flag_image_map)::const_iterator it = flag_image_map.find(country); + ERR_FAIL_COND_V_MSG( + it == flag_image_map.end(), nullptr, + vformat("Failed to find flags for country: %s", std_view_to_godot_string(country->get_identifier())) + ); + const typename decltype(it->second)::const_iterator it2 = it->second.find(flag_type); + ERR_FAIL_COND_V_MSG( + it2 == it->second.end(), nullptr, + vformat("Failed to find %s flag for country: %s", flag_type, std_view_to_godot_string(country->get_identifier())) + ); + return it2->second; } Vector2i GameSingleton::get_province_shape_image_subdivisions() const { @@ -306,10 +297,10 @@ Ref<Texture> GameSingleton::get_province_colour_texture() const { Error GameSingleton::_update_colour_image() { Map const& map = game_manager.get_map(); - if (!map.provinces_are_locked()) { - UtilityFunctions::push_error("Cannot generate province colour image before provinces are locked!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + !map.provinces_are_locked(), FAILED, "Cannot generate province colour image before provinces are locked!" + ); + /* We reshape the list of colours into a square, as each texture dimensions cannot exceed 16384. */ static constexpr int32_t PROVINCE_INDEX_SQRT = 1 << (sizeof(Province::index_t) * CHAR_BIT / 2); static constexpr int32_t colour_image_width = PROVINCE_INDEX_SQRT * sizeof(Mapmode::base_stripe_t) / sizeof(colour_argb_t); @@ -357,13 +348,9 @@ String GameSingleton::get_mapmode_identifier(int32_t index) const { Error GameSingleton::set_mapmode(String const& identifier) { Mapmode const* mapmode = game_manager.get_map().get_mapmode_by_identifier(godot_to_std_string(identifier)); - if (mapmode == nullptr) { - UtilityFunctions::push_error("Failed to set mapmode to: ", identifier); - return FAILED; - } + ERR_FAIL_NULL_V_MSG(mapmode, FAILED, vformat("Failed to find mapmode with identifier: %s", identifier)); mapmode_index = mapmode->get_index(); - _update_colour_image(); - return OK; + return _update_colour_image(); } int32_t GameSingleton::get_selected_province_index() const { @@ -390,37 +377,28 @@ String GameSingleton::get_province_building_identifier(int32_t index) const { } Error GameSingleton::expand_selected_province_building(int32_t building_index) { - const bool ret = game_manager.expand_selected_province_building(building_index); - if (!ret) { - UtilityFunctions::push_error("Failed to expand the currently selected province's building index ", building_index); - } - return ERR(ret); + ERR_FAIL_COND_V_MSG( + !game_manager.expand_selected_province_building(building_index), FAILED, + vformat("Failed to expand the currently selected province's building index %d", building_index) + ); + return OK; } int32_t GameSingleton::get_slave_pop_icon_index() const { const PopType::sprite_t sprite = game_manager.get_pop_manager().get_slave_sprite(); - if (sprite <= 0) { - UtilityFunctions::push_error("Slave sprite unset!"); - return 0; - } + ERR_FAIL_COND_V_MSG(sprite <= 0, 0, "Slave sprite unset!"); return sprite; } int32_t GameSingleton::get_administrative_pop_icon_index() const { const PopType::sprite_t sprite = game_manager.get_pop_manager().get_administrative_sprite(); - if (sprite <= 0) { - UtilityFunctions::push_error("Administrative sprite unset!"); - return 0; - } + ERR_FAIL_COND_V_MSG(sprite <= 0, 0, "Administrative sprite unset!"); return sprite; } int32_t GameSingleton::get_rgo_owner_pop_icon_index() const { const PopType::sprite_t sprite = game_manager.get_economy_manager().get_production_type_manager().get_rgo_owner_sprite(); - if (sprite <= 0) { - UtilityFunctions::push_error("RGO owner sprite unset!"); - return 0; - } + ERR_FAIL_COND_V_MSG(sprite <= 0, 0, "RGO owner sprite unset!"); return sprite; } @@ -473,10 +451,7 @@ void GameSingleton::try_tick() { } Error GameSingleton::_load_map_images(bool flip_vertical) { - if (province_shape_texture.is_valid()) { - UtilityFunctions::push_error("Map images have already been loaded!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG(province_shape_texture.is_valid(), FAILED, "Map images have already been loaded!"); Error err = OK; @@ -535,33 +510,26 @@ Error GameSingleton::_load_map_images(bool flip_vertical) { } Error GameSingleton::_load_terrain_variants() { - if (terrain_texture.is_valid()) { - UtilityFunctions::push_error("Terrain variants have already been loaded!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG(terrain_texture.is_valid(), FAILED, "Terrain variants have already been loaded!"); - static const String terrain_texturesheet_path = "map/terrain/texturesheet.tga"; + static const StringName terrain_texturesheet_path = "map/terrain/texturesheet.tga"; AssetManager* asset_manager = AssetManager::get_singleton(); ERR_FAIL_NULL_V(asset_manager, FAILED); // Load the terrain texture sheet and prepare to slice it up Ref<Image> terrain_sheet = asset_manager->get_image(terrain_texturesheet_path); - if (terrain_sheet.is_null()) { - UtilityFunctions::push_error("Failed to load terrain texture sheet: ", terrain_texturesheet_path); - return FAILED; - } + ERR_FAIL_NULL_V_MSG(terrain_sheet, FAILED, vformat("Failed to load terrain texture sheet: %s", terrain_texturesheet_path)); static constexpr int32_t SHEET_DIMS = 8, SHEET_SIZE = SHEET_DIMS * SHEET_DIMS; terrain_sheet->flip_y(); const int32_t sheet_width = terrain_sheet->get_width(), sheet_height = terrain_sheet->get_height(); - if (sheet_width < 1 || sheet_width % SHEET_DIMS != 0 || sheet_width != sheet_height) { - UtilityFunctions::push_error( - "Invalid terrain texture sheet dims: ", sheet_width, "x", sheet_height, - " (must be square with dims positive multiples of ", SHEET_DIMS, ")" - ); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + sheet_width < 1 || sheet_width % SHEET_DIMS != 0 || sheet_width != sheet_height, FAILED, vformat( + "Invalid terrain texture sheet dims: %dx%d (must be square with dims positive multiples of %d)", + sheet_width, sheet_height, SHEET_DIMS + ) + ); const int32_t slice_size = sheet_width / SHEET_DIMS; TypedArray<Image> terrain_images; @@ -588,29 +556,24 @@ Error GameSingleton::_load_terrain_variants() { } terrain_texture.instantiate(); - if (terrain_texture->create_from_images(terrain_images) != OK) { - UtilityFunctions::push_error("Failed to create terrain texture array!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + terrain_texture->create_from_images(terrain_images) != OK, FAILED, "Failed to create terrain texture array!" + ); return err; } Error GameSingleton::_load_flag_images() { - if (!flag_image_map.empty()) { - UtilityFunctions::push_error("Flag images have already been loaded!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG(!flag_image_map.empty(), FAILED, "Flag images have already been loaded!"); GovernmentTypeManager const& government_type_manager = game_manager.get_politics_manager().get_government_type_manager(); - if (!government_type_manager.government_types_are_locked()) { - UtilityFunctions::push_error("Cannot load flag images before government types are locked!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + !government_type_manager.government_types_are_locked(), FAILED, + "Cannot load flag images before government types are locked!" + ); CountryManager const& country_manager = game_manager.get_country_manager(); - if (!country_manager.countries_are_locked()) { - UtilityFunctions::push_error("Cannot load flag images before countries are locked!"); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + !country_manager.countries_are_locked(), FAILED, "Cannot load flag images before countries are locked!" + ); AssetManager* asset_manager = AssetManager::get_singleton(); ERR_FAIL_NULL_V(asset_manager, FAILED); @@ -629,11 +592,8 @@ Error GameSingleton::_load_flag_images() { std::map<StringName, Ref<Image>>& flag_images = flag_image_map[&country]; const String country_name = std_view_to_godot_string(country.get_identifier()); for (StringName const& flag_type : flag_types) { - String flag_path = flag_directory + country_name; - if (!flag_type.is_empty()) { - flag_path += flag_separator + flag_type; - } - flag_path += flag_extension; + const StringName flag_path = + flag_directory + country_name + (flag_type.is_empty() ? "" : flag_separator + flag_type) + flag_extension; const Ref<Image> flag_image = asset_manager->get_image(flag_path); if (flag_image.is_valid()) { flag_images.emplace(flag_type, flag_image); @@ -687,6 +647,6 @@ Error GameSingleton::load_defines_compatibility_mode(PackedStringArray const& fi return err; } -String GameSingleton::search_for_game_path(String hint_path) { +String GameSingleton::search_for_game_path(String const& hint_path) { return std_to_godot_string(Dataloader::search_for_game_path(godot_to_std_string(hint_path)).string()); } diff --git a/extension/src/openvic-extension/singletons/GameSingleton.hpp b/extension/src/openvic-extension/singletons/GameSingleton.hpp index 1f3905e..56f3c25 100644 --- a/extension/src/openvic-extension/singletons/GameSingleton.hpp +++ b/extension/src/openvic-extension/singletons/GameSingleton.hpp @@ -52,7 +52,7 @@ namespace OpenVic { * pointing to the defines folder. */ godot::Error load_defines_compatibility_mode(godot::PackedStringArray const& file_paths); - static godot::String search_for_game_path(godot::String hint_path = {}); + static godot::String search_for_game_path(godot::String const& hint_path = {}); /* Post-load/restart game setup - reset the game to post-load state and load the specified bookmark. */ godot::Error setup_game(int32_t bookmark_index); diff --git a/extension/src/openvic-extension/singletons/LoadLocalisation.cpp b/extension/src/openvic-extension/singletons/LoadLocalisation.cpp index 3da8bc8..6469820 100644 --- a/extension/src/openvic-extension/singletons/LoadLocalisation.cpp +++ b/extension/src/openvic-extension/singletons/LoadLocalisation.cpp @@ -33,10 +33,9 @@ LoadLocalisation::~LoadLocalisation() { Error LoadLocalisation::_load_file(String const& file_path, Ref<Translation> translation) const { const Ref<FileAccess> file = FileAccess::open(file_path, FileAccess::ModeFlags::READ); Error err = FileAccess::get_open_error(); - if (err != OK || file.is_null()) { - UtilityFunctions::push_error("Failed to load localisation file: ", file_path); - return err == OK ? FAILED : err; - } + ERR_FAIL_COND_V_MSG( + err != OK || file.is_null(), err == OK ? FAILED : err, vformat("Failed to open localisation file: %s", file_path) + ); int line_number = 0; while (!file->eof_reached()) { static const String delimeter = ";"; @@ -81,10 +80,10 @@ Error LoadLocalisation::load_file(String const& file_path, String const& locale) * FS-18, FS-24, FS-25 */ Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& locale) const { - if (!DirAccess::dir_exists_absolute(dir_path)) { - UtilityFunctions::push_error("Locale directory does not exist: ", dir_path); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + !DirAccess::dir_exists_absolute(dir_path), FAILED, vformat("Locale directory does not exist: %s", dir_path) + ); + /* This will add the locale to the list of loaded locales even if it has no * localisation files - this is useful for testing other aspects of localisation * such as number formatting and text direction. To disable this behaviour and @@ -93,10 +92,7 @@ Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& lo */ const Ref<Translation> translation = _get_translation(locale); const PackedStringArray files = DirAccess::get_files_at(dir_path); - if (files.size() < 1) { - UtilityFunctions::push_error("Locale directory does not contain any files: ", dir_path); - return FAILED; - } + ERR_FAIL_COND_V_MSG(files.size() < 1, FAILED, vformat("Locale directory does not contain any files: %s", dir_path)); Error err = OK; for (String const& file_name : files) { if (file_name.get_extension().to_lower() == "csv") { @@ -112,15 +108,13 @@ Error LoadLocalisation::load_locale_dir(String const& dir_path, String const& lo * FS-23 */ Error LoadLocalisation::load_localisation_dir(String const& dir_path) const { - if (!DirAccess::dir_exists_absolute(dir_path)) { - UtilityFunctions::push_error("Localisation directory does not exist: ", dir_path); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + !DirAccess::dir_exists_absolute(dir_path), FAILED, vformat("Localisation directory does not exist: %s", dir_path) + ); PackedStringArray const dirs = DirAccess::get_directories_at(dir_path); - if (dirs.size() < 1) { - UtilityFunctions::push_error("Localisation directory does not contain any sub-directories: ", dir_path); - return FAILED; - } + ERR_FAIL_COND_V_MSG( + dirs.size() < 1, FAILED, vformat("Localisation directory does not contain any sub-directories: %s", dir_path) + ); TranslationServer* server = TranslationServer::get_singleton(); ERR_FAIL_NULL_V(server, FAILED); Error err = OK; @@ -138,10 +132,9 @@ bool LoadLocalisation::add_message(std::string_view key, Dataloader::locale_t lo Ref<Translation>& translation = translations[locale]; if (translation.is_null()) { translation = _singleton->_get_translation(Dataloader::locale_names[locale]); - if (translation.is_null()) { - UtilityFunctions::push_error("Failed to get translation object: ", Dataloader::locale_names[locale]); - return false; - } + ERR_FAIL_NULL_V_MSG( + translation, false, vformat("Failed to get translation object: %s", Dataloader::locale_names[locale]) + ); } const StringName godot_key = Utilities::std_view_to_godot_string_name(key); const StringName godot_localisation = Utilities::std_view_to_godot_string_name(localisation); diff --git a/extension/src/openvic-extension/utility/UITools.cpp b/extension/src/openvic-extension/utility/UITools.cpp index cba65a4..67ea8f5 100644 --- a/extension/src/openvic-extension/utility/UITools.cpp +++ b/extension/src/openvic-extension/utility/UITools.cpp @@ -86,8 +86,7 @@ static T* new_control(GUI::Element const& element, String const& name) { if (it != orientation_map.end()) { node->set_anchors_and_offsets_preset(it->second); } else { - UtilityFunctions::push_error("Invalid orientation for GUI element ", - std_view_to_godot_string(element.get_name())); + UtilityFunctions::push_error("Invalid orientation for GUI element ", std_view_to_godot_string(element.get_name())); } node->set_position(Utilities::to_godot_fvec2(element.get_position())); node->set_h_size_flags(Control::SizeFlags::SIZE_SHRINK_BEGIN); @@ -426,12 +425,10 @@ static bool generate_element(GUI::Element const* element, String const& name, As { GUI::Window::get_type_static(), &generate_window } }; const decltype(type_map)::const_iterator it = type_map.find(element->get_type()); - if (it != type_map.end()) { - return it->second({ *element, name, asset_manager, result }); - } else { - UtilityFunctions::push_error("Invalid GUI element type: ", std_view_to_godot_string(element->get_type())); - return false; - } + ERR_FAIL_COND_V_MSG( + it == type_map.end(), false, vformat("Invalid GUI element type: %s", std_view_to_godot_string(element->get_type())) + ); + return it->second({ *element, name, asset_manager, result }); } bool UITools::generate_gui_element( diff --git a/extension/src/openvic-extension/utility/Utilities.cpp b/extension/src/openvic-extension/utility/Utilities.cpp index 8293e70..4cf515f 100644 --- a/extension/src/openvic-extension/utility/Utilities.cpp +++ b/extension/src/openvic-extension/utility/Utilities.cpp @@ -64,31 +64,23 @@ Ref<Resource> Utilities::load_resource(String const& path, String const& type_hi static Ref<Image> load_dds_image(String const& path) { gli::texture2d texture { gli::load_dds(Utilities::godot_to_std_string(path)) }; - if (texture.empty()) { - UtilityFunctions::push_error("Failed to load DDS file: ", path); - return nullptr; - } + ERR_FAIL_COND_V_MSG(texture.empty(), nullptr, vformat("Failed to load DDS file: %s", path)); static constexpr gli::format expected_format = gli::FORMAT_BGRA8_UNORM_PACK8; const bool needs_bgr_to_rgb = texture.format() == expected_format; if (!needs_bgr_to_rgb) { texture = gli::convert(texture, expected_format); - if (texture.empty()) { - UtilityFunctions::push_error("Failed to convert DDS file: ", path); - return nullptr; - } + ERR_FAIL_COND_V_MSG(texture.empty(), nullptr, vformat("Failed to convert DDS file: %s", path)); } const gli::texture2d::extent_type extent { texture.extent() }; - const int width = extent.x, height = extent.y, size = width * height * 4; + const int32_t width = extent.x, height = extent.y, size = width * height * 4; /* Only fail if there aren't enough bytes, everything seems to work fine if there are extra bytes and we ignore them */ - if (size > texture.size()) { - UtilityFunctions::push_error( - "Texture size ", static_cast<int64_t>(texture.size()), " mismatched with dims-based size ", size, " for ", path - ); - return nullptr; - } + ERR_FAIL_COND_V_MSG( + size > texture.size(), nullptr, + vformat("Texture size %d mismatched with dims-based size %d for %s", static_cast<int64_t>(texture.size()), size, path) + ); PackedByteArray pixels; pixels.resize(size); @@ -113,13 +105,11 @@ Ref<Image> Utilities::load_godot_image(String const& path) { } Ref<FontFile> Utilities::load_godot_font(String const& fnt_path, Ref<Image> const& image) { + ERR_FAIL_NULL_V(image, nullptr); Ref<FontFile> font; font.instantiate(); - const Error err = font->load_bitmap_font(fnt_path); + ERR_FAIL_COND_V_MSG(font->load_bitmap_font(fnt_path) != OK, nullptr, vformat("Failed to load font: %s", fnt_path)); font->set_texture_image(0, { font->get_fixed_size(), 0 }, 0, image); - if (err != OK) { - UtilityFunctions::push_error("Failed to load font (error ", err, "): ", fnt_path); - } return font; } |