summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-05-13 13:39:52 +0200
committerThorbjørn Lindeijer <thorbjorn@lindeijer.nl>2010-05-16 23:25:19 +0200
commite5c7ef4573adcdcafec7523a77724b171456b215 (patch)
tree797833ddc766d92131da86096de81741a94f4d20
parent0a48f0d41653d0a0758fc84fd6a18830bd37da18 (diff)
downloadmanaserv-e5c7ef4573adcdcafec7523a77724b171456b215.tar.gz
manaserv-e5c7ef4573adcdcafec7523a77724b171456b215.tar.bz2
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
-rw-r--r--example/clientdata/items.xml12
-rw-r--r--src/CMakeLists.txt7
-rw-r--r--src/Makefile.am16
-rw-r--r--src/account-server/main-account.cpp33
-rw-r--r--src/account-server/storage.cpp20
-rw-r--r--src/common/permissionmanager.cpp5
-rw-r--r--src/common/resourcemanager.cpp (renamed from src/game-server/resourcemanager.cpp)82
-rw-r--r--src/common/resourcemanager.hpp (renamed from src/game-server/resourcemanager.hpp)0
-rw-r--r--src/game-server/itemmanager.cpp7
-rw-r--r--src/game-server/main-game.cpp2
-rw-r--r--src/game-server/mapmanager.cpp6
-rw-r--r--src/game-server/mapreader.cpp6
-rw-r--r--src/game-server/monster.cpp2
-rw-r--r--src/game-server/monstermanager.cpp6
-rw-r--r--src/game-server/skillmanager.cpp2
-rw-r--r--src/game-server/statusmanager.cpp7
-rw-r--r--src/scripting/script.cpp8
-rw-r--r--src/utils/xml.cpp144
-rw-r--r--src/utils/xml.hpp60
19 files changed, 248 insertions, 177 deletions
diff --git a/example/clientdata/items.xml b/example/clientdata/items.xml
new file mode 100644
index 00000000..fbf899ac
--- /dev/null
+++ b/example/clientdata/items.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<items>
+ <item id="1"
+ image="use-food-candy.png"
+ name="Candy"
+ description="A sugar-free candy."
+ type="usable"
+ hp="5"
+ weight="1"
+ max-per-slot="30"
+ />
+</items>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 48aaf3ac..69393de9 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -103,6 +103,8 @@ SET(SRCS
common/configuration.hpp
common/configuration.cpp
common/inventorydata.hpp
+ common/resourcemanager.hpp
+ common/resourcemanager.cpp
net/bandwidth.hpp
net/bandwidth.cpp
net/connection.hpp
@@ -120,6 +122,8 @@ SET(SRCS
utils/logger.cpp
utils/processorutils.hpp
utils/processorutils.cpp
+ utils/string.hpp
+ utils/string.cpp
utils/stringfilter.h
utils/stringfilter.cpp
utils/timer.h
@@ -130,7 +134,6 @@ SET(SRCS
utils/tokendispenser.cpp
utils/xml.hpp
utils/xml.cpp
- utils/string.cpp
)
SET(SRCS_MANASERVACCOUNT
@@ -225,8 +228,6 @@ SET(SRCS_MANASERVGAME
game-server/postman.hpp
game-server/quest.hpp
game-server/quest.cpp
- game-server/resourcemanager.hpp
- game-server/resourcemanager.cpp
game-server/skillmanager.hpp
game-server/skillmanager.cpp
game-server/spawnarea.hpp
diff --git a/src/Makefile.am b/src/Makefile.am
index 3e31cf2a..ff53714f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -41,6 +41,8 @@ manaserv_account_SOURCES = \
common/configuration.hpp \
common/configuration.cpp \
common/inventorydata.hpp \
+ common/resourcemanager.hpp \
+ common/resourcemanager.cpp \
dal/dalexcept.h \
dal/dataprovider.h \
dal/dataprovider.cpp \
@@ -68,6 +70,8 @@ manaserv_account_SOURCES = \
utils/processorutils.cpp \
utils/sha256.h \
utils/sha256.cpp \
+ utils/string.hpp \
+ utils/string.cpp \
utils/stringfilter.h \
utils/stringfilter.cpp \
utils/timer.cpp \
@@ -76,8 +80,7 @@ manaserv_account_SOURCES = \
utils/tokendispenser.hpp \
utils/tokendispenser.cpp \
utils/xml.hpp \
- utils/xml.cpp \
- utils/string.cpp
+ utils/xml.cpp
manaserv_game_CXXFLAGS = -Wall
@@ -91,6 +94,8 @@ manaserv_game_SOURCES = \
common/inventorydata.hpp \
common/permissionmanager.hpp \
common/permissionmanager.cpp \
+ common/resourcemanager.hpp \
+ common/resourcemanager.cpp \
game-server/accountconnection.hpp \
game-server/accountconnection.cpp \
game-server/actor.hpp \
@@ -134,8 +139,6 @@ manaserv_game_SOURCES = \
game-server/postman.hpp \
game-server/quest.hpp \
game-server/quest.cpp \
- game-server/resourcemanager.hpp \
- game-server/resourcemanager.cpp \
game-server/skillmanager.hpp \
game-server/skillmanager.cpp \
game-server/spawnarea.hpp \
@@ -175,6 +178,8 @@ manaserv_game_SOURCES = \
utils/logger.cpp \
utils/processorutils.hpp \
utils/processorutils.cpp \
+ utils/string.hpp \
+ utils/string.cpp \
utils/stringfilter.h \
utils/stringfilter.cpp \
utils/timer.h \
@@ -187,8 +192,7 @@ manaserv_game_SOURCES = \
utils/xml.hpp \
utils/xml.cpp \
utils/zlib.hpp \
- utils/zlib.cpp \
- utils/string.cpp
+ utils/zlib.cpp
if BUILD_MYSQL
manaserv_account_SOURCES += \
diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp
index 0a3fd5f5..460a5f1b 100644
--- a/src/account-server/main-account.cpp
+++ b/src/account-server/main-account.cpp
@@ -18,14 +18,6 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <cstdlib>
-#include <getopt.h>
-#include <signal.h>
-#include <iostream>
-#include <fstream>
-#include <physfs.h>
-#include <enet/enet.h>
-
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
@@ -38,6 +30,7 @@
#include "chat-server/guildmanager.hpp"
#include "chat-server/post.hpp"
#include "common/configuration.hpp"
+#include "common/resourcemanager.hpp"
#include "net/bandwidth.hpp"
#include "net/connectionhandler.hpp"
#include "net/messageout.hpp"
@@ -46,6 +39,14 @@
#include "utils/stringfilter.h"
#include "utils/timer.h"
+#include <cstdlib>
+#include <getopt.h>
+#include <signal.h>
+#include <iostream>
+#include <fstream>
+#include <physfs.h>
+#include <enet/enet.h>
+
using utils::Logger;
// Default options that automake should be able to override.
@@ -63,16 +64,9 @@ Storage *storage;
/** Communications (chat) message handler */
ChatHandler *chatHandler;
-/** Chat Channels Manager */
ChatChannelManager *chatChannelManager;
-
-/** Guild Manager */
GuildManager *guildManager;
-
-/** Post Manager */
PostManager *postalManager;
-
-/** Bandwidth Monitor */
BandwidthMonitor *gBandwidth;
/** Callback used when SIGQUIT signal is received. */
@@ -147,6 +141,8 @@ static void initialize()
LOG_INFO("Using config file: " << configPath);
LOG_INFO("Using log file: " << logPath);
+ ResourceManager::initialize();
+
// check inter-server password
if (Configuration::getValue("net_password", "") == "")
{
@@ -164,15 +160,10 @@ static void initialize()
}
// --- Initialize the managers
- // Initialize the slang's and double quotes filter.
- stringFilter = new StringFilter;
- // Initialize the Chat channels manager
+ stringFilter = new StringFilter; // The slang's and double quotes filter.
chatChannelManager = new ChatChannelManager;
- // Initialise the Guild manager
guildManager = new GuildManager;
- // Initialise the post manager
postalManager = new PostManager;
- // Initialise the bandwidth monitor
gBandwidth = new BandwidthMonitor;
// --- Initialize the global handlers
diff --git a/src/account-server/storage.cpp b/src/account-server/storage.cpp
index 6e11f598..39735067 100644
--- a/src/account-server/storage.cpp
+++ b/src/account-server/storage.cpp
@@ -36,8 +36,7 @@
#include "utils/xml.hpp"
#include "utils/sha256.h"
-// TODO: make data/items.xml a constant or read it from config file
-static const char *DEFAULT_ITEM_FILE = "data/items.xml";
+static const char *DEFAULT_ITEM_FILE = "items.xml";
// defines the supported db version
static const char *DB_VERSION_PARAMETER = "database_version";
@@ -1920,24 +1919,18 @@ void Storage::deletePost(Letter *letter)
*/
void Storage::syncDatabase()
{
- xmlDocPtr doc = xmlReadFile(DEFAULT_ITEM_FILE, NULL, 0);
- if (!doc)
- {
- LOG_ERROR("Item Manager: Error while parsing item database (items.xml)!");
- return;
- }
+ XML::Document doc(DEFAULT_ITEM_FILE);
+ xmlNodePtr rootNode = doc.rootNode();
- xmlNodePtr node = xmlDocGetRootElement(doc);
- if (!node || !xmlStrEqual(node->name, BAD_CAST "items"))
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "items"))
{
- LOG_ERROR("Item Manager:(items.xml) is not a valid database file!");
- xmlFreeDoc(doc);
+ LOG_ERROR("Item Manager: Error while parsing item database (items.xml)!");
return;
}
mDb->beginTransaction();
int itmCount = 0;
- for (node = node->xmlChildrenNode; node != NULL; node = node->next)
+ for_each_xml_child_node(node, rootNode)
{
// Try to load the version of the item database. The version is defined
// as subversion tag embedded as XML attribute. So every modification
@@ -2024,7 +2017,6 @@ void Storage::syncDatabase()
}
mDb->commitTransaction();
- xmlFreeDoc(doc);
}
/**
diff --git a/src/common/permissionmanager.cpp b/src/common/permissionmanager.cpp
index ecfd4643..549e3628 100644
--- a/src/common/permissionmanager.cpp
+++ b/src/common/permissionmanager.cpp
@@ -18,16 +18,15 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <cstring>
-
#include "common/permissionmanager.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/character.hpp"
-#include "game-server/resourcemanager.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"
#include <string.h>
+#include <cstring>
static std::map<std::string, unsigned char> permissions;
static std::map<std::string, unsigned char> aliases;
diff --git a/src/game-server/resourcemanager.cpp b/src/common/resourcemanager.cpp
index 3545484b..c02bf7de 100644
--- a/src/game-server/resourcemanager.cpp
+++ b/src/common/resourcemanager.cpp
@@ -18,6 +18,12 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
+#include "common/resourcemanager.hpp"
+
+#include "common/configuration.hpp"
+
+#include "utils/logger.h"
+
#include <cstdlib>
#include <cstring>
@@ -31,83 +37,19 @@
#include <physfs.h>
-#include "game-server/resourcemanager.hpp"
-
-#include "utils/logger.h"
-
#define PKG_DATADIR ""
void ResourceManager::initialize()
{
PHYSFS_permitSymbolicLinks(1);
- // Add the main data directory to our PhysicsFS search path
- PHYSFS_addToSearchPath("data", 1);
- PHYSFS_addToSearchPath(PKG_DATADIR "data", 1);
-#ifdef _WIN32
- // Define the path in which to search
- std::string searchString = std::string("data/*.zip");
-
- // Create our find file data structure
- struct _finddata_t findFileInfo;
-
- // Find the first zipped file
- long handle =
- static_cast<long>(::_findfirst(searchString.c_str(), &findFileInfo));
- long file = handle;
-
- // Loop until all files we're searching for are found
- while (file >= 0) {
- // Define the file path string
- std::string filePath = std::string("data/") +
- std::string(findFileInfo.name);
+ const std::string clientDataPath =
+ Configuration::getValue("clientDataPath", "example/clientdata");
+ const std::string serverDataPath =
+ Configuration::getValue("serverDataPath", "example/serverdata");
- LOG_INFO("Adding to PhysicsFS: " << findFileInfo.name);
-
- // Add the zip file to our PhysicsFS search path
- PHYSFS_addToSearchPath(filePath.c_str(), 1);
-
- // Find the next file
- file = ::_findnext(handle, &findFileInfo);
- }
-
- // Shutdown findfile stuff
- ::_findclose(handle);
-#else
- // Retrieve the current path
- char programPath[256];
- if (!getcwd(programPath, 256))
- strcpy(programPath, ".");
- strncat(programPath, "/data", 256 - strlen(programPath) - 1);
-
- // Create our directory structure
- DIR *dir = opendir(programPath);
-
- // Return if the directory is invalid
- if (dir == NULL) {
- return;
- }
-
- struct dirent *direntry;
- while ((direntry = readdir(dir)) != NULL)
- {
- char *ext = strstr(direntry->d_name, ".zip");
-
- if (ext != NULL && strcmp(ext, ".zip") == 0)
- {
- // Define the file path string
- std::string filePath = std::string(programPath) +
- std::string("/") + std::string(direntry->d_name);
-
- LOG_INFO("Adding to PhysicsFS: " << filePath);
-
- // Add the zip file to our PhysicsFS search path
- PHYSFS_addToSearchPath(filePath.c_str(), 1);
- }
- }
-
- closedir(dir);
-#endif
+ PHYSFS_addToSearchPath(clientDataPath.c_str(), 1);
+ PHYSFS_addToSearchPath(serverDataPath.c_str(), 1);
}
bool ResourceManager::exists(const std::string &path)
diff --git a/src/game-server/resourcemanager.hpp b/src/common/resourcemanager.hpp
index 74c41891..74c41891 100644
--- a/src/game-server/resourcemanager.hpp
+++ b/src/common/resourcemanager.hpp
diff --git a/src/game-server/itemmanager.cpp b/src/game-server/itemmanager.cpp
index 5c946a47..96f9c210 100644
--- a/src/game-server/itemmanager.cpp
+++ b/src/game-server/itemmanager.cpp
@@ -18,19 +18,18 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <map>
-#include <set>
-
#include "game-server/itemmanager.hpp"
#include "defines.h"
+#include "common/resourcemanager.hpp"
#include "game-server/item.hpp"
-#include "game-server/resourcemanager.hpp"
#include "game-server/skillmanager.hpp"
#include "scripting/script.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"
+#include <map>
+#include <set>
#include <sstream>
typedef std::map< int, ItemClass * > ItemClasses;
diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp
index 36da4c3d..1e01bcb5 100644
--- a/src/game-server/main-game.cpp
+++ b/src/game-server/main-game.cpp
@@ -37,6 +37,7 @@
#include "common/configuration.hpp"
#include "common/permissionmanager.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/accountconnection.hpp"
#include "game-server/gamehandler.hpp"
#include "game-server/skillmanager.hpp"
@@ -45,7 +46,6 @@
#include "game-server/monstermanager.hpp"
#include "game-server/statusmanager.hpp"
#include "game-server/postman.hpp"
-#include "game-server/resourcemanager.hpp"
#include "game-server/state.hpp"
#include "net/bandwidth.hpp"
#include "net/connectionhandler.hpp"
diff --git a/src/game-server/mapmanager.cpp b/src/game-server/mapmanager.cpp
index b27348b2..ced6b582 100644
--- a/src/game-server/mapmanager.cpp
+++ b/src/game-server/mapmanager.cpp
@@ -18,17 +18,17 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <cassert>
-
#include "game-server/mapmanager.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/map.hpp"
#include "game-server/mapcomposite.hpp"
#include "game-server/mapreader.hpp"
-#include "game-server/resourcemanager.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"
+#include <cassert>
+
/**
* List of all the game maps, be they present or not on this server.
*/
diff --git a/src/game-server/mapreader.cpp b/src/game-server/mapreader.cpp
index 30e73bfc..80885821 100644
--- a/src/game-server/mapreader.cpp
+++ b/src/game-server/mapreader.cpp
@@ -18,15 +18,13 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <cstring>
-
#include "game-server/mapreader.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/map.hpp"
#include "game-server/mapcomposite.hpp"
#include "game-server/mapmanager.hpp"
#include "game-server/monstermanager.hpp"
-#include "game-server/resourcemanager.hpp"
#include "game-server/spawnarea.hpp"
#include "game-server/trigger.hpp"
#include "scripting/script.hpp"
@@ -37,6 +35,8 @@
#include "utils/zlib.hpp"
#include "utils/string.hpp"
+#include <cstring>
+
static std::vector< int > tilesetFirstGids;
bool MapReader::readMap(const std::string &filename, MapComposite
diff --git a/src/game-server/monster.cpp b/src/game-server/monster.cpp
index 5bd56cca..2489e735 100644
--- a/src/game-server/monster.cpp
+++ b/src/game-server/monster.cpp
@@ -21,10 +21,10 @@
#include "game-server/monster.hpp"
#include "common/configuration.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/character.hpp"
#include "game-server/collisiondetection.hpp"
#include "game-server/item.hpp"
-#include "game-server/resourcemanager.hpp"
#include "game-server/mapcomposite.hpp"
#include "game-server/state.hpp"
#include "scripting/script.hpp"
diff --git a/src/game-server/monstermanager.cpp b/src/game-server/monstermanager.cpp
index e3beda72..af6f3a20 100644
--- a/src/game-server/monstermanager.cpp
+++ b/src/game-server/monstermanager.cpp
@@ -18,16 +18,16 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <map>
-
#include "game-server/monstermanager.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/itemmanager.hpp"
#include "game-server/monster.hpp"
-#include "game-server/resourcemanager.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"
+#include <map>
+
typedef std::map< int, MonsterClass * > MonsterClasses;
static MonsterClasses monsterClasses; /**< Monster reference */
static std::string monsterReferenceFile;
diff --git a/src/game-server/skillmanager.cpp b/src/game-server/skillmanager.cpp
index c4a18e50..ea29e585 100644
--- a/src/game-server/skillmanager.cpp
+++ b/src/game-server/skillmanager.cpp
@@ -20,7 +20,7 @@
#include "game-server/skillmanager.hpp"
-#include "game-server/resourcemanager.hpp"
+#include "common/resourcemanager.hpp"
#include "utils/string.hpp" // for the toupper function
#include "utils/logger.h"
#include "utils/xml.hpp"
diff --git a/src/game-server/statusmanager.cpp b/src/game-server/statusmanager.cpp
index 3472ae79..173996dd 100644
--- a/src/game-server/statusmanager.cpp
+++ b/src/game-server/statusmanager.cpp
@@ -18,17 +18,16 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <map>
-#include <set>
-
#include "game-server/statusmanager.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/statuseffect.hpp"
-#include "game-server/resourcemanager.hpp"
#include "scripting/script.hpp"
#include "utils/logger.h"
#include "utils/xml.hpp"
+#include <map>
+#include <set>
#include <sstream>
typedef std::map< int, StatusEffect * > StatusEffects;
diff --git a/src/scripting/script.cpp b/src/scripting/script.cpp
index 39a04049..01d237f3 100644
--- a/src/scripting/script.cpp
+++ b/src/scripting/script.cpp
@@ -18,15 +18,15 @@
* along with The Mana Server. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <cstdlib>
-#include <map>
-
#include "scripting/script.hpp"
+#include "common/resourcemanager.hpp"
#include "game-server/being.hpp"
-#include "game-server/resourcemanager.hpp"
#include "utils/logger.h"
+#include <cstdlib>
+#include <map>
+
typedef std::map< std::string, Script::Factory > Engines;
static Engines *engines = NULL;
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index ab9ba77a..659f928b 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 e6b618c8..ee37b481 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