2012-01-26 12:39:46 -08:00
|
|
|
#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"
|
|
|
|
|
2012-01-26 14:21:23 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "wdirent.h"
|
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
2012-01-26 12:39:46 -08:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2012-01-26 14:21:23 -08:00
|
|
|
std::string PluginPath = std::string("Plugins/") + m_Directory + "/";
|
2012-01-26 12:39:46 -08:00
|
|
|
|
|
|
|
// Load all files for this plugin, and execute them
|
2012-01-26 14:21:23 -08:00
|
|
|
DIR* dp;
|
|
|
|
struct dirent *entry;
|
|
|
|
if(dp = opendir( PluginPath.c_str() ))
|
|
|
|
{
|
|
|
|
while(entry = readdir(dp))
|
2012-01-26 12:39:46 -08:00
|
|
|
{
|
2012-01-26 14:21:23 -08:00
|
|
|
std::string FileName = entry->d_name;
|
|
|
|
if( FileName.find(".lua") != std::string::npos )
|
|
|
|
{
|
|
|
|
std::string Path = PluginPath + FileName;
|
|
|
|
int s = luaL_loadfile(m_LuaState, Path.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(), Path.c_str() );
|
|
|
|
lua_close( m_LuaState );
|
|
|
|
m_LuaState = 0;
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-26 12:39:46 -08:00
|
|
|
|
2012-01-26 14:21:23 -08:00
|
|
|
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(), Path.c_str() );
|
|
|
|
lua_close( m_LuaState );
|
|
|
|
m_LuaState = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 12:39:46 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Call intialize function
|
2012-01-26 15:10:49 -08:00
|
|
|
if( !PushFunction("Initialize") )
|
2012-01-26 12:39:46 -08:00
|
|
|
{
|
|
|
|
lua_close( m_LuaState );
|
|
|
|
m_LuaState = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
|
|
|
|
|
2012-01-26 15:10:49 -08:00
|
|
|
|
|
|
|
if( !CallFunction(1, 1, "Initialize") )
|
2012-01-26 12:39:46 -08:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2012-01-26 15:10:49 -08:00
|
|
|
if( !PushFunction("Tick") )
|
|
|
|
return;
|
|
|
|
|
|
|
|
tolua_pushnumber( m_LuaState, a_Dt );
|
|
|
|
|
|
|
|
CallFunction(1, 0, "Tick");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPlugin_NewLua::OnPlayerJoin( cPlayer* a_Player )
|
|
|
|
{
|
|
|
|
if( !PushFunction("OnPlayerJoin") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
|
|
|
|
if( !CallFunction(1, 1, "OnPlayerJoin") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPlugin_NewLua::OnLogin( cPacket_Login* a_PacketData )
|
|
|
|
{
|
|
|
|
if( !PushFunction("OnLogin") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_Login");
|
|
|
|
|
|
|
|
if( !CallFunction(1, 1, "OnLogin") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPlugin_NewLua::OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player )
|
|
|
|
{
|
|
|
|
if( !PushFunction("OnBlockPlace") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockPlace");
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
|
|
|
|
if( !CallFunction(2, 1, "OnBlockPlace") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer )
|
|
|
|
{
|
|
|
|
if( !PushFunction("OnKilled") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_Killed, "cPawn");
|
|
|
|
tolua_pushusertype(m_LuaState, a_Killer, "cEntity");
|
|
|
|
|
|
|
|
if( !CallFunction(2, 1, "OnKilled") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
|
|
|
bool cPlugin_NewLua::PushFunction( const char* a_FunctionName )
|
|
|
|
{
|
|
|
|
lua_getglobal(m_LuaState, a_FunctionName);
|
2012-01-26 12:39:46 -08:00
|
|
|
if(!lua_isfunction(m_LuaState,-1))
|
|
|
|
{
|
2012-01-26 15:10:49 -08:00
|
|
|
LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName );
|
2012-01-26 12:39:46 -08:00
|
|
|
lua_pop(m_LuaState,1);
|
2012-01-26 15:10:49 -08:00
|
|
|
return false;
|
2012-01-26 12:39:46 -08:00
|
|
|
}
|
2012-01-26 15:10:49 -08:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-26 12:39:46 -08:00
|
|
|
|
2012-01-26 15:10:49 -08:00
|
|
|
bool cPlugin_NewLua::CallFunction( int a_NumArgs, int a_NumResults, const char* a_FunctionName )
|
|
|
|
{
|
|
|
|
int s = lua_pcall(m_LuaState, a_NumArgs, a_NumResults, 0);
|
2012-01-26 12:39:46 -08:00
|
|
|
if( report_errors( m_LuaState, s ) )
|
|
|
|
{
|
2012-01-26 15:10:49 -08:00
|
|
|
LOGWARN("Error in plugin %s calling function %s()", m_Directory.c_str(), a_FunctionName );
|
|
|
|
return false;
|
2012-01-26 12:39:46 -08:00
|
|
|
}
|
2012-01-26 15:10:49 -08:00
|
|
|
return true;
|
2012-01-26 12:39:46 -08:00
|
|
|
}
|