diff options
author | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2010-05-13 13:39:52 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn@lindeijer.nl> | 2010-05-16 23:25:19 +0200 |
commit | e5c7ef4573adcdcafec7523a77724b171456b215 (patch) | |
tree | 797833ddc766d92131da86096de81741a94f4d20 /src/utils | |
parent | 0a48f0d41653d0a0758fc84fd6a18830bd37da18 (diff) | |
download | manaserv-e5c7ef4573adcdcafec7523a77724b171456b215.tar.gz manaserv-e5c7ef4573adcdcafec7523a77724b171456b215.tar.xz manaserv-e5c7ef4573adcdcafec7523a77724b171456b215.zip |
Changed and split up the default location for loading data
Instead of loading data from a 'data' directory in the current working
directory, the server now uses clientDataPath and serverDataPath as
specified in the configuration. This removes the need to set up symbolic
links in order to merge these two types of data.
The default values point to example/clientdata and example/serverdata,
where a minimal example world can be developed to make setting up an
initial server quick and easy.
The XML::Document convenience class was copied over from the client.
Also, the ResourceManager is now shared between both servers, since the
account client is reading items.xml.
Reviewed-by: Jared Adams
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/xml.cpp | 144 | ||||
-rw-r--r-- | src/utils/xml.hpp | 60 |
2 files changed, 168 insertions, 36 deletions
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index ab9ba77..659f928 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -1,6 +1,7 @@ /* - * The Mana Server - * Copyright (C) 2006-2010 The Mana World Development Team + * XML utility functions + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers * * This file is part of The Mana Server. * @@ -18,47 +19,132 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ -#include <cstdlib> - #include "utils/xml.hpp" +#include "common/resourcemanager.hpp" +#include "utils/logger.h" + +#include <iostream> +#include <fstream> + namespace XML { + Document::Document(const std::string &filename, bool useResman): + mDoc(0) + { + int size; + char *data = NULL; + if (useResman) + { + data = ResourceManager::loadFile(filename, size); + } + else + { + std::ifstream file; + file.open(filename.c_str(), std::ios::in); -int getProperty(xmlNodePtr node, const char *name, int def) -{ - if (xmlChar *prop = xmlGetProp(node, BAD_CAST name)) + if (file.is_open()) + { + // Get length of file + file.seekg(0, std::ios::end); + size = file.tellg(); + file.seekg(0, std::ios::beg); + + data = (char*) malloc(size); + + file.read(data, size); + file.close(); + } + else + { + LOG_ERROR("(XML::Document) Error loading XML file: " + << filename); + } + } + + if (data) + { + mDoc = xmlParseMemory(data, size); + free(data); + + if (!mDoc) + { + LOG_ERROR("(XML::Document) Error parsing XML file: " + << filename); + } + } + else + { + LOG_ERROR("(XML::Document) Error loading XML file: " + << filename); + } + } + + Document::Document(const char *data, int size) + { + mDoc = xmlParseMemory(data, size); + } + + Document::~Document() { - int ret = atoi((char*)prop); - xmlFree(prop); + if (mDoc) + xmlFreeDoc(mDoc); + } + + xmlNodePtr Document::rootNode() + { + return mDoc ? xmlDocGetRootElement(mDoc) : 0; + } + + int getProperty(xmlNodePtr node, const char *name, int def) + { + int &ret = def; + + xmlChar *prop = xmlGetProp(node, BAD_CAST name); + if (prop) + { + ret = atoi((char*) prop); + xmlFree(prop); + } + return ret; } - return def; -} -double getFloatProperty(xmlNodePtr node, const char* name, double def) -{ - double &ret = def; + double getFloatProperty(xmlNodePtr node, const char *name, double def) + { + double &ret = def; - xmlChar *prop = xmlGetProp(node, BAD_CAST name); - if (prop) { - ret = atof((char*)prop); - xmlFree(prop); + xmlChar *prop = xmlGetProp(node, BAD_CAST name); + if (prop) + { + ret = atof((char*) prop); + xmlFree(prop); + } + + return ret; } - return ret; -} + std::string getProperty(xmlNodePtr node, const char *name, + const std::string &def) + { + xmlChar *prop = xmlGetProp(node, BAD_CAST name); + if (prop) + { + std::string val = (char*) prop; + xmlFree(prop); + return val; + } -std::string getProperty(xmlNodePtr node, const char *name, - const std::string &def) -{ - if (xmlChar *prop = xmlGetProp(node, BAD_CAST name)) + return def; + } + + xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name) { - std::string val = (char *)prop; - xmlFree(prop); - return val; + for_each_xml_child_node(child, parent) + if (xmlStrEqual(child->name, BAD_CAST name)) + return child; + + return NULL; } - return def; -} } // namespace XML diff --git a/src/utils/xml.hpp b/src/utils/xml.hpp index e6b618c..ee37b48 100644 --- a/src/utils/xml.hpp +++ b/src/utils/xml.hpp @@ -1,6 +1,7 @@ /* - * The Mana Server - * Copyright (C) 2004-2010 The Mana World Development Team + * XML utility functions + * Copyright (C) 2004-2009 The Mana World Development Team + * Copyright (C) 2009-2010 The Mana Developers * * This file is part of The Mana Server. * @@ -18,34 +19,79 @@ * along with The Mana Server. If not, see <http://www.gnu.org/licenses/>. */ + #ifndef XML_H #define XML_H -#include <string> #include <libxml/tree.h> -#include "game-server/item.hpp" +#include <string> +/** + * XML helper functions. + */ namespace XML { /** + * A helper class for parsing an XML document, which also cleans it up + * again (RAII). + */ + class Document + { + public: + /** + * Constructor that attempts to load the given file through the + * resource manager. Logs errors. + */ + Document(const std::string &filename, bool useResman = true); + + /** + * Constructor that attempts to load an XML document from memory. + * Does not log errors. + * + * @param data the string to parse as XML + * @param size the length of the string in bytes + */ + Document(const char *data, int size); + + /** + * Destructor. Frees the loaded XML file. + */ + ~Document(); + + /** + * Returns the root node of the document (or NULL if there was a + * load error). + */ + xmlNodePtr rootNode(); + + private: + xmlDocPtr mDoc; + }; + + /** * Gets an integer property from an xmlNodePtr. */ int getProperty(xmlNodePtr node, const char *name, int def); /** + * Gets an floating point property from an xmlNodePtr. + */ + double getFloatProperty(xmlNodePtr node, const char *name, double def); + + /** * Gets a string property from an xmlNodePtr. */ std::string getProperty(xmlNodePtr node, const char *name, const std::string &def); /** - * Gets an floating point property from an xmlNodePtr. + * Finds the first child node with the given name */ - double getFloatProperty(xmlNodePtr node, const char *name, double def); + xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name); } #define for_each_xml_child_node(var, parent) \ for (xmlNodePtr var = parent->xmlChildrenNode; var; var = var->next) -#endif +#endif // XML_H |