-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerAPI.cpp
More file actions
263 lines (240 loc) · 6.08 KB
/
TimerAPI.cpp
File metadata and controls
263 lines (240 loc) · 6.08 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
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
/** @file TimerAPI.cpp */
#include "LuaContext.h"
#include "Timer.h"
const std::string LuaContext::timer_module_name = "kq.timer";
void LuaContext::register_timer_module()
{
//Functions of kq.timer
static const luaL_Reg functions[] =
{
{ "start", timer_api_start },
//{ "stop_all", timer_api_stop_all },
{ NULL, NULL }
};
register_functions(timer_module_name, functions);
//Methods of the timer type
static const luaL_Reg methods[] =
{/*
{ "stop", timer_api_stop },
{ "is_with_sound", timer_api_is_with_sound },
{ "set_with_sound", timer_api_set_with_sound },
{ "is_suspended", timer_api_is_suspended },
{ "set_suspended", timer_api_set_suspended },
{ "is_suspended_with_map", timer_api_is_suspended_with_map },
{ "set_suspended_with_map", timer_api_set_suspended_with_map },*/
{ NULL, NULL }
};
static const luaL_Reg metamethods[] =
{
{ "__gc", userdata_meta_gc },
{ NULL, NULL }
};
register_type(timer_module_name, methods, metamethods);
}
/**
* @brief Pushes a timer userdata onto the stack.
* @param l a Lua context
* @param timer a timer
*/
void LuaContext::push_timer(lua_State* l, Timer& timer)
{
push_userdata(l, timer);
}
/**
* @brief Registers a timer into a context (table or a userdata).
* @param timer A timer.
* @param context_index Index of the table or userdata in the stack.
* @param callback_index Index of the function to call when the timer finishes.
*/
void LuaContext::add_timer(Timer* timer, int context_index, int callback_index)
{
const void* context;
if(lua_type(l, context_index) == LUA_TUSERDATA)
{
ExportableToLua** userdata = static_cast<ExportableToLua**>(lua_touserdata(l, context_index));
context = *userdata;
}
else
{
context = lua_topointer(l, context_index);
}
lua_pushvalue(l, callback_index);
int callback_ref = create_ref();
timers[timer].callback_ref = callback_ref;
timers[timer].context = context;
/*
Game* game = main_loop.get_game();
if(game != NULL)
{
//We are in a game: depending on the timer's cotext,
//when the map is suspended, also suspend the timer or not.
if(is_map(l, context_index) || is_entity(l, context_index) || is_item(l, context_index))
{
timer->set_is_suspended_with_map(true);
}
}
*/
timer->increment_refcount();
}
/**
* \brief Unregisters a timer associated to a context.
*
* This function can be called safely even while iterating on the timer list.
*
* \param timer A timer.
*/
void LuaContext::remove_timer(Timer* timer)
{
if (timers.find(timer) != timers.end())
{
if (!timer->is_finished())
{
cancel_callback(timers[timer].callback_ref);
}
timers[timer].callback_ref = LUA_REFNIL;
timers_to_remove.push_back(timer);
}
}
/**
* \brief Unregisters all timers associated to a context.
*
* This function can be called safely even while iterating on the timer list.
*
* \param context_index Index of a table or userdata containing timers.
*/
void LuaContext::remove_timers(int context_index)
{
std::list<Timer*> timers_to_remove;
const void* context;
if (lua_type(l, context_index) == LUA_TUSERDATA)
{
ExportableToLua** userdata = (ExportableToLua**) lua_touserdata(l, context_index);
context = *userdata;
}
else
{
context = lua_topointer(l, context_index);
}
std::map<Timer*, LuaTimerData>::iterator it;
for (it = timers.begin(); it != timers.end(); ++it)
{
Timer* timer = it->first;
if (it->second.context == context)
{
if (!timer->is_finished())
{
destroy_ref(it->second.callback_ref);
}
it->second.callback_ref = LUA_REFNIL;
timers_to_remove.push_back(timer);
}
}
}
/**
* \brief Destroys immediately all existing timers.
*/
void LuaContext::destroy_timers()
{
std::map<Timer*, LuaTimerData>::iterator it;
for (it = timers.begin(); it != timers.end(); ++it)
{
Timer* timer = it->first;
if (!timer->is_finished())
{
destroy_ref(it->second.callback_ref);
}
timer->decrement_refcount();
if (timer->get_refcount() == 0)
{
delete timer;
}
}
timers.clear();
}
/**
* @brief Updates all timers currently running for this script.
*/
void LuaContext::update_timers()
{
// Update all timers.
std::map<Timer*, LuaTimerData>::iterator it;
for (it = timers.begin(); it != timers.end(); ++it)
{
Timer* timer = it->first;
timer->update();
if (timer->is_finished())
{
do_callback(it->second.callback_ref);
it->second.callback_ref = LUA_REFNIL;
timers_to_remove.push_back(timer);
}
}
// Destroy the ones that should be removed.
std::list<Timer*>::iterator it2;
for (it2 = timers_to_remove.begin(); it2 != timers_to_remove.end(); ++it2)
{
Timer* timer = *it2;
if (timers.find(timer) != timers.end())
{
if (!timer->is_finished())
{
cancel_callback(timers[timer].callback_ref);
}
timers.erase(timer);
timer->decrement_refcount();
if (timer->get_refcount() == 0)
{
delete timer;
}
}
}
timers_to_remove.clear();
}
int LuaContext::timer_api_start(lua_State* l)
{
//Parameters: [context] delay callback
LuaContext& lua_context = get_lua_context(l);
if(lua_type(l, 1) != LUA_TNUMBER)
{
//The first parameter is the context
if(lua_type(l, 1) != LUA_TTABLE && lua_type(l, 1) != LUA_TUSERDATA)
{
luaL_typerror(l, 1, "table or userdata");
}
}
else
{
//No context specified: set a default context:
// - during a game: the current map,
// - outside a game: kq.main.
/*
Game* game = lua_context.get_main_loop().get_game();
if (game != NULL)
{
push_map(l, game->get_current_map());
}
else
{
LuaContext::push_main(l);
}
lua_insert(l, 1);*/
}
//Now the first parameter is the context
uint32_t delay = uint32_t(luaL_checkint(l, 2));
luaL_checktype(l, 3, LUA_TFUNCTION);
if(delay == 0)
{
//The delay is zero: call the function right now
lua_settop(l,3);
lua_context.call_function(0,0, "callback");
lua_pushnil(l);
}
else
{
//Create the timer.
Timer* timer = new Timer(delay);
lua_context.add_timer(timer, 1, 3);
push_timer(l, *timer);
}
return 1;
}