ManaPlus
avatardb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2004-2009 The Mana World Development Team
4  * Copyright (C) 2009-2010 The Mana Developers
5  * Copyright (C) 2011-2019 The ManaPlus Developers
6  * Copyright (C) 2019-2021 Andrei Karas
7  *
8  * This file is part of The ManaPlus Client.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include "resources/db/avatardb.h"
25 
26 #include "logger.h"
27 
28 #include "resources/beingcommon.h"
29 #include "resources/beinginfo.h"
30 
32 
33 #include "utils/dtor.h"
34 #include "utils/gettext.h"
35 
36 #include "configuration.h"
37 
38 #include "debug.h"
39 
40 namespace
41 {
43  bool mLoaded = false;
44 } // namespace
45 
47 {
48  if (mLoaded)
49  unload();
50 
51  logger->log1("Initializing avatar database...");
53  loadXmlFile(paths.getStringValue("avatarsPatchFile"), SkipError_true);
54  loadXmlDir("avatarsPatchDir", loadXmlFile)
55 }
56 
57 void AvatarDB::loadXmlFile(const std::string &fileName,
58  const SkipError skipError)
59 {
62  skipError);
63  XmlNodeConstPtrConst rootNode = doc.rootNode();
64 
65  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "avatars"))
66  {
67  logger->log("Avatars Database: Error while loading %s!",
68  fileName.c_str());
69  mLoaded = true;
70  return;
71  }
72 
73  for_each_xml_child_node(avatarNode, rootNode)
74  {
75  if (xmlNameEqual(avatarNode, "include"))
76  {
77  const std::string name = XML::getProperty(avatarNode, "name", "");
78  if (!name.empty())
79  loadXmlFile(name, skipError);
80  continue;
81  }
82 
83  if (!xmlNameEqual(avatarNode, "avatar"))
84  continue;
85 
87  avatarNode, "id", 0), BeingTypeId);
88  BeingInfo *currentInfo = nullptr;
89  if (mAvatarInfos.find(id) != mAvatarInfos.end())
90  currentInfo = mAvatarInfos[id];
91  if (currentInfo == nullptr)
92  currentInfo = new BeingInfo;
93 
94  currentInfo->setName(XML::langProperty(
95  // TRANSLATORS: unknown info name
96  avatarNode, "name", _("unnamed")));
97 
98  currentInfo->setTargetOffsetX(XML::getProperty(avatarNode,
99  "targetOffsetX", 0));
100 
101  currentInfo->setTargetOffsetY(XML::getProperty(avatarNode,
102  "targetOffsetY", 0));
103 
104  currentInfo->setWidth(XML::getProperty(avatarNode,
105  "width", 0));
106  currentInfo->setHeight(XML::getProperty(avatarNode,
107  "height", 0));
108 
109  SpriteDisplay display;
110 
111  // iterate <sprite>s and <sound>s
112  for_each_xml_child_node(spriteNode, avatarNode)
113  {
114  if (xmlNameEqual(spriteNode, "sprite"))
115  {
116  if (!XmlHaveChildContent(spriteNode))
117  continue;
118 
119  SpriteReference *const currentSprite = new SpriteReference;
120  currentSprite->sprite = XmlChildContent(spriteNode);
121  currentSprite->variant = XML::getProperty(
122  spriteNode, "variant", 0);
123  display.sprites.push_back(currentSprite);
124  }
125  }
126  currentInfo->setDisplay(display);
127  mAvatarInfos[id] = currentInfo;
128  }
129 
130  mLoaded = true;
131 }
132 
134 {
135  logger->log1("Unloading avatar database...");
137  mAvatarInfos.clear();
138  mLoaded = false;
139 }
140 
142 {
143  const BeingInfoIterator i = mAvatarInfos.find(id);
144  if (i == mAvatarInfos.end())
145  return BeingInfo::unknown;
146  return i->second;
147 }
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:43
#define loadXmlDir(name, function)
Definition: beingcommon.h:39
BeingInfos::iterator BeingInfoIterator
Definition: beinginfo.h:410
std::map< BeingTypeId, BeingInfo * > BeingInfos
Definition: beinginfo.h:409
int BeingTypeId
Definition: beingtypeid.h:30
void setName(const std::string &name)
Definition: beinginfo.h:65
void setDisplay(const SpriteDisplay &display)
Definition: beinginfo.cpp:127
void setTargetOffsetY(const int n)
Definition: beinginfo.h:138
static BeingInfo * unknown
Definition: beinginfo.h:56
void setWidth(const int n)
Definition: beinginfo.h:210
void setHeight(const int n)
Definition: beinginfo.h:213
void setTargetOffsetX(const int n)
Definition: beinginfo.h:132
std::string getStringValue(const std::string &key) const
void log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
void delete_all(Container &c)
Definition: dtor.h:56
#define _(s)
Definition: gettext.h:35
#define fromInt(val, name)
Definition: intdefines.h:46
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
void load()
Definition: avatardb.cpp:46
void unload()
Definition: avatardb.cpp:133
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: avatardb.cpp:57
BeingInfo * get(const BeingTypeId id)
Definition: avatardb.cpp:141
void unload()
Definition: net.cpp:180
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
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
std::vector< SpriteReference * > sprites
Definition: spritedisplay.h:47
std::string sprite
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30