1
0
Fork 0

Changed plugin hook registrations to use cLuaState::cCallback.

This commit is contained in:
Mattes D 2016-03-02 10:12:43 +01:00
parent af8c96026d
commit 4489a89fde
5 changed files with 190 additions and 1008 deletions

View File

@ -170,6 +170,16 @@ void cLuaState::cCallback::Clear(void)
bool cLuaState::cCallback::IsValid(void)
{
cCSLock lock(m_CS);
return m_Ref.IsValid();
}
void cLuaState::cCallback::Invalidate(void)
{
cCSLock Lock(m_CS);
@ -935,6 +945,24 @@ bool cLuaState::GetStackValue(int a_StackPos, bool & a_ReturnedVal)
bool cLuaState::GetStackValue(int a_StackPos, cCallback & a_Callback)
{
return a_Callback.RefStack(*this, a_StackPos);
}
bool cLuaState::GetStackValue(int a_StackPos, cCallbackPtr & a_Callback)
{
return a_Callback->RefStack(*this, a_StackPos);
}
bool cLuaState::GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result)
{
if (lua_isnumber(m_LuaState, a_StackPos))
@ -959,15 +987,6 @@ bool cLuaState::GetStackValue(int a_StackPos, cRef & a_Ref)
bool cLuaState::GetStackValue(int a_StackPos, cCallback & a_Callback)
{
return a_Callback.RefStack(*this, a_StackPos);
}
bool cLuaState::GetStackValue(int a_StackPos, double & a_ReturnedVal)
{
if (lua_isnumber(m_LuaState, a_StackPos))

View File

@ -155,6 +155,9 @@ public:
/** Frees the contained callback, if any. */
void Clear(void);
/** Returns true if the contained callback is valid. */
bool IsValid(void);
protected:
friend class cLuaState;
@ -332,9 +335,10 @@ public:
// Enum values are checked for their allowed values and fail if the value is not assigned.
bool GetStackValue(int a_StackPos, AString & a_Value);
bool GetStackValue(int a_StackPos, bool & a_Value);
bool GetStackValue(int a_StackPos, cCallback & a_Callback);
bool GetStackValue(int a_StackPos, cCallbackPtr & a_Callback);
bool GetStackValue(int a_StackPos, cPluginManager::CommandResult & a_Result);
bool GetStackValue(int a_StackPos, cRef & a_Ref);
bool GetStackValue(int a_StackPos, cCallback & a_Ref);
bool GetStackValue(int a_StackPos, double & a_Value);
bool GetStackValue(int a_StackPos, eBlockFace & a_Value);
bool GetStackValue(int a_StackPos, eWeather & a_Value);

View File

@ -993,7 +993,13 @@ static int tolua_cPluginManager_AddHook_FnRef(cPluginManager * a_PluginManager,
}
// Retrieve and check the hook type
int HookType = static_cast<int>(tolua_tonumber(S, a_ParamIdx, -1));
int HookType;
if (!S.GetStackValue(a_ParamIdx, HookType))
{
LOGWARNING("cPluginManager.AddHook(): Cannot read the hook type.");
S.LogStackTrace();
return 0;
}
if (!a_PluginManager->IsValidHookType(HookType))
{
LOGWARNING("cPluginManager.AddHook(): Invalid HOOK_TYPE parameter: %d", HookType);
@ -1002,7 +1008,14 @@ static int tolua_cPluginManager_AddHook_FnRef(cPluginManager * a_PluginManager,
}
// Add the hook to the plugin
if (!Plugin->AddHookRef(HookType, a_ParamIdx + 1))
auto callback = std::make_shared<cLuaState::cCallback>();
if (!S.GetStackValue(a_ParamIdx + 1, callback))
{
LOGWARNING("cPluginManager.AddHook(): Cannot read the callback parameter");
S.LogStackTrace();
return 0;
}
if (!Plugin->AddHookCallback(HookType, callback))
{
LOGWARNING("cPluginManager.AddHook(): Cannot add hook %d, unknown error.", HookType);
S.LogStackTrace();
@ -1059,10 +1072,11 @@ static int tolua_cPluginManager_AddHook_DefFn(cPluginManager * a_PluginManager,
}
// Retrieve the function to call and add it to the plugin:
lua_pushstring(S, FnName);
bool res = Plugin->AddHookRef(HookType, 1);
lua_pop(S, 1); // Pop the function off the stack
if (!res)
auto callback = std::make_shared<cLuaState::cCallback>();
lua_getglobal(S, FnName);
bool res = S.GetStackValue(-1, callback);
lua_pop(S, 1);
if (!res || !callback->IsValid())
{
LOGWARNING("cPluginManager.AddHook(): Function %s not found. Hook not added.", FnName);
S.LogStackTrace();

File diff suppressed because it is too large Load Diff

View File

@ -201,11 +201,9 @@ public:
/** Returns the name of Lua function that should handle the specified hook type in the older (#121) API */
static const char * GetHookFnName(int a_HookType);
/** Adds a Lua function to be called for the specified hook.
The function has to be on the Lua stack at the specified index a_FnRefIdx
Returns true if the hook was added successfully.
*/
bool AddHookRef(int a_HookType, int a_FnRefIdx);
/** Adds a Lua callback to be called for the specified hook.
Returns true if the hook was added successfully. */
bool AddHookCallback(int a_HookType, cLuaState::cCallbackPtr a_Callback);
/** Calls a function in this plugin's LuaState with parameters copied over from a_ForeignState.
The values that the function returns are placed onto a_ForeignState.
@ -233,10 +231,10 @@ protected:
typedef std::map<AString, int> CommandMap;
/** Provides an array of Lua function references */
typedef std::vector<cLuaState::cRef *> cLuaRefs;
typedef std::vector<cLuaState::cCallbackPtr> cLuaCallbacks;
/** Maps hook types into arrays of Lua function references to call for each hook type */
typedef std::map<int, cLuaRefs> cHookMap;
typedef std::map<int, cLuaCallbacks> cHookMap;
/** The mutex protecting m_LuaState and each of the m_Resettables[] against multithreaded use. */
@ -263,6 +261,27 @@ protected:
/** Removes all WebTabs currently registered for this plugin from the WebAdmin. */
void ClearWebTabs(void);
/** Calls a hook that has the simple format - single bool return value specifying whether the chain should continue.
The advanced hook types that need more processing implement a similar loop manually instead.
Returns true if any of hook calls wants to abort the hook (returned true), false if all hook calls returned false. */
template <typename... Args>
bool CallSimpleHooks(int a_HookType, Args && ... a_Args)
{
cCSLock lock(m_CriticalSection);
auto & hooks = m_HookMap[a_HookType];
bool res = false;
for (auto & hook: hooks)
{
hook->Call(std::forward<Args>(a_Args)..., cLuaState::Return, res);
if (res)
{
// Hook wants to terminate the chain processing
return true;
}
}
return false;
}
} ; // tolua_export