Advertisement
Dimitri_UA

gta3debug

Jan 9th, 2013
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <tchar.h>
  4.  
  5. #define patch(a, v, s)           _patch((void*)(a), (void*)(v), (s))
  6. #define insertCallWithJump(a, f) _insertCallWithJump((void *)(a), (void *)(f))
  7. #define redirectCall(a, f)       _redirectCall((void *)(a), (void *)(f))
  8.  
  9. void _patch             (void*, void*, int);
  10. void _insertCallWithJump(void*, void*);
  11. void _redirectCall      (void*, void*);
  12. void Debug              (const char *, ...);
  13. void ProcessDebug       (int);
  14.  
  15. #define GAME_TIME     *g_TimeMs
  16. #define SCREEN_WIDTH  *g_ScreenWidth
  17. #define SCREEN_HEIGHT *g_ScreenHeight
  18. #define VERSION       *g_VersionIdentifier
  19.  
  20. #define GTA_3_1_0     0x6B636F52
  21. #define GTA_3_1_1     0x49535049
  22.  
  23. int * g_TimeMs;
  24. int * g_ScreenWidth;
  25. int * g_ScreenHeight;
  26.  
  27. DWORD *g_VersionIdentifier = (DWORD *) 0x60F428;
  28.  
  29. FILE *g_DebugFile;
  30.  
  31. struct RwRGBA     { unsigned char r, g, b, a;                 };
  32. struct ScrollText { int index; char text[256]; int timeStart; };
  33.  
  34. void (__cdecl *AsciiToUnicode)       (const char *ascii, short *pUni);
  35. void (__cdecl *PrintString)          (float x, float y, short *text);
  36. void (__cdecl *SetFontStyle)         (int style);
  37. void (__cdecl *SetScale)             (float w, float h);
  38. void (__cdecl *SetColor)             (RwRGBA *color);
  39. void (__cdecl *SetJustifyOn)         ();
  40. void (__cdecl *SetDropShadowPosition)(int position);
  41. void (__cdecl *SetPropOn)            ();
  42. void (__cdecl *DrawBank)             (int num);
  43.  
  44. struct ScrollText g_aScrolls[20];
  45.  
  46. BOOL APIENTRY DllMain(HMODULE hModule, DWORD reason, LPVOID lpReserved)
  47. {
  48.   int i;
  49.   if(reason==DLL_PROCESS_ATTACH)
  50.   {
  51.     switch(VERSION)
  52.     {
  53.     case GTA_3_1_0:
  54.       redirectCall(0x501B56, ProcessDebug);
  55.       g_TimeMs              = (int *) 0x885B48;
  56.       g_ScreenWidth         = (int *) 0x8F436C;
  57.       g_ScreenHeight        = (int *) 0x8F4370;
  58.       AsciiToUnicode        = (void (__cdecl *)(const char *, short *)) 0x5009C0;
  59.       PrintString           = (void (__cdecl *)(float, float, short *)) 0x500F50;
  60.       SetFontStyle          = (void (__cdecl *)(int)                  ) 0x501DB0;
  61.       SetScale              = (void (__cdecl *)(float, float)         ) 0x501B80;
  62.       SetColor              = (void (__cdecl *)(RwRGBA *)             ) 0x501BD0;
  63.       SetJustifyOn          = (void (__cdecl *)()                     ) 0x501C60;
  64.       SetDropShadowPosition = (void (__cdecl *)(int)                  ) 0x501E70;
  65.       SetPropOn             = (void (__cdecl *)()                     ) 0x501DA0;
  66.       DrawBank              = (void (__cdecl *)(int)                  ) 0x51EC50;
  67.       break;
  68.     case GTA_3_1_1:
  69.       redirectCall(0x501C36, ProcessDebug);
  70.       g_TimeMs              = (int *) 0x885AF8;
  71.       g_ScreenWidth         = (int *) 0x8F4420;
  72.       g_ScreenHeight        = (int *) 0x8F4424;
  73.       AsciiToUnicode        = (void (__cdecl *)(const char *, short *)) 0x500AA0;
  74.       PrintString           = (void (__cdecl *)(float, float, short *)) 0x501030;
  75.       SetFontStyle          = (void (__cdecl *)(int)                  ) 0x501E90;
  76.       SetScale              = (void (__cdecl *)(float, float)         ) 0x501C60;
  77.       SetColor              = (void (__cdecl *)(RwRGBA *)             ) 0x501CB0;
  78.       SetJustifyOn          = (void (__cdecl *)()                     ) 0x501D40;
  79.       SetDropShadowPosition = (void (__cdecl *)(int)                  ) 0x501F50;
  80.       SetPropOn             = (void (__cdecl *)()                     ) 0x501E80;
  81.       DrawBank              = (void (__cdecl *)(int)                  ) 0x51EE80;
  82.       break;
  83.     default:
  84.       MessageBox(0, "Unknown GTA 3 version", "gta3debug.asi", MB_OK);
  85.       return FALSE;
  86.     }
  87.     insertCallWithJump(0x405DB0, Debug);
  88.     g_DebugFile = fopen("debug_log.txt", "w");
  89.   }
  90.   return TRUE;
  91. }
  92.  
  93. void ProcessDebug(int num)
  94. {
  95.   short buf[128];
  96.   RwRGBA color;
  97.   unsigned int i, j, time;
  98.   float y, r, b, a, w, h;
  99.   for(i = 0; i < 20; i++)
  100.   {
  101.     if(g_aScrolls[i].index && GAME_TIME > g_aScrolls[i].timeStart + 5000)
  102.       g_aScrolls[i].index = 0;
  103.   }
  104.   for(i = 0; i < 20; i++)
  105.   {
  106.     time = GAME_TIME - g_aScrolls[i].timeStart;
  107.     if(g_aScrolls[i].index)
  108.     {
  109.       if(time < 2000)
  110.       {
  111.         if(time < 1000)
  112.           r = b = 0.0;
  113.         else
  114.           r = b = time * 0.255 + 255.0;
  115.         a = 255.0;
  116.       }
  117.       else if(time > 4000)
  118.       {
  119.         r = b = 255.0;
  120.         a = (time - 4000) * -0.255 + 255.0;
  121.       }
  122.       else r = b = a = 255.0;
  123.       SetFontStyle(0);
  124.       SetPropOn();
  125.       SetJustifyOn();
  126.       SetScale(0.45, 1.0);
  127.       SetDropShadowPosition(0);
  128.       color.r = r; color.g = 255; color.b = b; color.a = a;
  129.       SetColor(&color);
  130.       AsciiToUnicode(g_aScrolls[i].text, buf);
  131.       PrintString(SCREEN_WIDTH / 640 * 10, SCREEN_HEIGHT / 448 + 10 + (g_aScrolls[i].index - 1) * 20, buf);
  132.     }
  133.   }
  134. }
  135.  
  136. void Debug(const char *format, ...)
  137. {
  138.   char final[256];
  139.   char debug[272];
  140.   char temp[16];
  141.   int i;
  142.   SYSTEMTIME time;
  143.   // update indices
  144.   for(i = 0; i < 20; i++)
  145.   {
  146.     if(g_aScrolls[i].index)
  147.       g_aScrolls[i].index += 1;
  148.     if(g_aScrolls[i].index > 20)
  149.       g_aScrolls[i].index = 0;
  150.   }
  151.   // place new text
  152.   for(i = 0; i < 20; i++)
  153.   {
  154.     if(!g_aScrolls[i].index)
  155.     {
  156.       g_aScrolls[i].index = 1;
  157.       g_aScrolls[i].timeStart = GAME_TIME;
  158.       break;
  159.     }
  160.   }
  161.   va_list ap;
  162.   va_start(ap, format);
  163.   vsnprintf(final, INT_MAX, format, ap);
  164.   va_end(ap);
  165.   strcpy_s(g_aScrolls[i].text, final);
  166.   GetLocalTime(&time);
  167.   sprintf_s(temp, "%02d:%02d:%02d::%03d: ", time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
  168.   strcpy_s(debug, temp);
  169.   strcat_s(debug, final);
  170.   fputs(debug, g_DebugFile); fputs("\n", g_DebugFile);
  171. }
  172.  
  173. void _patch(void* pAddress, void* pData, int iSize)
  174. {
  175.   unsigned long dwProtect[2];
  176.   VirtualProtect(pAddress, iSize, PAGE_EXECUTE_READWRITE, &dwProtect[0]);
  177.   memcpy(pAddress, pData, iSize);
  178.   VirtualProtect(pAddress, iSize, dwProtect[0], &dwProtect[1]);
  179. }
  180.  
  181. void _insertCallWithJump(void * pAddress, void * newFunc)
  182. {
  183.   DWORD pFunc = 0xE9;
  184.   patch((int)pAddress, &pFunc, 1);
  185.   pFunc = (int)newFunc - ((int)pAddress + 5);
  186.   patch((int)pAddress + 1, &pFunc, 4);
  187.   pFunc = 0xC3;
  188.   patch((int)pAddress + 5, &pFunc, 1);
  189. };
  190.  
  191. void _redirectCall(void * pAddress, void * newFunc)
  192. {
  193.   DWORD pFunc = (int)newFunc - ((int)pAddress + 5);
  194.   patch((int)pAddress + 1, &pFunc, 4);
  195. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement