|
1 | 1 | #define NOMINMAX |
2 | 2 |
|
| 3 | +#include <atomic> |
3 | 4 | #include <filesystem> |
4 | 5 | #include <format> |
5 | 6 | #include <limits> |
@@ -116,7 +117,7 @@ namespace RC |
116 | 117 | bool has_return_value{}; |
117 | 118 | // Will be non-nullptr if the UFunction has a return value |
118 | 119 | Unreal::FProperty* return_property{}; |
119 | | - bool scheduled_for_removal{}; |
| 120 | + std::atomic<bool> scheduled_for_removal{}; |
120 | 121 | }; |
121 | 122 | static std::vector<std::unique_ptr<LuaUnrealScriptFunctionData>> g_hooked_script_function_data{}; |
122 | 123 |
|
@@ -1783,7 +1784,10 @@ No overload found for function 'UnregisterHook'. |
1783 | 1784 | const auto hook_data = std::ranges::find_if(g_hooked_script_function_data, [&](const std::unique_ptr<LuaUnrealScriptFunctionData>& elem) { |
1784 | 1785 | return elem->post_callback_id == post_id && elem->pre_callback_id == pre_id; |
1785 | 1786 | }); |
1786 | | - hook_data->get()->scheduled_for_removal = true; |
| 1787 | + if (hook_data != g_hooked_script_function_data.end()) |
| 1788 | + { |
| 1789 | + hook_data->get()->scheduled_for_removal = true; |
| 1790 | + } |
1787 | 1791 | } |
1788 | 1792 | else |
1789 | 1793 | { |
@@ -4259,6 +4263,17 @@ No overload found for function 'FPackageName:IsValidLongPackageName'. |
4259 | 4263 | lua_resetthread(m_main_lua->get_lua_state()); |
4260 | 4264 | } |
4261 | 4265 |
|
| 4266 | + // Mark all hooks for this mod as scheduled_for_removal BEFORE closing Lua state |
| 4267 | + // This prevents hooks from firing with an invalid Lua state during the window between |
| 4268 | + // lua_close and the actual hook unregistration |
| 4269 | + for (auto& item : g_hooked_script_function_data) |
| 4270 | + { |
| 4271 | + if (item->mod == this) |
| 4272 | + { |
| 4273 | + item->scheduled_for_removal = true; |
| 4274 | + } |
| 4275 | + } |
| 4276 | + |
4262 | 4277 | lua_close(lua().get_lua_state()); |
4263 | 4278 |
|
4264 | 4279 | // Unhook all UFunctions for this mod & remove from the map that keeps track of which UFunctions have been hooked |
|
0 commit comments