]> git.lizzy.rs Git - minetest.git/blob - src/mods.cpp
Move globals from main.cpp to more sane locations
[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
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::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods)
113 {
114         std::map<std::string, 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::map<std::string, ModSpec> content =
122                                 flattenModTree(mod.modpack_content);
123                         result.insert(content.begin(),content.end());
124                         result.insert(std::make_pair(mod.name,mod));
125                 }
126                 else //not a modpack
127                 {
128                         result.insert(std::make_pair(mod.name,mod));
129                 }
130         }
131         return result;
132 }
133
134 std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
135 {
136         std::vector<ModSpec> result;
137         for(std::map<std::string,ModSpec>::iterator it = mods.begin();
138                 it != mods.end(); ++it)
139         {
140                 ModSpec mod = (*it).second;
141                 if(mod.is_modpack)
142                 {
143                         std::vector<ModSpec> content = flattenMods(mod.modpack_content);
144                         result.reserve(result.size() + content.size());
145                         result.insert(result.end(),content.begin(),content.end());
146
147                 }
148                 else //not a modpack
149                 {
150                         result.push_back(mod);
151                 }
152         }
153         return result;
154 }
155
156 ModConfiguration::ModConfiguration(std::string worldpath)
157 {
158         SubgameSpec gamespec = findWorldSubgame(worldpath);
159
160         // Add all game mods and all world mods
161         addModsInPath(gamespec.gamemods_path);
162         addModsInPath(worldpath + DIR_DELIM + "worldmods");
163
164         // check world.mt file for mods explicitely declared to be
165         // loaded or not by a load_mod_<modname> = ... line.
166         std::string worldmt = worldpath+DIR_DELIM+"world.mt";
167         Settings worldmt_settings;
168         worldmt_settings.readConfigFile(worldmt.c_str());
169         std::vector<std::string> names = worldmt_settings.getNames();
170         std::set<std::string> include_mod_names;
171         for(std::vector<std::string>::iterator it = names.begin();
172                 it != names.end(); ++it)
173         {
174                 std::string name = *it;
175                 // for backwards compatibility: exclude only mods which are
176                 // explicitely excluded. if mod is not mentioned at all, it is
177                 // enabled. So by default, all installed mods are enabled.
178                 if (name.compare(0,9,"load_mod_") == 0 &&
179                         worldmt_settings.getBool(name))
180                 {
181                         include_mod_names.insert(name.substr(9));
182                 }
183         }
184
185         // Collect all mods that are also in include_mod_names
186         std::vector<ModSpec> addon_mods;
187         for(std::set<std::string>::const_iterator it_path = gamespec.addon_mods_paths.begin();
188                         it_path != gamespec.addon_mods_paths.end(); ++it_path)
189         {
190                 std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(*it_path));
191                 for(std::vector<ModSpec>::iterator it = addon_mods_in_path.begin();
192                         it != addon_mods_in_path.end(); ++it)
193                 {
194                         ModSpec& mod = *it;
195                         if(include_mod_names.count(mod.name) != 0)
196                                 addon_mods.push_back(mod);
197                         else
198                                 worldmt_settings.setBool("load_mod_" + mod.name, false);
199                 }
200         }
201         worldmt_settings.updateConfigFile(worldmt.c_str());
202
203         addMods(addon_mods);
204
205         // report on name conflicts
206         if(!m_name_conflicts.empty()){
207                 std::string s = "Unresolved name conflicts for mods ";
208                 for(std::set<std::string>::const_iterator it = m_name_conflicts.begin();
209                                 it != m_name_conflicts.end(); ++it)
210                 {
211                         if(it != m_name_conflicts.begin()) s += ", ";
212                         s += std::string("\"") + (*it) + "\"";
213                 }
214                 s += ".";
215                 throw ModError(s);
216         }
217
218         // get the mods in order
219         resolveDependencies();
220 }
221
222 void ModConfiguration::addModsInPath(std::string path)
223 {
224         addMods(flattenMods(getModsInPath(path)));
225 }
226
227 void ModConfiguration::addMods(std::vector<ModSpec> new_mods)
228 {
229         // Maintain a map of all existing m_unsatisfied_mods.
230         // Keys are mod names and values are indices into m_unsatisfied_mods.
231         std::map<std::string, u32> existing_mods;
232         for(u32 i = 0; i < m_unsatisfied_mods.size(); ++i){
233                 existing_mods[m_unsatisfied_mods[i].name] = i;
234         }
235
236         // Add new mods
237         for(int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack){
238                 // First iteration:
239                 // Add all the mods that come from modpacks
240                 // Second iteration:
241                 // Add all the mods that didn't come from modpacks
242
243                 std::set<std::string> seen_this_iteration;
244
245                 for(std::vector<ModSpec>::const_iterator it = new_mods.begin();
246                                 it != new_mods.end(); ++it){
247                         const ModSpec &mod = *it;
248                         if(mod.part_of_modpack != (bool)want_from_modpack)
249                                 continue;
250                         if(existing_mods.count(mod.name) == 0){
251                                 // GOOD CASE: completely new mod.
252                                 m_unsatisfied_mods.push_back(mod);
253                                 existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
254                         }
255                         else if(seen_this_iteration.count(mod.name) == 0){
256                                 // BAD CASE: name conflict in different levels.
257                                 u32 oldindex = existing_mods[mod.name];
258                                 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
259                                 actionstream<<"WARNING: Mod name conflict detected: \""
260                                         <<mod.name<<"\""<<std::endl
261                                         <<"Will not load: "<<oldmod.path<<std::endl
262                                         <<"Overridden by: "<<mod.path<<std::endl;
263                                 m_unsatisfied_mods[oldindex] = mod;
264
265                                 // If there was a "VERY BAD CASE" name conflict
266                                 // in an earlier level, ignore it.
267                                 m_name_conflicts.erase(mod.name);
268                         }
269                         else{
270                                 // VERY BAD CASE: name conflict in the same level.
271                                 u32 oldindex = existing_mods[mod.name];
272                                 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
273                                 errorstream<<"WARNING: Mod name conflict detected: \""
274                                         <<mod.name<<"\""<<std::endl
275                                         <<"Will not load: "<<oldmod.path<<std::endl
276                                         <<"Will not load: "<<mod.path<<std::endl;
277                                 m_unsatisfied_mods[oldindex] = mod;
278                                 m_name_conflicts.insert(mod.name);
279                         }
280                         seen_this_iteration.insert(mod.name);
281                 }
282         }
283 }
284
285 void ModConfiguration::resolveDependencies()
286 {
287         // Step 1: Compile a list of the mod names we're working with
288         std::set<std::string> modnames;
289         for(std::vector<ModSpec>::iterator it = m_unsatisfied_mods.begin();
290                 it != m_unsatisfied_mods.end(); ++it){
291                 modnames.insert((*it).name);
292         }
293
294         // Step 2: get dependencies (including optional dependencies)
295         // of each mod, split mods into satisfied and unsatisfied
296         std::list<ModSpec> satisfied;
297         std::list<ModSpec> unsatisfied;
298         for(std::vector<ModSpec>::iterator it = m_unsatisfied_mods.begin();
299                         it != m_unsatisfied_mods.end(); ++it){
300                 ModSpec mod = *it;
301                 mod.unsatisfied_depends = mod.depends;
302                 // check which optional dependencies actually exist
303                 for(std::set<std::string>::iterator it_optdep = mod.optdepends.begin();
304                                 it_optdep != mod.optdepends.end(); ++it_optdep){
305                         std::string optdep = *it_optdep;
306                         if(modnames.count(optdep) != 0)
307                                 mod.unsatisfied_depends.insert(optdep);
308                 }
309                 // if a mod has no depends it is initially satisfied
310                 if(mod.unsatisfied_depends.empty())
311                         satisfied.push_back(mod);
312                 else
313                         unsatisfied.push_back(mod);
314         }
315
316         // Step 3: mods without unmet dependencies can be appended to
317         // the sorted list.
318         while(!satisfied.empty()){
319                 ModSpec mod = satisfied.back();
320                 m_sorted_mods.push_back(mod);
321                 satisfied.pop_back();
322                 for(std::list<ModSpec>::iterator it = unsatisfied.begin();
323                                 it != unsatisfied.end(); ){
324                         ModSpec& mod2 = *it;
325                         mod2.unsatisfied_depends.erase(mod.name);
326                         if(mod2.unsatisfied_depends.empty()){
327                                 satisfied.push_back(mod2);
328                                 it = unsatisfied.erase(it);
329                         }
330                         else{
331                                 ++it;
332                         }
333                 }
334         }
335
336         // Step 4: write back list of unsatisfied mods
337         m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
338 }
339
340 #if USE_CURL
341 Json::Value getModstoreUrl(std::string url)
342 {
343         std::vector<std::string> extra_headers;
344
345         bool special_http_header = true;
346
347         try {
348                 special_http_header = g_settings->getBool("modstore_disable_special_http_header");
349         } catch (SettingNotFoundException) {}
350
351         if (special_http_header) {
352                 extra_headers.push_back("Accept: application/vnd.minetest.mmdb-v1+json");
353         }
354         return fetchJsonValue(url, special_http_header ? &extra_headers : NULL);
355 }
356
357 #endif