3 Copyright (C) 2013 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 #include "convert_json.h"
31 static bool parseDependsLine(std::istream &is,
32 std::string &dep, std::set<char> &symbols)
34 std::getline(is, dep);
37 size_t pos = dep.size();
38 while(pos > 0 && !string_allowed(dep.substr(pos-1, 1), MODNAME_ALLOWED_CHARS)){
39 // last character is a symbol, not part of the modname
40 symbols.insert(dep[pos-1]);
43 dep = trim(dep.substr(0, pos));
47 void parseModContents(ModSpec &spec)
49 // NOTE: this function works in mutual recursion with getModsInPath
52 spec.optdepends.clear();
53 spec.is_modpack = false;
54 spec.modpack_content.clear();
56 // Handle modpacks (defined by containing modpack.txt)
57 std::ifstream modpack_is((spec.path+DIR_DELIM+"modpack.txt").c_str());
58 if(modpack_is.good()){ //a modpack, recursively get the mods in it
59 modpack_is.close(); // We don't actually need the file
60 spec.is_modpack = true;
61 spec.modpack_content = getModsInPath(spec.path, true);
63 // modpacks have no dependencies; they are defined and
64 // tracked separately for each mod in the modpack
66 else{ // not a modpack, parse the dependencies
67 std::ifstream is((spec.path+DIR_DELIM+"depends.txt").c_str());
70 std::set<char> symbols;
71 if(parseDependsLine(is, dep, symbols)){
72 if(symbols.count('?') != 0){
73 spec.optdepends.insert(dep);
76 spec.depends.insert(dep);
81 // FIXME: optdepends.txt is deprecated
82 // remove this code at some point in the future
83 std::ifstream is2((spec.path+DIR_DELIM+"optdepends.txt").c_str());
86 std::set<char> symbols;
87 if(parseDependsLine(is2, dep, symbols))
88 spec.optdepends.insert(dep);
93 std::map<std::string, ModSpec> getModsInPath(std::string path, bool part_of_modpack)
95 // NOTE: this function works in mutual recursion with parseModContents
97 std::map<std::string, ModSpec> result;
98 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
99 for(u32 j=0; j<dirlist.size(); j++){
102 std::string modname = dirlist[j].name;
103 // Ignore all directories beginning with a ".", especially
104 // VCS directories like ".git" or ".svn"
105 if(modname[0] == '.')
107 std::string modpath = path + DIR_DELIM + modname;
109 ModSpec spec(modname, modpath);
110 spec.part_of_modpack = part_of_modpack;
111 parseModContents(spec);
112 result.insert(std::make_pair(modname, spec));
117 std::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods)
119 std::map<std::string, ModSpec> result;
120 for(std::map<std::string,ModSpec>::iterator it = mods.begin();
121 it != mods.end(); ++it)
123 ModSpec mod = (*it).second;
126 std::map<std::string, ModSpec> content =
127 flattenModTree(mod.modpack_content);
128 result.insert(content.begin(),content.end());
129 result.insert(std::make_pair(mod.name,mod));
133 result.insert(std::make_pair(mod.name,mod));
139 std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
141 std::vector<ModSpec> result;
142 for(std::map<std::string,ModSpec>::iterator it = mods.begin();
143 it != mods.end(); ++it)
145 ModSpec mod = (*it).second;
148 std::vector<ModSpec> content = flattenMods(mod.modpack_content);
149 result.reserve(result.size() + content.size());
150 result.insert(result.end(),content.begin(),content.end());
155 result.push_back(mod);
161 ModConfiguration::ModConfiguration(std::string worldpath)
163 SubgameSpec gamespec = findWorldSubgame(worldpath);
165 // Add all game mods and all world mods
166 addModsInPath(gamespec.gamemods_path);
167 addModsInPath(worldpath + DIR_DELIM + "worldmods");
169 // check world.mt file for mods explicitely declared to be
170 // loaded or not by a load_mod_<modname> = ... line.
171 std::string worldmt = worldpath+DIR_DELIM+"world.mt";
172 Settings worldmt_settings;
173 worldmt_settings.readConfigFile(worldmt.c_str());
174 std::vector<std::string> names = worldmt_settings.getNames();
175 std::set<std::string> include_mod_names;
176 for(std::vector<std::string>::iterator it = names.begin();
177 it != names.end(); ++it)
179 std::string name = *it;
180 // for backwards compatibility: exclude only mods which are
181 // explicitely excluded. if mod is not mentioned at all, it is
182 // enabled. So by default, all installed mods are enabled.
183 if (name.compare(0,9,"load_mod_") == 0 &&
184 worldmt_settings.getBool(name))
186 include_mod_names.insert(name.substr(9));
190 // Collect all mods that are also in include_mod_names
191 std::vector<ModSpec> addon_mods;
192 for(std::set<std::string>::const_iterator it_path = gamespec.addon_mods_paths.begin();
193 it_path != gamespec.addon_mods_paths.end(); ++it_path)
195 std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(*it_path));
196 for(std::vector<ModSpec>::iterator it = addon_mods_in_path.begin();
197 it != addon_mods_in_path.end(); ++it)
200 if(include_mod_names.count(mod.name) != 0)
201 addon_mods.push_back(mod);
203 worldmt_settings.setBool("load_mod_" + mod.name, false);
206 worldmt_settings.updateConfigFile(worldmt.c_str());
210 // report on name conflicts
211 if(!m_name_conflicts.empty()){
212 std::string s = "Unresolved name conflicts for mods ";
213 for(std::set<std::string>::const_iterator it = m_name_conflicts.begin();
214 it != m_name_conflicts.end(); ++it)
216 if(it != m_name_conflicts.begin()) s += ", ";
217 s += std::string("\"") + (*it) + "\"";
223 // get the mods in order
224 resolveDependencies();
227 void ModConfiguration::addModsInPath(std::string path)
229 addMods(flattenMods(getModsInPath(path)));
232 void ModConfiguration::addMods(std::vector<ModSpec> new_mods)
234 // Maintain a map of all existing m_unsatisfied_mods.
235 // Keys are mod names and values are indices into m_unsatisfied_mods.
236 std::map<std::string, u32> existing_mods;
237 for(u32 i = 0; i < m_unsatisfied_mods.size(); ++i){
238 existing_mods[m_unsatisfied_mods[i].name] = i;
242 for(int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack){
244 // Add all the mods that come from modpacks
246 // Add all the mods that didn't come from modpacks
248 std::set<std::string> seen_this_iteration;
250 for(std::vector<ModSpec>::const_iterator it = new_mods.begin();
251 it != new_mods.end(); ++it){
252 const ModSpec &mod = *it;
253 if(mod.part_of_modpack != want_from_modpack)
255 if(existing_mods.count(mod.name) == 0){
256 // GOOD CASE: completely new mod.
257 m_unsatisfied_mods.push_back(mod);
258 existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
260 else if(seen_this_iteration.count(mod.name) == 0){
261 // BAD CASE: name conflict in different levels.
262 u32 oldindex = existing_mods[mod.name];
263 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
264 actionstream<<"WARNING: Mod name conflict detected: \""
265 <<mod.name<<"\""<<std::endl
266 <<"Will not load: "<<oldmod.path<<std::endl
267 <<"Overridden by: "<<mod.path<<std::endl;
268 m_unsatisfied_mods[oldindex] = mod;
270 // If there was a "VERY BAD CASE" name conflict
271 // in an earlier level, ignore it.
272 m_name_conflicts.erase(mod.name);
275 // VERY BAD CASE: name conflict in the same level.
276 u32 oldindex = existing_mods[mod.name];
277 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
278 errorstream<<"WARNING: Mod name conflict detected: \""
279 <<mod.name<<"\""<<std::endl
280 <<"Will not load: "<<oldmod.path<<std::endl
281 <<"Will not load: "<<mod.path<<std::endl;
282 m_unsatisfied_mods[oldindex] = mod;
283 m_name_conflicts.insert(mod.name);
285 seen_this_iteration.insert(mod.name);
290 void ModConfiguration::resolveDependencies()
292 // Step 1: Compile a list of the mod names we're working with
293 std::set<std::string> modnames;
294 for(std::vector<ModSpec>::iterator it = m_unsatisfied_mods.begin();
295 it != m_unsatisfied_mods.end(); ++it){
296 modnames.insert((*it).name);
299 // Step 2: get dependencies (including optional dependencies)
300 // of each mod, split mods into satisfied and unsatisfied
301 std::list<ModSpec> satisfied;
302 std::list<ModSpec> unsatisfied;
303 for(std::vector<ModSpec>::iterator it = m_unsatisfied_mods.begin();
304 it != m_unsatisfied_mods.end(); ++it){
306 mod.unsatisfied_depends = mod.depends;
307 // check which optional dependencies actually exist
308 for(std::set<std::string>::iterator it_optdep = mod.optdepends.begin();
309 it_optdep != mod.optdepends.end(); ++it_optdep){
310 std::string optdep = *it_optdep;
311 if(modnames.count(optdep) != 0)
312 mod.unsatisfied_depends.insert(optdep);
314 // if a mod has no depends it is initially satisfied
315 if(mod.unsatisfied_depends.empty())
316 satisfied.push_back(mod);
318 unsatisfied.push_back(mod);
321 // Step 3: mods without unmet dependencies can be appended to
323 while(!satisfied.empty()){
324 ModSpec mod = satisfied.back();
325 m_sorted_mods.push_back(mod);
326 satisfied.pop_back();
327 for(std::list<ModSpec>::iterator it = unsatisfied.begin();
328 it != unsatisfied.end(); ){
330 mod2.unsatisfied_depends.erase(mod.name);
331 if(mod2.unsatisfied_depends.empty()){
332 satisfied.push_back(mod2);
333 it = unsatisfied.erase(it);
341 // Step 4: write back list of unsatisfied mods
342 m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
346 Json::Value getModstoreUrl(std::string url)
348 struct curl_slist *chunk = NULL;
350 bool special_http_header = true;
353 special_http_header = g_settings->getBool("modstore_disable_special_http_header");
355 catch(SettingNotFoundException &e) {
358 if (special_http_header)
359 chunk = curl_slist_append(chunk, "Accept: application/vnd.minetest.mmdb-v1+json");
361 Json::Value retval = fetchJsonValue(url,chunk);
364 curl_slist_free_all(chunk);