forked from multitheftauto/mtasa-blue
/
CMapEventManager.cpp
394 lines (319 loc) · 13.1 KB
/
CMapEventManager.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CMapEventManager.cpp
* PURPOSE: Map event manager class
* DEVELOPERS: Christian Myhre Lundheim <>
* Jax <>
* Kevin Whiteside <>
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#include "StdInc.h"
CMapEventManager::CMapEventManager ( void )
{
m_bIteratingList = false;
m_bHasEvents = false;
}
CMapEventManager::~CMapEventManager ( void )
{
// If this happens while we're iterating the list, we're screwed
assert ( !m_bIteratingList );
// This should always be empty when m_bIteratingList is false
assert ( m_TrashCan.empty () );
// Delete all eventhandlers
DeleteAll ();
}
bool CMapEventManager::Add ( CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction, bool bPropagated, EEventPriorityType eventPriority, float fPriorityMod )
{
// Check for max name length
if ( strlen ( szName ) <= MAPEVENT_MAX_LENGTH_NAME )
{
// Make a new event
CMapEvent* pEvent = new CMapEvent ( pLuaMain, szName, iLuaFunction, bPropagated, eventPriority, fPriorityMod );
// Add now
AddInternal ( pEvent );
m_bHasEvents = true;
return true;
}
return false;
}
bool CMapEventManager::Delete ( CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction )
{
// Delete all the events with matching names
bool bRemovedSomeone = false;
EventsIter iter = m_EventsMap.begin ();
while ( iter != m_EventsMap.end () )
{
CMapEvent* pMapEvent = iter->second;
// Matching VM?
if ( pLuaMain == pMapEvent->GetVM () )
{
// If name supplied, check name and function
if ( !szName || ( ( strcmp ( pMapEvent->GetName (), szName ) == 0 ) && ( pMapEvent->GetLuaFunction () == iLuaFunction ) ) )
{
// Not alredy being destroyed?
if ( !pMapEvent->IsBeingDestroyed () )
{
// Are we in an event handler?
if ( m_bIteratingList )
{
// Put it in the trashcan
pMapEvent->SetBeingDestroyed ( true );
m_TrashCan.push_back ( pMapEvent );
// Remember that we deleted something
bRemovedSomeone = true;
}
else
{
// Delete the object
delete pMapEvent;
// Remove from list and remember that we deleted something
m_EventsMap.erase ( iter++ );
bRemovedSomeone = true;
continue;
}
}
}
}
// Increment iterator
++iter;
}
m_bHasEvents = !m_EventsMap.empty ();
// Return whether we actually destroyed someone or not
return bRemovedSomeone;
}
void CMapEventManager::DeleteAll ( void )
{
// Delete all the events
EventsIter iter = m_EventsMap.begin ();
while ( iter != m_EventsMap.end () )
{
CMapEvent* pMapEvent = iter->second;
// Delete it if it's not already being destroyed
if ( !pMapEvent->IsBeingDestroyed () )
{
delete pMapEvent;
m_EventsMap.erase ( iter++ );
}
else
++iter;
}
m_bHasEvents = !m_EventsMap.empty ();
}
bool CMapEventManager::Call ( const char* szName, const CLuaArguments& Arguments, class CElement* pSource, class CElement* pThis, CPlayer* pCaller )
{
// Check if no events
if ( !m_bHasEvents )
return false;
// Check if no events with a name match
EventsIterPair itPair = m_EventsMap.equal_range ( szName );
if ( itPair.first == itPair.second )
return false;
// Call all the events with matching names
bool bCalled = false;
bool bIsAlreadyIterating = m_bIteratingList;
m_bIteratingList = true;
// Copy the results into a array in case m_EventsMap is modified during the call
std::vector< CMapEvent* > matchingEvents;
for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter )
matchingEvents.push_back(iter->second);
for ( std::vector< CMapEvent* >::iterator iter = matchingEvents.begin() ; iter != matchingEvents.end() ; ++iter )
{
CMapEvent* pMapEvent = *iter;
// If it's not being destroyed
if ( !pMapEvent->IsBeingDestroyed () )
{
// Compare the names
dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 );
{
// Call if propagated?
if ( pSource == pThis || pMapEvent->IsPropagated () )
{
// Grab the current VM
lua_State* pState = pMapEvent->GetVM ()->GetVM ();
LUA_CHECKSTACK ( pState, 1 ); // Ensure some room
#if MTA_DEBUG
int luaStackPointer = lua_gettop ( pState );
#endif
TIMEUS startTime = GetTimeUs();
if ( !g_pGame->GetDebugHookManager()->OnPreEventFunction( szName, Arguments, pSource, pCaller, pMapEvent ) )
continue;
// Store the current values of the globals
lua_getglobal ( pState, "source" );
CLuaArgument OldSource ( pState, -1 );
lua_pop( pState, 1 );
lua_getglobal ( pState, "this" );
CLuaArgument OldThis ( pState, -1 );
lua_pop( pState, 1 );
lua_getglobal ( pState, "sourceResource" );
CLuaArgument OldResource ( pState, -1 );
lua_pop( pState, 1 );
lua_getglobal ( pState, "sourceResourceRoot" );
CLuaArgument OldResourceRoot ( pState, -1 );
lua_pop( pState, 1 );
lua_getglobal ( pState, "eventName" );
CLuaArgument OldEventName ( pState, -1 );
lua_pop( pState, 1 );
lua_getglobal ( pState, "client" );
CLuaArgument OldClient ( pState, -1 );
lua_pop( pState, 1 );
// Set the "source", "this", "sourceResource" and "sourceResourceRoot" globals on that VM
lua_pushelement ( pState, pSource );
lua_setglobal ( pState, "source" );
lua_pushelement ( pState, pThis );
lua_setglobal ( pState, "this" );
CLuaMain* pLuaMain = g_pGame->GetScriptDebugging()->GetTopLuaMain();
CResource* pSourceResource = pLuaMain ? pLuaMain->GetResource() : NULL;
if ( pSourceResource )
{
lua_pushresource ( pState, pSourceResource );
lua_setglobal ( pState, "sourceResource" );
lua_pushelement ( pState, pSourceResource->GetResourceRootElement() );
lua_setglobal ( pState, "sourceResourceRoot" );
}
else
{
lua_pushnil ( pState );
lua_setglobal ( pState, "sourceResource" );
lua_pushnil ( pState );
lua_setglobal ( pState, "sourceResourceRoot" );
}
lua_pushstring ( pState, szName );
lua_setglobal ( pState, "eventName" );
if ( pCaller )
{
lua_pushelement ( pState, pCaller );
lua_setglobal ( pState, "client" );
}
else
{
lua_pushnil ( pState );
lua_setglobal ( pState, "client" );
}
// Call it
pMapEvent->Call ( Arguments );
bCalled = true;
g_pGame->GetDebugHookManager()->OnPostEventFunction( szName, Arguments, pSource, pCaller, pMapEvent );
// Reset the globals on that VM
OldSource.Push ( pState );
lua_setglobal ( pState, "source" );
OldThis.Push ( pState );
lua_setglobal ( pState, "this" );
OldResource.Push ( pState );
lua_setglobal ( pState, "sourceResource" );
OldResourceRoot.Push ( pState );
lua_setglobal ( pState, "sourceResourceRoot" );
OldEventName.Push ( pState );
lua_setglobal ( pState, "eventName" );
OldClient.Push ( pState );
lua_setglobal ( pState, "client" );
#if MTA_DEBUG
assert ( lua_gettop ( pState ) == luaStackPointer );
#endif
CPerfStatLuaTiming::GetSingleton ()->UpdateLuaTiming ( pMapEvent->GetVM (), szName, GetTimeUs() - startTime );
}
}
}
}
// Clean out the trash if we're no longer calling events.
if ( !bIsAlreadyIterating )
{
TakeOutTheTrash ();
// We're no longer iterating the list
m_bIteratingList = false;
}
// Return whether we called atleast one func or not
return bCalled;
}
void CMapEventManager::TakeOutTheTrash ( void )
{
// Loop through our trashcan deleting every item
std::list < CMapEvent* > ::const_iterator iterTrash = m_TrashCan.begin ();
for ( ; iterTrash != m_TrashCan.end (); iterTrash++ )
{
CMapEvent* pMapEvent = *iterTrash;
// Remove from the eventhandler list
EventsIter iterMap = m_EventsMap.begin ();
while ( iterMap != m_EventsMap.end () )
{
if ( pMapEvent == iterMap->second )
m_EventsMap.erase ( iterMap++ );
else
++iterMap;
}
// Delete it
delete pMapEvent;
}
m_bHasEvents = !m_EventsMap.empty ();
// Clear the trashcan
m_TrashCan.clear ();
}
bool CMapEventManager::HandleExists ( CLuaMain* pLuaMain, const char* szName, const CLuaFunctionRef& iLuaFunction )
{
// Return true if we find an event which matches the handle
EventsIterPair itPair = m_EventsMap.equal_range ( szName );
for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter )
{
CMapEvent* pMapEvent = iter->second;
// Is it not being destroyed?
if ( !pMapEvent->IsBeingDestroyed () )
{
// Same lua main?
if ( pMapEvent->GetVM () == pLuaMain )
{
// Same name?
dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 );
{
// Same lua function?
if ( pMapEvent->GetLuaFunction () == iLuaFunction )
{
// It exists
return true;
}
}
}
}
}
// Doesn't exist
return false;
}
void CMapEventManager::AddInternal ( CMapEvent* pEvent )
{
// Find place to insert
EventsIterPair itPair = m_EventsMap.equal_range ( pEvent->GetName () );
EventsIter iter;
for ( iter = itPair.first ; iter != itPair.second ; ++iter )
{
if ( pEvent->IsHigherPriorityThan ( iter->second ) )
break;
}
// Do insert
m_EventsMap.insert ( iter, std::pair < SString, CMapEvent* > ( pEvent->GetName (), pEvent ) );
}
void CMapEventManager::GetHandles ( CLuaMain* pLuaMain, const char* szName, lua_State* luaVM )
{
unsigned int uiIndex = 0;
EventsIterPair itPair = m_EventsMap.equal_range ( szName );
for ( EventsIter iter = itPair.first ; iter != itPair.second ; ++iter )
{
CMapEvent* pMapEvent = iter->second;
// Is it not being destroyed?
if ( !pMapEvent->IsBeingDestroyed () )
{
// Same lua main?
if ( pMapEvent->GetVM () == pLuaMain )
{
// Same name?
dassert ( strcmp ( pMapEvent->GetName (), szName ) == 0 );
{
lua_pushnumber ( luaVM, ++uiIndex );
lua_getref ( luaVM, pMapEvent->GetLuaFunction ().ToInt() );
lua_settable ( luaVM, -3 );
}
}
}
}
}