ManaPlus
badgesdb.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2014-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/badgesdb.h"
23 
24 #include "configuration.h"
25 #include "logger.h"
26 
27 #include "fs/virtfs/tools.h"
28 
29 #include "utils/foreach.h"
30 #include "utils/xmlutils.h"
31 
32 #include "debug.h"
33 
34 namespace
35 {
40  bool mLoaded = false;
41 } // namespace
42 
43 static void loadXmlFile(const std::string &file,
44  const std::string &name,
45  BadgesInfos &arr,
46  const SkipError skipError)
47 {
48  readXmlStringMap(file,
49  "badges",
50  name,
51  "badge",
52  "name",
53  "image",
54  arr,
55  skipError);
56 }
57 
58 static void loadDB(const std::string &name, BadgesInfos &arr)
59 {
60  loadXmlFile(paths.getStringValue("badgesFile"),
61  name, arr, SkipError_false);
62  loadXmlFile(paths.getStringValue("badgesPatchFile"),
63  name, arr, SkipError_true);
64 
65  StringVect listVect;
67  "badgesPatchDir"),
68  listVect,
69  ".xml");
70  FOR_EACH (StringVectCIter, itVect, listVect)
71  loadXmlFile(*itVect, name, arr, SkipError_true);
72 }
73 
75 {
76  if (mLoaded)
77  unload();
78 
79  logger->log1("Initializing Badges database...");
80  loadDB("guild", mGuilds);
81  loadDB("name", mNames);
82  loadDB("party", mParties);
83  loadDB("clan", mClans);
84 }
85 
87 {
88  logger->log1("Unloading Badges database...");
89  mParties.clear();
90  mClans.clear();
91  mGuilds.clear();
92  mNames.clear();
93  mLoaded = false;
94 }
95 
96 const std::string BadgesDB::getPartyBadge(const std::string &name)
97 {
98  const BadgesInfosIter it = mParties.find(name);
99  if (it == mParties.end())
100  return std::string();
101  return (*it).second;
102 }
103 
104 const std::string BadgesDB::getNameBadge(const std::string &name)
105 {
106  const BadgesInfosIter it = mNames.find(name);
107  if (it == mNames.end())
108  return std::string();
109  return (*it).second;
110 }
111 
112 const std::string BadgesDB::getGuildBadge(const std::string &name)
113 {
114  const BadgesInfosIter it = mGuilds.find(name);
115  if (it == mGuilds.end())
116  return std::string();
117  return (*it).second;
118 }
119 
120 const std::string BadgesDB::getClanBadge(const std::string &name)
121 {
122  const BadgesInfosIter it = mClans.find(name);
123  if (it == mClans.end())
124  return std::string();
125  return (*it).second;
126 }
static void loadXmlFile(const std::string &file, const std::string &name, BadgesInfos &arr, const SkipError skipError)
Definition: badgesdb.cpp:43
static void loadDB(const std::string &name, BadgesInfos &arr)
Definition: badgesdb.cpp:58
std::map< std::string, std::string > BadgesInfos
Definition: badgesdb.h:30
BadgesInfos::const_iterator BadgesInfosIter
Definition: badgesdb.h:31
std::string getStringValue(const std::string &key) const
void log1(const char *const log_text)
Definition: logger.cpp:238
Configuration paths
#define FOR_EACH(type, iter, array)
Definition: foreach.h:25
Logger * logger
Definition: logger.cpp:89
void unload()
Definition: badgesdb.cpp:86
const std::string getClanBadge(const std::string &name)
Definition: badgesdb.cpp:120
void load()
Definition: badgesdb.cpp:74
const std::string getPartyBadge(const std::string &name)
Definition: badgesdb.cpp:96
const std::string getGuildBadge(const std::string &name)
Definition: badgesdb.cpp:112
const std::string getNameBadge(const std::string &name)
Definition: badgesdb.cpp:104
void unload()
Definition: net.cpp:180
void getFilesInDir(const std::string &dir, StringVect &list, const std::string &ext)
Definition: tools.cpp:81
const bool SkipError_false
Definition: skiperror.h:30
const bool SkipError_true
Definition: skiperror.h:30
bool SkipError
Definition: skiperror.h:30
StringVect::const_iterator StringVectCIter
Definition: stringvector.h:31
std::vector< std::string > StringVect
Definition: stringvector.h:29
void readXmlStringMap(const std::string &fileName, const std::string &rootName, const std::string &sectionName, const std::string &itemName, const std::string &attributeKeyName, const std::string &attributeValueName, std::map< std::string, std::string > &arr, const SkipError skipError)
Definition: xmlutils.cpp:80