Fix lua native bindings and lua script reload (#1575)

* fix: script manager and lua scripts: change the api so that the m_scripts array is only modified in a specific safe location: before it gets ticked.
* fix script manager: don't expose the script vector directly, for multithreading safety
* fix lua manager usage: don't iterate the module array without locking, nor un/load module from a script thread
* lua script: only do actual loading of lua modules in script mgr
* lua native bindin: fix pointer parameters, out C style parameters are returned as multiple return values lua-style
This commit is contained in:
Quentin
2023-07-01 22:40:17 +02:00
committed by GitHub
parent 57ac1a315c
commit d1e839651b
12 changed files with 43494 additions and 6581 deletions

View File

@ -7,16 +7,31 @@ namespace big
{
std::mutex m_module_lock;
public:
bool m_schedule_reload_modules;
public:
lua_manager();
~lua_manager();
void load_all_modules();
void unload_all_modules();
inline auto get_module_count() const
{
return m_modules.size();
}
void draw_gui(rage::joaat_t tab_hash);
void unload_module(rage::joaat_t module_id);
void load_module(const std::string& module_name);
void queue_load_module(const std::string& module_name, std::function<void(std::weak_ptr<lua_module>)> on_module_loaded);
void load_modules_from_queue();
std::weak_ptr<lua_module> get_module(rage::joaat_t module_id);
const std::vector<std::shared_ptr<lua_module>>& get_modules() const;
void reload_all_modules();
void handle_error(const sol::error& error, const sol::state_view& state);
template<template_str hash_str, typename Return = void, typename... Args>
@ -24,9 +39,11 @@ namespace big
{
constexpr auto hash = rage::joaat(hash_str.value);
for (auto& modules : get_modules())
std::lock_guard guard(m_module_lock);
for (auto& module : m_modules)
{
if (auto vec = modules->m_event_callbacks.find(hash); vec != modules->m_event_callbacks.end())
if (auto vec = module->m_event_callbacks.find(hash); vec != module->m_event_callbacks.end())
{
for (auto& cb : vec->second)
{
@ -56,11 +73,25 @@ namespace big
return std::nullopt;
}
private:
void load_all_modules();
void unload_all_modules();
inline void for_each_module(auto func)
{
std::lock_guard guard(m_module_lock);
for (auto& module : m_modules)
{
func(module);
}
}
private:
std::vector<std::shared_ptr<lua_module>> m_modules;
struct module_load_info
{
std::string m_name;
std::function<void(std::weak_ptr<lua_module>)> m_on_module_loaded;
};
std::queue<module_load_info> m_modules_load_queue;
};
inline lua_manager* g_lua_manager;