diff --git a/src/client/component/logger.cpp b/src/client/component/logger.cpp index 5f5fe60b..8e6f92c7 100644 --- a/src/client/component/logger.cpp +++ b/src/client/component/logger.cpp @@ -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; diff --git a/src/client/component/pathnodes.cpp b/src/client/component/pathnodes.cpp index 74bad445..24f29baa 100644 --- a/src/client/component/pathnodes.cpp +++ b/src/client/component/pathnodes.cpp @@ -508,17 +508,10 @@ namespace pathnodes void restore_code(const size_t ptr, char* data, const size_t size) { - const auto ptr_ = reinterpret_cast(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(ptr + i, data[i]); } - - VirtualProtect(ptr_, size, old_protect, &old_protect); - FlushInstructionCache(GetCurrentProcess(), ptr_, size); } void restore_functions() diff --git a/src/client/component/ui_scripting.cpp b/src/client/component/ui_scripting.cpp index 9f84e924..f0a9a553 100644 --- a/src/client/component/ui_scripting.cpp +++ b/src/client/component/ui_scripting.cpp @@ -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> converted_functions; + using converted_func_t = std::function; + std::unordered_map 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(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(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(ptr); + return closure; }