GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/statdb.cpp Lines: 14 98 14.3 %
Date: 2021-03-17 Branches: 2 210 1.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-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/statdb.h"
23
24
#include "configuration.h"
25
26
#include "enums/being/attributesstrings.h"
27
28
#include "utils/checkutils.h"
29
30
#include "resources/beingcommon.h"
31
32
#include "utils/gettext.h"
33
34
#include "debug.h"
35
36
namespace
37
{
38
    bool mLoaded = false;
39
1
    STD_VECTOR<BasicStat> mBasicStats;
40
1
    std::map<std::string, STD_VECTOR<BasicStat> > mStats;
41
1
    STD_VECTOR<std::string> mPages;
42
}  // namespace
43
44
void StatDb::addDefaultStats()
45
{
46
    mBasicStats.push_back(BasicStat(Attributes::PLAYER_STR,
47
        "str",
48
        // TRANSLATORS: player stat
49
        _("Strength")));
50
    mBasicStats.push_back(BasicStat(Attributes::PLAYER_AGI,
51
        "agi",
52
        // TRANSLATORS: player stat
53
        _("Agility")));
54
    mBasicStats.push_back(BasicStat(Attributes::PLAYER_VIT,
55
        "vit",
56
        // TRANSLATORS: player stat
57
        _("Vitality")));
58
    mBasicStats.push_back(BasicStat(Attributes::PLAYER_INT,
59
        "int",
60
        // TRANSLATORS: player stat
61
        _("Intelligence")));
62
    mBasicStats.push_back(BasicStat(Attributes::PLAYER_DEX,
63
        "dex",
64
        // TRANSLATORS: player stat
65
        _("Dexterity")));
66
    mBasicStats.push_back(BasicStat(Attributes::PLAYER_LUK,
67
        "luk",
68
        // TRANSLATORS: player stat
69
        _("Luck")));
70
}
71
72
1
const STD_VECTOR<BasicStat> &StatDb::getBasicStats()
73
{
74
1
    return mBasicStats;
75
}
76
77
const STD_VECTOR<BasicStat> &StatDb::getStats(const std::string &page)
78
{
79
    return mStats[page];
80
}
81
82
1
const STD_VECTOR<std::string> &StatDb::getPages()
83
{
84
1
    return mPages;
85
}
86
87
void StatDb::load()
88
{
89
    if (mLoaded)
90
        unload();
91
92
    logger->log1("Initializing stat database...");
93
94
    loadXmlFile(paths.getStringValue("statFile"), SkipError_false);
95
    loadXmlFile(paths.getStringValue("statPatchFile"), SkipError_true);
96
    loadXmlDir("statPatchDir", loadXmlFile)
97
    mLoaded = true;
98
}
99
100
static void loadBasicStats(XmlNodeConstPtr rootNode)
101
{
102
    const int maxAttr = static_cast<int>(Attributes::MAX_ATTRIBUTE);
103
    for_each_xml_child_node(node, rootNode)
104
    {
105
        if (xmlNameEqual(node, "stat"))
106
        {
107
            const std::string name = XML::getProperty(node, "name", "");
108
            const std::string attr = XML::getProperty(node, "attr", "");
109
            if (attr.empty() || AttributesEnum::find(attr) == false)
110
            {
111
                const int id = XML::getProperty(node, "id", 0);
112
                if (id <= 0 || id >= maxAttr)
113
                {
114
                    reportAlways("Wrong attr or id for basic "
115
                        "stat with name %s",
116
                        name.c_str())
117
                    continue;
118
                }
119
                const std::string tag = XML::getProperty(node, "tag", "");
120
                mBasicStats.push_back(BasicStat(static_cast<AttributesT>(id),
121
                    tag,
122
                    name));
123
            }
124
            else
125
            {
126
                const std::string tag = XML::getProperty(node, "tag", "");
127
                mBasicStats.push_back(BasicStat(AttributesEnum::get(attr),
128
                    tag,
129
                    name));
130
            }
131
        }
132
    }
133
}
134
135
static void loadStats(XmlNodeConstPtr rootNode,
136
                      const std::string &page)
137
{
138
    const int maxAttr = static_cast<int>(Attributes::MAX_ATTRIBUTE);
139
    STD_VECTOR<BasicStat> &stats = mStats[page];
140
    mPages.push_back(page);
141
    for_each_xml_child_node(node, rootNode)
142
    {
143
        if (xmlNameEqual(node, "stat"))
144
        {
145
            const std::string name = XML::getProperty(node, "name", "");
146
            const std::string attr = XML::getProperty(node, "attr", "");
147
            if (attr.empty() || AttributesEnum::find(attr) == false)
148
            {
149
                const int id = XML::getProperty(node, "id", 0);
150
                if (id <= 0 || id >= maxAttr)
151
                {
152
                    reportAlways("Wrong attr or id for extended "
153
                        "stat with name %s",
154
                        name.c_str())
155
                    continue;
156
                }
157
                stats.push_back(BasicStat(static_cast<AttributesT>(id),
158
                    std::string(),
159
                    name));
160
            }
161
            else
162
            {
163
                stats.push_back(BasicStat(AttributesEnum::get(attr),
164
                    std::string(),
165
                    name));
166
            }
167
        }
168
    }
169
}
170
171
void StatDb::loadXmlFile(const std::string &fileName,
172
                         const SkipError skipError)
173
{
174
    XML::Document doc(fileName,
175
        UseVirtFs_true,
176
        skipError);
177
    XmlNodeConstPtrConst rootNode = doc.rootNode();
178
179
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "stats"))
180
    {
181
        logger->log("StatDb: Error while loading %s!",
182
            fileName.c_str());
183
        if (skipError == SkipError_false)
184
            addDefaultStats();
185
        return;
186
    }
187
188
    for_each_xml_child_node(node, rootNode)
189
    {
190
        if (xmlNameEqual(node, "include"))
191
        {
192
            const std::string name = XML::getProperty(node, "name", "");
193
            if (!name.empty())
194
                loadXmlFile(name, skipError);
195
            continue;
196
        }
197
        else if (xmlNameEqual(node, "basic"))
198
        {
199
            loadBasicStats(node);
200
        }
201
        else if (xmlNameEqual(node, "extended"))
202
        {
203
            // TRANSLATORS: stats page name
204
            loadStats(node, _("Extended"));
205
        }
206
        else if (xmlNameEqual(node, "page"))
207
        {
208
            std::string page = XML::langProperty(node, "name", "");
209
            if (page.empty())
210
            {
211
                reportAlways("Page without name in stats.xml")
212
                page = "Unknown";
213
            }
214
            loadStats(node, page);
215
        }
216
    }
217
    if (skipError == SkipError_false)
218
    {
219
        if (mBasicStats.empty() &&
220
            mStats.empty())
221
        {
222
            reportAlways("StatDb: no stats found")
223
            addDefaultStats();
224
        }
225
    }
226
}
227
228
215
void StatDb::unload()
229
{
230
215
    logger->log1("Unloading stat database...");
231
232
215
    mBasicStats.clear();
233
215
    mStats.clear();
234
215
    mPages.clear();
235
215
    mLoaded = false;
236

218
}