summaryrefslogtreecommitdiffstats
path: root/src/Bindings/LuaState.cpp
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-11-11 10:32:42 +0100
committerMattes D <github@xoft.cz>2015-12-01 10:35:07 +0100
commitb8fbba5eb92cda32b13d65f3704adf778da82f38 (patch)
tree0c6e8f47ae152e5ffade6f0a7457505101764753 /src/Bindings/LuaState.cpp
parentMerge pull request #2696 from Gargaj/breeding (diff)
downloadcuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar
cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.gz
cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.bz2
cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.lz
cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.xz
cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.tar.zst
cuberite-b8fbba5eb92cda32b13d65f3704adf778da82f38.zip
Diffstat (limited to 'src/Bindings/LuaState.cpp')
-rw-r--r--src/Bindings/LuaState.cpp76
1 files changed, 74 insertions, 2 deletions
diff --git a/src/Bindings/LuaState.cpp b/src/Bindings/LuaState.cpp
index 385b1f659..f5b2cac4a 100644
--- a/src/Bindings/LuaState.cpp
+++ b/src/Bindings/LuaState.cpp
@@ -286,7 +286,7 @@ void cLuaState::AddPackagePath(const AString & a_PathVariable, const AString & a
bool cLuaState::LoadFile(const AString & a_FileName, bool a_LogWarnings)
{
ASSERT(IsValid());
-
+
// Load the file:
int s = luaL_loadfile(m_LuaState, a_FileName.c_str());
if (s != 0)
@@ -310,7 +310,42 @@ bool cLuaState::LoadFile(const AString & a_FileName, bool a_LogWarnings)
lua_pop(m_LuaState, 1);
return false;
}
-
+
+ return true;
+}
+
+
+
+
+
+bool cLuaState::LoadString(const AString & a_StringToLoad, const AString & a_FileName, bool a_LogWarnings)
+{
+ ASSERT(IsValid());
+
+ // Load the file:
+ int s = luaL_loadstring(m_LuaState, a_StringToLoad.c_str());
+ if (s != 0)
+ {
+ if (a_LogWarnings)
+ {
+ LOGWARNING("Can't load %s because of a load error in string from \"%s\": %d (%s)", m_SubsystemName.c_str(), a_FileName.c_str(), s, lua_tostring(m_LuaState, -1));
+ }
+ lua_pop(m_LuaState, 1);
+ return false;
+ }
+
+ // Execute the globals:
+ s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
+ if (s != 0)
+ {
+ if (a_LogWarnings)
+ {
+ LOGWARNING("Can't load %s because of an initialization error in string from \"%s\": %d (%s)", m_SubsystemName.c_str(), a_FileName.c_str(), s, lua_tostring(m_LuaState, -1));
+ }
+ lua_pop(m_LuaState, 1);
+ return false;
+ }
+
return true;
}
@@ -952,6 +987,11 @@ cLuaState::cStackValue cLuaState::WalkToValue(const AString & a_Name)
// Remove the previous value from the stack (keep only the new one):
lua_remove(m_LuaState, -2);
} // for elem - path[]
+ if (lua_isnil(m_LuaState, -1))
+ {
+ lua_pop(m_LuaState, 1);
+ return cStackValue();
+ }
return cStackValue(*this);
}
@@ -959,6 +999,38 @@ cLuaState::cStackValue cLuaState::WalkToValue(const AString & a_Name)
+cLuaState::cStackValue cLuaState::WalkToNamedGlobal(const AString & a_Name)
+{
+ // Iterate over path and replace the top of the stack with the walked element
+ lua_getglobal(m_LuaState, "_G");
+ auto path = StringSplit(a_Name, ".");
+ for (const auto & elem: path)
+ {
+ // If the value is not a table, bail out (error):
+ if (!lua_istable(m_LuaState, -1))
+ {
+ lua_pop(m_LuaState, 1);
+ return cStackValue();
+ }
+
+ // Get the next part of the path:
+ lua_getfield(m_LuaState, -1, elem.c_str());
+
+ // Remove the previous value from the stack (keep only the new one):
+ lua_remove(m_LuaState, -2);
+ } // for elem - path[]
+ if (lua_isnil(m_LuaState, -1))
+ {
+ lua_pop(m_LuaState, 1);
+ return cStackValue();
+ }
+ return std::move(cStackValue(*this));
+}
+
+
+
+
+
bool cLuaState::CallFunction(int a_NumResults)
{
ASSERT (m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first