summaryrefslogtreecommitdiffstats
path: root/src/Bindings
diff options
context:
space:
mode:
authorMattes D <github@xoft.cz>2015-12-03 13:07:23 +0100
committerMattes D <github@xoft.cz>2015-12-03 13:07:23 +0100
commit3fb356648453169758a9f5649f0ed4c5925d911f (patch)
tree57c90ff51aaeac4b1c2e05538a9f486da0a19f28 /src/Bindings
parentMerge pull request #2696 from Gargaj/breeding (diff)
parentChanged Nether defaults to use PieceStructures. (diff)
downloadcuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.gz
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.bz2
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.lz
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.xz
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.tar.zst
cuberite-3fb356648453169758a9f5649f0ed4c5925d911f.zip
Diffstat (limited to 'src/Bindings')
-rw-r--r--src/Bindings/LuaState.cpp76
-rw-r--r--src/Bindings/LuaState.h24
2 files changed, 92 insertions, 8 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
diff --git a/src/Bindings/LuaState.h b/src/Bindings/LuaState.h
index 4f688be6c..6b600e150 100644
--- a/src/Bindings/LuaState.h
+++ b/src/Bindings/LuaState.h
@@ -207,24 +207,30 @@ public:
/** Detaches a previously attached state. */
void Detach(void);
-
+
/** Returns true if the m_LuaState is valid */
bool IsValid(void) const { return (m_LuaState != nullptr); }
/** Returns the name of the subsystem, as specified when the instance was created. */
AString GetSubsystemName(void) const { return m_SubsystemName; }
-
+
/** Adds the specified path to package.<a_PathVariable> */
void AddPackagePath(const AString & a_PathVariable, const AString & a_Path);
-
+
/** Loads the specified file
Returns false and optionally logs a warning to the console if not successful (but the LuaState is kept open).
m_SubsystemName is displayed in the warning log message. */
bool LoadFile(const AString & a_FileName, bool a_LogWarnings = true);
-
+
+ /** Loads the specified string.
+ Returns false and optionally logs a warning to the console if not successful (but the LuaState is kept open).
+ a_FileName is the original filename from where the string was read, and is used only for logging. It may be empty.
+ m_SubsystemName is displayed in the warning log message. */
+ bool LoadString(const AString & a_StringToLoad, const AString & a_FileName, bool a_LogWarnings = true);
+
/** Returns true if a_FunctionName is a valid Lua function that can be called */
bool HasFunction(const char * a_FunctionName);
-
+
void PushNil(void);
// Push a const value onto the stack (keep alpha-sorted):
@@ -291,11 +297,17 @@ public:
}
/** Pushes the named value in the table at the top of the stack.
- a_Name may be a path containing multiple table levels, such as "_G.cChatColor.Blue".
+ a_Name may be a path containing multiple table levels, such as "cChatColor.Blue".
If the value is found, it is pushed on top of the stack and the returned cStackValue is valid.
If the value is not found, the stack is unchanged and the returned cStackValue is invalid. */
cStackValue WalkToValue(const AString & a_Name);
+ /** Pushes the named value in the global table to the top of the stack.
+ a_Name may be a path containing multiple table levels, such as "cChatColor.Blue".
+ If the value is found in the global table, it is pushed to the top of the stack and the returned cStackValue is valid.
+ If the value is not found, the stack is unchanged and the returned cStackValue is invalid. */
+ cStackValue WalkToNamedGlobal(const AString & a_Name);
+
/** Retrieves the named value in the table at the top of the Lua stack.
a_Name may be a path containing multiple table levels, such as "_G.cChatColor.Blue".
Returns true if the value was successfully retrieved, false on error. */