GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/itemfielddb.cpp Lines: 15 55 27.3 %
Date: 2017-11-29 Branches: 2 124 1.6 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2016-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "resources/db/itemfielddb.h"
22
23
#include "configuration.h"
24
25
#include "utils/checkutils.h"
26
#include "utils/dtor.h"
27
28
#include "resources/beingcommon.h"
29
30
#include "resources/item/itemfieldtype.h"
31
32
#include "debug.h"
33
34
namespace
35
{
36
2
    ItemFieldDb::FieldInfos mRequiredInfos;
37
2
    ItemFieldDb::FieldInfos mAddInfos;
38
    bool mLoaded = false;
39
}  // namespace
40
41
void ItemFieldDb::load()
42
{
43
    if (mLoaded)
44
        unload();
45
46
    logger->log1("Initializing item field database...");
47
48
    loadXmlFile(paths.getStringValue("itemFieldsFile"), SkipError_false);
49
    loadXmlFile(paths.getStringValue("itemFieldsPatchFile"), SkipError_true);
50
    loadXmlDir("itemFieldsPatchDir", loadXmlFile);
51
    mLoaded = true;
52
}
53
54
static void loadFields(XmlNodeConstPtr groupNode,
55
                       ItemFieldDb::FieldInfos &fields1,
56
                       ItemFieldDb::FieldInfos &fields2)
57
{
58
    for_each_xml_child_node(node, groupNode)
59
    {
60
        if (!xmlNameEqual(node, "field"))
61
            continue;
62
63
        const std::string name = XML::getProperty(node,
64
            "name",
65
            "");
66
        if (name.empty())
67
        {
68
            reportAlways("Empty name field in ItemFieldDb");
69
            continue;
70
        }
71
        const std::string description = XML::langProperty(node,
72
            "description",
73
            "");
74
        if (description.empty())
75
        {
76
            reportAlways("Empty description field in ItemFieldDb");
77
            continue;
78
        }
79
        const bool sign = XML::getBoolProperty(node,
80
            "signed",
81
            true);
82
        if (fields2.find(name) != fields2.end())
83
        {
84
            reportAlways(
85
                "Same field name detected in requeted and add groups: %s",
86
                name.c_str());
87
            continue;
88
        }
89
        if (fields1.find(name) != fields1.end())
90
        {
91
            reportAlways(
92
                "Same field name detected: %s",
93
                name.c_str());
94
            continue;
95
        }
96
        fields1[name] = new ItemFieldType(name,
97
            description,
98
            sign);
99
    }
100
}
101
102
void ItemFieldDb::loadXmlFile(const std::string &fileName,
103
                              const SkipError skipError)
104
{
105
    XML::Document doc(fileName,
106
        UseVirtFs_true,
107
        skipError);
108
    XmlNodeConstPtrConst rootNode = doc.rootNode();
109
110
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "itemfields"))
111
    {
112
        logger->log("ItemFieldDb: Error while loading %s!",
113
            fileName.c_str());
114
        return;
115
    }
116
117
    for_each_xml_child_node(node, rootNode)
118
    {
119
        if (xmlNameEqual(node, "include"))
120
        {
121
            const std::string name = XML::getProperty(node, "name", "");
122
            if (!name.empty())
123
                loadXmlFile(name, skipError);
124
            continue;
125
        }
126
127
        if (xmlNameEqual(node, "required"))
128
            loadFields(node, mRequiredInfos, mAddInfos);
129
        else if (xmlNameEqual(node, "add"))
130
            loadFields(node, mAddInfos, mRequiredInfos);
131
    }
132
}
133
134
384
void ItemFieldDb::unload()
135
{
136
384
    logger->log1("Unloading item database...");
137
138
384
    delete_all(mRequiredInfos);
139
384
    mRequiredInfos.clear();
140
384
    delete_all(mAddInfos);
141
384
    mAddInfos.clear();
142
384
    mLoaded = false;
143
384
}
144
145
6
const ItemFieldDb::FieldInfos &ItemFieldDb::getRequiredFields()
146
{
147
6
    return mRequiredInfos;
148
}
149
150
6
const ItemFieldDb::FieldInfos &ItemFieldDb::getAddFields()
151
{
152
6
    return mAddInfos;
153

6
}