From 5249ecc45d3e2bf8313135787eb994407f035892 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 13 Nov 2013 22:28:05 +0000 Subject: [PATCH] some code cleanup for xml parsing and loading saved games --- source/glest_game/game/game.cpp | 2 -- .../glest_game/menu/menu_state_load_game.cpp | 12 ++++++++++- source/shared_lib/include/xml/xml_parser.h | 1 - source/shared_lib/sources/xml/xml_parser.cpp | 21 ++++++------------- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index a8346b2b..620a7bdf 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -6457,8 +6457,6 @@ string Game::saveGame(string name, string path) { infoNode->addAttribute("color",info.color.getString(), mapTagReplacements); } - gameNode->addAttribute("timeDisplay",intToStr(timeDisplay), mapTagReplacements); - gameNode->addAttribute("disableSpeedChange",intToStr(disableSpeedChange), mapTagReplacements); xmlTree.save(saveGameFile); diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index 2e5cc0f0..1ff5c82d 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -287,17 +287,27 @@ void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton){ if(fileExists(filename) == true) { Lang &lang= Lang::getInstance(); + // Xerces is infinitely slower than rapidxml + // XmlTree xmlTree(XML_XERCES_ENGINE); XmlTree xmlTree(XML_RAPIDXML_ENGINE); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Before load of XML\n"); std::map mapExtraTagReplacementValues; xmlTree.load(filename, Properties::getTagReplacementValues(&mapExtraTagReplacementValues),true); if(SystemFlags::VERBOSE_MODE_ENABLED) printf("After load of XML\n"); const XmlNode *rootNode= xmlTree.getRootNode(); - if(rootNode->hasChild("megaglest-saved-game") == true) { + if(rootNode != NULL && rootNode->hasChild("megaglest-saved-game") == true) { rootNode = rootNode->getChild("megaglest-saved-game"); } + if(rootNode == NULL) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"Invalid XML saved game file: [%s]",filename.c_str()); + infoTextLabel.setText(szBuf); + return; + } + const XmlNode *versionNode= rootNode; string gameVer = versionNode->getAttribute("version")->getValue(); if(gameVer != glestVersionString && checkVersionComptability(gameVer, glestVersionString) == false) { diff --git a/source/shared_lib/include/xml/xml_parser.h b/source/shared_lib/include/xml/xml_parser.h index 34713936..baf3b9dc 100644 --- a/source/shared_lib/include/xml/xml_parser.h +++ b/source/shared_lib/include/xml/xml_parser.h @@ -97,7 +97,6 @@ public: class XmlIoRapid { private: static bool initialized; - xml_document<> *doc; private: XmlIoRapid(); diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 6ebe3dc0..3f2fd4d8 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -289,22 +289,12 @@ XmlIoRapid::XmlIoRapid() { void XmlIoRapid::init() { try{ - //printf("XmlIo init\n"); - XmlIoRapid::initialized= true; } catch(const exception &e){ SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error initializing XML system, msg %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); throw megaglest_runtime_error("Error initializing XML system"); } - - try { - doc = new xml_document<>(); - } - catch(const DOMException &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Exception while creating XML parser, msg: %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.getMessage()); - throw megaglest_runtime_error("Exception while creating XML parser"); - } } bool XmlIoRapid::isInitialized() { @@ -323,9 +313,6 @@ XmlIoRapid &XmlIoRapid::getInstance() { void XmlIoRapid::cleanup() { if(XmlIoRapid::initialized == true) { XmlIoRapid::initialized= false; - //printf("XmlIo cleanup\n"); - delete doc; - doc = NULL; } } @@ -394,11 +381,12 @@ XmlNode *XmlIoRapid::load(const string &path, const std::map &map if(showPerfStats) printf("In [%s::%s Line: %d] took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - doc->parse(&buffer.front()); + xml_document<> doc; + doc.parse(&buffer.front()); if(showPerfStats) printf("In [%s::%s Line: %d] took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - rootNode= new XmlNode(doc->first_node(),mapTagReplacementValues); + rootNode= new XmlNode(doc.first_node(),mapTagReplacementValues); if(showPerfStats) printf("In [%s::%s Line: %d] took msecs: " MG_I64_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); @@ -521,6 +509,8 @@ static string loadStackCacheName = string(__FILE__) + string("_loadStackCacheNam void XmlTree::load(const string &path, const std::map &mapTagReplacementValues, bool noValidation,bool skipStackCheck) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s] skipStackCheck = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str(),skipStackCheck); + clearRootNode(); + this->skipStackCheck = skipStackCheck; if(this->skipStackCheck == false) { //printf("XmlTree::load p [%p]\n",this); @@ -540,6 +530,7 @@ void XmlTree::load(const string &path, const std::map &mapTagRepl } loadPath = path; + if(this->engine_type == XML_XERCES_ENGINE) { this->rootNode= XmlIo::getInstance().load(path, mapTagReplacementValues, noValidation); }