Wolfrost

GDAuto.cpp

Jan 7th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.71 KB | None | 0 0
  1. // GDAuto.cpp : Source definition of class GDAuto.
  2. //
  3.  
  4. #include "GDAuto.h"
  5. #include <string>
  6. #include <sstream>
  7. #include <array>
  8. using namespace std;
  9.  
  10. #pragma region GEOMETRY DASH AUTO
  11.  
  12. GDAuto::GDAuto(const int key_toggle, string _filename)
  13.     :   KEY_TOGGLE(key_toggle),
  14.         jump('j'),
  15.         wait('w'),
  16.         sep('-')
  17. {
  18.     this->started = false;
  19.     this->filename = _filename;
  20.     //Command detection
  21.     GetCommand();
  22. }
  23.  
  24. GDAuto::~GDAuto()
  25. {
  26.     cout<<"Writer class has been destroyed."<<endl;
  27. }
  28.  
  29. void GDAuto::WriteFromInput()
  30. {
  31.     bool action = false; //true = ha saltato
  32.     bool write_start = false; //true = inizia a scrivere
  33.     int val = 0;
  34.     string sequence;
  35.     cout<<"WriteFromInput setup finished. Waiting for left click to start writing."<<endl;
  36.     while (true)
  37.         if (GetAsyncKeyState(VK_LBUTTON)) break;
  38.     cout<<"WriteFromInput started. Entering the loop..."<<endl;
  39.     while (true)
  40.     {
  41.  
  42.         //Se premi il tasto definito, interrompe il ciclo
  43.         if (GetAsyncKeyState(KEY_TOGGLE)) break;
  44.  
  45.         //Questo è il main loop dove legge i tasti e scrive
  46.         /* BACKUP
  47.         if (action)
  48.         {
  49.             if (GetAsyncKeyState(VK_LBUTTON))
  50.             {
  51.                 //sequence << jump << val << sep; vecchia versione con stringstream
  52.                 sequence += jump + NumberToString<int>(val) + sep;
  53.                 action = true;
  54.                 val = 0;
  55.             }
  56.             else
  57.             {
  58.                 if (!GetAsyncKeyState(VK_LBUTTON))
  59.                 {
  60.                     //sequence << wait << val << sep; vecchia versione con stringstream
  61.                     sequence += wait + NumberToString<int>(val) + sep;
  62.                     action = false;
  63.                     val = 0;
  64.                 }
  65.             }
  66.         }
  67.         */
  68.  
  69.         if (action)
  70.         {
  71.             if (!GetAsyncKeyState(VK_LBUTTON))
  72.             {
  73.                 sequence += jump + NumberToString<int>(val) + sep;
  74.                 action = false;
  75.                 val = 0;
  76.             }
  77.         }
  78.         else
  79.         {
  80.             if (GetAsyncKeyState(VK_LBUTTON))
  81.             {
  82.                 sequence += wait + NumberToString<int>(val) + sep;
  83.                 action = true;
  84.                 val = 0;
  85.             }
  86.         }
  87.  
  88.         val++;
  89.  
  90.         //Pausa il thread per 1ms, così ogni ciclata corrisponde ad 1 ms.
  91.         Sleep(1);
  92.  
  93.     }
  94.  
  95.     this->GDFile.open(this->filename);
  96.     if (this->GDFile.is_open()) {
  97.         //Scrivi tutto su file
  98.         this->GDFile << sequence;
  99.     } else cout<<"Unable to open file"<<endl;
  100.     cout<<"Finished."<<endl;
  101.     cout<<"The string that I wrote is:"<<endl;
  102.     cout<<sequence<<endl;
  103.     this->GDFile.close();
  104.     this->started = false;
  105. }
  106.  
  107. void GDAuto::GetCommand()
  108. {
  109.     //List of commands:
  110.     //help -> Show a list of all commands avaiable
  111.     //rec -> Setup program to record mouse activity
  112.     //exit -> Just quits the program
  113.     bool first_time = true;
  114.     string cmd = "";
  115.  
  116.     while (true)
  117.     {
  118.    
  119.     if (first_time) {
  120.         cout<<"Welcome to Geometry Dash Auto.\nPlease write a command (help for a list of commands)"<<endl;
  121.         cin>>cmd;
  122.     } else {
  123.         cout<<"\nPlease insert a command."<<endl;
  124.         cin>>cmd;
  125.     }
  126.  
  127.     if (cmd=="help")
  128.     {
  129.         cout<<"\nCOMMAND LIST:"<<endl;
  130.         cout<<"help -> Show a list of all commands avaiable;"<<endl;
  131.         cout<<"rec -> Setup program to record mouse activity;"<<endl;
  132.         cout<<"read -> Setup program to read files;"<<endl;
  133.         cout<<"delete -> Delete GDWriter's file if it exists;"<<endl;
  134.         cout<<"exit -> Just quits the program;"<<endl;
  135.     }
  136.     else if (cmd=="rec")
  137.     {
  138.         cout<<"rec command called. Writer is waiting for the toggle key ("<<KEY_TOGGLE<<")."<<endl;
  139.         while (true) {
  140.             if (GetAsyncKeyState(KEY_TOGGLE)) {
  141.                 this->started = true;
  142.                 Sleep(200);
  143.                 break;
  144.             }
  145.         }
  146.         while (started) this->WriteFromInput();
  147.     }
  148.     else if (cmd=="read")
  149.     {
  150.         this->ReadFromFile();
  151.     }
  152.     else if (cmd=="delete")
  153.     {
  154.         if (remove(this->filename.c_str())!=0)
  155.             cout<<"GDWriter's file ("<<filename<<") has been deleted."<<endl;
  156.         else {
  157.             //cout<<"I had some problems deleting GDWriter's file ("<<this->filename<<")."<<endl;
  158.             //cout<<"Maybe filename has not been initialized."<<endl;
  159.         }
  160.     }
  161.     else if (cmd=="exit")
  162.     {
  163.         break;
  164.     }
  165.     else
  166.     {
  167.         cout<<"This command is invalid."<<endl;
  168.     }
  169.  
  170.     cmd = "";
  171.     first_time = false;
  172.  
  173.     }
  174.  
  175.     cout<<"Killing program process..."<<endl;
  176. }
  177.  
  178. void GDAuto::ReadFromFile()
  179. {
  180.     wchar_t* buffer = GetFileName("Open a valid file");
  181.     ifstream gd_file_read (buffer);
  182.     string commands;
  183.     if (gd_file_read.is_open())
  184.     {
  185.         getline(gd_file_read,commands);
  186.         gd_file_read.close();
  187.     } else {cout<<"Unable to open file."<<endl; return;}
  188.  
  189.     vector<string> list_cmd = split(commands, sep);
  190.     int time = 0;
  191.     //mouse_event(MOUSEEVENTF_LEFTDOWN,NULL,NULL,NULL,NULL);
  192.     //mouse_event(MOUSEEVENTF_LEFTUP,NULL,NULL,NULL,NULL);
  193.  
  194.     cout<<"File loaded. Reader is waiting for the toggle key ("<<this->KEY_TOGGLE<<")."<<endl;
  195.  
  196.     while (!GetAsyncKeyState(KEY_TOGGLE)) continue;
  197.  
  198.     cout<<"Toggle on. Waiting for click to begin reading."<<endl;
  199.    
  200.     while (!GetAsyncKeyState(VK_LBUTTON)) continue;
  201.  
  202.     cout<<"Reading..."<<endl;
  203.  
  204.     for (int i=0; i<list_cmd.size(); i++)
  205.     {
  206.         //if (GetAsyncKeyState(KEY_TOGGLE)) break;
  207.  
  208.         if (list_cmd[i][0]==jump)
  209.         {
  210.             time = StringToNumber<int>(list_cmd[i].substr(1,list_cmd[i].length()-1));
  211.             mouse_event(MOUSEEVENTF_LEFTDOWN,NULL,NULL,NULL,NULL);
  212.             Sleep(time);
  213.             mouse_event(MOUSEEVENTF_LEFTUP,NULL,NULL,NULL,NULL);
  214.         }
  215.         else if (list_cmd[i][0]==wait)
  216.         {
  217.             time = StringToNumber<int>(list_cmd[i].substr(1,list_cmd[i].length()-1));
  218.             Sleep(time);
  219.         }
  220.         time = 0;
  221.     }
  222.     cout<<"Reading finished."<<endl;
  223. }
  224.  
  225. wchar_t* GDAuto::GetFileName(const string &prompt)
  226. {
  227.     const int BUFSIZE = 1024;
  228.     char buffer[BUFSIZE] = {0};
  229.  
  230.     wchar_t* wBuffer = new wchar_t[BUFSIZE];
  231.     MultiByteToWideChar(CP_ACP, 0, buffer, -1, wBuffer, BUFSIZE);
  232.  
  233.     wchar_t* wPrompt = new wchar_t[BUFSIZE];
  234.     MultiByteToWideChar(CP_ACP, 0, prompt.c_str(), -1, wPrompt, BUFSIZE);
  235.  
  236.     OPENFILENAME ofns = {0};
  237.     ofns.lStructSize = sizeof(ofns);
  238.     ofns.lpstrFile = wBuffer;
  239.     ofns.nMaxFile = BUFSIZE;
  240.     ofns.lpstrTitle = wPrompt;
  241.     GetOpenFileName(&ofns);
  242.     return wBuffer;
  243. }
  244.  
  245. #pragma endregion
Add Comment
Please, Sign In to add comment