From 170101646560d0b6efb6bda73128569d9152087c Mon Sep 17 00:00:00 2001 From: psiberx Date: Mon, 27 May 2024 00:35:17 +0300 Subject: [PATCH] Support latest SDK --- src/reverse/Relocation.h | 68 ++++++++++++++++++++++++++++++ src/scripting/FunctionOverride.cpp | 3 +- src/stdafx.h | 2 + xmake.lua | 5 +-- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 src/reverse/Relocation.h diff --git a/src/reverse/Relocation.h b/src/reverse/Relocation.h new file mode 100644 index 00000000..865d8bac --- /dev/null +++ b/src/reverse/Relocation.h @@ -0,0 +1,68 @@ +#pragma once + +#include +#include +#include + +#include + +#include +#include + +template<> +struct RED4ext::Detail::AddressResolverOverride : std::true_type +{ + inline static uintptr_t Resolve(uint32_t aHash) + { + using functionType = void* (*)(uint32_t); + static functionType resolveFunc = nullptr; + + static std::once_flag flag; + std::call_once(flag, + []() + { + char exePath[4096]; + GetModuleFileNameA(GetModuleHandle(nullptr), exePath, 4096); + + std::filesystem::path exe = exePath; + + auto dllName = exe.parent_path() / "version.dll"; + constexpr auto functionName = "ResolveAddress"; + + auto handle = LoadLibraryA(dllName.string().c_str()); + if (!handle) + { + std::stringstream stream; + stream << "Failed to get '" << dllName + << "' handle.\nProcess will now close.\n\nLast error: " << GetLastError(); + + MessageBoxA(nullptr, stream.str().c_str(), "Cyber Engine Tweaks", MB_ICONERROR | MB_OK); + TerminateProcess(GetCurrentProcess(), 1); + return; // Disable stupid warning + } + + resolveFunc = reinterpret_cast(GetProcAddress(handle, functionName)); + if (resolveFunc == nullptr) + { + std::stringstream stream; + stream << "Failed to get '" << functionName + << "' address.\nProcess will now close.\n\nLast error: " << GetLastError(); + + MessageBoxA(nullptr, stream.str().c_str(), "Cyber Engine Tweaks", MB_ICONERROR | MB_OK); + TerminateProcess(GetCurrentProcess(), 1); + } + }); + + auto address = resolveFunc(aHash); + if (address == nullptr) + { + std::stringstream stream; + stream << "Failed to resolve address for hash " << std::hex << std::showbase << aHash << ".\nProcess will now close."; + + MessageBoxA(nullptr, stream.str().c_str(), "Cyber Engine Tweaks", MB_ICONERROR | MB_OK); + TerminateProcess(GetCurrentProcess(), 1); + } + + return reinterpret_cast(address); + } +}; diff --git a/src/scripting/FunctionOverride.cpp b/src/scripting/FunctionOverride.cpp index a7a30a7e..2716d4ab 100644 --- a/src/scripting/FunctionOverride.cpp +++ b/src/scripting/FunctionOverride.cpp @@ -700,10 +700,11 @@ void FunctionOverride::CopyFunctionDescription(RED4ext::CBaseFunction* aFunc, RE } aFunc->unk20 = aRealFunc->unk20; - aFunc->bytecode = aRealFunc->bytecode; aFunc->unk48 = aRealFunc->unk48; aFunc->unkAC = aRealFunc->unkAC; aFunc->flags = aRealFunc->flags; aFunc->flags.isNative = aForceNative; + + std::memcpy(&aFunc->bytecode, &aRealFunc->bytecode, sizeof(aRealFunc->bytecode)); } diff --git a/src/stdafx.h b/src/stdafx.h index 641656f8..2add06f0 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -1,5 +1,7 @@ #pragma once +#include "reverse/Relocation.h" + #include #include #include diff --git a/xmake.lua b/xmake.lua index 77f41140..bf9c7b7f 100644 --- a/xmake.lua +++ b/xmake.lua @@ -7,7 +7,7 @@ add_rules("mode.debug","mode.releasedbg", "mode.release") add_rules("c.unity_build") add_cxflags("/bigobj", "/MP", "/EHsc") -add_defines("RED4EXT_STATIC_LIB", "UNICODE", "_UNICODE", "_CRT_SECURE_NO_WARNINGS") +add_defines("UNICODE", "_UNICODE", "_CRT_SECURE_NO_WARNINGS") local vsRuntime = "MD" @@ -53,13 +53,12 @@ add_requires("stb") add_requires("sol2", { configs = { includes_lua = false } }) add_requires("openrestry-luajit", { configs = { gc64 = true } }) -local imguiUserConfig = path.absolute("src/imgui_impl/imgui_user_config.h") +local imguiUserConfig = string.gsub(path.absolute("src/imgui_impl/imgui_user_config.h"), "\\", "/") add_requires("imgui v1.88-docking", { configs = { wchar32 = true, freetype = true, user_config = imguiUserConfig } }) target("RED4ext.SDK") set_kind("static") set_group("vendor") - add_files("vendor/RED4ext.SDK/src/**.cpp") add_headerfiles("vendor/RED4ext.SDK/include/**.hpp") add_includedirs("vendor/RED4ext.SDK/include/", { public = true }) on_install(function() end)