GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/moddb.cpp Lines: 8 46 17.4 %
Date: 2017-11-29 Branches: 0 84 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-2017  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
{
37
2
    ModInfos mModInfos;
38
    bool mLoaded = false;
39
}  // namespace
40
41
void ModDB::load()
42
{
43
    if (mLoaded)
44
        unload();
45
    logger->log1("Initializing mod database...");
46
    loadXmlFile(paths.getStringValue("modsFile"), SkipError_false);
47
    loadXmlFile(paths.getStringValue("modsPatchFile"), SkipError_true);
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
99
384
void ModDB::unload()
100
{
101
384
    logger->log1("Unloading mod database...");
102
384
    delete_all(mModInfos);
103
384
    mModInfos.clear();
104
384
    mLoaded = false;
105
384
}
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
115
const ModInfos &ModDB::getAll()
116
{
117
    return mModInfos;
118
4
}