Advertisement
Guest User

SQ map

a guest
Feb 14th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.63 KB | None | 0 0
  1. // dllmain.cpp : Defines the entry point for the DLL application.
  2. #include "stdafx.h"
  3.  
  4. #define _USE_MATH_DEFINES // for C
  5. #include <math.h>
  6. #include <float.h>
  7. #include <Windows.h>
  8. #include <stdio.h>
  9. #include <iostream>
  10. #include "../ScriptHook/ScriptHookManager.h"
  11.  
  12. /* Contains code posted by Wesser
  13. http://gtaforums.com/topic/569955-gta-sa-rectangular-hud/?p=1062888280
  14. */
  15.  
  16. struct Vec2
  17. {
  18.     Vec2(float x, float y)
  19.     {
  20.         fX = x;
  21.         fY = y;
  22.     }
  23.  
  24.     float fX;
  25.     float fY;
  26. };
  27.  
  28. struct Vector2
  29. {
  30.     Vector2(float x, float y)
  31.     {
  32.         fX = x;
  33.         fY = y;
  34.     }
  35.     Vector2()
  36.     { }
  37.  
  38.     float fX;
  39.     float fY;
  40. };
  41.  
  42. double otherMethod(Vector2 *a1);
  43.  
  44.  
  45. struct SRwV2d
  46. {
  47.     float m_fX;
  48.     float m_fY;
  49. };
  50.  
  51. struct SRwRect
  52. {
  53.     float m_fLeft;
  54.     float m_fTop;
  55.     float m_fRight;
  56.     float m_fBottom;
  57.  
  58.     void SetBounds(float fLeft, float fTop, float fRight, float fBottom)
  59.     {
  60.         m_fLeft = fLeft;
  61.         m_fTop = fTop;
  62.         m_fRight = fRight;
  63.         m_fBottom = fBottom;
  64.     }
  65. };
  66.  
  67. float clampWithinBounds(float fValue, float fMin, float fMax)
  68. {
  69.     return fValue > fMin ? (fValue < fMax ? fValue : fMax) : fMin;
  70. }
  71.  
  72. float isWithinEpsilonAbs(float fValue, float fCheck)
  73. {
  74.     return fabs(fValue - fCheck) < FLT_EPSILON;
  75. }
  76.  
  77. #ifdef METHOD1
  78. bool testPointWithinAARect(SRwV2d *pstPoint, SRwRect *pstRect, SRwV2d *pstClamp, float *pfDist)
  79. {
  80.     if (pstPoint->m_fX < pstRect->m_fLeft
  81.         || pstPoint->m_fY < pstRect->m_fTop
  82.         || pstPoint->m_fX > pstRect->m_fRight
  83.         || pstPoint->m_fY > pstRect->m_fBottom) {
  84.         pstClamp->m_fX = clampWithinBounds(pstPoint->m_fX, pstRect->m_fLeft, pstRect->m_fRight);
  85.         pstClamp->m_fY = clampWithinBounds(pstPoint->m_fY, pstRect->m_fTop, pstRect->m_fBottom);
  86.  
  87.         *pfDist = 1.0f + FLT_EPSILON;
  88.  
  89.         return false;
  90.     }
  91.  
  92.     pstClamp->m_fX = 0.0f;
  93.     pstClamp->m_fY = 0.0f;
  94.  
  95.     *pfDist = 0.0f;
  96.  
  97.     return true;
  98. }
  99. #else /* METHOD 1 */
  100. /* METHOD 2 */
  101. bool testPointWithinAARect(SRwV2d *pstPoint, SRwRect *pstRect, SRwV2d *pstClamp, float *pfDist)
  102. {
  103.     float fPointX, fPointY, fClampX, fClampY, fDistX, fDistY;
  104.  
  105.     fPointX = pstPoint->m_fX;
  106.     fPointY = pstPoint->m_fY;
  107.     fClampX = clampWithinBounds(fPointX, pstRect->m_fLeft, pstRect->m_fRight);
  108.     fClampY = clampWithinBounds(fPointY, pstRect->m_fTop, pstRect->m_fBottom);
  109.  
  110.     if (pstClamp) {
  111.         pstClamp->m_fX = fClampX;
  112.         pstClamp->m_fY = fClampY;
  113.     }
  114.  
  115.     if (pfDist) {
  116.         fDistX = fPointX - fClampX;
  117.         fDistY = fPointY - fClampY;
  118.  
  119.         // Is the point outside the rectangle?
  120.         if (fDistX) {
  121.             // Is the point in one of the outer squared regions in front of vertices?
  122.             if (fDistY) {
  123.                 // Isn't the point oblique relative to one of the vertices?
  124.                 if (!isWithinEpsilonAbs(fDistX, fDistY)) {
  125.                     *pfDist = sqrt((fPointX * fPointX) + (fPointY * fPointY));
  126.                     // The point lies on one of the rectangle (square) diagonals.
  127.                 }
  128.                 else {
  129.                     *pfDist = fabs(fDistX) * (float)M_SQRT2;
  130.                 }
  131.                 // The point is perpendicular to one of the edges on the Y axis.
  132.             }
  133.             else {
  134.                 *pfDist = fabs(fDistX);
  135.             }
  136.             // Is the point still outside the rectangle and perpendicular to one of the edges on the X axis?
  137.         }
  138.         else if (fDistY) {
  139.             *pfDist = fabs(fDistY);
  140.             // The point either lies on the border or is inside the rectangle and thus perpendicular to one of the edges.
  141.         }
  142.         else {
  143.             fPointX = fabs(fPointX);
  144.             fPointY = fabs(fPointY);
  145.             *pfDist = 1.0f - (fPointX < fPointY ? fPointX : fPointY);
  146.  
  147.             return true;
  148.         }
  149.  
  150.         return false;
  151.     }
  152.  
  153.     return fPointX == fClampX && fPointY == fClampY;
  154. }
  155. #endif
  156.  
  157.  
  158. // Converts degrees to radians.
  159. #define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
  160.  
  161. // Converts radians to degrees.
  162. #define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
  163.  
  164. #define MINX 1.0
  165. #define MINY 1.0
  166. #define MAXX 0.0
  167. #define MAXY 0.0
  168.  
  169.  
  170. #define ADD 1
  171. #define DIV 2
  172.  
  173. double otherMethod(Vector2 *a1)
  174. {
  175.     double result;
  176.     double degrees;
  177.  
  178.     result = sqrt(a1->fY * a1->fY + a1->fX * a1->fX);
  179.     if (result > 1.0)
  180.     {
  181.         if (a1->fX > MAXX && a1->fX < MINX && a1->fY > MAXY && a1->fY < MINY)
  182.             return 0.99;
  183.         degrees = radiansToDegrees(atan2(a1->fX, a1->fY));
  184.         if (degrees > 45.0 || degrees <= -45.0)
  185.         {
  186.             if (degrees > 45.0 && degrees <= 135.0)
  187.             {
  188.                 a1->fX = ((cos(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
  189.                 a1->fY = MAXY;
  190.             }
  191.             else if (degrees <= 135.0 && degrees > -135.0)
  192.             {
  193.                 a1->fX = ((cos(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
  194.                 a1->fY = MINY;
  195.             }
  196.             else
  197.             {
  198.                 a1->fX = MINX;
  199.                 a1->fY = ((sin(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
  200.             }
  201.         }
  202.         else
  203.         {
  204.             a1->fX = MAXX;
  205.             a1->fY = ((sin(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
  206.         }
  207.     }
  208.  
  209. #if 0
  210.     result = sqrt(a1->fY * a1->fY + a1->fX * a1->fX);
  211.     if (result > 1.0)
  212.     {
  213.         if (a1->fX > -1.0 && a1->fX < 1.0 && a1->fY > -1.0 && a1->fY < 1.0)
  214.             return 0.99;
  215.         degrees = radiansToDegrees(atan2(a1->fX, a1->fY));
  216.         if (degrees > 45.0 || degrees <= -45.0)
  217.         {
  218.             if (degrees > 45.0 && degrees <= 135.0)
  219.             {
  220.                 a1->fX = cos(degreesToRadians(degrees)) * M_SQRT2;
  221.                 a1->fY = 1.0;
  222.             }
  223.             else if (degrees <= 135.0 && degrees > -135.0)
  224.             {
  225.                 a1->fX = cos(degreesToRadians(degrees)) * M_SQRT2;
  226.                 a1->fY = -1.0;
  227.             }
  228.             else
  229.             {
  230.                 a1->fX = -1.0;
  231.                 a1->fY = sin(degreesToRadians(degrees)) * M_SQRT2;
  232.             }
  233.         }
  234.         else
  235.         {
  236.             a1->fX = 1.0;
  237.             a1->fY = sin(degreesToRadians(degrees)) * M_SQRT2;
  238.         }
  239.     }
  240. #endif
  241.  
  242.     return result;
  243. }
  244.  
  245. float getPointAARectDistance(SRwV2d *pstPoint)
  246. {
  247.     SRwRect stRect;
  248.     SRwV2d stClamp;
  249.     float fDist;
  250.  
  251.     stRect.SetBounds(0.0f /* Top */, 0.0f, 1.0f, 1.0f);
  252.  
  253.     if (!testPointWithinAARect(pstPoint, &stRect, &stClamp, &fDist)) {
  254.         pstPoint->m_fX = stClamp.m_fX;
  255.         pstPoint->m_fY = stClamp.m_fY;
  256.     }
  257.  
  258.     return fDist;
  259. }
  260.  
  261. Vector2 * v12;
  262. Vector2 * pViewportSize = new Vector2();
  263. float center;
  264. int bAbsolut;
  265.  
  266. #define MIN_X 0.025f
  267. #define MAX_X 0.975f
  268. #define MIN_Y 0.025f
  269. #define MAX_Y 0.975f
  270. #define ZERO 0.0f
  271. float fOriginalX;
  272. float fOriginalY;
  273.  
  274. void TestCalc()
  275. {
  276.     Vec2 * vec = new Vec2(0, 0);
  277.     for (float x = -2.0, y = -1.0, n = 0; x <= 2.0 && y <= 2.0; x += 0.1, y += 0.001, n++)
  278.     {
  279.         vec->fX = x;
  280.         vec->fY = y;
  281.         float k = otherMethod((Vector2*)vec);
  282.  
  283.         printf("[%.2f | %.2f ] %.2f %.2f %.2f", x, y, vec->fX, vec->fY, k);
  284.  
  285.         //if((int)n % 15 == 0)
  286.         printf("\n");
  287.     }
  288. }
  289.  
  290. void RenderUIElements()
  291. {
  292. Vector2 * v12;
  293. Vector2 * pViewportSize = new Vector2();
  294. float center;
  295. int bAbsolut;
  296.  
  297. #define MIN_X 0.025f
  298. #define MAX_X 0.975f
  299. #define MIN_Y 0.025f
  300. #define MAX_Y 0.975f
  301. #define ZERO 0.0f
  302. float fOriginalX;
  303. float fOriginalY;
  304. }
  305.  
  306. void _declspec(naked) Keks()
  307. {
  308.     _asm  mov v12, edx;
  309.     _asm  push eax; get absolute flag
  310.     _asm  mov eax, [ebp + 14h]; 3rd parameter
  311.     _asm  mov bAbsolut, eax;
  312.     _asm  pop eax;
  313.  
  314.     fOriginalX = v12->fX;
  315.     fOriginalY = v12->fY;
  316.  
  317.     // Calculate and check our coordinates
  318.     if (!bAbsolut)
  319.     {
  320.         //#define USE_NEW_METHOD
  321. #ifdef USE_NEW_METHOD
  322.         //getPointAARectDistance((SRwV2d*)v12);
  323.         otherMethod(v12);
  324. #else
  325.         if (v12->fX < MAX_X && v12->fX > MIN_X)
  326.         { // Check if the X axis is smaller than 1, but still positive
  327.             if (v12->fY < MAX_Y && v12->fY > MIN_Y) // Check if the Y Acis is smaller than 1, but still positive
  328.             {
  329.                 goto out; // Let GTA calc the position on itself
  330.             }
  331.             else
  332.             {
  333.                 if (v12->fY > MAX_Y) // Check if the Y Axis is bigger than 1 (set it 0.05 so it's not cutted of)
  334.                     v12->fY = MAX_Y;
  335.  
  336.                 if (v12->fY < MIN_Y) // Check if the Y Axis is smaller than 0 (set it +0.05 so it's not cutted of)
  337.                     v12->fY = MIN_Y;
  338.             }
  339.  
  340.             goto out;
  341.         }
  342.         else
  343.         {
  344.             if (v12->fX > MAX_X)
  345.                 v12->fX = MAX_X; // Check if the X Axis is bigger than 1 (set it 0.05 so it's not cutted of)
  346.  
  347.             if (v12->fX < MIN_X)
  348.                 v12->fX = MIN_X; // Check if the X Axis is smaller than 0 (set it +0.05 so it's not cutted of)
  349.         }
  350.  
  351.         if (v12->fY < MAX_Y && v12->fY > MIN_Y)
  352.         { // Check if the Y Acis is smaller than 1, but still positive
  353.             if (v12->fX < MAX_X && v12->fX > MIN_X)// Check if the X axis is smaller than 1, but still positive
  354.             {
  355.                 goto out;
  356.             }
  357.             else
  358.             {
  359.                 if (v12->fX > MAX_X) // Check if the X Axis is bigger than 1 (set it 0.05 so it's not cutted of)
  360.                     v12->fX = MAX_X;
  361.  
  362.                 if (v12->fX < 0) // Check if the X Axis is smaller than 0 (set it +0.05 so it's not cutted of)
  363.                     v12->fX = MIN_X;
  364.             }
  365.  
  366.             goto out;
  367.         }
  368.         else
  369.         {
  370.             if (v12->fY > MAX_Y) // Check if the Y Axis is bigger than 1 (set it 0.05 so it's not cutted of)
  371.                 v12->fY = MAX_Y;
  372.  
  373.             if (v12->fY < MIN_Y) // Check if the Y Axis is smaller than 0 (set it +0.05 so it's not cutted of)
  374.                 v12->fY = MIN_Y;
  375.         }
  376.         goto out;
  377. #endif
  378.     }
  379.  
  380. out:
  381.  
  382.     _asm    mov esp, ebp;
  383.     _asm    pop ebp;
  384.     _asm    retn;
  385. }
  386.  
  387. DWORD GetBase()
  388. {
  389.     static DWORD base = (DWORD)((DWORD)GetModuleHandle(NULL) - 0x400000);
  390.     return base;
  391. }
  392.  
  393. DWORD ResizeMapJmpBack;
  394. int a1, a2, a3;
  395.  
  396. _declspec(naked) signed int ResizeMap()
  397. {
  398.     _asm push eax;
  399.     _asm mov eax, [ebp + 10];
  400.     _asm mov a1, eax;
  401.     _asm mov eax, [ebp + 6];
  402.     _asm mov a2, eax;
  403.     _asm mov eax, [ebp + 0Ch];
  404.     _asm mov a3, eax;
  405.     _asm pushad;
  406.  
  407.     ResizeMapJmpBack = (GetBase() + 0x8364D7);
  408.  
  409.     _asm popad;
  410.     _asm pop eax;
  411.     _asm jmp ResizeMapJmpBack;
  412. }
  413.  
  414. DWORD sub_849BC0;
  415. _declspec(naked) void RenderMap()
  416. {
  417.     _asm  call sub_849BC0;
  418.     _asm  add esp, 4;
  419.     _asm  popad;
  420.  
  421.     /*g_pCore->GetGame()->SetRadarVisible(true);*/
  422.  
  423.     _asm  pushad;
  424.     _asm  retn;
  425. }
  426.  
  427.  
  428. typedef unsigned char BYTE;
  429.  
  430. void PatchAddress(bool bJmp, DWORD dwAddress, DWORD dwNewAddress)
  431. {
  432.     if (bJmp)
  433.         *(BYTE *)(dwAddress) = 0xE9; // jmp
  434.     else
  435.         *(BYTE *)(dwAddress) = 0xE8; // call
  436.  
  437.     *(DWORD *)(dwAddress + 0x1) = (DWORD)dwNewAddress;
  438. }
  439.  
  440.  
  441. #define X86_NOP 0x90
  442. #define X86_RETN 0xC3
  443. #define X86_CALL 0xE8
  444. #define X86_JMP 0xE9
  445.  
  446. struct stProtectionInfo
  447. {
  448.     DWORD dwAddress;
  449.     DWORD dwOldProtection;
  450.     int   iSize;
  451. };
  452.  
  453.  
  454. stProtectionInfo Unprotect(DWORD dwAddress, int iSize)
  455. {
  456.     stProtectionInfo protectionInfo;
  457.     protectionInfo.dwAddress = dwAddress;
  458.     protectionInfo.iSize = iSize;
  459.     DWORD dwOldProtection;
  460.     VirtualProtect((void *)dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection);
  461.     return protectionInfo;
  462. }
  463.  
  464. void Reprotect(stProtectionInfo protectionInfo)
  465. {
  466.     DWORD dwProtection;
  467.     VirtualProtect((void *)protectionInfo.dwAddress, protectionInfo.iSize, protectionInfo.dwOldProtection, &dwProtection);
  468. }
  469.  
  470. void * InstallDetourPatchInternal(DWORD dwAddress, DWORD dwDetourAddress, BYTE byteType, int iSize)
  471. {
  472.     // Allocate the trampoline memory
  473.     BYTE * pbyteTrampoline = (BYTE *)malloc(iSize + 5);
  474.  
  475.     // Unprotect the trampoline memory
  476.     Unprotect((DWORD)pbyteTrampoline, (iSize + 5));
  477.  
  478.     // Unprotect the address memory
  479.     stProtectionInfo protectionInfo = Unprotect(dwAddress, (iSize + 5));
  480.  
  481.     // Copy the overwritten address memory to the trampoline memory
  482.     memcpy(pbyteTrampoline, (void *)dwAddress, iSize);
  483.  
  484.     // Write the type to the trampoline memory
  485.     DWORD dwTrampoline = (DWORD)(pbyteTrampoline + iSize);
  486.     *(BYTE *)dwTrampoline = byteType;
  487.     *(DWORD *)(dwTrampoline + 1) = ((dwAddress + iSize) - dwTrampoline - 5);
  488.  
  489.     // Write the type to the address memory
  490.     *(BYTE *)dwAddress = byteType;
  491.     *(DWORD *)(dwAddress + 1) = (dwDetourAddress - dwAddress - 5);
  492.  
  493.     // Re-protect the address memory
  494.     Reprotect(protectionInfo);
  495.  
  496.     return pbyteTrampoline;
  497. }
  498.  
  499. void InstallJmpPatch(DWORD dwAddress, DWORD dwJmpAddress)
  500. {
  501.     InstallDetourPatchInternal(dwAddress, dwJmpAddress, X86_JMP, 5);
  502. }
  503.  
  504. void InstallCallPatch(DWORD dwAddress, DWORD dwCallAddress)
  505. {
  506.     InstallDetourPatchInternal(dwAddress, dwCallAddress, X86_CALL, 5);
  507. }
  508.  
  509.  
  510. void UnprotectMemory()
  511. {
  512.     BYTE                                                               *pImageBase = (BYTE *)(GetModuleHandle(NULL));
  513.     PIMAGE_DOS_HEADER                                               pDosHeader = (PIMAGE_DOS_HEADER)pImageBase;
  514.     PIMAGE_NT_HEADERS                                               pNtHeader = (PIMAGE_NT_HEADERS)(pImageBase + pDosHeader->e_lfanew);
  515.     PIMAGE_SECTION_HEADER                                   pSection = IMAGE_FIRST_SECTION(pNtHeader);
  516.     char                                                               *pszSectionName;
  517.  
  518.     for (int iSection = 0; iSection < pNtHeader->FileHeader.NumberOfSections; iSection++, pSection++) {
  519.         // Set value for compare
  520.         pszSectionName = (char *)pSection->Name;
  521.  
  522.         // Compare and check if the given memory segment identifier compares with our given segments
  523.         if (!strcmp(pszSectionName, ".text") || !strcmp(pszSectionName, ".rdata"))
  524.             Unprotect((DWORD)(pImageBase + pSection->VirtualAddress), ((pSection->Misc.VirtualSize + 4095) & ~4095));
  525.     }
  526. }
  527.  
  528. void Patch()
  529. {
  530.     // Other section
  531.     UnprotectMemory();
  532.  
  533.     sub_849BC0 = (GetBase() + 0x849BC0);
  534.  
  535.     // Change calc from circle to square(blips)
  536.     *(BYTE*)(GetBase() + 0x8385E7 + 0x6) = 0x1;
  537.     InstallJmpPatch(GetBase() + 0x8386AB, (DWORD)Keks); // E9
  538.  
  539.     // Enable square map(instead of circle map)
  540.     InstallJmpPatch(GetBase() + 0xA22C53, GetBase() + 0xA22EF3); // E9
  541.  
  542.     // Enable big radar
  543.     *(BYTE *)(GetBase() + 0x08364D0 + 0x6) = 0x1;
  544.  
  545.     // Hook resize map function
  546.     InstallJmpPatch(GetBase() + 0x8364D0, (DWORD)ResizeMap); // E9
  547.     *(WORD *)(GetBase() + 0x8364D0 + 0x5) = 0x9090;
  548.  
  549.     // Make blip small
  550.     *(BYTE *)(GetBase() + 0x4B516F + 0x6) = 0x1;
  551.     *(BYTE *)(GetBase() + 0x4B516F + 0x6) = 0x1;
  552.  
  553.     // Hook render map function
  554.     InstallCallPatch((GetBase() + 0xA22E71), (DWORD)RenderMap); // E8
  555. }
  556.  
  557. BOOLEAN WINAPI DllMain(HINSTANCE hDllHandle,
  558.                        DWORD     nReason,
  559.                        LPVOID    Reserved)
  560. {
  561.     //  Perform global initialization.
  562.  
  563.     switch (nReason)
  564.     {
  565.         case DLL_PROCESS_ATTACH:
  566.  
  567.         //  For optimization.
  568.  
  569.         DisableThreadLibraryCalls(hDllHandle);
  570.         Patch(); /* call Patch here */
  571.         break;
  572.  
  573.         case DLL_PROCESS_DETACH:
  574.  
  575.         break;
  576.     }
  577.  
  578.  
  579.     return TRUE;
  580.  
  581. }
  582. //  end DllMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement