fix(lua): better exception handling (#1734)

This commit is contained in:
Quentin
2023-07-16 23:32:34 +02:00
committed by GitHub
parent 2a81a041cf
commit 5a76164d43
8 changed files with 60 additions and 51 deletions

View File

@ -88,7 +88,7 @@ namespace lua::event
// Param: menu_event: menu_event: The menu_event that we want to respond to.
// Param: func: function: The function that will be called.
// Register a function that will be called each time the corresponding menu_event is triggered.
static void register_handler(const menu_event& menu_event, sol::function func, sol::this_state state)
static void register_handler(const menu_event& menu_event, sol::protected_function func, sol::this_state state)
{
const auto module = sol::state_view(state)["!this"].get<big::lua_module*>();

View File

@ -166,7 +166,7 @@ namespace lua::gui
// Param: name: string: Text written inside the button.
// Param: callback: function: function that will be called when the button is clicked.
// Add a button to the gui tab.
std::shared_ptr<lua::gui::button> add_button(const std::string& name, sol::function callback, sol::this_state state)
std::shared_ptr<lua::gui::button> add_button(const std::string& name, sol::protected_function callback, sol::this_state state)
{
auto element = std::make_shared<lua::gui::button>(name, callback);
add_element(state, m_tab_hash, element);

View File

@ -5,7 +5,7 @@
namespace lua::gui
{
button::button(std::string text, sol::function callback) :
button::button(std::string text, sol::protected_function callback) :
base_text_element(text),
m_callback(callback)
{

View File

@ -10,11 +10,11 @@ namespace lua::gui
// Class representing a gui button.
class button : public base_text_element
{
sol::function m_callback;
sol::protected_function m_callback;
bool m_execute_in_fiber_pool = true;
public:
button(std::string text, sol::function callback);
button(std::string text, sol::protected_function callback);
void draw() override;
};

View File

@ -70,14 +70,13 @@ namespace lua::script
// ENTITY.DELETE_ENTITY(spawnedVehicle)
// end)
// ```
static void register_looped(const std::string& name, sol::function func_, sol::this_state state)
static void register_looped(const std::string& name, sol::protected_function func_, sol::this_state state)
{
auto module = sol::state_view(state)["!this"].get<big::lua_module*>();
std::unique_ptr<big::script> lua_script = std::make_unique<big::script>(
[func_, state]() mutable {
sol::thread t = sol::thread::create(state);
sol::thread t = sol::thread::create(state);
sol::coroutine func = sol::coroutine(t.state(), func_);
while (big::g_running)
@ -97,8 +96,7 @@ namespace lua::script
}
}
},
name
);
name);
const auto registered_script = big::g_script_mgr.add_script(std::move(lua_script));
@ -115,25 +113,25 @@ namespace lua::script
// script.run_in_fiber(function (script)
// -- sleep until next game frame
// script:yield()
//
//
// local ModelHash = joaat("adder")
// if not STREAMING.IS_MODEL_IN_CDIMAGE(ModelHash) then return end
// STREAMING.REQUEST_MODEL(ModelHash) -- Request the model
// while not STREAMING.HAS_MODEL_LOADED(ModelHash) do -- Waits for the model to load
// script:yield()
// end
// local myPed = PLAYER.PLAYER_PED_ID()
// local myCoords = ENTITY.GET_ENTITY_COORDS(myPed, true)
// -- Spawns a networked vehicle on your current coords
// local spawnedVehicle = VEHICLE.CREATE_VEHICLE(ModelHash, myCoords.x, myCoords.y, myCoords.z, ENTITY.GET_ENTITY_HEADING(myPed), true, false)
// -- removes model from game memory as we no longer need it
// STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(ModelHash)
// if not STREAMING.IS_MODEL_IN_CDIMAGE(ModelHash) then return end
// STREAMING.REQUEST_MODEL(ModelHash) -- Request the model
// while not STREAMING.HAS_MODEL_LOADED(ModelHash) do -- Waits for the model to load
// script:yield()
// end
// local myPed = PLAYER.PLAYER_PED_ID()
// local myCoords = ENTITY.GET_ENTITY_COORDS(myPed, true)
// -- Spawns a networked vehicle on your current coords
// local spawnedVehicle = VEHICLE.CREATE_VEHICLE(ModelHash, myCoords.x, myCoords.y, myCoords.z, ENTITY.GET_ENTITY_HEADING(myPed), true, false)
// -- removes model from game memory as we no longer need it
// STREAMING.SET_MODEL_AS_NO_LONGER_NEEDED(ModelHash)
// -- sleep for 2s
// script:sleep(2000)
// ENTITY.DELETE_ENTITY(spawnedVehicle)
// script:sleep(2000)
// ENTITY.DELETE_ENTITY(spawnedVehicle)
// end)
// ```
static void run_in_fiber(sol::function func_, sol::this_state state)
static void run_in_fiber(sol::protected_function func_, sol::this_state state)
{
auto module = sol::state_view(state)["!this"].get<big::lua_module*>();