]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map_settings_manager.cpp
Merge pull request #59 from PrairieAstronomer/readme_irrlicht_change
[dragonfireclient.git] / src / map_settings_manager.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "debug.h"
21 #include "filesys.h"
22 #include "log.h"
23 #include "mapgen/mapgen.h"
24 #include "settings.h"
25
26 #include "map_settings_manager.h"
27
28 MapSettingsManager::MapSettingsManager(const std::string &map_meta_path):
29         m_map_meta_path(map_meta_path),
30         m_hierarchy(g_settings)
31 {
32         /*
33          * We build our own hierarchy which falls back to the global one.
34          * It looks as follows: (lowest prio first)
35          * 0: whatever is picked up from g_settings (incl. engine defaults)
36          * 1: defaults set by scripts (override_meta = false)
37          * 2: settings present in map_meta.txt or overriden by scripts
38          */
39         m_defaults = new Settings("", &m_hierarchy, 1);
40         m_map_settings = new Settings("[end_of_params]", &m_hierarchy, 2);
41 }
42
43
44 MapSettingsManager::~MapSettingsManager()
45 {
46         delete m_defaults;
47         delete m_map_settings;
48         delete mapgen_params;
49 }
50
51
52 bool MapSettingsManager::getMapSetting(
53         const std::string &name, std::string *value_out)
54 {
55         return m_map_settings->getNoEx(name, *value_out);
56 }
57
58
59 bool MapSettingsManager::getMapSettingNoiseParams(
60         const std::string &name, NoiseParams *value_out)
61 {
62         // TODO: Rename to "getNoiseParams"
63         return m_map_settings->getNoiseParams(name, *value_out);
64 }
65
66
67 bool MapSettingsManager::setMapSetting(
68         const std::string &name, const std::string &value, bool override_meta)
69 {
70         if (mapgen_params)
71                 return false;
72
73         if (override_meta)
74                 m_map_settings->set(name, value);
75         else
76                 m_defaults->set(name, value);
77
78         return true;
79 }
80
81
82 bool MapSettingsManager::setMapSettingNoiseParams(
83         const std::string &name, const NoiseParams *value, bool override_meta)
84 {
85         if (mapgen_params)
86                 return false;
87
88         if (override_meta)
89                 m_map_settings->setNoiseParams(name, *value);
90         else
91                 m_defaults->setNoiseParams(name, *value);
92
93         return true;
94 }
95
96
97 bool MapSettingsManager::loadMapMeta()
98 {
99         std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
100
101         if (!is.good()) {
102                 errorstream << "loadMapMeta: could not open "
103                         << m_map_meta_path << std::endl;
104                 return false;
105         }
106
107         if (!m_map_settings->parseConfigLines(is)) {
108                 errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
109                 return false;
110         }
111
112         return true;
113 }
114
115
116 bool MapSettingsManager::saveMapMeta()
117 {
118         // If mapgen params haven't been created yet; abort
119         if (!mapgen_params) {
120                 infostream << "saveMapMeta: mapgen_params not present! "
121                         << "Server startup was probably interrupted." << std::endl;
122                 return false;
123         }
124
125         // Paths set up by subgames.cpp, but not in unittests
126         if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
127                 errorstream << "saveMapMeta: could not create dirs to "
128                         << m_map_meta_path;
129                 return false;
130         }
131
132         mapgen_params->MapgenParams::writeParams(m_map_settings);
133         mapgen_params->writeParams(m_map_settings);
134
135         if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) {
136                 errorstream << "saveMapMeta: could not write "
137                         << m_map_meta_path << std::endl;
138                 return false;
139         }
140
141         return true;
142 }
143
144
145 MapgenParams *MapSettingsManager::makeMapgenParams()
146 {
147         if (mapgen_params)
148                 return mapgen_params;
149
150         assert(m_map_settings);
151         assert(m_defaults);
152
153         // Now, get the mapgen type so we can create the appropriate MapgenParams
154         std::string mg_name;
155         MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
156                 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
157
158         if (mgtype == MAPGEN_INVALID) {
159                 errorstream << "EmergeManager: mapgen '" << mg_name <<
160                         "' not valid; falling back to " <<
161                         Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
162                 mgtype = MAPGEN_DEFAULT;
163         }
164
165         // Create our MapgenParams
166         MapgenParams *params = Mapgen::createMapgenParams(mgtype);
167         if (!params)
168                 return nullptr;
169
170         params->mgtype = mgtype;
171
172         // Load the rest of the mapgen params from our active settings
173         params->MapgenParams::readParams(m_map_settings);
174         params->readParams(m_map_settings);
175
176         // Hold onto our params
177         mapgen_params = params;
178
179         return params;
180 }