dailydose

Source/Config.cpp

Jul 30th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Config.h>
  2. #include <sstream>
  3.  
  4. Config::Config(std::string fileName)
  5. {
  6.     ParseXml(fileName);
  7.     ParseIni("settings.ini");
  8. }
  9.  
  10. Config::~Config() {}
  11.  
  12. void Config::ParseIni(std::string fileName)
  13. {
  14.     char szPath[MAX_PATH];
  15.     GetCurrentDirectory(sizeof(szPath), szPath);
  16.     lstrcat(szPath, "\\");
  17.     lstrcat(szPath, fileName.c_str());
  18.  
  19.     if (!FileExists(szPath))
  20.     {
  21.         return;
  22.     }
  23.  
  24.     ini.open(fileName);
  25.     while (getline(ini, buff))
  26.     {
  27.         if (buff.find("KEY_GODMODE") != std::string::npos)
  28.         {
  29.             _ReadKey(hotkey.data1);
  30.         }
  31.         if (buff.find("KEY_NORELOAD") != std::string::npos)
  32.         {
  33.             _ReadKey(hotkey.data2);
  34.         }
  35.         if (buff.find("KEY_MONEY") != std::string::npos)
  36.         {
  37.             _ReadKey(hotkey.money);
  38.         }
  39.         if (buff.find("KEY_EXPERIENCE") != std::string::npos)
  40.         {
  41.             _ReadKey(hotkey.experience);
  42.         }
  43.         if (buff.find("KEY_CHEAT_ONEHITKILL") != std::string::npos)
  44.         {
  45.             _ReadKey(hotkey.ohk);
  46.         }
  47.         if (buff.find("KEY_CHEAT_NORELOAD") != std::string::npos)
  48.         {
  49.             _ReadKey(hotkey.nr);
  50.         }
  51.         if (buff.find("AMOUNT_MONEY") != std::string::npos)
  52.         {
  53.             _ReadAmount(amountMoney, money.type);
  54.         }
  55.         if (buff.find("AMOUNT_EXPERIENCE") != std::string::npos)
  56.         {
  57.             _ReadAmount(amountExperience, experience.type);
  58.         }
  59.     }
  60.     ini.close();
  61. }
  62.  
  63. void Config::_ReadAmount(amount& data, std::string& type)
  64. {
  65.     offset = buff.find("=") + 1;
  66.     buff = buff.substr(offset);
  67.     buff = buff.substr(buff.find_first_not_of(' '));
  68.  
  69.     if (type == "Float")
  70.     {
  71.         data.value.f = stof(buff);
  72.     }
  73.     else
  74.     {
  75.         data.value.i = stoi(buff);
  76.     }
  77.     data.type = type;
  78. }
  79.  
  80. void Config::_ReadKey(hot::keys& hotkey)
  81. {
  82.     offset = buff.find("=") + 1;
  83.     buff = buff.substr(offset);
  84.     hotkey.name = buff.substr(buff.find_first_not_of(' '));
  85.     hotkey.code = _Key2code(hotkey.name);
  86. }
  87.  
  88. int Config::_Key2code(std::string key)
  89. {
  90.     size_t len = key.length();
  91.     switch (len)
  92.     {
  93.     case 1: return VkKeyScan(key[0]) & 0xff;
  94.     case 2:
  95.     case 3: return VK_F1 - 1 + atoi(key.substr(1).c_str());
  96.     case 4:
  97.     case 5: return VK_NUMPAD0 + atoi(key.substr(3).c_str());
  98.     case 7:
  99.     case 8: return VK_NUMPAD0 + atoi(key.substr(6).c_str());
  100.     }
  101.     return -1;
  102. }
  103.  
  104. void Config::ParseXml(std::string& file)
  105. {
  106.     xml.open(file);
  107.     while (getline(xml, buff))
  108.     {
  109.         if (buff.find("<Description>\"HEALTH") != std::string::npos)
  110.         {
  111.             _ReadEntry(health);
  112.         }
  113.         if (buff.find("<Description>\"DATA1N2") != std::string::npos)
  114.         {
  115.             _ReadEntry(data1n2);
  116.         }
  117.         if (buff.find("<Description>\"DATA2N1") != std::string::npos)
  118.         {
  119.             _ReadEntry(data2n1);
  120.         }
  121.         if (buff.find("<Description>\"DATA2N2") != std::string::npos)
  122.         {
  123.             _ReadEntry(data2n2);
  124.         }
  125.         if (buff.find("<Description>\"DATA2N3") != std::string::npos)
  126.         {
  127.             _ReadEntry(data2n3);
  128.         }
  129.         if (buff.find("<Description>\"MONEY") != std::string::npos)
  130.         {
  131.             _ReadEntry(money);
  132.         }
  133.         if (buff.find("<Description>\"EXPERIENCE") != std::string::npos)
  134.         {
  135.             _ReadEntry(experience);
  136.         }
  137.         if (buff.find("<Description>\"MAXHEALTH") != std::string::npos)
  138.         {
  139.             _ReadValue(maxHealth);
  140.         }
  141.         if (buff.find("<Description>\"PROCESSNAME") != std::string::npos)
  142.         {
  143.             processName = _ReadSettings();
  144.         }
  145.         if (buff.find("<Description>\"CHEAT_ONEHITKILL") != std::string::npos)
  146.         {
  147.             _ReadCheat(ohk);
  148.         }
  149.         if (buff.find("<Description>\"CHEAT_NORELOAD") != std::string::npos)
  150.         {
  151.             _ReadCheat(nr);
  152.         }
  153.     }
  154.     xml.close();
  155. }
  156.  
  157. void Config::_ReadCheat(cheats& cheat)
  158. {
  159.     std::string opcode;
  160.     std::stringstream ss;
  161.  
  162.     while (getline(xml, buff))
  163.     {
  164.         if (buff.find("//Alt: db ") != std::string::npos)
  165.         {
  166.             ss << buff.substr(10);
  167.             while (ss >> opcode)
  168.             {
  169.                 cheat.original.push_back(static_cast<byte>(strtoul(opcode.c_str(), NULL, 16)));
  170.             }
  171.         }
  172.         if (buff.find("instructionAddress") != std::string::npos)
  173.         {
  174.             _ReadInstruction(cheat.instructionModule, cheat.instructionAddress);
  175.         }
  176.         if (buff.find("returnAddress") != std::string::npos)
  177.         {
  178.             _ReadInstruction(cheat.returnModule, cheat.returnAddress);
  179.             break;
  180.         }
  181.     }
  182.     getline(xml, buff);
  183.     while (getline(xml, buff))
  184.     {
  185.         if (buff.find("}") != std::string::npos)
  186.         {
  187.             break;
  188.         }
  189.  
  190.         ss.str(std::string());
  191.         ss.clear();
  192.         ss << buff;
  193.         while (ss >> opcode)
  194.         {
  195.             cheat.inject.push_back(static_cast<byte>(strtoul(opcode.c_str(), NULL, 16)));
  196.         }
  197.     }
  198.  
  199.     cheat.jump.push_back(0xE9);
  200.     for (size_t i = 1; i < 5; i++)
  201.     {
  202.         cheat.jump.push_back(0);
  203.     }
  204.     for (size_t i = 5; i < cheat.original.size(); i++)
  205.     {
  206.         cheat.jump.push_back(0x90);
  207.     }
  208.     cheat.exists = true;
  209. }
  210.  
  211. void Config::_ReadInstruction(std::string& module, DWORD& address)
  212. {
  213.     // read Module
  214.     offset = buff.find(":") + 3;
  215.     size = buff.find("+") - offset - 1;
  216.     module = buff.substr(offset, size);
  217.  
  218.     // read Base Address
  219.     offset = buff.find("+") + 1;
  220.     address = std::stoul(buff.substr(offset), nullptr, 16);
  221. }
  222.  
  223. void Config::_ReadEntry(entrys& data)
  224. {
  225.     while (getline(xml, buff))
  226.     {
  227.         if (buff.find("<VariableType>") != std::string::npos)
  228.         {
  229.             _ReadType(data);
  230.         }
  231.         if (buff.find("<Address>") != std::string::npos)
  232.         {
  233.             _ReadAddress(data);
  234.         }
  235.         if (buff.find("<Offsets>") != std::string::npos)
  236.         {
  237.             _ReadOffsets(data);
  238.             break;
  239.         }
  240.     }
  241. }
  242.  
  243. void Config::_ReadType(entrys& data)
  244. {
  245.     offset = buff.find(">") + 1;
  246.     size = buff.find("/") - offset - 1;
  247.     data.type = buff.substr(offset, size);
  248.  
  249.     if (data.type == "Byte")
  250.     {
  251.         data.size = 1;
  252.     }
  253.     else if (data.type == "2 Bytes")
  254.     {
  255.         data.size = 2;
  256.     }
  257.     else
  258.     {
  259.         data.size = 4;
  260.     }
  261. }
  262.  
  263. void Config::_ReadAddress(entrys& data)
  264. {
  265.     // read Module
  266.     offset = buff.find(">") + 2;
  267.     size = buff.find("+") - offset - 1;
  268.     data.module = buff.substr(offset, size);
  269.  
  270.     // read Base Address
  271.     offset = buff.find("+") + 1;
  272.     size = buff.find("/") - offset - 1;
  273.     data.address = std::stoul(buff.substr(offset, size), nullptr, 16);
  274. }
  275.  
  276. void Config::_ReadOffsets(entrys& data)
  277. {
  278.     while (getline(xml, buff))
  279.     {
  280.         if (buff.find("</Offsets>") != std::string::npos)
  281.         {
  282.             break;
  283.         }
  284.  
  285.         offset = buff.find(">") + 1;
  286.         size = buff.find("/") - offset - 1;
  287.         data.offsets[data.count] = std::stoul(buff.substr(offset, size), nullptr, 16);
  288.         ++data.count;
  289.     }
  290.     --data.count;
  291. }
  292.  
  293. void Config::_ReadValue(amount& amount)
  294. {
  295.     while (getline(xml, buff))
  296.     {
  297.         if (buff.find("<VariableType>") != std::string::npos)
  298.         {
  299.             offset = buff.find(">") + 1;
  300.             size = buff.find("/") - offset - 1;
  301.             amount.type = buff.substr(offset, size);
  302.  
  303.             if ("Float" == amount.type)
  304.             {
  305.                 amount.value.f = stof(_ReadSettings());
  306.             }
  307.             else
  308.             {
  309.                 amount.value.i = stoi(_ReadSettings());
  310.             }
  311.             return;
  312.         }
  313.     }
  314. }
  315.  
  316. std::string Config::_ReadSettings()
  317. {
  318.     while (getline(xml, buff))
  319.     {
  320.         if (buff.find("<Address>") != std::string::npos)
  321.         {
  322.             offset = buff.find(">") + 1;
  323.             size = buff.find("/") - offset - 1;
  324.             return buff.substr(offset, size);
  325.         }
  326.     }
  327.     return NULL;
  328. }
  329.  
  330. BOOL Config::FileExists(LPCTSTR szPath)
  331. {
  332.     DWORD dwAttrib = GetFileAttributes(szPath);
  333.  
  334.     return (dwAttrib != INVALID_FILE_ATTRIBUTES
  335.         && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
  336. }
Add Comment
Please, Sign In to add comment