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