Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream> // cout, cerr
- #include <fstream> // ifstream, ofstream, getline()
- #include <cstring> // strncmp()
- #include <unistd.h> // getlogin_r()
- using namespace std;
- typedef struct {
- unsigned short right;
- unsigned short throttle;
- unsigned short left;
- unsigned short brake;
- unsigned short frontWeapon1;
- unsigned short frontWeapon2;
- unsigned short frontWeapon3;
- unsigned short extra;
- unsigned short rearWeapon;
- } KeySettings;
- class MiniCarRacingKeyControler {
- private:
- string pathToFile;
- string fullData;
- public:
- MiniCarRacingKeyControler (string pathToFile) : pathToFile(pathToFile) {}
- ~MiniCarRacingKeyControler() /* throw() */ {}
- void readData () {
- // Getting whole file information.
- ifstream file(pathToFile);
- if (file.is_open() == false)
- throw (runtime_error("MiniCarRacingKeyControler::readData(): file " + pathToFile + " cannot be opened for reading!"));
- string rawData;
- while (getline(file, rawData)) {
- if (fullData.size() > 0)
- fullData += '\n';
- fullData += rawData;
- }
- file.close();
- }
- void saveData () const {
- // Saving full data to file.
- ofstream file(pathToFile);
- if (file.is_open() == false)
- throw (runtime_error("MiniCarRacingKeyControler::saveData(): file " + pathToFile + " cannot be opened for writing!"));
- file << fullData;
- file.close();
- }
- void setPlayerControls (const string& playerName, KeySettings keySettings) {
- // We'll have to compare those variables more than 10k times, why not save them to temporaries?
- unsigned int fullDataSize = fullData.size();
- const char* playerNameCStr = playerName.c_str();
- unsigned int playerNameSize = playerName.size();
- // Searching for byte where player data begins.
- unsigned int playerDataIndex = 0;
- for (unsigned int i = 0; i < fullDataSize; ++i) {
- if (!strncmp(playerNameCStr, &fullData[i], playerNameSize)) {
- playerDataIndex = i;
- break;
- }
- }
- if (playerDataIndex == 0)
- throw (runtime_error("MiniCarRacingKeyControler::setPlayerControls(): there is no player called " + playerName + "!"));
- // Rewriting keyboard controls bytes.
- /*
- * NAME BYTE
- * Right 22
- * Throttle 34
- * Left 46
- * Brake 58
- * Front Weapon 1 70
- * Front Weapon 2 82
- * Front Weapon 3 94
- * Extra 106
- * Rear Weapon 118
- */
- fullData[playerDataIndex + 21 ] = keySettings.right;
- fullData[playerDataIndex + 33 ] = keySettings.throttle;
- fullData[playerDataIndex + 45 ] = keySettings.left;
- fullData[playerDataIndex + 57 ] = keySettings.brake;
- fullData[playerDataIndex + 69 ] = keySettings.frontWeapon1;
- fullData[playerDataIndex + 81 ] = keySettings.frontWeapon2;
- fullData[playerDataIndex + 93 ] = keySettings.frontWeapon3;
- fullData[playerDataIndex + 105 ] = keySettings.extra;
- fullData[playerDataIndex + 117 ] = keySettings.rearWeapon;
- }
- bool checkIfPlayerExists (const string& playerName) {
- unsigned int fullDataSize = fullData.size();
- const char* playerNameCStr = playerName.c_str();
- unsigned int playerNameSize = playerName.size();
- for (unsigned int i = 0; i < fullDataSize; ++i) {
- if (!strncmp(playerNameCStr, &fullData[i], playerNameSize)) {
- return true;
- }
- }
- return false;
- }
- // Accesor methods.
- const string& getPathToFile () const {
- return pathToFile;
- }
- void setPathToFile (string& newPathToFile) {
- pathToFile = newPathToFile;
- }
- const string& getFullData () const {
- return fullData;
- }
- };
- int main (int argc, char** argv) {
- // Getting data.
- string pathToFile;
- if (argc >= 2)
- pathToFile = argv[1];
- else {
- char login[100];
- getlogin_r(login, 100);
- pathToFile = "/home/";
- pathToFile += login;
- pathToFile += "/.wine/drive_c/windows/MiniCarRacing.ini";
- }
- MiniCarRacingKeyControler controler(pathToFile);
- controler.readData();
- // Menu.
- string playerName;
- cout << "Player name: ";
- cin >> playerName;
- if (controler.checkIfPlayerExists(playerName) == false)
- throw (runtime_error("main(): there is no player called " + playerName + "!"));
- KeySettings keySettings;
- cout << "Right: ";
- cin >> keySettings.right;
- cout << "Throttle: ";
- cin >> keySettings.throttle;
- cout << "Left: ";
- cin >> keySettings.left;
- cout << "Brake: ";
- cin >> keySettings.brake;
- cout << "Front Weapon 1: ";
- cin >> keySettings.frontWeapon1;
- cout << "Front Weapon 2: ";
- cin >> keySettings.frontWeapon2;
- cout << "Front Weapon 3: ";
- cin >> keySettings.frontWeapon3;
- cout << "Extra: ";
- cin >> keySettings.extra;
- cout << "Rear Weapon: ";
- cin >> keySettings.rearWeapon;
- // Saving controls.
- controler.setPlayerControls("Jezor", keySettings);
- controler.saveData();
- // Exited cleanly.
- cout << "Player data saved successfully!" << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment