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)
31 m_map_settings = Settings::createLayer(SL_MAP, "[end_of_params]");
32 Mapgen::setDefaultSettings(Settings::getLayer(SL_DEFAULTS));
36 MapSettingsManager::~MapSettingsManager()
38 delete m_map_settings;
43 bool MapSettingsManager::getMapSetting(
44 const std::string &name, std::string *value_out)
46 // Get from map_meta.txt, then try from all other sources
47 if (m_map_settings->getNoEx(name, *value_out))
50 // Compatibility kludge
52 return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out);
58 bool MapSettingsManager::getMapSettingNoiseParams(
59 const std::string &name, NoiseParams *value_out)
61 // TODO: Rename to "getNoiseParams"
62 return m_map_settings->getNoiseParams(name, *value_out);
66 bool MapSettingsManager::setMapSetting(
67 const std::string &name, const std::string &value, bool override_meta)
73 m_map_settings->set(name, value);
75 Settings::getLayer(SL_GLOBAL)->set(name, value);
81 bool MapSettingsManager::setMapSettingNoiseParams(
82 const std::string &name, const NoiseParams *value, bool override_meta)
88 m_map_settings->setNoiseParams(name, *value);
90 Settings::getLayer(SL_GLOBAL)->setNoiseParams(name, *value);
96 bool MapSettingsManager::loadMapMeta()
98 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
101 errorstream << "loadMapMeta: could not open "
102 << m_map_meta_path << std::endl;
106 if (!m_map_settings->parseConfigLines(is)) {
107 errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
115 bool MapSettingsManager::saveMapMeta()
117 // If mapgen params haven't been created yet; abort
118 if (!mapgen_params) {
119 infostream << "saveMapMeta: mapgen_params not present! "
120 << "Server startup was probably interrupted." << std::endl;
124 // Paths set up by subgames.cpp, but not in unittests
125 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
126 errorstream << "saveMapMeta: could not create dirs to "
131 mapgen_params->MapgenParams::writeParams(m_map_settings);
132 mapgen_params->writeParams(m_map_settings);
134 if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) {
135 errorstream << "saveMapMeta: could not write "
136 << m_map_meta_path << std::endl;
144 MapgenParams *MapSettingsManager::makeMapgenParams()
147 return mapgen_params;
149 assert(m_map_settings != NULL);
151 // At this point, we have (in order of precedence):
152 // 1). SL_MAP containing map_meta.txt settings or
153 // explicit overrides from scripts
154 // 2). SL_GLOBAL containing all user-specified config file
156 // 3). SL_DEFAULTS containing any low-priority settings from
157 // scripts, e.g. mods using Lua as an enhanced config file)
159 // Now, get the mapgen type so we can create the appropriate MapgenParams
161 MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
162 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
164 if (mgtype == MAPGEN_INVALID) {
165 errorstream << "EmergeManager: mapgen '" << mg_name <<
166 "' not valid; falling back to " <<
167 Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
168 mgtype = MAPGEN_DEFAULT;
171 // Create our MapgenParams
172 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
176 params->mgtype = mgtype;
178 // Load the rest of the mapgen params from our active settings
179 params->MapgenParams::readParams(m_map_settings);
180 params->readParams(m_map_settings);
182 // Hold onto our params
183 mapgen_params = params;