diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index 3c97da82..1f8264b1 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -11,10 +11,12 @@ #include "../constants.h" #include "../data/dynamic.h" #include "../debug/Logger.h" +#include "../coders/imageio.h" #include "../files/files.h" #include "../files/engine_paths.h" #include "../content/Content.h" #include "../content/ContentPack.h" +#include "../graphics/core/Texture.hpp" #include "../logic/scripting/scripting.h" static debug::Logger logger("assets-loader"); @@ -204,6 +206,26 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) { loader.add(AssetType::atlas, TEXTURES_FOLDER+"/items", "items"); } +bool AssetsLoader::loadExternalTexture( + Assets* assets, + const std::string& name, + std::vector alternatives +) { + for (auto& path : alternatives) { + if (fs::exists(path)) { + try { + auto image = imageio::read(path.string()); + assets->store(Texture::from(image.get()), name); + return true; + } catch (const std::exception& err) { + logger.error() << "error while loading external " + << path.u8string() << ": " << err.what(); + } + } + } + return false; +} + const ResPaths* AssetsLoader::getPaths() const { return paths; } diff --git a/src/assets/AssetsLoader.h b/src/assets/AssetsLoader.h index a32e03bf..0ce278c2 100644 --- a/src/assets/AssetsLoader.h +++ b/src/assets/AssetsLoader.h @@ -93,15 +93,21 @@ public: bool hasNext() const; bool loadNext(); + std::shared_ptr startTask(runnable onDone); + + const ResPaths* getPaths() const; + aloader_func getLoader(AssetType tag); + /// @brief Enqueue core and content assets /// @param loader target loader /// @param content engine content static void addDefaults(AssetsLoader& loader, const Content* content); - std::shared_ptr startTask(runnable onDone); - - const ResPaths* getPaths() const; - aloader_func getLoader(AssetType tag); + static bool loadExternalTexture( + Assets* assets, + const std::string& name, + std::vector alternatives + ); }; #endif // ASSETS_ASSETS_LOADER_H diff --git a/src/logic/scripting/lua/libpack.cpp b/src/logic/scripting/lua/libpack.cpp index 24188f2a..3b72e145 100644 --- a/src/logic/scripting/lua/libpack.cpp +++ b/src/logic/scripting/lua/libpack.cpp @@ -2,12 +2,11 @@ #include "lua_commons.h" #include "../scripting.h" #include "../../../engine.h" -#include "../../../coders/imageio.h" +#include "../../../assets/AssetsLoader.h" #include "../../../files/engine_paths.h" #include "../../../files/WorldFiles.h" #include "../../../world/Level.h" #include "../../../world/World.h" -#include "../../../graphics/core/Texture.hpp" #include #include @@ -75,21 +74,14 @@ static int l_pack_get_info(lua_State* L, const ContentPack& pack, const Content* lua_pushstring(L, pack.version.c_str()); lua_setfield(L, -2, "version"); - // FIXME: hmm auto assets = scripting::engine->getAssets(); std::string icon = pack.id+".icon"; - if (assets->getTexture(icon) == nullptr) { - auto iconfile = pack.folder/fs::path("icon.png"); - if (!fs::exists(iconfile)) { - iconfile = pack.folder/fs::path("preview.png"); - } - if (fs::exists(iconfile)) { - auto image = imageio::read(iconfile.string()); - assets->store(Texture::from(image.get()), icon); - } else { - icon = "gui/no_icon"; - } + if (!AssetsLoader::loadExternalTexture(assets, icon, { + pack.folder/fs::path("icon.png") + })) { + icon = "gui/no_icon"; } + lua_pushstring(L, icon.c_str()); lua_setfield(L, -2, "icon"); diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 5e81274d..7d252461 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -2,8 +2,7 @@ #include "api_lua.h" #include "../scripting.h" #include "../../../assets/Assets.h" -#include "../../../coders/imageio.h" -#include "../../../graphics/core/Texture.hpp" +#include "../../../assets/AssetsLoader.h" #include "../../../files/engine_paths.h" #include "../../../world/Level.h" #include "../../../world/World.h" @@ -26,18 +25,13 @@ static int l_world_get_list(lua_State* L) { lua_pushstring(L, name.c_str()); lua_setfield(L, -2, "name"); - // FIXME: hmm auto assets = scripting::engine->getAssets(); std::string icon = "world:"+name+".icon"; - - if (assets->getTexture(icon) == nullptr) { - auto iconfile = worlds[i]/fs::path("preview.png"); - if (fs::is_regular_file(iconfile)) { - auto image = imageio::read(iconfile.string()); - assets->store(Texture::from(image.get()), icon); - } else { - icon = "gui/no_world_icon"; - } + if (!AssetsLoader::loadExternalTexture(assets, icon, { + worlds[i]/fs::path("icon.png"), + worlds[i]/fs::path("preview.png") + })) { + icon = "gui/no_world_icon"; } lua_pushstring(L, icon.c_str()); lua_setfield(L, -2, "icon");