3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
29 // Get a dependency-sorted list of ModSpecs
30 core::list<ModSpec> getMods(core::list<std::string> &modspaths)
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++){
44 std::string modname = dirlist[j].name;
45 std::string modpath = modspath + DIR_DELIM + modname;
46 // Detect mod name conflicts
48 std::map<std::string, std::string>::const_iterator i;
49 i = mod_names.find(modname);
50 if(i != mod_names.end()){
52 infostream<<"WARNING: Mod name conflict detected: "
54 <<"Already loaded: "<<i->second<<std::endl
55 <<"Will not load: "<<modpath<<std::endl;
59 std::set<std::string> depends;
60 std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
61 std::ios_base::binary);
64 std::getline(is, dep);
69 ModSpec spec(modname, modpath, depends);
70 mods_unsorted.push_back(spec);
72 mods_satisfied.push(spec);
73 mod_names[modname] = modpath;
76 // Sort by depencencies
77 while(!mods_satisfied.empty()){
78 ModSpec mod = mods_satisfied.front();
80 mods_sorted.push_back(mod);
81 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
82 i != mods_unsorted.end(); i++){
84 if(mod2.unsatisfied_depends.empty())
86 mod2.unsatisfied_depends.erase(mod.name);
87 if(!mod2.unsatisfied_depends.empty())
89 mods_satisfied.push(mod2);
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++){
97 if(mod.unsatisfied_depends.empty())
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)<<"\"";
106 errs<<"."<<std::endl;
107 mods_sorted.push_back(mod);
109 if(errs.str().size() != 0){
110 throw ModError(errs.str());