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