iw4x-client/src/Main.cpp

103 lines
2.4 KiB
C++
Raw Normal View History

2017-01-19 16:23:59 -05:00
#include "STDInclude.hpp"
namespace Main
{
void SetEnvironment()
{
wchar_t exeName[512];
GetModuleFileName(GetModuleHandle(nullptr), exeName, sizeof(exeName) / 2);
2017-01-19 16:23:59 -05:00
wchar_t* exeBaseName = wcsrchr(exeName, L'\\');
exeBaseName[0] = L'\0';
SetCurrentDirectory(exeName);
}
void Initialize()
{
Main::SetEnvironment();
Utils::Cryptography::Initialize();
2017-01-30 15:13:30 -05:00
Components::Loader::Initialize();
2017-01-19 16:23:59 -05:00
2017-01-30 15:13:30 -05:00
#if defined(DEBUG) || defined(FORCE_UNIT_TESTS)
if (Components::Loader::PerformingUnitTests())
2017-01-19 16:23:59 -05:00
{
2017-01-30 15:13:30 -05:00
DWORD result = (Components::Loader::PerformUnitTests() ? 0 : -1);
Components::Loader::Uninitialize();
ExitProcess(result);
2017-01-19 16:23:59 -05:00
}
#else
2017-01-30 15:13:30 -05:00
if (Components::Flags::HasFlag("tests"))
{
Components::Logger::Print("Unit tests are disabled outside the debug environment!\n");
}
2017-01-30 15:13:30 -05:00
#endif
2017-01-19 16:23:59 -05:00
}
void Uninitialize()
{
2017-01-30 15:13:30 -05:00
Components::Loader::Uninitialize();
Utils::Cache::Uninitialize();
google::protobuf::ShutdownProtobufLibrary();
2017-01-19 16:23:59 -05:00
}
2017-05-30 17:34:52 -04:00
__declspec(naked) void EntryPoint()
{
__asm
{
pushad
call Main::Initialize
popad
push 6BAC14h // Continue init routine
push 6CA062h // ___security_init_cookie
retn
}
}
2017-01-19 16:23:59 -05:00
}
BOOL APIENTRY DllMain(HMODULE /*hModule*/, DWORD ul_reason_for_call, LPVOID /*lpReserved*/)
{
if (ul_reason_for_call == DLL_PROCESS_ATTACH)
{
// Not sure if it conflicts with our TLS variables
//DisableThreadLibraryCalls(hModule);
2017-02-16 21:27:38 -05:00
Steam::Proxy::RunMod();
2017-01-19 16:23:59 -05:00
// Ensure we're working with our desired binary
2017-05-30 17:35:56 -04:00
if (Utils::Hook::Get<DWORD>(0x4C0FFF) != 0x6824748B) return FALSE;
2017-01-19 16:23:59 -05:00
2017-06-04 06:21:57 -04:00
#if !defined(DEBUG) && !defined(DISABLE_ANTICHEAT)
[]()
{
2017-03-10 17:18:19 -05:00
Components::AntiCheat::ProtectProcess();
Components::AntiCheat::PatchThreadCreation();
}();
#endif
2017-01-19 16:23:59 -05:00
DWORD oldProtect;
std::uint8_t* module = reinterpret_cast<std::uint8_t*>(GetModuleHandle(nullptr));
2017-01-19 16:23:59 -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
2017-05-30 17:34:52 -04:00
// Install entry point hook
Utils::Hook(0x6BAC0F, Main::EntryPoint, HOOK_JUMP).install()->quick();
2017-01-19 16:23:59 -05:00
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH)
{
Main::Uninitialize();
}
else if(ul_reason_for_call == DLL_THREAD_ATTACH)
{
#if !defined(DEBUG) && !defined(DISABLE_ANTICHEAT)
[]()
{
Components::AntiCheat::VerifyThreadIntegrity();
}();
#endif
}
2017-01-19 16:23:59 -05:00
return TRUE;
}