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