1
0

Fixed AddHook() wanting old-style function names.

Error reported by STR_Warrior in the forum http://forum.mc-server.org/showthread.php?tid=1227&pid=9620#pid9620
This commit is contained in:
madmaxoft 2013-08-22 21:03:20 +02:00
parent c565950e1f
commit 7eae58281a
4 changed files with 22 additions and 22 deletions

View File

@ -113,13 +113,6 @@ public:
/// All bound console commands are to be removed, do any language-dependent cleanup here /// All bound console commands are to be removed, do any language-dependent cleanup here
virtual void ClearConsoleCommands(void) {} ; virtual void ClearConsoleCommands(void) {} ;
/** Called from cPluginManager::AddHook() to check if the hook can be added.
Plugin API providers may check if the plugin is written correctly (has the hook handler function)
Returns true if the hook can be added (handler exists)
Descendants should also log the specific error message as a warning if they return false.
*/
virtual bool CanAddHook(int a_Hook) = 0;
// tolua_begin // tolua_begin
const AString & GetName(void) const { return m_Name; } const AString & GetName(void) const { return m_Name; }
void SetName(const AString & a_Name) { m_Name = a_Name; } void SetName(const AString & a_Name) { m_Name = a_Name; }

View File

@ -1240,13 +1240,16 @@ void cPluginLua::ClearConsoleCommands(void)
bool cPluginLua::CanAddHook(int a_Hook) bool cPluginLua::CanAddOldStyleHook(int a_HookType)
{ {
const char * FnName = GetHookFnName(a_Hook); const char * FnName = GetHookFnName(a_HookType);
if (FnName == NULL) if (FnName == NULL)
{ {
// Unknown hook ID // Unknown hook ID
LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.", GetName().c_str(), a_Hook); LOGWARNING("Plugin %s wants to add an unknown hook ID (%d). The plugin need not work properly.",
GetName().c_str(), a_HookType
);
m_LuaState.LogStackTrace();
return false; return false;
} }
@ -1257,12 +1260,9 @@ bool cPluginLua::CanAddHook(int a_Hook)
} }
LOGWARNING("Plugin %s wants to add a hook (%d), but it doesn't provide the callback function \"%s\" for it. The plugin need not work properly.", LOGWARNING("Plugin %s wants to add a hook (%d), but it doesn't provide the callback function \"%s\" for it. The plugin need not work properly.",
GetName().c_str(), a_Hook, FnName GetName().c_str(), a_HookType, FnName
); );
// Lua stacktrace:
m_LuaState.LogStackTrace(); m_LuaState.LogStackTrace();
return false; return false;
} }
@ -1270,9 +1270,9 @@ bool cPluginLua::CanAddHook(int a_Hook)
const char * cPluginLua::GetHookFnName(int a_Hook) const char * cPluginLua::GetHookFnName(int a_HookType)
{ {
switch (a_Hook) switch (a_HookType)
{ {
case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups"; case cPluginManager::HOOK_BLOCK_TO_PICKUPS: return "OnBlockToPickups";
case cPluginManager::HOOK_CHAT: return "OnChat"; case cPluginManager::HOOK_CHAT: return "OnChat";
@ -1331,7 +1331,17 @@ bool cPluginLua::AddHookRef(int a_HookType, int a_FnRefIdx)
{ {
ASSERT(m_CriticalSection.IsLockedByCurrentThread()); // It probably has to be, how else would we have a LuaState? ASSERT(m_CriticalSection.IsLockedByCurrentThread()); // It probably has to be, how else would we have a LuaState?
m_HookMap[a_HookType].push_back(new cLuaState::cRef(m_LuaState, a_FnRefIdx)); // Check if the function reference is valid:
cLuaState::cRef * Ref = new cLuaState::cRef(m_LuaState, a_FnRefIdx);
if ((Ref == NULL) || !Ref->IsValid())
{
LOGWARNING("Plugin %s tried to add a hook %d with bad handler function.", GetName().c_str(), a_HookType);
m_LuaState.LogStackTrace();
delete Ref;
return false;
}
m_HookMap[a_HookType].push_back(Ref);
return true; return true;
} }

View File

@ -100,7 +100,8 @@ public:
virtual void ClearConsoleCommands(void) override; virtual void ClearConsoleCommands(void) override;
virtual bool CanAddHook(int a_Hook) override; /// Returns true if the plugin contains the function for the specified hook type, using the old-style registration (#121)
bool CanAddOldStyleHook(int a_HookType);
// cWebPlugin override // cWebPlugin override
virtual const AString GetWebTitle(void) const {return GetName(); } virtual const AString GetWebTitle(void) const {return GetName(); }

View File

@ -1619,10 +1619,6 @@ void cPluginManager::AddHook(cPlugin * a_Plugin, int a_Hook)
LOGWARN("Called cPluginManager::AddHook() with a_Plugin == NULL"); LOGWARN("Called cPluginManager::AddHook() with a_Plugin == NULL");
return; return;
} }
if (!a_Plugin->CanAddHook(a_Hook))
{
return;
}
PluginList & Plugins = m_Hooks[a_Hook]; PluginList & Plugins = m_Hooks[a_Hook];
Plugins.remove(a_Plugin); Plugins.remove(a_Plugin);
Plugins.push_back(a_Plugin); Plugins.push_back(a_Plugin);