Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1.  
  2.  
  3. //=====================================================================================
  4.  
  5. typedef HRESULT(WINAPI* CreateDevice_Prototype) (LPDIRECT3D9, UINT, D3DDEVTYPE, HWND, DWORD, D3DPRESENT_PARAMETERS*, LPDIRECT3DDEVICE9*);
  6. typedef HRESULT(WINAPI* tEndScene) (LPDIRECT3DDEVICE9);
  7. typedef HRESULT(WINAPI* Reset_Prototype) (LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
  8.  
  9. CreateDevice_Prototype CreateDevice_Pointer = nullptr;
  10. tEndScene oEndScene = nullptr;
  11. Reset_Prototype Reset_Pointer = nullptr;
  12.  
  13. HRESULT WINAPI Direct3DCreate9_VMTable(VOID);
  14. HRESULT WINAPI CreateDevice_Detour(LPDIRECT3D9, UINT, D3DDEVTYPE, HWND, DWORD, D3DPRESENT_PARAMETERS*, LPDIRECT3DDEVICE9*);
  15. HRESULT WINAPI hkEndScene(LPDIRECT3DDEVICE9);
  16. HRESULT WINAPI Reset_Detour(LPDIRECT3DDEVICE9, D3DPRESENT_PARAMETERS*);
  17.  
  18. DWORD WINAPI VirtualMethodTableRepatchingLoopToCounterExtensionRepatching(LPVOID);
  19. PDWORD Direct3D_VMTable = nullptr;
  20.  
  21. #pragma region VMTable
  22.  
  23. PBYTE HookVTableFunction(PDWORD* dwVTable, PBYTE dwHook, INT Index)
  24. {
  25. DWORD dwOld = 0;
  26. VirtualProtect((void*)((*dwVTable) + (Index * 4)), 4, PAGE_EXECUTE_READWRITE, &dwOld);
  27.  
  28. PBYTE pOrig = ((PBYTE)(*dwVTable)[Index]);
  29. (*dwVTable)[Index] = (DWORD)dwHook;
  30.  
  31. VirtualProtect((void*)((*dwVTable) + (Index * 4)), 4, dwOld, &dwOld);
  32.  
  33. return pOrig;
  34. }
  35.  
  36. decltype(&Direct3DCreate9) fnCreateDirect3d9 = NULL;
  37.  
  38. LRESULT CALLBACK MsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); }
  39. BOOLEAN InitializeOverlay()
  40. {
  41. HcPathLogNormal("Overlay init thread is up.");
  42.  
  43. while (!HcModuleHandleA("d3d9.dll"))
  44. {
  45. Sleep(250);
  46. }
  47.  
  48. HcPathLogNormal("d3d9.dll located.");
  49.  
  50. WNDCLASSEXA wc =
  51. {
  52. sizeof(WNDCLASSEXA),
  53. CS_CLASSDC,
  54. MsgProc,
  55. 0L,
  56. 0L,
  57. HcModuleHandleA(NULL),
  58. NULL,
  59. NULL,
  60. NULL,
  61. NULL,
  62. "Window",
  63. NULL
  64. };
  65.  
  66. if (!RegisterClassExA(&wc))
  67. {
  68. HcPathLogNormal("Failed registering the class. Possible existing module injected.");
  69. return FALSE;
  70. }
  71.  
  72. HWND hWnd = CreateWindowA("Window",
  73. NULL,
  74. WS_OVERLAPPEDWINDOW,
  75. 100,
  76. 100,
  77. 300,
  78. 300,
  79. GetDesktopWindow(),
  80. NULL,
  81. wc.hInstance,
  82. NULL);
  83.  
  84. if (!hWnd)
  85. {
  86. HcPathLogNormal("Failed creating the window.");
  87. return FALSE;
  88. }
  89.  
  90. fnCreateDirect3d9 = (decltype(&Direct3DCreate9))HcModuleProcedureAddressA(HcModuleHandleA("d3d9.dll"), "Direct3DCreate9");
  91. if (!fnCreateDirect3d9)
  92. {
  93. HcPathLogNormal("Failed locating Direct3DCreate9");
  94. return FALSE;
  95. }
  96.  
  97. LPDIRECT3D9 pD3D = fnCreateDirect3d9(D3D_SDK_VERSION);
  98. if (!pD3D)
  99. {
  100. DestroyWindow(hWnd);
  101. HcPathLogNormal("Failed creating the %d version SDK", D3D_SDK_VERSION);
  102. return FALSE;
  103. }
  104.  
  105. HcPathLogNormal("Created window and pd3d");
  106.  
  107. D3DPRESENT_PARAMETERS d3dpp;
  108.  
  109. ZeroMemory(&d3dpp, sizeof(d3dpp));
  110. d3dpp.Windowed = TRUE;
  111. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  112. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  113.  
  114. LPDIRECT3DDEVICE9 pd3dDevice;
  115. pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice);
  116.  
  117. Direct3D_VMTable = (PDWORD)*(PDWORD)pd3dDevice;
  118.  
  119. *(PDWORD)&oEndScene = (DWORD)Direct3D_VMTable[42];
  120. *(PDWORD)&Reset_Pointer = (DWORD)Direct3D_VMTable[16];
  121.  
  122. if (HcProcessCreateThread(NtCurrentProcess, VirtualMethodTableRepatchingLoopToCounterExtensionRepatching, NULL, 0) == nullptr)
  123. {
  124. HcPathLogNormal("CreateDevice_Detour(): CreateThread() failed, error code: %d", GetLastError());
  125. DestroyWindow(hWnd);
  126. return FALSE;
  127. }
  128.  
  129. HcPathLogNormal("VMT Thread is up.");
  130.  
  131. DestroyWindow(hWnd);
  132. return TRUE;
  133. }
  134.  
  135. #pragma region Reset
  136.  
  137. LPD3DXFONT Self_Font;
  138. HRESULT WINAPI Reset_Detour(LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* PresentationParameters)
  139. {
  140. if (Self_Font != nullptr)
  141. {
  142. Self_Font->OnLostDevice();
  143. }
  144.  
  145. if (overlay::MasterOverlayControl->GetFont() != nullptr)
  146. {
  147. overlay::MasterOverlayControl->GetFont()->OnLostDevice();
  148. }
  149.  
  150. HRESULT return_value = Reset_Pointer(pDevice, PresentationParameters);
  151.  
  152. if (return_value == D3D_OK)
  153. {
  154.  
  155. if (Self_Font != nullptr)
  156. {
  157. Self_Font->OnResetDevice();
  158. }
  159.  
  160. if (overlay::MasterOverlayControl->GetFont() != nullptr)
  161. {
  162. overlay::MasterOverlayControl->GetFont()->OnResetDevice();
  163. }
  164.  
  165. }
  166.  
  167. return return_value;
  168. }
  169.  
  170. #pragma endregion
  171.  
  172. #pragma region VirtualMethodTableRepatchingLoopToCounterExtensionRepatching
  173.  
  174. DetourContext ctxEndScene;
  175. DWORD WINAPI VirtualMethodTableRepatchingLoopToCounterExtensionRepatching(LPVOID Param)
  176. {
  177. UNREFERENCED_PARAMETER(Param);
  178.  
  179. ctxEndScene.lpSource = (LPVOID)Direct3D_VMTable[42];
  180. ctxEndScene.lpDestination = hkEndScene;
  181.  
  182. if (HcHookDetour(&ctxEndScene) != HOOK_NO_ERR)
  183. {
  184. HcPathLogNormal("Failed hooking endscene.");
  185. return 0;
  186. }
  187.  
  188. oEndScene = (tEndScene) ctxEndScene.pbReconstructed;
  189.  
  190. HcPathLogNormal("EndScene is hooked.");
  191.  
  192. while (!overlay::MasterOverlayControl->GetDevice())
  193. {
  194. Sleep(50);
  195. }
  196.  
  197. HcPathLogNormal("Device is found.");
  198.  
  199. if (HcHookDetourContextRestore(&ctxEndScene) != HOOK_NO_ERR)
  200. {
  201. HcPathLogNormal("Failed restoring endscene.");
  202. return 0;
  203. }
  204.  
  205. HcPathLogNormal("Endscene is restored.");
  206.  
  207. /* Repatch the function table, just in case of a change. */
  208. while (true)
  209. {
  210. HookVTableFunction((PDWORD*)overlay::MasterOverlayControl->GetDevice(),
  211. (PBYTE)hkEndScene, 42);
  212.  
  213. HookVTableFunction((PDWORD*)overlay::MasterOverlayControl->GetDevice(),
  214. (PBYTE)Reset_Detour, 16);
  215.  
  216. Sleep(100);
  217. }
  218. }
  219.  
  220. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement