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