From 8dbef7d7ea77114af164f494850972fff3e81fc3 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sun, 17 Apr 2011 07:04:38 +0000 Subject: [PATCH] - fixed configurator to properly read / write mg ini files --- mk/linux/configuration.xml | 15 ++------ source/configurator/configuration.cpp | 36 +++++++++++++------ source/configurator/configuration.h | 8 +++-- source/shared_lib/include/util/properties.h | 7 ++-- source/shared_lib/sources/util/properties.cpp | 27 +++++++++++--- 5 files changed, 60 insertions(+), 33 deletions(-) diff --git a/mk/linux/configuration.xml b/mk/linux/configuration.xml index 99efe1fc..d36725b1 100644 --- a/mk/linux/configuration.xml +++ b/mk/linux/configuration.xml @@ -1,8 +1,9 @@ - <file-name value="glest.ini"/> - <icon value="true" path="megaglest.ico"/> + <file-name-main value="glest.ini"/> + <file-name value="$HOME/.megaglest/glestuser.ini"/> + <icon value="true" path="glest.ico"/> <field-groups> <field-group name="General"> <field type="Int"> @@ -62,16 +63,6 @@ <min value="0"/> <max value="20"/> </field> - <field type="Enum"> - <name value="Fog of war enabled"/> - <variable-name value="FogOfWar"/> - <description value=""/> - <default value="true"/> - <enums> - <enum value="true"/> - <enum value="false"/> - </enums> - </field> </field-group> <field-group name="Renderer"> diff --git a/source/configurator/configuration.cpp b/source/configurator/configuration.cpp index c2deb1fb..97c89023 100644 --- a/source/configurator/configuration.cpp +++ b/source/configurator/configuration.cpp @@ -4,12 +4,10 @@ #include "xml_parser.h" #include "util.h" -#include "properties.h" #include "conversion.h" using namespace std; using namespace Shared::Xml; -using namespace Shared::Util; namespace Configurator{ @@ -30,10 +28,16 @@ void Configuration::load(const string &path){ throw runtime_error("Cannot find file: " + path); } loadStructure(path); - if(fileExists(fileName) == false) { - throw runtime_error("Cannot find file: " + fileName); + + if(fileExists(fileNameMain) == false) { + throw runtime_error("Cannot find file: " + fileNameMain); + } + loadValues(fileNameMain); + + if(fileExists(fileName) == true) { + // throw runtime_error("Cannot find file: " + fileName); + loadValues(fileName,false); } - loadValues(fileName); } void Configuration::loadStructure(const string &path){ @@ -46,14 +50,20 @@ void Configuration::loadStructure(const string &path){ //title title= configurationNode->getChild("title")->getAttribute("value")->getValue(); + //fileNameMain + fileNameMain= configurationNode->getChild("file-name-main")->getAttribute("value")->getValue(); + Properties::applyTagsToValue(fileNameMain); + //fileName fileName= configurationNode->getChild("file-name")->getAttribute("value")->getValue(); + Properties::applyTagsToValue(fileName); //icon XmlNode *iconNode= configurationNode->getChild("icon"); icon= iconNode->getAttribute("value")->getBoolValue(); if(icon){ iconPath= iconNode->getAttribute("path")->getValue(); + Properties::applyTagsToValue(iconPath); } const XmlNode *fieldGroupsNode= configurationNode->getChild("field-groups"); @@ -68,10 +78,11 @@ void Configuration::loadStructure(const string &path){ } } -void Configuration::loadValues(const string &path){ - Properties properties; +void Configuration::loadValues(const string &path,bool clearCurrentProperties) { + //printf("\nLoading [%s] clearCurrentProperties = %d\n",path.c_str(),clearCurrentProperties); - properties.load(path); + //Properties properties; + properties.load(path,clearCurrentProperties); for(unsigned int i=0; i<fieldGroups.size(); ++i){ FieldGroup *fg= fieldGroups[i]; @@ -82,10 +93,13 @@ void Configuration::loadValues(const string &path){ } } -void Configuration::save(){ - Properties properties; +void Configuration::save() { + //Properties properties; - properties.load(fileName); + //properties.load(fileNameMain); + //if(fileExists(fileName) == true) { + // properties.load(fileName,false); + //} for(unsigned int i=0; i<fieldGroups.size(); ++i){ FieldGroup *fg= fieldGroups[i]; diff --git a/source/configurator/configuration.h b/source/configurator/configuration.h index 21b48b33..2ce1789a 100644 --- a/source/configurator/configuration.h +++ b/source/configurator/configuration.h @@ -3,15 +3,15 @@ #include <string> #include <vector> - #include <wx/wx.h> - +#include "properties.h" #include "xml_parser.h" using std::string; using std::vector; using Shared::Xml::XmlNode; +using namespace Shared::Util; namespace Configurator{ @@ -32,10 +32,12 @@ public: private: string title; + string fileNameMain; string fileName; bool icon; string iconPath; FieldGroups fieldGroups; + Properties properties; public: ~Configuration(); @@ -43,7 +45,7 @@ public: void load(const string &path); void loadStructure(const string &path); - void loadValues(const string &path); + void loadValues(const string &path,bool clearCurrentProperties=true); void save(); diff --git a/source/shared_lib/include/util/properties.h b/source/shared_lib/include/util/properties.h index 946dd453..e2ba7874 100644 --- a/source/shared_lib/include/util/properties.h +++ b/source/shared_lib/include/util/properties.h @@ -30,7 +30,7 @@ namespace Shared{ namespace Util{ /// ini-like file loader // ===================================================== -class Properties{ +class Properties { private: static const int maxLine= 4096; @@ -45,13 +45,12 @@ private: string path; static string applicationPath; - bool applyTagsToValue(string &value); public: static void setApplicationPath(string value) { applicationPath=value; } static string getApplicationPath() { return applicationPath; } void clear(); - void load(const string &path); + void load(const string &path,bool clearCurrentProperties=true); void save(const string &path); int getPropertyCount() const {return (int)propertyVector.size();} @@ -76,6 +75,8 @@ public: void setFloat(const string &key, float value); void setString(const string &key, const string &value); + static bool applyTagsToValue(string &value); + string getpath() const { return path;} string toString(); diff --git a/source/shared_lib/sources/util/properties.cpp b/source/shared_lib/sources/util/properties.cpp index a41440a7..c30106df 100644 --- a/source/shared_lib/sources/util/properties.cpp +++ b/source/shared_lib/sources/util/properties.cpp @@ -37,7 +37,7 @@ string Properties::applicationPath = ""; // class Properties // ===================================================== -void Properties::load(const string &path){ +void Properties::load(const string &path, bool clearCurrentProperties) { ifstream fileStream; char lineBuffer[maxLine]=""; @@ -52,7 +52,9 @@ void Properties::load(const string &path){ throw runtime_error("Can't open propertyMap file: " + path); } - propertyMap.clear(); + if(clearCurrentProperties == true) { + propertyMap.clear(); + } while(!fileStream.eof()){ fileStream.getline(lineBuffer, maxLine); lineBuffer[maxLine-1]='\0'; @@ -76,8 +78,25 @@ void Properties::load(const string &path){ if(applyTagsToValue(value) == true) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Property key [%s] now has value [%s]\n",key.c_str(),value.c_str()); } - propertyMap.insert(PropertyPair(key, value)); - propertyVector.push_back(PropertyPair(key, value)); + + bool replaceExisting = false; + if(propertyMap.find(key) != propertyMap.end()) { + replaceExisting = true; + } + propertyMap[key] = value; + + if(replaceExisting == false) { + propertyVector.push_back(PropertyPair(key, value)); + } + else { + for(unsigned int i = 0; i < propertyVector.size(); ++i) { + PropertyPair ¤tPair = propertyVector[i]; + if(currentPair.first == key) { + currentPair.second = value; + break; + } + } + } } } }