iw4x-client/src/Main.cpp

86 lines
1.9 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;
void SetEnvironment()
{
wchar_t exeName[512];
GetModuleFileName(GetModuleHandle(NULL), exeName, sizeof(exeName) / 2);
wchar_t* exeBaseName = wcsrchr(exeName, L'\\');
exeBaseName[0] = L'\0';
exeBaseName++;
SetCurrentDirectory(exeName);
}
2016-01-24 07:06:52 -05:00
void Initialize()
2015-12-23 08:45:53 -05:00
{
Main::EntryPointHook.uninstall();
2016-02-19 17:57:06 -05:00
SetEnvironment();
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-08-17 20:18:45 -04:00
BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
2016-01-24 07:44:42 -05:00
{
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-10-30 09:17:49 -04:00
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-10-30 09:17:49 -04:00
//VirtualProtect(module, 0x6C73000, PAGE_EXECUTE_READWRITE, &oldProtect); // Unprotect the entire process
2016-02-29 09:49:27 -05:00
VirtualProtect(module + 0x1000, 0x2D6000, PAGE_EXECUTE_READ, &oldProtect); // Protect the .text segment
2016-01-24 07:06:52 -05:00
2016-10-30 09:17:49 -04: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
})->install();
2016-01-24 07:44:42 -05:00
}
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
}