Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // dllmain.cpp : Defines the entry point for the DLL application.
- #include "stdafx.h"
- #define _USE_MATH_DEFINES // for C
- #include <math.h>
- #include <float.h>
- #include <Windows.h>
- #include <stdio.h>
- #include <iostream>
- #include "../ScriptHook/ScriptHookManager.h"
- /* Contains code posted by Wesser
- http://gtaforums.com/topic/569955-gta-sa-rectangular-hud/?p=1062888280
- */
- struct Vec2
- {
- Vec2(float x, float y)
- {
- fX = x;
- fY = y;
- }
- float fX;
- float fY;
- };
- struct Vector2
- {
- Vector2(float x, float y)
- {
- fX = x;
- fY = y;
- }
- Vector2()
- { }
- float fX;
- float fY;
- };
- double otherMethod(Vector2 *a1);
- struct SRwV2d
- {
- float m_fX;
- float m_fY;
- };
- struct SRwRect
- {
- float m_fLeft;
- float m_fTop;
- float m_fRight;
- float m_fBottom;
- void SetBounds(float fLeft, float fTop, float fRight, float fBottom)
- {
- m_fLeft = fLeft;
- m_fTop = fTop;
- m_fRight = fRight;
- m_fBottom = fBottom;
- }
- };
- float clampWithinBounds(float fValue, float fMin, float fMax)
- {
- return fValue > fMin ? (fValue < fMax ? fValue : fMax) : fMin;
- }
- float isWithinEpsilonAbs(float fValue, float fCheck)
- {
- return fabs(fValue - fCheck) < FLT_EPSILON;
- }
- #ifdef METHOD1
- bool testPointWithinAARect(SRwV2d *pstPoint, SRwRect *pstRect, SRwV2d *pstClamp, float *pfDist)
- {
- if (pstPoint->m_fX < pstRect->m_fLeft
- || pstPoint->m_fY < pstRect->m_fTop
- || pstPoint->m_fX > pstRect->m_fRight
- || pstPoint->m_fY > pstRect->m_fBottom) {
- pstClamp->m_fX = clampWithinBounds(pstPoint->m_fX, pstRect->m_fLeft, pstRect->m_fRight);
- pstClamp->m_fY = clampWithinBounds(pstPoint->m_fY, pstRect->m_fTop, pstRect->m_fBottom);
- *pfDist = 1.0f + FLT_EPSILON;
- return false;
- }
- pstClamp->m_fX = 0.0f;
- pstClamp->m_fY = 0.0f;
- *pfDist = 0.0f;
- return true;
- }
- #else /* METHOD 1 */
- /* METHOD 2 */
- bool testPointWithinAARect(SRwV2d *pstPoint, SRwRect *pstRect, SRwV2d *pstClamp, float *pfDist)
- {
- float fPointX, fPointY, fClampX, fClampY, fDistX, fDistY;
- fPointX = pstPoint->m_fX;
- fPointY = pstPoint->m_fY;
- fClampX = clampWithinBounds(fPointX, pstRect->m_fLeft, pstRect->m_fRight);
- fClampY = clampWithinBounds(fPointY, pstRect->m_fTop, pstRect->m_fBottom);
- if (pstClamp) {
- pstClamp->m_fX = fClampX;
- pstClamp->m_fY = fClampY;
- }
- if (pfDist) {
- fDistX = fPointX - fClampX;
- fDistY = fPointY - fClampY;
- // Is the point outside the rectangle?
- if (fDistX) {
- // Is the point in one of the outer squared regions in front of vertices?
- if (fDistY) {
- // Isn't the point oblique relative to one of the vertices?
- if (!isWithinEpsilonAbs(fDistX, fDistY)) {
- *pfDist = sqrt((fPointX * fPointX) + (fPointY * fPointY));
- // The point lies on one of the rectangle (square) diagonals.
- }
- else {
- *pfDist = fabs(fDistX) * (float)M_SQRT2;
- }
- // The point is perpendicular to one of the edges on the Y axis.
- }
- else {
- *pfDist = fabs(fDistX);
- }
- // Is the point still outside the rectangle and perpendicular to one of the edges on the X axis?
- }
- else if (fDistY) {
- *pfDist = fabs(fDistY);
- // The point either lies on the border or is inside the rectangle and thus perpendicular to one of the edges.
- }
- else {
- fPointX = fabs(fPointX);
- fPointY = fabs(fPointY);
- *pfDist = 1.0f - (fPointX < fPointY ? fPointX : fPointY);
- return true;
- }
- return false;
- }
- return fPointX == fClampX && fPointY == fClampY;
- }
- #endif
- // Converts degrees to radians.
- #define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
- // Converts radians to degrees.
- #define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
- #define MINX 1.0
- #define MINY 1.0
- #define MAXX 0.0
- #define MAXY 0.0
- #define ADD 1
- #define DIV 2
- double otherMethod(Vector2 *a1)
- {
- double result;
- double degrees;
- result = sqrt(a1->fY * a1->fY + a1->fX * a1->fX);
- if (result > 1.0)
- {
- if (a1->fX > MAXX && a1->fX < MINX && a1->fY > MAXY && a1->fY < MINY)
- return 0.99;
- degrees = radiansToDegrees(atan2(a1->fX, a1->fY));
- if (degrees > 45.0 || degrees <= -45.0)
- {
- if (degrees > 45.0 && degrees <= 135.0)
- {
- a1->fX = ((cos(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
- a1->fY = MAXY;
- }
- else if (degrees <= 135.0 && degrees > -135.0)
- {
- a1->fX = ((cos(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
- a1->fY = MINY;
- }
- else
- {
- a1->fX = MINX;
- a1->fY = ((sin(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
- }
- }
- else
- {
- a1->fX = MAXX;
- a1->fY = ((sin(degreesToRadians(degrees)) * M_SQRT2) + ADD) / DIV;
- }
- }
- #if 0
- result = sqrt(a1->fY * a1->fY + a1->fX * a1->fX);
- if (result > 1.0)
- {
- if (a1->fX > -1.0 && a1->fX < 1.0 && a1->fY > -1.0 && a1->fY < 1.0)
- return 0.99;
- degrees = radiansToDegrees(atan2(a1->fX, a1->fY));
- if (degrees > 45.0 || degrees <= -45.0)
- {
- if (degrees > 45.0 && degrees <= 135.0)
- {
- a1->fX = cos(degreesToRadians(degrees)) * M_SQRT2;
- a1->fY = 1.0;
- }
- else if (degrees <= 135.0 && degrees > -135.0)
- {
- a1->fX = cos(degreesToRadians(degrees)) * M_SQRT2;
- a1->fY = -1.0;
- }
- else
- {
- a1->fX = -1.0;
- a1->fY = sin(degreesToRadians(degrees)) * M_SQRT2;
- }
- }
- else
- {
- a1->fX = 1.0;
- a1->fY = sin(degreesToRadians(degrees)) * M_SQRT2;
- }
- }
- #endif
- return result;
- }
- float getPointAARectDistance(SRwV2d *pstPoint)
- {
- SRwRect stRect;
- SRwV2d stClamp;
- float fDist;
- stRect.SetBounds(0.0f /* Top */, 0.0f, 1.0f, 1.0f);
- if (!testPointWithinAARect(pstPoint, &stRect, &stClamp, &fDist)) {
- pstPoint->m_fX = stClamp.m_fX;
- pstPoint->m_fY = stClamp.m_fY;
- }
- return fDist;
- }
- Vector2 * v12;
- Vector2 * pViewportSize = new Vector2();
- float center;
- int bAbsolut;
- #define MIN_X 0.025f
- #define MAX_X 0.975f
- #define MIN_Y 0.025f
- #define MAX_Y 0.975f
- #define ZERO 0.0f
- float fOriginalX;
- float fOriginalY;
- void TestCalc()
- {
- Vec2 * vec = new Vec2(0, 0);
- for (float x = -2.0, y = -1.0, n = 0; x <= 2.0 && y <= 2.0; x += 0.1, y += 0.001, n++)
- {
- vec->fX = x;
- vec->fY = y;
- float k = otherMethod((Vector2*)vec);
- printf("[%.2f | %.2f ] %.2f %.2f %.2f", x, y, vec->fX, vec->fY, k);
- //if((int)n % 15 == 0)
- printf("\n");
- }
- }
- void RenderUIElements()
- {
- Vector2 * v12;
- Vector2 * pViewportSize = new Vector2();
- float center;
- int bAbsolut;
- #define MIN_X 0.025f
- #define MAX_X 0.975f
- #define MIN_Y 0.025f
- #define MAX_Y 0.975f
- #define ZERO 0.0f
- float fOriginalX;
- float fOriginalY;
- }
- void _declspec(naked) Keks()
- {
- _asm mov v12, edx;
- _asm push eax; get absolute flag
- _asm mov eax, [ebp + 14h]; 3rd parameter
- _asm mov bAbsolut, eax;
- _asm pop eax;
- fOriginalX = v12->fX;
- fOriginalY = v12->fY;
- // Calculate and check our coordinates
- if (!bAbsolut)
- {
- //#define USE_NEW_METHOD
- #ifdef USE_NEW_METHOD
- //getPointAARectDistance((SRwV2d*)v12);
- otherMethod(v12);
- #else
- if (v12->fX < MAX_X && v12->fX > MIN_X)
- { // Check if the X axis is smaller than 1, but still positive
- if (v12->fY < MAX_Y && v12->fY > MIN_Y) // Check if the Y Acis is smaller than 1, but still positive
- {
- goto out; // Let GTA calc the position on itself
- }
- else
- {
- if (v12->fY > MAX_Y) // Check if the Y Axis is bigger than 1 (set it 0.05 so it's not cutted of)
- v12->fY = MAX_Y;
- if (v12->fY < MIN_Y) // Check if the Y Axis is smaller than 0 (set it +0.05 so it's not cutted of)
- v12->fY = MIN_Y;
- }
- goto out;
- }
- else
- {
- if (v12->fX > MAX_X)
- v12->fX = MAX_X; // Check if the X Axis is bigger than 1 (set it 0.05 so it's not cutted of)
- if (v12->fX < MIN_X)
- v12->fX = MIN_X; // Check if the X Axis is smaller than 0 (set it +0.05 so it's not cutted of)
- }
- if (v12->fY < MAX_Y && v12->fY > MIN_Y)
- { // Check if the Y Acis is smaller than 1, but still positive
- if (v12->fX < MAX_X && v12->fX > MIN_X)// Check if the X axis is smaller than 1, but still positive
- {
- goto out;
- }
- else
- {
- if (v12->fX > MAX_X) // Check if the X Axis is bigger than 1 (set it 0.05 so it's not cutted of)
- v12->fX = MAX_X;
- if (v12->fX < 0) // Check if the X Axis is smaller than 0 (set it +0.05 so it's not cutted of)
- v12->fX = MIN_X;
- }
- goto out;
- }
- else
- {
- if (v12->fY > MAX_Y) // Check if the Y Axis is bigger than 1 (set it 0.05 so it's not cutted of)
- v12->fY = MAX_Y;
- if (v12->fY < MIN_Y) // Check if the Y Axis is smaller than 0 (set it +0.05 so it's not cutted of)
- v12->fY = MIN_Y;
- }
- goto out;
- #endif
- }
- out:
- _asm mov esp, ebp;
- _asm pop ebp;
- _asm retn;
- }
- DWORD GetBase()
- {
- static DWORD base = (DWORD)((DWORD)GetModuleHandle(NULL) - 0x400000);
- return base;
- }
- DWORD ResizeMapJmpBack;
- int a1, a2, a3;
- _declspec(naked) signed int ResizeMap()
- {
- _asm push eax;
- _asm mov eax, [ebp + 10];
- _asm mov a1, eax;
- _asm mov eax, [ebp + 6];
- _asm mov a2, eax;
- _asm mov eax, [ebp + 0Ch];
- _asm mov a3, eax;
- _asm pushad;
- ResizeMapJmpBack = (GetBase() + 0x8364D7);
- _asm popad;
- _asm pop eax;
- _asm jmp ResizeMapJmpBack;
- }
- DWORD sub_849BC0;
- _declspec(naked) void RenderMap()
- {
- _asm call sub_849BC0;
- _asm add esp, 4;
- _asm popad;
- /*g_pCore->GetGame()->SetRadarVisible(true);*/
- _asm pushad;
- _asm retn;
- }
- typedef unsigned char BYTE;
- void PatchAddress(bool bJmp, DWORD dwAddress, DWORD dwNewAddress)
- {
- if (bJmp)
- *(BYTE *)(dwAddress) = 0xE9; // jmp
- else
- *(BYTE *)(dwAddress) = 0xE8; // call
- *(DWORD *)(dwAddress + 0x1) = (DWORD)dwNewAddress;
- }
- #define X86_NOP 0x90
- #define X86_RETN 0xC3
- #define X86_CALL 0xE8
- #define X86_JMP 0xE9
- struct stProtectionInfo
- {
- DWORD dwAddress;
- DWORD dwOldProtection;
- int iSize;
- };
- stProtectionInfo Unprotect(DWORD dwAddress, int iSize)
- {
- stProtectionInfo protectionInfo;
- protectionInfo.dwAddress = dwAddress;
- protectionInfo.iSize = iSize;
- DWORD dwOldProtection;
- VirtualProtect((void *)dwAddress, iSize, PAGE_EXECUTE_READWRITE, &dwOldProtection);
- return protectionInfo;
- }
- void Reprotect(stProtectionInfo protectionInfo)
- {
- DWORD dwProtection;
- VirtualProtect((void *)protectionInfo.dwAddress, protectionInfo.iSize, protectionInfo.dwOldProtection, &dwProtection);
- }
- void * InstallDetourPatchInternal(DWORD dwAddress, DWORD dwDetourAddress, BYTE byteType, int iSize)
- {
- // Allocate the trampoline memory
- BYTE * pbyteTrampoline = (BYTE *)malloc(iSize + 5);
- // Unprotect the trampoline memory
- Unprotect((DWORD)pbyteTrampoline, (iSize + 5));
- // Unprotect the address memory
- stProtectionInfo protectionInfo = Unprotect(dwAddress, (iSize + 5));
- // Copy the overwritten address memory to the trampoline memory
- memcpy(pbyteTrampoline, (void *)dwAddress, iSize);
- // Write the type to the trampoline memory
- DWORD dwTrampoline = (DWORD)(pbyteTrampoline + iSize);
- *(BYTE *)dwTrampoline = byteType;
- *(DWORD *)(dwTrampoline + 1) = ((dwAddress + iSize) - dwTrampoline - 5);
- // Write the type to the address memory
- *(BYTE *)dwAddress = byteType;
- *(DWORD *)(dwAddress + 1) = (dwDetourAddress - dwAddress - 5);
- // Re-protect the address memory
- Reprotect(protectionInfo);
- return pbyteTrampoline;
- }
- void InstallJmpPatch(DWORD dwAddress, DWORD dwJmpAddress)
- {
- InstallDetourPatchInternal(dwAddress, dwJmpAddress, X86_JMP, 5);
- }
- void InstallCallPatch(DWORD dwAddress, DWORD dwCallAddress)
- {
- InstallDetourPatchInternal(dwAddress, dwCallAddress, X86_CALL, 5);
- }
- void UnprotectMemory()
- {
- BYTE *pImageBase = (BYTE *)(GetModuleHandle(NULL));
- PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pImageBase;
- PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)(pImageBase + pDosHeader->e_lfanew);
- PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pNtHeader);
- char *pszSectionName;
- for (int iSection = 0; iSection < pNtHeader->FileHeader.NumberOfSections; iSection++, pSection++) {
- // Set value for compare
- pszSectionName = (char *)pSection->Name;
- // Compare and check if the given memory segment identifier compares with our given segments
- if (!strcmp(pszSectionName, ".text") || !strcmp(pszSectionName, ".rdata"))
- Unprotect((DWORD)(pImageBase + pSection->VirtualAddress), ((pSection->Misc.VirtualSize + 4095) & ~4095));
- }
- }
- void Patch()
- {
- // Other section
- UnprotectMemory();
- sub_849BC0 = (GetBase() + 0x849BC0);
- // Change calc from circle to square(blips)
- *(BYTE*)(GetBase() + 0x8385E7 + 0x6) = 0x1;
- InstallJmpPatch(GetBase() + 0x8386AB, (DWORD)Keks); // E9
- // Enable square map(instead of circle map)
- InstallJmpPatch(GetBase() + 0xA22C53, GetBase() + 0xA22EF3); // E9
- // Enable big radar
- *(BYTE *)(GetBase() + 0x08364D0 + 0x6) = 0x1;
- // Hook resize map function
- InstallJmpPatch(GetBase() + 0x8364D0, (DWORD)ResizeMap); // E9
- *(WORD *)(GetBase() + 0x8364D0 + 0x5) = 0x9090;
- // Make blip small
- *(BYTE *)(GetBase() + 0x4B516F + 0x6) = 0x1;
- *(BYTE *)(GetBase() + 0x4B516F + 0x6) = 0x1;
- // Hook render map function
- InstallCallPatch((GetBase() + 0xA22E71), (DWORD)RenderMap); // E8
- }
- BOOLEAN WINAPI DllMain(HINSTANCE hDllHandle,
- DWORD nReason,
- LPVOID Reserved)
- {
- // Perform global initialization.
- switch (nReason)
- {
- case DLL_PROCESS_ATTACH:
- // For optimization.
- DisableThreadLibraryCalls(hDllHandle);
- Patch(); /* call Patch here */
- break;
- case DLL_PROCESS_DETACH:
- break;
- }
- return TRUE;
- }
- // end DllMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement