UI scripting changes + cleanup

This commit is contained in:
fed 2023-10-22 20:08:56 +02:00
parent b0a1f5ca73
commit 134bcbae10
No known key found for this signature in database
GPG Key ID: 1D2C630F04722996
3 changed files with 27 additions and 20 deletions

View File

@ -14,7 +14,6 @@ namespace logger
namespace
{
utils::hook::detour com_error_hook;
utils::hook::detour nullsub_48_hook;
utils::hook::detour sub_32AEF0;
game::dvar_t* logger_dev = nullptr;

View File

@ -508,17 +508,10 @@ namespace pathnodes
void restore_code(const size_t ptr, char* data, const size_t size)
{
const auto ptr_ = reinterpret_cast<char*>(ptr);
DWORD old_protect;
VirtualProtect(ptr_, size, PAGE_EXECUTE_READWRITE, &old_protect);
for (auto i = 0; i < size; i++)
{
ptr_[i] = data[i];
utils::hook::set<char>(ptr + i, data[i]);
}
VirtualProtect(ptr_, size, old_protect, &old_protect);
FlushInstructionCache(GetCurrentProcess(), ptr_, size);
}
void restore_functions()

View File

@ -37,7 +37,8 @@ namespace ui_scripting
const auto lui_updater = utils::nt::load_resource(LUI_UPDATER);
const auto lua_json = utils::nt::load_resource(LUA_JSON);
std::unordered_map<game::hks::cclosure*, std::function<arguments(const function_arguments& args)>> converted_functions;
using converted_func_t = std::function<arguments(const function_arguments& args)>;
std::unordered_map<game::hks::cclosure*, converted_func_t> converted_functions;
utils::hook::detour hks_start_hook;
utils::hook::detour hks_shutdown_hook;
@ -852,10 +853,10 @@ namespace ui_scripting
return utils::hook::invoke<int>(0x1402D9410, state, compiler_options, reader, reader_data, chunk_name);
}
std::string current_error;
int main_handler(game::hks::lua_State* state)
{
bool error = false;
static std::string error_str;
auto error = false;
try
{
@ -866,15 +867,14 @@ namespace ui_scripting
}
const auto closure = value.v.cClosure;
if (!converted_functions.contains(closure))
if (closure->m_numUpvalues < 1)
{
return 0;
}
const auto& function = converted_functions[closure];
const auto function = reinterpret_cast<converted_func_t*>(closure->m_upvalues->v.i64);
const auto args = get_return_values();
const auto results = function(args);
const auto results = function->operator()(args);
for (const auto& result : results)
{
@ -885,13 +885,13 @@ namespace ui_scripting
}
catch (const std::exception& e)
{
current_error = e.what();
error_str = e.what();
error = true;
}
if (error)
{
game::hks::hksi_luaL_error(state, current_error.data());
game::hks::hksi_luaL_error(state, error_str.data());
}
return 0;
@ -907,8 +907,23 @@ namespace ui_scripting
game::hks::cclosure* convert_function(F f)
{
const auto state = *game::hks::lua_state;
const auto closure = game::hks::cclosure_Create(state, main_handler, 0, 0, 0);
converted_functions[closure] = wrap_function(f);
const auto top = state->m_apistack.top;
game::hks::HksObject func_ptr{};
func_ptr.t = game::hks::TUI64;
func_ptr.v.i64 = 0;
push_value(func_ptr);
const auto closure = game::hks::cclosure_Create(state, main_handler, 1, 0, 0);
state->m_apistack.top = top;
const auto function = wrap_function(f);
const auto [iterator, was_inserted] = converted_functions.insert(std::make_pair(closure, function));
const auto ptr = &iterator->second;
closure->m_upvalues[0].t = game::hks::TUI64;
closure->m_upvalues[0].v.i64 = reinterpret_cast<size_t>(ptr);
return closure;
}