2021-09-09 21:21:51 -04:00
|
|
|
#include <std_include.hpp>
|
|
|
|
#include "loader/component_loader.hpp"
|
|
|
|
|
|
|
|
#include "game/game.hpp"
|
|
|
|
#include "game/dvars.hpp"
|
|
|
|
|
|
|
|
#include "chat.hpp"
|
|
|
|
#include "scheduler.hpp"
|
|
|
|
#include "command.hpp"
|
2021-09-19 12:20:15 -04:00
|
|
|
|
2021-09-09 21:21:51 -04:00
|
|
|
#include "ui_scripting.hpp"
|
|
|
|
|
|
|
|
#include "game/ui_scripting/lua/engine.hpp"
|
2021-10-02 12:30:21 -04:00
|
|
|
#include "game/ui_scripting/execution.hpp"
|
2021-10-20 18:21:50 -04:00
|
|
|
#include "game/ui_scripting/lua/error.hpp"
|
2021-09-09 21:21:51 -04:00
|
|
|
|
|
|
|
#include <utils/string.hpp>
|
2021-09-26 19:37:48 -04:00
|
|
|
#include <utils/hook.hpp>
|
2021-09-09 21:21:51 -04:00
|
|
|
|
|
|
|
namespace ui_scripting
|
|
|
|
{
|
2021-09-26 19:37:48 -04:00
|
|
|
namespace
|
2021-09-09 21:21:51 -04:00
|
|
|
{
|
2021-10-02 12:30:21 -04:00
|
|
|
std::unordered_map<game::hks::cclosure*, sol::protected_function> converted_functions;
|
|
|
|
|
2021-09-26 19:37:48 -04:00
|
|
|
utils::hook::detour hksi_lual_error_hook;
|
2021-09-30 18:55:14 -04:00
|
|
|
utils::hook::detour hksi_lual_error_hook2;
|
2021-09-26 19:37:48 -04:00
|
|
|
utils::hook::detour hks_start_hook;
|
|
|
|
utils::hook::detour hks_shutdown_hook;
|
2021-09-09 21:21:51 -04:00
|
|
|
|
2021-09-26 19:37:48 -04:00
|
|
|
bool error_hook_enabled = false;
|
|
|
|
|
|
|
|
void hksi_lual_error_stub(game::hks::lua_State* s, const char* fmt, ...)
|
|
|
|
{
|
2022-01-31 16:47:30 -05:00
|
|
|
char va_buffer[2048] = {0};
|
2021-09-26 19:37:48 -04:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vsprintf_s(va_buffer, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
const auto formatted = std::string(va_buffer);
|
|
|
|
|
|
|
|
if (!error_hook_enabled)
|
|
|
|
{
|
|
|
|
return hksi_lual_error_hook.invoke<void>(s, formatted.data());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::runtime_error(formatted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* hks_start_stub(char a1)
|
|
|
|
{
|
|
|
|
const auto _ = gsl::finally([]()
|
2021-09-09 21:21:51 -04:00
|
|
|
{
|
|
|
|
ui_scripting::lua::engine::start();
|
2021-09-26 19:37:48 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return hks_start_hook.invoke<void*>(a1);
|
|
|
|
}
|
2021-09-09 21:21:51 -04:00
|
|
|
|
2021-09-26 19:37:48 -04:00
|
|
|
void hks_shutdown_stub()
|
|
|
|
{
|
|
|
|
ui_scripting::lua::engine::stop();
|
|
|
|
hks_shutdown_hook.invoke<void*>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:30:21 -04:00
|
|
|
int main_function_handler(game::hks::lua_State* state)
|
|
|
|
{
|
|
|
|
const auto value = state->m_apistack.base[-1];
|
|
|
|
if (value.t != game::hks::TCFUNCTION)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto closure = value.v.cClosure;
|
|
|
|
if (converted_functions.find(closure) == converted_functions.end())
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto function = converted_functions[closure];
|
|
|
|
const auto count = static_cast<int>(state->m_apistack.top - state->m_apistack.base);
|
|
|
|
const auto arguments = get_return_values(count);
|
|
|
|
const auto s = function.lua_state();
|
|
|
|
|
|
|
|
std::vector<sol::lua_value> converted_args;
|
|
|
|
|
|
|
|
for (const auto& argument : arguments)
|
|
|
|
{
|
|
|
|
converted_args.push_back(lua::convert(s, argument));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto results = function(sol::as_args(converted_args));
|
2021-10-20 18:21:50 -04:00
|
|
|
lua::handle_error(results);
|
2021-10-02 12:30:21 -04:00
|
|
|
|
|
|
|
for (const auto& result : results)
|
|
|
|
{
|
|
|
|
push_value(lua::convert({s, result}));
|
|
|
|
}
|
|
|
|
|
|
|
|
return results.return_count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void add_converted_function(game::hks::cclosure* closure, const sol::protected_function& function)
|
|
|
|
{
|
|
|
|
converted_functions[closure] = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear_converted_functions()
|
|
|
|
{
|
|
|
|
converted_functions.clear();
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:37:48 -04:00
|
|
|
void enable_error_hook()
|
|
|
|
{
|
|
|
|
error_hook_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable_error_hook()
|
|
|
|
{
|
|
|
|
error_hook_enabled = false;
|
|
|
|
}
|
|
|
|
|
2021-09-28 16:39:27 -04:00
|
|
|
void notify(const event& e)
|
|
|
|
{
|
|
|
|
lua::engine::notify(e);
|
2021-09-26 19:37:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class component final : public component_interface
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void post_unpack() override
|
|
|
|
{
|
2022-02-06 15:36:49 -05:00
|
|
|
scheduler::loop(ui_scripting::lua::engine::run_frame, scheduler::pipeline::lui);
|
2021-09-26 19:37:48 -04:00
|
|
|
|
2021-12-03 14:47:16 -05:00
|
|
|
hks_start_hook.create(0x328BE0_b, hks_start_stub);
|
|
|
|
hks_shutdown_hook.create(0x3203B0_b, hks_shutdown_stub);
|
|
|
|
hksi_lual_error_hook.create(0x2E3E40_b, hksi_lual_error_stub);
|
|
|
|
hksi_lual_error_hook2.create(0x2DCB40_b, hksi_lual_error_stub);
|
2021-09-09 21:21:51 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_COMPONENT(ui_scripting::component)
|