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 // Ignore all directories beginning with a ".", especially
43 // VCS directories like ".git" or ".svn"
46 std::string modpath = modspath + DIR_DELIM + modname;
47 TRACESTREAM(<<indentation<<"collectMods: "<<modname<<" at \""<<modpath<<"\""<<std::endl);
48 // Handle modpacks (defined by containing modpack.txt)
50 std::ifstream is((modpath+DIR_DELIM+"modpack.txt").c_str(),
51 std::ios_base::binary);
54 is.close(); // We don't actually need the file
56 collectMods(modpath, mods_satisfied, mods_unsorted, mod_names, indentation);
60 // Detect mod name conflicts
62 std::map<std::string, std::string>::const_iterator i;
63 i = mod_names.find(modname);
64 if(i != mod_names.end()){
66 infostream<<indentation<<"WARNING: Mod name conflict detected: "
68 <<"Already loaded: "<<i->second<<std::endl
69 <<"Will not load: "<<modpath<<std::endl;
73 std::set<std::string> depends;
74 std::ifstream is((modpath+DIR_DELIM+"depends.txt").c_str(),
75 std::ios_base::binary);
78 std::getline(is, dep);
83 ModSpec spec(modname, modpath, depends);
84 mods_unsorted.push_back(spec);
86 mods_satisfied.push(spec);
87 mod_names[modname] = modpath;
91 // Get a dependency-sorted list of ModSpecs
92 core::list<ModSpec> getMods(core::list<std::string> &modspaths)
95 std::queue<ModSpec> mods_satisfied;
96 core::list<ModSpec> mods_unsorted;
97 core::list<ModSpec> mods_sorted;
98 // name, path: For detecting name conflicts
99 std::map<std::string, std::string> mod_names;
100 for(core::list<std::string>::Iterator i = modspaths.begin();
101 i != modspaths.end(); i++){
102 std::string modspath = *i;
103 collectMods(modspath, mods_satisfied, mods_unsorted, mod_names, "");
105 // Sort by depencencies
106 while(!mods_satisfied.empty()){
107 ModSpec mod = mods_satisfied.front();
108 mods_satisfied.pop();
109 mods_sorted.push_back(mod);
110 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
111 i != mods_unsorted.end(); i++){
113 if(mod2.unsatisfied_depends.empty())
115 mod2.unsatisfied_depends.erase(mod.name);
116 if(!mod2.unsatisfied_depends.empty())
118 mods_satisfied.push(mod2);
121 std::ostringstream errs(std::ios::binary);
122 // Check unsatisfied dependencies
123 for(core::list<ModSpec>::Iterator i = mods_unsorted.begin();
124 i != mods_unsorted.end(); i++){
126 if(mod.unsatisfied_depends.empty())
128 errs<<"mod \""<<mod.name
129 <<"\" has unsatisfied dependencies:";
130 for(std::set<std::string>::iterator
131 i = mod.unsatisfied_depends.begin();
132 i != mod.unsatisfied_depends.end(); i++){
133 errs<<" \""<<(*i)<<"\"";
135 errs<<"."<<std::endl;
136 mods_sorted.push_back(mod);
138 // If an error occurred, throw an exception
139 if(errs.str().size() != 0){
140 throw ModError(errs.str());
142 // Print out some debug info
143 TRACESTREAM(<<"Detected mods in load order:"<<std::endl);
144 for(core::list<ModSpec>::Iterator i = mods_sorted.begin();
145 i != mods_sorted.end(); i++){
147 TRACESTREAM(<<"name=\""<<mod.name<<"\" path=\""<<mod.path<<"\"");
148 TRACESTREAM(<<" depends=[");
149 for(std::set<std::string>::iterator i = mod.depends.begin();
150 i != mod.depends.end(); i++)
151 TRACESTREAM(<<" "<<(*i));
152 TRACESTREAM(<<" ] unsatisfied_depends=[");
153 for(std::set<std::string>::iterator i = mod.unsatisfied_depends.begin();
154 i != mod.unsatisfied_depends.end(); i++)
155 TRACESTREAM(<<" "<<(*i));
156 TRACESTREAM(<<" ]"<<std::endl);