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.
27 #include "convert_json.h"
28 #include "exceptions.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
51 info.readConfigFile((spec.path+DIR_DELIM+"mod.conf").c_str());
53 if (info.exists("name"))
54 spec.name = info.get("name");
57 spec.optdepends.clear();
58 spec.is_modpack = false;
59 spec.modpack_content.clear();
61 // Handle modpacks (defined by containing modpack.txt)
62 std::ifstream modpack_is((spec.path+DIR_DELIM+"modpack.txt").c_str());
63 if(modpack_is.good()){ //a modpack, recursively get the mods in it
64 modpack_is.close(); // We don't actually need the file
65 spec.is_modpack = true;
66 spec.modpack_content = getModsInPath(spec.path, true);
68 // modpacks have no dependencies; they are defined and
69 // tracked separately for each mod in the modpack
71 else{ // not a modpack, parse the dependencies
72 std::ifstream is((spec.path+DIR_DELIM+"depends.txt").c_str());
75 std::set<char> symbols;
76 if(parseDependsLine(is, dep, symbols)){
77 if(symbols.count('?') != 0){
78 spec.optdepends.insert(dep);
81 spec.depends.insert(dep);
88 std::map<std::string, ModSpec> getModsInPath(std::string path, bool part_of_modpack)
90 // NOTE: this function works in mutual recursion with parseModContents
92 std::map<std::string, ModSpec> result;
93 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
94 for(u32 j=0; j<dirlist.size(); j++){
97 std::string modname = dirlist[j].name;
98 // Ignore all directories beginning with a ".", especially
99 // VCS directories like ".git" or ".svn"
100 if(modname[0] == '.')
102 std::string modpath = path + DIR_DELIM + modname;
104 ModSpec spec(modname, modpath);
105 spec.part_of_modpack = part_of_modpack;
106 parseModContents(spec);
107 result.insert(std::make_pair(modname, spec));
112 std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
114 std::vector<ModSpec> result;
115 for(std::map<std::string,ModSpec>::iterator it = mods.begin();
116 it != mods.end(); ++it)
118 ModSpec mod = (*it).second;
121 std::vector<ModSpec> content = flattenMods(mod.modpack_content);
122 result.reserve(result.size() + content.size());
123 result.insert(result.end(),content.begin(),content.end());
128 result.push_back(mod);
134 ModConfiguration::ModConfiguration(const std::string &worldpath):
135 m_unsatisfied_mods(),
141 void ModConfiguration::printUnsatisfiedModsError() const
143 for (std::vector<ModSpec>::const_iterator it = m_unsatisfied_mods.begin();
144 it != m_unsatisfied_mods.end(); ++it) {
146 errorstream << "mod \"" << mod.name << "\" has unsatisfied dependencies: ";
147 for (std::unordered_set<std::string>::iterator dep_it =
148 mod.unsatisfied_depends.begin();
149 dep_it != mod.unsatisfied_depends.end(); ++dep_it)
150 errorstream << " \"" << *dep_it << "\"";
151 errorstream << std::endl;
155 void ModConfiguration::addModsInPath(const std::string &path)
157 addMods(flattenMods(getModsInPath(path)));
160 void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
162 // Maintain a map of all existing m_unsatisfied_mods.
163 // Keys are mod names and values are indices into m_unsatisfied_mods.
164 std::map<std::string, u32> existing_mods;
165 for(u32 i = 0; i < m_unsatisfied_mods.size(); ++i){
166 existing_mods[m_unsatisfied_mods[i].name] = i;
170 for(int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack){
172 // Add all the mods that come from modpacks
174 // Add all the mods that didn't come from modpacks
176 std::set<std::string> seen_this_iteration;
178 for (std::vector<ModSpec>::const_iterator it = new_mods.begin();
179 it != new_mods.end(); ++it) {
180 const ModSpec &mod = *it;
181 if(mod.part_of_modpack != (bool)want_from_modpack)
183 if(existing_mods.count(mod.name) == 0){
184 // GOOD CASE: completely new mod.
185 m_unsatisfied_mods.push_back(mod);
186 existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
188 else if(seen_this_iteration.count(mod.name) == 0){
189 // BAD CASE: name conflict in different levels.
190 u32 oldindex = existing_mods[mod.name];
191 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
192 warningstream<<"Mod name conflict detected: \""
193 <<mod.name<<"\""<<std::endl
194 <<"Will not load: "<<oldmod.path<<std::endl
195 <<"Overridden by: "<<mod.path<<std::endl;
196 m_unsatisfied_mods[oldindex] = mod;
198 // If there was a "VERY BAD CASE" name conflict
199 // in an earlier level, ignore it.
200 m_name_conflicts.erase(mod.name);
203 // VERY BAD CASE: name conflict in the same level.
204 u32 oldindex = existing_mods[mod.name];
205 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
206 warningstream<<"Mod name conflict detected: \""
207 <<mod.name<<"\""<<std::endl
208 <<"Will not load: "<<oldmod.path<<std::endl
209 <<"Will not load: "<<mod.path<<std::endl;
210 m_unsatisfied_mods[oldindex] = mod;
211 m_name_conflicts.insert(mod.name);
213 seen_this_iteration.insert(mod.name);
218 void ModConfiguration::addModsFormConfig(const std::string &settings_path, const std::set<std::string> &mods)
221 std::set<std::string> load_mod_names;
223 conf.readConfigFile(settings_path.c_str());
224 std::vector<std::string> names = conf.getNames();
225 for (std::vector<std::string>::iterator it = names.begin();
226 it != names.end(); ++it) {
227 std::string name = *it;
228 if (name.compare(0,9,"load_mod_")==0 && conf.getBool(name))
229 load_mod_names.insert(name.substr(9));
232 std::vector<ModSpec> addon_mods;
233 for (std::set<std::string>::const_iterator i = mods.begin();
234 i != mods.end(); ++i) {
235 std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(*i));
236 for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
237 it != addon_mods_in_path.end(); ++it) {
238 const ModSpec& mod = *it;
239 if (load_mod_names.count(mod.name) != 0)
240 addon_mods.push_back(mod);
242 conf.setBool("load_mod_" + mod.name, false);
245 conf.updateConfigFile(settings_path.c_str());
248 checkConflictsAndDeps();
250 // complain about mods declared to be loaded, but not found
251 for (std::vector<ModSpec>::iterator it = addon_mods.begin();
252 it != addon_mods.end(); ++it)
253 load_mod_names.erase((*it).name);
254 std::vector<ModSpec> UnsatisfiedMods = getUnsatisfiedMods();
255 for (std::vector<ModSpec>::iterator it = UnsatisfiedMods.begin();
256 it != UnsatisfiedMods.end(); ++it)
257 load_mod_names.erase((*it).name);
258 if (!load_mod_names.empty()) {
259 errorstream << "The following mods could not be found:";
260 for (std::set<std::string>::iterator it = load_mod_names.begin();
261 it != load_mod_names.end(); ++it)
262 errorstream << " \"" << (*it) << "\"";
263 errorstream << std::endl;
267 void ModConfiguration::checkConflictsAndDeps()
269 // report on name conflicts
270 if (!m_name_conflicts.empty()) {
271 std::string s = "Unresolved name conflicts for mods ";
272 for (std::unordered_set<std::string>::const_iterator it =
273 m_name_conflicts.begin(); it != m_name_conflicts.end(); ++it) {
274 if (it != m_name_conflicts.begin()) s += ", ";
275 s += std::string("\"") + (*it) + "\"";
281 // get the mods in order
282 resolveDependencies();
285 void ModConfiguration::resolveDependencies()
287 // Step 1: Compile a list of the mod names we're working with
288 std::set<std::string> modnames;
289 for(std::vector<ModSpec>::iterator it = m_unsatisfied_mods.begin();
290 it != m_unsatisfied_mods.end(); ++it){
291 modnames.insert((*it).name);
294 // Step 2: get dependencies (including optional dependencies)
295 // of each mod, split mods into satisfied and unsatisfied
296 std::list<ModSpec> satisfied;
297 std::list<ModSpec> unsatisfied;
298 for (std::vector<ModSpec>::iterator it = m_unsatisfied_mods.begin();
299 it != m_unsatisfied_mods.end(); ++it) {
301 mod.unsatisfied_depends = mod.depends;
302 // check which optional dependencies actually exist
303 for (std::unordered_set<std::string>::iterator it_optdep = mod.optdepends.begin();
304 it_optdep != mod.optdepends.end(); ++it_optdep) {
305 std::string optdep = *it_optdep;
306 if (modnames.count(optdep) != 0)
307 mod.unsatisfied_depends.insert(optdep);
309 // if a mod has no depends it is initially satisfied
310 if (mod.unsatisfied_depends.empty())
311 satisfied.push_back(mod);
313 unsatisfied.push_back(mod);
316 // Step 3: mods without unmet dependencies can be appended to
318 while(!satisfied.empty()){
319 ModSpec mod = satisfied.back();
320 m_sorted_mods.push_back(mod);
321 satisfied.pop_back();
322 for(std::list<ModSpec>::iterator it = unsatisfied.begin();
323 it != unsatisfied.end(); ){
325 mod2.unsatisfied_depends.erase(mod.name);
326 if(mod2.unsatisfied_depends.empty()){
327 satisfied.push_back(mod2);
328 it = unsatisfied.erase(it);
336 // Step 4: write back list of unsatisfied mods
337 m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
340 ServerModConfiguration::ServerModConfiguration(const std::string &worldpath):
341 ModConfiguration(worldpath)
343 SubgameSpec gamespec = findWorldSubgame(worldpath);
345 // Add all game mods and all world mods
346 addModsInPath(gamespec.gamemods_path);
347 addModsInPath(worldpath + DIR_DELIM + "worldmods");
350 std::string worldmt = worldpath + DIR_DELIM + "world.mt";
351 addModsFormConfig(worldmt, gamespec.addon_mods_paths);
355 ClientModConfiguration::ClientModConfiguration(const std::string &path):
356 ModConfiguration(path)
358 std::set<std::string> paths;
359 std::string path_user = porting::path_user + DIR_DELIM + "clientmods";
361 paths.insert(path_user);
363 std::string settings_path = path_user + DIR_DELIM + "mods.conf";
364 addModsFormConfig(settings_path, paths);
368 ModMetadata::ModMetadata(const std::string &mod_name):
373 void ModMetadata::clear()
379 bool ModMetadata::save(const std::string &root_path)
382 for (StringMap::const_iterator it = m_stringvars.begin();
383 it != m_stringvars.end(); ++it) {
384 json[it->first] = it->second;
387 if (!fs::PathExists(root_path)) {
388 if (!fs::CreateAllDirs(root_path)) {
389 errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
390 << root_path << "' tree cannot be created." << std::endl;
393 } else if (!fs::IsDir(root_path)) {
394 errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
395 << root_path << "' is not a directory." << std::endl;
399 bool w_ok = fs::safeWriteToFile(root_path + DIR_DELIM + m_mod_name,
400 Json::FastWriter().write(json));
405 errorstream << "ModMetadata[" << m_mod_name << "]: failed write file." << std::endl;
410 bool ModMetadata::load(const std::string &root_path)
412 m_stringvars.clear();
414 std::ifstream is((root_path + DIR_DELIM + m_mod_name).c_str(), std::ios_base::binary);
421 if (!reader.parse(is, root)) {
422 errorstream << "ModMetadata[" << m_mod_name << "]: failed read data "
423 "(Json decoding failure)." << std::endl;
427 const Json::Value::Members attr_list = root.getMemberNames();
428 for (Json::Value::Members::const_iterator it = attr_list.begin();
429 it != attr_list.end(); ++it) {
430 Json::Value attr_value = root[*it];
431 m_stringvars[*it] = attr_value.asString();
437 bool ModMetadata::setString(const std::string &name, const std::string &var)
439 m_modified = Metadata::setString(name, var);