bool VConfigManager::writeDirectoryConfig(const QString &path, const QJsonObject &configJson) { QString configFile = fetchDirConfigFilePath(path); QFile config(configFile); // We use Unix LF for config file. if (!config.open(QIODevice::WriteOnly)) { qWarning() << "fail to open directory configuration file for write:" << configFile; return false; } QJsonDocument configDoc(configJson); config.write(configDoc.toJson()); return true; }
bool COptions::LoadConfiguration (void) { // Try to open the old configuration file FILE* pConfigFile = fopen( oldconfigFileName.c_str(), "rb" ); // If the old configuration file exists if ( pConfigFile != NULL ) { theLog.WriteLine( "Options => Note: Reading old configuration file (config.dat) values now. Once the XML file is written, you may delete the old configuration file." ); // Read each configuration value in the file fread(&m_TimeUpMinutes, sizeof(int), 1, pConfigFile); fread(&m_TimeUpSeconds, sizeof(int), 1, pConfigFile); fread(&m_TimeStartMinutes, sizeof(int), 1, pConfigFile); fread(&m_TimeStartSeconds, sizeof(int), 1, pConfigFile); fread(&m_DisplayMode, sizeof(EDisplayMode), 1, pConfigFile); fread(&m_BattleCount, sizeof(int), 1, pConfigFile); fread(m_BomberType, sizeof(EBomberType), MAX_PLAYERS, pConfigFile); fread(m_PlayerInput, sizeof(int), MAX_PLAYERS, pConfigFile); fread(m_Control, sizeof(int), MAX_PLAYER_INPUT * NUM_CONTROLS, pConfigFile); fread(&m_Level, sizeof(int), 1, pConfigFile); // The configuration file is not needed anymore fclose( pConfigFile ); } TiXmlDocument configDoc( configFileName ); // Try to load XML file if ( configDoc.LoadFile() ) { // The file could be loaded successfully int tempRevision = 0; TiXmlHandle configHandle( &configDoc ); TiXmlElement *confRevision = configHandle.FirstChild( "Bombermaaan" ).FirstChild( "Configuration" ).FirstChild( "ConfigRevision" ).ToElement(); if ( confRevision ) confRevision->QueryIntAttribute( "value", &tempRevision ); theLog.WriteLine( "Options => Configuration file was successfully loaded and is at revision %d.", tempRevision ); ReadIntFromXML( configDoc, "TimeUp", "minutes", &m_TimeUpMinutes ); ReadIntFromXML( configDoc, "TimeUp", "seconds", &m_TimeUpSeconds ); ReadIntFromXML( configDoc, "TimeStart", "minutes", &m_TimeStartMinutes ); ReadIntFromXML( configDoc, "TimeStart", "seconds", &m_TimeStartSeconds ); ReadIntFromXML(configDoc, "BattleMode", "value", (int*)&m_BattleMode); ReadIntFromXML( configDoc, "BattleCount", "value", &m_BattleCount ); ReadIntFromXML( configDoc, "LevelFileNumber", "value", &m_Level ); ReadIntFromXML( configDoc, "DisplayMode", "value", (int*) &m_DisplayMode ); for ( int i = 0; i < MAX_PLAYERS; i++ ) { std::ostringstream oss; oss << "bomber" << i; std::string attributeName = oss.str(); ReadIntFromXML( configDoc, "BomberTypes", attributeName, (int*) (&m_BomberType[i]) ); ReadIntFromXML(configDoc, "BomberTeams", attributeName, (int*)(&m_BomberTeam[i])); ReadIntFromXML(configDoc, "PlayerInputs", attributeName, (int*)(&m_PlayerInput[i])); } // // Read the control settings // List of input devices (keyboard n, joystick n) -> Control (up, down, ..., action1, action2) -> Key/Button // // Create a handle to the XML document TiXmlHandle handle( &configDoc ); // Fetch the element TiXmlElement *element = handle.FirstChild( "Bombermaaan" ).FirstChild( "Configuration" ).FirstChild( "ControlList" ).FirstChild( "Control" ).ToElement(); // If the element exists, go on if ( element ) { // Loop through all sub-elements of the ControlList node for ( ; element; element = element->NextSiblingElement() ) { int id = -1; element->QueryIntAttribute( "id", &id ); // The id must be between 0 and MAX_PLAYER_INPUT if ( id < 0 || id >= MAX_PLAYER_INPUT ) continue; // Read all control values (up, down, left, right, action1, action2) for ( unsigned int ctrl = 0; ctrl < NUM_CONTROLS; ctrl++ ) { int ctrldata = -1; std::ostringstream oss; oss << "control" << ctrl; std::string attributeName = oss.str(); element->QueryIntAttribute( attributeName, &ctrldata); // Verify we have read a valid number if ( ctrldata >= 0 ) { m_Control[id][ctrl] = ctrldata; } } } } } else { // The configuration could not be loaded, maybe it doesn't exist theLog.WriteLine ("Options => Configuration file could not be loaded." ); } //! We always return true since it doesn't matter if the configuration file could not be loaded // Success return true; }
bool ConfigParser::parseRov(QROV& rov) const { QByteArray fileContents = readFile(); if(fileContents.isEmpty()) return false; QJsonDocument configDoc(QJsonDocument::fromJson(fileContents)); QJsonObject baseObj = configDoc.object(); QJsonArray jsonSensors = baseObj["sensors"].toArray(); QJsonArray jsonRelays = baseObj["relays"].toArray(); QJsonArray jsonServos = baseObj["servos"].toArray(); QJsonValue jsonMotorLayout = baseObj["motorLayout"]; QJsonArray jsonMotorGears = baseObj["motorGears"].toArray(); QJsonValue jsonMaxDepth = baseObj["maxDepth"]; QJsonValue jsonGearIncButton = baseObj["gearIncButton"]; QJsonValue jsonGearDecButton = baseObj["gearDecButton"]; if(jsonSensors.size() == 0 || jsonRelays.size() == 0 || jsonServos.size() == 0 || jsonMotorLayout.isUndefined() || jsonMaxDepth.isUndefined()) return false; //TODO: READ IP VIDEO FEEDS? rov.relays.clear(); rov.servos.clear(); rov.sensors.clear(); rov.motors.clear(); rov.motorGears.clear(); for(int i=0; i<jsonRelays.count(); i++) { if(!jsonRelays[i].toObject().contains("name")) return false; rov.relays.append(QROVRelay(jsonRelays[i].toObject()["name"].toString(), false)); } for(int i=0; i<jsonServos.count(); i++) { if(!jsonServos[i].toObject().contains("name") || !jsonServos[i].toObject().contains("min") || !jsonServos[i].toObject().contains("max") || !jsonServos[i].toObject().contains("defaultValue")) return false; const int defVal = jsonServos[i].toObject()["defaultValue"].toInt(); rov.servos.append(QROVServo(jsonServos[i].toObject()["name"].toString(), defVal, jsonServos[i].toObject()["min"].toInt(), jsonServos[i].toObject()["max"].toInt(), defVal)); } for(int i=0; i<jsonSensors.count(); i++) { if(!jsonSensors[i].toObject().contains("units") || !jsonSensors[i].toObject().contains("name")) return false; rov.sensors.append(QROVSensor(jsonSensors[i].toObject()["name"].toString(), jsonSensors[i].toObject()["units"].toString(), 0)); } if(jsonMotorLayout.toString() == "vector") { rov.motorLayout = vectorDrive; for(int i=0; i<6; i++) { rov.motors.append(QROVMotor(1500)); } } else if(jsonMotorLayout.toString() == "tank") { rov.motorLayout = tankDrive; for(int i=0; i<3; i++) { rov.motors.append(QROVMotor(1500)); } } else { qWarning() << "Motor layout: " << jsonMotorLayout.toString() << " not defined!"; return false; } rov.motorGears.append(0); // in this gear, disable the ROV for(int i=0; i<jsonMotorGears.count(); i++) { rov.motorGears.append(jsonMotorGears[i].toDouble()); } if(rov.motorGears.length() < 1) { rov.motorGears.append(1.0); } rov.maxDepth = jsonMaxDepth.toDouble(100); rov.gearIncButton = jsonGearIncButton.toInt(0); rov.gearDecButton = jsonGearDecButton.toInt(0); return true; }