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