Skip to content

Commit

Permalink
Added overload for on_lua_start that fires for every mod
Browse files Browse the repository at this point in the history
  • Loading branch information
UE4SS committed Oct 22, 2023
1 parent 3cea237 commit bed71e0
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 16 deletions.
12 changes: 12 additions & 0 deletions UE4SS/include/Mod/CppMod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LuaMadeSimple::Lua*>& hook_luas) -> void;

auto fire_on_lua_start(LuaMadeSimple::Lua& lua, LuaMadeSimple::Lua& main_lua, LuaMadeSimple::Lua& async_lua, std::vector<LuaMadeSimple::Lua*>& hook_luas)
-> void;

auto fire_on_lua_stop(StringViewType mod_name,
LuaMadeSimple::Lua& lua,
LuaMadeSimple::Lua& main_lua,
LuaMadeSimple::Lua& async_lua,
std::vector<LuaMadeSimple::Lua*>& hook_luas) -> void;

auto fire_on_lua_stop(LuaMadeSimple::Lua& lua, LuaMadeSimple::Lua& main_lua, LuaMadeSimple::Lua& async_lua, std::vector<LuaMadeSimple::Lua*>& hook_luas)
-> void;

Expand Down
40 changes: 37 additions & 3 deletions UE4SS/include/Mod/CppUserModBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LuaMadeSimple::Lua*>& hook_luas) -> void
{
}

/**
* Executes after a Lua mod of the same name is started.
* @param lua This is the main Lua instance.
Expand All @@ -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<LuaMadeSimple::Lua*>& 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.
Expand All @@ -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<LuaMadeSimple::Lua*>& hook_luas) -> void
LuaMadeSimple::Lua& main_lua,
LuaMadeSimple::Lua& async_lua,
std::vector<LuaMadeSimple::Lua*>& hook_luas) -> void
{
}

Expand Down
4 changes: 2 additions & 2 deletions UE4SS/include/Mod/LuaMod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 24 additions & 0 deletions UE4SS/src/Mod/CppMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<LuaMadeSimple::Lua*>& 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<LuaMadeSimple::Lua*>& hook_luas)
-> void
{
Expand All @@ -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<LuaMadeSimple::Lua*>& 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<LuaMadeSimple::Lua*>& hook_luas)
-> void
{
Expand Down
45 changes: 34 additions & 11 deletions UE4SS/src/Mod/LuaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CppMod>(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<CppMod*>(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<CppMod>(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<CppMod*>(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);
}
}
}

Expand All @@ -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
{
Expand All @@ -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())
{
Expand Down

0 comments on commit bed71e0

Please sign in to comment.