diff options
author | madmaxoft <github@xoft.cz> | 2013-08-06 19:28:09 +0200 |
---|---|---|
committer | madmaxoft <github@xoft.cz> | 2013-08-06 19:28:09 +0200 |
commit | c55fabb5ad34ec57760b90ea11960f7f426990db (patch) | |
tree | eaa53bca6eff7fca7d4547b46860222d9580af9c /source/LuaScript.cpp | |
parent | Fixed compilation in ManualBindings (diff) | |
download | cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.tar cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.tar.gz cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.tar.bz2 cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.tar.lz cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.tar.xz cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.tar.zst cuberite-c55fabb5ad34ec57760b90ea11960f7f426990db.zip |
Diffstat (limited to 'source/LuaScript.cpp')
-rw-r--r-- | source/LuaScript.cpp | 232 |
1 files changed, 15 insertions, 217 deletions
diff --git a/source/LuaScript.cpp b/source/LuaScript.cpp index 8d92c238f..f27d1793f 100644 --- a/source/LuaScript.cpp +++ b/source/LuaScript.cpp @@ -9,31 +9,16 @@ extern "C" { -#include "lualib.h" + #include "lualib.h" } - #include "tolua++.h" -#include "Bindings.h" -#include "ManualBindings.h" - -// fwd: SQLite/lsqlite3.c -extern "C" -{ - LUALIB_API int luaopen_lsqlite3(lua_State * L); -} - -// fwd: LuaExpat/lxplib.c: -extern "C" -{ - int luaopen_lxp(lua_State * L); -} cLuaScript::cLuaScript() - : m_LuaState(NULL) + : m_LuaState("cLuaScript") { } @@ -42,166 +27,45 @@ cLuaScript::cLuaScript() -cLuaScript::~cLuaScript() -{ - if( m_LuaState ) - { - lua_close( m_LuaState ); - m_LuaState = 0; - } -} - - - - - void cLuaScript::Initialize() { // Check to see if this script has not been initialized before - ASSERT(!m_LuaState); + ASSERT(!m_LuaState.IsValid()); // Create a Lua state and bind all libraries to it - m_LuaState = lua_open(); - luaL_openlibs(m_LuaState); - tolua_AllToLua_open(m_LuaState); - ManualBindings::Bind(m_LuaState); - luaopen_lsqlite3(m_LuaState); - luaopen_lxp(m_LuaState); + m_LuaState.Create(); } -bool cLuaScript::LoadFile( const char* a_FilePath ) +bool cLuaScript::LoadFile(const char * a_FilePath) { // Make sure the plugin is initialized - ASSERT(m_LuaState); - - // Load the file into the Lua state - int s = luaL_loadfile(m_LuaState, a_FilePath ); - if (ReportErrors(s)) - { - return false; - } - return true; -} - - - - - -bool cLuaScript::Execute() -{ - // Make sure we got a Lua state - ASSERT(m_LuaState); - - // Execute the script as it is right now - int s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0); - if( ReportErrors( s ) ) - { - return false; - } - return true; -} - - + ASSERT(m_LuaState.IsValid()); - - -bool cLuaScript::ReportErrors( int a_Status ) -{ - if (a_Status == 0) - { - // No error to report - return false; - } - - // Status was set to error so get the error from the Lua state and log it - LOGERROR("LUA: %s", lua_tostring(m_LuaState, -1)); - lua_pop(m_LuaState, 1); - - // Return true to indicate that an error was returned - return true; + return m_LuaState.LoadFile(a_FilePath); } -bool cLuaScript::LuaPushFunction( const char * a_FunctionName, bool a_bLogError /*= true*/ ) +bool cLuaScript::CallShowPage(cWebAdmin & a_WebAdmin, HTTPTemplateRequest & a_Request, AString & a_ReturnedString) { - ASSERT(m_LuaState); - - // Find and push the function on the Lua stack - lua_getglobal(m_LuaState, a_FunctionName); - - // Make sure we found a function - if (!lua_isfunction(m_LuaState, -1)) - { - if (a_bLogError) - { - LOGWARN("LUA: Could not find function %s()", a_FunctionName); - } - - // Pop the pushed 'object' back - lua_pop(m_LuaState, 1); - return false; - } - - // Successfully pushed a function to the Lua stack - return true; -} - - - - - -bool cLuaScript::LuaCallFunction( int a_NumArgs, int a_NumResults, const char * a_FunctionName ) -{ - ASSERT(m_LuaState); - - // Make sure there's a lua function on the stack - ASSERT(lua_isfunction(m_LuaState, -a_NumArgs - 1)); - - // Call the desired function - int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0); - - // Check for errors - if (ReportErrors(s)) + ASSERT(m_LuaState.IsValid()); + + m_LuaState.PushFunction("ShowPage"); + m_LuaState.PushUserType(&a_WebAdmin, "cWebAdmin"); + m_LuaState.PushUserType(&a_Request, "HTTPTemplateRequest"); + if (!m_LuaState.CallFunction(1)) { - LOGWARN("LUA: Error calling function %s()", a_FunctionName); return false; } - - // Successfully executed function - return true; -} - - - - - -bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString ) -{ - // Make sure we have the required things to call a function - ASSERT(m_LuaState); - ASSERT(a_Function); - - // Push the desired function on the stack - if (!LuaPushFunction(a_Function)) - { - return false; - } - - if (!LuaCallFunction(0, 1, a_Function)) - { - return false; - } - if (lua_isstring(m_LuaState, -1)) { - ReturnedString = tolua_tostring(m_LuaState, -1, ""); + a_ReturnedString.assign(tolua_tostring(m_LuaState, -1, "")); } lua_pop(m_LuaState, 1); return true; @@ -210,69 +74,3 @@ bool cLuaScript::CallFunction( const char* a_Function, AString& ReturnedString ) - -bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType, AString& ReturnedString ) -{ - // Make sure we have the required things to call a function - ASSERT(m_LuaState); - ASSERT(a_Function); - - // Push the desired function on the stack - if (!LuaPushFunction(a_Function)) - { - return false; - } - - tolua_pushusertype(m_LuaState, a_UserType.Object, a_UserType.ClassName); - - if (!LuaCallFunction(1, 1, a_Function)) - { - return false; - } - - if (lua_isstring(m_LuaState, -1)) - { - ReturnedString = tolua_tostring(m_LuaState, -1, ""); - } - lua_pop(m_LuaState, 1); - return true; -} - - - - - -bool cLuaScript::CallFunction( const char* a_Function, const sLuaUsertype& a_UserType1, const sLuaUsertype& a_UserType2, AString& ReturnedString ) -{ - // Make sure we have the required things to call a function - ASSERT(m_LuaState); - ASSERT(a_Function); - - // Push the desired function on the stack - if (!LuaPushFunction(a_Function)) - { - return false; - } - - tolua_pushusertype(m_LuaState, a_UserType1.Object, a_UserType1.ClassName); - tolua_pushusertype(m_LuaState, a_UserType2.Object, a_UserType2.ClassName); - - if (!LuaCallFunction(2, 1, a_Function)) - { - return false; - } - - if (lua_isstring(m_LuaState, -1)) - { - ReturnedString = tolua_tostring(m_LuaState, -1, ""); - } - lua_pop(m_LuaState, 1); - return true; -} - - - - - - - |