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