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