]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map_settings_manager.cpp
Fix formspec list spacing (#10861)
[dragonfireclient.git] / src / map_settings_manager.cpp
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
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)
33 {
34         assert(m_user_settings != NULL);
35
36         Mapgen::setDefaultSettings(m_map_settings);
37         // This inherits the combined defaults provided by loadGameConfAndInitWorld.
38         m_map_settings->overrideDefaults(user_settings);
39 }
40
41
42 MapSettingsManager::~MapSettingsManager()
43 {
44         delete m_map_settings;
45         delete mapgen_params;
46 }
47
48
49 bool MapSettingsManager::getMapSetting(
50         const std::string &name, std::string *value_out)
51 {
52         if (m_map_settings->getNoEx(name, *value_out))
53                 return true;
54
55         // Compatibility kludge
56         if (m_user_settings == g_settings && name == "seed")
57                 return m_user_settings->getNoEx("fixed_map_seed", *value_out);
58
59         return m_user_settings->getNoEx(name, *value_out);
60 }
61
62
63 bool MapSettingsManager::getMapSettingNoiseParams(
64         const std::string &name, NoiseParams *value_out)
65 {
66         return m_map_settings->getNoiseParams(name, *value_out) ||
67                 m_user_settings->getNoiseParams(name, *value_out);
68 }
69
70
71 bool MapSettingsManager::setMapSetting(
72         const std::string &name, const std::string &value, bool override_meta)
73 {
74         if (mapgen_params)
75                 return false;
76
77         if (override_meta)
78                 m_map_settings->set(name, value);
79         else
80                 m_map_settings->setDefault(name, value);
81
82         return true;
83 }
84
85
86 bool MapSettingsManager::setMapSettingNoiseParams(
87         const std::string &name, const NoiseParams *value, bool override_meta)
88 {
89         if (mapgen_params)
90                 return false;
91
92         m_map_settings->setNoiseParams(name, *value, !override_meta);
93         return true;
94 }
95
96
97 bool MapSettingsManager::loadMapMeta()
98 {
99         std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
100
101         if (!is.good()) {
102                 errorstream << "loadMapMeta: could not open "
103                         << m_map_meta_path << std::endl;
104                 return false;
105         }
106
107         if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) {
108                 errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl;
109                 return false;
110         }
111
112         return true;
113 }
114
115
116 bool MapSettingsManager::saveMapMeta()
117 {
118         // If mapgen params haven't been created yet; abort
119         if (!mapgen_params)
120                 return false;
121
122         if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
123                 errorstream << "saveMapMeta: could not create dirs to "
124                         << m_map_meta_path;
125                 return false;
126         }
127
128         std::ostringstream oss(std::ios_base::binary);
129         Settings conf;
130
131         mapgen_params->MapgenParams::writeParams(&conf);
132         mapgen_params->writeParams(&conf);
133         conf.writeLines(oss);
134
135         // NOTE: If there are ever types of map settings other than
136         // those relating to map generation, save them here
137
138         oss << "[end_of_params]\n";
139
140         if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) {
141                 errorstream << "saveMapMeta: could not write "
142                         << m_map_meta_path << std::endl;
143                 return false;
144         }
145
146         return true;
147 }
148
149
150 MapgenParams *MapSettingsManager::makeMapgenParams()
151 {
152         if (mapgen_params)
153                 return mapgen_params;
154
155         assert(m_user_settings != NULL);
156         assert(m_map_settings != NULL);
157
158         // At this point, we have (in order of precedence):
159         // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or
160         //     explicit overrides from scripts
161         // 2). m_mapgen_settings->m_defaults containing script-set mgparams without
162         //     overrides
163         // 3). g_settings->m_settings containing all user-specified config file
164         //     settings
165         // 4). g_settings->m_defaults containing any low-priority settings from
166         //     scripts, e.g. mods using Lua as an enhanced config file)
167
168         // Now, get the mapgen type so we can create the appropriate MapgenParams
169         std::string mg_name;
170         MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
171                 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
172         if (mgtype == MAPGEN_INVALID) {
173                 errorstream << "EmergeManager: mapgen '" << mg_name <<
174                         "' not valid; falling back to " <<
175                         Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
176                 mgtype = MAPGEN_DEFAULT;
177         }
178
179         // Create our MapgenParams
180         MapgenParams *params = Mapgen::createMapgenParams(mgtype);
181         if (!params)
182                 return nullptr;
183
184         params->mgtype = mgtype;
185
186         // Load the rest of the mapgen params from our active settings
187         params->MapgenParams::readParams(m_map_settings);
188         params->readParams(m_map_settings);
189
190         // Hold onto our params
191         mapgen_params = params;
192
193         return params;
194 }