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.
26 #include "map_settings_manager.h"
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)
34 assert(m_user_settings != NULL);
38 MapSettingsManager::~MapSettingsManager()
40 delete m_map_settings;
45 bool MapSettingsManager::getMapSetting(
46 const std::string &name, std::string *value_out)
48 if (m_map_settings->getNoEx(name, *value_out))
51 // Compatibility kludge
52 if (m_user_settings == g_settings && name == "seed")
53 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
55 return m_user_settings->getNoEx(name, *value_out);
59 bool MapSettingsManager::getMapSettingNoiseParams(
60 const std::string &name, NoiseParams *value_out)
62 return m_map_settings->getNoiseParams(name, *value_out) ||
63 m_user_settings->getNoiseParams(name, *value_out);
67 bool MapSettingsManager::setMapSetting(
68 const std::string &name, const std::string &value, bool override_meta)
74 m_map_settings->set(name, value);
76 m_map_settings->setDefault(name, value);
82 bool MapSettingsManager::setMapSettingNoiseParams(
83 const std::string &name, const NoiseParams *value, bool override_meta)
88 m_map_settings->setNoiseParams(name, *value, !override_meta);
93 bool MapSettingsManager::loadMapMeta()
95 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
98 errorstream << "loadMapMeta: could not open "
99 << m_map_meta_path << std::endl;
103 if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
104 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
112 bool MapSettingsManager::saveMapMeta()
114 // If mapgen params haven't been created yet; abort
118 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
119 errorstream << "saveMapMeta: could not create dirs to "
124 std::ostringstream oss(std::ios_base::binary);
127 mapgen_params->MapgenParams::writeParams(&conf);
128 mapgen_params->writeParams(&conf);
129 conf.writeLines(oss);
131 // NOTE: If there are ever types of map settings other than
132 // those relating to map generation, save them here
134 oss << "[end_of_params]\n";
136 if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
137 errorstream << "saveMapMeta: could not write "
138 << m_map_meta_path << std::endl;
146 MapgenParams *MapSettingsManager::makeMapgenParams()
149 return mapgen_params;
151 assert(m_user_settings != NULL);
152 assert(m_map_settings != NULL);
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
159 // 3). g_settings->m_settings containing all user-specified config file
161 // 4). g_settings->m_defaults containing any low-priority settings from
162 // scripts, e.g. mods using Lua as an enhanced config file)
164 // Now, get the mapgen type so we can create the appropriate MapgenParams
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;
175 // Create our MapgenParams
176 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
180 params->mgtype = mgtype;
182 // Load the rest of the mapgen params from our active settings
183 params->MapgenParams::readParams(m_user_settings);
184 params->MapgenParams::readParams(m_map_settings);
185 params->readParams(m_user_settings);
186 params->readParams(m_map_settings);
188 // Hold onto our params
189 mapgen_params = params;