diff options
author | cedeel@gmail.com <cedeel@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-06-14 15:06:06 +0200 |
---|---|---|
committer | cedeel@gmail.com <cedeel@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-06-14 15:06:06 +0200 |
commit | 92c59963f82f81aa3202657e7fdbb2592924ede3 (patch) | |
tree | b7eb2474528a4998fa102e3ec9119b908cee08b4 /source/cPlugin_Lua.cpp | |
parent | Added HOOK_WEATHER_CHANGE. (diff) | |
download | cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.gz cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.bz2 cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.lz cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.xz cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.tar.zst cuberite-92c59963f82f81aa3202657e7fdbb2592924ede3.zip |
Diffstat (limited to 'source/cPlugin_Lua.cpp')
-rw-r--r-- | source/cPlugin_Lua.cpp | 194 |
1 files changed, 97 insertions, 97 deletions
diff --git a/source/cPlugin_Lua.cpp b/source/cPlugin_Lua.cpp index d25df0242..a4cf972f4 100644 --- a/source/cPlugin_Lua.cpp +++ b/source/cPlugin_Lua.cpp @@ -1,98 +1,98 @@ -
-#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
-
-#define LUA_USE_POSIX
-#include "cPlugin_Lua.h"
-#include "cPluginManager.h"
-#include "cRoot.h"
-
-extern "C"
-{
- #include "lualib.h"
-}
-
-#include "tolua++.h"
-#include "Bindings.h"
-#include "ManualBindings.h"
-
-bool report_errors(lua_State* lua, int status)
-{
- if ( status!=0 )
- {
- std::string s = lua_tostring(lua, -1);
- LOGERROR("-- %s", s.c_str() );
- lua_pop(lua, 1);
- return true;
- }
- return false;
-}
-
-cPlugin_Lua::~cPlugin_Lua()
-{
- UnloadPlugins();
- if( m_LuaState )
- {
- lua_close( m_LuaState );
- m_LuaState = 0;
- }
-}
-
-cPlugin_Lua::cPlugin_Lua(const char* a_Plugin)
-: m_LuaState( 0 )
-{
- m_FileName.assign( a_Plugin );
-}
-
-bool cPlugin_Lua::Initialize()
-{
- if( !m_LuaState )
- {
- m_LuaState = lua_open();
- luaL_openlibs( m_LuaState );
- tolua_AllToLua_open(m_LuaState);
- ManualBindings::Bind( m_LuaState );
- }
-
- int s = luaL_loadfile(m_LuaState, (std::string("Plugins/") + m_FileName ).c_str() );
- if( report_errors( m_LuaState, s ) )
- {
- LOGERROR("Can't load plugin %s", m_FileName.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", m_FileName.c_str() );
- lua_close( m_LuaState );
- m_LuaState = 0;
- return false;
- }
- return true;
-}
-
-void cPlugin_Lua::AddPlugin( cPlugin* a_Plugin )
-{
- m_Plugins.push_back( a_Plugin );
-}
-
-void cPlugin_Lua::RemovePlugin( cPlugin* a_Plugin )
-{
- m_Plugins.remove( a_Plugin );
- cRoot::Get()->GetPluginManager()->RemovePlugin( a_Plugin, true );
-}
-
-void cPlugin_Lua::UnloadPlugins()
-{
- while( m_Plugins.begin() != m_Plugins.end() )
- {
- RemovePlugin( *m_Plugins.begin() );
- }
-}
-
-lua_State* cPlugin_Lua::GetLuaState()
-{
- return m_LuaState;
+ +#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules + +#define LUA_USE_POSIX +#include "cPlugin_Lua.h" +#include "cPluginManager.h" +#include "cRoot.h" + +extern "C" +{ + #include "lualib.h" +} + +#include "tolua++.h" +#include "Bindings.h" +#include "ManualBindings.h" + +bool report_errors(lua_State* lua, int status) +{ + if ( status!=0 ) + { + std::string s = lua_tostring(lua, -1); + LOGERROR("-- %s", s.c_str() ); + lua_pop(lua, 1); + return true; + } + return false; +} + +cPlugin_Lua::~cPlugin_Lua() +{ + UnloadPlugins(); + if( m_LuaState ) + { + lua_close( m_LuaState ); + m_LuaState = 0; + } +} + +cPlugin_Lua::cPlugin_Lua(const char* a_Plugin) +: m_LuaState( 0 ) +{ + m_FileName.assign( a_Plugin ); +} + +bool cPlugin_Lua::Initialize() +{ + if( !m_LuaState ) + { + m_LuaState = lua_open(); + luaL_openlibs( m_LuaState ); + tolua_AllToLua_open(m_LuaState); + ManualBindings::Bind( m_LuaState ); + } + + int s = luaL_loadfile(m_LuaState, (std::string("Plugins/") + m_FileName ).c_str() ); + if( report_errors( m_LuaState, s ) ) + { + LOGERROR("Can't load plugin %s", m_FileName.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", m_FileName.c_str() ); + lua_close( m_LuaState ); + m_LuaState = 0; + return false; + } + return true; +} + +void cPlugin_Lua::AddPlugin( cPlugin* a_Plugin ) +{ + m_Plugins.push_back( a_Plugin ); +} + +void cPlugin_Lua::RemovePlugin( cPlugin* a_Plugin ) +{ + m_Plugins.remove( a_Plugin ); + cRoot::Get()->GetPluginManager()->RemovePlugin( a_Plugin, true ); +} + +void cPlugin_Lua::UnloadPlugins() +{ + while( m_Plugins.begin() != m_Plugins.end() ) + { + RemovePlugin( *m_Plugins.begin() ); + } +} + +lua_State* cPlugin_Lua::GetLuaState() +{ + return m_LuaState; }
\ No newline at end of file |