forked from ElunaLuaEngine/Eluna
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElunaEventMgr.cpp
More file actions
162 lines (137 loc) · 4.25 KB
/
ElunaEventMgr.cpp
File metadata and controls
162 lines (137 loc) · 4.25 KB
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
/*
* Copyright (C) 2010 - 2015 Eluna Lua Engine <http://emudevs.com/>
* This program is free software licensed under GPL version 3
* Please see the included DOCS/LICENSE.md for more information
*/
#include "ElunaEventMgr.h"
#include "LuaEngine.h"
#include "Object.h"
extern "C"
{
#include "lua.h"
#include "lauxlib.h"
};
LuaEvent::LuaEvent(ElunaEventProcessor* _events, int _funcRef, uint32 _delay, uint32 _calls) :
to_Abort(false), events(_events), funcRef(_funcRef), delay(_delay), calls(_calls)
{
}
LuaEvent::~LuaEvent()
{
luaL_unref((*events->E)->L, LUA_REGISTRYINDEX, funcRef); // Free lua function ref
}
void LuaEvent::Execute()
{
LOCK_ELUNA;
// In multithread get map from object and the map's lua state
lua_rawgeti((*events->E)->L, LUA_REGISTRYINDEX, funcRef);
Eluna::Push((*events->E)->L, funcRef);
Eluna::Push((*events->E)->L, delay);
Eluna::Push((*events->E)->L, calls);
if (calls) // Must be before calling
--calls;
Eluna::Push((*events->E)->L, events->obj);
(*events->E)->ExecuteCall(4, 0);
ASSERT(!(*events->E)->event_level);
(*events->E)->InvalidateObjects();
}
ElunaEventProcessor::ElunaEventProcessor(Eluna** _E, WorldObject* _obj) : m_time(0), obj(_obj), E(_E)
{
if (obj)
{
EventMgr::WriteGuard guard((*E)->eventMgr->GetLock());
(*E)->eventMgr->processors.insert(this);
}
}
ElunaEventProcessor::~ElunaEventProcessor()
{
RemoveEvents_internal();
if (obj && Eluna::initialized)
{
EventMgr::WriteGuard guard((*E)->eventMgr->GetLock());
(*E)->eventMgr->processors.erase(this);
}
}
void ElunaEventProcessor::Update(uint32 diff)
{
m_time += diff;
for (EventList::iterator it = eventList.begin(); it != eventList.end() && it->first <= m_time; it = eventList.begin())
{
LuaEvent* event = it->second;
eventList.erase(it);
eventMap.erase(event->funcRef);
if (event->to_Abort)
{
delete event;
continue;
}
bool remove = event->calls == 1;
if (!remove)
AddEvent(event); // Reschedule before calling incase RemoveEvents used
event->Execute();
if (remove)
delete event;
}
}
void ElunaEventProcessor::RemoveEvents()
{
for (EventList::iterator it = eventList.begin(); it != eventList.end(); ++it)
it->second->to_Abort = true;
}
void ElunaEventProcessor::RemoveEvents_internal()
{
//if (!final)
//{
// for (EventList::iterator it = eventList.begin(); it != eventList.end(); ++it)
// it->second->to_Abort = true;
// return;
//}
for (EventList::iterator it = eventList.begin(); it != eventList.end(); ++it)
delete it->second;
eventList.clear();
eventMap.clear();
}
void ElunaEventProcessor::RemoveEvent(int eventId)
{
if (eventMap.find(eventId) != eventMap.end())
eventMap[eventId]->to_Abort = true;
}
void ElunaEventProcessor::AddEvent(LuaEvent* event)
{
eventList.insert(std::pair<uint64, LuaEvent*>(m_time + event->delay, event));
eventMap[event->funcRef] = event;
}
void ElunaEventProcessor::AddEvent(int funcRef, uint32 delay, uint32 repeats)
{
AddEvent(new LuaEvent(this, funcRef, delay, repeats));
}
EventMgr::EventMgr(Eluna** _E) : globalProcessor(new ElunaEventProcessor(_E, NULL)), E(_E)
{
}
EventMgr::~EventMgr()
{
{
ReadGuard guard(GetLock());
if (!processors.empty())
for (ProcessorSet::const_iterator it = processors.begin(); it != processors.end(); ++it) // loop processors
(*it)->RemoveEvents_internal();
globalProcessor->RemoveEvents_internal();
}
delete globalProcessor;
globalProcessor = NULL;
}
void EventMgr::RemoveEvents()
{
ReadGuard guard(GetLock());
if (!processors.empty())
for (ProcessorSet::const_iterator it = processors.begin(); it != processors.end(); ++it) // loop processors
(*it)->RemoveEvents();
globalProcessor->RemoveEvents();
}
void EventMgr::RemoveEvent(int eventId)
{
ReadGuard guard(GetLock());
if (!processors.empty())
for (ProcessorSet::const_iterator it = processors.begin(); it != processors.end(); ++it) // loop processors
(*it)->RemoveEvent(eventId);
globalProcessor->RemoveEvent(eventId);
}