iw4x-client/src/Main.cpp

70 lines
1.6 KiB
C++
Raw Normal View History

2015-12-23 08:45:53 -05:00
#include "STDInclude.hpp"
2016-01-24 07:06:52 -05:00
namespace Main
2015-12-23 08:45:53 -05:00
{
2015-12-23 09:00:01 -05:00
static Utils::Hook EntryPointHook;
2016-01-24 07:06:52 -05:00
void Initialize()
2015-12-23 08:45:53 -05:00
{
2016-01-24 07:44:42 -05:00
Main::EntryPointHook.Uninstall();
2016-02-19 17:57:06 -05:00
2016-06-14 13:49:27 -04:00
Utils::Cryptography::Initialize();
2016-01-24 07:06:52 -05:00
Components::Loader::Initialize();
2016-02-19 17:57:06 -05:00
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
if (Components::Loader::PerformingUnitTests())
{
DWORD result = (Components::Loader::PerformUnitTests() ? 0 : -1);
Components::Loader::Uninitialize();
2016-02-05 19:01:33 -05:00
ExitProcess(result);
}
#else
if (Components::Flags::HasFlag("tests"))
{
Components::Logger::Print("Unit tests are disabled outside the debug environment!\n");
}
#endif
2015-12-23 08:45:53 -05:00
}
2016-01-24 07:06:52 -05:00
void Uninitialize()
2015-12-23 08:45:53 -05:00
{
Components::Loader::Uninitialize();
}
2016-01-24 07:44:42 -05:00
}
2015-12-23 08:45:53 -05:00
2016-01-24 07:44:42 -05:00
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
2016-01-24 07:06:52 -05:00
{
2016-01-25 19:41:21 -05:00
// Ensure we're working with our desired binary
2016-01-26 10:12:41 -05:00
if (Utils::Hook::Get<DWORD>(0x4C0FFF) != 0x6824748B)
2016-01-25 19:41:21 -05:00
{
return FALSE;
}
2016-01-24 07:44:42 -05:00
DWORD oldProtect;
2016-06-19 11:40:30 -04:00
std::uint8_t* module = reinterpret_cast<std::uint8_t*>(GetModuleHandle(NULL));
2016-02-29 09:49:27 -05:00
VirtualProtect(module, 0x6C73000, PAGE_EXECUTE_READWRITE, &oldProtect); // Unprotect the entire process
VirtualProtect(module + 0x1000, 0x2D6000, PAGE_EXECUTE_READ, &oldProtect); // Protect the .text segment
2016-01-24 07:06:52 -05:00
2016-01-24 07:44:42 -05:00
Main::EntryPointHook.Initialize(0x6BAC0F, [] ()
2016-01-24 07:06:52 -05:00
{
2016-01-24 07:44:42 -05:00
__asm
{
2016-02-18 12:04:47 -05:00
// This has to be called, otherwise the hook is not uninstalled and we're looping into infinity
2016-01-24 07:44:42 -05:00
call Main::Initialize
2016-01-24 07:54:36 -05:00
2016-01-24 07:44:42 -05:00
mov eax, 6BAC0Fh
jmp eax
}
2016-01-24 07:06:52 -05:00
2016-01-24 07:44:42 -05:00
})->Install();
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
Main::Uninitialize();
2016-01-24 07:06:52 -05:00
}
2016-01-24 07:44:42 -05:00
return TRUE;
2015-12-23 08:45:53 -05:00
}