Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Timer.cpp
Go to the documentation of this file.
1#include "Timer.h"
2
3#include "../IBaseApi.h"
4
5#include <Timer.h>
6
7namespace API
8{
10 {
11 game_api->GetCommands()->AddOnTimerCallback("API.TimerUpdate", std::bind(&Timer::Update, this));
12 }
13
15 {
16 game_api->GetCommands()->RemoveOnTimerCallback("API.TimerUpdate");
17 }
18
20 {
21 static Timer instance;
22 return instance;
23 }
24
25 void Timer::DelayExecuteInternal(const std::function<void()>& callback, int delay_seconds, const FString& identifier, const FString& moduleName)
26 {
27 const auto now = std::chrono::system_clock::now();
28 const auto exec_time = now + std::chrono::seconds(delay_seconds);
29
30 timer_funcs_.emplace_back(std::make_shared<TimerFunc>(exec_time, callback, true, 1, 0, identifier, moduleName));
31 }
32
33 void Timer::RecurringExecuteInternal(const std::function<void()>& callback, int execution_interval,
34 int execution_counter, bool async, const FString& identifier, const FString& moduleName)
35 {
36 if (async)
37 {
38 std::thread([callback, execution_interval, execution_counter]()
39 {
40 if (execution_counter == -1)
41 {
42 for (;;)
43 {
44 callback();
45 std::this_thread::sleep_for(std::chrono::seconds(execution_interval));
46 }
47 }
48
49 for (int i = 0; i < execution_counter; ++i)
50 {
51 callback();
52 std::this_thread::sleep_for(std::chrono::seconds(execution_interval));
53 }
54 }).detach();
55 }
56 else
57 {
58 const auto now = std::chrono::system_clock::now();
59 timer_funcs_.emplace_back(
60 std::make_shared<TimerFunc>(now, callback, false, execution_counter, execution_interval, identifier, moduleName));
61 }
62 }
63
64 void Timer::UnloadTimerInternal(const FString& identifier)
65 {
66 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(),
67 [&](const auto& data)
68 {
69 return data->identifier.Equals(identifier);
70 }), timer_funcs_.end());
71 }
72
73 void Timer::UnloadTimersFromModule(const FString& moduleName)
74 {
75 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(),
76 [&](const auto& data)
77 {
78 return data->moduleName.Equals(moduleName);
79 }), timer_funcs_.end());
80 }
81
82 void Timer::Update()
83 {
84 if (timer_funcs_.empty())
85 {
86 return;
87 }
88
89 const auto now = std::chrono::system_clock::now();
90
91 bool remove = false;
92 const auto timer_funcs = timer_funcs_;
93 for (const auto& data : timer_funcs)
94 {
95 if (data == nullptr) continue;
96
97 if (now >= data->next_time)
98 {
99 if (data->exec_once)
100 {
101 remove = true;
102 }
103 else
104 {
105 if (data->execution_counter > 0)
106 {
107 --data->execution_counter;
108 }
109 else if (data->execution_counter != -1)
110 {
111 remove = true;
112 continue;
113 }
114
115 data->next_time = now + std::chrono::seconds(data->execution_interval);
116 }
117
118 data->callback();
119 }
120 }
121
122 if (remove)
123 {
124 timer_funcs_.erase(std::remove_if(timer_funcs_.begin(), timer_funcs_.end(), [&now](const auto& data)
125 {
126 return (now >= data->next_time && data->exec_once) ||
127 (!data->exec_once && data->execution_counter == 0);
128 }), timer_funcs_.end());
129 }
130 }
131} // namespace API
ARK_API void DelayExecuteInternal(const std::function< void()> &callback, int delay_seconds, const FString &identifier="", const FString &moduleName="")
Definition Timer.cpp:25
void Update()
Definition Timer.cpp:82
static ARK_API Timer & Get()
Definition Timer.cpp:19
ARK_API void UnloadTimerInternal(const FString &identifier)
Definition Timer.cpp:64
ARK_API void UnloadTimersFromModule(const FString &moduleName)
Definition Timer.cpp:73
ARK_API void RecurringExecuteInternal(const std::function< void()> &callback, int execution_interval, int execution_counter, bool async, const FString &identifier, const FString &moduleName)
Definition Timer.cpp:33
Definition IBaseApi.h:9
Definition json.hpp:4518