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