Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=====================================================================================
- typedef HRESULT(WINAPI* CreateDevice_Prototype) (LPDIRECT3D9, UINT, D3DDEVTYPE, HWND, DWORD, D3DPRESENT_PARAMETERS*, LPDIRECT3DDEVICE9*);
- typedef HRESULT(WINAPI* tEndScene) (LPDIRECT3DDEVICE9);
- typedef HRESULT(WINAPI* Reset_Prototype) (LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
- CreateDevice_Prototype CreateDevice_Pointer = nullptr;
- tEndScene oEndScene = nullptr;
- Reset_Prototype Reset_Pointer = nullptr;
- HRESULT WINAPI Direct3DCreate9_VMTable(VOID);
- HRESULT WINAPI CreateDevice_Detour(LPDIRECT3D9, UINT, D3DDEVTYPE, HWND, DWORD, D3DPRESENT_PARAMETERS*, LPDIRECT3DDEVICE9*);
- HRESULT WINAPI hkEndScene(LPDIRECT3DDEVICE9);
- HRESULT WINAPI Reset_Detour(LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
- DWORD WINAPI VirtualMethodTableRepatchingLoopToCounterExtensionRepatching(LPVOID);
- PDWORD Direct3D_VMTable = nullptr;
- #pragma region VMTable
- PBYTE HookVTableFunction(PDWORD* dwVTable, PBYTE dwHook, INT Index)
- {
- DWORD dwOld = 0;
- VirtualProtect((void*)((*dwVTable) + (Index * 4)), 4, PAGE_EXECUTE_READWRITE, &dwOld);
- PBYTE pOrig = ((PBYTE)(*dwVTable)[Index]);
- (*dwVTable)[Index] = (DWORD)dwHook;
- VirtualProtect((void*)((*dwVTable) + (Index * 4)), 4, dwOld, &dwOld);
- return pOrig;
- }
- decltype(&Direct3DCreate9) fnCreateDirect3d9 = NULL;
- LRESULT CALLBACK MsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); }
- BOOLEAN InitializeOverlay()
- {
- HcPathLogNormal("Overlay init thread is up.");
- while (!HcModuleHandleA("d3d9.dll"))
- {
- Sleep(250);
- }
- HcPathLogNormal("d3d9.dll located.");
- WNDCLASSEXA wc =
- {
- sizeof(WNDCLASSEXA),
- CS_CLASSDC,
- MsgProc,
- 0L,
- 0L,
- HcModuleHandleA(NULL),
- NULL,
- NULL,
- NULL,
- NULL,
- "Window",
- NULL
- };
- if (!RegisterClassExA(&wc))
- {
- HcPathLogNormal("Failed registering the class. Possible existing module injected.");
- return FALSE;
- }
- HWND hWnd = CreateWindowA("Window",
- NULL,
- WS_OVERLAPPEDWINDOW,
- 100,
- 100,
- 300,
- 300,
- GetDesktopWindow(),
- NULL,
- wc.hInstance,
- NULL);
- if (!hWnd)
- {
- HcPathLogNormal("Failed creating the window.");
- return FALSE;
- }
- fnCreateDirect3d9 = (decltype(&Direct3DCreate9))HcModuleProcedureAddressA(HcModuleHandleA("d3d9.dll"), "Direct3DCreate9");
- if (!fnCreateDirect3d9)
- {
- HcPathLogNormal("Failed locating Direct3DCreate9");
- return FALSE;
- }
- LPDIRECT3D9 pD3D = fnCreateDirect3d9(D3D_SDK_VERSION);
- if (!pD3D)
- {
- DestroyWindow(hWnd);
- HcPathLogNormal("Failed creating the %d version SDK", D3D_SDK_VERSION);
- return FALSE;
- }
- HcPathLogNormal("Created window and pd3d");
- D3DPRESENT_PARAMETERS d3dpp;
- ZeroMemory(&d3dpp, sizeof(d3dpp));
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
- LPDIRECT3DDEVICE9 pd3dDevice;
- pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice);
- Direct3D_VMTable = (PDWORD)*(PDWORD)pd3dDevice;
- *(PDWORD)&oEndScene = (DWORD)Direct3D_VMTable[42];
- *(PDWORD)&Reset_Pointer = (DWORD)Direct3D_VMTable[16];
- if (HcProcessCreateThread(NtCurrentProcess, VirtualMethodTableRepatchingLoopToCounterExtensionRepatching, NULL, 0) == nullptr)
- {
- HcPathLogNormal("CreateDevice_Detour(): CreateThread() failed, error code: %d", GetLastError());
- DestroyWindow(hWnd);
- return FALSE;
- }
- HcPathLogNormal("VMT Thread is up.");
- DestroyWindow(hWnd);
- return TRUE;
- }
- #pragma region Reset
- LPD3DXFONT Self_Font;
- HRESULT WINAPI Reset_Detour(LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* PresentationParameters)
- {
- if (Self_Font != nullptr)
- {
- Self_Font->OnLostDevice();
- }
- if (overlay::MasterOverlayControl->GetFont() != nullptr)
- {
- overlay::MasterOverlayControl->GetFont()->OnLostDevice();
- }
- HRESULT return_value = Reset_Pointer(pDevice, PresentationParameters);
- if (return_value == D3D_OK)
- {
- if (Self_Font != nullptr)
- {
- Self_Font->OnResetDevice();
- }
- if (overlay::MasterOverlayControl->GetFont() != nullptr)
- {
- overlay::MasterOverlayControl->GetFont()->OnResetDevice();
- }
- }
- return return_value;
- }
- #pragma endregion
- #pragma region VirtualMethodTableRepatchingLoopToCounterExtensionRepatching
- DetourContext ctxEndScene;
- DWORD WINAPI VirtualMethodTableRepatchingLoopToCounterExtensionRepatching(LPVOID Param)
- {
- UNREFERENCED_PARAMETER(Param);
- ctxEndScene.lpSource = (LPVOID)Direct3D_VMTable[42];
- ctxEndScene.lpDestination = hkEndScene;
- if (HcHookDetour(&ctxEndScene) != HOOK_NO_ERR)
- {
- HcPathLogNormal("Failed hooking endscene.");
- return 0;
- }
- oEndScene = (tEndScene) ctxEndScene.pbReconstructed;
- HcPathLogNormal("EndScene is hooked.");
- while (!overlay::MasterOverlayControl->GetDevice())
- {
- Sleep(50);
- }
- HcPathLogNormal("Device is found.");
- if (HcHookDetourContextRestore(&ctxEndScene) != HOOK_NO_ERR)
- {
- HcPathLogNormal("Failed restoring endscene.");
- return 0;
- }
- HcPathLogNormal("Endscene is restored.");
- /* Repatch the function table, just in case of a change. */
- while (true)
- {
- HookVTableFunction((PDWORD*)overlay::MasterOverlayControl->GetDevice(),
- (PBYTE)hkEndScene, 42);
- HookVTableFunction((PDWORD*)overlay::MasterOverlayControl->GetDevice(),
- (PBYTE)Reset_Detour, 16);
- Sleep(100);
- }
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement