ManaPlus
sounddb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2013-2019 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 
28 
29 #include "resources/beingcommon.h"
30 
31 #include "debug.h"
32 
33 namespace
34 {
35  std::string mDefault;
36  STD_VECTOR<std::string> mSounds;
37 } // namespace
38 
40 {
41  unload();
42  logger->log1("Initializing sound database...");
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 {
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 
87 {
88  logger->log1("Unloading sound database...");
90  for (int f = 0; f < NotifyTypes::TYPE_END; f ++)
91  mSounds[f].clear();
92 }
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 }
SkipError
bool SkipError
Definition: skiperror.h:29
SkipError_true
const bool SkipError_true
Definition: skiperror.h:29
notifymanager.h
SoundDB::getSound
std::string & getSound(const int id)
Definition: sounddb.cpp:94
logger.h
Net::unload
void unload()
Definition: net.cpp:179
paths
Configuration paths
Definition: configuration.cpp:55
notifytypes.h
SkipError_false
const bool SkipError_false
Definition: skiperror.h:29
XML::Document
Definition: libxml.h:52
sounddb.h
NotifyManager::getIndexBySound
int getIndexBySound(const std::string &sound)
Definition: notifymanager.cpp:204
XML::getProperty
int getProperty(const xmlNodePtr node, const char *const name, int def)
Definition: libxml.cpp:173
logger
Logger * logger
Definition: logger.cpp:88
fileName
std::string fileName
Definition: testmain.cpp:38
loadXmlDir
#define loadXmlDir(name, function)
Definition: beingcommon.h:38
anonymous_namespace{sounddb.cpp}::mSounds
std::vector< std::string > mSounds
Definition: sounddb.cpp:36
beingcommon.h
anonymous_namespace{sounddb.cpp}::mDefault
std::string mDefault
Definition: sounddb.cpp:35
loadXmlFile
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:42
UseVirtFs_true
const bool UseVirtFs_true
Definition: usevirtfs.h:29
SoundDB::loadXmlFile
void loadXmlFile(const std::string &fileName, const SkipError skipError)
Definition: sounddb.cpp:48
Configuration::getStringValue
std::string getStringValue(const std::string &key) const
Definition: configuration.cpp:487
Logger::log1
void log1(const char *const log_text)
Definition: logger.cpp:237
SoundDB::unload
void unload()
Definition: sounddb.cpp:86
configuration.h
XML::Document::rootNode
xmlNodePtr rootNode()
Definition: libxml.cpp:168
SoundDB::load
void load()
Definition: sounddb.cpp:39
PlayerInfo::clear
void clear()
Definition: playerinfo.cpp:451
NotifyTypes::TYPE_END
@ TYPE_END
Definition: notifytypes.h:253
for_each_xml_child_node
#define for_each_xml_child_node(var, parent)
Definition: libxml.h:160
debug.h