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):
31 m_map_meta_path(map_meta_path),
32 m_map_settings(new Settings()),
33 m_user_settings(user_settings)
35 assert(m_user_settings != NULL);
39 MapSettingsManager::~MapSettingsManager()
41 delete m_map_settings;
46 bool MapSettingsManager::getMapSetting(
47 const std::string &name, std::string *value_out)
49 if (m_map_settings->getNoEx(name, *value_out))
52 // Compatibility kludge
53 if (m_user_settings == g_settings && name == "seed")
54 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
56 return m_user_settings->getNoEx(name, *value_out);
60 bool MapSettingsManager::getMapSettingNoiseParams(
61 const std::string &name, NoiseParams *value_out)
63 return m_map_settings->getNoiseParams(name, *value_out) ||
64 m_user_settings->getNoiseParams(name, *value_out);
68 bool MapSettingsManager::setMapSetting(
69 const std::string &name, const std::string &value, bool override_meta)
75 m_map_settings->set(name, value);
77 m_map_settings->setDefault(name, value);
83 bool MapSettingsManager::setMapSettingNoiseParams(
84 const std::string &name, const NoiseParams *value, bool override_meta)
89 m_map_settings->setNoiseParams(name, *value, !override_meta);
94 bool MapSettingsManager::loadMapMeta()
96 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
99 errorstream << "loadMapMeta: could not open "
100 << m_map_meta_path << std::endl;
104 if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
105 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
113 bool MapSettingsManager::saveMapMeta()
115 // If mapgen params haven't been created yet; abort
119 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
120 errorstream << "saveMapMeta: could not create dirs to "
125 std::ostringstream oss(std::ios_base::binary);
128 mapgen_params->MapgenParams::writeParams(&conf);
129 mapgen_params->writeParams(&conf);
130 conf.writeLines(oss);
132 // NOTE: If there are ever types of map settings other than
133 // those relating to map generation, save them here
135 oss << "[end_of_params]\n";
137 if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
138 errorstream << "saveMapMeta: could not write "
139 << m_map_meta_path << std::endl;
147 MapgenParams *MapSettingsManager::makeMapgenParams()
150 return mapgen_params;
152 assert(m_user_settings != NULL);
153 assert(m_map_settings != NULL);
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
160 // 3). g_settings->m_settings containing all user-specified config file
162 // 4). g_settings->m_defaults containing any low-priority settings from
163 // scripts, e.g. mods using Lua as an enhanced config file)
165 // Now, get the mapgen type so we can create the appropriate MapgenParams
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;
176 // Create our MapgenParams
177 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
181 params->mgtype = mgtype;
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);
189 // Hold onto our params
190 mapgen_params = params;