From 086bcec9c23c197261eb3fb79e841b137d953410 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 19 Feb 2024 12:58:29 +0300 Subject: [PATCH] critical lua fixes --- src/items/Inventories.cpp | 1 + src/items/Inventory.cpp | 4 ++++ src/items/Inventory.h | 2 ++ src/logic/scripting/lua/LuaState.cpp | 2 +- src/logic/scripting/lua/LuaState.h | 2 +- src/logic/scripting/lua/libgui.cpp | 15 +++++++++++++-- src/logic/scripting/lua/libgui.h | 10 ++++++---- src/logic/scripting/lua/lua_util.h | 6 ++++-- src/logic/scripting/scripting_functional.cpp | 6 +++++- 9 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/items/Inventories.cpp b/src/items/Inventories.cpp index 53167093..07b33706 100644 --- a/src/items/Inventories.cpp +++ b/src/items/Inventories.cpp @@ -51,6 +51,7 @@ std::shared_ptr Inventories::clone(int64_t id) { auto origptr = reinterpret_cast(original.get()); auto clone = std::make_shared(*origptr); clone->setId(level.getWorld()->getNextInventoryId()); + store(clone); return clone; } diff --git a/src/items/Inventory.cpp b/src/items/Inventory.cpp index 256e9083..1e682e11 100644 --- a/src/items/Inventory.cpp +++ b/src/items/Inventory.cpp @@ -5,6 +5,10 @@ Inventory::Inventory(int64_t id, size_t size) : id(id), slots(size) { } +Inventory::Inventory(const Inventory& orig) { + this->slots = orig.slots; +} + ItemStack& Inventory::getSlot(size_t index) { return slots.at(index); } diff --git a/src/items/Inventory.h b/src/items/Inventory.h index 68226598..677f8d9a 100644 --- a/src/items/Inventory.h +++ b/src/items/Inventory.h @@ -19,6 +19,8 @@ class Inventory : Serializable { public: Inventory(int64_t id, size_t size); + Inventory(const Inventory& orig); + ItemStack& getSlot(size_t index); size_t findEmptySlot(size_t begin=0, size_t end=-1) const; size_t findSlotByItem(itemid_t id, size_t begin=0, size_t end=-1); diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index 381d4edc..ff0bca6d 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -33,7 +33,7 @@ lua::LuaState::LuaState() { setglobal(envName(0)); } -const std::string lua::LuaState::envName(int env) const { +const std::string lua::LuaState::envName(int env) { return "_ENV"+util::mangleid(env); } diff --git a/src/logic/scripting/lua/LuaState.h b/src/logic/scripting/lua/LuaState.h index 8c76f233..bff884d3 100644 --- a/src/logic/scripting/lua/LuaState.h +++ b/src/logic/scripting/lua/LuaState.h @@ -25,7 +25,7 @@ namespace lua { LuaState(); ~LuaState(); - const std::string envName(int env) const; + static const std::string envName(int env); void loadbuffer(int env, const std::string& src, const std::string& file); int gettop() const; int pushivec3(luaint x, luaint y, luaint z); diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index d8b8f960..6819ee4f 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -3,6 +3,7 @@ #include #include "../scripting.h" #include "lua_util.h" +#include "LuaState.h" #include "../../../engine.h" #include "../../../assets/Assets.h" @@ -97,9 +98,9 @@ int l_gui_setattr(lua_State* L) { auto node = getDocumentNode(L, docname, element); if (attr == "pos") { - node->setCoord(lua::tovec2(L, 1)); + node->setCoord(lua::tovec2(L, 4)); } else if (attr == "size") { - node->setSize(lua::tovec2(L, 1)); + node->setSize(lua::tovec2(L, 4)); } else { if (setattr(L, dynamic_cast(node), attr)) return 0; @@ -108,3 +109,13 @@ int l_gui_setattr(lua_State* L) { } return 0; } + +int l_gui_get_env(lua_State* L) { + auto name = lua_tostring(L, 1); + auto doc = scripting::engine->getAssets()->getLayout(name); + if (doc == nullptr) { + luaL_error(L, "document '%s' not found", name); + } + lua_getglobal(L, lua::LuaState::envName(doc->getEnvironment()).c_str()); + return 1; +} diff --git a/src/logic/scripting/lua/libgui.h b/src/logic/scripting/lua/libgui.h index 056b40ea..bbc225b8 100644 --- a/src/logic/scripting/lua/libgui.h +++ b/src/logic/scripting/lua/libgui.h @@ -1,16 +1,18 @@ #ifndef LOGIC_SCRIPTING_API_LIBGUI_H_ #define LOGIC_SCRIPTING_API_LIBGUI_H_ -#include +#include "lua_commons.h" extern int l_gui_getviewport(lua_State* L); extern int l_gui_getattr(lua_State* L); extern int l_gui_setattr(lua_State* L); +extern int l_gui_get_env(lua_State* L); static const luaL_Reg guilib [] = { - {"get_viewport", l_gui_getviewport}, - {"getattr", l_gui_getattr}, - {"setattr", l_gui_setattr}, + {"get_viewport", lua_wrap_errors}, + {"getattr", lua_wrap_errors}, + {"setattr", lua_wrap_errors}, + {"get_env", lua_wrap_errors}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_util.h b/src/logic/scripting/lua/lua_util.h index fa750fb9..55fa43fe 100644 --- a/src/logic/scripting/lua/lua_util.h +++ b/src/logic/scripting/lua/lua_util.h @@ -87,10 +87,12 @@ namespace lua { } inline glm::vec2 tovec2(lua_State* L, int idx) { - lua_rawgeti(L, idx, 1); + lua_pushvalue(L, idx); + lua_rawgeti(L, -1, 1); lua::luanumber x = lua_tonumber(L, -1); lua_pop(L, -1); - lua_rawgeti(L, idx, 1); + lua_rawgeti(L, -2, 2); lua::luanumber y = lua_tonumber(L, -1); lua_pop(L, -1); + lua_pop(L, -1); return glm::vec2(x, y); } } diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 520de0a0..e075590b 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -17,7 +17,11 @@ runnable scripting::create_runnable( const std::string& file ) { return [=](){ - state->execute(env, src, file); + try { + state->execute(env, src, file); + } catch (const lua::luaerror& err) { + std::cerr << err.what() << std::endl; + } }; }