Advertisement
midlow

Untitled

Dec 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.91 KB | None | 0 0
  1. #include <thread>
  2. #include "Hooks.h"
  3. #include "Utils\Utils.h"
  4. #include "Features\Features.h"
  5.  
  6. Misc     g_Misc;
  7. Hooks    g_Hooks;
  8. Settings g_Settings;
  9.  
  10.  
  11. void Hooks::Init()
  12. {
  13.     // Get window handle
  14.     while (!(g_Hooks.hCSGOWindow = FindWindowA("Valve001", nullptr)))
  15.     {
  16.         using namespace std::literals::chrono_literals;
  17.         std::this_thread::sleep_for(50ms);
  18.     }
  19.  
  20.     interfaces::Init();                         // Get interfaces
  21.     g_pNetvars = std::make_unique<NetvarTree>();// Get netvars after getting interfaces as we use them
  22.  
  23.     Utils::Log("Hooking in progress...");
  24.  
  25.  
  26.     // D3D Device pointer
  27.     const uintptr_t d3dDevice = **reinterpret_cast<uintptr_t**>(Utils::FindSignature("shaderapidx9.dll", "A1 ? ? ? ? 50 8B 08 FF 51 0C") + 1);
  28.  
  29.     if (g_Hooks.hCSGOWindow)        // Hook WNDProc to capture mouse / keyboard input
  30.         g_Hooks.pOriginalWNDProc = reinterpret_cast<WNDPROC>(SetWindowLongPtr(g_Hooks.hCSGOWindow, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(Hooks::WndProc)));
  31.  
  32.  
  33.     // VMTHooks
  34.     g_Hooks.pD3DDevice9Hook = std::make_unique<VMTHook>(reinterpret_cast<void*>(d3dDevice));
  35.     g_Hooks.pClientModeHook = std::make_unique<VMTHook>(g_pClientMode);
  36.     g_Hooks.pSurfaceHook    = std::make_unique<VMTHook>(g_pSurface);
  37.  
  38.     // Hook the table functions
  39.     g_Hooks.pD3DDevice9Hook->Hook(vtable_indexes::reset,      Hooks::Reset);
  40.     g_Hooks.pD3DDevice9Hook->Hook(vtable_indexes::present,    Hooks::Present);
  41.     g_Hooks.pClientModeHook->Hook(vtable_indexes::createMove, Hooks::CreateMove);
  42.     g_Hooks.pSurfaceHook   ->Hook(vtable_indexes::lockCursor, Hooks::LockCursor);
  43.  
  44.  
  45.     // Create event listener, no need for it now so it will remain commented.
  46.     //const std::vector<const char*> vecEventNames = { "" };
  47.     //g_Hooks.pEventListener = std::make_unique<EventListener>(vecEventNames);
  48.  
  49.     Utils::Log("Hooking completed!");
  50. }
  51.  
  52.  
  53. void Hooks::Restore()
  54. {
  55.     Utils::Log("Unhooking in progress...");
  56.     {   // Unhook every function we hooked and restore original one
  57.         g_Hooks.pD3DDevice9Hook->Unhook(vtable_indexes::reset);
  58.         g_Hooks.pD3DDevice9Hook->Unhook(vtable_indexes::present);
  59.         g_Hooks.pClientModeHook->Unhook(vtable_indexes::createMove);
  60.         g_Hooks.pSurfaceHook->Unhook(vtable_indexes::lockCursor);
  61.         SetWindowLongPtr(g_Hooks.hCSGOWindow, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_Hooks.pOriginalWNDProc));
  62.  
  63.         g_pNetvars.reset();   /* Need to reset by-hand, global pointer so doesnt go out-of-scope */
  64.     }
  65.     Utils::Log("Unhooking succeded!");
  66.  
  67.     // Destroy fonts and all textures we created
  68.     g_Render.InvalidateDeviceObjects();
  69.     g_Fonts.DeleteDeviceObjects();
  70. }
  71.  
  72.  
  73. bool __fastcall Hooks::CreateMove(IClientMode* thisptr, void* edx, float sample_frametime, CUserCmd* pCmd)
  74. {
  75.     // Call original createmove before we start screwing with it
  76.     static auto oCreateMove = g_Hooks.pClientModeHook->GetOriginal<CreateMove_t>(24);
  77.     oCreateMove(thisptr, edx, sample_frametime, pCmd);
  78.  
  79.     if (!pCmd || !pCmd->command_number)
  80.         return oCreateMove;
  81.  
  82.     // Get globals
  83.     g::pCmd         = pCmd;
  84.     g::pLocalEntity = g_pEntityList->GetClientEntity(g_pEngine->GetLocalPlayer());
  85.     if (!g::pLocalEntity)
  86.         return oCreateMove;
  87.  
  88.  
  89.     g_Misc.OnCreateMove();
  90.     // run shit outside enginepred
  91.  
  92.     engine_prediction::RunEnginePred();
  93.     // run shit in enginepred
  94.     engine_prediction::EndEnginePred();
  95.  
  96.     return false;
  97. }
  98.  
  99.  
  100. void __fastcall Hooks::LockCursor(ISurface* thisptr, void* edx)
  101. {
  102.     static auto oLockCursor = g_Hooks.pSurfaceHook->GetOriginal<LockCursor_t>(vtable_indexes::lockCursor);
  103.  
  104.     if (!g_Settings.bMenuOpened)
  105.         return oLockCursor(thisptr, edx);
  106.  
  107.     g_pSurface->UnlockCursor();
  108. }
  109.  
  110.  
  111. HRESULT __stdcall Hooks::Reset(IDirect3DDevice9* pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters)
  112. {
  113.     static auto oReset = g_Hooks.pD3DDevice9Hook->GetOriginal<Reset_t>(16);
  114.  
  115.     if (g_Hooks.bInitializedDrawManager)
  116.     {
  117.         Utils::Log("Reseting draw manager.");
  118.         g_Render.InvalidateDeviceObjects();
  119.         HRESULT hr = oReset(pDevice, pPresentationParameters);
  120.         g_Render.RestoreDeviceObjects(pDevice);
  121.         Utils::Log("DrawManager reset succeded.");
  122.         return hr;
  123.     }
  124.  
  125.     return oReset(pDevice, pPresentationParameters);
  126. }
  127.  
  128.  
  129. HRESULT __stdcall Hooks::Present(IDirect3DDevice9* pDevice, const RECT* pSourceRect, const RECT* pDestRect,
  130.                                  HWND hDestWindowOverride,  const RGNDATA* pDirtyRegion)
  131. {
  132.     IDirect3DStateBlock9* stateBlock = nullptr;
  133.     pDevice->CreateStateBlock(D3DSBT_PIXELSTATE, &stateBlock);
  134.  
  135.     [pDevice]()
  136.     {
  137.         if (!g_Hooks.bInitializedDrawManager)
  138.         {
  139.             Utils::Log("Initializing Draw manager");
  140.             g_Render.InitDeviceObjects(pDevice);
  141.             g_Hooks.nMenu.Initialize();
  142.             g_Hooks.bInitializedDrawManager = true;
  143.             Utils::Log("Draw manager initialized");
  144.         }
  145.         else
  146.         {
  147.             g_Render.SetupRenderStates(); // Sets up proper render states for our state block
  148.  
  149.             static std::string szWatermark = "Antario";
  150.             g_Render.String(8, 8, CD3DFONT_DROPSHADOW, Color(250, 150, 200, 180), g_Fonts.pFontTahoma8.get(), szWatermark.c_str());
  151.  
  152.             if (g_Settings.bMenuOpened)
  153.             {
  154.                 g_Hooks.nMenu.Render();             // Render our menu
  155.                 g_Hooks.nMenu.mouseCursor->Render();// Render mouse cursor in the end so its not overlapped
  156.             }
  157.  
  158.             // Put your draw calls here
  159.             g_ESP.Render();
  160.         }
  161.     }();
  162.  
  163.     stateBlock->Apply();
  164.     stateBlock->Release();
  165.  
  166.     static auto oPresent = g_Hooks.pD3DDevice9Hook->GetOriginal<Present_t>(17);
  167.     return oPresent(pDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
  168. }
  169.  
  170.  
  171. LRESULT Hooks::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  172. {
  173.     // for now as a lambda, to be transfered somewhere
  174.     // Thanks uc/WasserEsser for pointing out my mistake!
  175.     // Working when you HOLD th button, not when you press it.
  176.     const auto getButtonHeld = [uMsg, wParam](bool& bButton, int vKey)
  177.     {
  178.         if (wParam != vKey) return;
  179.  
  180.         if (uMsg == WM_KEYDOWN)
  181.             bButton = true;
  182.         else if (uMsg == WM_KEYUP)
  183.             bButton = false;
  184.     };
  185.  
  186.     const auto getButtonToggle = [uMsg, wParam](bool& bButton, int vKey)
  187.     {
  188.         if (wParam != vKey) return;
  189.  
  190.         if (uMsg == WM_KEYUP)
  191.             bButton = !bButton;
  192.     };
  193.  
  194.     getButtonToggle(g_Settings.bMenuOpened, VK_INSERT);
  195.  
  196.     if (g_Hooks.bInitializedDrawManager)
  197.     {
  198.         // our wndproc capture fn
  199.         if (g_Settings.bMenuOpened)
  200.         {
  201.             g_Hooks.nMenu.MsgProc(uMsg, wParam, lParam);
  202.             return true;
  203.         }
  204.     }
  205.  
  206.     // Call original wndproc to make game use input again
  207.     return CallWindowProcA(g_Hooks.pOriginalWNDProc, hWnd, uMsg, wParam, lParam);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement