summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaState.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2016-08-16 11:55:12 +0200
committerMattes D <github@xoft.cz>2016-08-22 20:01:23 +0200
commit89c9c6fe46ce4a273a15b7f882c3d0935a868145 (patch)
treec31f1d1e91172966fadf93cf4b8ad41f26c3fc0a /src/Bindings/LuaState.cpp
parentDelay EntityChangedWorld players' callback until Entity fully linked to world (#3330) (diff)
downloadcuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.tar
cuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.tar.gz
cuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.tar.bz2
cuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.tar.lz
cuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.tar.xz
cuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.tar.zst
cuberite-89c9c6fe46ce4a273a15b7f882c3d0935a868145.zip
Diffstat (limited to 'src/Bindings/LuaState.cpp')
-rw-r--r--src/Bindings/LuaState.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index fd29e8e34..b5832802d 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -341,6 +341,33 @@ void cLuaState::cStackTable::ForEachArrayElement(std::function<bool(cLuaState &
+void cLuaState::cStackTable::ForEachElement(std::function<bool(cLuaState & a_LuaState)> a_ElementCallback) const
+{
+ #ifdef _DEBUG
+ auto stackTop = lua_gettop(m_LuaState);
+ #endif
+ lua_pushvalue(m_LuaState, m_StackPos); // Stk: <table>
+ lua_pushnil(m_LuaState); // Stk: <table> nil
+ while (lua_next(m_LuaState, -2)) // Stk: <table> <key> <val>
+ {
+ auto shouldAbort = a_ElementCallback(m_LuaState);
+ ASSERT(lua_gettop(m_LuaState) == stackTop + 3); // The element callback must not change the Lua stack below the value
+ lua_pop(m_LuaState, 1); // Stk: <table> <key>
+ if (shouldAbort)
+ {
+ // The callback wants to abort
+ lua_pop(m_LuaState, 2); // Stk: empty
+ return;
+ }
+ }
+ // Stk: <table>
+ lua_pop(m_LuaState, 1); // Stk: empty
+}
+
+
+
+
+
////////////////////////////////////////////////////////////////////////////////
// cLuaState:
@@ -748,6 +775,24 @@ void cLuaState::Push(const AString & a_String)
+void cLuaState::Push(const AStringMap & a_Dictionary)
+{
+ ASSERT(IsValid());
+
+ lua_createtable(m_LuaState, 0, static_cast<int>(a_Dictionary.size()));
+ int newTable = lua_gettop(m_LuaState);
+ for (const auto & item: a_Dictionary)
+ {
+ Push(item.first); // key
+ Push(item.second); // value
+ lua_rawset(m_LuaState, newTable);
+ }
+}
+
+
+
+
+
void cLuaState::Push(const AStringVector & a_Vector)
{
ASSERT(IsValid());
@@ -1113,6 +1158,37 @@ bool cLuaState::GetStackValue(int a_StackPos, AString & a_Value)
+bool cLuaState::GetStackValue(int a_StackPos, AStringMap & a_Value)
+{
+ // Retrieve all values in a string => string dictionary table:
+ if (!lua_istable(m_LuaState, a_StackPos))
+ {
+ return false;
+ }
+ cStackTable tbl(*this, a_StackPos);
+ bool isValid = true;
+ tbl.ForEachElement([&isValid, &a_Value](cLuaState & a_LuaState)
+ {
+ AString key, val;
+ if (a_LuaState.GetStackValues(-2, key, val))
+ {
+ a_Value[key] = val;
+ }
+ else
+ {
+ isValid = false;
+ return true;
+ }
+ return false;
+ }
+ );
+ return isValid;
+}
+
+
+
+
+
bool cLuaState::GetStackValue(int a_StackPos, bool & a_ReturnedVal)
{
a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0);