critical lua fixes

This commit is contained in:
MihailRis 2024-02-19 12:58:29 +03:00
parent 9d82e3416b
commit 086bcec9c2
9 changed files with 37 additions and 11 deletions

View File

@ -51,6 +51,7 @@ std::shared_ptr<Inventory> Inventories::clone(int64_t id) {
auto origptr = reinterpret_cast<const Inventory*>(original.get());
auto clone = std::make_shared<Inventory>(*origptr);
clone->setId(level.getWorld()->getNextInventoryId());
store(clone);
return clone;
}

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,7 @@
#include <iostream>
#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<gui::Button*>(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;
}

View File

@ -1,16 +1,18 @@
#ifndef LOGIC_SCRIPTING_API_LIBGUI_H_
#define LOGIC_SCRIPTING_API_LIBGUI_H_
#include <lua.hpp>
#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<l_gui_getviewport>},
{"getattr", lua_wrap_errors<l_gui_getattr>},
{"setattr", lua_wrap_errors<l_gui_setattr>},
{"get_env", lua_wrap_errors<l_gui_get_env>},
{NULL, NULL}
};

View File

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

View File

@ -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;
}
};
}