]> git.lizzy.rs Git - minetest.git/blob - src/mods.cpp
Make shift the default descent control on ladders and when flying
[minetest.git] / src / mods.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 <queue>
22 #include <fstream>
23 #include <sstream>
24 #include <map>
25 #include "filesys.h"
26 #include "strfnd.h"
27 #include "log.h"
28
29 static void collectMods(const std::string &modspath,
30                 std::queue<ModSpec> &mods_satisfied,
31                 core::list<ModSpec> &mods_unsorted,
32                 std::map<std::string, std::string> &mod_names,
33                 std::string indentation)
34 {
35         TRACESTREAM(<<indentation<<"collectMods(\""<<modspath<<"\")"<<std::endl);
36         TRACEDO(indentation += "  ");
37         std::vector<fs::DirListNode> dirlist = fs::GetDirListing(modspath);
38         for(u32 j=0; j<dirlist.size(); j++){
39                 if(!dirlist[j].dir)
40                         continue;
41                 std::string modname = dirlist[j].name;
42                 std::string modpath = modspath + DIR_DELIM + modname;
43                 TRACESTREAM(<<indentation<<"collectMods: "<<modname<<" at \""<<modpath<<"\""<<std::endl);
44                 // Handle modpacks (defined by containing modpack.txt)
45                 {
46                         std::ifstream is((modpath+DIR_DELIM+"modpack.txt").c_str(),
47                                         std::ios_base::binary);
48                         if(is.good()){
49                                 // Is a modpack
50                                 is.close(); // We don't actually need the file
51                                 // Recurse into it
52                                 collectMods(modpath, mods_satisfied, mods_unsorted, mod_names, indentation);
53                                 continue;
54                         }
55                 }
56                 // Detect mod name conflicts
57                 {
58                         std::map<std::string, std::string>::const_iterator i;
59                         i = mod_names.find(modname);
60                         if(i != mod_names.end()){
61                                 std::string s;
62                                 infostream<<indentation<<"WARNING: Mod name conflict detected: "
63                                                 <<std::endl
64                                                 <<"Already loaded: "<<i->second<<std::endl
65                                                 <<"Will not load: "<<modpath<<std::endl;
66                                 continue;
67                         }
68                 }
69                 std::set<std::string> depends;
70                 std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
71                                 std::ios_base::binary);
72                 while(is.good()){
73                         std::string dep;
74                         std::getline(is, dep);
75                         dep = trim(dep);
76                         if(dep != "")
77                                 depends.insert(dep);
78                 }
79                 ModSpec spec(modname, modpath, depends);
80                 mods_unsorted.push_back(spec);
81                 if(depends.empty())
82                         mods_satisfied.push(spec);
83                 mod_names[modname] = modpath;
84         }
85 }
86
87 // Get a dependency-sorted list of ModSpecs
88 core::list<ModSpec> getMods(core::list<std::string> &modspaths)
89                 throw(ModError)
90 {
91         std::queue<ModSpec> mods_satisfied;
92         core::list<ModSpec> mods_unsorted;
93         core::list<ModSpec> mods_sorted;
94         // name, path: For detecting name conflicts
95         std::map<std::string, std::string> mod_names;
96         for(core::list<std::string>::Iterator i = modspaths.begin();
97                         i != modspaths.end(); i++){
98                 std::string modspath = *i;
99                 collectMods(modspath, mods_satisfied, mods_unsorted, mod_names, "");
100         }
101         // Sort by depencencies
102         while(!mods_satisfied.empty()){
103                 ModSpec mod = mods_satisfied.front();
104                 mods_satisfied.pop();
105                 mods_sorted.push_back(mod);
106                 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
107                                 i != mods_unsorted.end(); i++){
108                         ModSpec &mod2 = *i;
109                         if(mod2.unsatisfied_depends.empty())
110                                 continue;
111                         mod2.unsatisfied_depends.erase(mod.name);
112                         if(!mod2.unsatisfied_depends.empty())
113                                 continue;
114                         mods_satisfied.push(mod2);
115                 }
116         }
117         std::ostringstream errs(std::ios::binary);
118         // Check unsatisfied dependencies
119         for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
120                         i != mods_unsorted.end(); i++){
121                 ModSpec &mod = *i;
122                 if(mod.unsatisfied_depends.empty())
123                         continue;
124                 errs<<"mod \""<<mod.name
125                                 <<"\" has unsatisfied dependencies:";
126                 for(std::set<std::string>::iterator
127                                 i = mod.unsatisfied_depends.begin();
128                                 i != mod.unsatisfied_depends.end(); i++){
129                         errs<<" \""<<(*i)<<"\"";
130                 }
131                 errs<<"."<<std::endl;
132                 mods_sorted.push_back(mod);
133         }
134         // If an error occurred, throw an exception
135         if(errs.str().size() != 0){
136                 throw ModError(errs.str());
137         }
138         // Print out some debug info
139         TRACESTREAM(<<"Detected mods in load order:"<<std::endl);
140         for(core::list<ModSpec>::Iterator i = mods_sorted.begin();
141                         i != mods_sorted.end(); i++){
142                 ModSpec &mod = *i;
143                 TRACESTREAM(<<"name=\""<<mod.name<<"\" path=\""<<mod.path<<"\"");
144                 TRACESTREAM(<<" depends=[");
145                 for(std::set<std::string>::iterator i = mod.depends.begin();
146                                 i != mod.depends.end(); i++)
147                         TRACESTREAM(<<" "<<(*i));
148                 TRACESTREAM(<<" ] unsatisfied_depends=[");
149                 for(std::set<std::string>::iterator i = mod.unsatisfied_depends.begin();
150                                 i != mod.unsatisfied_depends.end(); i++)
151                         TRACESTREAM(<<" "<<(*i));
152                 TRACESTREAM(<<" ]"<<std::endl);
153         }
154         return mods_sorted;
155 }
156
157