AssetsLoader::loadExternalTexture

This commit is contained in:
MihailRis 2024-04-23 03:50:03 +03:00
parent 0c731f3fe4
commit cc5784c878
4 changed files with 44 additions and 30 deletions

View File

@ -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<std::filesystem::path> 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;
}

View File

@ -93,15 +93,21 @@ public:
bool hasNext() const;
bool loadNext();
std::shared_ptr<Task> 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<Task> startTask(runnable onDone);
const ResPaths* getPaths() const;
aloader_func getLoader(AssetType tag);
static bool loadExternalTexture(
Assets* assets,
const std::string& name,
std::vector<std::filesystem::path> alternatives
);
};
#endif // ASSETS_ASSETS_LOADER_H

View File

@ -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 <string>
#include <filesystem>
@ -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");

View File

@ -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");