ManaPlus
colordb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008 Aethyra Development Team
4  * Copyright (C) 2011-2019 The ManaPlus Developers
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include "resources/db/colordb.h"
23 
24 #include "configuration.h"
25 
26 #include "utils/cast.h"
27 #include "utils/checkutils.h"
28 
29 #include "resources/beingcommon.h"
30 
31 #include "debug.h"
32 
33 namespace
34 {
35  int mHairColorsSize = 0;
36  bool mLoaded = false;
37  std::string mFail("#ffffff");
39 } // namespace
40 
42 {
43  if (mLoaded)
44  unload();
45 
46  logger->log1("Initializing color database...");
47 
48  std::map<ItemColor, ItemColorData> colors;
49  ColorListsIterator it = mColorLists.find("hair");
50  if (it != mColorLists.end())
51  colors = it->second;
52  loadHair(paths.getStringValue("hairColorFile"),
53  colors,
55  loadHair(paths.getStringValue("hairColorPatchFile"),
56  colors,
58  StringVect list;
60  "hairColorPatchDir"), list, ".xml");
61  FOR_EACH (StringVectCIter, it2, list)
62  loadHair(*it2, colors, SkipError_true);
63 
64  mColorLists["hair"] = colors;
65 
66  loadColorLists(paths.getStringValue("itemColorsFile"),
68  loadColorLists(paths.getStringValue("itemColorsPatchFile"),
70  loadXmlDir("itemColorsPatchDir", loadColorLists)
71 
72  it = mColorLists.find("hair");
73  if (it != mColorLists.end())
74  mHairColorsSize = CAST_S32((*it).second.size());
75  else
76  mHairColorsSize = 0;
77  mLoaded = true;
78 }
79 
80 void ColorDB::loadHair(const std::string &fileName,
81  std::map<ItemColor, ItemColorData> &colors,
82  const SkipError skipError)
83 {
86  skipError);
87  XmlNodeConstPtrConst root = doc->rootNode();
88 
89  if ((root == nullptr) || !xmlNameEqual(root, "colors"))
90  {
91  logger->log("ColorDB: Failed to find hair colors file.");
92  if (colors.find(ItemColor_zero) == colors.end())
93  colors[ItemColor_zero] = ItemColorData(ItemColor_zero, "", "");
94  delete doc;
95  return;
96  }
97 
98  reportAlways("Found legacy hair.xml")
99  for_each_xml_child_node(node, root)
100  {
101  if (xmlNameEqual(node, "include"))
102  {
103  const std::string name = XML::getProperty(node, "name", "");
104  if (!name.empty())
105  loadHair(name, colors, skipError);
106  continue;
107  }
108  else if (xmlNameEqual(node, "color"))
109  {
111  node, "id", 0), ItemColor);
112 
113  if (colors.find(id) != colors.end())
114  {
115  reportAlways("ColorDB: Redefinition of dye ID %d",
116  toInt(id, int))
117  }
118 
119  colors[id] = ItemColorData(id, XML::langProperty(node, "name", ""),
120  XML::getProperty(node, "value", "#FFFFFF"));
121  }
122  }
123 
124  delete doc;
125 }
126 
127 void ColorDB::loadColorLists(const std::string &fileName,
128  const SkipError skipError)
129 {
132  skipError);
133  XmlNodeConstPtrConst root = doc->rootNode();
134  if (root == nullptr)
135  {
136  delete doc;
137  return;
138  }
139 
140  for_each_xml_child_node(node, root)
141  {
142  if (xmlNameEqual(node, "include"))
143  {
144  const std::string name = XML::getProperty(node, "name", "");
145  if (!name.empty())
146  loadColorLists(name, skipError);
147  continue;
148  }
149  else if (xmlNameEqual(node, "list"))
150  {
151  const std::string name = XML::getProperty(node, "name", "");
152  if (name.empty())
153  continue;
154 
155  std::map <ItemColor, ItemColorData> colors;
156  const ColorListsIterator it = mColorLists.find(name);
157 
158  if (it != mColorLists.end())
159  colors = it->second;
160 
161  for_each_xml_child_node(colorNode, node)
162  {
163  if (xmlNameEqual(colorNode, "color"))
164  {
165  const int id = XML::getProperty(colorNode, "id", -1);
166  if (id > -1)
167  {
169  XML::langProperty(colorNode, "name", ""),
170  XML::getProperty(colorNode, "value", ""));
171  colors[c.id] = c;
172  }
173  }
174  }
175  mColorLists[name] = colors;
176  }
177  }
178  delete doc;
179 }
180 
182 {
183  logger->log1("Unloading color database...");
184 
185  mColorLists.clear();
186  mLoaded = false;
187 }
188 
189 std::string &ColorDB::getHairColorName(const ItemColor id)
190 {
191  if (!mLoaded)
192  load();
193 
194  const ColorListsIterator it = mColorLists.find("hair");
195  if (it == mColorLists.end())
196  {
197  reportAlways("ColorDB: Error, hair colors list empty")
198  return mFail;
199  }
200 
201  const ColorIterator i = (*it).second.find(id);
202 
203  if (i == (*it).second.end())
204  {
205  reportAlways("ColorDB: Error, unknown dye ID# %d",
206  toInt(id, int))
207  return mFail;
208  }
209  return i->second.name;
210 }
211 
213 {
214  return mHairColorsSize;
215 }
216 
217 const std::map <ItemColor, ItemColorData>
218  *ColorDB::getColorsList(const std::string &name)
219 {
220  const ColorListsIterator it = mColorLists.find(name);
221 
222  if (it != mColorLists.end())
223  return &it->second;
224  return nullptr;
225 }
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
ItemColorData
Definition: itemcolordata.h:31
ColorDB::ColorListsIterator
ColorLists::iterator ColorListsIterator
Definition: colordb.h:71
cast.h
SkipError_true
const bool SkipError_true
Definition: skiperror.h:29
ColorDB::ColorIterator
Colors::iterator ColorIterator
Definition: colordb.h:68
anonymous_namespace{colordb.cpp}::mColorLists
ColorDB::ColorLists mColorLists
Definition: colordb.cpp:38
ColorDB::getHairColorName
std::string & getHairColorName(const ItemColor id)
Definition: colordb.cpp:189
Net::unload
void unload()
Definition: net.cpp:179
paths
Configuration paths
Definition: configuration.cpp:55
anonymous_namespace{colordb.cpp}::mHairColorsSize
int mHairColorsSize
Definition: colordb.cpp:35
anonymous_namespace{colordb.cpp}::mLoaded
bool mLoaded
Definition: colordb.cpp:36
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
StringVect
std::vector< std::string > StringVect
Definition: stringvector.h:28
XML::Document
Definition: libxml.h:52
toInt
#define toInt(val, name)
Definition: intdefines.h:46
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
bool
logger
Logger * logger
Definition: logger.cpp:88
ColorDB::unload
void unload()
Definition: colordb.cpp:181
ColorDB::load
void load()
Definition: colordb.cpp:41
fileName
std::string fileName
Definition: testmain.cpp:38
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
checkutils.h
beingcommon.h
colordb.h
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
ColorDB::getHairSize
int getHairSize()
Definition: colordb.cpp:212
ItemColor
uint16_t ItemColor
Definition: itemcolor.h:29
ColorDB::loadHair
void loadHair(const std::string &fileName, std::map< ItemColor, ItemColorData > &colors, const SkipError skipError)
Definition: colordb.cpp:80
XML::langProperty
std::string langProperty(const xmlNodePtr node, const char *const name, const std::string &def)
Definition: libxml.cpp:257
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
ItemColorData::id
ItemColor id
Definition: itemcolordata.h:51
VirtFs::getFilesInDir
void getFilesInDir(const std::string &dir, StringVect &list, const std::string &ext)
Definition: tools.cpp:80
configuration.h
ColorDB::ColorLists
std::map< std::string, std::map< ItemColor, ItemColorData > > ColorLists
Definition: colordb.h:70
fromInt
#define fromInt(val, name)
Definition: intdefines.h:45
XML::Document::rootNode
xmlNodePtr rootNode()
Definition: libxml.cpp:168
AvatarDB::load
void load()
Definition: avatardb.cpp:45
ItemColor_zero
const ItemColor ItemColor_zero
Definition: itemcolor.h:29
StringVectCIter
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:30
CAST_S32
#define CAST_S32
Definition: cast.h:29
ColorDB::loadColorLists
void loadColorLists(const std::string &fileName, const SkipError skipError)
Definition: colordb.cpp:127
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
ColorDB::getColorsList
const std::map< ItemColor, ItemColorData > * getColorsList(const std::string &name)
Definition: colordb.cpp:218
anonymous_namespace{colordb.cpp}::mFail
std::string mFail("#ffffff")
Logger::log
void log(const char *const log_text,...)
Definition: logger.cpp:268
FOR_EACH
#define FOR_EACH(type, iter, array)
Definition: foreach.h:24
debug.h