dailydose

Source/Trainer.cpp

Jul 30th, 2016
122
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 <resource.h>
  4. #include <TlHelp32.h>
  5. #pragma comment(lib,"Winmm.lib")
  6. // [console version]
  7. #include <iostream>
  8. #include <sstream>
  9. #include <iomanip>
  10. // [/console version]
  11.  
  12. HWND Trainer::wnd = NULL;
  13.  
  14. Trainer::Trainer()
  15. {
  16.     sound[0] = GetResource(SND_OFF);
  17.     sound[1] = GetResource(SND_ON);
  18.     sound[2] = GetResource(SND_MONEY);
  19.     sound[3] = GetResource(SND_EXPERIENCE);
  20.     bGodMode = false;
  21.     bNoReload = false;
  22.  
  23.     SetConsoleTitle("GameTrainer+");
  24.     // SetWindowText()
  25.     // [console version]
  26.     system("color a");
  27.     // [/console version]
  28. }
  29.  
  30. Trainer::~Trainer()
  31. {
  32.     delete cfg;
  33.     delete game;
  34. }
  35.  
  36. // [console version]
  37. bool Trainer::ChooseConfig()
  38. {
  39.     char cfgDir[MAX_PATH], szPath[MAX_PATH];
  40.     GetCurrentDirectory(sizeof(cfgDir), cfgDir);
  41.     lstrcat(cfgDir, "\\Games\\");
  42.     memcpy(szPath, cfgDir, MAX_PATH);
  43.     lstrcat(szPath, "*.*");
  44.  
  45.     if (!PrintCheatTables(szPath))
  46.         return false;
  47.  
  48.     // Select CheatTable file
  49.     std::cout << std::endl;
  50.     std::string fileName;
  51.     do
  52.     {
  53.         std::cout << "Enter CheatTable name:" << std::endl;
  54.         std::cin >> fileName;
  55.         memcpy(szPath, cfgDir, MAX_PATH);
  56.         lstrcat(szPath, fileName.c_str());
  57.     } while (!Config::FileExists(szPath));
  58.  
  59.     cfg = new Config(szPath);
  60.     CheckXml();
  61.     CheckHotKeys();
  62.  
  63.     std::string title = cfg->processName;
  64.     SetConsoleTitle(title.append(" - GameTrainer+").c_str());
  65.     // SetWindowText()
  66.     PrintMsg(MSG_WAIT_FOR_START);
  67.     return true;
  68. }
  69.  
  70. bool Trainer::PrintCheatTables(char* szPath)
  71. {
  72.     // Step 1: Find config files
  73.     WIN32_FIND_DATA ffd;
  74.     HANDLE hFind = FindFirstFile(szPath, &ffd);
  75.     if (hFind == INVALID_HANDLE_VALUE)
  76.     {
  77.         std::cout << "Config files was not found." << std::endl;
  78.         system("pause");
  79.         return false;
  80.     }
  81.  
  82.     // Step 2: Print config list
  83.     std::cout << "Config list:\n" << std::endl;
  84.     do
  85.     {
  86.         if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  87.         {
  88.             std::cout << ffd.cFileName << std::endl;
  89.         }
  90.     } while (FindNextFile(hFind, &ffd));
  91.     FindClose(hFind);
  92.     return true;
  93. }
  94.  
  95. void Trainer::CheckXml()
  96. {
  97.     if (cfg->processName == "")
  98.     {
  99.         std::cout << "Enter procces name:" << std::endl;
  100.         std::cin >> cfg->processName;
  101.     }
  102.     if (cfg->maxHealth.type == "")
  103.     {
  104.         std::cout << "Enter max health:" << std::endl;
  105.         SetAmountValues(cfg->maxHealth, cfg->health.type);
  106.     }
  107.     if (cfg->money.size != 0 && cfg->amountMoney.type == "")
  108.     {
  109.         std::cout << "Enter amount of added Data3:" << std::endl;
  110.         SetAmountValues(cfg->amountMoney, cfg->money.type);
  111.     }
  112.     if (cfg->experience.size != 0 && cfg->amountExperience.type == "")
  113.     {
  114.         std::cout << "Enter amount of added Data4:" << std::endl;
  115.         SetAmountValues(cfg->amountExperience, cfg->experience.type);
  116.     }
  117. }
  118.  
  119. void Trainer::SetAmountValues(Config::amount& data, std::string type)
  120. {
  121.     if (type == "Float")
  122.         std::cin >> data.value.f;
  123.     else
  124.         std::cin >> data.value.i;
  125. }
  126.  
  127. void Trainer::CheckHotKeys()
  128. {
  129.     if (cfg->hotkey.data1.code != -1 && cfg->hotkey.data1.name != "")
  130.         return;
  131.  
  132.     cfg->hotkey.data1.name = "F6";
  133.     cfg->hotkey.data1.code = VK_F6;
  134.  
  135.     cfg->hotkey.data2.name = "F7";
  136.     cfg->hotkey.data2.code = VK_F7;
  137.  
  138.     cfg->hotkey.money.name = "F8";
  139.     cfg->hotkey.money.code = VK_F8;
  140.  
  141.     cfg->hotkey.experience.name = "F9";
  142.     cfg->hotkey.experience.code = VK_F9;
  143.  
  144.     cfg->hotkey.ohk.name = "NUM0";
  145.     cfg->hotkey.ohk.code = VK_NUMPAD0;
  146.  
  147.     cfg->hotkey.nr.name = "NUM1";
  148.     cfg->hotkey.nr.code = VK_NUMPAD1;
  149. }
  150.  
  151. void Trainer::PrintMsg(Msg type)
  152. {
  153.     system("cls");
  154.     std::cout << std::endl << std::endl << std::endl
  155.         << "\t$$$$$$_$$$$$___$$$$__$$$$$$_$$__$$_$$$$$__$$$$$" << std::endl
  156.         << "\t__$$___$$__$$_$$__$$___$$___$$$_$$_$$_____$$__$$_______$$" << std::endl
  157.         << "\t__$$___$$$$$__$$$$$$___$$___$$_$$$_$$$$___$$$$$______$$$$$$" << std::endl
  158.         << "\t__$$___$$__$$_$$__$$___$$___$$__$$_$$_____$$__$$_______$$" << std::endl
  159.         << "\t__$$___$$__$$_$$__$$_$$$$$$_$$__$$_$$$$$__$$__$$" << std::endl
  160.         << std::endl << std::endl << std::endl << std::endl;
  161.  
  162.     if (type == MSG_WAIT_FOR_START)
  163.     {
  164.         std::cout
  165.             << "\t\t\t Waiting for start:" << std::endl
  166.             << "\t\t\t " << cfg->processName << std::endl;
  167.     }
  168.     else if (type == MSG_HOTKEYS)
  169.     {
  170.         std::cout << "\t\t\t "
  171.             << cfg->hotkey.data1.name
  172.             << " - toggle GodMode" << std::endl;
  173.  
  174.         if (cfg->data2n1.size != 0 || cfg->data2n2.size != 0 || cfg->data2n3.size != 0)
  175.             std::cout << "\t\t\t "
  176.             << cfg->hotkey.data2.name
  177.             << " - toggle NoReload" << std::endl;
  178.  
  179.         if (cfg->money.size != 0)
  180.             std::cout << "\t\t\t "
  181.             << cfg->hotkey.money.name
  182.             << " - increase "
  183.             << GetAmount(cfg->money, cfg->amountMoney)
  184.             << " Money" << std::endl;
  185.  
  186.         if (cfg->experience.size != 0)
  187.             std::cout << "\t\t\t "
  188.             << cfg->hotkey.experience.name
  189.             << " - increase "
  190.             << GetAmount(cfg->experience, cfg->amountExperience)
  191.             << " Experience" << std::endl;
  192.  
  193.         if (cfg->ohk.exists)
  194.             std::cout << "\t\t\t "
  195.             << cfg->hotkey.ohk.name
  196.             << " - toggle One Hit Kill" << std::endl;
  197.  
  198.         if (cfg->nr.exists)
  199.             std::cout << "\t\t\t "
  200.             << cfg->hotkey.nr.name
  201.             << " - toggle No Reload" << std::endl;
  202.     }
  203. }
  204.  
  205. inline std::string Trainer::GetAmount(Config::entrys& entry, Config::amount& amount)
  206. {
  207.     std::stringstream ss;
  208.     if (entry.type == "Float")
  209.     {
  210.         ss << std::fixed << std::setprecision(2) << amount.value.f;
  211.     }
  212.     else
  213.     {
  214.         ss << amount.value.i;
  215.     }
  216.  
  217.     return ss.str();
  218. }
  219. // [/console version]
  220.  
  221. void Trainer::Start()
  222. {
  223.     game = new Game(cfg, GetProcessIdByName(cfg->processName));
  224.     PrintMsg(MSG_HOTKEYS);
  225.     while (true)
  226.     {
  227.         // Close Trainer IF GAME is NOT RUNNING
  228.         if (!GameIsRun())
  229.         {
  230.             break;
  231.         }
  232.  
  233.         // Continue IF GAME is not active || PLAYER is DEAD
  234.         if (!GameOnFocus() || game->PlayerIsDead())
  235.         {
  236.             Sleep(1000);
  237.             continue;
  238.         }
  239.  
  240.         // Rewrite data if FREEZE FLAG enabled
  241.         if (bGodMode)
  242.         {
  243.             game->UpdateData1();
  244.         }
  245.         if (bNoReload)
  246.         {
  247.             game->UpdateData2();
  248.         }
  249.  
  250.         // Keyboard handling
  251.         if (GetAsyncKeyState(cfg->hotkey.data1.code) & 1)
  252.         {
  253.             bGodMode = !bGodMode;
  254.             Speech(bGodMode ? SOUND_ON : SOUND_OFF);
  255.         }
  256.         if (GetAsyncKeyState(cfg->hotkey.data2.code) & 1)
  257.         {
  258.             bNoReload = !bNoReload;
  259.             Speech(bNoReload ? SOUND_ON : SOUND_OFF);
  260.         }
  261.         if (cfg->money.size != 0 && GetAsyncKeyState(cfg->hotkey.money.code) & 1)
  262.         {
  263.             game->AddCurrency(Game::POINTS_MONEY);
  264.             Speech(SOUND_MONEY);
  265.         }
  266.         if (cfg->experience.size != 0 && GetAsyncKeyState(cfg->hotkey.experience.code) & 1)
  267.         {
  268.             game->AddCurrency(Game::POINTS_EXPERIENCE);
  269.             Speech(SOUND_EXPERIENCE);
  270.         }
  271.         if (cfg->ohk.exists && GetAsyncKeyState(cfg->hotkey.ohk.code) & 1)
  272.         {
  273.             game->ToggleCheat(Game::CHEAT_ONEHITKILL);
  274.             Speech(game->ohk.active ? SOUND_ON : SOUND_OFF);
  275.         }
  276.         if (cfg->nr.exists && GetAsyncKeyState(cfg->hotkey.nr.code) & 1)
  277.         {
  278.             game->ToggleCheat(Game::CHEAT_NORELOAD);
  279.             Speech(game->nr.active ? SOUND_ON : SOUND_OFF);
  280.         }
  281.         Sleep(50);
  282.     }
  283. }
  284.  
  285. bool Trainer::TrainerIsRun()
  286. {
  287.     HANDLE mutex = CreateMutex(NULL, TRUE, "GameTrainer+");
  288.  
  289.     if (mutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
  290.     {
  291.         if (EnumWindows(&EnumWindowsProc, NULL) && wnd != NULL)
  292.         {
  293.             ShowWindow(wnd, SW_RESTORE);
  294.             SetForegroundWindow(wnd);
  295.             return true;
  296.         }
  297.     }
  298.  
  299.     return false;
  300. }
  301.  
  302. BOOL CALLBACK Trainer::EnumWindowsProc(HWND hwnd, LPARAM lParam)
  303. {
  304.     char szTextWin[255]; DWORD dwPID = NULL;
  305.     if (GetWindowText(hwnd, szTextWin, sizeof(szTextWin)))
  306.     {
  307.         CharToOem(szTextWin, szTextWin);
  308.         if (std::string::npos != ((std::string)szTextWin).find("GameTrainer+"))
  309.         {
  310.             szTextWin[0] = '\0';
  311.             if (GetClassName(hwnd, szTextWin, sizeof(szTextWin)))
  312.             {
  313.                 if ((std::string)"ConsoleWindowClass" == (std::string)szTextWin)
  314.                     wnd = hwnd;
  315.             }
  316.         }
  317.     }
  318.     szTextWin[0] = '\0';
  319.     return TRUE;
  320. }
  321.  
  322. bool Trainer::GameOnFocus()
  323. {
  324.     DWORD idActive;
  325.     GetWindowThreadProcessId(GetForegroundWindow(), &idActive);
  326.     return idActive == GetProcessIdByName(cfg->processName);
  327. }
  328.  
  329. inline void Trainer::Speech(Sound type)
  330. {
  331.     sndPlaySound(sound[type], SND_MEMORY | SND_ASYNC | SND_NODEFAULT);
  332. }
  333.  
  334. bool Trainer::GameIsRun()
  335. {
  336.     return 0 != GetProcessIdByName(cfg->processName);
  337. }
  338.  
  339. DWORD Trainer::GetProcessIdByName(std::string processName)
  340. {
  341.     DWORD pid = 0;
  342.  
  343.     // Create toolhelp snapshot
  344.     HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  345.     PROCESSENTRY32 process;
  346.     ZeroMemory(&process, sizeof(process));
  347.     process.dwSize = sizeof(process);
  348.  
  349.     // Walkthrough all processes
  350.     if (Process32First(snapshot, &process))
  351.     {
  352.         do
  353.         {
  354.             // Compare process.szExeFile based on format of name, i.e., trim file path
  355.             // trim .exe if necessary, etc
  356.             if (processName == static_cast<std::string>(process.szExeFile))
  357.             {
  358.                 pid = process.th32ProcessID;
  359.                 break;
  360.             }
  361.         } while (Process32Next(snapshot, &process));
  362.     }
  363.  
  364.     CloseHandle(snapshot);
  365.     return pid;
  366. }
  367.  
  368. // MSDN.function
  369. LPSTR Trainer::GetResource(int resourceId)
  370. {
  371.     HMODULE hInst = GetModuleHandle(NULL);
  372.     if (!hInst)
  373.     {
  374.         return FALSE;
  375.     }
  376.     LPSTR lpRes;
  377.     HANDLE hResInfo, hRes;
  378.  
  379.     // Find the WAVE resource.
  380.  
  381.     hResInfo = FindResource(hInst, MAKEINTRESOURCE(resourceId), "WAVE");
  382.     if (hResInfo == NULL)
  383.         return FALSE;
  384.  
  385.     // Load the WAVE resource.
  386.  
  387.     hRes = LoadResource(hInst, (HRSRC)hResInfo);
  388.     if (hRes == NULL)
  389.         return FALSE;
  390.  
  391.     // Lock the WAVE resource and play it.
  392.  
  393.     lpRes = (LPSTR)LockResource(hRes);
  394.     if (lpRes != NULL)
  395.     {
  396.         UnlockResource(hRes);
  397.     }
  398.  
  399.     // Free the WAVE resource and return success or failure.
  400.  
  401.     FreeResource(hRes);
  402.     return lpRes;
  403. }
Add Comment
Please, Sign In to add comment