- fixed configurator to properly read / write mg ini files

This commit is contained in:
Mark Vejvoda 2011-04-17 07:04:38 +00:00
parent 734db6c7d4
commit 8dbef7d7ea
5 changed files with 60 additions and 33 deletions

View File

@ -1,8 +1,9 @@
<?xml version="1.0" standalone="yes"?> <?xml version="1.0" standalone="yes"?>
<configuration> <configuration>
<title value="Mega Glest"/> <title value="Mega Glest"/>
<file-name value="glest.ini"/> <file-name-main value="glest.ini"/>
<icon value="true" path="megaglest.ico"/> <file-name value="$HOME/.megaglest/glestuser.ini"/>
<icon value="true" path="glest.ico"/>
<field-groups> <field-groups>
<field-group name="General"> <field-group name="General">
<field type="Int"> <field type="Int">
@ -62,16 +63,6 @@
<min value="0"/> <min value="0"/>
<max value="20"/> <max value="20"/>
</field> </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>
<field-group name="Renderer"> <field-group name="Renderer">

View File

@ -4,12 +4,10 @@
#include "xml_parser.h" #include "xml_parser.h"
#include "util.h" #include "util.h"
#include "properties.h"
#include "conversion.h" #include "conversion.h"
using namespace std; using namespace std;
using namespace Shared::Xml; using namespace Shared::Xml;
using namespace Shared::Util;
namespace Configurator{ namespace Configurator{
@ -30,10 +28,16 @@ void Configuration::load(const string &path){
throw runtime_error("Cannot find file: " + path); throw runtime_error("Cannot find file: " + path);
} }
loadStructure(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){ void Configuration::loadStructure(const string &path){
@ -46,14 +50,20 @@ void Configuration::loadStructure(const string &path){
//title //title
title= configurationNode->getChild("title")->getAttribute("value")->getValue(); title= configurationNode->getChild("title")->getAttribute("value")->getValue();
//fileNameMain
fileNameMain= configurationNode->getChild("file-name-main")->getAttribute("value")->getValue();
Properties::applyTagsToValue(fileNameMain);
//fileName //fileName
fileName= configurationNode->getChild("file-name")->getAttribute("value")->getValue(); fileName= configurationNode->getChild("file-name")->getAttribute("value")->getValue();
Properties::applyTagsToValue(fileName);
//icon //icon
XmlNode *iconNode= configurationNode->getChild("icon"); XmlNode *iconNode= configurationNode->getChild("icon");
icon= iconNode->getAttribute("value")->getBoolValue(); icon= iconNode->getAttribute("value")->getBoolValue();
if(icon){ if(icon){
iconPath= iconNode->getAttribute("path")->getValue(); iconPath= iconNode->getAttribute("path")->getValue();
Properties::applyTagsToValue(iconPath);
} }
const XmlNode *fieldGroupsNode= configurationNode->getChild("field-groups"); const XmlNode *fieldGroupsNode= configurationNode->getChild("field-groups");
@ -68,10 +78,11 @@ void Configuration::loadStructure(const string &path){
} }
} }
void Configuration::loadValues(const string &path){ void Configuration::loadValues(const string &path,bool clearCurrentProperties) {
Properties properties; //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){ for(unsigned int i=0; i<fieldGroups.size(); ++i){
FieldGroup *fg= fieldGroups[i]; FieldGroup *fg= fieldGroups[i];
@ -82,10 +93,13 @@ void Configuration::loadValues(const string &path){
} }
} }
void Configuration::save(){ void Configuration::save() {
Properties properties; //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){ for(unsigned int i=0; i<fieldGroups.size(); ++i){
FieldGroup *fg= fieldGroups[i]; FieldGroup *fg= fieldGroups[i];

View File

@ -3,15 +3,15 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <wx/wx.h> #include <wx/wx.h>
#include "properties.h"
#include "xml_parser.h" #include "xml_parser.h"
using std::string; using std::string;
using std::vector; using std::vector;
using Shared::Xml::XmlNode; using Shared::Xml::XmlNode;
using namespace Shared::Util;
namespace Configurator{ namespace Configurator{
@ -32,10 +32,12 @@ public:
private: private:
string title; string title;
string fileNameMain;
string fileName; string fileName;
bool icon; bool icon;
string iconPath; string iconPath;
FieldGroups fieldGroups; FieldGroups fieldGroups;
Properties properties;
public: public:
~Configuration(); ~Configuration();
@ -43,7 +45,7 @@ public:
void load(const string &path); void load(const string &path);
void loadStructure(const string &path); void loadStructure(const string &path);
void loadValues(const string &path); void loadValues(const string &path,bool clearCurrentProperties=true);
void save(); void save();

View File

@ -30,7 +30,7 @@ namespace Shared{ namespace Util{
/// ini-like file loader /// ini-like file loader
// ===================================================== // =====================================================
class Properties{ class Properties {
private: private:
static const int maxLine= 4096; static const int maxLine= 4096;
@ -45,13 +45,12 @@ private:
string path; string path;
static string applicationPath; static string applicationPath;
bool applyTagsToValue(string &value);
public: public:
static void setApplicationPath(string value) { applicationPath=value; } static void setApplicationPath(string value) { applicationPath=value; }
static string getApplicationPath() { return applicationPath; } static string getApplicationPath() { return applicationPath; }
void clear(); void clear();
void load(const string &path); void load(const string &path,bool clearCurrentProperties=true);
void save(const string &path); void save(const string &path);
int getPropertyCount() const {return (int)propertyVector.size();} int getPropertyCount() const {return (int)propertyVector.size();}
@ -76,6 +75,8 @@ public:
void setFloat(const string &key, float value); void setFloat(const string &key, float value);
void setString(const string &key, const string &value); void setString(const string &key, const string &value);
static bool applyTagsToValue(string &value);
string getpath() const { return path;} string getpath() const { return path;}
string toString(); string toString();

View File

@ -37,7 +37,7 @@ string Properties::applicationPath = "";
// class Properties // class Properties
// ===================================================== // =====================================================
void Properties::load(const string &path){ void Properties::load(const string &path, bool clearCurrentProperties) {
ifstream fileStream; ifstream fileStream;
char lineBuffer[maxLine]=""; char lineBuffer[maxLine]="";
@ -52,7 +52,9 @@ void Properties::load(const string &path){
throw runtime_error("Can't open propertyMap file: " + path); throw runtime_error("Can't open propertyMap file: " + path);
} }
propertyMap.clear(); if(clearCurrentProperties == true) {
propertyMap.clear();
}
while(!fileStream.eof()){ while(!fileStream.eof()){
fileStream.getline(lineBuffer, maxLine); fileStream.getline(lineBuffer, maxLine);
lineBuffer[maxLine-1]='\0'; lineBuffer[maxLine-1]='\0';
@ -76,8 +78,25 @@ void Properties::load(const string &path){
if(applyTagsToValue(value) == true) { if(applyTagsToValue(value) == true) {
if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Property key [%s] now has value [%s]\n",key.c_str(),value.c_str()); 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 &currentPair = propertyVector[i];
if(currentPair.first == key) {
currentPair.second = value;
break;
}
}
}
} }
} }
} }