From ada8810666e32d40f970ca86bd4558d1fdd5f241 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 3 Oct 2013 14:51:26 +0000 Subject: [PATCH] added support for properly named attack-strength tag (and keep support for old name) --- source/glest_game/types/skill_type.cpp | 5 +- source/glest_game/types/upgrade_type.cpp | 12 +++-- source/shared_lib/include/xml/xml_parser.h | 2 + source/shared_lib/sources/xml/xml_parser.cpp | 56 ++++++++++++++++---- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/source/glest_game/types/skill_type.cpp b/source/glest_game/types/skill_type.cpp index 8b98fb16..e9db5b39 100644 --- a/source/glest_game/types/skill_type.cpp +++ b/source/glest_game/types/skill_type.cpp @@ -762,7 +762,10 @@ void AttackSkillType::load(const XmlNode *sn, const XmlNode *attackBoostsNode, endPathWithSlash(currentPath); //misc - attackStrength= sn->getChild("attack-strenght")->getAttribute("value")->getIntValue(); + std::vector attackStrengthXMLTags; + attackStrengthXMLTags.push_back("attack-strenght"); + attackStrengthXMLTags.push_back("attack-strength"); + attackStrength= sn->getChildWithAliases(attackStrengthXMLTags)->getAttribute("value")->getIntValue(); attackVar= sn->getChild("attack-var")->getAttribute("value")->getIntValue(); if(attackVar < 0) { diff --git a/source/glest_game/types/upgrade_type.cpp b/source/glest_game/types/upgrade_type.cpp index 4ce72ad8..30cdb647 100644 --- a/source/glest_game/types/upgrade_type.cpp +++ b/source/glest_game/types/upgrade_type.cpp @@ -102,10 +102,14 @@ void UpgradeTypeBase::load(const XmlNode *upgradeNode, string upgradename) { } attackStrengthIsMultiplier = false; - if(upgradeNode->hasChild("attack-strenght") == true) { - attackStrength= upgradeNode->getChild("attack-strenght")->getAttribute("value")->getIntValue(); - if(upgradeNode->getChild("attack-strenght")->getAttribute(VALUE_PERCENT_MULTIPLIER_KEY_NAME,false) != NULL) { - attackStrengthIsMultiplier = upgradeNode->getChild("attack-strenght")->getAttribute(VALUE_PERCENT_MULTIPLIER_KEY_NAME)->getBoolValue(); + + std::vector attackStrengthXMLTags; + attackStrengthXMLTags.push_back("attack-strenght"); + attackStrengthXMLTags.push_back("attack-strength"); + if(upgradeNode->hasChildWithAliases(attackStrengthXMLTags) == true) { + attackStrength= upgradeNode->getChildWithAliases(attackStrengthXMLTags)->getAttribute("value")->getIntValue(); + if(upgradeNode->getChildWithAliases(attackStrengthXMLTags)->getAttribute(VALUE_PERCENT_MULTIPLIER_KEY_NAME,false) != NULL) { + attackStrengthIsMultiplier = upgradeNode->getChildWithAliases(attackStrengthXMLTags)->getAttribute(VALUE_PERCENT_MULTIPLIER_KEY_NAME)->getBoolValue(); //printf("Found attackStrengthIsMultiplier = %d\n",attackStrengthIsMultiplier); } diff --git a/source/shared_lib/include/xml/xml_parser.h b/source/shared_lib/include/xml/xml_parser.h index 8cd8887b..34713936 100644 --- a/source/shared_lib/include/xml/xml_parser.h +++ b/source/shared_lib/include/xml/xml_parser.h @@ -178,9 +178,11 @@ public: XmlNode *getChild(unsigned int i) const; XmlNode *getChild(const string &childName, unsigned int childIndex=0) const; + XmlNode *getChildWithAliases(vector childNameList, unsigned int childIndex=0) const; vector getChildList(const string &childName) const; bool hasChildAtIndex(const string &childName, int childIndex=0) const; bool hasChild(const string &childName) const; + bool hasChildWithAliases(vector childNameList) const; int clearChild(const string &childName); diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index be3a90ae..c0ad523e 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -749,10 +749,11 @@ vector XmlNode::getChildList(const string &childName) const { return list; } -XmlNode *XmlNode::getChild(const string &childName, unsigned int i) const{ - if(superNode && !hasChildNoSuper(childName)) +XmlNode *XmlNode::getChild(const string &childName, unsigned int i) const { + if(superNode && hasChildNoSuper(childName) == false) { return superNode->getChild(childName,i); - if(i>=children.size()){ + } + if(i >= children.size()) { throw megaglest_runtime_error("\"" + name + "\" node doesn't have "+intToStr(i+1)+" children named \"" + childName + "\"\n\nTree: "+getTreeString()); } @@ -769,6 +770,39 @@ XmlNode *XmlNode::getChild(const string &childName, unsigned int i) const{ throw megaglest_runtime_error("Node \""+getName()+"\" doesn't have "+intToStr(i+1)+" children named \""+childName+"\"\n\nTree: "+getTreeString()); } +bool XmlNode::hasChildNoSuper(const string &childName) const { + //int count= 0; + for(unsigned int j = 0; j < children.size(); ++j) { + if(children[j]->getName() == childName) { + return true; + } + } + return false; +} +XmlNode * XmlNode::getChildWithAliases(vector childNameList, unsigned int childIndex) const { + for(int aliasIndex = 0; aliasIndex < childNameList.size(); ++aliasIndex) { + const string &childName = childNameList[aliasIndex]; + if(superNode && hasChildNoSuper(childName) == false) { + return superNode->getChild(childName,childIndex); + } + if(childIndex >= children.size()) { + throw megaglest_runtime_error("\"" + name + "\" node doesn't have "+intToStr(childIndex+1)+" children named \"" + childName + "\"\n\nTree: "+getTreeString()); + } + + int count= 0; + for(unsigned int j = 0; j < children.size(); ++j) { + if(children[j]->getName() == childName) { + if(count == childIndex) { + return children[j]; + } + count++; + } + } + } + + throw megaglest_runtime_error("Node \""+getName()+"\" doesn't have "+intToStr(childIndex+1)+" children named \""+ (childNameList.empty() ? "???" : childNameList[0]) +"\"\n\nTree: "+getTreeString()); +} + bool XmlNode::hasChildAtIndex(const string &childName, int i) const { if(superNode && !hasChildNoSuper(childName)) return superNode->hasChildAtIndex(childName,i); @@ -789,15 +823,17 @@ bool XmlNode::hasChildAtIndex(const string &childName, int i) const { bool XmlNode::hasChild(const string &childName) const { return hasChildNoSuper(childName) || (superNode && superNode->hasChild(childName)); } - -bool XmlNode::hasChildNoSuper(const string &childName) const { - //int count= 0; - for(unsigned int j = 0; j < children.size(); ++j) { - if(children[j]->getName() == childName) { - return true; + +bool XmlNode::hasChildWithAliases(vector childNameList) const { + bool result = false; + for(int aliasIndex = 0; aliasIndex < childNameList.size(); ++aliasIndex) { + const string &childName = childNameList[aliasIndex]; + result = hasChild(childName); + if(result == true) { + break; } } - return false; + return result; } XmlNode *XmlNode::addChild(const string &name, const string text) {