3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
23 #include "mapgen/mapgen.h"
26 #include "map_settings_manager.h"
28 MapSettingsManager::MapSettingsManager(const std::string &map_meta_path):
29 m_map_meta_path(map_meta_path),
30 m_hierarchy(g_settings)
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
39 m_defaults = new Settings("", &m_hierarchy, 1);
40 m_map_settings = new Settings("[end_of_params]", &m_hierarchy, 2);
44 MapSettingsManager::~MapSettingsManager()
47 delete m_map_settings;
52 bool MapSettingsManager::getMapSetting(
53 const std::string &name, std::string *value_out)
55 // Try getting it normally first
56 if (m_map_settings->getNoEx(name, *value_out))
59 // If not we may have to resolve some compatibility kludges
61 return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out);
66 bool MapSettingsManager::getMapSettingNoiseParams(
67 const std::string &name, NoiseParams *value_out)
69 // TODO: Rename to "getNoiseParams"
70 return m_map_settings->getNoiseParams(name, *value_out);
74 bool MapSettingsManager::setMapSetting(
75 const std::string &name, const std::string &value, bool override_meta)
81 m_map_settings->set(name, value);
83 m_defaults->set(name, value);
89 bool MapSettingsManager::setMapSettingNoiseParams(
90 const std::string &name, const NoiseParams *value, bool override_meta)
96 m_map_settings->setNoiseParams(name, *value);
98 m_defaults->setNoiseParams(name, *value);
104 bool MapSettingsManager::loadMapMeta()
106 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
109 errorstream << "loadMapMeta: could not open "
110 << m_map_meta_path << std::endl;
114 if (!m_map_settings->parseConfigLines(is)) {
115 errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
123 bool MapSettingsManager::saveMapMeta()
125 // If mapgen params haven't been created yet; abort
126 if (!mapgen_params) {
127 infostream << "saveMapMeta: mapgen_params not present! "
128 << "Server startup was probably interrupted." << std::endl;
132 // Paths set up by subgames.cpp, but not in unittests
133 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
134 errorstream << "saveMapMeta: could not create dirs to "
139 mapgen_params->MapgenParams::writeParams(m_map_settings);
140 mapgen_params->writeParams(m_map_settings);
142 if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) {
143 errorstream << "saveMapMeta: could not write "
144 << m_map_meta_path << std::endl;
152 MapgenParams *MapSettingsManager::makeMapgenParams()
155 return mapgen_params;
157 assert(m_map_settings);
160 // Now, get the mapgen type so we can create the appropriate MapgenParams
162 MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
163 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
165 if (mgtype == MAPGEN_INVALID) {
166 errorstream << "EmergeManager: mapgen '" << mg_name <<
167 "' not valid; falling back to " <<
168 Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
169 mgtype = MAPGEN_DEFAULT;
172 // Create our MapgenParams
173 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
177 params->mgtype = mgtype;
179 // Load the rest of the mapgen params from our active settings
180 params->MapgenParams::readParams(m_map_settings);
181 params->readParams(m_map_settings);
183 // Hold onto our params
184 mapgen_params = params;