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