GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/skillunitdb.cpp Lines: 2 62 3.2 %
Date: 2017-11-29 Branches: 0 126 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2017  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "resources/db/skillunitdb.h"
24
25
#include "configuration.h"
26
27
#include "resources/beingcommon.h"
28
#include "resources/beinginfo.h"
29
30
#include "resources/sprite/spritereference.h"
31
32
#include "utils/checkutils.h"
33
#include "utils/dtor.h"
34
#include "utils/gettext.h"
35
36
#include "debug.h"
37
38
namespace
39
{
40
2
    BeingInfos mSkillUnitInfos;
41
    bool mLoaded = false;
42
}  // namespace
43
44
void SkillUnitDb::load()
45
{
46
    if (mLoaded)
47
        unload();
48
49
    logger->log1("Initializing skill unit database...");
50
    loadXmlFile(paths.getStringValue("skillUnitsFile"), SkipError_false);
51
    loadXmlFile(paths.getStringValue("skillUnitsPatchFile"), SkipError_true);
52
    loadXmlDir("skillUnitsPatchDir", loadXmlFile);
53
    mLoaded = true;
54
}
55
56
void SkillUnitDb::loadXmlFile(const std::string &fileName,
57
                              const SkipError skipError)
58
{
59
    XML::Document doc(fileName, UseVirtFs_true, skipError);
60
    XmlNodeConstPtrConst rootNode = doc.rootNode();
61
62
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "skillunits"))
63
    {
64
        logger->log("SkillUnitDb: Error while loading %s!",
65
            fileName.c_str());
66
        return;
67
    }
68
69
    // iterate <skillunit>s
70
    for_each_xml_child_node(skillUnitNode, rootNode)
71
    {
72
        if (xmlNameEqual(skillUnitNode, "include"))
73
        {
74
            const std::string name = XML::getProperty(skillUnitNode,
75
                "name", "");
76
            if (!name.empty())
77
                loadXmlFile(name, skipError);
78
            continue;
79
        }
80
        else if (!xmlNameEqual(skillUnitNode, "skillunit"))
81
        {
82
            continue;
83
        }
84
85
        const BeingTypeId id = fromInt(XML::getProperty(
86
            skillUnitNode, "id", -1), BeingTypeId);
87
        if (id == BeingTypeId_negOne)
88
        {
89
            reportAlways("SkillUnitDb: skill unit with missing ID in %s!",
90
                paths.getStringValue("skillUnitsFile").c_str());
91
            continue;
92
        }
93
94
        BeingInfo *currentInfo = nullptr;
95
        if (mSkillUnitInfos.find(id) != mSkillUnitInfos.end())
96
            currentInfo = mSkillUnitInfos[id];
97
        if (currentInfo == nullptr)
98
            currentInfo = new BeingInfo;
99
100
        currentInfo->setName(XML::langProperty(skillUnitNode,
101
            // TRANSLATORS: unknown info name
102
            "name", _("skill")));
103
104
        currentInfo->setTargetSelection(XML::getBoolProperty(skillUnitNode,
105
            "targetSelection", true));
106
107
        BeingCommon::readBasicAttributes(currentInfo, skillUnitNode, "attack");
108
        BeingCommon::readWalkingAttributes(currentInfo, skillUnitNode, 0);
109
110
        currentInfo->setDeadSortOffsetY(XML::getProperty(skillUnitNode,
111
            "deadSortOffsetY", 31));
112
113
        SpriteDisplay display;
114
        for_each_xml_child_node(spriteNode, skillUnitNode)
115
        {
116
            if (!XmlHaveChildContent(spriteNode))
117
                continue;
118
119
            if (xmlNameEqual(spriteNode, "sprite"))
120
            {
121
                SpriteReference *const currentSprite = new SpriteReference;
122
                currentSprite->sprite = XmlChildContent(spriteNode);
123
                currentSprite->variant =
124
                    XML::getProperty(spriteNode, "variant", 0);
125
                display.sprites.push_back(currentSprite);
126
            }
127
            else if (xmlNameEqual(spriteNode, "particlefx"))
128
            {
129
                std::string particlefx = XmlChildContent(spriteNode);
130
                display.particles.push_back(particlefx);
131
            }
132
        }
133
134
        currentInfo->setDisplay(display);
135
136
        mSkillUnitInfos[id] = currentInfo;
137
    }
138
}
139
140
void SkillUnitDb::unload()
141
{
142
    logger->log1("Unloading skill unit database...");
143
    delete_all(mSkillUnitInfos);
144
    mSkillUnitInfos.clear();
145
146
    mLoaded = false;
147
}
148
149
BeingInfo *SkillUnitDb::get(const BeingTypeId id)
150
{
151
    const BeingInfoIterator i = mSkillUnitInfos.find(id);
152
153
    if (i == mSkillUnitInfos.end())
154
    {
155
        reportAlways("SkillUnitDb: Warning, unknown skill unit id "
156
            "%d requested",
157
            toInt(id, int));
158
        return BeingInfo::unknown;
159
    }
160
    return i->second;
161
4
}