ManaPlus
clandb.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/clandb.h"
25 
26 #include "configuration.h"
27 
28 #include "resources/beingcommon.h"
29 #include "resources/claninfo.h"
30 
32 
33 #include "utils/checkutils.h"
34 #include "utils/dtor.h"
35 #include "utils/gettext.h"
36 #include "utils/itemxmlutils.h"
37 
38 #include "debug.h"
39 
40 namespace
41 {
42  std::map<int, ClanInfo *> mClansInfos;
43  bool mLoaded = false;
44 } // namespace
45 
47 {
48  if (mLoaded)
49  unload();
50 
51  logger->log1("Initializing clans database...");
53  loadXmlFile(paths.getStringValue("clansPatchFile"), SkipError_true);
54  loadXmlDir("clansPatchDir", loadXmlFile)
55 
56  mLoaded = true;
57 }
58 
59 void ClanDb::loadXmlFile(const std::string &fileName,
60  const SkipError skipError)
61 {
62  XML::Document doc(fileName, UseVirtFs_true, skipError);
63  XmlNodeConstPtr rootNode = doc.rootNode();
64 
65  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "clans"))
66  {
67  logger->log("Clans database: Error while loading %s!",
68  paths.getStringValue("clansFile").c_str());
69  mLoaded = true;
70  return;
71  }
72 
73  const ItemFieldInfos &addFields =
75 
76  // iterate <clan>s
77  for_each_xml_child_node(clanNode, rootNode)
78  {
79  if (xmlNameEqual(clanNode, "include"))
80  {
81  const std::string name = XML::getProperty(
82  clanNode, "name", "");
83  if (!name.empty())
84  loadXmlFile(name, skipError);
85  continue;
86  }
87  if (!xmlNameEqual(clanNode, "clan"))
88  continue;
89 
90  const int id = XML::getProperty(clanNode, "id", 0);
91  ClanInfo *clanInfo = nullptr;
92  if (mClansInfos.find(id) != mClansInfos.end())
93  {
94  reportAlways("ClanDb: Redefinition of clan ID %d", id)
95  clanInfo = mClansInfos[id];
96  }
97  if (clanInfo == nullptr)
98  clanInfo = new ClanInfo;
99 
100  clanInfo->id = id;
101  clanInfo->name = XML::langProperty(
102  // TRANSLATORS: unknown clan name
103  clanNode, "name", _("unnamed"));
104 
105  readItemStatsVector(clanInfo->stats,
106  clanNode,
107  addFields);
108 
109  mClansInfos[id] = clanInfo;
110  }
111 }
112 
114 {
115  logger->log1("Unloading clans database...");
117  mClansInfos.clear();
118 
119  mLoaded = false;
120 }
121 
122 const ClanInfo *ClanDb::get(const int clanId)
123 {
124  std::map<int, ClanInfo *>::const_iterator i =
125  mClansInfos.find(clanId);
126 
127  if (i == mClansInfos.end())
128  {
129  i = mClansInfos.find(clanId);
130  if (i == mClansInfos.end())
131  {
132  reportAlways("ClanDb: Warning, unknown clan ID "
133  "%d requested",
134  clanId)
135  return nullptr;
136  }
137  }
138  return i->second;
139 }
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
#define reportAlways(...)
Definition: checkutils.h:253
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
std::map< std::string, ItemFieldType * > ItemFieldInfos
void readItemStatsVector(std::vector< std::string > &effect, const xmlNodePtr node, const ItemFieldInfos &fields)
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: clandb.cpp:59
const ClanInfo * get(const int clanId)
Definition: clandb.cpp:122
void load()
Definition: clandb.cpp:46
void unload()
Definition: clandb.cpp:113
const ItemFieldInfos & getAddFields()
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
std::map< int, ClanInfo * > mClansInfos
Definition: clandb.cpp:42
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
int id
Definition: claninfo.h:43
std::vector< std::string > stats
Definition: claninfo.h:41
std::string name
Definition: claninfo.h:42
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30