-
Notifications
You must be signed in to change notification settings - Fork 13
/
listeners.cpp
118 lines (107 loc) · 2.97 KB
/
listeners.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include "listeners.h"
#include "vhook.h"
using namespace SourceHook;
ke::Vector<EntityListener> g_EntityListeners;
ke::Vector<DHooksManager *>g_pRemoveList;
void FrameCleanupHooks(void *data)
{
for (int i = g_pRemoveList.length() - 1; i >= 0; i--)
{
DHooksManager *manager = g_pRemoveList.at(i);
delete manager;
g_pRemoveList.remove(i);
}
}
void DHooks::OnCoreMapEnd()
{
for(int i = g_pHooks.length() -1; i >= 0; i--)
{
DHooksManager *manager = g_pHooks.at(i);
if(manager->callback->hookType == HookType_GameRules)
{
delete manager;
g_pHooks.remove(i);
}
}
}
void DHooksEntityListener::CleanupListeners(IPluginContext *pContext)
{
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
{
if(pContext == NULL || pContext == g_EntityListeners.at(i).callback->GetParentRuntime()->GetDefaultContext())
{
g_EntityListeners.remove(i);
}
}
}
void DHooksEntityListener::OnEntityCreated(CBaseEntity *pEntity, const char *classname)
{
int entity = gamehelpers->EntityToBCompatRef(pEntity);
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
{
EntityListener listerner = g_EntityListeners.at(i);
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);
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
{
EntityListener listerner = g_EntityListeners.at(i);
if(listerner.type == ListenType_Deleted)
{
IPluginFunction *callback = listerner.callback;
callback->PushCell(gamehelpers->EntityToBCompatRef(pEntity));
callback->Execute(NULL);
}
}
for(int i = g_pHooks.length() -1; i >= 0; i--)
{
DHooksManager *manager = g_pHooks.at(i);
if(manager->callback->hookType == HookType_Entity && manager->callback->entity == entity)
{
if(g_pRemoveList.length() == 0)
{
smutils->AddFrameAction(&FrameCleanupHooks, NULL);
}
g_pRemoveList.append(manager);
g_pHooks.remove(i);
}
}
}
bool DHooksEntityListener::AddPluginEntityListener(ListenType type, IPluginFunction *callback)
{
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
{
EntityListener listerner = g_EntityListeners.at(i);
if(listerner.callback == callback && listerner.type == type)
{
return true;
}
}
EntityListener listener;
listener.callback = callback;
listener.type = type;
g_EntityListeners.append(listener);
return true;
}
bool DHooksEntityListener::RemovePluginEntityListener(ListenType type, IPluginFunction *callback)
{
for(int i = g_EntityListeners.length() -1; i >= 0; i--)
{
EntityListener listerner = g_EntityListeners.at(i);
if(listerner.callback == callback && listerner.type == type)
{
g_EntityListeners.remove(i);
return true;
}
}
return false;
}