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 return m_map_settings->getNoEx(name, *value_out);
59 bool MapSettingsManager::getMapSettingNoiseParams(
60 const std::string &name, NoiseParams *value_out)
62 // TODO: Rename to "getNoiseParams"
63 return m_map_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_defaults->set(name, value);
82 bool MapSettingsManager::setMapSettingNoiseParams(
83 const std::string &name, const NoiseParams *value, bool override_meta)
89 m_map_settings->setNoiseParams(name, *value);
91 m_defaults->setNoiseParams(name, *value);
97 bool MapSettingsManager::loadMapMeta()
99 std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
102 errorstream << "loadMapMeta: could not open "
103 << m_map_meta_path << std::endl;
107 if (!m_map_settings->parseConfigLines(is)) {
108 errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
116 bool MapSettingsManager::saveMapMeta()
118 // If mapgen params haven't been created yet; abort
119 if (!mapgen_params) {
120 infostream << "saveMapMeta: mapgen_params not present! "
121 << "Server startup was probably interrupted." << std::endl;
125 // Paths set up by subgames.cpp, but not in unittests
126 if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
127 errorstream << "saveMapMeta: could not create dirs to "
132 mapgen_params->MapgenParams::writeParams(m_map_settings);
133 mapgen_params->writeParams(m_map_settings);
135 if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) {
136 errorstream << "saveMapMeta: could not write "
137 << m_map_meta_path << std::endl;
145 MapgenParams *MapSettingsManager::makeMapgenParams()
148 return mapgen_params;
150 assert(m_map_settings);
153 // Now, get the mapgen type so we can create the appropriate MapgenParams
155 MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
156 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
158 if (mgtype == MAPGEN_INVALID) {
159 errorstream << "EmergeManager: mapgen '" << mg_name <<
160 "' not valid; falling back to " <<
161 Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
162 mgtype = MAPGEN_DEFAULT;
165 // Create our MapgenParams
166 MapgenParams *params = Mapgen::createMapgenParams(mgtype);
170 params->mgtype = mgtype;
172 // Load the rest of the mapgen params from our active settings
173 params->MapgenParams::readParams(m_map_settings);
174 params->readParams(m_map_settings);
176 // Hold onto our params
177 mapgen_params = params;