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 68 of file colordb.h.

◆ ColorLists

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

Definition at line 70 of file colordb.h.

◆ ColorListsIterator

typedef ColorLists::iterator ColorDB::ColorListsIterator

Definition at line 71 of file colordb.h.

◆ Colors

Definition at line 67 of file colordb.h.

Function Documentation

◆ getColorsList()

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

Definition at line 218 of file colordb.cpp.

219 {
220  const ColorListsIterator it = mColorLists.find(name);
221 
222  if (it != mColorLists.end())
223  return &it->second;
224  return nullptr;
225 }

References anonymous_namespace{colordb.cpp}::mColorLists.

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

◆ getHairColorName()

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

Definition at line 189 of file colordb.cpp.

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 }

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 41 of file colordb.cpp.

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 }

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 127 of file colordb.cpp.

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 }

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 80 of file colordb.cpp.

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 }

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

Clear the chars data

Definition at line 181 of file colordb.cpp.

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

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

Referenced by DbManager::unloadDb().

reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
ItemColorData
Definition: itemcolordata.h:31
ColorDB::ColorListsIterator
ColorLists::iterator ColorListsIterator
Definition: colordb.h:71
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
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
logger
Logger * logger
Definition: logger.cpp:88
AttributesEnum::find
bool find(const std::string &key)
Definition: attributesstrings.cpp:32
if
if(!vert) return
fileName
std::string fileName
Definition: testmain.cpp:38
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
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
VirtFs::getFilesInDir
void getFilesInDir(const std::string &dir, StringVect &list, const std::string &ext)
Definition: tools.cpp:80
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
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