Skip to content

Commit 9b4ee72

Browse files
authored
Fix RemoveMapChangeTimers (#720)
1 parent 38d248d commit 9b4ee72

File tree

1 file changed

+64
-45
lines changed

1 file changed

+64
-45
lines changed

src/core/timer_system.cpp

Lines changed: 64 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@
3030
*/
3131

3232
#include "core/timer_system.h"
33-
#include <algorithm>
3433

3534
#include <public/eiface.h>
3635

36+
#include <algorithm>
37+
3738
#include "core/globals.h"
3839
#include "core/log.h"
39-
#include "scripting/callback_manager.h"
4040
#include "core/managers/player_manager.h"
41+
#include "scripting/callback_manager.h"
4142

4243
namespace counterstrikesharp {
4344
namespace timers {
@@ -46,8 +47,7 @@ double timer_next_think = 0.0f;
4647
} // namespace timers
4748

4849
timers::Timer::Timer(float interval, float exec_time, CallbackT callback, int flags)
49-
: m_interval(interval), m_exec_time(exec_time), m_flags(flags), m_kill_me(false),
50-
m_in_exec(false)
50+
: m_interval(interval), m_exec_time(exec_time), m_flags(flags), m_kill_me(false), m_in_exec(false)
5151
{
5252
m_callback = globals::callbackManager.CreateCallback("Timer");
5353
m_callback->AddListener(callback);
@@ -76,7 +76,8 @@ void TimerSystem::OnShutdown()
7676

7777
void TimerSystem::OnLevelEnd()
7878
{
79-
if (on_map_end_callback && on_map_end_callback->GetFunctionCount()) {
79+
if (on_map_end_callback && on_map_end_callback->GetFunctionCount())
80+
{
8081
on_map_end_callback->ScriptContext().Reset();
8182
on_map_end_callback->Execute();
8283
}
@@ -89,7 +90,8 @@ void TimerSystem::OnLevelEnd()
8990

9091
void TimerSystem::OnStartupServer()
9192
{
92-
if (m_has_map_ticked) {
93+
if (m_has_map_ticked)
94+
{
9395
CALL_GLOBAL_LISTENER(OnLevelEnd());
9496

9597
CSSHARP_CORE_TRACE("name={0}", "LevelShutdown");
@@ -101,26 +103,32 @@ void TimerSystem::OnStartupServer()
101103

102104
void TimerSystem::OnGameFrame(bool simulating)
103105
{
104-
if (simulating && m_has_map_ticked) {
106+
if (simulating && m_has_map_ticked)
107+
{
105108
timers::universal_time += globals::getGlobalVars()->curtime - m_last_ticked_time;
106-
if (!m_has_map_simulated) {
109+
if (!m_has_map_simulated)
110+
{
107111
m_has_map_simulated = true;
108112
}
109-
} else {
113+
}
114+
else
115+
{
110116
timers::universal_time += globals::engine_fixed_tick_interval;
111117
}
112118

113119
m_last_ticked_time = globals::getGlobalVars()->curtime;
114120
m_has_map_ticked = true;
115121

116122
// Handle timer tick
117-
if (timers::universal_time >= timers::timer_next_think) {
123+
if (timers::universal_time >= timers::timer_next_think)
124+
{
118125
RunFrame();
119126

120127
timers::timer_next_think = CalculateNextThink(timers::timer_next_think, 0.1f);
121128
}
122129

123-
if (m_on_tick_callback_->GetFunctionCount()) {
130+
if (m_on_tick_callback_->GetFunctionCount())
131+
{
124132
m_on_tick_callback_->ScriptContext().Reset();
125133
m_on_tick_callback_->Execute();
126134
}
@@ -130,18 +138,23 @@ void TimerSystem::OnGameFrame(bool simulating)
130138

131139
double TimerSystem::CalculateNextThink(double last_think_time, float interval)
132140
{
133-
if (timers::universal_time - last_think_time - interval <= 0.1) {
141+
if (timers::universal_time - last_think_time - interval <= 0.1)
142+
{
134143
return last_think_time + interval;
135-
} else {
144+
}
145+
else
146+
{
136147
return timers::universal_time + interval;
137148
}
138149
}
139150

140151
void TimerSystem::RunFrame()
141152
{
142-
for (int i = m_once_off_timers.size() - 1; i >= 0; i--) {
153+
for (int i = m_once_off_timers.size() - 1; i >= 0; i--)
154+
{
143155
auto timer = m_once_off_timers[i];
144-
if (timers::universal_time >= timer->m_exec_time) {
156+
if (timers::universal_time >= timer->m_exec_time)
157+
{
145158
timer->m_in_exec = true;
146159
timer->m_callback->ScriptContext().Reset();
147160
timer->m_callback->Execute();
@@ -151,14 +164,17 @@ void TimerSystem::RunFrame()
151164
}
152165
}
153166

154-
for (int i = m_repeat_timers.size() - 1; i >= 0; i--) {
167+
for (int i = m_repeat_timers.size() - 1; i >= 0; i--)
168+
{
155169
auto timer = m_repeat_timers[i];
156-
if (timers::universal_time >= timer->m_exec_time) {
170+
if (timers::universal_time >= timer->m_exec_time)
171+
{
157172
timer->m_in_exec = true;
158173
timer->m_callback->ScriptContext().Reset();
159174
timer->m_callback->Execute();
160175

161-
if (timer->m_kill_me) {
176+
if (timer->m_kill_me)
177+
{
162178
m_repeat_timers.erase(m_repeat_timers.begin() + i);
163179
delete timer;
164180
continue;
@@ -172,17 +188,17 @@ void TimerSystem::RunFrame()
172188

173189
void TimerSystem::RemoveMapChangeTimers()
174190
{
175-
for (auto timer : m_once_off_timers) {
176-
if (timer->m_flags & TIMER_FLAG_NO_MAPCHANGE) {
177-
KillTimer(timer);
191+
auto isMapChangeTimer = [](timers::Timer* timer) {
192+
bool shouldRemove = timer->m_flags & TIMER_FLAG_NO_MAPCHANGE;
193+
if (shouldRemove)
194+
{
195+
delete timer;
178196
}
179-
}
197+
return shouldRemove;
198+
};
180199

181-
for (auto timer : m_repeat_timers) {
182-
if (timer->m_flags & TIMER_FLAG_NO_MAPCHANGE) {
183-
KillTimer(timer);
184-
}
185-
}
200+
std::erase_if(m_once_off_timers, isMapChangeTimer);
201+
std::erase_if(m_repeat_timers, isMapChangeTimer);
186202
}
187203

188204
timers::Timer* TimerSystem::CreateTimer(float interval, CallbackT callback, int flags)
@@ -191,7 +207,8 @@ timers::Timer* TimerSystem::CreateTimer(float interval, CallbackT callback, int
191207

192208
auto timer = new timers::Timer(interval, exec_time, callback, flags);
193209

194-
if (flags & TIMER_FLAG_REPEAT) {
210+
if (flags & TIMER_FLAG_REPEAT)
211+
{
195212
m_repeat_timers.push_back(timer);
196213
return timer;
197214
}
@@ -202,39 +219,41 @@ timers::Timer* TimerSystem::CreateTimer(float interval, CallbackT callback, int
202219

203220
void TimerSystem::KillTimer(timers::Timer* timer)
204221
{
205-
if (!timer)
206-
return;
222+
if (!timer) return;
207223

208224
if (std::find(m_repeat_timers.begin(), m_repeat_timers.end(), timer) == m_repeat_timers.end() &&
209-
std::find(m_once_off_timers.begin(), m_once_off_timers.end(), timer) ==
210-
m_once_off_timers.end()) {
225+
std::find(m_once_off_timers.begin(), m_once_off_timers.end(), timer) == m_once_off_timers.end())
226+
{
211227
return;
212228
}
213229

214-
if (timer->m_kill_me)
215-
return;
230+
if (timer->m_kill_me) return;
216231

217232
// If were executing, make sure it doesn't run again next time.
218-
if (timer->m_in_exec) {
233+
if (timer->m_in_exec)
234+
{
219235
timer->m_kill_me = true;
220236
return;
221237
}
222238

223-
if (timer->m_flags & TIMER_FLAG_REPEAT) {
224-
auto it = std::remove_if(m_repeat_timers.begin(), m_repeat_timers.end(),
225-
[timer](timers::Timer* i) { return timer == i; });
239+
if (timer->m_flags & TIMER_FLAG_REPEAT)
240+
{
241+
auto it = std::remove_if(m_repeat_timers.begin(), m_repeat_timers.end(), [timer](timers::Timer* i) {
242+
return timer == i;
243+
});
226244

227245
bool success;
228-
if ((success = it != m_repeat_timers.end()))
229-
m_repeat_timers.erase(it, m_repeat_timers.end());
246+
if ((success = it != m_repeat_timers.end())) m_repeat_timers.erase(it, m_repeat_timers.end());
230247
delete timer;
231-
} else {
232-
auto it = std::remove_if(m_once_off_timers.begin(), m_once_off_timers.end(),
233-
[timer](timers::Timer* i) { return timer == i; });
248+
}
249+
else
250+
{
251+
auto it = std::remove_if(m_once_off_timers.begin(), m_once_off_timers.end(), [timer](timers::Timer* i) {
252+
return timer == i;
253+
});
234254

235255
bool success;
236-
if ((success = it != m_once_off_timers.end()))
237-
m_once_off_timers.erase(it, m_once_off_timers.end());
256+
if ((success = it != m_once_off_timers.end())) m_once_off_timers.erase(it, m_once_off_timers.end());
238257
delete timer;
239258
}
240259
}

0 commit comments

Comments
 (0)