@@ -247,29 +247,34 @@ namespace RC
247247 // Fetch the data corresponding to this UFunction
248248 auto & lua_data = *static_cast <LuaUnrealScriptFunctionData*>(custom_data);
249249
250- if (lua_data.scheduled_for_removal )
251- {
252- const auto function_name_no_prefix = get_function_name_without_prefix (lua_data.unreal_function ->GetFullName ());
250+ auto remove_if_scheduled = [&] {
251+ if (lua_data.scheduled_for_removal )
252+ {
253+ const auto function_name_no_prefix = get_function_name_without_prefix (lua_data.unreal_function ->GetFullName ());
253254
254- Output::send<LogLevel::Verbose>(STR (" Unregistering native pre-hook ({}) for {}\n " ), lua_data.pre_callback_id , function_name_no_prefix);
255- lua_data.unreal_function ->UnregisterHook (lua_data.pre_callback_id );
256- luaL_unref (lua_data.lua .get_lua_state (), LUA_REGISTRYINDEX, lua_data.lua_callback_ref );
255+ Output::send<LogLevel::Verbose>(STR (" Unregistering native pre-hook ({}) for {}\n " ), lua_data.pre_callback_id , function_name_no_prefix);
256+ lua_data.unreal_function ->UnregisterHook (lua_data.pre_callback_id );
257+ luaL_unref (lua_data.lua .get_lua_state (), LUA_REGISTRYINDEX, lua_data.lua_callback_ref );
257258
258- Output::send<LogLevel::Verbose>(STR (" Unregistering native post-hook ({}) for {}\n " ), lua_data.post_callback_id , function_name_no_prefix);
259- lua_data.unreal_function ->UnregisterHook (lua_data.post_callback_id );
260- if (lua_data.lua_post_callback_ref != -1 )
261- {
262- luaL_unref (lua_data.lua .get_lua_state (), LUA_REGISTRYINDEX, lua_data.lua_post_callback_ref );
263- }
259+ Output::send<LogLevel::Verbose>(STR (" Unregistering native post-hook ({}) for {}\n " ), lua_data.post_callback_id , function_name_no_prefix);
260+ lua_data.unreal_function ->UnregisterHook (lua_data.post_callback_id );
261+ if (lua_data.lua_post_callback_ref != -1 )
262+ {
263+ luaL_unref (lua_data.lua .get_lua_state (), LUA_REGISTRYINDEX, lua_data.lua_post_callback_ref );
264+ }
264265
265- const auto mod = get_mod_ref (lua_data.lua );
266- luaL_unref (mod->lua ().get_lua_state (), LUA_REGISTRYINDEX, lua_data.lua_thread_ref );
267- std::erase_if (g_hooked_script_function_data, [&](const std::unique_ptr<LuaUnrealScriptFunctionData>& elem) {
268- return elem.get () == &lua_data;
269- });
266+ const auto mod = get_mod_ref (lua_data.lua );
267+ luaL_unref (mod->lua ().get_lua_state (), LUA_REGISTRYINDEX, lua_data.lua_thread_ref );
268+ std::erase_if (g_hooked_script_function_data, [&](const std::unique_ptr<LuaUnrealScriptFunctionData>& elem) {
269+ return elem.get () == &lua_data;
270+ });
270271
271- return ;
272- }
272+ return ;
273+ }
274+ };
275+
276+ // Removes pre & post-hook callbacks if UnregisterHook was called in the pre-callback hook.
277+ remove_if_scheduled ();
273278
274279 // This is a promise that we're in the game thread, used by other functions to ensure that we don't execute when unsafe
275280 set_is_in_game_thread (lua_data.lua , true );
@@ -430,6 +435,9 @@ namespace RC
430435 // No longer promising to be in the game thread
431436 // Must be done before cleanup since cleanup deletes lua_data
432437 set_is_in_game_thread (lua_data.lua , false );
438+
439+ // Removes the post-hook callback if UnregisterHook was called in the post-hook callback.
440+ remove_if_scheduled ();
433441 }
434442
435443 static auto register_input_globals (const LuaMadeSimple::Lua& lua) -> void
0 commit comments