forked from openmultiplayer/open.mp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimers_main.cpp
More file actions
133 lines (118 loc) · 2.62 KB
/
Copy pathtimers_main.cpp
File metadata and controls
133 lines (118 loc) · 2.62 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* The original code is copyright (c) 2022, open.mp team and contributors.
*/
#include "timer.hpp"
#include <sdk.hpp>
#include <list>
class TimersComponent final : public ITimersComponent, public CoreEventHandler
{
private:
ICore* core = nullptr;
std::list<Timer*> timers;
public:
StringView componentName() const override
{
return "Timers";
}
SemanticVersion componentVersion() const override
{
return SemanticVersion(OMP_VERSION_MAJOR, OMP_VERSION_MINOR, OMP_VERSION_PATCH, BUILD_NUMBER);
}
void onLoad(ICore* core) override
{
this->core = core;
core->getEventDispatcher().addEventHandler(this);
}
~TimersComponent()
{
if (core)
{
core->getEventDispatcher().removeEventHandler(this);
}
for (auto timer : timers)
{
delete timer;
}
timers.clear();
}
ITimer* create(TimerTimeOutHandler* handler, Milliseconds interval, bool repeating) override
{
Timer* timer = new Timer(handler, interval, interval, repeating ? 0 : 1);
timers.push_back(timer);
return timer;
}
ITimer* create(TimerTimeOutHandler* handler, Milliseconds initial, Milliseconds interval, unsigned int count) override
{
Timer* timer = new Timer(handler, initial, interval, count);
timers.push_back(timer);
return timer;
}
void onTick(Microseconds elapsed, TimePoint now) override
{
for (auto it = timers.begin(); it != timers.end();)
{
Timer* timer = *it;
bool deleteTimer = false;
if (!timer->running())
{
deleteTimer = true;
}
else
{
if (timer->paused())
{
++it;
continue;
}
const TimePoint now = Time::now();
const Milliseconds diff = duration_cast<Milliseconds>(now - timer->getTimeout());
if (diff.count() >= 0)
{
timer->handler()->timeout(*timer);
if (timer->trigger())
{
timer->setTimeout(now + timer->interval() - diff);
}
else
{
deleteTimer = true;
}
}
}
if (deleteTimer)
{
delete timer;
it = timers.erase(it);
}
else
{
++it;
}
}
}
void free() override
{
delete this;
}
void reset() override
{
// Nothing to reset here. Although some timers are reset on GMX it isn't all of them, and
// that's handled at a lower level by pawn itself.
}
const size_t count() const override
{
return std::count_if(timers.begin(), timers.end(),
[](Timer* timer)
{
return timer->running();
});
}
};
COMPONENT_ENTRY_POINT()
{
return new TimersComponent();
}