summaryrefslogtreecommitdiffstats
path: root/source/cPlugin_NewLua.cpp
diff options
context:
space:
mode:
authorfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-01-26 21:39:46 +0100
committerfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2012-01-26 21:39:46 +0100
commit4e34e438f12abb31f2409ce9d50df8acc2128555 (patch)
tree68d180344b0f730779c18c927fafc07de5ee5b2b /source/cPlugin_NewLua.cpp
parentProbably fixed a deadlock! http://www.mc-server.org/support/index.php?do=details&task_id=147 (diff)
downloadcuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.tar
cuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.tar.gz
cuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.tar.bz2
cuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.tar.lz
cuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.tar.xz
cuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.tar.zst
cuberite-4e34e438f12abb31f2409ce9d50df8acc2128555.zip
Diffstat (limited to 'source/cPlugin_NewLua.cpp')
-rw-r--r--source/cPlugin_NewLua.cpp121
1 files changed, 121 insertions, 0 deletions
diff --git a/source/cPlugin_NewLua.cpp b/source/cPlugin_NewLua.cpp
new file mode 100644
index 000000000..90647e011
--- /dev/null
+++ b/source/cPlugin_NewLua.cpp
@@ -0,0 +1,121 @@
+#define LUA_USE_POSIX
+#include "cPlugin_NewLua.h"
+#include "cMCLogger.h"
+
+extern "C"
+{
+#include "lualib.h"
+}
+
+#include "tolua++.h"
+#include "Bindings.h"
+#include "ManualBindings.h"
+
+extern bool report_errors(lua_State* lua, int status);
+
+cPlugin_NewLua::cPlugin_NewLua( const char* a_PluginName )
+ : m_LuaState( 0 )
+{
+ m_Directory = a_PluginName;
+}
+
+cPlugin_NewLua::~cPlugin_NewLua()
+{
+ if( m_LuaState )
+ {
+ lua_close( m_LuaState );
+ m_LuaState = 0;
+ }
+}
+
+bool cPlugin_NewLua::Initialize()
+{
+ if( !m_LuaState )
+ {
+ m_LuaState = lua_open();
+ luaL_openlibs( m_LuaState );
+ tolua_AllToLua_open(m_LuaState);
+ ManualBindings::Bind( m_LuaState );
+ }
+
+ std::string PluginFiles[] = { std::string("Plugins/") + m_Directory + "/tick.lua"
+ ,std::string("Plugins/") + m_Directory + "/main.lua" };
+
+
+ // Load all files for this plugin, and execute them
+ for( int i = 0; i < 2; ++i )
+ {
+ int s = luaL_loadfile(m_LuaState, PluginFiles[i].c_str() );
+ if( report_errors( m_LuaState, s ) )
+ {
+ LOGERROR("Can't load plugin %s because of an error in file %s", m_Directory.c_str(), PluginFiles[i].c_str() );
+ lua_close( m_LuaState );
+ m_LuaState = 0;
+ return false;
+ }
+
+ s = lua_pcall(m_LuaState, 0, LUA_MULTRET, 0);
+ if( report_errors( m_LuaState, s ) )
+ {
+ LOGERROR("Error in plugin %s in file %s", m_Directory.c_str(), PluginFiles[i].c_str() );
+ lua_close( m_LuaState );
+ m_LuaState = 0;
+ return false;
+ }
+ }
+
+
+ // Call intialize function
+ lua_getglobal(m_LuaState, "Initialize");
+ if(!lua_isfunction(m_LuaState,-1))
+ {
+ LOGWARN("Error in plugin %s: Could not find function Initialize()", m_Directory.c_str() );
+ lua_pop(m_LuaState,1);
+ lua_close( m_LuaState );
+ m_LuaState = 0;
+ return false;
+ }
+
+ tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
+
+ // do the call (1 arguments, 1 result)
+ int s = lua_pcall(m_LuaState, 1, 1, 0);
+ if( report_errors( m_LuaState, s ) )
+ {
+ LOGWARN("Error in plugin %s calling function Initialize()", m_Directory.c_str() );
+ lua_close( m_LuaState );
+ m_LuaState = 0;
+ return false;
+ }
+
+ if( !lua_isboolean( m_LuaState, -1 ) )
+ {
+ LOGWARN("Error in plugin %s Initialize() must return a boolean value!", m_Directory.c_str() );
+ lua_close( m_LuaState );
+ m_LuaState = 0;
+ return false;
+ }
+
+ bool bSuccess = (tolua_toboolean( m_LuaState, -1, 0) > 0);
+ return bSuccess;
+}
+
+void cPlugin_NewLua::Tick(float a_Dt)
+{
+ lua_getglobal(m_LuaState, "Tick");
+ if(!lua_isfunction(m_LuaState,-1))
+ {
+ LOGWARN("Error in plugin %s: Could not find function Tick()", m_Directory.c_str() );
+ lua_pop(m_LuaState,1);
+ return;
+ }
+
+ tolua_pushnumber( m_LuaState, a_Dt );
+
+ // do the call (1 arguments, 0 result)/
+ int s = lua_pcall(m_LuaState, 1, 0, 0);
+ if( report_errors( m_LuaState, s ) )
+ {
+ LOGWARN("Error in plugin %s calling function Tick()", m_Directory.c_str() );
+ }
+} \ No newline at end of file