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