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