GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/moddb.cpp Lines: 8 49 16.3 %
Date: 2021-03-17 Branches: 0 82 0.0 %

Line Branch Exec Source
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
{
38
1
    ModInfos mModInfos;
39
    bool mLoaded = false;
40
}  // namespace
41
42
void ModDB::load()
43
{
44
    if (mLoaded)
45
        unload();
46
    logger->log1("Initializing mod database...");
47
    loadXmlFile(paths.getStringValue("modsFile"), SkipError_false);
48
    loadXmlFile(paths.getStringValue("modsPatchFile"), SkipError_true);
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
100
215
void ModDB::unload()
101
{
102
215
    logger->log1("Unloading mod database...");
103
215
    delete_all(mModInfos);
104
215
    mModInfos.clear();
105
215
    mLoaded = false;
106
215
}
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
116
const ModInfos &ModDB::getAll()
117
{
118
    return mModInfos;
119
2
}