1 /*
2 Minetest
3 Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 
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.
9 
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.
14 
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.
18 */
19 
20 #include "debug.h"
21 #include "filesys.h"
22 #include "log.h"
23 #include "mapgen/mapgen.h"
24 #include "settings.h"
25 
26 #include "map_settings_manager.h"
27 
MapSettingsManager(const std::string & map_meta_path)28 MapSettingsManager::MapSettingsManager(const std::string &map_meta_path):
29 	m_map_meta_path(map_meta_path)
30 {
31 	m_map_settings = Settings::createLayer(SL_MAP, "[end_of_params]");
32 	Mapgen::setDefaultSettings(Settings::getLayer(SL_DEFAULTS));
33 }
34 
35 
~MapSettingsManager()36 MapSettingsManager::~MapSettingsManager()
37 {
38 	delete m_map_settings;
39 	delete mapgen_params;
40 }
41 
42 
getMapSetting(const std::string & name,std::string * value_out)43 bool MapSettingsManager::getMapSetting(
44 	const std::string &name, std::string *value_out)
45 {
46 	// Get from map_meta.txt, then try from all other sources
47 	if (m_map_settings->getNoEx(name, *value_out))
48 		return true;
49 
50 	// Compatibility kludge
51 	if (name == "seed")
52 		return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out);
53 
54 	return false;
55 }
56 
57 
getMapSettingNoiseParams(const std::string & name,NoiseParams * value_out)58 bool MapSettingsManager::getMapSettingNoiseParams(
59 	const std::string &name, NoiseParams *value_out)
60 {
61 	// TODO: Rename to "getNoiseParams"
62 	return m_map_settings->getNoiseParams(name, *value_out);
63 }
64 
65 
setMapSetting(const std::string & name,const std::string & value,bool override_meta)66 bool MapSettingsManager::setMapSetting(
67 	const std::string &name, const std::string &value, bool override_meta)
68 {
69 	if (mapgen_params)
70 		return false;
71 
72 	if (override_meta)
73 		m_map_settings->set(name, value);
74 	else
75 		Settings::getLayer(SL_GLOBAL)->set(name, value);
76 
77 	return true;
78 }
79 
80 
setMapSettingNoiseParams(const std::string & name,const NoiseParams * value,bool override_meta)81 bool MapSettingsManager::setMapSettingNoiseParams(
82 	const std::string &name, const NoiseParams *value, bool override_meta)
83 {
84 	if (mapgen_params)
85 		return false;
86 
87 	if (override_meta)
88 		m_map_settings->setNoiseParams(name, *value);
89 	else
90 		Settings::getLayer(SL_GLOBAL)->setNoiseParams(name, *value);
91 
92 	return true;
93 }
94 
95 
loadMapMeta()96 bool MapSettingsManager::loadMapMeta()
97 {
98 	std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
99 
100 	if (!is.good()) {
101 		errorstream << "loadMapMeta: could not open "
102 			<< m_map_meta_path << std::endl;
103 		return false;
104 	}
105 
106 	if (!m_map_settings->parseConfigLines(is)) {
107 		errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
108 		return false;
109 	}
110 
111 	return true;
112 }
113 
114 
saveMapMeta()115 bool MapSettingsManager::saveMapMeta()
116 {
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;
121 		return false;
122 	}
123 
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 "
127 			<< m_map_meta_path;
128 		return false;
129 	}
130 
131 	mapgen_params->MapgenParams::writeParams(m_map_settings);
132 	mapgen_params->writeParams(m_map_settings);
133 
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;
137 		return false;
138 	}
139 
140 	return true;
141 }
142 
143 
makeMapgenParams()144 MapgenParams *MapSettingsManager::makeMapgenParams()
145 {
146 	if (mapgen_params)
147 		return mapgen_params;
148 
149 	assert(m_map_settings != NULL);
150 
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
155 	//     settings
156 	// 3). SL_DEFAULTS containing any low-priority settings from
157 	//     scripts, e.g. mods using Lua as an enhanced config file)
158 
159 	// Now, get the mapgen type so we can create the appropriate MapgenParams
160 	std::string mg_name;
161 	MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
162 		Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
163 
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;
169 	}
170 
171 	// Create our MapgenParams
172 	MapgenParams *params = Mapgen::createMapgenParams(mgtype);
173 	if (!params)
174 		return nullptr;
175 
176 	params->mgtype = mgtype;
177 
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);
181 
182 	// Hold onto our params
183 	mapgen_params = params;
184 
185 	return params;
186 }
187