Translated using Weblate (Russian)
[oweals/minetest.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(Settings *user_settings,
29                 const std::string &map_meta_path):
30         m_map_meta_path(map_meta_path),
31         m_map_settings(new Settings()),
32         m_user_settings(user_settings)
33 {
34         assert(m_user_settings != NULL);
35 }
36
37
38 MapSettingsManager::~MapSettingsManager()
39 {
40         delete m_map_settings;
41         delete mapgen_params;
42 }
43
44
45 bool MapSettingsManager::getMapSetting(
46         const std::string &name, std::string *value_out)
47 {
48         if (m_map_settings->getNoEx(name, *value_out))
49                 return true;
50
51         // Compatibility kludge
52         if (m_user_settings == g_settings && name == "seed")
53                 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
54
55         return m_user_settings->getNoEx(name, *value_out);
56 }
57
58
59 bool MapSettingsManager::getMapSettingNoiseParams(
60         const std::string &name, NoiseParams *value_out)
61 {
62         return m_map_settings->getNoiseParams(name, *value_out) ||
63                 m_user_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_map_settings->setDefault(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         m_map_settings->setNoiseParams(name, *value, !override_meta);
89         return true;
90 }
91
92
93 bool MapSettingsManager::loadMapMeta()
94 {
95         std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
96
97         if (!is.good()) {
98                 errorstream << "loadMapMeta: could not open "
99                         << m_map_meta_path << std::endl;
100                 return false;
101         }
102
103         if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
104                 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
105                 return false;
106         }
107
108         return true;
109 }
110
111
112 bool MapSettingsManager::saveMapMeta()
113 {
114         // If mapgen params haven't been created yet; abort
115         if (!mapgen_params)
116                 return false;
117
118         if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
119                 errorstream << "saveMapMeta: could not create dirs to "
120                         << m_map_meta_path;
121                 return false;
122         }
123
124         std::ostringstream oss(std::ios_base::binary);
125         Settings conf;
126
127         mapgen_params->MapgenParams::writeParams(&conf);
128         mapgen_params->writeParams(&conf);
129         conf.writeLines(oss);
130
131         // NOTE: If there are ever types of map settings other than
132         // those relating to map generation, save them here
133
134         oss << "[end_of_params]\n";
135
136         if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
137                 errorstream << "saveMapMeta: could not write "
138                         << m_map_meta_path << std::endl;
139                 return false;
140         }
141
142         return true;
143 }
144
145
146 MapgenParams *MapSettingsManager::makeMapgenParams()
147 {
148         if (mapgen_params)
149                 return mapgen_params;
150
151         assert(m_user_settings != NULL);
152         assert(m_map_settings != NULL);
153
154         // At this point, we have (in order of precedence):
155         // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or
156         //     explicit overrides from scripts
157         // 2). m_mapgen_settings->m_defaults containing script-set mgparams without
158         //     overrides
159         // 3). g_settings->m_settings containing all user-specified config file
160         //     settings
161         // 4). g_settings->m_defaults containing any low-priority settings from
162         //     scripts, e.g. mods using Lua as an enhanced config file)
163
164         // Now, get the mapgen type so we can create the appropriate MapgenParams
165         std::string mg_name;
166         MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
167                 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
168         if (mgtype == MAPGEN_INVALID) {
169                 errorstream << "EmergeManager: mapgen '" << mg_name <<
170                         "' not valid; falling back to " <<
171                         Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
172                 mgtype = MAPGEN_DEFAULT;
173         }
174
175         // Create our MapgenParams
176         MapgenParams *params = Mapgen::createMapgenParams(mgtype);
177         if (!params)
178                 return nullptr;
179
180         params->mgtype = mgtype;
181
182         // Load the mapgen param defaults
183         /* FIXME: Why is it done like this? MapgenParams should just
184          * set the defaults in its constructor instead. */
185         {
186                 Settings default_settings;
187                 Mapgen::setDefaultSettings(&default_settings);
188                 params->MapgenParams::readParams(&default_settings);
189                 params->readParams(&default_settings);
190         }
191
192         // Load the rest of the mapgen params from our active settings
193         params->MapgenParams::readParams(m_user_settings);
194         params->MapgenParams::readParams(m_map_settings);
195         params->readParams(m_user_settings);
196         params->readParams(m_map_settings);
197
198         // Hold onto our params
199         mapgen_params = params;
200
201         return params;
202 }