2013-08-19 17:07:25 +02:00
|
|
|
#include "listeners.h"
|
|
|
|
#include "vhook.h"
|
|
|
|
|
|
|
|
using namespace SourceHook;
|
|
|
|
|
2016-08-27 16:20:38 +02:00
|
|
|
ke::Vector<EntityListener> g_EntityListeners;
|
|
|
|
ke::Vector<DHooksManager *>g_pRemoveList;
|
2016-08-27 01:26:52 +02:00
|
|
|
|
|
|
|
void FrameCleanupHooks(void *data)
|
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
for (int i = g_pRemoveList.length() - 1; i >= 0; i--)
|
2016-08-27 01:26:52 +02:00
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
DHooksManager *manager = g_pRemoveList.at(i);
|
|
|
|
delete manager;
|
|
|
|
g_pRemoveList.remove(i);
|
2016-08-27 01:26:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 19:14:48 +02:00
|
|
|
void DHooks::OnCoreMapEnd()
|
2013-08-22 04:18:52 +02:00
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_pHooks.length() -1; i >= 0; i--)
|
2013-08-22 04:18:52 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
DHooksManager *manager = g_pHooks.at(i);
|
2013-08-22 04:18:52 +02:00
|
|
|
if(manager->callback->hookType == HookType_GameRules)
|
|
|
|
{
|
|
|
|
delete manager;
|
2016-08-27 16:20:38 +02:00
|
|
|
g_pHooks.remove(i);
|
2013-08-22 04:18:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-19 17:07:25 +02:00
|
|
|
void DHooksEntityListener::CleanupListeners(IPluginContext *pContext)
|
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
|
2013-08-19 17:07:25 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
if(pContext == NULL || pContext == g_EntityListeners.at(i).callback->GetParentRuntime()->GetDefaultContext())
|
2013-08-19 17:07:25 +02:00
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
g_EntityListeners.remove(i);
|
2013-08-19 17:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DHooksEntityListener::OnEntityCreated(CBaseEntity *pEntity, const char *classname)
|
|
|
|
{
|
|
|
|
int entity = gamehelpers->EntityToBCompatRef(pEntity);
|
|
|
|
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
|
2013-08-19 17:07:25 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
EntityListener listerner = g_EntityListeners.at(i);
|
2013-08-19 17:07:25 +02:00
|
|
|
if(listerner.type == ListenType_Created)
|
|
|
|
{
|
|
|
|
IPluginFunction *callback = listerner.callback;
|
|
|
|
callback->PushCell(entity);
|
|
|
|
callback->PushString(classname);
|
|
|
|
callback->Execute(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DHooksEntityListener::OnEntityDestroyed(CBaseEntity *pEntity)
|
|
|
|
{
|
|
|
|
int entity = gamehelpers->EntityToBCompatRef(pEntity);
|
|
|
|
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
|
2013-08-19 17:07:25 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
EntityListener listerner = g_EntityListeners.at(i);
|
2013-08-19 17:07:25 +02:00
|
|
|
if(listerner.type == ListenType_Deleted)
|
|
|
|
{
|
|
|
|
IPluginFunction *callback = listerner.callback;
|
|
|
|
callback->PushCell(gamehelpers->EntityToBCompatRef(pEntity));
|
|
|
|
callback->Execute(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_pHooks.length() -1; i >= 0; i--)
|
2013-08-19 17:07:25 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
DHooksManager *manager = g_pHooks.at(i);
|
2016-12-09 14:22:09 +01:00
|
|
|
if(manager->callback->hookType == HookType_Entity && manager->callback->entity == entity)
|
2013-08-19 17:07:25 +02:00
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
if(g_pRemoveList.length() == 0)
|
2016-08-27 03:01:59 +02:00
|
|
|
{
|
|
|
|
smutils->AddFrameAction(&FrameCleanupHooks, NULL);
|
|
|
|
}
|
2016-08-27 16:20:38 +02:00
|
|
|
|
|
|
|
g_pRemoveList.append(manager);
|
|
|
|
g_pHooks.remove(i);
|
2013-08-19 17:07:25 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-20 04:33:44 +02:00
|
|
|
}
|
2013-08-22 20:55:33 +02:00
|
|
|
bool DHooksEntityListener::AddPluginEntityListener(ListenType type, IPluginFunction *callback)
|
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
|
2013-08-22 20:55:33 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
EntityListener listerner = g_EntityListeners.at(i);
|
2013-08-22 20:55:33 +02:00
|
|
|
if(listerner.callback == callback && listerner.type == type)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
EntityListener listener;
|
|
|
|
listener.callback = callback;
|
|
|
|
listener.type = type;
|
2016-08-27 16:20:38 +02:00
|
|
|
g_EntityListeners.append(listener);
|
2013-08-22 20:55:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool DHooksEntityListener::RemovePluginEntityListener(ListenType type, IPluginFunction *callback)
|
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
|
2013-08-22 20:55:33 +02:00
|
|
|
{
|
2014-08-19 19:14:48 +02:00
|
|
|
EntityListener listerner = g_EntityListeners.at(i);
|
2013-08-22 20:55:33 +02:00
|
|
|
if(listerner.callback == callback && listerner.type == type)
|
|
|
|
{
|
2016-08-27 16:20:38 +02:00
|
|
|
g_EntityListeners.remove(i);
|
2013-08-22 20:55:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|