]> git.lizzy.rs Git - minetest.git/blob - src/mods.cpp
Merge support for anaglyph stereo
[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                 // Ignore all directories beginning with a ".", especially
43                 // VCS directories like ".git" or ".svn"
44                 if(modname[0] == '.')
45                         continue;
46                 std::string modpath = modspath + DIR_DELIM + modname;
47                 TRACESTREAM(<<indentation<<"collectMods: "<<modname<<" at \""<<modpath<<"\""<<std::endl);
48                 // Handle modpacks (defined by containing modpack.txt)
49                 {
50                         std::ifstream is((modpath+DIR_DELIM+"modpack.txt").c_str(),
51                                         std::ios_base::binary);
52                         if(is.good()){
53                                 // Is a modpack
54                                 is.close(); // We don't actually need the file
55                                 // Recurse into it
56                                 collectMods(modpath, mods_satisfied, mods_unsorted, mod_names, indentation);
57                                 continue;
58                         }
59                 }
60                 // Detect mod name conflicts
61                 {
62                         std::map<std::string, std::string>::const_iterator i;
63                         i = mod_names.find(modname);
64                         if(i != mod_names.end()){
65                                 std::string s;
66                                 infostream<<indentation<<"WARNING: Mod name conflict detected: "
67                                                 <<std::endl
68                                                 <<"Already loaded: "<<i->second<<std::endl
69                                                 <<"Will not load: "<<modpath<<std::endl;
70                                 continue;
71                         }
72                 }
73                 std::set<std::string> depends;
74                 std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
75                                 std::ios_base::binary);
76                 while(is.good()){
77                         std::string dep;
78                         std::getline(is, dep);
79                         dep = trim(dep);
80                         if(dep != "")
81                                 depends.insert(dep);
82                 }
83                 ModSpec spec(modname, modpath, depends);
84                 mods_unsorted.push_back(spec);
85                 if(depends.empty())
86                         mods_satisfied.push(spec);
87                 mod_names[modname] = modpath;
88         }
89 }
90
91 // Get a dependency-sorted list of ModSpecs
92 core::list<ModSpec> getMods(core::list<std::string> &modspaths)
93                 throw(ModError)
94 {
95         std::queue<ModSpec> mods_satisfied;
96         core::list<ModSpec> mods_unsorted;
97         core::list<ModSpec> mods_sorted;
98         // name, path: For detecting name conflicts
99         std::map<std::string, std::string> mod_names;
100         for(core::list<std::string>::Iterator i = modspaths.begin();
101                         i != modspaths.end(); i++){
102                 std::string modspath = *i;
103                 collectMods(modspath, mods_satisfied, mods_unsorted, mod_names, "");
104         }
105         // Sort by depencencies
106         while(!mods_satisfied.empty()){
107                 ModSpec mod = mods_satisfied.front();
108                 mods_satisfied.pop();
109                 mods_sorted.push_back(mod);
110                 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
111                                 i != mods_unsorted.end(); i++){
112                         ModSpec &mod2 = *i;
113                         if(mod2.unsatisfied_depends.empty())
114                                 continue;
115                         mod2.unsatisfied_depends.erase(mod.name);
116                         if(!mod2.unsatisfied_depends.empty())
117                                 continue;
118                         mods_satisfied.push(mod2);
119                 }
120         }
121         std::ostringstream errs(std::ios::binary);
122         // Check unsatisfied dependencies
123         for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
124                         i != mods_unsorted.end(); i++){
125                 ModSpec &mod = *i;
126                 if(mod.unsatisfied_depends.empty())
127                         continue;
128                 errs<<"mod \""<<mod.name
129                                 <<"\" has unsatisfied dependencies:";
130                 for(std::set<std::string>::iterator
131                                 i = mod.unsatisfied_depends.begin();
132                                 i != mod.unsatisfied_depends.end(); i++){
133                         errs<<" \""<<(*i)<<"\"";
134                 }
135                 errs<<"."<<std::endl;
136                 mods_sorted.push_back(mod);
137         }
138         // If an error occurred, throw an exception
139         if(errs.str().size() != 0){
140                 throw ModError(errs.str());
141         }
142         // Print out some debug info
143         TRACESTREAM(<<"Detected mods in load order:"<<std::endl);
144         for(core::list<ModSpec>::Iterator i = mods_sorted.begin();
145                         i != mods_sorted.end(); i++){
146                 ModSpec &mod = *i;
147                 TRACESTREAM(<<"name=\""<<mod.name<<"\" path=\""<<mod.path<<"\"");
148                 TRACESTREAM(<<" depends=[");
149                 for(std::set<std::string>::iterator i = mod.depends.begin();
150                                 i != mod.depends.end(); i++)
151                         TRACESTREAM(<<" "<<(*i));
152                 TRACESTREAM(<<" ] unsatisfied_depends=[");
153                 for(std::set<std::string>::iterator i = mod.unsatisfied_depends.begin();
154                                 i != mod.unsatisfied_depends.end(); i++)
155                         TRACESTREAM(<<" "<<(*i));
156                 TRACESTREAM(<<" ]"<<std::endl);
157         }
158         return mods_sorted;
159 }
160
161