From 2e63940b770c00b0b11ee171ccfd5087ef2bbae8 Mon Sep 17 00:00:00 2001 From: Arthur <121949966+ShinyWasabi@users.noreply.github.com> Date: Sun, 28 Jan 2024 17:18:44 +0300 Subject: [PATCH] feat(lua): expose the self class and add new menu events (#2656) --- docs/lua/tables/menu_event.md | 26 ++++++++++++++- src/core/data/menu_event.hpp | 2 ++ src/lua/bindings/event.cpp | 24 ++++++++++++++ src/lua/bindings/self.cpp | 43 +++++++++++++++++++++++++ src/lua/bindings/self.hpp | 6 ++++ src/lua/lua_module.cpp | 2 ++ src/views/core/view_heading.cpp | 2 ++ src/views/settings/view_lua_scripts.cpp | 7 ++-- 8 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/lua/bindings/self.cpp create mode 100644 src/lua/bindings/self.hpp diff --git a/docs/lua/tables/menu_event.md b/docs/lua/tables/menu_event.md index ba2ee0b0..f3c6c4cd 100644 --- a/docs/lua/tables/menu_event.md +++ b/docs/lua/tables/menu_event.md @@ -2,7 +2,7 @@ Table containing all possible events to which you can respond. -## Fields (6) +## Fields (8) ### `PlayerLeave` @@ -79,3 +79,27 @@ end) - Type: `integer` +### `MenuUnloaded` + +Event that is triggered when we unload YimMenu. +**Example Usage:** +```lua +event.register_handler(menu_event.MenuUnloaded, function () + log.info("Menu unloaded.") +end) +``` + +- Type: `integer` + +### `ScriptsReloaded` + +Event that is triggered when we reload the Lua scripts. +**Example Usage:** +```lua +event.register_handler(menu_event.ScriptsReloaded, function () + log.info("Scripts reloaded.") +end) +``` + +- Type: `integer` + diff --git a/src/core/data/menu_event.hpp b/src/core/data/menu_event.hpp index 9661a823..e124122b 100644 --- a/src/core/data/menu_event.hpp +++ b/src/core/data/menu_event.hpp @@ -8,4 +8,6 @@ enum class menu_event PlayerMgrShutdown, ChatMessageReceived, ScriptedGameEventReceived, + MenuUnloaded, + ScriptsReloaded, }; \ No newline at end of file diff --git a/src/lua/bindings/event.cpp b/src/lua/bindings/event.cpp index cb31d497..b134ffc4 100644 --- a/src/lua/bindings/event.cpp +++ b/src/lua/bindings/event.cpp @@ -80,6 +80,28 @@ namespace lua::event // end) // ``` + // Lua API: Field + // Table: menu_event + // Field: MenuUnloaded: integer + // Event that is triggered when we unload YimMenu. + // **Example Usage:** + // ```lua + // event.register_handler(menu_event.MenuUnloaded, function () + // log.info("Menu unloaded.") + // end) + // ``` + + // Lua API: Field + // Table: menu_event + // Field: ScriptsReloaded: integer + // Event that is triggered when we reload the Lua scripts. + // **Example Usage:** + // ```lua + // event.register_handler(menu_event.ScriptsReloaded, function () + // log.info("Scripts reloaded.") + // end) + // ``` + // Lua API: Table // Name: event // Table for responding to various events. The list of events is available in the menu_event table. @@ -107,6 +129,8 @@ namespace lua::event {"PlayerMgrShutdown", menu_event::PlayerMgrShutdown}, {"ChatMessageReceived", menu_event::ChatMessageReceived}, {"ScriptedGameEventReceived", menu_event::ScriptedGameEventReceived}, + {"MenuUnloaded", menu_event::MenuUnloaded}, + {"ScriptsReloaded", menu_event::ScriptsReloaded}, }); diff --git a/src/lua/bindings/self.cpp b/src/lua/bindings/self.cpp new file mode 100644 index 00000000..a0dfeeab --- /dev/null +++ b/src/lua/bindings/self.cpp @@ -0,0 +1,43 @@ +#pragma once +#include "self.hpp" + +namespace lua_self = self; + +namespace lua::self +{ + static Ped get_ped() + { + return lua_self::ped; + } + + static Player get_id() + { + return lua_self::id; + } + + static Vector3 get_pos() + { + return lua_self::pos; + } + + static Vector3 get_rot() + { + return lua_self::rot; + } + + static Vehicle get_veh() + { + return lua_self::veh; + } + + void bind(sol::state& state) + { + auto ns = state["self"].get_or_create(); + + ns["get_ped"] = get_ped; + ns["get_id"] = get_id; + ns["get_pos"] = get_pos; + ns["get_rot"] = get_rot; + ns["get_veh"] = get_veh; + } +} \ No newline at end of file diff --git a/src/lua/bindings/self.hpp b/src/lua/bindings/self.hpp new file mode 100644 index 00000000..32d1fbe9 --- /dev/null +++ b/src/lua/bindings/self.hpp @@ -0,0 +1,6 @@ +#pragma once + +namespace lua::self +{ + void bind(sol::state& state); +} \ No newline at end of file diff --git a/src/lua/lua_module.cpp b/src/lua/lua_module.cpp index 37607bd2..34f544b7 100644 --- a/src/lua/lua_module.cpp +++ b/src/lua/lua_module.cpp @@ -13,6 +13,7 @@ #include "bindings/native.hpp" #include "bindings/network.hpp" #include "bindings/script.hpp" +#include "bindings/self.hpp" #include "bindings/stats.hpp" #include "bindings/tunables.hpp" #include "bindings/vector.hpp" @@ -283,6 +284,7 @@ namespace big lua::global_table::bind(m_state); lua::imgui::bind(m_state, m_state.globals()); lua::entities::bind(m_state); + lua::self::bind(m_state); lua::stats::bind(m_state); lua::weapons::bind(m_state); lua::vehicles::bind(m_state); diff --git a/src/views/core/view_heading.cpp b/src/views/core/view_heading.cpp index d9674545..614e54d5 100644 --- a/src/views/core/view_heading.cpp +++ b/src/views/core/view_heading.cpp @@ -31,6 +31,7 @@ namespace big // empty the pool, we want the that job below run no matter what for clean up purposes. g_fiber_pool->reset(); g_fiber_pool->queue_job([] { + g_lua_manager->trigger_event(); for (auto& command : g_looped_commands) if (command->is_enabled()) command->on_disable(); @@ -40,6 +41,7 @@ namespace big } else { + g_lua_manager->trigger_event(); g_running = false; } } diff --git a/src/views/settings/view_lua_scripts.cpp b/src/views/settings/view_lua_scripts.cpp index 9219e08b..bae793bf 100644 --- a/src/views/settings/view_lua_scripts.cpp +++ b/src/views/settings/view_lua_scripts.cpp @@ -16,8 +16,11 @@ namespace big if (components::button("VIEW_LUA_SCRIPTS_RELOAD_ALL"_T)) { - g_lua_manager->unload_all_modules(); - g_lua_manager->load_all_modules(); + g_fiber_pool->queue_job([] { + g_lua_manager->trigger_event(); + g_lua_manager->unload_all_modules(); + g_lua_manager->load_all_modules(); + }); } ImGui::SameLine(); ImGui::Checkbox("VIEW_LUA_SCRIPTS_AUTO_RELOAD_CHANGED_SCRIPTS"_T.data(), &g.lua.enable_auto_reload_changed_scripts);