ManaPlus
petdb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2008-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/petdb.h"
24 
25 #include "configuration.h"
26 
27 #include "resources/beingcommon.h"
28 #include "resources/beinginfo.h"
29 
31 
32 #include "utils/checkutils.h"
33 #include "utils/dtor.h"
34 #include "utils/gettext.h"
35 
36 #include "debug.h"
37 
38 namespace
39 {
41  bool mLoaded = false;
42 } // namespace
43 
45 {
46  if (mLoaded)
47  unload();
48 
49  logger->log1("Initializing PET database...");
52  loadXmlDir("petsPatchDir", loadXmlFile)
53  mLoaded = true;
54 }
55 
56 void PETDB::loadXmlFile(const std::string &fileName,
57  const SkipError skipError)
58 {
61  skipError);
62  XmlNodeConstPtrConst rootNode = doc.rootNode();
63 
64  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "pets"))
65  {
66  logger->log("PET Database: Error while loading %s!",
67  fileName.c_str());
68  return;
69  }
70 
71  // iterate <pet>s
72  for_each_xml_child_node(petNode, rootNode)
73  {
74  if (xmlNameEqual(petNode, "include"))
75  {
76  const std::string name = XML::getProperty(petNode, "name", "");
77  if (!name.empty())
78  loadXmlFile(name, skipError);
79  continue;
80  }
81  else if (!xmlNameEqual(petNode, "pet"))
82  {
83  continue;
84  }
85 
87  petNode, "id", -1), BeingTypeId);
88  if (id == BeingTypeId_negOne)
89  {
90  reportAlways("PET Database: PET with missing ID in %s!",
91  paths.getStringValue("petsFile").c_str())
92  continue;
93  }
94 
95  BeingInfo *currentInfo = nullptr;
96  if (mPETInfos.find(id) != mPETInfos.end())
97  currentInfo = mPETInfos[id];
98  if (currentInfo == nullptr)
99  currentInfo = new BeingInfo;
100 
101  currentInfo->setName(XML::langProperty(petNode,
102  // TRANSLATORS: unknown info name
103  "name", _("pet")));
104 
105  currentInfo->setTargetSelection(XML::getBoolProperty(petNode,
106  "targetSelection", true));
107 
108  BeingCommon::readBasicAttributes(currentInfo, petNode, "talk");
109  BeingCommon::readWalkingAttributes(currentInfo, petNode, 0);
110 
111  currentInfo->setDeadSortOffsetY(XML::getProperty(petNode,
112  "deadSortOffsetY", 31));
113 
114  const std::string returnMessage = XML::langProperty(petNode,
115  // TRANSLATORS: popup menu item
116  // TRANSLATORS: pet return to egg
117  "removeMessage", _("Return to egg"));
118  currentInfo->setString(0, returnMessage);
119 
120  SpriteDisplay display;
121  for_each_xml_child_node(spriteNode, petNode)
122  {
123  if (!XmlHaveChildContent(spriteNode))
124  continue;
125 
126  if (xmlNameEqual(spriteNode, "sprite"))
127  {
128  SpriteReference *const currentSprite = new SpriteReference;
129  currentSprite->sprite = XmlChildContent(spriteNode);
130  currentSprite->variant =
131  XML::getProperty(spriteNode, "variant", 0);
132  display.sprites.push_back(currentSprite);
133  }
134  else if (xmlNameEqual(spriteNode, "particlefx"))
135  {
136  std::string particlefx = XmlChildContent(spriteNode);
137  display.particles.push_back(particlefx);
138  }
139  }
140 
141  currentInfo->setDisplay(display);
142 
143  mPETInfos[id] = currentInfo;
144  }
145 }
146 
148 {
149  logger->log1("Unloading PET database...");
151  mPETInfos.clear();
152 
153  mLoaded = false;
154 }
155 
157 {
158  const BeingInfoIterator i = mPETInfos.find(id);
159 
160  if (i == mPETInfos.end())
161  {
162  reportAlways("PETDB: Warning, unknown PET ID %d requested",
163  toInt(id, int))
164  return BeingInfo::unknown;
165  }
166  return i->second;
167 }
SkipError
bool SkipError
Definition: skiperror.h:29
BeingInfos
std::map< BeingTypeId, BeingInfo * > BeingInfos
Definition: beinginfo.h:408
anonymous_namespace{petdb.cpp}::mPETInfos
BeingInfos mPETInfos
Definition: petdb.cpp:40
BeingInfo::setDisplay
void setDisplay(const SpriteDisplay &display)
Definition: beinginfo.cpp:126
reportAlways
#define reportAlways(...)
Definition: checkutils.h:252
SkipError_true
const bool SkipError_true
Definition: skiperror.h:29
PETDB::get
BeingInfo * get(const BeingTypeId id)
Definition: petdb.cpp:156
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
anonymous_namespace{petdb.cpp}::mLoaded
bool mLoaded
Definition: petdb.cpp:41
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
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
fileName
std::string fileName
Definition: testmain.cpp:38
SpriteReference
Definition: spritereference.h:30
PETDB::load
void load()
Definition: petdb.cpp:44
XML::getBoolProperty
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:268
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
checkutils.h
petdb.h
BeingInfo::setString
void setString(const int idx, const std::string &value)
Definition: beinginfo.h:343
BeingInfo::setTargetSelection
void setTargetSelection(const bool n)
Definition: beinginfo.h:179
beingcommon.h
BeingTypeId_negOne
const BeingTypeId BeingTypeId_negOne
Definition: beingtypeid.h:30
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
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
BeingCommon::readBasicAttributes
void readBasicAttributes(BeingInfo *const info, xmlNode *const node, const std::string &hoverCursor)
Definition: beingcommon.cpp:36
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
SpriteDisplay::particles
StringVect particles
Definition: spritedisplay.h:47
PETDB::loadXmlFile
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: petdb.cpp:56
delete_all
void delete_all(Container &c)
Definition: dtor.h:55
BeingInfo::setDeadSortOffsetY
void setDeadSortOffsetY(const int n)
Definition: beinginfo.h:194
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
BeingCommon::readWalkingAttributes
void readWalkingAttributes(BeingInfo *const info, xmlNode *const node, const int moreBlockFlags)
Definition: beingcommon.cpp:59
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
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
PETDB::unload
void unload()
Definition: petdb.cpp:147