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 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.
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.
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.
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)
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++){
41 std::string modname = dirlist[j].name;
42 std::string modpath = modspath + DIR_DELIM + modname;
43 TRACESTREAM(<<indentation<<"collectMods: "<<modname<<" at \""<<modpath<<"\""<<std::endl);
44 // Handle modpacks (defined by containing modpack.txt)
46 std::ifstream is((modpath+DIR_DELIM+"modpack.txt").c_str(),
47 std::ios_base::binary);
50 is.close(); // We don't actually need the file
52 collectMods(modpath, mods_satisfied, mods_unsorted, mod_names, indentation);
56 // Detect mod name conflicts
58 std::map<std::string, std::string>::const_iterator i;
59 i = mod_names.find(modname);
60 if(i != mod_names.end()){
62 infostream<<indentation<<"WARNING: Mod name conflict detected: "
64 <<"Already loaded: "<<i->second<<std::endl
65 <<"Will not load: "<<modpath<<std::endl;
69 std::set<std::string> depends;
70 std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
71 std::ios_base::binary);
74 std::getline(is, dep);
79 ModSpec spec(modname, modpath, depends);
80 mods_unsorted.push_back(spec);
82 mods_satisfied.push(spec);
83 mod_names[modname] = modpath;
87 // Get a dependency-sorted list of ModSpecs
88 core::list<ModSpec> getMods(core::list<std::string> &modspaths)
91 std::queue<ModSpec> mods_satisfied;
92 core::list<ModSpec> mods_unsorted;
93 core::list<ModSpec> mods_sorted;
94 // name, path: For detecting name conflicts
95 std::map<std::string, std::string> mod_names;
96 for(core::list<std::string>::Iterator i = modspaths.begin();
97 i != modspaths.end(); i++){
98 std::string modspath = *i;
99 collectMods(modspath, mods_satisfied, mods_unsorted, mod_names, "");
101 // Sort by depencencies
102 while(!mods_satisfied.empty()){
103 ModSpec mod = mods_satisfied.front();
104 mods_satisfied.pop();
105 mods_sorted.push_back(mod);
106 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
107 i != mods_unsorted.end(); i++){
109 if(mod2.unsatisfied_depends.empty())
111 mod2.unsatisfied_depends.erase(mod.name);
112 if(!mod2.unsatisfied_depends.empty())
114 mods_satisfied.push(mod2);
117 std::ostringstream errs(std::ios::binary);
118 // Check unsatisfied dependencies
119 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
120 i != mods_unsorted.end(); i++){
122 if(mod.unsatisfied_depends.empty())
124 errs<<"mod \""<<mod.name
125 <<"\" has unsatisfied dependencies:";
126 for(std::set<std::string>::iterator
127 i = mod.unsatisfied_depends.begin();
128 i != mod.unsatisfied_depends.end(); i++){
129 errs<<" \""<<(*i)<<"\"";
131 errs<<"."<<std::endl;
132 mods_sorted.push_back(mod);
134 // If an error occurred, throw an exception
135 if(errs.str().size() != 0){
136 throw ModError(errs.str());
138 // Print out some debug info
139 TRACESTREAM(<<"Detected mods in load order:"<<std::endl);
140 for(core::list<ModSpec>::Iterator i = mods_sorted.begin();
141 i != mods_sorted.end(); i++){
143 TRACESTREAM(<<"name=\""<<mod.name<<"\" path=\""<<mod.path<<"\"");
144 TRACESTREAM(<<" depends=[");
145 for(std::set<std::string>::iterator i = mod.depends.begin();
146 i != mod.depends.end(); i++)
147 TRACESTREAM(<<" "<<(*i));
148 TRACESTREAM(<<" ] unsatisfied_depends=[");
149 for(std::set<std::string>::iterator i = mod.unsatisfied_depends.begin();
150 i != mod.unsatisfied_depends.end(); i++)
151 TRACESTREAM(<<" "<<(*i));
152 TRACESTREAM(<<" ]"<<std::endl);