Mana
Loading...
Searching...
No Matches
settingsmanager.cpp
Go to the documentation of this file.
1/*
2 * The Mana Server
3 * Copyright (C) 2013 The Mana World Development Team
4 *
5 * This file is part of The Mana Server.
6 *
7 * The Mana Server 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 * The Mana Server 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 The Mana Server. If not, see <http://www.gnu.org/licenses/>.
19 */
20
22
24#include "resources/emotedb.h"
25#include "resources/hairdb.h"
26#include "resources/itemdb.h"
27#include "resources/monsterdb.h"
28#include "resources/npcdb.h"
29#include "resources/abilitydb.h"
30#include "resources/questdb.h"
32
33#include "net/net.h"
34
35#include "utils/xml.h"
36#include "utils/path.h"
37
38#include "configuration.h"
39#include "log.h"
40#include "units.h"
41
42#include <string>
43#include <set>
44
46{
47 static std::string mSettingsFile;
48 static std::set<std::string> mIncludedFiles;
49
50 static bool loadFile(const std::string &filename);
51
52 void load()
53 {
54 // initialize managers
55 paths.clear();
57 hairDB.init();
58 itemDb->init();
66
67 // load stuff from settings
68 if (!loadFile("settings.xml"))
69 {
70 // fall back to loading certain known files for TMW compatibility
71 loadFile("paths.xml");
72 loadFile("items.xml");
73 loadFile("monsters.xml");
74 loadFile("npcs.xml");
75 loadFile("emotes.xml");
76 loadFile("status-effects.xml");
77 loadFile("itemcolors.xml");
78 loadFile("units.xml");
79 }
80
90
92 {
94 }
95 }
96
97 void unload()
98 {
105 if (itemDb)
106 itemDb->unload();
107 hairDB.unload();
109 }
110
114 static bool loadFile(const std::string &filename)
115 {
116 Log::info("Loading game settings from %s", filename.c_str());
117
118 XML::Document doc(filename);
119 XML::Node node = doc.rootNode();
120
121 // add file to include set
122 mIncludedFiles.insert(filename);
123
124 // FIXME: check root node's name when bjorn decides it's time
125 if (!node /*|| node.name() != "settings" */)
126 {
127 Log::info("Settings Manager: %s is not a valid settings file!", filename.c_str());
128 return false;
129 }
130
131 if (node.name() == "monsters")
132 {
133 if (node.hasAttribute("offset"))
134 {
136 }
137 }
138
139 // go through every node
140 for (auto childNode : node.children())
141 {
142 if (childNode.name() == "include")
143 {
144 // include an other file
145 std::string includeFile = childNode.getProperty("file", std::string());
146
147 if (!includeFile.empty())
148 {
149 // build absolute path
150 const auto path = utils::path(filename);
151 includeFile = utils::cleanPath(utils::joinPaths(path, includeFile));
152 }
153 else
154 {
155 // try to get name property, which has an absolute value
156 includeFile = childNode.getProperty("name", std::string());
157 }
158
159 // check if file property was given
160 if (!includeFile.empty())
161 {
162 // check if we're not entering a loop
163 if (mIncludedFiles.find(includeFile) != mIncludedFiles.end())
164 {
165 Log::warn("Circular include loop detecting while including %s from %s", includeFile.c_str(), filename.c_str());
166 }
167 else
168 {
169 loadFile(includeFile);
170 }
171 }
172 else
173 {
174 Log::warn("<include> element without 'file' or 'name' attribute in %s", filename.c_str());
175 }
176 }
177 else if (childNode.name() == "option")
178 {
179 // options from paths.xml
180 std::string name = childNode.getProperty("name", std::string());
181 std::string value = childNode.getProperty("value", std::string());
182
183 if (!name.empty())
184 paths.setValue(name, value);
185 else
186 Log::warn("option without a name found in %s", filename.c_str());
187 }
188 else if (childNode.name() == "attribute")
189 {
190 Attributes::readAttributeNode(childNode, filename);
191 }
192 else if (childNode.name() == "points")
193 {
194 Attributes::readPointsNode(childNode, filename);
195 }
196 else if (childNode.name() == "color")
197 {
198 hairDB.readHairColorNode(childNode, filename);
199 }
200 else if (childNode.name() == "list")
201 {
202 // todo: consider if we need a "color DB", but in tmwa clientdata
203 // I only see hair colors in the itemcolors.xml file.
204 const std::string name = childNode.getProperty("name", std::string());
205 if (name == "hair")
206 {
207 for (auto hairColorNode : childNode.children())
208 {
209 if (hairColorNode.name() == "color")
210 hairDB.readHairColorNode(hairColorNode, filename);
211 }
212 }
213 }
214 else if (childNode.name() == "item")
215 {
216 itemDb->readItemNode(childNode, filename);
217 }
218 else if (childNode.name() == "monster")
219 {
220 MonsterDB::readMonsterNode(childNode, filename);
221 }
222 else if (childNode.name() == "ability")
223 {
224 AbilityDB::readAbilityNode(childNode, filename);
225 }
226 else if (childNode.name() == "npc")
227 {
228 NPCDB::readNPCNode(childNode, filename);
229 }
230 else if (childNode.name() == "var")
231 {
232 QuestDB::readQuestVarNode(childNode, filename);
233 }
234 else if (childNode.name() == "emote")
235 {
236 EmoteDB::readEmoteNode(childNode, filename);
237 }
238 else if (childNode.name() == "status-effect")
239 {
240 StatusEffectDB::readStatusEffectNode(childNode, filename);
241 }
242 else if (childNode.name() == "unit")
243 {
244 Units::readUnitNode(childNode, filename);
245 }
246 }
247
248 mIncludedFiles.erase(filename);
249 return true;
250 }
251}
void clear()
Re-sets all data in the configuration.
void setValue(const std::string &key, const std::string &value)
Sets an option using a string value.
void checkStatus()
Definition hairdb.cpp:48
void init()
Definition hairdb.cpp:29
void readHairColorNode(XML::Node node, const std::string &filename)
Definition hairdb.cpp:38
void unload()
Clear the color data.
Definition hairdb.cpp:53
virtual void checkStatus()=0
virtual void readItemNode(XML::Node node, const std::string &filename)=0
virtual void init()=0
virtual void unload()
Frees item data.
Definition itemdb.cpp:268
static void init()
static void checkStatus()
static void readStatusEffectNode(XML::Node node, const std::string &filename)
static void unload()
static void init()
Definition units.cpp:53
static void readUnitNode(XML::Node node, const std::string &filename)
Definition units.cpp:95
static void checkStatus()
Definition units.cpp:146
A helper class for parsing an XML document, which also cleans it up again (RAII).
Definition xml.h:190
std::string_view name() const
Definition xml.h:46
int getProperty(const char *name, int def) const
Definition xml.h:144
bool hasAttribute(const char *name) const
Definition xml.h:139
HairDB hairDB
Hair styles and colors info database.
Definition client.cpp:107
Configuration paths
XML default paths information reader.
Definition client.cpp:99
ItemDB * itemDb
Items info database.
Definition client.cpp:106
void unload()
Definition abilitydb.cpp:81
void checkStatus()
Definition abilitydb.cpp:76
void init()
Definition abilitydb.cpp:49
void readAbilityNode(XML::Node node, const std::string &filename)
Definition abilitydb.cpp:55
void informItemDB()
void readPointsNode(XML::Node node, const std::string &filename)
Read points node.
void checkStatus()
Check if all the data loaded by readPointsNode and readAttributeNode is ok.
void unload()
void readAttributeNode(XML::Node node, const std::string &filename)
Read attribute node.
void checkStatus()
Definition emotedb.cpp:99
void readEmoteNode(XML::Node node, const std::string &filename)
Definition emotedb.cpp:49
void init()
Definition emotedb.cpp:39
void unload()
Definition emotedb.cpp:104
void warn(const char *log_text,...) LOG_PRINTF_ATTR
void info(const char *log_text,...) LOG_PRINTF_ATTR
void init()
Initialize MonsterDB.
Definition monsterdb.cpp:51
void setMonsterIdOffset(int offset)
Definition monsterdb.cpp:60
void readMonsterNode(XML::Node node, const std::string &filename)
Read <monster> node from settings.
Definition monsterdb.cpp:68
void unload()
void checkStatus()
Check if everything was loaded correctly.
void unload()
Definition npcdb.cpp:83
void checkStatus()
Definition npcdb.cpp:78
void init()
Definition npcdb.cpp:38
void readNPCNode(XML::Node node, const std::string &filename)
Definition npcdb.cpp:44
ServerType getNetworkType()
Definition net.cpp:200
void unload()
Definition questdb.cpp:119
void readQuestVarNode(XML::Node node, const std::string &filename)
Definition questdb.cpp:45
void init()
Definition questdb.cpp:40
std::string cleanPath(const std::string &path)
Removes relative elements from the path.
Definition path.cpp:86
std::string_view path(std::string_view fullFilePath)
Returns the path without the file name.
Definition path.cpp:29
std::string joinPaths(std::string_view path1, std::string_view path2)
Join two path elements into one.
Definition path.cpp:55