GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/badgesdb.cpp Lines: 16 50 32.0 %
Date: 2021-03-17 Branches: 3 50 6.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2014-2019  The ManaPlus Developers
4
 *  Copyright (C) 2019-2021  Andrei Karas
5
 *
6
 *  This file is part of The ManaPlus Client.
7
 *
8
 *  This program is free software; you can redistribute it and/or modify
9
 *  it under the terms of the GNU General Public License as published by
10
 *  the Free Software Foundation; either version 2 of the License, or
11
 *  any later version.
12
 *
13
 *  This program is distributed in the hope that it will be useful,
14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 *  GNU General Public License for more details.
17
 *
18
 *  You should have received a copy of the GNU General Public License
19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "resources/db/badgesdb.h"
23
24
#include "configuration.h"
25
#include "logger.h"
26
27
#include "fs/virtfs/tools.h"
28
29
#include "utils/foreach.h"
30
#include "utils/xmlutils.h"
31
32
#include "debug.h"
33
34
namespace
35
{
36
1
    BadgesInfos mGuilds;
37
1
    BadgesInfos mNames;
38
1
    BadgesInfos mParties;
39
1
    BadgesInfos mClans;
40
    bool mLoaded = false;
41
}  // namespace
42
43
static void loadXmlFile(const std::string &file,
44
                        const std::string &name,
45
                        BadgesInfos &arr,
46
                        const SkipError skipError)
47
{
48
    readXmlStringMap(file,
49
        "badges",
50
        name,
51
        "badge",
52
        "name",
53
        "image",
54
        arr,
55
        skipError);
56
}
57
58
static void loadDB(const std::string &name, BadgesInfos &arr)
59
{
60
    loadXmlFile(paths.getStringValue("badgesFile"),
61
        name, arr, SkipError_false);
62
    loadXmlFile(paths.getStringValue("badgesPatchFile"),
63
        name, arr, SkipError_true);
64
65
    StringVect listVect;
66
    VirtFs::getFilesInDir(paths.getStringValue(
67
        "badgesPatchDir"),
68
        listVect,
69
        ".xml");
70
    FOR_EACH (StringVectCIter, itVect, listVect)
71
        loadXmlFile(*itVect, name, arr, SkipError_true);
72
}
73
74
void BadgesDB::load()
75
{
76
    if (mLoaded)
77
        unload();
78
79
    logger->log1("Initializing Badges database...");
80
    loadDB("guild", mGuilds);
81
    loadDB("name", mNames);
82
    loadDB("party", mParties);
83
    loadDB("clan", mClans);
84
}
85
86
204
void BadgesDB::unload()
87
{
88
204
    logger->log1("Unloading Badges database...");
89
204
    mParties.clear();
90
204
    mClans.clear();
91
204
    mGuilds.clear();
92
204
    mNames.clear();
93
204
    mLoaded = false;
94
204
}
95
96
const std::string BadgesDB::getPartyBadge(const std::string &name)
97
{
98
    const BadgesInfosIter it = mParties.find(name);
99
    if (it == mParties.end())
100
        return std::string();
101
    return (*it).second;
102
}
103
104
6
const std::string BadgesDB::getNameBadge(const std::string &name)
105
{
106
12
    const BadgesInfosIter it = mNames.find(name);
107
6
    if (it == mNames.end())
108
        return std::string();
109
    return (*it).second;
110
}
111
112
const std::string BadgesDB::getGuildBadge(const std::string &name)
113
{
114
    const BadgesInfosIter it = mGuilds.find(name);
115
    if (it == mGuilds.end())
116
        return std::string();
117
    return (*it).second;
118
}
119
120
const std::string BadgesDB::getClanBadge(const std::string &name)
121
{
122
    const BadgesInfosIter it = mClans.find(name);
123
    if (it == mClans.end())
124
        return std::string();
125
    return (*it).second;
126

3
}