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