ManaPlus
Typedefs | Functions
ColorDB Namespace Reference

Typedefs

typedef std::map< ItemColor, ItemColorDataColors
 
typedef Colors::iterator ColorIterator
 
typedef std::map< std::string, std::map< ItemColor, ItemColorData > > ColorLists
 
typedef ColorLists::iterator ColorListsIterator
 

Functions

void load ()
 
void loadHair (const std::string &fileName, std::map< ItemColor, ItemColorData > &colors, const SkipError skipError)
 
void loadColorLists (const std::string &fileName, const SkipError skipError)
 
void unload ()
 
std::string & getHairColorName (const ItemColor id)
 
int getHairSize ()
 
const std::map< ItemColor, ItemColorData > * getColorsList (const std::string &name)
 

Detailed Description

Color information database.

Typedef Documentation

◆ ColorIterator

typedef Colors::iterator ColorDB::ColorIterator

Definition at line 69 of file colordb.h.

◆ ColorLists

typedef std::map<std::string, std::map <ItemColor, ItemColorData> > ColorDB::ColorLists

Definition at line 71 of file colordb.h.

◆ ColorListsIterator

typedef ColorLists::iterator ColorDB::ColorListsIterator

Definition at line 72 of file colordb.h.

◆ Colors

Definition at line 68 of file colordb.h.

Function Documentation

◆ getColorsList()

const std::map< ItemColor, ItemColorData > * ColorDB::getColorsList ( const std::string &  name)

Definition at line 219 of file colordb.cpp.

220 {
221  const ColorListsIterator it = mColorLists.find(name);
222 
223  if (it != mColorLists.end())
224  return &it->second;
225  return nullptr;
226 }
ColorLists::iterator ColorListsIterator
Definition: colordb.h:72
ColorDB::ColorLists mColorLists
Definition: colordb.cpp:39

References anonymous_namespace{colordb.cpp}::mColorLists.

Referenced by BeingInfo::setColorsList(), ItemInfo::setColorsList(), and ItemInfo::setIconColorsList().

◆ getHairColorName()

std::string & ColorDB::getHairColorName ( const ItemColor  id)

Definition at line 190 of file colordb.cpp.

191 {
192  if (!mLoaded)
193  load();
194 
195  const ColorListsIterator it = mColorLists.find("hair");
196  if (it == mColorLists.end())
197  {
198  reportAlways("ColorDB: Error, hair colors list empty")
199  return mFail;
200  }
201 
202  const ColorIterator i = (*it).second.find(id);
203 
204  if (i == (*it).second.end())
205  {
206  reportAlways("ColorDB: Error, unknown dye ID# %d",
207  toInt(id, int))
208  return mFail;
209  }
210  return i->second.name;
211 }
#define reportAlways(...)
Definition: checkutils.h:253
if(!vert) return
#define toInt(val, name)
Definition: intdefines.h:47
bool find(const std::string &key)
void load()
Definition: avatardb.cpp:46
Colors::iterator ColorIterator
Definition: colordb.h:69
std::string mFail("#ffffff")

References AvatarDB::load(), anonymous_namespace{colordb.cpp}::mColorLists, anonymous_namespace{colordb.cpp}::mFail(), anonymous_namespace{colordb.cpp}::mLoaded, reportAlways, and toInt.

Referenced by CharCreateDialog::updateHair().

◆ getHairSize()

int ColorDB::getHairSize ( )

◆ load()

void ColorDB::load ( )

Loads the color data from colors.xml.

Definition at line 42 of file colordb.cpp.

