GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/itemfielddb.cpp Lines: 15 56 26.8 %
Date: 2021-03-17 Branches: 2 122 1.6 %

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/itemfielddb.h"
23
24
#include "configuration.h"
25
26
#include "utils/checkutils.h"
27
#include "utils/dtor.h"
28
29
#include "resources/beingcommon.h"
30
31
#include "resources/item/itemfieldtype.h"
32
33
#include "debug.h"
34
35
namespace
36
{
37
1
    ItemFieldInfos mRequiredInfos;
38
1
    ItemFieldInfos mAddInfos;
39
    bool mLoaded = false;
40
}  // namespace
41
42
void ItemFieldDb::load()
43
{
44
    if (mLoaded)
45
        unload();
46
47
    logger->log1("Initializing item field database...");
48
49
    loadXmlFile(paths.getStringValue("itemFieldsFile"), SkipError_false);
50
    loadXmlFile(paths.getStringValue("itemFieldsPatchFile"), SkipError_true);
51
    loadXmlDir("itemFieldsPatchDir", loadXmlFile)
52
    mLoaded = true;
53
}
54
55
static void loadFields(XmlNodeConstPtr groupNode,
56
                       ItemFieldInfos &fields1,
57
                       ItemFieldInfos &fields2)
58
{
59
    for_each_xml_child_node(node, groupNode)
60
    {
61
        if (!xmlNameEqual(node, "field"))
62
            continue;
63
64
        const std::string name = XML::getProperty(node,
65
            "name",
66
            "");
67
        if (name.empty())
68
        {
69
            reportAlways("Empty name field in ItemFieldDb")
70
            continue;
71
        }
72
        const std::string description = XML::langProperty(node,
73
            "description",
74
            "");
75
        if (description.empty())
76
        {
77
            reportAlways("Empty description field in ItemFieldDb")
78
            continue;
79
        }
80
        const bool sign = XML::getBoolProperty(node,
81
            "signed",
82
            true);
83
        if (fields2.find(name) != fields2.end())
84
        {
85
            reportAlways(
86
                "Same field name detected in requeted and add groups: %s",
87
                name.c_str())
88
            continue;
89
        }
90
        if (fields1.find(name) != fields1.end())
91
        {
92
            reportAlways(
93
                "Same field name detected: %s",
94
                name.c_str())
95
            continue;
96
        }
97
        fields1[name] = new ItemFieldType(name,
98
            description,
99
            sign);
100
    }
101
}
102
103
void ItemFieldDb::loadXmlFile(const std::string &fileName,
104
                              const SkipError skipError)
105
{
106
    XML::Document doc(fileName,
107
        UseVirtFs_true,
108
        skipError);
109
    XmlNodeConstPtrConst rootNode = doc.rootNode();
110
111
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "itemfields"))
112
    {
113
        logger->log("ItemFieldDb: Error while loading %s!",
114
            fileName.c_str());
115
        return;
116
    }
117
118
    for_each_xml_child_node(node, rootNode)
119
    {
120
        if (xmlNameEqual(node, "include"))
121
        {
122
            const std::string name = XML::getProperty(node, "name", "");
123
            if (!name.empty())
124
                loadXmlFile(name, skipError);
125
            continue;
126
        }
127
128
        if (xmlNameEqual(node, "required"))
129
            loadFields(node, mRequiredInfos, mAddInfos);
130
        else if (xmlNameEqual(node, "add"))
131
            loadFields(node, mAddInfos, mRequiredInfos);
132
    }
133
}
134
135
215
void ItemFieldDb::unload()
136
{
137
215
    logger->log1("Unloading item database...");
138
139
215
    delete_all(mRequiredInfos);
140
215
    mRequiredInfos.clear();
141
215
    delete_all(mAddInfos);
142
215
    mAddInfos.clear();
143
215
    mLoaded = false;
144
215
}
145
146
3
const ItemFieldInfos &ItemFieldDb::getRequiredFields()
147
{
148
3
    return mRequiredInfos;
149
}
150
151
3
const ItemFieldInfos &ItemFieldDb::getAddFields()
152
{
153
3
    return mAddInfos;
154

3
}