ManaPlus
deaddb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-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 
22 #include "resources/db/deaddb.h"
23 
24 #include "configuration.h"
25 #include "logger.h"
26 
28 
29 #include "resources/beingcommon.h"
30 
31 #include "debug.h"
32 
33 namespace
34 {
35  bool mLoaded = false;
36  STD_VECTOR<std::string> mMessages;
37 } // namespace
38 
40 {
41  if (mLoaded)
42  unload();
43 
44  logger->log1("Initializing dead database...");
45 
46  loadXmlFile(paths.getStringValue("deadMessagesFile"), SkipError_false);
47  loadXmlFile(paths.getStringValue("deadMessagesPatchFile"), SkipError_true);
48  loadXmlDir("deadMessagesPatchDir", loadXmlFile)
49  mLoaded = true;
50 }
51 
52 void DeadDB::loadXmlFile(const std::string &fileName,
53  const SkipError skipError)
54 {
57  skipError);
58  XmlNodeConstPtrConst root = doc->rootNode();
59 
60  if ((root == nullptr) || !xmlNameEqual(root, "messages"))
61  {
62  logger->log("DeadDB: Failed to parse %s.",
63  paths.getStringValue("deadMessagesFile").c_str());
64  delete doc;
65  return;
66  }
67 
68  for_each_xml_child_node(node, root)
69  {
70  if (xmlNameEqual(node, "include"))
71  {
72  const std::string name = XML::getProperty(node, "name", "");
73  if (!name.empty())
74  loadXmlFile(name, skipError);
75  continue;
76  }
77  else if (xmlNameEqual(node, "message"))
78  {
79  XmlChar *const data = reinterpret_cast<XmlChar*>(
80  XmlNodeGetContent(node));
81  if (data == nullptr)
82  continue;
83  if (*data == 0)
84  {
85  XmlFree(data);
86  continue;
87  }
88  mMessages.push_back(data);
89  XmlFree(data);
90  }
91  }
92 
93  delete doc;
94 }
95 
97 {
98  logger->log1("Unloading dead database...");
99  mMessages.clear();
100  mLoaded = false;
101 }
102 
104 {
105  const size_t sz = mMessages.size();
106  if (sz == 0U)
107  return std::string();
108  return translator->getStr(mMessages[rand() % sz]);
109 }
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 log(const char *const log_text,...)
Definition: logger.cpp:269
void log1(const char *const log_text)
Definition: logger.cpp:238
const std::string getStr(const std::string &str)
Definition: podict.cpp:45
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
uint32_t data
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: deaddb.cpp:52
std::string getRandomString()
Definition: deaddb.cpp:103
void unload()
Definition: deaddb.cpp:96
void load()
Definition: deaddb.cpp:39
void unload()
Definition: net.cpp:180
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:174
std::vector< std::string > mMessages
Definition: deaddb.cpp:36
PoDict * translator
Definition: podict.cpp:28
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