dailydose

Source/Game.cpp

Jul 30th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Trainer.h>
  2. #include <Game.h>
  3. #include <TlHelp32.h>
  4.  
  5.  
  6. Game::Game(Config* cfg, int pId)
  7. {
  8.     this->cfg = cfg;
  9.     this->pId = pId;
  10.     hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pId);
  11.  
  12. #ifdef _WIN64
  13.     dwSize = IsWow64() ? 4 : 8;
  14. #else
  15.     dwSize = 4;
  16. #endif
  17.  
  18.     InitCheats();
  19. }
  20.  
  21. Game::~Game()
  22. {
  23.     VirtualFreeEx(hProc, injectMemory, 0, MEM_RELEASE);
  24.     CloseHandle(hProc);
  25. }
  26.  
  27. bool Game::PlayerIsDead()
  28. {
  29.     ptr.health = GetPointer(cfg->health);
  30.  
  31.     if (cfg->health.type == "Float")
  32.     {
  33.         ReadProcessMemory(hProc, (LPVOID)ptr.health, &health.f, cfg->health.size, NULL);
  34.         return health.f <= 0 || health.f > cfg->maxHealth.value.f;
  35.     }
  36.  
  37.     ReadProcessMemory(hProc, (LPVOID)ptr.health, &health.i, cfg->health.size, NULL);
  38.     return health.i <= 0 || health.i > cfg->maxHealth.value.i;
  39. }
  40.  
  41. void Game::InitCheats()
  42. {
  43.     if (cfg->ohk.exists)
  44.     {
  45.         _InitCheat(ohk, cfg->ohk);
  46.     }
  47.     if (cfg->nr.exists)
  48.     {
  49.         _InitCheat(nr, cfg->nr);
  50.     }
  51.  
  52.     injectMemory = VirtualAllocEx
  53.     (
  54.         hProc,
  55.         NULL,
  56.         ohk.cheatSize + nr.cheatSize,
  57.         MEM_RESERVE | MEM_COMMIT,
  58.         PAGE_EXECUTE_READWRITE
  59.     );
  60.  
  61.     if (cfg->ohk.exists)
  62.     {
  63.         _WriteCheat(ohk);
  64.     }
  65.     if (cfg->nr.exists)
  66.     {
  67.         _WriteCheat(nr);
  68.     }
  69. }
  70.  
  71. void Game::_InitCheat(cheats& cheat, Config::cheats& cfg)
  72. {
  73.     cheat.instructionAddress = GetModule(cfg.instructionModule) + cfg.instructionAddress;
  74.     cheat.returnAddress = GetModule(cfg.returnModule) + cfg.returnAddress;
  75.     cheat.cheatSize = cfg.inject.size();
  76.  
  77.     cheat.original = &cfg.original[0];
  78.     cheat.jump = &cfg.jump[0];
  79.     cheat.inject = &cfg.inject[0];
  80. }
  81.  
  82. void Game::_WriteCheat(cheats& cheat)
  83. {
  84.     cheat.offset = injectOffset;
  85.     _ComputeBytes(cheat);
  86.  
  87.     WriteProcessMemory
  88.     (
  89.         hProc,
  90.         (LPVOID)((DWORD_PTR)injectMemory + injectOffset),
  91.         cheat.inject,
  92.         cheat.cheatSize,
  93.         NULL
  94.     );
  95.     injectOffset += cheat.cheatSize;
  96. }
  97.  
  98. void Game::ToggleCheat(CHEATS_TYPE type)
  99. {
  100.     switch (type)
  101.     {
  102.     case CHEAT_ONEHITKILL:
  103.         _ToggleCheat(ohk, cfg->ohk.original.size());
  104.         break;
  105.  
  106.     case CHEAT_NORELOAD:
  107.         _ToggleCheat(nr, cfg->nr.original.size());
  108.         break;
  109.     }
  110.  
  111. }
  112.  
  113. void Game::_ToggleCheat(cheats& cheat, size_t sz)
  114. {
  115.     if (injectMemory == NULL)
  116.     {
  117.         return;
  118.     }
  119.     if (cheat.active)
  120.     {
  121.         WriteProcessMemory(hProc, (LPVOID)cheat.instructionAddress, cheat.original, sz, NULL);
  122.     }
  123.     else
  124.     {
  125.         WriteProcessMemory(hProc, (LPVOID)cheat.instructionAddress, cheat.jump, sz, NULL);
  126.     }
  127.     cheat.active = !cheat.active;
  128. }
  129.  
  130. void Game::_ComputeBytes(cheats& cheat)
  131. {
  132.     cheat.toOriginal.i = (DWORD_PTR)injectMemory + cheat.offset - cheat.instructionAddress - 5;
  133.     for (int i = 0; i < 4; i++)
  134.         cheat.jump[1 + i] = cheat.toOriginal.b[i];
  135.  
  136.     cheat.toInject.i = cheat.returnAddress - (cheat.cheatSize - 5 + (DWORD_PTR)injectMemory + cheat.offset) - 5;
  137.     for (int i = 0; i < 4; i++)
  138.         cheat.inject[cheat.cheatSize - 4 + i] = cheat.toInject.b[i];
  139. }
  140.  
  141. void Game::UpdateData1()
  142. {
  143.     RewriteData(prevPtr.health, ptr.health, max.health, cfg->health.size);
  144.  
  145.     if (cfg->data1n2.size != 0)
  146.     {
  147.         ptr.data1n2 = GetPointer(cfg->data1n2);
  148.         RewriteData(prevPtr.data1n2, ptr.data1n2, max.data1n2, cfg->data1n2.size);
  149.     }
  150. }
  151.  
  152. void Game::UpdateData2()
  153. {
  154.     if (cfg->data2n1.size != 0)
  155.     {
  156.         ptr.data2n1 = GetPointer(cfg->data2n1);
  157.         RewriteData(prevPtr.data2n1, ptr.data2n1, max.data2n1, cfg->data2n1.size);
  158.     }
  159.  
  160.     if (cfg->data2n2.size != 0)
  161.     {
  162.         ptr.data2n2 = GetPointer(cfg->data2n2);
  163.         RewriteData(prevPtr.data2n2, ptr.data2n2, max.data2n2, cfg->data2n2.size);
  164.     }
  165.  
  166.     if (cfg->data2n3.size != 0)
  167.     {
  168.         ptr.data2n3 = GetPointer(cfg->data2n3);
  169.         RewriteData(prevPtr.data2n3, ptr.data2n3, max.data2n3, cfg->data2n3.size);
  170.     }
  171. }
  172.  
  173. void Game::AddCurrency(POINTS_TYPE type)
  174. {
  175.     switch (type)
  176.     {
  177.     case POINTS_MONEY:
  178.         AddData(money, cfg->amountMoney, cfg->money, ptr.money);
  179.         break;
  180.     case POINTS_EXPERIENCE:
  181.         AddData(experience, cfg->amountExperience, cfg->experience, ptr.experience);
  182.         break;
  183.     }
  184. }
  185.  
  186. void Game::AddData(types& value, Config::amount& amount, Config::entrys& e, DWORD_PTR& ptr)
  187. {
  188.     ptr = GetPointer(e);
  189.     if (e.type == "Float")
  190.     {
  191.         ReadProcessMemory(hProc, (LPVOID)ptr, &value.f, e.size, NULL);
  192.         value.f += amount.value.f;
  193.         WriteProcessMemory(hProc, (LPVOID)ptr, &value.f, e.size, NULL);
  194.     }
  195.     else
  196.     {
  197.         ReadProcessMemory(hProc, (LPVOID)ptr, &value.i, e.size, NULL);
  198.         value.i += amount.value.i;
  199.         WriteProcessMemory(hProc, (LPVOID)ptr, &value.i, e.size, NULL);
  200.     }
  201. }
  202.  
  203. void Game::RewriteData(DWORD_PTR& prevPtr, DWORD_PTR ptr, int& freezedValue, short sz)
  204. {
  205.     if (prevPtr != ptr)
  206.     {
  207.         prevPtr = ptr;
  208.         ReadProcessMemory(hProc, (LPVOID)ptr, &freezedValue, sz, NULL);
  209.         return;
  210.     }
  211.  
  212.     ReadProcessMemory(hProc, (LPCVOID)ptr, &currentValue, sz, NULL);
  213.  
  214.     if (freezedValue > currentValue)
  215.     {
  216.         WriteProcessMemory(hProc, (LPVOID)ptr, &freezedValue, sz, NULL);
  217.     }
  218.     else
  219.     {
  220.         freezedValue = currentValue;
  221.     }
  222. }
  223.  
  224. DWORD_PTR Game::GetPointer(Config::entrys& e)
  225. {
  226.     ReadProcessMemory(hProc, (LPCVOID)(GetModule(e.module) + e.address), &ptr.temp, dwSize, NULL);
  227.  
  228.     if (e.count == -1)
  229.     {
  230.         return ptr.temp;
  231.     }
  232.     for (int i = 0; i < e.count; i++)
  233.     {
  234.         ReadProcessMemory(hProc, (LPCVOID)(ptr.temp + e.offsets[e.count - i]), &ptr.temp, dwSize, NULL);
  235.     }
  236.     return ptr.temp + e.offsets[0];
  237. }
  238.  
  239. DWORD_PTR Game::GetModule(std::string moduleName)
  240. {
  241.     MODULEENTRY32 lpModuleEntry = { 0 };
  242.     HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pId);
  243.  
  244.     if (!hSnapShot)
  245.         return NULL;
  246.     lpModuleEntry.dwSize = sizeof(lpModuleEntry);
  247.     BOOL bModule = Module32First(hSnapShot, &lpModuleEntry);
  248.     while (bModule)
  249.     {
  250.         if (moduleName == static_cast<std::string>(lpModuleEntry.szModule))
  251.         {
  252.             CloseHandle(hSnapShot);
  253.             return (DWORD_PTR)lpModuleEntry.modBaseAddr;
  254.         }
  255.         bModule = Module32Next(hSnapShot, &lpModuleEntry);
  256.     }
  257.     CloseHandle(hSnapShot);
  258.     return NULL;
  259. }
  260.  
  261. BOOL Game::IsWow64()
  262. {
  263.     BOOL bIsWow64 = FALSE;
  264.  
  265.     typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
  266.  
  267.     LPFN_ISWOW64PROCESS fnIsWow64Process;
  268.     fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
  269.         GetModuleHandle(TEXT("kernel32")), "IsWow64Process");
  270.  
  271.     if (NULL != fnIsWow64Process)
  272.     {
  273.         if (!fnIsWow64Process(hProc, &bIsWow64))
  274.         {
  275.             //handle error
  276.         }
  277.     }
  278.     return bIsWow64;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment