Advertisement
MrChrHD

ETS2/ATS Trip Exporter (csv) Rev 2

Aug 31st, 2018
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 20.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <exception>
  5. #include <cstdio>
  6. #include <windows.h>
  7. #include <ShellApi.h>
  8. #include <vector>
  9. #include <sstream>
  10. using namespace std;
  11.  
  12. bool Decrypt();
  13. bool Read();
  14. bool CSVExport();
  15. bool IsDecryptorHere();
  16. bool isFileHere(const char* path);
  17.  
  18. //Data Structure
  19. struct trip
  20. {
  21.     string sourceCity;
  22.     string sourceCompany;
  23.     string destinationCity;
  24.     string destinationCompany;
  25.     string cargo ;
  26.     string gainedXP;
  27.     string income;
  28.     string fines;
  29.     string distance;
  30.     string damage;
  31.     string truck;
  32.     string typeContract;
  33. };
  34.  
  35.  
  36. //Global variables
  37. vector<trip> tripList; //List of saved trips
  38.  
  39. /*
  40.     Main. Calls more specific functions and checks for correct execution.
  41.     If execution fails, it will try to call again the function, for a maximum of 3 tries.
  42. */
  43. int main()
  44. {
  45.     //Number of tries made to call a function - Counter
  46.     int nRetries = 0;
  47.     //Boolean checking result of function execution - true = success / false = failure
  48.     bool success = false;
  49.     do
  50.     {
  51.         try
  52.         {
  53.             //Counter update
  54.             nRetries++;
  55.             //Calls function that returns either true or false
  56.             success = Decrypt(); //Decrypt will execute SII Decrypt and create a .txt file. It returns true if everything went right or false if something failed.
  57.             if(success == false)
  58.                 cout << "Error. Retrying." << endl;
  59.         }
  60.         catch(exception& e) //In case of an exception it will terminate the process
  61.         {
  62.             terminate();
  63.         }
  64.     }
  65.     while(nRetries < 3 && success == false);
  66.     //If it didn't work after 3 times the program will exit - Should never be triggered
  67.     if(success == false)
  68.     {
  69.         cout << "Too many errors. The program will now terminate. Press enter to continue..." << endl;
  70.         getchar();
  71.         return 0;
  72.     }
  73.     //if it worked resets counter and status
  74.     else
  75.     {
  76.         nRetries = 0;
  77.         success = false;
  78.         do
  79.         {
  80.             try
  81.             {
  82.                 //Counter update
  83.                 nRetries++;
  84.                 //Calls function that returns either true or false
  85.                 success = Read(); //Read will read the .txt file created in Decrypt function and format all the data.
  86.                 if(success == false)
  87.                     cout << "Error. Retrying." << endl;
  88.             }
  89.             catch(exception& e) //In case of an exception it will terminate the process - Should never be triggered
  90.             {
  91.                 terminate();
  92.             }
  93.         }
  94.         while(nRetries < 3 && success == false);
  95.         //If it didn't work after 3 times the program will exit
  96.         if(success == false)
  97.         {
  98.             cout << "Too many errors. The program will now terminate. Press enter to continue..." << endl;
  99.             getchar();
  100.             return 0;
  101.         }
  102.         //if it worked resets counter and status
  103.         else
  104.         {
  105.             nRetries = 0;
  106.             success = false;
  107.             do
  108.             {
  109.                 try
  110.                 {
  111.                     //Counter update
  112.                     nRetries++;
  113.                     //Calls function that returns either true or false
  114.                     success = CSVExport(); //CSVExport will create a .csv excel file with all the data obtained in Read().
  115.                     if(success == false)
  116.                         cout << "Error. Retrying." << endl;
  117.                 }
  118.                 catch(exception& e) //In case of an exception it will terminate the process - Should never be triggered
  119.                 {
  120.                     terminate();
  121.                 }
  122.             }
  123.             while(nRetries < 3 && success == false);
  124.             //If it didn't work after 3 times the program will exit
  125.             if(success == false)
  126.             {
  127.                 cout << "Too many errors. The program will now terminate. Press enter to continue..." << endl;
  128.                 getchar();
  129.                 return 0;
  130.             }
  131.             //Program executed correctly so no point in leaving it open. :P
  132.             else
  133.             {
  134.                 cout << "Thanks for using this tool. Press enter to continue..." << endl;
  135.                 getchar();
  136.                 return 0;
  137.             }
  138.         }
  139.     }
  140. }
  141.  
  142. /*
  143.     Decrypt. Cheks for SII_Decrypt.exe. Asks user for game.sii directory. Checks input and decrypts file to game.txt.
  144.     In case of errors function terminates back to main.
  145. */
  146. bool Decrypt()
  147. {
  148.     string path = "";
  149.     cout << endl << "---- Step 1 of 3: Decrypt save data ----" << endl;
  150.     cout << "Trying to find SII_Decrypt.exe..." << endl;
  151.     //Checks if SII_Decrypt.exe is available
  152.     bool fileFound = IsDecryptorHere();
  153.     if(fileFound == false)
  154.     {
  155.         //If not returns an error end exits Decrypt() function
  156.         cout << "Error: SII_Decrypt.exe not found." << endl << endl;
  157.         return false;
  158.     }
  159.     else
  160.     {
  161.         //Else asks for game.sii path
  162.         cout << "SII_Decrypt.exe found." << endl << endl;
  163.         do
  164.         {
  165.             cout << "Input required. Please write game.sii directory. Refer to readme.txt about how to get it." << endl << "Your input: ";
  166.             getline(cin,path);
  167.         }
  168.         while(path.empty());
  169.         path = path + "\\game.sii";
  170.         cout << endl << "Checking input..." << endl;
  171.         fileFound = isFileHere(path.c_str()); //Checks if game.sii is there
  172.         if(fileFound == false)
  173.         {
  174.             //If not returns an error end exits Decrypt() function
  175.             cout << "Error: game.sii not found." << endl << endl;
  176.             return false;
  177.         }
  178.         else
  179.         {
  180.             cout << "game.sii found." << endl << endl;
  181.             cout << "Decrypting... please wait until the other console closes itself, and then press enter." << endl;
  182.             string variables = "\"" + path + "\" game.txt";
  183.             ShellExecute(NULL, "open", "SII_Decrypt.exe", variables.c_str(), NULL, SW_SHOWDEFAULT);
  184.             getchar();
  185.             fileFound = isFileHere("game.txt"); //Checks if game.txt exists and therefore if the decrypt process worked fine
  186.             if(fileFound == false)
  187.             {
  188.                 //If not returns an error end exits Decrypt() function
  189.                 cout << "Error: Something went wrong with decryption." << endl << endl;
  190.                 return false;
  191.             }
  192.             else
  193.             {
  194.                 cout << "File decrypted. Step completed. Press enter to continue." << endl << endl;
  195.                 getchar();
  196.                 return true;
  197.             }
  198.         }
  199.     }
  200.  
  201. }
  202.  
  203. /*
  204.     Reads, formats and stores all the data.
  205. */
  206. bool Read()
  207. {
  208.     cout << endl << "---- Step 2 of 3: Read and format data ----" << endl;
  209.     string deliverylog = "";
  210.     ifstream game;
  211.     string temp = "";
  212.     int line1 = 0;
  213.     bool found = false;
  214.     //Open File
  215.     game.open("game.txt");
  216.     while(getline(game, temp))
  217.     {
  218.         line1++;
  219.         //Find ID
  220.         if(temp.find("delivery_log:", 0) != string::npos )
  221.         {
  222.             deliverylog =  temp;
  223.             cout << "Delivery Log ID found: " << deliverylog << " - Line " << line1 << "." << endl;
  224.             found = true;
  225.             break;
  226.         }
  227.     }
  228.     game.close();
  229.     if(found == false) //Error while finding id. It's not possible to get this error (I think).
  230.     {
  231.         cout << "Error. Could not find Delivery Log ID. This should't be possible so it's probably a bug. Please retry. If it doesn't work, send me an email." << endl;
  232.         return false;
  233.     }
  234.     else //Formatting ID and saving array
  235.     {
  236.         //Since the found string is like delivery_log: _nameless.24f.4266.54d0 we have to get the string from the second _ so the program cuts the string twice
  237.         size_t position = deliverylog.find("_");
  238.         deliverylog = deliverylog.substr(position+1);
  239.         position = deliverylog.find("_");
  240.         deliverylog = deliverylog.substr(position);
  241.         cout << "Formatted ID is: " << deliverylog << endl;
  242.  
  243.         found = false; //Check
  244.         string parameter = "delivery_log : " + deliverylog;
  245.  
  246.         //Now the list of the jobs is needed so we must obtain the array from the file. Since it's variable it will be saved in a string vector
  247.         vector<string> jobsIDS;
  248.  
  249.         string formatted = "";
  250.         int line2 = 0;
  251.         game.open("game.txt");
  252.         while(getline(game, temp))
  253.         {
  254.             line2++;
  255.             //Find array
  256.             if(temp.find(parameter, 0) != string::npos && line2 > line1)
  257.             {
  258.                 cout << "Array Found." << endl;
  259.  
  260.                 getline(game, temp); //version, no need so skip
  261.                 getline(game, temp); //entries, more useful
  262.                 if(temp == "entries: 0")
  263.                 {
  264.                     cout << "No trips found." << endl;
  265.                     break;
  266.                 }
  267.                 else
  268.                 {
  269.                     while(getline(game, temp))
  270.                     {
  271.                         if(temp.find("entries[", 0) != string::npos)
  272.                         {
  273.                             position = temp.find("_");
  274.                             formatted = temp.substr(position); //String is like entries[0]: _nameless.24f.4150.fa80 so the program cuts the whole thing from _ to the end of the line
  275.                             cout << "Found: " << formatted << " - Saving!"<< endl;
  276.                             jobsIDS.push_back(formatted); //Save array entry inside vector
  277.                             found = true; //Check
  278.                         }
  279.                     }
  280.                     break;
  281.                 }
  282.             }
  283.         }
  284.         game.close();
  285.         if(found == false)
  286.         {
  287.             return false;
  288.         }
  289.  
  290.  
  291.         bool done = false; //Checks if a job's data is saved
  292.         //After obtaining job ids we have to record their data. as always, it's variable so vector it is!
  293.  
  294.         game.open("game.txt");
  295.         string sourceCity, sourceCompany, destinationCity, destinationCompany, cargo, XP, income, km, damage, fines, truck, type;
  296.         while(getline(game, temp) && jobsIDS.size() > 0)
  297.         {
  298.             if(temp.find("delivery_log_entry : " + jobsIDS.at(0), 0) != string::npos)
  299.             {
  300.                 //After finding the log entry just select things that are needed
  301.  
  302.                 getline(game, temp); // params: 23, useless, so skip
  303.                 while(getline(game, temp))
  304.                 {
  305.                     if(temp.find("params[1]", 0) != string::npos) //Source data
  306.                     {
  307.                         position = temp.find("\"");
  308.                         formatted = temp.substr(position+1);                     //String is like params[1]: "company.volatile.exomar.roma" so the program cuts from p to " inculded
  309.  
  310.                         //Remove company
  311.                         position = formatted.find(".");
  312.                         formatted = formatted.substr(position+1);
  313.                         position = formatted.find("\"");
  314.                         formatted = formatted.substr(0,position); //Remove final "
  315.  
  316.  
  317.                         //remove second entry
  318.                         position = formatted.find(".");
  319.                         formatted = formatted.substr(position+1);
  320.  
  321.                         sourceCompany = formatted; //Save company
  322.  
  323.                         //remove third entry
  324.                         position = formatted.find(".");
  325.                         formatted = formatted.substr(position+1);
  326.                         //Uppercase first letter
  327.                         formatted[0] = toupper(formatted[0]);
  328.  
  329.                         sourceCity = formatted; //Save city
  330.  
  331.                         cout << "Src. comp.: " << sourceCompany << " - Src. city: " << sourceCity << endl;
  332.  
  333.  
  334.                     }
  335.                     if(temp.find("params[2]", 0) != string::npos) //Destination data
  336.                     {
  337.                         position = temp.find("\"");
  338.                         formatted = temp.substr(position+1); //String is like params[1]: "company.volatile.exomar.roma" so the program cuts the whole thing from p to "
  339.  
  340.                         //Remove company
  341.                         position = formatted.find(".");
  342.                         formatted = formatted.substr(position+1);
  343.                         position = formatted.find("\"");
  344.                         formatted = formatted.substr(0,position); //Remove final "
  345.  
  346.                         //remove second entry
  347.                         position = formatted.find(".");
  348.                         formatted = formatted.substr(position+1);
  349.  
  350.                         destinationCompany = formatted; //Save company
  351.  
  352.                         //remove third entry
  353.                         position = formatted.find(".");
  354.                         formatted = formatted.substr(position+1);
  355.  
  356.                         //Uppercase first letter
  357.                         formatted[0] = toupper(formatted[0]);
  358.  
  359.                         destinationCity =  formatted; //Save city
  360.  
  361.                         cout << "Dest. comp.: " << destinationCompany << " - Dest. city: " << destinationCity << endl;
  362.  
  363.                     }
  364.                     if(temp.find("params[3]", 0) != string::npos) //Cargo
  365.                     {
  366.                         position = temp.find("\"");
  367.                         formatted = temp.substr(position+1);
  368.  
  369.                         //Remove cargo
  370.                         position = formatted.find(".");
  371.                         formatted = formatted.substr(position+1);
  372.                         position = formatted.find("\"");
  373.                         formatted = formatted.substr(0,position); //Remove final "
  374.                         cargo = formatted;
  375.                         cout << "Cargo: " << cargo << endl;
  376.  
  377.                     }
  378.                     if(temp.find("params[4]", 0) != string::npos) //XP
  379.                     {
  380.                         position = temp.find(": ");
  381.                         formatted = temp.substr(position+1);
  382.                         XP = formatted;
  383.                         cout << "XP: " << XP << endl;
  384.                     }
  385.                     if(temp.find("params[5]", 0) != string::npos) //Income
  386.                     {
  387.                         position = temp.find(": ");
  388.                         formatted = temp.substr(position+1);
  389.                         income = formatted;
  390.                         cout << "Income: " << income << endl;
  391.                     }
  392.                     if(temp.find("params[6]", 0) != string::npos) //Distance
  393.                     {
  394.                         position = temp.find(": ");
  395.                         formatted = temp.substr(position+1);
  396.                         km = formatted;
  397.                         cout << "Distance: " << km << endl;
  398.                     }
  399.                     if(temp.find("params[7]", 0) != string::npos) //Cargo damage
  400.                     {
  401.                         position = temp.find("\"");
  402.                         formatted = temp.substr(position+1);
  403.                         position = formatted.find("\"");
  404.                         formatted = formatted.substr(0,position); //Remove final "
  405.                         //Damage formatting
  406.                         float temp1 = stof(formatted); //converts string to float 0.061
  407.                         temp1 = temp1 * 100; //6.1
  408.                         ostringstream convert;
  409.                         convert << temp1;
  410.  
  411.                         formatted = convert.str(); //Puts back into string
  412.  
  413.                         damage = formatted + "%"; //Final formatting
  414.                         cout << "Cargo damage: " << damage << endl;
  415.                     }
  416.                     if(temp.find("params[14]", 0) != string::npos) //Fines
  417.                     {
  418.                         position = temp.find(": ");
  419.                         formatted = temp.substr(position+1);
  420.                         fines = formatted;
  421.                         cout << "Total fines: " << fines << endl;
  422.                     }
  423.                     if(temp.find("params[16]", 0) != string::npos) //Truck
  424.                     {
  425.                         position = temp.find("\"");
  426.                         formatted = temp.substr(position+1, temp.length()-1); //String is like params[1]: "vehicle.iveco.strails" so the program cuts the whole thing from v to s
  427.  
  428.                         //Remove vehicle
  429.                         position = formatted.find(".");
  430.                         formatted = formatted.substr(position+1);
  431.                         position = formatted.find("\"");
  432.                         formatted = formatted.substr(0,position); //Remove final "
  433.  
  434.                         truck = formatted;
  435.  
  436.                         cout << "truck: " << truck << endl;
  437.  
  438.                     }
  439.                     if(temp.find("params[18]", 0) != string::npos) //Cargo Type
  440.                     {
  441.                         position = temp.find(": ");
  442.                         formatted = temp.substr(position+1);
  443.  
  444.                         if(formatted == " quick")
  445.                         {
  446.                             formatted = "Quick Job";
  447.                         }
  448.                         if(formatted == " compn")
  449.                         {
  450.                             formatted = "Freight Market";
  451.                         }
  452.                         if(formatted == " on_compn")
  453.                         {
  454.                             formatted = "External Contract (World of Trucks)";
  455.                         }
  456.                         if(formatted == " freerm")
  457.                         {
  458.                             formatted = "Free Roam";
  459.                             sourceCompany = "Free Roam";
  460.                             sourceCity = "Free Roam";
  461.                             destinationCity = "Free Roam";
  462.                             destinationCompany = "Free Roam";
  463.                             damage = "0%"; //Bugged better set it to 0%
  464.                             truck = "Free Roam";
  465.                         }
  466.  
  467.                         type = formatted;
  468.                         cout << "Cargo Type: " << type << endl;
  469.  
  470.                         //Other ways crashed so yay
  471.                         trip temporary = (trip)
  472.                         {
  473.                             sourceCity, sourceCompany, destinationCity, destinationCompany, cargo, XP, income, fines, km, damage, truck, type
  474.                         };
  475.                         tripList.push_back(temporary);
  476.                         done = true;
  477.                     }
  478.                     if(done == true) //Checks if it's done with a job
  479.                     {
  480.                         break;
  481.                     }
  482.                 }
  483.                 jobsIDS.erase(jobsIDS.begin());
  484.                 done = false;
  485.             }
  486.         }
  487.         game.close();
  488.     }
  489.     return true;
  490. }
  491.  
  492. /*
  493.     The data is written in a .csv file
  494. */
  495. bool CSVExport()
  496. {
  497.     cout << endl << "---- Step 3 of 3: Export data to CSV ----" << endl;
  498.     ofstream csv;
  499.     csv.open("Trips.csv");
  500.     cout << "Writing data..." << endl;
  501.     csv << "Source City; Source Company; Destination City; Destination Company; Cargo; Cargo Damage; Gained XP; Income; Fines; Distance; Truck; Cargo Type;\n";
  502.     csv << "\n";
  503.     while(tripList.size() > 0)
  504.     {
  505.         csv << tripList.at(0).sourceCity << ";" << tripList.at(0).sourceCompany << ";" << tripList.at(0).destinationCity << ";" << tripList.at(0).destinationCompany << ";" << tripList.at(0).cargo  << ";" << tripList.at(0).damage << ";" << tripList.at(0).gainedXP << ";" << tripList.at(0).income << ";" << tripList.at(0).fines << ";" << tripList.at(0).distance << ";" << tripList.at(0).truck << ";" << tripList.at(0).typeContract << ";\n";
  506.         tripList.erase(tripList.begin());
  507.     }
  508.  
  509.     cout << "Done." << endl;
  510.     csv.close();
  511.     return true;
  512. }
  513.  
  514. /*
  515.     Checks if SII_Decrypt.exe is in this program's folder
  516. */
  517. bool IsDecryptorHere()
  518. {
  519.     ifstream decryptor("SII_Decrypt.exe");
  520.     return decryptor.good();
  521. }
  522.  
  523. /*
  524.     Checks if file exists
  525. */
  526. bool isFileHere(const char* path)
  527. {
  528.     ifstream file(path);
  529.     return file.good();
  530. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement