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(
29 Settings *user_settings, const std::string &map_meta_path)
31 m_map_meta_path = map_meta_path;
32 m_user_settings = user_settings;
33 m_map_settings = new Settings;
36 assert(m_user_settings != NULL);
40 MapSettingsManager::~MapSettingsManager()
42 delete m_map_settings;
47 bool MapSettingsManager::getMapSetting(
48 const std::string &name, std::string *value_out)
50 if (m_map_settings->getNoEx(name, *value_out))
53 // Compatibility kludge
54 if (m_user_settings == g_settings && name == "seed")
55 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
57 return m_user_settings->getNoEx(name, *value_out);
61 bool MapSettingsManager::getMapSettingNoiseParams(
62 const std::string &name, NoiseParams *value_out)
64 return m_map_settings->getNoiseParams(name, *value_out) ||
65 m_user_settings->getNoiseParams(name, *value_out);
69 bool MapSettingsManager::setMapSetting(
70 const std::string &name, const std::string &value, bool override_meta)
76 m_map_settings->set(name, value);
78 m_map_settings->setDefault(name, value);
84 bool MapSettingsManager::setMapSettingNoiseParams(
85 const std::string &name, const NoiseParams *value, bool override_meta)
90 m_map_settings->setNoiseParams(name, *value, !override_meta);
95 bool MapSettingsManager::loadMapMeta()
97 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
100 errorstream << "loadMapMeta: could not open "
101 << m_map_meta_path << std::endl;
105 if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
106 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
114 bool MapSettingsManager::saveMapMeta()
116 // If mapgen params haven't been created yet; abort
120 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
121 errorstream << "saveMapMeta: could not create dirs to "
126 std::ostringstream oss(std::ios_base::binary);
129 mapgen_params->MapgenParams::writeParams(&conf);
130 mapgen_params->writeParams(&conf);
131 conf.writeLines(oss);
133 // NOTE: If there are ever types of map settings other than
134 // those relating to map generation, save them here
136 oss << "[end_of_params]\n";
138 if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
139 errorstream << "saveMapMeta: could not write "
140 << m_map_meta_path << std::endl;
148 MapgenParams *MapSettingsManager::makeMapgenParams()
151 return mapgen_params;
153 assert(m_user_settings != NULL);
154 assert(m_map_settings != NULL);
156 // At this point, we have (in order of precedence):
157 // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or
158 // explicit overrides from scripts
159 // 2). m_mapgen_settings->m_defaults containing script-set mgparams without
161 // 3). g_settings->m_settings containing all user-specified config file
163 // 4). g_settings->m_defaults containing any low-priority settings from
164 // scripts, e.g. mods using Lua as an enhanced config file)
166 // Now, get the mapgen type so we can create the appropriate MapgenParams
168 MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
169 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
170 if (mgtype == MAPGEN_INVALID) {
171 errorstream << "EmergeManager: mapgen '" << mg_name <<
172 "' not valid; falling back to " <<
173 Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
174 mgtype = MAPGEN_DEFAULT;
177 // Create our MapgenParams
178 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
182 params->mgtype = mgtype;
184 // Load the rest of the mapgen params from our active settings
185 params->MapgenParams::readParams(m_user_settings);
186 params->MapgenParams::readParams(m_map_settings);
187 params->readParams(m_user_settings);
188 params->readParams(m_map_settings);
190 // Hold onto our params
191 mapgen_params = params;