GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/petdb.cpp Lines: 8 65 12.3 %
Date: 2017-11-29 Branches: 0 130 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/petdb.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 mPETInfos;
41
    bool mLoaded = false;
42
}  // namespace
43
44
void PETDB::load()
45
{
46
    if (mLoaded)
47
        unload();
48
49
    logger->log1("Initializing PET database...");
50
    loadXmlFile(paths.getStringValue("petsFile"), SkipError_false);
51
    loadXmlFile(paths.getStringValue("petsPatchFile"), SkipError_true);
52
    loadXmlDir("petsPatchDir", loadXmlFile);
53
    mLoaded = true;
54
}
55
56
void PETDB::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, "pets"))
65
    {
66
        logger->log("PET Database: Error while loading %s!",
67
            fileName.c_str());
68
        return;
69
    }
70
71
    // iterate <pet>s
72
    for_each_xml_child_node(petNode, rootNode)
73
    {
74
        if (xmlNameEqual(petNode, "include"))
75
        {
76
            const std::string name = XML::getProperty(petNode, "name", "");
77
            if (!name.empty())
78
                loadXmlFile(name, skipError);
79
            continue;
80
        }
81
        else if (!xmlNameEqual(petNode, "pet"))
82
        {
83
            continue;
84
        }
85
86
        const BeingTypeId id = fromInt(XML::getProperty(
87
            petNode, "id", -1), BeingTypeId);
88
        if (id == BeingTypeId_negOne)
89
        {
90
            reportAlways("PET Database: PET with missing ID in %s!",
91
                paths.getStringValue("petsFile").c_str());
92
            continue;
93
        }
94
95
        BeingInfo *currentInfo = nullptr;
96
        if (mPETInfos.find(id) != mPETInfos.end())
97
            currentInfo = mPETInfos[id];
98
        if (currentInfo == nullptr)
99
            currentInfo = new BeingInfo;
100
101
        currentInfo->setName(XML::langProperty(petNode,
102
            // TRANSLATORS: unknown info name
103
            "name", _("pet")));
104
105
        currentInfo->setTargetSelection(XML::getBoolProperty(petNode,
106
            "targetSelection", true));
107
108
        BeingCommon::readBasicAttributes(currentInfo, petNode, "talk");
109
        BeingCommon::readWalkingAttributes(currentInfo, petNode, 0);
110
111
        currentInfo->setDeadSortOffsetY(XML::getProperty(petNode,
112
            "deadSortOffsetY", 31));
113
114
        const std::string returnMessage = XML::langProperty(petNode,
115
            // TRANSLATORS: popup menu item
116
            // TRANSLATORS: pet return to egg
117
            "removeMessage", _("Return to egg"));
118
        currentInfo->setString(0, returnMessage);
119
120
        SpriteDisplay display;
121
        for_each_xml_child_node(spriteNode, petNode)
122
        {
123
            if (!XmlHaveChildContent(spriteNode))
124
                continue;
125
126
            if (xmlNameEqual(spriteNode, "sprite"))
127
            {
128
                SpriteReference *const currentSprite = new SpriteReference;
129
                currentSprite->sprite = XmlChildContent(spriteNode);
130
                currentSprite->variant =
131
                    XML::getProperty(spriteNode, "variant", 0);
132
                display.sprites.push_back(currentSprite);
133
            }
134
            else if (xmlNameEqual(spriteNode, "particlefx"))
135
            {
136
                std::string particlefx = XmlChildContent(spriteNode);
137
                display.particles.push_back(particlefx);
138
            }
139
        }
140
141
        currentInfo->setDisplay(display);
142
143
        mPETInfos[id] = currentInfo;
144
    }
145
}
146
147
384
void PETDB::unload()
148
{
149
384
    logger->log1("Unloading PET database...");
150
384
    delete_all(mPETInfos);
151
384
    mPETInfos.clear();
152
153
384
    mLoaded = false;
154
384
}
155
156
BeingInfo *PETDB::get(const BeingTypeId id)
157
{
158
    const BeingInfoIterator i = mPETInfos.find(id);
159
160
    if (i == mPETInfos.end())
161
    {
162
        reportAlways("PETDB: Warning, unknown PET ID %d requested",
163
            toInt(id, int));
164
        return BeingInfo::unknown;
165
    }
166
    return i->second;
167
4
}