2019-03-21 20:18:31 +01:00
|
|
|
#pragma once
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "script.hpp"
|
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2022-06-27 20:12:45 +02:00
|
|
|
using script_list = std::vector<std::unique_ptr<script>>;
|
|
|
|
|
2019-03-21 20:18:31 +01:00
|
|
|
class script_mgr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit script_mgr() = default;
|
2023-03-01 21:27:15 +00:00
|
|
|
~script_mgr() = default;
|
2019-03-21 20:18:31 +01:00
|
|
|
|
2023-06-06 07:40:40 +00:00
|
|
|
script* add_script(std::unique_ptr<script> script);
|
|
|
|
void remove_script(script* script);
|
2019-03-21 20:18:31 +01:00
|
|
|
void remove_all_scripts();
|
|
|
|
|
2023-07-01 22:40:17 +02:00
|
|
|
inline void for_each_script(auto func)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
|
|
|
|
|
|
|
for (const auto& script : m_scripts)
|
|
|
|
{
|
|
|
|
func(script);
|
|
|
|
}
|
|
|
|
}
|
2022-06-27 20:12:45 +02:00
|
|
|
|
2019-03-21 20:18:31 +01:00
|
|
|
void tick();
|
2023-03-01 21:27:15 +00:00
|
|
|
|
2023-07-11 09:24:44 +02:00
|
|
|
[[nodiscard]] inline bool can_tick() const
|
|
|
|
{
|
|
|
|
return m_can_tick;
|
|
|
|
}
|
|
|
|
|
2019-03-21 20:18:31 +01:00
|
|
|
private:
|
2023-07-11 09:24:44 +02:00
|
|
|
void ensure_main_fiber();
|
2019-03-21 20:18:31 +01:00
|
|
|
void tick_internal();
|
2023-03-01 21:27:15 +00:00
|
|
|
|
2019-03-21 20:18:31 +01:00
|
|
|
private:
|
|
|
|
std::recursive_mutex m_mutex;
|
2022-06-27 20:12:45 +02:00
|
|
|
script_list m_scripts;
|
2023-07-01 22:40:17 +02:00
|
|
|
script_list m_scripts_to_add;
|
2023-07-11 09:24:44 +02:00
|
|
|
|
|
|
|
bool m_can_tick = false;
|
2019-03-21 20:18:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
inline script_mgr g_script_mgr;
|
2022-06-27 20:12:45 +02:00
|
|
|
}
|