ManaPlus
Typedefs | Functions
HorseDB Namespace Reference

Typedefs

typedef HorseInfos::iterator HorseInfosIterator
 

Functions

void load ()
 
void loadXmlFile (const std::string &fileName, const SkipError skipError)
 
void loadSpecialXmlFile (const std::string &fileName, const SkipError skipError)
 
void unload ()
 
HorseInfoget (const int id, const bool allowNull)
 
int size ()
 

Detailed Description

Horse information database.

Typedef Documentation

◆ HorseInfosIterator

typedef HorseInfos::iterator HorseDB::HorseInfosIterator

Definition at line 57 of file horsedb.h.

Function Documentation

◆ get()

HorseInfo * HorseDB::get ( const int  id,
const bool  allowNull 
)

Definition at line 285 of file horsedb.cpp.

286 {
287  const HorseInfos::const_iterator i = mHorseInfos.find(id);
288 
289  if (i == mHorseInfos.end())
290  {
291  if (allowNull)
292  return nullptr;
293  reportAlways("HorseDB: Warning, unknown horse ID %d requested",
294  id)
295  return &mUnknown;
296  }
297  return i->second;
298 }
#define reportAlways(...)
Definition: checkutils.h:253

References anonymous_namespace{horsedb.cpp}::mHorseInfos, anonymous_namespace{horsedb.cpp}::mUnknown, and reportAlways.

Referenced by Being::setHorse().

◆ load()

void HorseDB::load ( )

Definition at line 60 of file horsedb.cpp.

61 {
62  if (mLoaded)
63  unload();
64 
65  logger->log1("Initializing horse database...");
66 
67  SpriteReference *currentSprite = new SpriteReference;
68  currentSprite->sprite = pathJoin(paths.getStringValue("sprites"),
69  paths.getStringValue("spriteErrorFile"));
70  currentSprite->variant = 0;
71  mUnknown.downSprites.push_back(currentSprite);
72 
73  currentSprite = new SpriteReference;
74  currentSprite->sprite = pathJoin(paths.getStringValue("sprites"),
75  paths.getStringValue("spriteErrorFile"));
76  currentSprite->variant = 0;
77  mUnknown.upSprites.push_back(currentSprite);
78 
80  loadXmlFile(paths.getStringValue("horsesPatchFile"), SkipError_true);
81  loadXmlDir("horsesPatchDir", loadXmlFile)
82 
83  mLoaded = true;
84 }
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
std::string getStringValue(const std::string &key) const
void log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: net.cpp:180
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
std::string pathJoin(std::string str1, const std::string &str2)
std::vector< SpriteReference * > upSprites
Definition: horseinfo.h:53
std::vector< SpriteReference * > downSprites
Definition: horseinfo.h:52
std::string sprite

References HorseInfo::downSprites, Configuration::getStringValue(), loadXmlDir, loadXmlFile(), Logger::log1(), logger, anonymous_namespace{horsedb.cpp}::mLoaded, anonymous_namespace{horsedb.cpp}::mUnknown, pathJoin(), paths, SkipError_false, SkipError_true, SpriteReference::sprite, Net::unload(), HorseInfo::upSprites, and SpriteReference::variant.

Referenced by DbManager::loadDb().

◆ loadSpecialXmlFile()

void HorseDB::loadSpecialXmlFile ( const std::string &  fileName,
const SkipError  skipError 
)

◆ loadXmlFile()

void HorseDB::loadXmlFile ( const std::string &  fileName,
const SkipError  skipError 
)

Definition at line 149 of file horsedb.cpp.

151 {
154  skipError);
155  XmlNodePtrConst rootNode = doc.rootNode();
156 
157  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "horses"))
158  {
159  logger->log("Horse Database: Error while loading %s!",
160  fileName.c_str());
161  return;
162  }
163 
164  // iterate <emote>s
165  for_each_xml_child_node(horseNode, rootNode)
166  {
167  if (xmlNameEqual(horseNode, "include"))
168  {
169  const std::string name = XML::getProperty(horseNode, "name", "");
170  if (!name.empty())
171  loadXmlFile(name, skipError);
172  continue;
173  }
174  else if (!xmlNameEqual(horseNode, "horse"))
175  {
176  continue;
177  }
178 
179  const int id = XML::getProperty(horseNode, "id", -1);
180 
181  if (id == -1)
182  {
183  reportAlways("Horse Database: Horse with missing ID in %s!",
184  paths.getStringValue("horsesFile").c_str())
185  continue;
186  }
187  HorseInfo *currentInfo = nullptr;
188  if (mHorseInfos.find(id) != mHorseInfos.end())
189  currentInfo = mHorseInfos[id];
190  else
191  currentInfo = new HorseInfo;
192 
193  if (currentInfo == nullptr)
194  continue;
195  for_each_xml_child_node(spriteNode, horseNode)
196  {
197  if (xmlNameEqual(spriteNode, "down"))
198  loadDownSprites(spriteNode, currentInfo);
199  else if (xmlNameEqual(spriteNode, "up"))
200  loadUpSprites(spriteNode, currentInfo);
201  else if (xmlNameEqual(spriteNode, "offset"))
202  loadRiderOffset(spriteNode, currentInfo);
203  }
204  mHorseInfos[id] = currentInfo;
205  }
206 }
void log(const char *const log_text,...)
Definition: logger.cpp:269
#define new
Definition: debug_new.h:147
if(!vert) return
static void loadRiderOffset(xmlNode *const node, HorseInfo *const currentInfo)
Definition: horsedb.cpp:130
static void loadDownSprites(xmlNode *const parentNode, HorseInfo *const currentInfo)
Definition: horsedb.cpp:238
static void loadUpSprites(xmlNode *const parentNode, HorseInfo *const currentInfo)
Definition: horsedb.cpp:252
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
bool find(const std::string &key)
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, XML::getProperty(), Configuration::getStringValue(), loadDownSprites(), loadRiderOffset(), loadUpSprites(), loadXmlFile(), Logger::log(), logger, anonymous_namespace{horsedb.cpp}::mHorseInfos, paths, reportAlways, XML::Document::rootNode(), and UseVirtFs_true.

◆ size()

int HorseDB::size ( )

Definition at line 300 of file horsedb.cpp.

301 {
302  return static_cast<signed int>(mHorseInfos.size());
303 }

References anonymous_namespace{horsedb.cpp}::mHorseInfos.

◆ unload()

void HorseDB::unload ( )

Definition at line 266 of file horsedb.cpp.

267 {
268  logger->log1("Unloading horse database...");
269  FOR_EACH (HorseInfos::const_iterator, i, mHorseInfos)
270  {
271  delete_all(i->second->upSprites);
272  delete_all(i->second->downSprites);
273  delete i->second;
274  }
275  mHorseInfos.clear();
276 
279  mUnknown.upSprites.clear();
280  mUnknown.downSprites.clear();
281 
282  mLoaded = false;
283 }
void delete_all(Container &c)
Definition: dtor.h:56
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25

References delete_all(), HorseInfo::downSprites, FOR_EACH, Logger::log1(), logger, anonymous_namespace{horsedb.cpp}::mHorseInfos, anonymous_namespace{horsedb.cpp}::mLoaded, anonymous_namespace{horsedb.cpp}::mUnknown, and HorseInfo::upSprites.

Referenced by DbManager::unloadDb().