ManaPlus
itemfielddb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2016-2019 The ManaPlus Developers
4  * Copyright (C) 2019-2021 Andrei Karas
5  *
6  * This file is part of The ManaPlus Client.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include "configuration.h"
25 
26 #include "utils/checkutils.h"
27 #include "utils/dtor.h"
28 
29 #include "resources/beingcommon.h"
30 
32 
33 #include "debug.h"
34 
35 namespace
36 {
39  bool mLoaded = false;
40 } // namespace
41 
43 {
44  if (mLoaded)
45  unload();
46 
47  logger->log1("Initializing item field database...");
48 
49  loadXmlFile(paths.getStringValue("itemFieldsFile"), SkipError_false);
50  loadXmlFile(paths.getStringValue("itemFieldsPatchFile"), SkipError_true);
51  loadXmlDir("itemFieldsPatchDir", loadXmlFile)
52  mLoaded = true;
53 }
54 
55 static void loadFields(XmlNodeConstPtr groupNode,
56  ItemFieldInfos &fields1,
57  ItemFieldInfos &fields2)
58 {
59  for_each_xml_child_node(node, groupNode)
60  {
61  if (!xmlNameEqual(node, "field"))
62  continue;
63 
64  const std::string name = XML::getProperty(node,
65  "name",
66  "");
67  if (name.empty())
68  {
69  reportAlways("Empty name field in ItemFieldDb")
70  continue;
71  }
72  const std::string description = XML::langProperty(node,
73  "description",
74  "");
75  if (description.empty())
76  {
77  reportAlways("Empty description field in ItemFieldDb")
78  continue;
79  }
80  const bool sign = XML::getBoolProperty(node,
81  "signed",
82  true);
83  if (fields2.find(name) != fields2.end())
84  {
86  "Same field name detected in requeted and add groups: %s",
87  name.c_str())
88  continue;
89  }
90  if (fields1.find(name) != fields1.end())
91  {
93  "Same field name detected: %s",
94  name.c_str())
95  continue;
96  }
97  fields1[name] = new ItemFieldType(name,
98  description,
99  sign);
100  }
101 }
102 
103 void ItemFieldDb::loadXmlFile(const std::string &fileName,
104  const SkipError skipError)
105 {
108  skipError);
109  XmlNodeConstPtrConst rootNode = doc.rootNode();
110 
111  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "itemfields"))
112  {
113  logger->log("ItemFieldDb: Error while loading %s!",
114  fileName.c_str());
115  return;
116  }
117 
118  for_each_xml_child_node(node, rootNode)
119  {
120  if (xmlNameEqual(node, "include"))
121  {
122  const std::string name = XML::getProperty(node, "name", "");
123  if (!name.empty())
124  loadXmlFile(name, skipError);
125  continue;
126  }
127 
128  if (xmlNameEqual(node, "required"))
130  else if (xmlNameEqual(node, "add"))
132  }
133 }
134 
136 {
137  logger->log1("Unloading item database...");
138 
140  mRequiredInfos.clear();
142  mAddInfos.clear();
143  mLoaded = false;
144 }
145 
147 {
148  return mRequiredInfos;
149 }
150 
152 {
153  return mAddInfos;
154 }
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
static void loadFields(const xmlNodePtr groupNode, ItemFieldInfos &fields1, ItemFieldInfos &fields2)
Definition: itemfielddb.cpp:55
std::map< std::string, ItemFieldType * > ItemFieldInfos
#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)
const ItemFieldInfos & getRequiredFields()
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
bool getBoolProperty(const xmlNodePtr node, const char *const name, const bool def)
Definition: libxml.cpp:269
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
std::string fileName
Definition: testmain.cpp:39
const bool UseVirtFs_true
Definition: usevirtfs.h:30