GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/db/sounddb.cpp Lines: 9 36 25.0 %
Date: 2017-11-29 Branches: 4 64 6.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include "resources/db/sounddb.h"
22
23
#include "configuration.h"
24
#include "logger.h"
25
#include "notifymanager.h"
26
27
#include "enums/resources/notifytypes.h"
28
29
#include "resources/beingcommon.h"
30
31
#include "debug.h"
32
33
namespace
34
{
35
2
    std::string mDefault;
36
2
    STD_VECTOR<std::string> mSounds;
37
}  // namespace
38
39
void SoundDB::load()
40
{
41
    unload();
42
    logger->log1("Initializing sound database...");
43
    loadXmlFile(paths.getStringValue("soundsFile"), SkipError_false);
44
    loadXmlFile(paths.getStringValue("soundsPatchFile"), SkipError_true);
45
    loadXmlDir("soundsPatchDir", loadXmlFile);
46
}
47
48
void SoundDB::loadXmlFile(const std::string &fileName,
49
                          const SkipError skipError)
50
{
51
    XML::Document *doc = new XML::Document(fileName,
52
        UseVirtFs_true,
53
        skipError);
54
    XmlNodeConstPtrConst root = doc->rootNode();
55
56
    if ((root == nullptr) || !xmlNameEqual(root, "sounds"))
57
    {
58
        delete doc;
59
        return;
60
    }
61
62
    for_each_xml_child_node(node, root)
63
    {
64
        if (xmlNameEqual(node, "include"))
65
        {
66
            const std::string name = XML::getProperty(node, "name", "");
67
            if (!name.empty())
68
                loadXmlFile(name, skipError);
69
            continue;
70
        }
71
        else if (xmlNameEqual(node, "sound"))
72
        {
73
            const std::string name = XML::getProperty(node, "name", "");
74
            const int id = NotifyManager::getIndexBySound(name);
75
            if (id != 0)
76
            {
77
                const std::string value = XML::getProperty(node, "value", "");
78
                mSounds[id] = value;
79
            }
80
        }
81
    }
82
83
    delete doc;
84
}
85
86
384
void SoundDB::unload()
87
{
88
384
    logger->log1("Unloading sound database...");
89
384
    mSounds.resize(NotifyTypes::TYPE_END);
90
85248
    for (int f = 0; f < NotifyTypes::TYPE_END; f ++)
91
254592
        mSounds[f].clear();
92
384
}
93
94
std::string &SoundDB::getSound(const int id)
95
{
96
    if (id < 0 || id >= NotifyTypes::TYPE_END)
97
        return mDefault;
98
    return mSounds[id];
99

6
}