]> git.lizzy.rs Git - dragonfireclient.git/blob - src/map_settings_manager.cpp
Don't let HTTP API pass through untrusted function
[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(const std::string &map_meta_path):
29         m_map_meta_path(map_meta_path),
30         m_hierarchy(g_settings)
31 {
32         /*
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
38          */
39         m_defaults = new Settings("", &m_hierarchy, 1);
40         m_map_settings = new Settings("[end_of_params]", &m_hierarchy, 2);
41 }
42
43
44 MapSettingsManager::~MapSettingsManager()
45 {
46         delete m_defaults;
47         delete m_map_settings;
48         delete mapgen_params;
49 }
50
51
52 bool MapSettingsManager::getMapSetting(
53         const std::string &name, std::string *value_out)
54 {
55         // Try getting it normally first
56         if (m_map_settings->getNoEx(name, *value_out))
57                 return true;
58
59         // If not we may have to resolve some compatibility kludges
60         if (name == "seed")
61                 return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out);
62         return false;
63 }
64
65
66 bool MapSettingsManager::getMapSettingNoiseParams(
67         const std::string &name, NoiseParams *value_out)
68 {
69         // TODO: Rename to "getNoiseParams"
70         return m_map_settings->getNoiseParams(name, *value_out);
71 }
72
73
74 bool MapSettingsManager::setMapSetting(
75         const std::string &name, const std::string &value, bool override_meta)
76 {
77         if (mapgen_params)
78                 return false;
79
80         if (override_meta)
81                 m_map_settings->set(name, value);
82         else
83                 m_defaults->set(name, value);
84
85         return true;
86 }
87
88
89 bool MapSettingsManager::setMapSettingNoiseParams(
90         const std::string &name, const NoiseParams *value, bool override_meta)
91 {
92         if (mapgen_params)
93                 return false;
94
95         if (override_meta)
96                 m_map_settings->setNoiseParams(name, *value);
97         else
98                 m_defaults->setNoiseParams(name, *value);
99
100         return true;
101 }
102
103
104 bool MapSettingsManager::loadMapMeta()
105 {
106         std::ifstream is(m_map_meta_path.c_str(), std::ios_base::binary);
107
108         if (!is.good()) {
109                 errorstream << "loadMapMeta: could not open "
110                         << m_map_meta_path << std::endl;
111                 return false;
112         }
113
114         if (!m_map_settings->parseConfigLines(is)) {
115                 errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl;
116                 return false;
117         }
118
119         return true;
120 }
121
122
123 bool MapSettingsManager::saveMapMeta()
124 {
125         // If mapgen params haven't been created yet; abort
126         if (!mapgen_params) {
127                 infostream << "saveMapMeta: mapgen_params not present! "
128                         << "Server startup was probably interrupted." << std::endl;
129                 return false;
130         }
131
132         // Paths set up by subgames.cpp, but not in unittests
133         if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) {
134                 errorstream << "saveMapMeta: could not create dirs to "
135                         << m_map_meta_path;
136                 return false;
137         }
138
139         mapgen_params->MapgenParams::writeParams(m_map_settings);
140         mapgen_params->writeParams(m_map_settings);
141
142         if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) {
143                 errorstream << "saveMapMeta: could not write "
144                         << m_map_meta_path << std::endl;
145                 return false;
146         }
147
148         return true;
149 }
150
151
152 MapgenParams *MapSettingsManager::makeMapgenParams()
153 {
154         if (mapgen_params)
155                 return mapgen_params;
156
157         assert(m_map_settings);
158         assert(m_defaults);
159
160         // Now, get the mapgen type so we can create the appropriate MapgenParams
161         std::string mg_name;
162         MapgenType mgtype = getMapSetting("mg_name", &mg_name) ?
163                 Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT;
164
165         if (mgtype == MAPGEN_INVALID) {
166                 errorstream << "EmergeManager: mapgen '" << mg_name <<
167                         "' not valid; falling back to " <<
168                         Mapgen::getMapgenName(MAPGEN_DEFAULT) << std::endl;
169                 mgtype = MAPGEN_DEFAULT;
170         }
171
172         // Create our MapgenParams
173         MapgenParams *params = Mapgen::createMapgenParams(mgtype);
174         if (!params)
175                 return nullptr;
176
177         params->mgtype = mgtype;
178
179         // Load the rest of the mapgen params from our active settings
180         params->MapgenParams::readParams(m_map_settings);
181         params->readParams(m_map_settings);
182
183         // Hold onto our params
184         mapgen_params = params;
185
186         return params;
187 }