]> git.lizzy.rs Git - minetest-m13.git/blob - src/mods.cpp
3ecf107e530e3437977ea425200208a143ea50a1
[minetest-m13.git] / src / mods.cpp
1 /*
2 Minetest-m13
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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 // Get a dependency-sorted list of ModSpecs
30 core::list<ModSpec> getMods(core::list<std::string> &modspaths)
31                 throw(ModError)
32 {
33         std::queue<ModSpec> mods_satisfied;
34         core::list<ModSpec> mods_unsorted;
35         core::list<ModSpec> mods_sorted;
36         // name, path: For detecting name conflicts
37         std::map<std::string, std::string> mod_names;
38         for(core::list<std::string>::Iterator i = modspaths.begin();
39                         i != modspaths.end(); i++){
40                 std::string modspath = *i;
41                 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(modspath);
42                 for(u32 j=0; j<dirlist.size(); j++){
43                         if(!dirlist[j].dir)
44                                 continue;
45                         std::string modname = dirlist[j].name;
46                         std::string modpath = modspath + DIR_DELIM + modname;
47                         // Detect mod name conflicts
48                         {
49                                 std::map<std::string, std::string>::const_iterator i;
50                                 i = mod_names.find(modname);
51                                 if(i != mod_names.end()){
52                                         std::string s;
53                                         infostream<<"WARNING: Mod name conflict detected: "
54                                                         <<std::endl
55                                                         <<"Already loaded: "<<i->second<<std::endl
56                                                         <<"Will not load: "<<modpath<<std::endl;
57                                         continue;
58                                 }
59                         }
60                         std::set<std::string> depends;
61                         std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
62                                         std::ios_base::binary);
63                         while(is.good()){
64                                 std::string dep;
65                                 std::getline(is, dep);
66                                 dep = trim(dep);
67                                 if(dep != "")
68                                         depends.insert(dep);
69                         }
70                         ModSpec spec(modname, modpath, depends);
71                         mods_unsorted.push_back(spec);
72                         if(depends.empty())
73                                 mods_satisfied.push(spec);
74                         mod_names[modname] = modpath;
75                 }
76         }
77         // Sort by depencencies
78         while(!mods_satisfied.empty()){
79                 ModSpec mod = mods_satisfied.front();
80                 mods_satisfied.pop();
81                 mods_sorted.push_back(mod);
82                 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
83                                 i != mods_unsorted.end(); i++){
84                         ModSpec &mod2 = *i;
85                         if(mod2.unsatisfied_depends.empty())
86                                 continue;
87                         mod2.unsatisfied_depends.erase(mod.name);
88                         if(!mod2.unsatisfied_depends.empty())
89                                 continue;
90                         mods_satisfied.push(mod2);
91                 }
92         }
93         std::ostringstream errs(std::ios::binary);
94         // Check unsatisfied dependencies
95         for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
96                         i != mods_unsorted.end(); i++){
97                 ModSpec &mod = *i;
98                 if(mod.unsatisfied_depends.empty())
99                         continue;
100                 errs<<"mod \""<<mod.name
101                                 <<"\" has unsatisfied dependencies:";
102                 for(std::set<std::string>::iterator
103                                 i = mod.unsatisfied_depends.begin();
104                                 i != mod.unsatisfied_depends.end(); i++){
105                         errs<<" \""<<(*i)<<"\"";
106                 }
107                 errs<<"."<<std::endl;
108                 mods_sorted.push_back(mod);
109         }
110         if(errs.str().size() != 0){
111                 throw ModError(errs.str());
112         }
113         return mods_sorted;
114 }
115
116