GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/clandb.cpp Lines: 2 48 4.2 %
Date: 2018-09-20 Branches: 0 86 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-2018  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/clandb.h"
24
25
#include "configuration.h"
26
27
#include "resources/beingcommon.h"
28
#include "resources/claninfo.h"
29
30
#include "resources/db/itemfielddb.h"
31
32
#include "utils/checkutils.h"
33
#include "utils/dtor.h"
34
#include "utils/gettext.h"
35
#include "utils/itemxmlutils.h"
36
37
#include "debug.h"
38
39
namespace
40
{
41
1
    std::map<int, ClanInfo *> mClansInfos;
42
    bool mLoaded = false;
43
}  // namespace
44
45
void ClanDb::load()
46
{
47
    if (mLoaded)
48
        unload();
49
50
    logger->log1("Initializing clans database...");
51
    loadXmlFile(paths.getStringValue("clansFile"), SkipError_false);
52
    loadXmlFile(paths.getStringValue("clansPatchFile"), SkipError_true);
53
    loadXmlDir("clansPatchDir", loadXmlFile);
54
55
    mLoaded = true;
56
}
57
58
void ClanDb::loadXmlFile(const std::string &fileName,
59
                         const SkipError skipError)
60
{
61
    XML::Document doc(fileName, UseVirtFs_true, skipError);
62
    XmlNodeConstPtr rootNode = doc.rootNode();
63
64
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "clans"))
65
    {
66
        logger->log("Clans database: Error while loading %s!",
67
            paths.getStringValue("clansFile").c_str());
68
        mLoaded = true;
69
        return;
70
    }
71
72
    const ItemFieldInfos &addFields =
73
        ItemFieldDb::getAddFields();
74
75
    // iterate <clan>s
76
    for_each_xml_child_node(clanNode, rootNode)
77
    {
78
        if (xmlNameEqual(clanNode, "include"))
79
        {
80
            const std::string name = XML::getProperty(
81
                clanNode, "name", "");
82
            if (!name.empty())
83
                loadXmlFile(name, skipError);
84
            continue;
85
        }
86
        if (!xmlNameEqual(clanNode, "clan"))
87
            continue;
88
89
        const int id = XML::getProperty(clanNode, "id", 0);
90
        ClanInfo *clanInfo = nullptr;
91
        if (mClansInfos.find(id) != mClansInfos.end())
92
        {
93
            reportAlways("ClanDb: Redefinition of clan ID %d", id);
94
            clanInfo = mClansInfos[id];
95
        }
96
        if (clanInfo == nullptr)
97
            clanInfo = new ClanInfo;
98
99
        clanInfo->id = id;
100
        clanInfo->name = XML::langProperty(
101
            // TRANSLATORS: unknown clan name
102
            clanNode, "name", _("unnamed"));
103
104
        readItemStatsVector(clanInfo->stats,
105
            clanNode,
106
            addFields);
107
108
        mClansInfos[id] = clanInfo;
109
    }
110
}
111
112
void ClanDb::unload()
113
{
114
    logger->log1("Unloading clans database...");
115
    delete_all(mClansInfos);
116
    mClansInfos.clear();
117
118
    mLoaded = false;
119
}
120
121
const ClanInfo *ClanDb::get(const int clanId)
122
{
123
    std::map<int, ClanInfo *>::const_iterator i =
124
        mClansInfos.find(clanId);
125
126
    if (i == mClansInfos.end())
127
    {
128
        i = mClansInfos.find(clanId);
129
        if (i == mClansInfos.end())
130
        {
131
            reportAlways("ClanDb: Warning, unknown clan ID "
132
                "%d requested",
133
                clanId);
134
            return nullptr;
135
        }
136
    }
137
    return i->second;
138
2
}