Facebook
From Cream Dolphin, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 217
  1. #include "plugin.h"
  2. #include "game_sa\RenderWare.h"
  3. #include "game_sa\common.h"
  4. #include "game_sa\CFont.h"
  5. #include "game_sa\CPed.h"
  6.  
  7. #include <Psapi.h>
  8.  
  9. #pragma comment( lib, "psapi.lib" )
  10.  
  11. #define E_ADDR_GAMEPROCESS      0x53E981
  12.  
  13. using namespace plugin;
  14.  
  15. #pragma pack(push, 1)
  16. typedef struct stOpcodeRelCall
  17. {
  18.         BYTE bOpcode;
  19.         DWORD dwRelAddr;
  20. } OpcodeRelCall;
  21. #pragma pack(pop)
  22.  
  23. class GPS {
  24. private:
  25.         HANDLE hThread = NULL;
  26.  
  27. public:
  28.         GPS() {
  29.                 hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)GPS::init, (LPVOID)this, 0, (LPDWORD)NULL);
  30.         }
  31.  
  32.         ~GPS() {
  33.                 // Check if thread still running on process
  34.                 if (hThread != NULL)
  35.                         TerminateThread(hThread, 0);
  36.         }
  37.  
  38.         static LPVOID WINAPI init(LPVOID *lpParam) {
  39.                 MODULEINFO miSampDll;
  40.                 DWORD dwSampDllBaseAddr, dwSampDllEndAddr, dwCallAddr;
  41.  
  42.                 GPS *sender = (GPS *)lpParam;
  43.  
  44.                 stOpcodeRelCall *fnGameProc = (stOpcodeRelCall *)E_ADDR_GAMEPROCESS;
  45.  
  46.                 // Check if E_ADDR_GAMEPROCESS opcode is a relative call (0xE8)
  47.                 while (fnGameProc->bOpcode != 0xE8)
  48.                         Sleep(100);
  49.  
  50.                 while (true) {
  51.                         Sleep(100);
  52.  
  53.                         // Get samp.dll module information to get base address and end address
  54.                         if (!GetModuleInformation(GetCurrentProcess(), GetModuleHandle("samp.dll"), &miSampDll, sizeof(MODULEINFO))) {
  55.                                 continue;
  56.                         }
  57.  
  58.                         // Some stupid calculation
  59.                         dwSampDllBaseAddr = (DWORD)miSampDll.lpBaseOfDll;
  60.                         dwSampDllEndAddr = dwSampDllBaseAddr + miSampDll.SizeOfImage;
  61.  
  62.                         // Calculate destination address by offset and relative call opcode size
  63.                         dwCallAddr = fnGameProc->dwRelAddr + E_ADDR_GAMEPROCESS + 5;
  64.  
  65.                         // Check if dwCallAddr is a samp.dll's hook address,
  66.                         // to make sure this plugin hook (Events::gameProcessEvent) not replaced by samp.dll
  67.                         if (dwCallAddr >= dwSampDllBaseAddr && dwCallAddr <= dwSampDllEndAddr)
  68.                                 break;
  69.                 }
  70.  
  71.                 // Just wait a few secs for the game loaded fully to avoid any conflicts and crashes
  72.                 // I don't know what the elegant way is :)
  73.                 while (!FindPlayerPed(0))
  74.                         Sleep(5000);
  75.  
  76.                 // Run the plugin
  77.                 sender->run();
  78.  
  79.                 // Reset the thread handle
  80.                 sender->hThread = NULL;
  81.  
  82.                 return NULL;
  83.         }
  84.  
  85.         void run() {
  86.                 Events::drawHudEvent += [] {
  87.                         CPed *player = FindPlayerPed(0);
  88.                         if (player)
  89.                         {
  90.                                 printf("%f\n", player->m_fCurrentRotation);
  91.                         }
  92.                 };
  93.  
  94.         }
  95.  
  96. } gps;