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