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