Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <chrono>
5
6#include "API/ARK/Ark.h"
7
8namespace API
9{
10 class Timer
11 {
12 public:
13 ARK_API static Timer& Get();
14
15 Timer(const Timer&) = delete;
16 Timer(Timer&&) = delete;
17 Timer& operator=(const Timer&) = delete;
18 Timer& operator=(Timer&&) = delete;
19
20 /**
21 * \brief Executes function after X seconds
22 * \tparam Func Callback function type
23 * \tparam Args Callback arguments types
24 * \param callback Callback function
25 * \param delay Delay in seconds
26 * \param args Callback arguments
27 */
28 template <typename Func, typename... Args>
29 void DelayExecute(const Func& callback, int delay, Args&&... args)
30 {
32 }
33
34 /**
35 * \brief Executes function after X seconds
36 * \param identifier Unique identifier for the delay execute
37 * \tparam Func Callback function type
38 * \tparam Args Callback arguments types
39 * \param callback Callback function
40 * \param delay Delay in seconds
41 * \param args Callback arguments
42 */
43 template <typename Func, typename... Args>
44 void DelayExecute(const FString& identifier, const Func& callback, int delay, Args&&... args)
45 {
47 }
48
49 /**
50 * \brief Executes function every X seconds
51 * \tparam Func Callback function type
52 * \tparam Args Callback arguments types
53 * \param callback Callback function
54 * \param execution_interval Delay between executions in seconds
55 * \param execution_counter Amount of times to execute function, -1 for unlimited
56 * \param async If true, function will be executed in the new thread
57 * \param args Callback arguments
58 */
59 template <typename Func, typename... Args>
60 void RecurringExecute(const Func& callback, int execution_interval,
61 int execution_counter, bool async, Args&&... args)
62 {
66 }
67
68 /**
69 * \brief Executes function every X seconds
70 * \param identifier Unique identifier for the delay execute
71 * \tparam Func Callback function type
72 * \tparam Args Callback arguments types
73 * \param callback Callback function
74 * \param execution_interval Delay between executions in seconds
75 * \param execution_counter Amount of times to execute function, -1 for unlimited
76 * \param async If true, function will be executed in the new thread
77 * \param args Callback arguments
78 */
79 template <typename Func, typename... Args>
80 void RecurringExecute(const FString& identifier, const Func& callback, int execution_interval,
81 int execution_counter, bool async, Args&&... args)
82 {
85 }
86
87 /**
88 * \brief Unloads timer by identifier
89 * \param identifier Unique identifier for the timer, either a delay execute or a recurring execute
90 */
91 void UnloadTimer(const FString& identifier)
92 {
93 UnloadTimerInternal(identifier);
94 }
95
96 /**
97 * \brief Unloads all timers from the caller plugin
98 */
100 {
102 }
103
104 private:
105 friend class PluginManager;
106
108 {
109 TimerFunc(const std::chrono::time_point<std::chrono::system_clock>& next_time,
110 std::function<void()> callback,
111 bool exec_once, int execution_counter, int execution_interval,
112 FString identifier, FString moduleName)
114 callback(move(callback)),
115 exec_once(exec_once),
116 execution_counter(execution_counter),
117 execution_interval(execution_interval),
120 {
121 }
122
126 std::function<void()> callback;
130 };
131
132 Timer();
133 ~Timer();
134
135 ARK_API void DelayExecuteInternal(const std::function<void()>& callback, int delay_seconds, const FString& identifier = "", const FString& moduleName = "");
136 ARK_API void RecurringExecuteInternal(const std::function<void()>& callback, int execution_interval,
137 int execution_counter, bool async, const FString& identifier, const FString& moduleName);
138 ARK_API void UnloadTimerInternal(const FString& identifier);
139 ARK_API void UnloadTimersFromModule(const FString& moduleName);
140
141 void Update();
142
144 };
145} // namespace API
#define ARK_API
Definition Ark.h:16
FORCEINLINE FString GetDllName()
Definition Helpers.h:5
std::vector< std::shared_ptr< Plugin > > loaded_plugins_
std::vector< std::shared_ptr< Plugin > >::const_iterator FindPlugin(const std::string &plugin_name)
Find plugin by it's name.
void LoadAllPlugins()
Find and load all plugins.
static PluginManager & Get()
static nlohmann::json ReadPluginInfo(const std::string &plugin_name)
bool IsPluginLoaded(const std::string &plugin_name)
Returns true if plugin was loaded, false otherwise.
std::shared_ptr< Plugin > & LoadPlugin(const std::string &plugin_name) noexcept(false)
Load plugin by it's name.
static void DetectPluginChangesTimerCallback()
Checks for auto plugin reloads.
void UnloadPlugin(const std::string &plugin_name) noexcept(false)
Unload plugin by it's name. Plugin must free all used resources.
static nlohmann::json ReadSettingsConfig()
void RecurringExecute(const Func &callback, int execution_interval, int execution_counter, bool async, Args &&... args)
Executes function every X seconds.
Definition Timer.h:60
Timer(const Timer &)=delete
ARK_API void DelayExecuteInternal(const std::function< void()> &callback, int delay_seconds, const FString &identifier="", const FString &moduleName="")
Definition Timer.cpp:25
Timer & operator=(Timer &&)=delete
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
void UnloadAllTimers()
Unloads all timers from the caller plugin.
Definition Timer.h:99
void DelayExecute(const Func &callback, int delay, Args &&... args)
Executes function after X seconds.
Definition Timer.h:29
void DelayExecute(const FString &identifier, const Func &callback, int delay, Args &&... args)
Executes function after X seconds.
Definition Timer.h:44
Timer(Timer &&)=delete
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
void UnloadTimer(const FString &identifier)
Unloads timer by identifier.
Definition Timer.h:91
Timer & operator=(const Timer &)=delete
void RecurringExecute(const FString &identifier, const Func &callback, int execution_interval, int execution_counter, bool async, Args &&... args)
Executes function every X seconds.
Definition Timer.h:80
std::vector< std::shared_ptr< TimerFunc > > timer_funcs_
Definition Timer.h:143
Definition IBaseApi.h:9
ARK_API std::string GetCurrentDir()
Definition Tools.cpp:8
namespace for Niels Lohmann
Definition json.hpp:89
Definition json.hpp:4518
TimerFunc(const std::chrono::time_point< std::chrono::system_clock > &next_time, std::function< void()> callback, bool exec_once, int execution_counter, int execution_interval, FString identifier, FString moduleName)
Definition Timer.h:109
std::function< void()> callback
Definition Timer.h:126
std::chrono::time_point< std::chrono::system_clock > next_time
Definition Timer.h:125