ManaPlus
moddb.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/moddb.h"
25 
26 #include "configuration.h"
27 #include "logger.h"
28 
29 #include "resources/beingcommon.h"
30 
31 #include "utils/dtor.h"
32 #include "utils/gettext.h"
33 
34 #include "debug.h"
35 
36 namespace
37 {
39  bool mLoaded = false;
40 } // namespace
41 
43 {
44  if (mLoaded)
45  unload();
46  logger->log1("Initializing mod database...");
49  loadXmlDir("modsPatchDir", loadXmlFile)
50  mLoaded = true;
51 }
52 
53 void ModDB::loadXmlFile(const std::string &fileName,
54  const SkipError skipError)
55 {
56  XML::Document doc(fileName, UseVirtFs_true, skipError);
57  XmlNodeConstPtrConst rootNode = doc.rootNode();
58 
59  if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "mods"))
60  {
61  logger->log("Mods Database: Error while loading %s!",
62  fileName.c_str());
63  return;
64  }
65 
66  for_each_xml_child_node(modNode, rootNode)
67  {
68  if (xmlNameEqual(modNode, "include"))
69  {
70  const std::string name = XML::getProperty(modNode, "name", "");
71  if (!name.empty())
72  loadXmlFile(name, skipError);
73  continue;
74  }
75 
76  if (!xmlNameEqual(modNode, "mod"))
77  continue;
78 
79  const std::string name = XML::langProperty(
80  // TRANSLATORS: unknown info name
81  modNode, "name", _("unnamed"));
82  ModInfo *currentInfo = nullptr;
83  if (mModInfos.find(name) != mModInfos.end())
84  currentInfo = mModInfos[name];
85  if (currentInfo == nullptr)
86  currentInfo = new ModInfo;
87 
88  currentInfo->setName(name);
89  currentInfo->setDescription(XML::langProperty(
90  modNode, "description", ""));
91  currentInfo->setHelp(XML::getProperty(
92  modNode, "help", ""));
93  currentInfo->setLocalDir(XML::getProperty(
94  modNode, "localdir", ""));
95 
96  mModInfos[name] = currentInfo;
97  }
98 }
99 
101 {
102  logger->log1("Unloading mod database...");
104  mModInfos.clear();
105  mLoaded = false;
106 }
107 
108 ModInfo *ModDB::get(const std::string &name)
109 {
110  const ModInfoIterator i = mModInfos.find(name);
111  if (i == mModInfos.end())
112  return nullptr;
113  return i->second;
114 }
115 
117 {
118  return mModInfos;
119 }
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
void setDescription(const std::string &text)
Definition: modinfo.h:43
void setLocalDir(const std::string &text)
Definition: modinfo.h:55
void setName(const std::string &name)
Definition: modinfo.h:37
void setHelp(const std::string &text)
Definition: modinfo.h:49
xmlNodePtr rootNode()
Definition: libxml.cpp:169
Configuration paths
void delete_all(Container &c)
Definition: dtor.h:56
#define _(s)
Definition: gettext.h:35
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:161
Logger * logger
Definition: logger.cpp:89
std::map< std::string, ModInfo * > ModInfos
Definition: modinfo.h:68
ModInfos::iterator ModInfoIterator
Definition: modinfo.h:69
ModInfo * get(const std::string &name)
Definition: moddb.cpp:108
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: moddb.cpp:53
void load()
Definition: moddb.cpp:42
void unload()
Definition: moddb.cpp:100
const ModInfos & getAll()
Definition: moddb.cpp:116
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
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