From bf32ca896ccf51385eb67812088ba5861ca2ede2 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 13 Mar 2011 21:23:33 +0200 Subject: Add ability to remap maps to different files. --- src/resources/mapdb.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++ src/resources/mapdb.h | 50 ++++++++++++++++++++++++++ src/resources/mapreader.cpp | 30 ++++++++-------- src/resources/mapreader.h | 3 +- 4 files changed, 156 insertions(+), 15 deletions(-) create mode 100644 src/resources/mapdb.cpp create mode 100644 src/resources/mapdb.h (limited to 'src/resources') diff --git a/src/resources/mapdb.cpp b/src/resources/mapdb.cpp new file mode 100644 index 000000000..37b127c02 --- /dev/null +++ b/src/resources/mapdb.cpp @@ -0,0 +1,88 @@ +/* + * Color database + * Copyright (C) 2008 Aethyra Development Team + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "resources/mapdb.h" + +#include "configuration.h" +#include "client.h" +#include "log.h" + +#include "utils/xml.h" + +#include + +namespace +{ + bool mLoaded = false; + MapDB::Maps mMaps; +} + +void MapDB::load() +{ + if (mLoaded) + unload(); + + XML::Document *doc = new XML::Document( + paths.getStringValue("maps") + "remap.xml"); + + xmlNodePtr root = doc->rootNode(); + if (!root) + return; + + for_each_xml_child_node(node, root) + { + if (xmlStrEqual(node->name, BAD_CAST "map")) + { + std::string name = XML::getProperty(node, "name", ""); + if (name.empty()) + continue; + + std::string value = XML::getProperty(node, "value", ""); + if (value.empty()) + continue; + + mMaps[name] = value; + } + } + + delete doc; + + mLoaded = true; +} + + +void MapDB::unload() +{ + logger->log1("Unloading map database..."); + + mMaps.clear(); + mLoaded = false; +} + +std::string MapDB::getMapName(std::string name) +{ + MapIterator it = mMaps.find(name); + logger->log("map: " + name); + + if (it != mMaps.end()) + return it->second; + return name; +} diff --git a/src/resources/mapdb.h b/src/resources/mapdb.h new file mode 100644 index 000000000..2ba084297 --- /dev/null +++ b/src/resources/mapdb.h @@ -0,0 +1,50 @@ +/* + * Color database + * Copyright (C) 2008 Aethyra Development Team + * Copyright (C) 2011 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef MAPDB_H +#define MAPDB_H + +#include +#include + +/** + * Color information database. + */ +namespace MapDB +{ + /** + * Loads the map remap data from maps\remap.xml. + */ + void load(); + + /** + * Clear the remap data + */ + void unload(); + + std::string getMapName(std::string name); + + // Maps DB + typedef std::map Maps; + typedef Maps::iterator MapIterator; +} + +#endif diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 5a635a8c2..d3cd22988 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -29,6 +29,7 @@ #include "resources/animation.h" #include "resources/image.h" +#include "resources/mapdb.h" #include "resources/resourcemanager.h" #include "utils/base64.h" @@ -177,25 +178,26 @@ int inflateMemory(unsigned char *in, unsigned int inLength, return outLength; } -Map *MapReader::readMap(const std::string &filename) +Map *MapReader::readMap(const std::string &filename, + const std::string &realFilename) { - logger->log("Attempting to read map %s", filename.c_str()); + logger->log("Attempting to read map %s", realFilename.c_str()); // Load the file through resource manager ResourceManager *resman = ResourceManager::getInstance(); int fileSize; - void *buffer = resman->loadFile(filename, fileSize); + void *buffer = resman->loadFile(realFilename, fileSize); Map *map = NULL; if (buffer == NULL) { - logger->log("Map file not found (%s)", filename.c_str()); + logger->log("Map file not found (%s)", realFilename.c_str()); return NULL; } unsigned char *inflated; unsigned int inflatedSize; - if (filename.find(".gz", filename.length() - 3) != std::string::npos) + if (realFilename.find(".gz", realFilename.length() - 3) != std::string::npos) { // Inflate the gzipped map data inflatedSize = @@ -205,7 +207,7 @@ Map *MapReader::readMap(const std::string &filename) if (inflated == NULL) { logger->log("Could not decompress map file (%s)", - filename.c_str()); + realFilename.c_str()); return NULL; } } @@ -224,20 +226,20 @@ Map *MapReader::readMap(const std::string &filename) if (node) { if (!xmlStrEqual(node->name, BAD_CAST "map")) - { - logger->log("Error: Not a map file (%s)!", filename.c_str()); - } + logger->log("Error: Not a map file (%s)!", realFilename.c_str()); else - { - map = readMap(node, filename); - } + map = readMap(node, realFilename); } else { - logger->log("Error while parsing map file (%s)!", filename.c_str()); + logger->log("Error while parsing map file (%s)!", realFilename.c_str()); } - if (map) map->setProperty("_filename", filename); + if (map) + { + map->setProperty("_filename", realFilename); + map->setProperty("_realfilename", filename); + } return map; } diff --git a/src/resources/mapreader.h b/src/resources/mapreader.h index 6995aaf8e..927b76173 100644 --- a/src/resources/mapreader.h +++ b/src/resources/mapreader.h @@ -46,7 +46,8 @@ class MapReader /** * Read an XML map from a file. */ - static Map *readMap(const std::string &filename); + static Map *readMap(const std::string &filename, + const std::string &realFilename); /** * Read an XML map from a parsed XML tree. The path is used to find the -- cgit v1.2.3-60-g2f50