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