diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 3bc2147e..8915c2b1 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -523,9 +523,6 @@ function time.post_runnable(runnable) table.insert(__post_runnables, runnable) 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/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index 7b4253c5..6e65b790 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -15,6 +15,7 @@ // Libraries extern const luaL_Reg applib[]; +extern const luaL_Reg assetslib[]; extern const luaL_Reg audiolib[]; extern const luaL_Reg base64lib[]; extern const luaL_Reg bjsonlib[]; diff --git a/src/logic/scripting/lua/libs/libassets.cpp b/src/logic/scripting/lua/libs/libassets.cpp new file mode 100644 index 00000000..6dbb305c --- /dev/null +++ b/src/logic/scripting/lua/libs/libassets.cpp @@ -0,0 +1,50 @@ +#include "api_lua.hpp" + +#include "assets/Assets.hpp" +#include "coders/png.hpp" +#include "debug/Logger.hpp" +#include "engine/Engine.hpp" +#include "graphics/core/Texture.hpp" +#include "util/Buffer.hpp" + +using namespace scripting; + +static void load_texture( + const ubyte* bytes, size_t size, const std::string& destname +) { + try { + engine->getAssets()->store(png::load_texture(bytes, size), destname); + } catch (const std::runtime_error& err) { + debug::Logger logger("lua.assetslib"); + logger.error() << 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 { + auto string = lua::bytearray_as_string(L, 1); + load_texture( + reinterpret_cast(string.data()), + string.size(), + lua::require_string(L, 2) + ); + lua::pop(L); + } + return 0; +} + +const luaL_Reg assetslib[] = { + {"load_texture", lua::wrap}, + {NULL, NULL} +}; diff --git a/src/logic/scripting/lua/libs/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp index 7ebdbc38..4f4c46cd 100644 --- a/src/logic/scripting/lua/libs/libcore.cpp +++ b/src/logic/scripting/lua/libs/libcore.cpp @@ -2,12 +2,10 @@ #include #include "api_lua.hpp" -#include "coders/png.hpp" #include "constants.hpp" #include "assets/Assets.hpp" #include "content/Content.hpp" #include "content/ContentControl.hpp" -#include "debug/Logger.hpp" #include "engine/Engine.hpp" #include "io/engine_paths.hpp" #include "io/io.hpp" @@ -225,41 +223,6 @@ static int l_get_setting_info(lua::State* L) { throw std::runtime_error("unsupported setting type"); } -static void load_texture( - const ubyte* bytes, size_t size, const std::string& destname -) { - try { - engine->getAssets()->store(png::load_texture(bytes, size), destname); - } catch (const std::runtime_error& err) { - debug::Logger logger("lua.corelib"); - logger.error() << 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 { - auto string = lua::bytearray_as_string(L, 1); - load_texture( - reinterpret_cast(string.data()), - string.size(), - lua::require_string(L, 2) - ); - lua::pop(L); - } - return 0; -} - static int l_open_folder(lua::State* L) { platform::open_folder(io::resolve(lua::require_string(L, 1))); return 0; @@ -322,6 +285,5 @@ const luaL_Reg corelib[] = { {"open_folder", lua::wrap}, {"quit", lua::wrap}, {"capture_output", lua::wrap}, - {"__load_texture", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index ca335c49..891b89c7 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -64,6 +64,7 @@ static void create_libs(State* L, StateType stateType) { openlib(L, "__vc_app", applib); } if (stateType == StateType::BASE || stateType == StateType::SCRIPT) { + openlib(L, "assets", assetslib); openlib(L, "audio", audiolib); openlib(L, "console", consolelib); openlib(L, "core", corelib);