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)
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++){
45 std::string modname = dirlist[j].name;
46 std::string modpath = modspath + DIR_DELIM + modname;
47 // Detect mod name conflicts
49 std::map<std::string, std::string>::const_iterator i;
50 i = mod_names.find(modname);
51 if(i != mod_names.end()){
53 infostream<<"WARNING: Mod name conflict detected: "
55 <<"Already loaded: "<<i->second<<std::endl
56 <<"Will not load: "<<modpath<<std::endl;
60 std::set<std::string> depends;
61 std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
62 std::ios_base::binary);
65 std::getline(is, dep);
70 ModSpec spec(modname, modpath, depends);
71 mods_unsorted.push_back(spec);
73 mods_satisfied.push(spec);
74 mod_names[modname] = modpath;
77 // Sort by depencencies
78 while(!mods_satisfied.empty()){
79 ModSpec mod = mods_satisfied.front();
81 mods_sorted.push_back(mod);
82 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
83 i != mods_unsorted.end(); i++){
85 if(mod2.unsatisfied_depends.empty())
87 mod2.unsatisfied_depends.erase(mod.name);
88 if(!mod2.unsatisfied_depends.empty())
90 mods_satisfied.push(mod2);
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++){
98 if(mod.unsatisfied_depends.empty())
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)<<"\"";
107 errs<<"."<<std::endl;
108 mods_sorted.push_back(mod);
110 if(errs.str().size() != 0){
111 throw ModError(errs.str());