From c80ad48f42bffbff33e2c05ea7e3f13eb2022c61 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 7 Nov 2024 07:58:31 +0300 Subject: [PATCH] add assets.load_texture & update CHANGELOG.md --- CHANGELOG.md | 6 +++ res/scripts/stdlib.lua | 3 ++ src/logic/scripting/lua/libs/libcore.cpp | 47 +++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c683ddbd..8e6dac81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Table of contents: - on_block_placed (documented) - on_block_interact - libraries: + - gfx.particles - utf8 - rules - bindings: @@ -76,6 +77,11 @@ Table of contents: - hud._is_content_access - hud._set_content_access - hud._set_debug_cheats +- gfx.particles.emit +- gfx.particles.stop +- gfx.particles.get_origin +- gfx.particles.set_origin +- assets.load_texture Documented: - file.read_combined_list diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 09fa5ae7..c7d90339 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -291,6 +291,9 @@ function __vc_on_world_quit() _rules.clear() end +assets = {} +assets.load_texture = core.__load_texture + -- --------- Deprecated functions ------ -- local function wrap_deprecated(func, name, alternatives) return function (...) diff --git a/src/logic/scripting/lua/libs/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp index ee611b63..5a100b9c 100644 --- a/src/logic/scripting/lua/libs/libcore.cpp +++ b/src/logic/scripting/lua/libs/libcore.cpp @@ -181,6 +181,49 @@ static int l_get_setting_info(lua::State* L) { throw std::runtime_error("unsupported setting type"); } +#include "coders/png.hpp" +#include "debug/Logger.hpp" +#include "files/files.hpp" +#include "graphics/core/Texture.hpp" + +/// FIXME: replace with in-memory implementation + +static void load_texture( + const ubyte* bytes, size_t size, const std::string& destname +) { + auto path = engine->getPaths()->resolve("export:.__vc_imagedata"); + try { + files::write_bytes(path, bytes, size); + engine->getAssets()->store(png::load_texture(path.u8string()), destname); + std::filesystem::remove(path); + } catch (const std::runtime_error& err) { + debug::Logger logger("lua.corelib"); + logger.error() << "could not to decode image: " << err.what(); + } +} + +static int l_load_texture(lua::State* L) { + if (lua::istable(L, 1)) { + lua::pushvalue(L, 1); + size_t size = lua::objlen(L, 1); + util::Buffer buffer(size); + for (size_t i = 0; i < size; i++) { + lua::rawgeti(L, i + 1); + buffer[i] = lua::tointeger(L, -1); + lua::pop(L); + } + lua::pop(L); + load_texture(buffer.data(), buffer.size(), lua::require_string(L, 2)); + } else if (auto bytes = lua::touserdata(L, 1)) { + load_texture( + bytes->data().data(), + bytes->data().size(), + lua::require_string(L, 2) + ); + } + return 0; +} + #include "util/platform.hpp" static int l_open_folder(lua::State* L) { @@ -208,4 +251,6 @@ const luaL_Reg corelib[] = { {"get_setting_info", lua::wrap}, {"open_folder", lua::wrap}, {"quit", lua::wrap}, - {NULL, NULL}}; + {"__load_texture", lua::wrap}, + {NULL, NULL} +};