GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/avatardb.cpp Lines: 8 62 12.9 %
Date: 2021-03-17 Branches: 0 98 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-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/avatardb.h"
25
26
#include "logger.h"
27
28
#include "resources/beingcommon.h"
29
#include "resources/beinginfo.h"
30
31
#include "resources/sprite/spritereference.h"
32
33
#include "utils/dtor.h"
34
#include "utils/gettext.h"
35
36
#include "configuration.h"
37
38
#include "debug.h"
39
40
namespace
41
{
42
1
    BeingInfos mAvatarInfos;
43
    bool mLoaded = false;
44
}  // namespace
45
46
void AvatarDB::load()
47
{
48
    if (mLoaded)
49
        unload();
50
51
    logger->log1("Initializing avatar database...");
52
    loadXmlFile(paths.getStringValue("avatarsFile"), SkipError_false);
53
    loadXmlFile(paths.getStringValue("avatarsPatchFile"), SkipError_true);
54
    loadXmlDir("avatarsPatchDir", loadXmlFile)
55
}
56
57
void AvatarDB::loadXmlFile(const std::string &fileName,
58
                           const SkipError skipError)
59
{
60
    XML::Document doc(fileName,
61
        UseVirtFs_true,
62
        skipError);
63
    XmlNodeConstPtrConst rootNode = doc.rootNode();
64
65
    if ((rootNode == nullptr) || !xmlNameEqual(rootNode, "avatars"))
66
    {
67
        logger->log("Avatars Database: Error while loading %s!",
68
            fileName.c_str());
69
        mLoaded = true;
70
        return;
71
    }
72
73
    for_each_xml_child_node(avatarNode, rootNode)
74
    {
75
        if (xmlNameEqual(avatarNode, "include"))
76
        {
77
            const std::string name = XML::getProperty(avatarNode, "name", "");
78
            if (!name.empty())
79
                loadXmlFile(name, skipError);
80
            continue;
81
        }
82
83
        if (!xmlNameEqual(avatarNode, "avatar"))
84
            continue;
85
86
        const BeingTypeId id = fromInt(XML::getProperty(
87
            avatarNode, "id", 0), BeingTypeId);
88
        BeingInfo *currentInfo = nullptr;
89
        if (mAvatarInfos.find(id) != mAvatarInfos.end())
90
            currentInfo = mAvatarInfos[id];
91
        if (currentInfo == nullptr)
92
            currentInfo = new BeingInfo;
93
94
        currentInfo->setName(XML::langProperty(
95
            // TRANSLATORS: unknown info name
96
            avatarNode, "name", _("unnamed")));
97
98
        currentInfo->setTargetOffsetX(XML::getProperty(avatarNode,
99
            "targetOffsetX", 0));
100
101
        currentInfo->setTargetOffsetY(XML::getProperty(avatarNode,
102
            "targetOffsetY", 0));
103
104
        currentInfo->setWidth(XML::getProperty(avatarNode,
105
            "width", 0));
106
        currentInfo->setHeight(XML::getProperty(avatarNode,
107
            "height", 0));
108
109
        SpriteDisplay display;
110
111
        // iterate <sprite>s and <sound>s
112
        for_each_xml_child_node(spriteNode, avatarNode)
113
        {
114
            if (xmlNameEqual(spriteNode, "sprite"))
115
            {
116
                if (!XmlHaveChildContent(spriteNode))
117
                    continue;
118
119
                SpriteReference *const currentSprite = new SpriteReference;
120
                currentSprite->sprite = XmlChildContent(spriteNode);
121
                currentSprite->variant = XML::getProperty(
122
                    spriteNode, "variant", 0);
123
                display.sprites.push_back(currentSprite);
124
            }
125
        }
126
        currentInfo->setDisplay(display);
127
        mAvatarInfos[id] = currentInfo;
128
    }
129
130
    mLoaded = true;
131
}
132
133
204
void AvatarDB::unload()
134
{
135
204
    logger->log1("Unloading avatar database...");
136
204
    delete_all(mAvatarInfos);
137
204
    mAvatarInfos.clear();
138
204
    mLoaded = false;
139
204
}
140
141
BeingInfo *AvatarDB::get(const BeingTypeId id)
142
{
143
    const BeingInfoIterator i = mAvatarInfos.find(id);
144
    if (i == mAvatarInfos.end())
145
        return BeingInfo::unknown;
146
    return i->second;
147
2
}