Add LUI notify function

This commit is contained in:
Federico Cecchetto 2022-03-17 20:58:26 +01:00
parent fc6deb476b
commit cea4d720be
4 changed files with 44 additions and 0 deletions

View File

@ -146,6 +146,8 @@ namespace game
WEAK symbol<void(int clientNum, const char* menu, WEAK symbol<void(int clientNum, const char* menu,
int a3, int a4, unsigned int a5)> LUI_OpenMenu{0x14039D5F0, 0x1404CD210}; int a3, int a4, unsigned int a5)> LUI_OpenMenu{0x14039D5F0, 0x1404CD210};
WEAK symbol<bool(int clientNum, const char* name, hks::lua_State* s)> LUI_BeginEvent{0x1400D27F0, 0x140161A00};
WEAK symbol<void(hks::lua_State* s)> LUI_EndEvent{0x1400D3A80, 0x140162CD0};
WEAK symbol<bool(int clientNum, const char* menu)> Menu_IsMenuOpenAndVisible{0x1404709C0, 0x1404C7320}; WEAK symbol<bool(int clientNum, const char* menu)> Menu_IsMenuOpenAndVisible{0x1404709C0, 0x1404C7320};

View File

@ -37,6 +37,45 @@ namespace ui_scripting
return values; return values;
} }
bool notify(const std::string& name, const event_arguments& arguments)
{
const auto state = *game::hks::lua_state;
if (!game::LUI_BeginEvent(0, name.data(), state))
{
return false;
}
const auto _1 = gsl::finally(&disable_error_hook);
enable_error_hook();
const auto top = state->m_apistack.top;
try
{
const auto event = get_return_value(0).as<table>();
for (const auto& arg : arguments)
{
event.set(arg.first, arg.second);
}
}
catch (...)
{
}
state->m_apistack.top = top;
try
{
game::LUI_EndEvent(state);
}
catch (const std::exception& e)
{
throw std::runtime_error(std::string("Error while processing event: ") + e.what());
}
return true;
}
arguments call_script_function(const function& function, const arguments& arguments) arguments call_script_function(const function& function, const arguments& arguments)
{ {
const auto state = *game::hks::lua_state; const auto state = *game::hks::lua_state;

View File

@ -9,6 +9,8 @@ namespace ui_scripting
script_value get_return_value(int offset); script_value get_return_value(int offset);
arguments get_return_values(int count); arguments get_return_values(int count);
bool notify(const std::string& name, const event_arguments& arguments);
arguments call_script_function(const function& function, const arguments& arguments); arguments call_script_function(const function& function, const arguments& arguments);
script_value get_field(const userdata& self, const script_value& key); script_value get_field(const userdata& self, const script_value& key);

View File

@ -53,4 +53,5 @@ namespace ui_scripting
}; };
using arguments = std::vector<script_value>; using arguments = std::vector<script_value>;
using event_arguments = std::unordered_map<std::string, script_value>;
} }