Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.54 KB | None | 0 0
  1. #include <windows.h>
  2. #include <dwmapi.h>
  3. #include <d3d9.h>
  4. #include <d3dx9.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7.  
  8. #pragma comment(lib,"d3d9.lib")
  9. #pragma comment(lib,"d3dx9.lib")
  10. #pragma comment(lib,"dwmapi.lib")
  11.  
  12. #pragma warning(disable: 4018)
  13. #pragma warning(disable: 4244)
  14.  
  15. #define ARMA_CLIENT 0xDFCDD8
  16. #define ARMA_PLAYERINFO 0xDEEAE8
  17. #define ARMA_TRANSFORMATION 0xE27D94
  18. #define SAFE_RELEASE(x) if(x) { x->Release(); x = NULL; }
  19.  
  20. char g_szRandom[25] = { 0 };
  21.  
  22. IDirect3D9Ex* g_pDirect3D;
  23. IDirect3DDevice9Ex* g_pDirect3DDevice;
  24. ID3DXFont* g_pFont;
  25. ID3DXLine* g_pLine;
  26.  
  27. HWND hWnd;
  28. HWND g_ArmaHWND;
  29. DWORD g_ArmaPID;
  30. HANDLE g_ArmaHANDLE;
  31. float g_fResolution[2] = { 100, 100 };
  32.  
  33. D3DXVECTOR3 InvViewTranslation;
  34. D3DXVECTOR3 InvViewRight;
  35. D3DXVECTOR3 InvViewUp;
  36. D3DXVECTOR3 InvViewForward;
  37. D3DXVECTOR3 ViewPortMatrix;
  38. D3DXVECTOR3 ProjD1;
  39. D3DXVECTOR3 ProjD2;
  40.  
  41. D3DXVECTOR3 g_LocalPos;
  42.  
  43. LRESULT WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  44. void Render();
  45. void RenderGame();
  46. void InitDirectX();
  47. void ShutdownDirectX();
  48. template <class T> T Read(DWORD dwAddress);
  49. D3DXVECTOR3 WorldToScreen(D3DXVECTOR3 in);
  50. void DrawBox(int x, int y, int w, int h, DWORD dwColor);
  51. void DrawText(int x, int y, DWORD dwColor, char* szText, ...);
  52. void DrawTextBorder(int x, int y, DWORD dwColor, char* szText, ...);
  53. static BOOL CALLBACK ArmaEnumFunc(HWND hwnd, LPARAM lParam);
  54.  
  55. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  56. {
  57.     srand((unsigned int)time(NULL));
  58.     for(int i = 0; i < 25; i++)
  59.         g_szRandom[i] = (char)(rand()%255);
  60.  
  61.     CreateMutex(0, false, "GFZFLDLSLDLJFDKL");
  62.     if(GetLastError() == ERROR_ALREADY_EXISTS)
  63.     {
  64.         MessageBox(NULL, "Error: #1", g_szRandom, MB_OK);
  65.         ExitProcess(0);
  66.     }
  67.  
  68.     BOOL bComp = FALSE;
  69.     DwmIsCompositionEnabled(&bComp);
  70.     if(!bComp)
  71.     {
  72.         MessageBox(NULL, "Error: #2", g_szRandom, MB_OK);
  73.         ExitProcess(0);
  74.     }
  75.  
  76.     WNDCLASSEX wc = {
  77.         sizeof(WNDCLASSEX),
  78.         NULL,
  79.         WindowProc,
  80.         NULL,
  81.         NULL,
  82.         hInstance,
  83.         LoadIcon(NULL, IDI_APPLICATION),
  84.         LoadCursor(NULL, IDC_ARROW),
  85.         NULL,
  86.         NULL,
  87.         g_szRandom,
  88.         LoadIcon(NULL, IDI_APPLICATION)
  89.     };
  90.     RegisterClassEx(&wc);
  91.  
  92.     while(!g_ArmaHWND)
  93.     {
  94.         EnumWindows(ArmaEnumFunc, NULL);
  95.         if(g_ArmaHWND)
  96.         {
  97.             GetWindowThreadProcessId(g_ArmaHWND, &g_ArmaPID);
  98.             if(g_ArmaPID != 0)
  99.             {
  100.                 g_ArmaHANDLE = OpenProcess(PROCESS_VM_READ, false, g_ArmaPID);
  101.                 if(g_ArmaHANDLE == NULL)
  102.                 {
  103.                     g_ArmaHWND = NULL;
  104.                     g_ArmaPID = NULL;
  105.                     g_ArmaHANDLE = NULL;
  106.                     continue;
  107.                 }
  108.             }
  109.         }
  110.     }
  111.  
  112.     while(GetForegroundWindow() != g_ArmaHWND)
  113.         Sleep(100);
  114.  
  115.     Sleep(100);
  116.  
  117.     RECT armaClientRect;
  118.     GetClientRect(g_ArmaHWND, &armaClientRect);
  119.     g_fResolution[0] = armaClientRect.right;
  120.     g_fResolution[1] = armaClientRect.bottom;
  121.  
  122.     hWnd = CreateWindowEx(WS_EX_TOPMOST | WS_EX_COMPOSITED | WS_EX_TRANSPARENT | WS_EX_LAYERED, g_szRandom, g_szRandom, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, g_fResolution[0], g_fResolution[1], NULL, NULL, hInstance, NULL);
  123.     SetWindowLong(hWnd, GWL_EXSTYLE, ((int)GetWindowLong(hWnd, GWL_EXSTYLE) | WS_EX_LAYERED | WS_EX_TRANSPARENT));
  124.     SetLayeredWindowAttributes(hWnd, RGB(0, 0, 0), 0, ULW_COLORKEY);
  125.     SetLayeredWindowAttributes(hWnd, 0, 255, LWA_ALPHA);
  126.  
  127.     MARGINS margins = { -1 };
  128.     if(FAILED(DwmExtendFrameIntoClientArea(hWnd, &margins)))
  129.     {
  130.         MessageBox(NULL, "Error: #3", g_szRandom, MB_OK);
  131.         ExitProcess(0);
  132.     }
  133.  
  134.     InitDirectX();
  135.    
  136.     SetWindowPos(g_ArmaHWND, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  137.  
  138.     ShowWindow(hWnd, SW_SHOWDEFAULT);
  139.     UpdateWindow(hWnd);
  140.  
  141.     MSG uMsg;
  142.     while(true)
  143.     {
  144.         if(!g_ArmaHANDLE)
  145.             g_ArmaHANDLE = OpenProcess(PROCESS_VM_READ, false, g_ArmaPID);
  146.  
  147.         if(!g_ArmaHANDLE)
  148.             ExitProcess(0);
  149.  
  150.         if(!g_ArmaHWND)
  151.             ExitProcess(0);
  152.  
  153.         SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  154.        
  155.         RECT armaClientRect;
  156.         RECT armaWindowRect;
  157.  
  158.         GetClientRect(g_ArmaHWND, &armaClientRect);
  159.         GetWindowRect(g_ArmaHWND, &armaWindowRect);
  160.  
  161.         if(g_fResolution[0] != armaClientRect.right && g_fResolution[1] != armaClientRect.bottom)
  162.         {
  163.             MoveWindow(hWnd, armaWindowRect.left + 8, armaWindowRect.top + 30, armaClientRect.right, armaClientRect.bottom, FALSE);
  164.             ShutdownDirectX();
  165.             InitDirectX();
  166.             g_fResolution[0] = armaClientRect.right;
  167.             g_fResolution[1] = armaClientRect.bottom;
  168.         }
  169.         else
  170.             MoveWindow(hWnd, armaWindowRect.left + 8, armaWindowRect.top + 30, g_fResolution[0], g_fResolution[1], FALSE);
  171.  
  172.         // Render
  173.         Render();
  174.  
  175.         while(PeekMessage(&uMsg, NULL, 0, 0, PM_REMOVE))
  176.         {
  177.             if(uMsg.message == WM_QUIT)
  178.                 break;
  179.  
  180.             TranslateMessage(&uMsg);
  181.             DispatchMessage(&uMsg);
  182.         }
  183.     }
  184.     return 1;
  185. }
  186.  
  187. LRESULT WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  188. {
  189.     switch(uMsg)
  190.     {
  191.     case WM_DESTROY:
  192.         {
  193.             PostQuitMessage(0);
  194.             ExitProcess(0);
  195.             return 0;
  196.         }
  197.     }
  198.     return DefWindowProc(hWnd, uMsg, wParam, lParam);
  199. }
  200.  
  201. void Render()
  202. {
  203.     if(g_pDirect3DDevice == NULL)
  204.     {
  205.         MessageBox(NULL, "Error: #8", g_szRandom, MB_OK);
  206.         ExitProcess(0);
  207.     }
  208.     g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, 0, 1.0f, 0);
  209.     if(FAILED(g_pDirect3DDevice->BeginScene()))
  210.     {
  211.         MessageBox(NULL, "Error: #9", g_szRandom, MB_OK);
  212.         ExitProcess(0);
  213.     }
  214.  
  215.     RenderGame();
  216.  
  217.     g_pDirect3DDevice->EndScene();
  218.     g_pDirect3DDevice->PresentEx(NULL, NULL, NULL, NULL, NULL);
  219.  
  220. }
  221.  
  222. void RenderGame()
  223. {
  224.     g_ArmaHANDLE = OpenProcess(PROCESS_VM_READ, false, g_ArmaPID);
  225.     if(g_ArmaHANDLE)
  226.     {
  227.         DWORD dwTransformations = Read<DWORD>(ARMA_TRANSFORMATION);
  228.         DWORD dwObjectTable = Read<DWORD>(ARMA_CLIENT);
  229.         DWORD dwPlayerInfo = Read<DWORD>(ARMA_PLAYERINFO);
  230.         DWORD dwLocalPlayer = Read<DWORD>(Read<DWORD>(dwObjectTable + 0x13A8) + 0x4);
  231.  
  232.         // Update W2S
  233.         {
  234.             DWORD dwTransData = Read<DWORD>(dwTransformations + 0x90);
  235.             InvViewRight = Read<D3DXVECTOR3>(dwTransData + 0x4);
  236.             InvViewUp = Read<D3DXVECTOR3>(dwTransData + 0x10);
  237.             InvViewForward = Read<D3DXVECTOR3>(dwTransData + 0x1C);
  238.             InvViewTranslation = Read<D3DXVECTOR3>(dwTransData + 0x28);
  239.             ViewPortMatrix = Read<D3DXVECTOR3>(dwTransData + 0x54);
  240.             ProjD1 = Read<D3DXVECTOR3>(dwTransData + 0xCC);
  241.             ProjD2 = Read<D3DXVECTOR3>(dwTransData + 0xD8);
  242.         }
  243.  
  244.         // LocalPlayer
  245.         {
  246.             if(dwLocalPlayer)
  247.             {
  248.                 g_LocalPos = Read<D3DXVECTOR3>(Read<DWORD>(dwLocalPlayer + 0x18) + 0x28);
  249.                 DrawTextBorder(10, 22, D3DCOLOR_ARGB(255, 255, 0, 0), "Position: %0.1f/%0.1f", g_LocalPos.x/100, g_LocalPos.z/100);
  250.             }
  251.         }
  252.  
  253.         // Range to Rectile
  254.         {
  255.             float fRange = Read<float>(Read<DWORD>(dwObjectTable + 0x8) + 0x30);
  256.             DrawTextBorder(10, 10, D3DCOLOR_ARGB(255, 255, 0, 0), "Range: %0.fm", fRange);
  257.         }
  258.  
  259.         // Loop Object Table
  260.         {
  261.             DWORD dwObjectTablePtr = Read<DWORD>(dwObjectTable + 0x5FC);
  262.             DWORD dwObjectTableArray = Read<DWORD>(dwObjectTablePtr + 0x0);
  263.             DWORD dwObjectTableSize = Read<DWORD>(dwObjectTablePtr + 0x4);
  264.             DrawTextBorder(10, 34, D3DCOLOR_ARGB(255, 255, 0, 0), "ObjectTableSize: %u", dwObjectTableSize);
  265.  
  266.             for(int i = 0; i < dwObjectTableSize; i++)
  267.             {
  268.                 DWORD dwObjectPtr = Read<DWORD>(dwObjectTableArray + (i * 0x34));
  269.                 DWORD dwEntity = Read<DWORD>(dwObjectPtr + 0x4);
  270.  
  271.                 if(dwEntity == dwLocalPlayer)
  272.                     continue;
  273.  
  274.                 DWORD dwEntity1 = Read<DWORD>(dwEntity + 0x3C);
  275.                 DWORD dwEntity2 = Read<DWORD>(dwEntity1 + 0x30);
  276.  
  277.                 char szObjectName[32];
  278.                 ReadProcessMemory(g_ArmaHANDLE, (LPCVOID)(dwEntity2 + 0x8), &szObjectName, 32, 0);
  279.  
  280.                 char szObjectType[25];
  281.                 ReadProcessMemory(g_ArmaHANDLE, (LPCVOID)(Read<DWORD>(dwEntity1 + 0x6C) + 0x8), &szObjectType, 25, 0);
  282.  
  283.                 D3DXVECTOR3 pos = Read<D3DXVECTOR3>(Read<DWORD>(dwEntity + 0x18) + 0x28);
  284.            
  285.                 float dx = (g_LocalPos.x - pos.x);
  286.                 float dy = (g_LocalPos.y - pos.y);
  287.                 float dz = (g_LocalPos.z - pos.z);
  288.  
  289.                 float dist = sqrt((dx*dx) + (dy*dy) + (dz*dz));
  290.                 if(dist >= 2000.0f)
  291.                     continue;
  292.  
  293.                 pos = WorldToScreen(pos);
  294.                 if(pos.z > 0.01)
  295.                 {
  296.                     if(!strcmp(szObjectType, "soldier"))
  297.                     {
  298.                         char szWeaponName[25] = { 0 };
  299.                         DWORD dwWeaponID = Read<DWORD>(dwEntity + 0x6E0);
  300.                         if(dwWeaponID != -1)
  301.                         {
  302.                             DWORD dwWeaponNameAddress_a = Read<DWORD>(dwEntity + 0x694);
  303.                             DWORD dwWeaponNameAddress_b = Read<DWORD>(dwWeaponNameAddress_a + (0x24 * dwWeaponID + 8)) ;
  304.                             DWORD dwWeaponNameAddress_c = Read<DWORD>(dwWeaponNameAddress_b + 0x10);
  305.                             DWORD dwWeaponNameAddress = Read<DWORD>(dwWeaponNameAddress_c + 0x4);
  306.                             ReadProcessMemory(g_ArmaHANDLE, (LPCVOID)(dwWeaponNameAddress + 0x8), &szWeaponName, 25, 0);
  307.                         }
  308.  
  309.                         if(Read<BYTE>(dwEntity + 0x20C) != 1)
  310.                         {
  311.                             DrawTextBorder(pos.x, pos.y, D3DCOLOR_ARGB(255, 0, 200, 0), "%s[%0.fm]\n%s", szObjectName, dist, szWeaponName);
  312.                         }
  313.                     }
  314.                     else if(!strcmp(szObjectType, "car") || !strcmp(szObjectType, "helicopter") || !strcmp(szObjectType, "motorcycle") || !strcmp(szObjectType, "ship") || !strcmp(szObjectType, "airplane"))
  315.                     {
  316.                         DWORD dwPartsAmount = Read<DWORD>(dwEntity + 0xC4);
  317.                         DWORD dwParts = Read<DWORD>(dwEntity + 0xC0);
  318.  
  319.                         float fHealth = 0.0f;
  320.                         for(int i = 0; i < dwPartsAmount; i++)
  321.                         {
  322.                             fHealth += Read<float>(dwParts + i * 0x4) * 100;
  323.                         }
  324.                         fHealth = 100.0 - (fHealth / dwPartsAmount);
  325.  
  326.                         DrawTextBorder(pos.x, pos.y, D3DCOLOR_ARGB(255, 0, 0, 255), "%s[%0.fm]\n%0.1f", szObjectName, dist, fHealth);
  327.                     }
  328.                 }
  329.             }
  330.         }
  331.     }
  332. }
  333.  
  334. void InitDirectX()
  335. {
  336.     if(FAILED(Direct3DCreate9Ex(D3D_SDK_VERSION, &g_pDirect3D)))
  337.     {
  338.         MessageBox(NULL, "Error: #4", g_szRandom, MB_OK);
  339.         ExitProcess(0);
  340.     }
  341.  
  342.     D3DPRESENT_PARAMETERS pp = { 0 };
  343.     pp.Windowed = TRUE;
  344.     pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  345.     pp.hDeviceWindow = hWnd;
  346.     pp.BackBufferFormat = D3DFMT_A8R8G8B8;
  347.     pp.BackBufferWidth = g_fResolution[0];
  348.     pp.BackBufferHeight = g_fResolution[1];
  349.     pp.EnableAutoDepthStencil = TRUE;
  350.     pp.AutoDepthStencilFormat = D3DFMT_D16;
  351.  
  352.     if(FAILED(g_pDirect3D->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, NULL, &g_pDirect3DDevice)))
  353.     {
  354.         MessageBox(NULL, "Error: #5", g_szRandom, MB_OK);
  355.         ExitProcess(0);
  356.     }
  357.  
  358.     if(FAILED(D3DXCreateFont(g_pDirect3DDevice, 12, 0, FW_NORMAL, 1, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &g_pFont)))
  359.     {
  360.         MessageBox(NULL, "Error: #6", g_szRandom, MB_OK);
  361.         ExitProcess(0);
  362.     }
  363.  
  364.     if(FAILED(D3DXCreateLine(g_pDirect3DDevice, &g_pLine)))
  365.     {
  366.         MessageBox(NULL, "Error: #7", g_szRandom, MB_OK);
  367.         ExitProcess(0);
  368.     }
  369.     g_pLine->SetWidth(1);
  370.     g_pLine->SetPattern(0xFFFFFFFF);
  371.     g_pLine->SetAntialias(FALSE);
  372.  
  373. }
  374.  
  375. void ShutdownDirectX()
  376. {
  377.     SAFE_RELEASE(g_pLine)
  378.     SAFE_RELEASE(g_pFont)
  379.     SAFE_RELEASE(g_pDirect3DDevice)
  380.     SAFE_RELEASE(g_pDirect3D)
  381. }
  382.  
  383. template <class T> T Read(DWORD dwAddress)
  384. {
  385.     T t;
  386.     ReadProcessMemory(g_ArmaHANDLE, (LPCVOID)dwAddress, &t, sizeof(t), 0);
  387.     return t;
  388. }
  389.  
  390. D3DXVECTOR3 WorldToScreen(D3DXVECTOR3 in)
  391. {
  392.     D3DXVECTOR3 out, temp;
  393.  
  394.     D3DXVec3Subtract(&temp, &in, &InvViewTranslation);
  395.     float x = D3DXVec3Dot(&temp, &InvViewRight);
  396.     float y = D3DXVec3Dot(&temp, &InvViewUp);
  397.     float z = D3DXVec3Dot(&temp, &InvViewForward);
  398.  
  399.     out.x = ViewPortMatrix.x * (1 + (x / ProjD1.x / z));
  400.     out.y = ViewPortMatrix.y * (1 - (y / ProjD2.y / z));
  401.     out.z = z;
  402.  
  403.     return out;
  404. }
  405.  
  406. void DrawBox(int x, int y, int w, int h, DWORD dwColor)
  407. {
  408.     D3DXVECTOR2 lines[5];
  409.     lines[0] = D3DXVECTOR2(x, y);
  410.     lines[1] = D3DXVECTOR2(x+w, y);
  411.     lines[2] = D3DXVECTOR2(x+w, y+h);
  412.     lines[3] = D3DXVECTOR2(x, y+h);
  413.     lines[4] = D3DXVECTOR2(x, y);
  414.     g_pLine->Draw(lines, 5, dwColor);
  415. }
  416.  
  417. void DrawText(int x, int y, DWORD dwColor, char* szText, ...)
  418. {
  419.     RECT rect = { x, y, x + 800, y + 800 };
  420.     char buffer[MAX_PATH] = { 0 };
  421.     va_list va_alist;
  422.     va_start(va_alist, szText);
  423.     vsprintf_s(buffer, szText, va_alist);
  424.     va_end(va_alist);
  425.  
  426.     g_pFont->DrawText(NULL, buffer, -1, &rect, DT_NOCLIP, dwColor);
  427. }
  428.  
  429. void DrawTextBorder(int x, int y, DWORD dwColor, char* szText, ...)
  430. {
  431.  
  432.     char buffer[MAX_PATH] = { 0 };
  433.     va_list va_alist;
  434.     va_start(va_alist, szText);
  435.     vsprintf_s(buffer, szText, va_alist);
  436.     va_end(va_alist);
  437.  
  438.     DrawText(x, y - 1, 0xFF000000, buffer);
  439.     DrawText(x, y + 1, 0xFF000000, buffer);
  440.     DrawText(x - 1, y, 0xFF000000, buffer);
  441.     DrawText(x + 1, y, 0xFF000000, buffer);
  442.  
  443.     DrawText(x, y, dwColor, buffer);
  444. }
  445.  
  446. static BOOL CALLBACK ArmaEnumFunc(HWND hwnd, LPARAM lParam)
  447. {
  448.     char title[MAX_PATH];
  449.     GetWindowText(hwnd, title, MAX_PATH);
  450.  
  451.     if(!strncmp(title, "ArmA 2 OA", strlen("ArmA 2 OA")))
  452.     {
  453.         g_ArmaHWND = hwnd;
  454.         return FALSE;
  455.     }
  456.     return TRUE;
  457. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement