Guest User

https://www.gamingonlinux.com/forum/topic/1689?page=1#r5195

a guest
Aug 5th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.93 KB | None | 0 0
  1. #include <iostream> // cout, cerr
  2. #include <fstream>  // ifstream, ofstream, getline()
  3. #include <cstring>  // strncmp()
  4. #include <unistd.h> // getlogin_r()
  5.  
  6. using namespace std;
  7.  
  8. typedef struct {
  9.     unsigned short right;
  10.     unsigned short throttle;
  11.     unsigned short left;
  12.     unsigned short brake;
  13.     unsigned short frontWeapon1;
  14.     unsigned short frontWeapon2;
  15.     unsigned short frontWeapon3;
  16.     unsigned short extra;
  17.     unsigned short rearWeapon;
  18. } KeySettings;
  19.  
  20. class MiniCarRacingKeyControler {
  21. private:
  22.     string pathToFile;
  23.     string fullData;
  24.  
  25. public:
  26.     MiniCarRacingKeyControler (string pathToFile) : pathToFile(pathToFile) {}
  27.  
  28.     ~MiniCarRacingKeyControler() /* throw() */ {}
  29.  
  30.     void readData () {
  31.         // Getting whole file information.
  32.  
  33.             ifstream file(pathToFile);
  34.  
  35.             if (file.is_open() == false)
  36.                 throw (runtime_error("MiniCarRacingKeyControler::readData(): file " + pathToFile + " cannot be opened for reading!"));
  37.  
  38.             string rawData;
  39.  
  40.             while (getline(file, rawData)) {
  41.                 if (fullData.size() > 0)
  42.                     fullData += '\n';
  43.  
  44.                 fullData += rawData;
  45.             }
  46.  
  47.             file.close();
  48.     }
  49.  
  50.     void saveData () const {
  51.         // Saving full data to file.
  52.  
  53.             ofstream file(pathToFile);
  54.  
  55.             if (file.is_open() == false)
  56.                 throw (runtime_error("MiniCarRacingKeyControler::saveData(): file " + pathToFile + " cannot be opened for writing!"));
  57.  
  58.             file << fullData;
  59.  
  60.             file.close();
  61.     }
  62.  
  63.     void setPlayerControls (const string& playerName, KeySettings keySettings) {
  64.         // We'll have to compare those variables more than 10k times, why not save them to temporaries?
  65.  
  66.             unsigned int fullDataSize = fullData.size();
  67.             const char* playerNameCStr = playerName.c_str();
  68.             unsigned int playerNameSize = playerName.size();
  69.  
  70.         // Searching for byte where player data begins.
  71.  
  72.             unsigned int playerDataIndex = 0;
  73.  
  74.             for (unsigned int i = 0; i < fullDataSize; ++i) {
  75.                 if (!strncmp(playerNameCStr, &fullData[i], playerNameSize)) {
  76.                     playerDataIndex = i;
  77.                     break;
  78.                 }
  79.             }
  80.  
  81.             if (playerDataIndex == 0)
  82.                 throw (runtime_error("MiniCarRacingKeyControler::setPlayerControls(): there is no player called " + playerName + "!"));
  83.  
  84.         // Rewriting keyboard controls bytes.
  85.  
  86.         /*
  87.          *  NAME            BYTE
  88.          *  Right           22
  89.          *  Throttle        34
  90.          *  Left            46
  91.          *  Brake           58
  92.          *  Front Weapon 1  70
  93.          *  Front Weapon 2  82
  94.          *  Front Weapon 3  94
  95.          *  Extra           106
  96.          *  Rear Weapon     118
  97.         */
  98.  
  99.             fullData[playerDataIndex + 21  ] = keySettings.right;
  100.             fullData[playerDataIndex + 33  ] = keySettings.throttle;
  101.             fullData[playerDataIndex + 45  ] = keySettings.left;
  102.             fullData[playerDataIndex + 57  ] = keySettings.brake;
  103.             fullData[playerDataIndex + 69  ] = keySettings.frontWeapon1;
  104.             fullData[playerDataIndex + 81  ] = keySettings.frontWeapon2;
  105.             fullData[playerDataIndex + 93  ] = keySettings.frontWeapon3;
  106.             fullData[playerDataIndex + 105 ] = keySettings.extra;
  107.             fullData[playerDataIndex + 117 ] = keySettings.rearWeapon;
  108.     }
  109.  
  110.     bool checkIfPlayerExists (const string& playerName) {
  111.         unsigned int fullDataSize = fullData.size();
  112.         const char* playerNameCStr = playerName.c_str();
  113.         unsigned int playerNameSize = playerName.size();
  114.  
  115.         for (unsigned int i = 0; i < fullDataSize; ++i) {
  116.             if (!strncmp(playerNameCStr, &fullData[i], playerNameSize)) {
  117.                 return true;
  118.             }
  119.         }
  120.  
  121.         return false;
  122.     }
  123.  
  124.     // Accesor methods.
  125.  
  126.     const string& getPathToFile () const {
  127.         return pathToFile;
  128.     }
  129.  
  130.     void setPathToFile (string& newPathToFile) {
  131.         pathToFile = newPathToFile;
  132.     }
  133.  
  134.     const string& getFullData () const {
  135.         return fullData;
  136.     }
  137.  
  138.  
  139. };
  140.  
  141. int main (int argc, char** argv) {
  142.     // Getting data.
  143.  
  144.         string pathToFile;
  145.  
  146.         if (argc >= 2)
  147.             pathToFile = argv[1];
  148.  
  149.         else {
  150.             char login[100];
  151.  
  152.             getlogin_r(login, 100);
  153.  
  154.             pathToFile = "/home/";
  155.             pathToFile += login;
  156.             pathToFile += "/.wine/drive_c/windows/MiniCarRacing.ini";
  157.         }
  158.  
  159.         MiniCarRacingKeyControler controler(pathToFile);
  160.  
  161.         controler.readData();
  162.  
  163.     // Menu.
  164.  
  165.         string playerName;
  166.  
  167.         cout << "Player name: ";
  168.             cin >> playerName;
  169.  
  170.         if (controler.checkIfPlayerExists(playerName) == false)
  171.             throw (runtime_error("main(): there is no player called " + playerName + "!"));
  172.  
  173.         KeySettings keySettings;
  174.             cout << "Right: ";
  175.                 cin >> keySettings.right;
  176.  
  177.             cout << "Throttle: ";
  178.                 cin >> keySettings.throttle;
  179.  
  180.             cout << "Left: ";
  181.                 cin >> keySettings.left;
  182.  
  183.             cout << "Brake: ";
  184.                 cin >> keySettings.brake;
  185.  
  186.             cout << "Front Weapon 1: ";
  187.                 cin >> keySettings.frontWeapon1;
  188.  
  189.             cout << "Front Weapon 2: ";
  190.                 cin >> keySettings.frontWeapon2;
  191.  
  192.             cout << "Front Weapon 3: ";
  193.                 cin >> keySettings.frontWeapon3;
  194.  
  195.             cout << "Extra: ";
  196.                 cin >> keySettings.extra;
  197.  
  198.             cout << "Rear Weapon: ";
  199.                 cin >> keySettings.rearWeapon;
  200.  
  201.     // Saving controls.
  202.  
  203.         controler.setPlayerControls("Jezor", keySettings);
  204.  
  205.         controler.saveData();
  206.  
  207.     // Exited cleanly.
  208.  
  209.         cout << "Player data saved successfully!" << endl;
  210.  
  211.         return 0;
  212. }
Add Comment
Please, Sign In to add comment