43 {
44  if (mLoaded)
45  unload();
46 
47  logger->log1("Initializing color database...");
48 
49  std::map<ItemColor, ItemColorData> colors;
50  ColorListsIterator it = mColorLists.find("hair");
51  if (it != mColorLists.end())
52  colors = it->second;
53  loadHair(paths.getStringValue("hairColorFile"),
54  colors,
56  loadHair(paths.getStringValue("hairColorPatchFile"),
57  colors,
59  StringVect list;
61  "hairColorPatchDir"), list, ".xml");
62  FOR_EACH (StringVectCIter, it2, list)
63  loadHair(*it2, colors, SkipError_true);
64 
65  mColorLists["hair"] = colors;
66 
67  loadColorLists(paths.getStringValue("itemColorsFile"),
69  loadColorLists(paths.getStringValue("itemColorsPatchFile"),
71  loadXmlDir("itemColorsPatchDir", loadColorLists)
72 
73  it = mColorLists.find("hair");
74  if (it != mColorLists.end())
75  mHairColorsSize = CAST_S32((*it).second.size());
76  else
77  mHairColorsSize = 0;
78  mLoaded = true;
79 }
#define loadXmlDir(name, function)
Definition: beingcommon.h:39
#define CAST_S32
Definition: cast.h:30
std::string getStringValue(const std::string &key) const
void log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Logger * logger
Definition: logger.cpp:89
void loadColorLists(const std::string &fileName, const SkipError skipError)
Definition: colordb.cpp:128
void loadHair(const std::string &fileName, std::map< ItemColor, ItemColorData > &colors, const SkipError skipError)
Definition: colordb.cpp:81
void unload()
Definition: net.cpp:180
void getFilesInDir(const std::string &dir, StringVect &list, const std::string &ext)
Definition: tools.cpp:81
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29

References CAST_S32, FOR_EACH, VirtFs::getFilesInDir(), Configuration::getStringValue(), loadColorLists(), loadHair(), loadXmlDir, Logger::log1(), logger, anonymous_namespace{colordb.cpp}::mColorLists, anonymous_namespace{colordb.cpp}::mHairColorsSize, anonymous_namespace{colordb.cpp}::mLoaded, paths, SkipError_false, SkipError_true, and Net::unload().

Referenced by DbManager::loadDb().

◆ loadColorLists()

void ColorDB::loadColorLists ( const std::string &  fileName,
const SkipError  skipError 
)

Definition at line 128 of file colordb.cpp.

130 {
133  skipError);
134  XmlNodeConstPtrConst root = doc->rootNode();
135  if (root == nullptr)
136  {
137  delete doc;
138  return;
139  }
140 
141  for_each_xml_child_node(node, root)
142  {
143  if (xmlNameEqual(node, "include"))
144  {
145  const std::string name = XML::getProperty(node, "name", "");
146  if (!name.empty())
147  loadColorLists(name, skipError);
148  continue;
149  }
150  else if (xmlNameEqual(node, "list"))
151  {
152  const std::string name = XML::getProperty(node, "name", "");
153  if (name.empty())
154  continue;
155 
156  std::map <ItemColor, ItemColorData> colors;
157  const ColorListsIterator it = mColorLists.find(name);
158 
159  if (it != mColorLists.end())
160  colors = it->second;
161 
162  for_each_xml_child_node(colorNode, node)
163  {
164  if (xmlNameEqual(colorNode, "color"))
165  {
166  const int id = XML::getProperty(colorNode, "id", -1);
167  if (id > -1)
168  {
170  XML::langProperty(colorNode, "name", ""),
171  XML::getProperty(colorNode, "value", ""));
172  colors[c.id] = c;
173  }
174  }
175  }
176  mColorLists[name] = colors;
177  }
178  }
179  delete doc;
180 }
xmlNodePtr rootNode()
Definition: libxml.cpp:169
#define fromInt(val, name)
Definition: intdefines.h:46
uint16_t ItemColor
Definition: itemcolor.h:30
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
std::string langProperty(const xmlNodePtr node, const char *const name, const std::string &def)
Definition: libxml.cpp:258
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30

References fileName, for_each_xml_child_node, fromInt, XML::getProperty(), ItemColorData::id, XML::langProperty(), anonymous_namespace{colordb.cpp}::mColorLists, XML::Document::rootNode(), and UseVirtFs_true.

Referenced by load().

◆ loadHair()

void ColorDB::loadHair ( const std::string &  fileName,
std::map< ItemColor, ItemColorData > &  colors,
const SkipError  skipError 
)

Loads the color data from colors.xml.

Definition at line 81 of file colordb.cpp.

84 {
87  skipError);
88  XmlNodeConstPtrConst root = doc->rootNode();
89 
90  if ((root == nullptr) || !xmlNameEqual(root, "colors"))
91  {
92  logger->log("ColorDB: Failed to find hair colors file.");
93  if (colors.find(ItemColor_zero) == colors.end())
94  colors[ItemColor_zero] = ItemColorData(ItemColor_zero, "", "");
95  delete doc;
96  return;
97  }
98 
99  reportAlways("Found legacy hair.xml")
100  for_each_xml_child_node(node, root)
101  {
102  if (xmlNameEqual(node, "include"))
103  {
104  const std::string name = XML::getProperty(node, "name", "");
105  if (!name.empty())
106  loadHair(name, colors, skipError);
107  continue;
108  }
109  else if (xmlNameEqual(node, "color"))
110  {
112  node, "id", 0), ItemColor);
113 
114  if (colors.find(id) != colors.end())
115  {
116  reportAlways("ColorDB: Redefinition of dye ID %d",
117  toInt(id, int))
118  }
119 
120  colors[id] = ItemColorData(id, XML::langProperty(node, "name", ""),
121  XML::getProperty(node, "value", "#FFFFFF"));
122  }
123  }
124 
125  delete doc;
126 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
const ItemColor ItemColor_zero
Definition: itemcolor.h:30

References fileName, for_each_xml_child_node, fromInt, XML::getProperty(), ItemColor_zero, XML::langProperty(), Logger::log(), logger, reportAlways, XML::Document::rootNode(), toInt, and UseVirtFs_true.

Referenced by load().

◆ unload()

void ColorDB::unload ( )

Clear the color data

Definition at line 182 of file colordb.cpp.

183 {
184  logger->log1("Unloading color database...");
185 
186  mColorLists.clear();
187  mLoaded = false;
188 }

References Logger::log1(), logger, anonymous_namespace{colordb.cpp}::mColorLists, and anonymous_namespace{colordb.cpp}::mLoaded.

Referenced by DbManager::unloadDb().