This repository was archived by the owner on Aug 29, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathLuaTimer.h
More file actions
112 lines (96 loc) · 2.23 KB
/
Copy pathLuaTimer.h
File metadata and controls
112 lines (96 loc) · 2.23 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
#ifndef __LUATIMER_H__
#define __LUATIMER_H__
struct Timer
{
int TimerIndex;
Timer()
{
TimerIndex = -1;
}
int Create(lua_State* L)
{
const int args = lua_gettop(L);
if (args >= 3)
{
Timer *t = luabridge::Userdata::get<Timer>(L, 1, false);
if(t->TimerIndex == -1)
{
timer newTimer;
if (!lua_isfunction(L, 2))
{
API::Log::Warning("Timer:Create arg 1 must be a function.");
lua_pop(L, args);
return 0;
}
lua_pushvalue(L, 2);
newTimer.functionKey = luaL_ref(L, LUA_REGISTRYINDEX); //function
newTimer.tick = lua_tointeger(L, 3); //delay in ms
newTimer.lastTick = std::chrono::system_clock::now();
newTimer.used = true;
if (args > 3)
{
for (unsigned int i = 4; i <= args; i++)
{
FuncArg arg = FuncArg();
switch (lua_type(L, i))
{
case LUA_TBOOLEAN:
arg.type = ArgType::TYPE_BOOLEAN;
arg.boolean = lua_toboolean(L, i);
break;
case LUA_TSTRING:
arg.type = ArgType::TYPE_STRING;
arg.string = lua_tostring(L, i);
break;
case LUA_TNUMBER:
arg.type = ArgType::TYPE_NUMBER;
arg.number = lua_tonumber(L, i);
break;
default:
int r = luaL_ref(L, LUA_REGISTRYINDEX);
arg.type = ArgType::TYPE_OTHER;
arg.other = r;
break;
}
newTimer.args.push_back(arg);
}
}
t->TimerIndex = timers.size();
timers.push_back(newTimer);
}
else
{
API::Log::Warning("Timer:Create this timer is already created.");
}
t = nullptr;
}
else
{
API::Log::Warning("Timer:Create requires args (function, int delay/ms, optional[ARGS]...).");
}
lua_pop(L, args);
return 0;
}
int Destroy(lua_State* L)
{
const int args = lua_gettop(L);
if (args == 1)
{
Timer *t = luabridge::Userdata::get<Timer>(L, 1, false);
timers[t->TimerIndex].used = false;
timers[t->TimerIndex].functionKey = 0;
timers[t->TimerIndex].args = std::vector<FuncArg>();
timers[t->TimerIndex].tick = 0;
timers[t->TimerIndex].lastTick = std::chrono::system_clock::now();
t->TimerIndex = -1;
t = nullptr;
}
else
{
API::Log::Warning("Timer:Destroy requires no args.");
}
lua_pop(L, args);
return 0;
}
};
#endif