1
0

cPlugin_NewLua is now completely rewritten to use templated LuaState calls.

This commit is contained in:
madmaxoft 2013-08-08 16:02:07 +02:00
parent 9e34a878ef
commit cc920ea929
3 changed files with 179 additions and 316 deletions

View File

@ -200,7 +200,25 @@ bool cLuaState::LoadFile(const AString & a_FileName)
bool cLuaState::PushFunction(const char * a_FunctionName, bool a_ShouldLogFailure /* = true */)
bool cLuaState::HasFunction(const char * a_FunctionName)
{
if (!IsValid())
{
// This happens if cPlugin::Initialize() fails with an error
return false;
}
lua_getglobal(m_LuaState, a_FunctionName);
bool res = (!lua_isnil(m_LuaState, -1) && lua_isfunction(m_LuaState, -1));
lua_pop(m_LuaState, 1);
return res;
}
bool cLuaState::PushFunction(const char * a_FunctionName)
{
ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack
@ -212,11 +230,8 @@ bool cLuaState::PushFunction(const char * a_FunctionName, bool a_ShouldLogFailur
lua_getglobal(m_LuaState, a_FunctionName);
if (!lua_isfunction(m_LuaState, -1))
{
if (a_ShouldLogFailure)
{
LOGWARNING("Error in %s: Could not find function %s()", m_SubsystemName.c_str(), a_FunctionName);
}
lua_pop(m_LuaState, 1);
return false;
}
@ -229,7 +244,7 @@ bool cLuaState::PushFunction(const char * a_FunctionName, bool a_ShouldLogFailur
bool cLuaState::PushFunctionFromRegistry(int a_FnRef)
bool cLuaState::PushFunction(int a_FnRef)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs == -1); // If not, there's already something pushed onto the stack
@ -545,6 +560,45 @@ void cLuaState::Push(TakeDamageInfo * a_TDI)
void cLuaState::Push(cWindow * a_Window)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first
tolua_pushusertype(m_LuaState, a_Window, "cWindow");
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::Push(cPlugin_NewLua * a_Plugin)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first
tolua_pushusertype(m_LuaState, a_Plugin, "cPlugin_NewLua");
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::Push(const HTTPRequest * a_Request)
{
ASSERT(IsValid());
ASSERT(m_NumCurrentFunctionArgs >= 0); // A function must be pushed to stack first
tolua_pushusertype(m_LuaState, (void *)a_Request, "HTTPRequest");
m_NumCurrentFunctionArgs += 1;
}
void cLuaState::GetReturn(int a_StackPos, bool & a_ReturnedVal)
{
a_ReturnedVal = (tolua_toboolean(m_LuaState, a_StackPos, a_ReturnedVal ? 1 : 0) > 0);

View File

@ -45,6 +45,9 @@ class cChunkDesc;
class cCraftingGrid;
class cCraftingRecipe;
struct TakeDamageInfo;
class cWindow;
class cPlugin_NewLua;
struct HTTPRequest;
@ -120,16 +123,18 @@ public:
*/
bool LoadFile(const AString & a_FileName);
/// Returns true if a_FunctionName is a valid Lua function that can be called
bool HasFunction(const char * a_FunctionName);
/** Pushes the function of the specified name onto the stack.
Returns true if successful.
If a_ShouldLogFail is true, logs a warning on failure (incl. m_SubsystemName)
Returns true if successful. Logs a warning on failure (incl. m_SubsystemName)
*/
bool PushFunction(const char * a_FunctionName, bool a_ShouldLogFailure = true);
bool PushFunction(const char * a_FunctionName);
/** Pushes a function that has been saved into the global registry, identified by a_FnRef.
Returns true if successful. Logs a warning on failure
*/
bool PushFunctionFromRegistry(int a_FnRef);
bool PushFunction(int a_FnRef);
/** Pushes a function that is stored in a table ref.
Returns true if successful, false on failure. Doesn't log failure.
@ -159,12 +164,27 @@ public:
void Push(const cCraftingGrid * a_Grid);
void Push(const cCraftingRecipe * a_Recipe);
void Push(TakeDamageInfo * a_TDI);
void Push(cWindow * a_Window);
void Push(cPlugin_NewLua * a_Plugin);
void Push(const HTTPRequest * a_Request);
/// Call any 0-param 0-return Lua function in a single line:
template <typename FnT>
bool Call(FnT a_FnName)
{
if (!PushFunction(a_FnName))
{
return false;
}
return CallFunction(0);
}
/// Call any 1-param 0-return Lua function in a single line:
template<
typename FnT,
typename ArgT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1)
bool Call(FnT a_FnName, ArgT1 a_Arg1)
{
if (!PushFunction(a_FnName))
{
@ -174,11 +194,26 @@ public:
return CallFunction(0);
}
/// Call any 2-param 0-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename ArgT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
Push(a_Arg2);
return CallFunction(0);
}
/// Call any 1-param 1-return Lua function in a single line:
template<
typename ArgT1, typename RetT1
typename FnT, typename ArgT1, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -196,9 +231,9 @@ public:
/// Call any 2-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename RetT1
typename FnT, typename ArgT1, typename ArgT2, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -217,9 +252,9 @@ public:
/// Call any 3-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename RetT1
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -239,9 +274,9 @@ public:
/// Call any 4-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename RetT1
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -262,9 +297,9 @@ public:
/// Call any 5-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename RetT1
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -286,10 +321,10 @@ public:
/// Call any 6-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -312,10 +347,10 @@ public:
/// Call any 7-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -339,10 +374,10 @@ public:
/// Call any 8-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -367,10 +402,10 @@ public:
/// Call any 9-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename ArgT9, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -396,10 +431,10 @@ public:
/// Call any 10-param 1-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5, typename ArgT6,
typename ArgT7, typename ArgT8, typename ArgT9, typename ArgT10, typename RetT1
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, ArgT10 a_Arg10, const cRet & a_Mark, RetT1 & a_Ret1)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, ArgT10 a_Arg10, const cRet & a_Mark, RetT1 & a_Ret1)
{
if (!PushFunction(a_FnName))
{
@ -424,11 +459,32 @@ public:
return true;
}
/// Call any 1-param 2-return Lua function in a single line:
template<
typename FnT, typename ArgT1, typename RetT1, typename RetT2
>
bool Call(FnT a_FnName, ArgT1 a_Arg1, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
return false;
}
Push(a_Arg1);
if (!CallFunction(2))
{
return false;
}
GetReturn(-2, a_Ret1);
GetReturn(-1, a_Ret2);
lua_pop(m_LuaState, 2);
return true;
}
/// Call any 2-param 2-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename RetT1, typename RetT2
typename FnT, typename ArgT1, typename ArgT2, typename RetT1, typename RetT2
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2)
{
if (!PushFunction(a_FnName))
{
@ -448,11 +504,11 @@ public:
/// Call any 9-param 5-return Lua function in a single line:
template<
typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename FnT, typename ArgT1, typename ArgT2, typename ArgT3, typename ArgT4, typename ArgT5,
typename ArgT6, typename ArgT7, typename ArgT8, typename ArgT9,
typename RetT1, typename RetT2, typename RetT3, typename RetT4, typename RetT5
>
bool Call(const char * a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3, RetT4 & a_Ret4, RetT5 & a_Ret5)
bool Call(FnT a_FnName, ArgT1 a_Arg1, ArgT2 a_Arg2, ArgT3 a_Arg3, ArgT4 a_Arg4, ArgT5 a_Arg5, ArgT6 a_Arg6, ArgT7 a_Arg7, ArgT8 a_Arg8, ArgT9 a_Arg9, const cRet & a_Mark, RetT1 & a_Ret1, RetT2 & a_Ret2, RetT3 & a_Ret3, RetT4 & a_Ret4, RetT5 & a_Ret5)
{
if (!PushFunction(a_FnName))
{
@ -480,6 +536,7 @@ public:
return true;
}
/// Retrieve value returned at a_StackPos, if it is a valid bool. If not, a_ReturnedVal is unchanged
void GetReturn(int a_StackPos, bool & a_ReturnedVal);

View File

@ -72,29 +72,22 @@ bool cPlugin_NewLua::Initialize(void)
} // for itr - Files[]
// Call intialize function
if (!m_LuaState.PushFunction("Initialize"))
bool res = false;
if (!m_LuaState.Call("Initialize", this, cLuaState::Return, res))
{
LOGWARNING("Error in plugin %s: Cannot call the Initialize() function. Plugin is temporarily disabled.", GetName().c_str());
m_LuaState.Close();
return false;
}
m_LuaState.PushUserType(this, "cPlugin_NewLua");
if (!m_LuaState.CallFunction(1))
if (!res)
{
LOGINFO("Plugin %s: Initialize() call failed, plugin is temporarily disabled.", GetName().c_str());
m_LuaState.Close();
return false;
}
if (!lua_isboolean(m_LuaState, -1))
{
LOGWARNING("Error in plugin %s: Initialize() must return a boolean value!", GetName().c_str());
m_LuaState.Close();
return false;
}
bool bSuccess = (tolua_toboolean(m_LuaState, -1, 0) > 0);
return bSuccess;
return true;
}
@ -104,12 +97,11 @@ bool cPlugin_NewLua::Initialize(void)
void cPlugin_NewLua::OnDisable(void)
{
cCSLock Lock(m_CriticalSection);
if (!m_LuaState.PushFunction("OnDisable", false)) // false = don't log error if not found
if (!m_LuaState.HasFunction("OnDisable"))
{
return;
}
m_LuaState.CallFunction(0);
m_LuaState.Call("OnDisable");
}
@ -144,31 +136,6 @@ bool cPlugin_NewLua::OnChat(cPlayer * a_Player, AString & a_Message)
bool res = false;
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHAT), a_Player, a_Message, cLuaState::Return, res, a_Message);
return res;
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_CHAT);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_Player);
m_LuaState.PushString(a_Message.c_str());
if (!m_LuaState.CallFunction(2))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -2, 0) > 0);
if (lua_isstring(m_LuaState, -1))
{
a_Message = tolua_tostring(m_LuaState, -1, "");
}
lua_pop(m_LuaState, 2);
return bRetVal;
*/
}
@ -179,33 +146,8 @@ bool cPlugin_NewLua::OnChunkAvailable(cWorld * a_World, int a_ChunkX, int a_Chun
{
cCSLock Lock(m_CriticalSection);
bool res = false;
if (!m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_AVAILABLE), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res))
{
return false;
}
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_AVAILABLE), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res);
return res;
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_AVAILABLE);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_World);
m_LuaState.PushNumber(a_ChunkX);
m_LuaState.PushNumber(a_ChunkZ);
if (!m_LuaState.CallFunction(1))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
*/
}
@ -218,29 +160,6 @@ bool cPlugin_NewLua::OnChunkGenerated(cWorld * a_World, int a_ChunkX, int a_Chun
bool res = false;
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATED), a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc, cLuaState::Return, res);
return res;
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATED);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_World);
m_LuaState.PushNumber(a_ChunkX);
m_LuaState.PushNumber(a_ChunkZ);
m_LuaState.PushUserType(a_ChunkDesc, "cChunkDesc");
if (!m_LuaState.CallFunction(1))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
*/
}
@ -253,29 +172,6 @@ bool cPlugin_NewLua::OnChunkGenerating(cWorld * a_World, int a_ChunkX, int a_Chu
bool res = false;
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATING), a_World, a_ChunkX, a_ChunkZ, a_ChunkDesc, cLuaState::Return, res);
return res;
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_GENERATING);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_World);
m_LuaState.PushNumber(a_ChunkX);
m_LuaState.PushNumber(a_ChunkZ);
m_LuaState.PushUserType(a_ChunkDesc, "cChunkDesc");
if (!m_LuaState.CallFunction(1))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
*/
}
@ -286,33 +182,8 @@ bool cPlugin_NewLua::OnChunkUnloaded(cWorld * a_World, int a_ChunkX, int a_Chunk
{
cCSLock Lock(m_CriticalSection);
bool res = false;
if (!m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADED), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res))
{
return false;
}
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADED), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res);
return res;
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADED);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_World);
m_LuaState.PushNumber(a_ChunkX);
m_LuaState.PushNumber(a_ChunkZ);
if (!m_LuaState.CallFunction(1))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
*/
}
@ -323,34 +194,9 @@ bool cPlugin_NewLua::OnChunkUnloading(cWorld * a_World, int a_ChunkX, int a_Chun
{
cCSLock Lock(m_CriticalSection);
bool res = false;
if (!m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res))
{
return false;
}
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING), a_World, a_ChunkX, a_ChunkZ, cLuaState::Return, res);
return res;
}
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_CHUNK_UNLOADING);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_World);
m_LuaState.PushNumber(a_ChunkX);
m_LuaState.PushNumber(a_ChunkZ);
if (!m_LuaState.CallFunction(1))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
}
*/
@ -362,27 +208,6 @@ bool cPlugin_NewLua::OnCollectingPickup(cPlayer * a_Player, cPickup * a_Pickup)
bool res = false;
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_COLLECTING_PICKUP), a_Player, a_Pickup, cLuaState::Return, res);
return res;
/*
const char * FnName = GetHookFnName(cPluginManager::HOOK_COLLECTING_PICKUP);
ASSERT(FnName != NULL);
if (!m_LuaState.PushFunction(FnName))
{
return false;
}
m_LuaState.PushObject(a_Player);
m_LuaState.PushObject(a_Pickup);
if (!m_LuaState.CallFunction(1))
{
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
*/
}
@ -393,10 +218,7 @@ bool cPlugin_NewLua::OnCraftingNoRecipe(const cPlayer * a_Player, const cCraftin
{
cCSLock Lock(m_CriticalSection);
bool res = false;
if (!m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CRAFTING_NO_RECIPE), (cPlayer *)a_Player, a_Grid, a_Recipe, cLuaState::Return, res))
{
return false;
}
m_LuaState.Call(GetHookFnName(cPluginManager::HOOK_CRAFTING_NO_RECIPE), (cPlayer *)a_Player, a_Grid, a_Recipe, cLuaState::Return, res);
return res;
}
@ -823,29 +645,9 @@ bool cPlugin_NewLua::HandleCommand(const AStringVector & a_Split, cPlayer * a_Pl
}
cCSLock Lock(m_CriticalSection);
// Push the function to be called:
if (!m_LuaState.PushFunctionFromRegistry(cmd->second))
{
LOGWARNING("Command handler function for \"%s\" is invalid", cmd->first.c_str());
return false;
}
m_LuaState.Push(a_Split);
m_LuaState.Push(a_Player);
// Call function:
if (!m_LuaState.CallFunction(1))
{
LOGWARNING("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
return false;
}
// Handle return value:
bool RetVal = (tolua_toboolean(m_LuaState, -1, 0) > 0);
lua_pop(m_LuaState, 1); // Pop return value
return RetVal;
bool res = false;
m_LuaState.Call(cmd->second, a_Split, a_Player, cLuaState::Return, res);
return res;
}
@ -865,29 +667,14 @@ bool cPlugin_NewLua::HandleConsoleCommand(const AStringVector & a_Split, cComman
}
cCSLock Lock(m_CriticalSection);
// Push the function to be called:
m_LuaState.PushFunctionFromRegistry(cmd->second);
m_LuaState.Push(a_Split);
// Call function:
if (!m_LuaState.CallFunction(2))
bool res = false;
AString str;
m_LuaState.Call(cmd->second, a_Split, cLuaState::Return, res, str);
if (res && !str.empty())
{
LOGWARNING("Lua error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
return false;
}
// Handle return values:
if (lua_isstring(m_LuaState, -1))
{
AString str = tolua_tocppstring(m_LuaState, -1, "");
a_Output.Out(str);
}
bool RetVal = (tolua_toboolean(m_LuaState, -2, 0) > 0);
lua_pop(m_LuaState, 2); // Pop return values
return RetVal;
return res;
}
@ -943,11 +730,7 @@ bool cPlugin_NewLua::CanAddHook(cPluginManager::PluginHook a_Hook)
}
// Check if the function is available
lua_getglobal(m_LuaState, FnName);
bool res = lua_isfunction(m_LuaState, -1);
lua_pop(m_LuaState, 1);
if (res)
if (m_LuaState.HasFunction(FnName))
{
return true;
}
@ -1040,7 +823,9 @@ AString cPlugin_NewLua::HandleWebRequest(const HTTPRequest * a_Request )
std::pair< std::string, std::string > TabName = GetTabNameForRequest(a_Request);
std::string SafeTabName = TabName.second;
if (SafeTabName.empty())
{
return "";
}
sWebPluginTab * Tab = 0;
for (TabList::iterator itr = GetTabs().begin(); itr != GetTabs().end(); ++itr)
@ -1052,28 +837,15 @@ AString cPlugin_NewLua::HandleWebRequest(const HTTPRequest * a_Request )
}
}
if( Tab )
if (Tab != NULL)
{
m_LuaState.PushFunctionFromRegistry(Tab->UserData);
// Push HTTPRequest
m_LuaState.PushUserType((void*)a_Request, "const HTTPRequest");
if (!m_LuaState.CallFunction(1))
AString Contents = Printf("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str());
if (!m_LuaState.Call(Tab->UserData, a_Request, cLuaState::Return, Contents))
{
return "Lua encountered error while processing the page request";
}
if (!lua_isstring(m_LuaState, -1))
{
LOGWARNING("WebPlugin tab '%s' did not return a string!", Tab->Title.c_str());
lua_pop(m_LuaState, 1); // Pop return value
return Printf("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str());
}
RetVal += tolua_tostring(m_LuaState, -1, 0);
lua_pop(m_LuaState, 1); // Pop return value
// LOGINFO("ok. Stack size: %i", lua_gettop(m_LuaState) );
RetVal += Contents;
}
return RetVal;
@ -1140,21 +912,9 @@ bool cPlugin_NewLua::CallbackWindowClosing(int a_FnRef, cWindow & a_Window, cPla
ASSERT(a_FnRef != LUA_REFNIL);
cCSLock Lock(m_CriticalSection);
m_LuaState.PushFunctionFromRegistry(a_FnRef);
m_LuaState.PushUserType(&a_Window, "cWindow");
m_LuaState.Push(&a_Player);
m_LuaState.Push(a_CanRefuse);
// Call function:
if (!m_LuaState.CallFunction(1))
{
LOGWARNING("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
return false;
}
bool bRetVal = (tolua_toboolean(m_LuaState, -1, false) > 0);
lua_pop(m_LuaState, 1);
return bRetVal;
bool res;
m_LuaState.Call(a_FnRef, &a_Window, &a_Player, a_CanRefuse, cLuaState::Return, res);
return res;
}
@ -1166,15 +926,7 @@ void cPlugin_NewLua::CallbackWindowSlotChanged(int a_FnRef, cWindow & a_Window,
ASSERT(a_FnRef != LUA_REFNIL);
cCSLock Lock(m_CriticalSection);
m_LuaState.PushFunctionFromRegistry(a_FnRef);
m_LuaState.PushUserType(&a_Window, "cWindow");
m_LuaState.Push(a_SlotNum);
// Call function:
if (!m_LuaState.CallFunction(0))
{
LOGWARNING("LUA error in %s. Stack size: %i", __FUNCTION__, lua_gettop(m_LuaState));
}
m_LuaState.Call(a_FnRef, &a_Window, a_SlotNum);
}