From 4159ba015c5f2035d188367d85d85052a7673cdc Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Wed, 18 Jan 2012 18:10:26 +0100 Subject: ColorDB -> HairDB. This will ease the reading of the next patch about hair handling at character creation time in tAthena. As requested by bjorn. Reviewed-by: bjorn --- src/CMakeLists.txt | 4 +- src/being.cpp | 2 +- src/client.cpp | 6 +- src/gui/charcreatedialog.cpp | 10 ++-- src/gui/charselectdialog.cpp | 2 +- src/net/manaserv/beinghandler.cpp | 6 +- src/net/manaserv/charhandler.cpp | 4 +- src/net/tmwa/beinghandler.cpp | 8 +-- src/net/tmwa/charserverhandler.cpp | 4 +- src/resources/colordb.cpp | 116 ------------------------------------ src/resources/colordb.h | 51 ---------------- src/resources/hairdb.cpp | 117 +++++++++++++++++++++++++++++++++++++ src/resources/hairdb.h | 52 +++++++++++++++++ 13 files changed, 192 insertions(+), 190 deletions(-) delete mode 100644 src/resources/colordb.cpp delete mode 100644 src/resources/colordb.h create mode 100644 src/resources/hairdb.cpp create mode 100644 src/resources/hairdb.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0e2f1567..575a00f0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -363,12 +363,12 @@ SET(SRCS resources/animation.h resources/beinginfo.cpp resources/beinginfo.h - resources/colordb.cpp - resources/colordb.h resources/dye.cpp resources/dye.h resources/emotedb.cpp resources/emotedb.h + resources/hairdb.cpp + resources/hairdb.h resources/image.cpp resources/image.h resources/imageloader.cpp diff --git a/src/being.cpp b/src/being.cpp index c7ee9083..9483e897 100644 --- a/src/being.cpp +++ b/src/being.cpp @@ -51,7 +51,7 @@ #include "net/npchandler.h" #include "resources/beinginfo.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "resources/emotedb.h" #include "resources/image.h" #include "resources/itemdb.h" diff --git a/src/client.cpp b/src/client.cpp index 0c41c7cc..768fae1e 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -64,7 +64,7 @@ #include "net/net.h" #include "net/worldinfo.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "resources/emotedb.h" #include "resources/image.h" #include "resources/itemdb.h" @@ -449,7 +449,7 @@ Client::~Client() SDL_RemoveTimer(mSecondsCounterId); // Unload XML databases - ColorDB::unload(); + HairDB::unload(); EmoteDB::unload(); delete itemDb; MonsterDB::unload(); @@ -759,7 +759,7 @@ int Client::exec() Event::trigger(Event::ClientChannel, Event::LoadingDatabases); // Load XML databases - ColorDB::load(); + HairDB::load(); switch (Net::getNetworkType()) { case ServerInfo::TMWATHENA: diff --git a/src/gui/charcreatedialog.cpp b/src/gui/charcreatedialog.cpp index 746295b5..cdd6739c 100644 --- a/src/gui/charcreatedialog.cpp +++ b/src/gui/charcreatedialog.cpp @@ -42,7 +42,7 @@ #include "net/messageout.h" #include "net/net.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "utils/gettext.h" #include "utils/stringutils.h" @@ -57,7 +57,7 @@ CharCreateDialog::CharCreateDialog(CharSelectDialog *parent, int slot): mPlayer = new Being(0, ActorSprite::PLAYER, 0, NULL); mPlayer->setGender(GENDER_MALE); - int numberOfHairColors = ColorDB::size(); + int numberOfHairColors = HairDB::size(); mHairStyle = rand() % mPlayer->getNumOfHairstyles(); mHairColor = rand() % numberOfHairColors; @@ -363,10 +363,10 @@ void CharCreateDialog::updateHair() if (mHairStyle < 0) mHairStyle += Being::getNumOfHairstyles(); - mHairColor %= ColorDB::size(); + mHairColor %= HairDB::size(); if (mHairColor < 0) - mHairColor += ColorDB::size(); + mHairColor += HairDB::size(); mPlayer->setSprite(Net::getCharHandler()->hairSprite(), - mHairStyle * -1, ColorDB::get(mHairColor)); + mHairStyle * -1, HairDB::get(mHairColor)); } diff --git a/src/gui/charselectdialog.cpp b/src/gui/charselectdialog.cpp index 42c514ec..2972c492 100644 --- a/src/gui/charselectdialog.cpp +++ b/src/gui/charselectdialog.cpp @@ -49,7 +49,7 @@ #include "net/messageout.h" #include "net/net.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "utils/gettext.h" #include "utils/stringutils.h" diff --git a/src/net/manaserv/beinghandler.cpp b/src/net/manaserv/beinghandler.cpp index 3e635d43..980c538c 100644 --- a/src/net/manaserv/beinghandler.cpp +++ b/src/net/manaserv/beinghandler.cpp @@ -38,7 +38,7 @@ #include "net/manaserv/playerhandler.h" #include "net/manaserv/manaserv_protocol.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "utils/gettext.h" @@ -144,7 +144,7 @@ void BeingHandler::handleBeingEnterMessage(Net::MessageIn &msg) being->setName(name); } int hs = msg.readInt8(), hc = msg.readInt8(); - being->setSprite(SPRITE_LAYER_HAIR, hs * -1, ColorDB::get(hc)); + being->setSprite(SPRITE_LAYER_HAIR, hs * -1, HairDB::get(hc)); being->setGender(msg.readInt8() == ManaServ::GENDER_MALE ? ::GENDER_MALE : ::GENDER_FEMALE); handleLooks(being, msg); @@ -334,7 +334,7 @@ void BeingHandler::handleBeingLooksChangeMessage(Net::MessageIn &msg) { int style = msg.readInt16(); int color = msg.readInt16(); - being->setSprite(SPRITE_LAYER_HAIR, style * -1, ColorDB::get(color)); + being->setSprite(SPRITE_LAYER_HAIR, style * -1, HairDB::get(color)); } } diff --git a/src/net/manaserv/charhandler.cpp b/src/net/manaserv/charhandler.cpp index 8a072d26..a36ae1da 100644 --- a/src/net/manaserv/charhandler.cpp +++ b/src/net/manaserv/charhandler.cpp @@ -41,7 +41,7 @@ #include "net/manaserv/manaserv_protocol.h" #include "net/manaserv/attributes.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "utils/dtor.h" #include "utils/gettext.h" @@ -392,7 +392,7 @@ void CharHandler::updateCharacters() player->setName(info.name); player->setGender(info.gender); player->setSprite(SPRITE_LAYER_HAIR, info.hairStyle * -1, - ColorDB::get(info.hairColor)); + HairDB::get(info.hairColor)); character->data.mAttributes[LEVEL] = info.level; character->data.mAttributes[CHAR_POINTS] = info.characterPoints; character->data.mAttributes[CORR_POINTS] = info.correctionPoints; diff --git a/src/net/tmwa/beinghandler.cpp b/src/net/tmwa/beinghandler.cpp index e3e92a0b..f2ce7df8 100644 --- a/src/net/tmwa/beinghandler.cpp +++ b/src/net/tmwa/beinghandler.cpp @@ -35,7 +35,7 @@ #include "net/tmwa/protocol.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "resources/emotedb.h" #include @@ -245,7 +245,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) dstBeing->setGender((gender == 0) ? GENDER_FEMALE : GENDER_MALE); // Set these after the gender, as the sprites may be gender-specific - dstBeing->setSprite(SPRITE_HAIR, hairStyle * -1, ColorDB::get(hairColor)); + dstBeing->setSprite(SPRITE_HAIR, hairStyle * -1, HairDB::get(hairColor)); dstBeing->setSprite(SPRITE_BOTTOMCLOTHES, headBottom); dstBeing->setSprite(SPRITE_TOPCLOTHES, headMid); dstBeing->setSprite(SPRITE_HAT, headTop); @@ -487,7 +487,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) dstBeing->setSprite(SPRITE_TOPCLOTHES, id); break; case 6: // eAthena LOOK_HAIR_COLOR - dstBeing->setSpriteColor(SPRITE_HAIR, ColorDB::get(id)); + dstBeing->setSpriteColor(SPRITE_HAIR, HairDB::get(id)); break; case 8: // eAthena LOOK_SHIELD dstBeing->setSprite(SPRITE_SHIELD, id); @@ -614,7 +614,7 @@ void BeingHandler::handleMessage(Net::MessageIn &msg) //dstBeing->setSprite(SPRITE_CAPE, cape); //dstBeing->setSprite(SPRITE_MISC1, misc1); //dstBeing->setSprite(SPRITE_MISC2, misc2); - dstBeing->setSprite(SPRITE_HAIR, hairStyle * -1, ColorDB::get(hairColor)); + dstBeing->setSprite(SPRITE_HAIR, hairStyle * -1, HairDB::get(hairColor)); if (msg.getId() == SMSG_PLAYER_MOVE) { diff --git a/src/net/tmwa/charserverhandler.cpp b/src/net/tmwa/charserverhandler.cpp index 83682efe..f90537ec 100644 --- a/src/net/tmwa/charserverhandler.cpp +++ b/src/net/tmwa/charserverhandler.cpp @@ -38,7 +38,7 @@ #include "net/tmwa/network.h" #include "net/tmwa/protocol.h" -#include "resources/colordb.h" +#include "resources/hairdb.h" #include "utils/dtor.h" #include "utils/gettext.h" @@ -240,7 +240,7 @@ void CharServerHandler::readPlayerData(Net::MessageIn &msg, Net::Character *char tempPlayer->setSprite(SPRITE_SHIELD, msg.readInt16()); tempPlayer->setSprite(SPRITE_HAT, msg.readInt16()); // head option top tempPlayer->setSprite(SPRITE_TOPCLOTHES, msg.readInt16()); // head option mid - tempPlayer->setSprite(SPRITE_HAIR, hairStyle * -1, ColorDB::get(msg.readInt16())); + tempPlayer->setSprite(SPRITE_HAIR, hairStyle * -1, HairDB::get(msg.readInt16())); tempPlayer->setSprite(SPRITE_MISC2, msg.readInt16()); tempPlayer->setName(msg.readString(24)); diff --git a/src/resources/colordb.cpp b/src/resources/colordb.cpp deleted file mode 100644 index d0b46b4e..00000000 --- a/src/resources/colordb.cpp +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Color database - * Copyright (C) 2008 Aethyra Development Team - * - * This file is part of The Mana 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/colordb.h" - -#include "log.h" - -#include "utils/xml.h" - -#include - -namespace -{ - ColorDB::Colors mColors; - bool mLoaded = false; - std::string mFail = "#ffffff"; -} - -void ColorDB::load() -{ - if (mLoaded) - unload(); - - XML::Document *doc = new XML::Document(HAIR_XML_FILE); - xmlNodePtr root = doc->rootNode(); - bool hairXml = true; - - if (!root || !xmlStrEqual(root->name, BAD_CAST "colors")) - { - logger->log("Trying to fall back on " COLORS_XML_FILE); - - hairXml = false; - - delete doc; - doc = new XML::Document(COLORS_XML_FILE); - root = doc->rootNode(); - - if (!root || !xmlStrEqual(root->name, BAD_CAST "colors")) - { - logger->log("ColorDB: Failed to find any color files."); - mColors[0] = mFail; - mLoaded = true; - - delete doc; - - return; - } - } - for_each_xml_child_node(node, root) - { - if (xmlStrEqual(node->name, BAD_CAST "color")) - { - int id = XML::getProperty(node, "id", 0); - - if (mColors.find(id) != mColors.end()) - { - logger->log("ColorDB: Redefinition of dye ID %d", id); - } - - mColors[id] = hairXml ? XML::getProperty(node, "value", "#FFFFFF") : - XML::getProperty(node, "dye", "#FFFFFF"); - } - } - - delete doc; - - mLoaded = true; -} - -void ColorDB::unload() -{ - logger->log("Unloading color database..."); - - mColors.clear(); - mLoaded = false; -} - -std::string &ColorDB::get(int id) -{ - if (!mLoaded) - load(); - - ColorIterator i = mColors.find(id); - - if (i == mColors.end()) - { - logger->log("ColorDB: Error, unknown dye Id# %d", id); - return mFail; - } - else - { - return i->second; - } -} - -int ColorDB::size() -{ - return mColors.size(); -} diff --git a/src/resources/colordb.h b/src/resources/colordb.h deleted file mode 100644 index 57b52388..00000000 --- a/src/resources/colordb.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Color database - * Copyright (C) 2008 Aethyra Development Team - * - * This file is part of The Mana 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 COLOR_MANAGER_H -#define COLOR_MANAGER_H - -#include -#include - -/** - * Color information database. - */ -namespace ColorDB -{ - /** - * Loads the color data from colors.xml. - */ - void load(); - - /** - * Clear the color data - */ - void unload(); - - std::string &get(int id); - - int size(); - - // Color DB - typedef std::map Colors; - typedef Colors::iterator ColorIterator; -} - -#endif diff --git a/src/resources/hairdb.cpp b/src/resources/hairdb.cpp new file mode 100644 index 00000000..abf64bd5 --- /dev/null +++ b/src/resources/hairdb.cpp @@ -0,0 +1,117 @@ +/* + * Color database + * Copyright (C) 2008 Aethyra Development Team + * Copyright (C) 2009-2012 The Mana Developers + * + * This file is part of The Mana 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/hairdb.h" + +#include "log.h" + +#include "utils/xml.h" + +#include + +namespace +{ + HairDB::Colors mColors; + bool mLoaded = false; + std::string mFail = "#ffffff"; +} + +void HairDB::load() +{ + if (mLoaded) + unload(); + + XML::Document *doc = new XML::Document(HAIR_XML_FILE); + xmlNodePtr root = doc->rootNode(); + bool hairXml = true; + + if (!root || !xmlStrEqual(root->name, BAD_CAST "colors")) + { + logger->log("Trying to fall back on " COLORS_XML_FILE); + + hairXml = false; + + delete doc; + doc = new XML::Document(COLORS_XML_FILE); + root = doc->rootNode(); + + if (!root || !xmlStrEqual(root->name, BAD_CAST "colors")) + { + logger->log("ColorDB: Failed to find any color files."); + mColors[0] = mFail; + mLoaded = true; + + delete doc; + + return; + } + } + for_each_xml_child_node(node, root) + { + if (xmlStrEqual(node->name, BAD_CAST "color")) + { + int id = XML::getProperty(node, "id", 0); + + if (mColors.find(id) != mColors.end()) + { + logger->log("ColorDB: Redefinition of dye ID %d", id); + } + + mColors[id] = hairXml ? XML::getProperty(node, "value", "#FFFFFF") : + XML::getProperty(node, "dye", "#FFFFFF"); + } + } + + delete doc; + + mLoaded = true; +} + +void HairDB::unload() +{ + logger->log("Unloading color database..."); + + mColors.clear(); + mLoaded = false; +} + +std::string &HairDB::get(int id) +{ + if (!mLoaded) + load(); + + ColorIterator i = mColors.find(id); + + if (i == mColors.end()) + { + logger->log("ColorDB: Error, unknown dye Id# %d", id); + return mFail; + } + else + { + return i->second; + } +} + +int HairDB::size() +{ + return mColors.size(); +} diff --git a/src/resources/hairdb.h b/src/resources/hairdb.h new file mode 100644 index 00000000..c29dbb83 --- /dev/null +++ b/src/resources/hairdb.h @@ -0,0 +1,52 @@ +/* + * Color database + * Copyright (C) 2008 Aethyra Development Team + * Copyright (C) 2009-2012 The Mana Developers + * + * This file is part of The Mana 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 HAIR_MANAGER_H +#define HAIR_MANAGER_H + +#include +#include + +/** + * Hair information database. + */ +namespace HairDB +{ + /** + * Loads the color data from colors.xml. + */ + void load(); + + /** + * Clear the color data + */ + void unload(); + + std::string &get(int id); + + int size(); + + // Hair Color DB + typedef std::map Colors; + typedef Colors::iterator ColorIterator; +} + +#endif -- cgit v1.2.3-70-g09d2