diff --git a/include/RED4ext/Relocation-inl.hpp b/include/RED4ext/Relocation-inl.hpp new file mode 100644 index 000000000..83d85e769 --- /dev/null +++ b/include/RED4ext/Relocation-inl.hpp @@ -0,0 +1,67 @@ +#pragma once + +#ifdef RED4EXT_STATIC_LIB +#include +#endif + +#include +#include + +#include + +#include + +RED4EXT_INLINE uintptr_t RED4ext::RelocBase::GetImageBase() +{ + static const auto base = reinterpret_cast(GetModuleHandle(nullptr)); + return base; +} + +RED4EXT_INLINE +uintptr_t RED4ext::UniversalRelocBase::Resolve(UniversalRelocSegment aSegment, uint64_t aHash) +{ + using functionType = uintptr_t (*)(UniversalRelocSegment, uint64_t); + static functionType resolveFunc = nullptr; + + static std::once_flag flag; + std::call_once(flag, + []() + { + constexpr auto dllName = "RED4ext.dll"; + constexpr auto functionName = "RED4ext_ResolveAddress"; + + auto handle = LoadLibraryA(dllName); + 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(), "RED4ext.SDK", MB_ICONERROR | MB_OK); + TerminateProcess(GetCurrentProcess(), 1); + } + + 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(), "RED4ext.SDK", MB_ICONERROR | MB_OK); + TerminateProcess(GetCurrentProcess(), 1); + } + }); + + auto address = resolveFunc(aSegment, aHash); + if (address == 0) + { + std::stringstream stream; + stream << "Failed to resolve address for hash " << std::hex << aHash << ".\nProcess will now close."; + + MessageBoxA(nullptr, stream.str().c_str(), "RED4ext.SDK", MB_ICONERROR | MB_OK); + TerminateProcess(GetCurrentProcess(), 1); + } + + return address; +} diff --git a/include/RED4ext/Relocation.hpp b/include/RED4ext/Relocation.hpp index 7dc48db43..f9bebd7c6 100644 --- a/include/RED4ext/Relocation.hpp +++ b/include/RED4ext/Relocation.hpp @@ -1,21 +1,13 @@ #pragma once #include -#include -#include - -#include namespace RED4ext { class RelocBase { public: - inline static uintptr_t GetImageBase() - { - static const auto base = reinterpret_cast(GetModuleHandle(nullptr)); - return base; - } + static uintptr_t GetImageBase(); }; /** @@ -98,53 +90,7 @@ enum class UniversalRelocSegment : uint32_t class UniversalRelocBase { public: - static uintptr_t Resolve(UniversalRelocSegment aSegment, uint64_t aHash) - { - using functionType = uintptr_t (*)(UniversalRelocSegment, uint64_t); - static functionType resolveFunc = nullptr; - - static std::once_flag flag; - std::call_once(flag, - []() - { - constexpr auto dllName = "RED4ext.dll"; - constexpr auto functionName = "RED4ext_ResolveAddress"; - - auto handle = LoadLibraryA(dllName); - 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(), "RED4ext.SDK", MB_ICONERROR | MB_OK); - TerminateProcess(GetCurrentProcess(), 1); - } - - 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(), "RED4ext.SDK", MB_ICONERROR | MB_OK); - TerminateProcess(GetCurrentProcess(), 1); - } - }); - - auto address = resolveFunc(aSegment, aHash); - if (address == 0) - { - std::stringstream stream; - stream << "Failed to resolve address for hash " << std::hex << aHash << ".\nProcess will now close."; - - MessageBoxA(nullptr, stream.str().c_str(), "RED4ext.SDK", MB_ICONERROR | MB_OK); - TerminateProcess(GetCurrentProcess(), 1); - } - - return address; - } + static uintptr_t Resolve(UniversalRelocSegment aSegment, uint64_t aHash); }; /** @@ -218,3 +164,7 @@ class UniversalRelocVtbl : private UniversalRelocBase }; } // namespace RED4ext + +#ifdef RED4EXT_HEADER_ONLY +#include +#endif diff --git a/src/Relocation.cpp b/src/Relocation.cpp new file mode 100644 index 000000000..fcb53cb03 --- /dev/null +++ b/src/Relocation.cpp @@ -0,0 +1,6 @@ +#ifndef RED4EXT_STATIC_LIB +#error Please define 'RED4EXT_STATIC_LIB' to compile this file. +#endif + +#include +