From b23287bb39b0a823b578b9e6d5fa4e4bbbc3b451 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Mon, 4 Aug 2008 20:01:19 +0000 Subject: Fixed another crash when reading inlined object properties. --- src/game-server/mapreader.cpp | 36 +++++++++++++++++++++++++----------- src/game-server/mapreader.hpp | 10 ++++++---- 2 files changed, 31 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/game-server/mapreader.cpp b/src/game-server/mapreader.cpp index c24c99dd..46baaa43 100644 --- a/src/game-server/mapreader.cpp +++ b/src/game-server/mapreader.cpp @@ -276,7 +276,7 @@ Map* MapReader::readMap(xmlNodePtr node, std::string const &path, } int npcId = -1; - char const *scriptText = NULL; + std::string scriptText; for_each_xml_child_node(propertiesNode, objectNode) { @@ -296,15 +296,15 @@ Map* MapReader::readMap(xmlNodePtr node, std::string const &path, } else if (value == "SCRIPT") { - scriptText = (const char *)propertyNode->xmlChildrenNode->content; + scriptText = getObjectProperty(propertyNode, ""); } } } } - if (npcId != -1 && scriptText != NULL) + if (npcId != -1 && !scriptText.empty()) { - s->loadNPC(objName, npcId, objX, objY, scriptText); + s->loadNPC(objName, npcId, objX, objY, scriptText.c_str()); } else { @@ -322,7 +322,7 @@ Map* MapReader::readMap(xmlNodePtr node, std::string const &path, } std::string scriptFilename; - char const *scriptText = NULL; + std::string scriptText; for_each_xml_child_node(propertiesNode, objectNode) { @@ -338,12 +338,12 @@ Map* MapReader::readMap(xmlNodePtr node, std::string const &path, std::string value = XML::getProperty(propertyNode, "name", std::string()); if (value == "FILENAME") { - scriptFilename = (const char *)propertyNode->xmlChildrenNode->content; + scriptFilename = getObjectProperty(propertyNode, ""); trim(scriptFilename); } else if (value == "TEXT") { - scriptText = (const char *)propertyNode->xmlChildrenNode->content; + scriptText = getObjectProperty(propertyNode, ""); } } } @@ -353,9 +353,9 @@ Map* MapReader::readMap(xmlNodePtr node, std::string const &path, { s->loadFile(scriptFilename); } - else if (scriptText != NULL) + else if (!scriptText.empty()) { - s->load(scriptText); + s->load(scriptText.c_str()); } else { @@ -490,14 +490,28 @@ void MapReader::readLayer(xmlNodePtr node, Map *map) } } +std::string MapReader::getObjectProperty(xmlNodePtr node, + const std::string &def) +{ + if (xmlHasProp(node, BAD_CAST "value")) + { + return XML::getProperty(node, "value", def); + } + else if (const char *prop = (const char *)node->xmlChildrenNode->content) + { + return std::string(prop); + } + return std::string(); +} + int MapReader::getObjectProperty(xmlNodePtr node, int def) { int val = def; - if(xmlHasProp(node, BAD_CAST "value")) + if (xmlHasProp(node, BAD_CAST "value")) { val = XML::getProperty(node, "value", def); } - else if(const char * prop = (const char *)node->xmlChildrenNode->content) + else if (const char *prop = (const char *)node->xmlChildrenNode->content) { val = atoi(prop); } diff --git a/src/game-server/mapreader.hpp b/src/game-server/mapreader.hpp index cb3e70ac..a809cdd9 100644 --- a/src/game-server/mapreader.hpp +++ b/src/game-server/mapreader.hpp @@ -45,27 +45,29 @@ class MapReader static void readMap(const std::string &filename, MapComposite *composite); private: - /** * Read an XML map from a parsed XML tree, and populate things with objects * in that map. */ static Map* readMap(xmlNodePtr node, std::string const &path, MapComposite *composite, std::vector &things); - + /** * Reads a map layer and adds it to the given map. */ static void readLayer(xmlNodePtr node, Map *map); + /** + * Get the string value from the given object property node. + */ + static std::string getObjectProperty(xmlNodePtr node, const std::string &def); + /** * Get the integer value from the given object property node. */ static int getObjectProperty(xmlNodePtr node, int def); static void setTileWithGid(Map *map, int x, int y, int gid); - - }; #endif -- cgit v1.2.3-60-g2f50