2023-03-01 21:27:15 +00:00
|
|
|
#include "script_mgr.hpp"
|
|
|
|
|
2019-03-21 20:18:31 +01:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "gta/script_thread.hpp"
|
|
|
|
#include "gta_util.hpp"
|
|
|
|
#include "pointers.hpp"
|
2023-11-30 04:47:39 -05:00
|
|
|
#include "script/tlsContext.hpp"
|
2019-03-21 20:18:31 +01:00
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2023-07-22 13:05:43 +02:00
|
|
|
void script_mgr::add_script(std::unique_ptr<script> script)
|
2019-03-21 20:18:31 +01:00
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
2023-07-01 22:40:17 +02:00
|
|
|
|
2023-07-22 13:05:43 +02:00
|
|
|
m_scripts.push_back(std::move(script));
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void script_mgr::remove_all_scripts()
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
|
|
|
|
2023-07-01 22:40:17 +02:00
|
|
|
m_scripts.clear();
|
2022-06-27 20:12:45 +02:00
|
|
|
}
|
|
|
|
|
2019-03-21 20:18:31 +01:00
|
|
|
void script_mgr::tick()
|
|
|
|
{
|
2024-03-12 09:42:11 +01:00
|
|
|
gta_util::execute_as_script("main_persistent"_J, std::mem_fn(&script_mgr::tick_internal), this);
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
|
|
|
|
2023-07-11 09:24:44 +02:00
|
|
|
void script_mgr::ensure_main_fiber()
|
|
|
|
{
|
|
|
|
ConvertThreadToFiber(nullptr);
|
|
|
|
|
|
|
|
m_can_tick = true;
|
|
|
|
}
|
|
|
|
|
2023-07-22 13:05:43 +02:00
|
|
|
static void lua_manager_tick()
|
2019-03-21 20:18:31 +01:00
|
|
|
{
|
2023-07-02 22:32:46 +02:00
|
|
|
g_lua_manager->reload_changed_scripts();
|
|
|
|
|
2023-07-22 13:05:43 +02:00
|
|
|
g_lua_manager->for_each_module([](const std::shared_ptr<lua_module>& module) {
|
|
|
|
module->tick_scripts();
|
|
|
|
module->cleanup_done_scripts();
|
2023-07-01 22:40:17 +02:00
|
|
|
});
|
2023-07-22 13:05:43 +02:00
|
|
|
}
|
2023-07-01 22:40:17 +02:00
|
|
|
|
2023-07-22 13:05:43 +02:00
|
|
|
void script_mgr::tick_internal()
|
|
|
|
{
|
|
|
|
static bool ensure_it = (ensure_main_fiber(), true);
|
2023-07-01 22:40:17 +02:00
|
|
|
|
2023-07-22 13:05:43 +02:00
|
|
|
std::lock_guard lock(m_mutex);
|
2023-07-01 22:40:17 +02:00
|
|
|
|
2023-07-22 13:05:43 +02:00
|
|
|
lua_manager_tick();
|
2023-07-01 22:40:17 +02:00
|
|
|
|
|
|
|
for (const auto& script : m_scripts)
|
|
|
|
{
|
2022-06-27 20:12:45 +02:00
|
|
|
if (script->is_enabled())
|
|
|
|
script->tick();
|
2023-07-01 22:40:17 +02:00
|
|
|
}
|
2019-03-21 20:18:31 +01:00
|
|
|
}
|
2022-09-30 20:41:26 +02:00
|
|
|
}
|