Add warning when disabling secure.enable_security (#9943)
[oweals/minetest.git] / src / content / mods.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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 <cctype>
21 #include <fstream>
22 #include <json/json.h>
23 #include <algorithm>
24 #include "content/mods.h"
25 #include "filesys.h"
26 #include "log.h"
27 #include "content/subgames.h"
28 #include "settings.h"
29 #include "porting.h"
30 #include "convert_json.h"
31
32 bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
33 {
34         dep = trim(dep);
35         symbols.clear();
36         size_t pos = dep.size();
37         while (pos > 0 &&
38                         !string_allowed(dep.substr(pos - 1, 1), MODNAME_ALLOWED_CHARS)) {
39                 // last character is a symbol, not part of the modname
40                 symbols.insert(dep[pos - 1]);
41                 --pos;
42         }
43         dep = trim(dep.substr(0, pos));
44         return !dep.empty();
45 }
46
47 void parseModContents(ModSpec &spec)
48 {
49         // NOTE: this function works in mutual recursion with getModsInPath
50         Settings info;
51         info.readConfigFile((spec.path + DIR_DELIM + "mod.conf").c_str());
52
53         if (info.exists("name"))
54                 spec.name = info.get("name");
55
56         if (info.exists("author"))
57                 spec.author = info.get("author");
58
59         if (info.exists("release"))
60                 spec.release = info.getS32("release");
61
62         spec.depends.clear();
63         spec.optdepends.clear();
64         spec.is_modpack = false;
65         spec.modpack_content.clear();
66
67         // Handle modpacks (defined by containing modpack.txt)
68         std::ifstream modpack_is((spec.path + DIR_DELIM + "modpack.txt").c_str());
69         std::ifstream modpack2_is((spec.path + DIR_DELIM + "modpack.conf").c_str());
70         if (modpack_is.good() || modpack2_is.good()) {
71                 if (modpack_is.good())
72                         modpack_is.close();
73
74                 if (modpack2_is.good())
75                         modpack2_is.close();
76
77                 spec.is_modpack = true;
78                 spec.modpack_content = getModsInPath(spec.path, true);
79
80         } else {
81                 // Attempt to load dependencies from mod.conf
82                 bool mod_conf_has_depends = false;
83                 if (info.exists("depends")) {
84                         mod_conf_has_depends = true;
85                         std::string dep = info.get("depends");
86                         // clang-format off
87                         dep.erase(std::remove_if(dep.begin(), dep.end(),
88                                         static_cast<int (*)(int)>(&std::isspace)), dep.end());
89                         // clang-format on
90                         for (const auto &dependency : str_split(dep, ',')) {
91                                 spec.depends.insert(dependency);
92                         }
93                 }
94
95                 if (info.exists("optional_depends")) {
96                         mod_conf_has_depends = true;
97                         std::string dep = info.get("optional_depends");
98                         // clang-format off
99                         dep.erase(std::remove_if(dep.begin(), dep.end(),
100                                         static_cast<int (*)(int)>(&std::isspace)), dep.end());
101                         // clang-format on
102                         for (const auto &dependency : str_split(dep, ',')) {
103                                 spec.optdepends.insert(dependency);
104                         }
105                 }
106
107                 // Fallback to depends.txt
108                 if (!mod_conf_has_depends) {
109                         std::vector<std::string> dependencies;
110
111                         std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
112                         while (is.good()) {
113                                 std::string dep;
114                                 std::getline(is, dep);
115                                 dependencies.push_back(dep);
116                         }
117
118                         for (auto &dependency : dependencies) {
119                                 std::unordered_set<char> symbols;
120                                 if (parseDependsString(dependency, symbols)) {
121                                         if (symbols.count('?') != 0) {
122                                                 spec.optdepends.insert(dependency);
123                                         } else {
124                                                 spec.depends.insert(dependency);
125                                         }
126                                 }
127                         }
128                 }
129
130                 if (info.exists("description")) {
131                         spec.desc = info.get("description");
132                 } else {
133                         std::ifstream is((spec.path + DIR_DELIM + "description.txt")
134                                                          .c_str());
135                         spec.desc = std::string((std::istreambuf_iterator<char>(is)),
136                                         std::istreambuf_iterator<char>());
137                 }
138         }
139 }
140
141 std::map<std::string, ModSpec> getModsInPath(
142                 const std::string &path, bool part_of_modpack)
143 {
144         // NOTE: this function works in mutual recursion with parseModContents
145
146         std::map<std::string, ModSpec> result;
147         std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
148         std::string modpath;
149
150         for (const fs::DirListNode &dln : dirlist) {
151                 if (!dln.dir)
152                         continue;
153
154                 const std::string &modname = dln.name;
155                 // Ignore all directories beginning with a ".", especially
156                 // VCS directories like ".git" or ".svn"
157                 if (modname[0] == '.')
158                         continue;
159
160                 modpath.clear();
161                 modpath.append(path).append(DIR_DELIM).append(modname);
162
163                 ModSpec spec(modname, modpath, part_of_modpack);
164                 parseModContents(spec);
165                 result.insert(std::make_pair(modname, spec));
166         }
167         return result;
168 }
169
170 std::vector<ModSpec> flattenMods(const std::map<std::string, ModSpec> &mods)
171 {
172         std::vector<ModSpec> result;
173         for (const auto &it : mods) {
174                 const ModSpec &mod = it.second;
175                 if (mod.is_modpack) {
176                         std::vector<ModSpec> content = flattenMods(mod.modpack_content);
177                         result.reserve(result.size() + content.size());
178                         result.insert(result.end(), content.begin(), content.end());
179
180                 } else // not a modpack
181                 {
182                         result.push_back(mod);
183                 }
184         }
185         return result;
186 }
187
188 ModConfiguration::ModConfiguration(const std::string &worldpath)
189 {
190 }
191
192 void ModConfiguration::printUnsatisfiedModsError() const
193 {
194         for (const ModSpec &mod : m_unsatisfied_mods) {
195                 errorstream << "mod \"" << mod.name
196                             << "\" has unsatisfied dependencies: ";
197                 for (const std::string &unsatisfied_depend : mod.unsatisfied_depends)
198                         errorstream << " \"" << unsatisfied_depend << "\"";
199                 errorstream << std::endl;
200         }
201 }
202
203 void ModConfiguration::addModsInPath(const std::string &path)
204 {
205         addMods(flattenMods(getModsInPath(path)));
206 }
207
208 void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
209 {
210         // Maintain a map of all existing m_unsatisfied_mods.
211         // Keys are mod names and values are indices into m_unsatisfied_mods.
212         std::map<std::string, u32> existing_mods;
213         for (u32 i = 0; i < m_unsatisfied_mods.size(); ++i) {
214                 existing_mods[m_unsatisfied_mods[i].name] = i;
215         }
216
217         // Add new mods
218         for (int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack) {
219                 // First iteration:
220                 // Add all the mods that come from modpacks
221                 // Second iteration:
222                 // Add all the mods that didn't come from modpacks
223
224                 std::set<std::string> seen_this_iteration;
225
226                 for (const ModSpec &mod : new_mods) {
227                         if (mod.part_of_modpack != (bool)want_from_modpack)
228                                 continue;
229
230                         if (existing_mods.count(mod.name) == 0) {
231                                 // GOOD CASE: completely new mod.
232                                 m_unsatisfied_mods.push_back(mod);
233                                 existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
234                         } else if (seen_this_iteration.count(mod.name) == 0) {
235                                 // BAD CASE: name conflict in different levels.
236                                 u32 oldindex = existing_mods[mod.name];
237                                 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
238                                 warningstream << "Mod name conflict detected: \""
239                                               << mod.name << "\"" << std::endl
240                                               << "Will not load: " << oldmod.path
241                                               << std::endl
242                                               << "Overridden by: " << mod.path
243                                               << std::endl;
244                                 m_unsatisfied_mods[oldindex] = mod;
245
246                                 // If there was a "VERY BAD CASE" name conflict
247                                 // in an earlier level, ignore it.
248                                 m_name_conflicts.erase(mod.name);
249                         } else {
250                                 // VERY BAD CASE: name conflict in the same level.
251                                 u32 oldindex = existing_mods[mod.name];
252                                 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
253                                 warningstream << "Mod name conflict detected: \""
254                                               << mod.name << "\"" << std::endl
255                                               << "Will not load: " << oldmod.path
256                                               << std::endl
257                                               << "Will not load: " << mod.path
258                                               << std::endl;
259                                 m_unsatisfied_mods[oldindex] = mod;
260                                 m_name_conflicts.insert(mod.name);
261                         }
262
263                         seen_this_iteration.insert(mod.name);
264                 }
265         }
266 }
267
268 void ModConfiguration::addModsFromConfig(
269                 const std::string &settings_path, const std::set<std::string> &mods)
270 {
271         Settings conf;
272         std::set<std::string> load_mod_names;
273
274         conf.readConfigFile(settings_path.c_str());
275         std::vector<std::string> names = conf.getNames();
276         for (const std::string &name : names) {
277                 if (name.compare(0, 9, "load_mod_") == 0 && conf.get(name) != "false" &&
278                                 conf.get(name) != "nil")
279                         load_mod_names.insert(name.substr(9));
280         }
281
282         std::vector<ModSpec> addon_mods;
283         for (const std::string &i : mods) {
284                 std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(i));
285                 for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
286                                 it != addon_mods_in_path.end(); ++it) {
287                         const ModSpec &mod = *it;
288                         if (load_mod_names.count(mod.name) != 0)
289                                 addon_mods.push_back(mod);
290                         else
291                                 conf.setBool("load_mod_" + mod.name, false);
292                 }
293         }
294         conf.updateConfigFile(settings_path.c_str());
295
296         addMods(addon_mods);
297         checkConflictsAndDeps();
298
299         // complain about mods declared to be loaded, but not found
300         for (const ModSpec &addon_mod : addon_mods)
301                 load_mod_names.erase(addon_mod.name);
302
303         std::vector<ModSpec> unsatisfiedMods = getUnsatisfiedMods();
304
305         for (const ModSpec &unsatisfiedMod : unsatisfiedMods)
306                 load_mod_names.erase(unsatisfiedMod.name);
307
308         if (!load_mod_names.empty()) {
309                 errorstream << "The following mods could not be found:";
310                 for (const std::string &mod : load_mod_names)
311                         errorstream << " \"" << mod << "\"";
312                 errorstream << std::endl;
313         }
314 }
315
316 void ModConfiguration::checkConflictsAndDeps()
317 {
318         // report on name conflicts
319         if (!m_name_conflicts.empty()) {
320                 std::string s = "Unresolved name conflicts for mods ";
321                 for (std::unordered_set<std::string>::const_iterator it =
322                                                 m_name_conflicts.begin();
323                                 it != m_name_conflicts.end(); ++it) {
324                         if (it != m_name_conflicts.begin())
325                                 s += ", ";
326                         s += std::string("\"") + (*it) + "\"";
327                 }
328                 s += ".";
329                 throw ModError(s);
330         }
331
332         // get the mods in order
333         resolveDependencies();
334 }
335
336 void ModConfiguration::resolveDependencies()
337 {
338         // Step 1: Compile a list of the mod names we're working with
339         std::set<std::string> modnames;
340         for (const ModSpec &mod : m_unsatisfied_mods) {
341                 modnames.insert(mod.name);
342         }
343
344         // Step 2: get dependencies (including optional dependencies)
345         // of each mod, split mods into satisfied and unsatisfied
346         std::list<ModSpec> satisfied;
347         std::list<ModSpec> unsatisfied;
348         for (ModSpec mod : m_unsatisfied_mods) {
349                 mod.unsatisfied_depends = mod.depends;
350                 // check which optional dependencies actually exist
351                 for (const std::string &optdep : mod.optdepends) {
352                         if (modnames.count(optdep) != 0)
353                                 mod.unsatisfied_depends.insert(optdep);
354                 }
355                 // if a mod has no depends it is initially satisfied
356                 if (mod.unsatisfied_depends.empty())
357                         satisfied.push_back(mod);
358                 else
359                         unsatisfied.push_back(mod);
360         }
361
362         // Step 3: mods without unmet dependencies can be appended to
363         // the sorted list.
364         while (!satisfied.empty()) {
365                 ModSpec mod = satisfied.back();
366                 m_sorted_mods.push_back(mod);
367                 satisfied.pop_back();
368                 for (auto it = unsatisfied.begin(); it != unsatisfied.end();) {
369                         ModSpec &mod2 = *it;
370                         mod2.unsatisfied_depends.erase(mod.name);
371                         if (mod2.unsatisfied_depends.empty()) {
372                                 satisfied.push_back(mod2);
373                                 it = unsatisfied.erase(it);
374                         } else {
375                                 ++it;
376                         }
377                 }
378         }
379
380         // Step 4: write back list of unsatisfied mods
381         m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
382 }
383
384 #ifndef SERVER
385 ClientModConfiguration::ClientModConfiguration(const std::string &path) :
386                 ModConfiguration(path)
387 {
388         std::set<std::string> paths;
389         std::string path_user = porting::path_user + DIR_DELIM + "clientmods";
390         paths.insert(path);
391         paths.insert(path_user);
392
393         std::string settings_path = path_user + DIR_DELIM + "mods.conf";
394         addModsFromConfig(settings_path, paths);
395 }
396 #endif
397
398 ModMetadata::ModMetadata(const std::string &mod_name) : m_mod_name(mod_name)
399 {
400 }
401
402 void ModMetadata::clear()
403 {
404         Metadata::clear();
405         m_modified = true;
406 }
407
408 bool ModMetadata::save(const std::string &root_path)
409 {
410         Json::Value json;
411         for (StringMap::const_iterator it = m_stringvars.begin();
412                         it != m_stringvars.end(); ++it) {
413                 json[it->first] = it->second;
414         }
415
416         if (!fs::PathExists(root_path)) {
417                 if (!fs::CreateAllDirs(root_path)) {
418                         errorstream << "ModMetadata[" << m_mod_name
419                                     << "]: Unable to save. '" << root_path
420                                     << "' tree cannot be created." << std::endl;
421                         return false;
422                 }
423         } else if (!fs::IsDir(root_path)) {
424                 errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
425                             << root_path << "' is not a directory." << std::endl;
426                 return false;
427         }
428
429         bool w_ok = fs::safeWriteToFile(
430                         root_path + DIR_DELIM + m_mod_name, fastWriteJson(json));
431
432         if (w_ok) {
433                 m_modified = false;
434         } else {
435                 errorstream << "ModMetadata[" << m_mod_name << "]: failed write file."
436                             << std::endl;
437         }
438         return w_ok;
439 }
440
441 bool ModMetadata::load(const std::string &root_path)
442 {
443         m_stringvars.clear();
444
445         std::ifstream is((root_path + DIR_DELIM + m_mod_name).c_str(),
446                         std::ios_base::binary);
447         if (!is.good()) {
448                 return false;
449         }
450
451         Json::Value root;
452         Json::CharReaderBuilder builder;
453         builder.settings_["collectComments"] = false;
454         std::string errs;
455
456         if (!Json::parseFromStream(builder, is, &root, &errs)) {
457                 errorstream << "ModMetadata[" << m_mod_name
458                             << "]: failed read data "
459                                "(Json decoding failure). Message: "
460                             << errs << std::endl;
461                 return false;
462         }
463
464         const Json::Value::Members attr_list = root.getMemberNames();
465         for (const auto &it : attr_list) {
466                 Json::Value attr_value = root[it];
467                 m_stringvars[it] = attr_value.asString();
468         }
469
470         return true;
471 }
472
473 bool ModMetadata::setString(const std::string &name, const std::string &var)
474 {
475         m_modified = Metadata::setString(name, var);
476         return m_modified;
477 }