2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-01-26 15:39:46 -05:00
|
|
|
#define LUA_USE_POSIX
|
|
|
|
#include "cPlugin_NewLua.h"
|
|
|
|
#include "cMCLogger.h"
|
2012-01-27 18:47:32 -05:00
|
|
|
#include "cWebPlugin_Lua.h"
|
2012-01-26 15:39:46 -05:00
|
|
|
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#include "lualib.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "tolua++.h"
|
|
|
|
#include "Bindings.h"
|
|
|
|
#include "ManualBindings.h"
|
|
|
|
|
2012-01-27 18:47:32 -05:00
|
|
|
#ifdef _WIN32
|
2012-03-25 10:24:51 -04:00
|
|
|
// #include "wdirent.h"
|
2012-01-27 18:47:32 -05:00
|
|
|
#else
|
|
|
|
#include <dirent.h>
|
2012-01-26 17:21:23 -05:00
|
|
|
#endif
|
|
|
|
|
2012-01-26 15:39:46 -05: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()
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-01-27 18:47:32 -05:00
|
|
|
for( WebPluginList::iterator itr = m_WebPlugins.begin(); itr != m_WebPlugins.end(); ++itr )
|
|
|
|
{
|
|
|
|
delete *itr;
|
|
|
|
}
|
|
|
|
m_WebPlugins.clear();
|
|
|
|
|
2012-01-26 15:39:46 -05:00
|
|
|
if( m_LuaState )
|
|
|
|
{
|
|
|
|
lua_close( m_LuaState );
|
|
|
|
m_LuaState = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cPlugin_NewLua::Initialize()
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-01-26 15:39:46 -05:00
|
|
|
if( !m_LuaState )
|
|
|
|
{
|
|
|
|
m_LuaState = lua_open();
|
|
|
|
luaL_openlibs( m_LuaState );
|
|
|
|
tolua_AllToLua_open(m_LuaState);
|
|
|
|
ManualBindings::Bind( m_LuaState );
|
|
|
|
}
|
|
|
|
|
2012-01-26 17:21:23 -05:00
|
|
|
std::string PluginPath = std::string("Plugins/") + m_Directory + "/";
|
2012-01-26 15:39:46 -05:00
|
|
|
|
|
|
|
// Load all files for this plugin, and execute them
|
2012-03-25 10:24:51 -04:00
|
|
|
AStringList Files = GetDirectoryContents(PluginPath.c_str());
|
|
|
|
for (AStringList::const_iterator itr = Files.begin(); itr != Files.end(); ++itr)
|
2012-01-27 18:47:32 -05:00
|
|
|
{
|
2012-03-25 10:24:51 -04:00
|
|
|
if (itr->rfind(".lua") == AString::npos)
|
2012-01-26 15:39:46 -05:00
|
|
|
{
|
2012-03-25 10:24:51 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
AString Path = PluginPath + *itr;
|
|
|
|
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 15:39:46 -05:00
|
|
|
}
|
|
|
|
|
2012-03-25 10:24:51 -04: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;
|
|
|
|
}
|
|
|
|
} // for itr - Files[]
|
2012-01-26 15:39:46 -05:00
|
|
|
|
|
|
|
// Call intialize function
|
2012-01-26 18:10:49 -05:00
|
|
|
if( !PushFunction("Initialize") )
|
2012-01-26 15:39:46 -05:00
|
|
|
{
|
|
|
|
lua_close( m_LuaState );
|
|
|
|
m_LuaState = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, this, "cPlugin_NewLua");
|
2012-01-26 18:10:49 -05:00
|
|
|
|
|
|
|
if( !CallFunction(1, 1, "Initialize") )
|
2012-01-26 15:39:46 -05: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;
|
|
|
|
}
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
void cPlugin_NewLua::OnDisable()
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnDisable", false) ) // false = don't log error if not found
|
|
|
|
return;
|
|
|
|
|
|
|
|
CallFunction(0, 0, "OnDisable");
|
|
|
|
}
|
|
|
|
|
2012-01-26 15:39:46 -05:00
|
|
|
void cPlugin_NewLua::Tick(float a_Dt)
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-01-26 18:10:49 -05:00
|
|
|
if( !PushFunction("Tick") )
|
|
|
|
return;
|
|
|
|
|
|
|
|
tolua_pushnumber( m_LuaState, a_Dt );
|
|
|
|
|
|
|
|
CallFunction(1, 0, "Tick");
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::OnCollectItem( cPickup* a_Pickup, cPlayer* a_Player )
|
2012-01-26 18:10:49 -05:00
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnCollectItem") )
|
2012-01-26 18:10:49 -05:00
|
|
|
return false;
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
tolua_pushusertype(m_LuaState, a_Pickup, "cPickup");
|
2012-01-26 18:10:49 -05:00
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !CallFunction(2, 1, "OnCollectItem") )
|
2012-01-26 18:10:49 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::OnDisconnect( std::string a_Reason, cPlayer* a_Player )
|
2012-01-26 18:10:49 -05:00
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnDisconnect") )
|
2012-01-26 18:10:49 -05:00
|
|
|
return false;
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
tolua_pushstring( m_LuaState, a_Reason.c_str() );
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
2012-01-26 18:10:49 -05:00
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !CallFunction(2, 1, "OnDisconnect") )
|
2012-01-26 18:10:49 -05:00
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-26 18:10:49 -05:00
|
|
|
bool cPlugin_NewLua::OnBlockPlace( cPacket_BlockPlace* a_PacketData, cPlayer* a_Player )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-01-26 18:10:49 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::OnBlockDig( cPacket_BlockDig* a_PacketData, cPlayer* a_Player, cItem* a_PickupItem )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnBlockDig") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_PacketData, "cPacket_BlockDig");
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
tolua_pushusertype(m_LuaState, a_PickupItem, "cItem");
|
|
|
|
|
|
|
|
if( !CallFunction(3, 1, "OnBlockDig") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::OnChat( const char* a_Chat, cPlayer* a_Player )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnChat") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tolua_pushstring( m_LuaState, a_Chat );
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
|
|
|
|
if( !CallFunction(2, 1, "OnChat") )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool bRetVal = (tolua_toboolean( m_LuaState, -1, 0) > 0);
|
|
|
|
return bRetVal;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::OnLogin( cPacket_Login* a_PacketData )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
void cPlugin_NewLua::OnPlayerSpawn( cPlayer* a_Player )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnPlayerSpawn") )
|
|
|
|
return;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
|
|
|
|
CallFunction(1, 0, "OnPlayerSpawn");
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::OnPlayerJoin( cPlayer* a_Player )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
void cPlugin_NewLua::OnPlayerMove( cPlayer* a_Player )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnPlayerMove") )
|
|
|
|
return;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_Player, "cPlayer");
|
|
|
|
|
|
|
|
CallFunction(1, 0, "OnPlayerMove");
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
void cPlugin_NewLua::OnTakeDamage( cPawn* a_Pawn, TakeDamageInfo* a_TakeDamageInfo )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-02-01 14:19:51 -05:00
|
|
|
if( !PushFunction("OnTakeDamage") )
|
|
|
|
return;
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_Pawn, "cPawn");
|
|
|
|
tolua_pushusertype(m_LuaState, a_TakeDamageInfo, "TakeDamageInfo");
|
|
|
|
|
|
|
|
CallFunction(2, 0, "OnTakeDamage");
|
|
|
|
}
|
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-26 18:10:49 -05:00
|
|
|
bool cPlugin_NewLua::OnKilled( cPawn* a_Killed, cEntity* a_Killer )
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-01-26 18:10:49 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-02-01 14:19:51 -05:00
|
|
|
|
2012-06-05 11:20:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPlugin_NewLua::OnChunkGenerated(cWorld * a_World, int a_ChunkX, int a_ChunkZ)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
|
|
|
if (!PushFunction("OnChunkGenerated"))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tolua_pushusertype(m_LuaState, a_World, "cWorld");
|
|
|
|
tolua_pushnumber (m_LuaState, a_ChunkX);
|
|
|
|
tolua_pushnumber (m_LuaState, a_ChunkZ);
|
|
|
|
|
|
|
|
CallFunction(3, 0, "OnChunkGenerated");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-27 18:47:32 -05:00
|
|
|
cWebPlugin_Lua* cPlugin_NewLua::CreateWebPlugin(lua_State* a_LuaState)
|
|
|
|
{
|
2012-02-22 10:15:44 -05:00
|
|
|
cCSLock Lock( m_CriticalSection );
|
2012-01-27 18:47:32 -05:00
|
|
|
if( a_LuaState != m_LuaState )
|
|
|
|
{
|
|
|
|
LOGERROR("Not allowed to create a WebPlugin from another plugin but your own!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
cWebPlugin_Lua* WebPlugin = new cWebPlugin_Lua( this );
|
2012-01-26 18:10:49 -05:00
|
|
|
|
2012-01-27 18:47:32 -05:00
|
|
|
m_WebPlugins.push_back( WebPlugin );
|
|
|
|
|
|
|
|
return WebPlugin;
|
|
|
|
}
|
2012-01-26 18:10:49 -05:00
|
|
|
|
|
|
|
|
|
|
|
// Helper functions
|
2012-02-01 14:19:51 -05:00
|
|
|
bool cPlugin_NewLua::PushFunction( const char* a_FunctionName, bool a_bLogError /* = true */ )
|
2012-01-26 18:10:49 -05:00
|
|
|
{
|
|
|
|
lua_getglobal(m_LuaState, a_FunctionName);
|
2012-01-26 15:39:46 -05:00
|
|
|
if(!lua_isfunction(m_LuaState,-1))
|
|
|
|
{
|
2012-02-01 14:19:51 -05:00
|
|
|
if( a_bLogError )
|
|
|
|
{
|
|
|
|
LOGWARN("Error in plugin %s: Could not find function %s()", m_Directory.c_str(), a_FunctionName );
|
|
|
|
}
|
2012-01-26 15:39:46 -05:00
|
|
|
lua_pop(m_LuaState,1);
|
2012-01-26 18:10:49 -05:00
|
|
|
return false;
|
2012-01-26 15:39:46 -05:00
|
|
|
}
|
2012-01-26 18:10:49 -05:00
|
|
|
return true;
|
|
|
|
}
|
2012-01-26 15:39:46 -05:00
|
|
|
|
2012-01-26 18:10:49 -05: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 15:39:46 -05:00
|
|
|
if( report_errors( m_LuaState, s ) )
|
|
|
|
{
|
2012-01-26 18:10:49 -05:00
|
|
|
LOGWARN("Error in plugin %s calling function %s()", m_Directory.c_str(), a_FunctionName );
|
|
|
|
return false;
|
2012-01-26 15:39:46 -05:00
|
|
|
}
|
2012-01-26 18:10:49 -05:00
|
|
|
return true;
|
2012-01-26 15:39:46 -05:00
|
|
|
}
|