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