From bed71e0eb6e57b9766a063675eeae236b500a1a2 Mon Sep 17 00:00:00 2001 From: UE4SS Date: Sun, 22 Oct 2023 21:45:18 +0200 Subject: [PATCH] Added overload for on_lua_start that fires for every mod --- UE4SS/include/Mod/CppMod.hpp | 12 ++++++++ UE4SS/include/Mod/CppUserModBase.hpp | 40 +++++++++++++++++++++++-- UE4SS/include/Mod/LuaMod.hpp | 4 +-- UE4SS/src/Mod/CppMod.cpp | 24 +++++++++++++++ UE4SS/src/Mod/LuaMod.cpp | 45 +++++++++++++++++++++------- 5 files changed, 109 insertions(+), 16 deletions(-) diff --git a/UE4SS/include/Mod/CppMod.hpp b/UE4SS/include/Mod/CppMod.hpp index 6d537f1bc..9bdb42afe 100644 --- a/UE4SS/include/Mod/CppMod.hpp +++ b/UE4SS/include/Mod/CppMod.hpp @@ -41,9 +41,21 @@ namespace RC auto start_mod() -> void override; auto uninstall() -> void override; + auto fire_on_lua_start(StringViewType mod_name, + LuaMadeSimple::Lua& lua, + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void; + auto fire_on_lua_start(LuaMadeSimple::Lua& lua, LuaMadeSimple::Lua& main_lua, LuaMadeSimple::Lua& async_lua, std::vector& hook_luas) -> void; + auto fire_on_lua_stop(StringViewType mod_name, + LuaMadeSimple::Lua& lua, + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void; + auto fire_on_lua_stop(LuaMadeSimple::Lua& lua, LuaMadeSimple::Lua& main_lua, LuaMadeSimple::Lua& async_lua, std::vector& hook_luas) -> void; diff --git a/UE4SS/include/Mod/CppUserModBase.hpp b/UE4SS/include/Mod/CppUserModBase.hpp index 96b8d3f64..449cacc94 100644 --- a/UE4SS/include/Mod/CppUserModBase.hpp +++ b/UE4SS/include/Mod/CppUserModBase.hpp @@ -56,6 +56,23 @@ namespace RC { } + /** + * Executes after a Lua mod is started. + * Executes for every Lua mod that is starting. + * @param mod_name This is the name of the Lua mod that was started. + * @param lua This is the main Lua instance. + * @param main_lua This is the main Lua thread instance. + * @param async_lua This is the Lua instance for asynchronous things like ExecuteAsync and ExecuteWithDelay. + * @param hook_luas This is a container of Lua instances that are used for game-thread hooks like ExecuteInGameThread. + */ + RC_UE4SS_API auto virtual on_lua_start(StringViewType mod_name, + LuaMadeSimple::Lua& lua, + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void + { + } + /** * Executes after a Lua mod of the same name is started. * @param lua This is the main Lua instance. @@ -70,6 +87,23 @@ namespace RC { } + /** + * Executes before a Lua mod is about to be stopped. + * Executes for every Lua mod that is stopping. + * @param mod_name This is the name of the Lua mod that is about to be stopped. + * @param lua This is the main Lua instance. + * @param main_lua This is the main Lua thread instance. + * @param async_lua This is the Lua instance for asynchronous things like ExecuteAsync and ExecuteWithDelay. + * @param hook_luas This is a container of Lua instances that are used for game-thread hooks like ExecuteInGameThread. + */ + RC_UE4SS_API auto virtual on_lua_stop(StringViewType mod_name, + LuaMadeSimple::Lua& lua, + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void + { + } + /** * Executes before a Lua mod of the same name is about to be stopped. * @param lua This is the main Lua instance. @@ -78,9 +112,9 @@ namespace RC * @param hook_luas This is a container of Lua instances that are used for game-thread hooks like ExecuteInGameThread. */ RC_UE4SS_API auto virtual on_lua_stop(LuaMadeSimple::Lua& lua, - LuaMadeSimple::Lua& main_lua, - LuaMadeSimple::Lua& async_lua, - std::vector& hook_luas) -> void + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void { } diff --git a/UE4SS/include/Mod/LuaMod.hpp b/UE4SS/include/Mod/LuaMod.hpp index 44314b40b..7298537a8 100644 --- a/UE4SS/include/Mod/LuaMod.hpp +++ b/UE4SS/include/Mod/LuaMod.hpp @@ -128,8 +128,8 @@ namespace RC auto setup_lua_global_functions(const LuaMadeSimple::Lua& lua) const -> void; auto setup_lua_global_functions_main_state_only() const -> void; auto setup_lua_classes(const LuaMadeSimple::Lua& lua) const -> void; - auto fire_on_lua_start_for_cpp_mod() -> void; - auto fire_on_lua_stop_for_cpp_mod() -> void; + auto fire_on_lua_start_for_cpp_mods() -> void; + auto fire_on_lua_stop_for_cpp_mods() -> void; public: auto start_mod() -> void override; diff --git a/UE4SS/src/Mod/CppMod.cpp b/UE4SS/src/Mod/CppMod.cpp index 87821c42e..ab9241606 100644 --- a/UE4SS/src/Mod/CppMod.cpp +++ b/UE4SS/src/Mod/CppMod.cpp @@ -78,6 +78,18 @@ namespace RC } } + auto CppMod::fire_on_lua_start(StringViewType mod_name, + LuaMadeSimple::Lua& lua, + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void + { + if (m_mod) + { + m_mod->on_lua_start(mod_name, lua, main_lua, async_lua, hook_luas); + } + } + auto CppMod::fire_on_lua_start(LuaMadeSimple::Lua& lua, LuaMadeSimple::Lua& main_lua, LuaMadeSimple::Lua& async_lua, std::vector& hook_luas) -> void { @@ -87,6 +99,18 @@ namespace RC } } + auto CppMod::fire_on_lua_stop(StringViewType mod_name, + LuaMadeSimple::Lua& lua, + LuaMadeSimple::Lua& main_lua, + LuaMadeSimple::Lua& async_lua, + std::vector& hook_luas) -> void + { + if (m_mod) + { + m_mod->on_lua_stop(mod_name, lua, main_lua, async_lua, hook_luas); + } + } + auto CppMod::fire_on_lua_stop(LuaMadeSimple::Lua& lua, LuaMadeSimple::Lua& main_lua, LuaMadeSimple::Lua& async_lua, std::vector& hook_luas) -> void { diff --git a/UE4SS/src/Mod/LuaMod.cpp b/UE4SS/src/Mod/LuaMod.cpp index e02c70fac..fd6c4507e 100644 --- a/UE4SS/src/Mod/LuaMod.cpp +++ b/UE4SS/src/Mod/LuaMod.cpp @@ -3224,21 +3224,43 @@ No overload found for function 'FPackageName:IsValidLongPackageName'. lua_setglobal(lua.get_lua_state(), "__OriginalReturnValue"); } - auto LuaMod::fire_on_lua_start_for_cpp_mod() -> void + auto LuaMod::fire_on_lua_start_for_cpp_mods() -> void { - auto cpp_mod = UE4SSProgram::find_mod_by_name(get_name(), UE4SSProgram::IsInstalled::Yes); - if (cpp_mod) + if (!is_started()) { - cpp_mod->fire_on_lua_start(m_lua, *m_main_lua, *m_async_lua, m_hook_lua); + return; + } + + for (const auto& mod : UE4SSProgram::m_mods) + { + if (auto cpp_mod = dynamic_cast(mod.get()); cpp_mod && mod->is_started()) + { + if (mod->get_name() == get_name()) + { + cpp_mod->fire_on_lua_start(m_lua, *m_main_lua, *m_async_lua, m_hook_lua); + } + cpp_mod->fire_on_lua_start(get_name(), m_lua, *m_main_lua, *m_async_lua, m_hook_lua); + } } } - auto LuaMod::fire_on_lua_stop_for_cpp_mod() -> void + auto LuaMod::fire_on_lua_stop_for_cpp_mods() -> void { - auto cpp_mod = UE4SSProgram::find_mod_by_name(get_name(), UE4SSProgram::IsInstalled::Yes); - if (cpp_mod) + if (!is_started()) { - cpp_mod->fire_on_lua_stop(m_lua, *m_main_lua, *m_async_lua, m_hook_lua); + return; + } + + for (const auto& mod : UE4SSProgram::m_mods) + { + if (auto cpp_mod = dynamic_cast(mod.get()); cpp_mod && mod->is_started()) + { + if (mod->get_name() == get_name()) + { + cpp_mod->fire_on_lua_stop(m_lua, *m_main_lua, *m_async_lua, m_hook_lua); + } + cpp_mod->fire_on_lua_stop(get_name(), m_lua, *m_main_lua, *m_async_lua, m_hook_lua); + } } } @@ -3248,13 +3270,14 @@ No overload found for function 'FPackageName:IsValidLongPackageName'. make_main_state(this, lua()); setup_lua_global_functions_main_state_only(); - fire_on_lua_start_for_cpp_mod(); - make_async_state(this, lua()); start_async_thread(); m_is_started = true; + + fire_on_lua_start_for_cpp_mods(); + // Don't crash on syntax errors. try { @@ -3273,7 +3296,7 @@ No overload found for function 'FPackageName:IsValidLongPackageName'. Output::send(STR("Stopping mod '{}' for uninstall\n"), m_mod_name); - fire_on_lua_stop_for_cpp_mod(); + fire_on_lua_stop_for_cpp_mods(); if (m_async_thread.joinable()) {