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