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/tls_context.hpp"
|
|
|
|
#include "gta_util.hpp"
|
2019-07-29 23:19:21 +02:00
|
|
|
#include "invoker.hpp"
|
2019-03-21 20:18:31 +01:00
|
|
|
#include "pointers.hpp"
|
2023-07-01 22:40:17 +02:00
|
|
|
#include "lua/lua_manager.hpp"
|
2019-03-21 20:18:31 +01:00
|
|
|
|
|
|
|
namespace big
|
|
|
|
{
|
2023-06-06 07:40:40 +00:00
|
|
|
script* 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-06-06 07:40:40 +00:00
|
|
|
auto* ret = script.get();
|
2023-07-01 22:40:17 +02:00
|
|
|
m_scripts_to_add.push_back(std::move(script));
|
2023-06-06 07:40:40 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void script_mgr::remove_script(script* scr)
|
|
|
|
{
|
|
|
|
std::lock_guard lock(m_mutex);
|
2023-07-01 22:40:17 +02:00
|
|
|
|
|
|
|
scr->m_should_be_deleted = true;
|
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()
|
|
|
|
{
|
|
|
|
gta_util::execute_as_script(RAGE_JOAAT("main_persistent"), std::mem_fn(&script_mgr::tick_internal), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void script_mgr::tick_internal()
|
|
|
|
{
|
|
|
|
static bool ensure_main_fiber = (ConvertThreadToFiber(nullptr), true);
|
|
|
|
|
|
|
|
std::lock_guard lock(m_mutex);
|
2023-07-01 22:40:17 +02:00
|
|
|
|
|
|
|
if (g_lua_manager->m_schedule_reload_modules)
|
|
|
|
{
|
|
|
|
g_lua_manager->unload_all_modules();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::erase_if(m_scripts, [](std::unique_ptr<script>& iter) {
|
|
|
|
return iter->m_should_be_deleted;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (g_lua_manager->m_schedule_reload_modules)
|
|
|
|
{
|
|
|
|
g_lua_manager->load_all_modules();
|
|
|
|
g_lua_manager->m_schedule_reload_modules = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_lua_manager->load_modules_from_queue();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_scripts_to_add.size())
|
|
|
|
{
|
|
|
|
for (auto& script_to_add : m_scripts_to_add)
|
|
|
|
{
|
|
|
|
m_scripts.push_back(std::move(script_to_add));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_scripts_to_add.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|