|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <filesystem> |
| 4 | +#include <mutex> |
| 5 | +#include <sstream> |
| 6 | + |
| 7 | +#include <Windows.h> |
| 8 | + |
| 9 | +#include <RED4ext/Common.hpp> |
| 10 | +#include <RED4ext/Detail/Memory.hpp> |
| 11 | + |
| 12 | +template<> |
| 13 | +struct RED4ext::Detail::AddressResolverOverride<uint32_t> : std::true_type |
| 14 | +{ |
| 15 | + inline static uintptr_t Resolve(uint32_t aHash) |
| 16 | + { |
| 17 | + using functionType = void* (*)(uint32_t); |
| 18 | + static functionType resolveFunc = nullptr; |
| 19 | + |
| 20 | + static std::once_flag flag; |
| 21 | + std::call_once(flag, |
| 22 | + []() |
| 23 | + { |
| 24 | + char exePath[4096]; |
| 25 | + GetModuleFileNameA(GetModuleHandle(nullptr), exePath, 4096); |
| 26 | + |
| 27 | + std::filesystem::path exe = exePath; |
| 28 | + |
| 29 | + auto dllName = exe.parent_path() / "version.dll"; |
| 30 | + constexpr auto functionName = "ResolveAddress"; |
| 31 | + |
| 32 | + auto handle = LoadLibraryA(dllName.string().c_str()); |
| 33 | + if (!handle) |
| 34 | + { |
| 35 | + std::stringstream stream; |
| 36 | + stream << "Failed to get '" << dllName |
| 37 | + << "' handle.\nProcess will now close.\n\nLast error: " << GetLastError(); |
| 38 | + |
| 39 | + MessageBoxA(nullptr, stream.str().c_str(), "Cyber Engine Tweaks", MB_ICONERROR | MB_OK); |
| 40 | + TerminateProcess(GetCurrentProcess(), 1); |
| 41 | + return; // Disable stupid warning |
| 42 | + } |
| 43 | + |
| 44 | + resolveFunc = reinterpret_cast<functionType>(GetProcAddress(handle, functionName)); |
| 45 | + if (resolveFunc == nullptr) |
| 46 | + { |
| 47 | + std::stringstream stream; |
| 48 | + stream << "Failed to get '" << functionName |
| 49 | + << "' address.\nProcess will now close.\n\nLast error: " << GetLastError(); |
| 50 | + |
| 51 | + MessageBoxA(nullptr, stream.str().c_str(), "Cyber Engine Tweaks", MB_ICONERROR | MB_OK); |
| 52 | + TerminateProcess(GetCurrentProcess(), 1); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + auto address = resolveFunc(aHash); |
| 57 | + if (address == nullptr) |
| 58 | + { |
| 59 | + std::stringstream stream; |
| 60 | + stream << "Failed to resolve address for hash " << std::hex << std::showbase << aHash << ".\nProcess will now close."; |
| 61 | + |
| 62 | + MessageBoxA(nullptr, stream.str().c_str(), "Cyber Engine Tweaks", MB_ICONERROR | MB_OK); |
| 63 | + TerminateProcess(GetCurrentProcess(), 1); |
| 64 | + } |
| 65 | + |
| 66 | + return reinterpret_cast<uintptr_t>(address); |
| 67 | + } |
| 68 | +}; |
0 commit comments