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.
22 #include <json/json.h>
24 #include "content/mods.h"
27 #include "content/subgames.h"
30 #include "convert_json.h"
32 bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
36 size_t pos = dep.size();
38 !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");
56 if (info.exists("author"))
57 spec.author = info.get("author");
59 if (info.exists("release"))
60 spec.release = info.getS32("release");
63 spec.optdepends.clear();
64 spec.is_modpack = false;
65 spec.modpack_content.clear();
67 // Handle modpacks (defined by containing modpack.txt)
68 std::ifstream modpack_is((spec.path + DIR_DELIM + "modpack.txt").c_str());
69 std::ifstream modpack2_is((spec.path + DIR_DELIM + "modpack.conf").c_str());
70 if (modpack_is.good() || modpack2_is.good()) {
71 if (modpack_is.good())
74 if (modpack2_is.good())
77 spec.is_modpack = true;
78 spec.modpack_content = getModsInPath(spec.path, true);
81 // Attempt to load dependencies from mod.conf
82 bool mod_conf_has_depends = false;
83 if (info.exists("depends")) {
84 mod_conf_has_depends = true;
85 std::string dep = info.get("depends");
87 dep.erase(std::remove_if(dep.begin(), dep.end(),
88 static_cast<int (*)(int)>(&std::isspace)), dep.end());
90 for (const auto &dependency : str_split(dep, ',')) {
91 spec.depends.insert(dependency);
95 if (info.exists("optional_depends")) {
96 mod_conf_has_depends = true;
97 std::string dep = info.get("optional_depends");
99 dep.erase(std::remove_if(dep.begin(), dep.end(),
100 static_cast<int (*)(int)>(&std::isspace)), dep.end());
102 for (const auto &dependency : str_split(dep, ',')) {
103 spec.optdepends.insert(dependency);
107 // Fallback to depends.txt
108 if (!mod_conf_has_depends) {
109 std::vector<std::string> dependencies;
111 std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
114 std::getline(is, dep);
115 dependencies.push_back(dep);
118 for (auto &dependency : dependencies) {
119 std::unordered_set<char> symbols;
120 if (parseDependsString(dependency, symbols)) {
121 if (symbols.count('?') != 0) {
122 spec.optdepends.insert(dependency);
124 spec.depends.insert(dependency);
130 if (info.exists("description")) {
131 spec.desc = info.get("description");
133 std::ifstream is((spec.path + DIR_DELIM + "description.txt")
135 spec.desc = std::string((std::istreambuf_iterator<char>(is)),
136 std::istreambuf_iterator<char>());
141 std::map<std::string, ModSpec> getModsInPath(
142 const std::string &path, bool part_of_modpack)
144 // NOTE: this function works in mutual recursion with parseModContents
146 std::map<std::string, ModSpec> result;
147 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
150 for (const fs::DirListNode &dln : dirlist) {
154 const std::string &modname = dln.name;
155 // Ignore all directories beginning with a ".", especially
156 // VCS directories like ".git" or ".svn"
157 if (modname[0] == '.')
161 modpath.append(path).append(DIR_DELIM).append(modname);
163 ModSpec spec(modname, modpath, part_of_modpack);
164 parseModContents(spec);
165 result.insert(std::make_pair(modname, spec));
170 std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
172 std::vector<ModSpec> result;
173 for (const auto &it : mods) {
174 const ModSpec &mod = it.second;
175 if (mod.is_modpack) {
176 std::vector<ModSpec> content = flattenMods(mod.modpack_content);
177 result.reserve(result.size() + content.size());
178 result.insert(result.end(), content.begin(), content.end());
180 } else // not a modpack
182 result.push_back(mod);
188 ModConfiguration::ModConfiguration(const std::string &worldpath)
192 void ModConfiguration::printUnsatisfiedModsError() const
194 for (const ModSpec &mod : m_unsatisfied_mods) {
195 errorstream << "mod \"" << mod.name
196 << "\" has unsatisfied dependencies: ";
197 for (const std::string &unsatisfied_depend : mod.unsatisfied_depends)
198 errorstream << " \"" << unsatisfied_depend << "\"";
199 errorstream << std::endl;
203 void ModConfiguration::addModsInPath(const std::string &path)
205 addMods(flattenMods(getModsInPath(path)));
208 void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
210 // Maintain a map of all existing m_unsatisfied_mods.
211 // Keys are mod names and values are indices into m_unsatisfied_mods.
212 std::map<std::string, u32> existing_mods;
213 for (u32 i = 0; i < m_unsatisfied_mods.size(); ++i) {
214 existing_mods[m_unsatisfied_mods[i].name] = i;
218 for (int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack) {
220 // Add all the mods that come from modpacks
222 // Add all the mods that didn't come from modpacks
224 std::set<std::string> seen_this_iteration;
226 for (const ModSpec &mod : new_mods) {
227 if (mod.part_of_modpack != (bool)want_from_modpack)
230 if (existing_mods.count(mod.name) == 0) {
231 // GOOD CASE: completely new mod.
232 m_unsatisfied_mods.push_back(mod);
233 existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
234 } else if (seen_this_iteration.count(mod.name) == 0) {
235 // BAD CASE: name conflict in different levels.
236 u32 oldindex = existing_mods[mod.name];
237 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
238 warningstream << "Mod name conflict detected: \""
239 << mod.name << "\"" << std::endl
240 << "Will not load: " << oldmod.path
242 << "Overridden by: " << mod.path
244 m_unsatisfied_mods[oldindex] = mod;
246 // If there was a "VERY BAD CASE" name conflict
247 // in an earlier level, ignore it.
248 m_name_conflicts.erase(mod.name);
250 // VERY BAD CASE: name conflict in the same level.
251 u32 oldindex = existing_mods[mod.name];
252 const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
253 warningstream << "Mod name conflict detected: \""
254 << mod.name << "\"" << std::endl
255 << "Will not load: " << oldmod.path
257 << "Will not load: " << mod.path
259 m_unsatisfied_mods[oldindex] = mod;
260 m_name_conflicts.insert(mod.name);
263 seen_this_iteration.insert(mod.name);
268 void ModConfiguration::addModsFromConfig(
269 const std::string &settings_path, const std::set<std::string> &mods)
272 std::set<std::string> load_mod_names;
274 conf.readConfigFile(settings_path.c_str());
275 std::vector<std::string> names = conf.getNames();
276 for (const std::string &name : names) {
277 if (name.compare(0, 9, "load_mod_") == 0 && conf.get(name) != "false" &&
278 conf.get(name) != "nil")
279 load_mod_names.insert(name.substr(9));
282 std::vector<ModSpec> addon_mods;
283 for (const std::string &i : mods) {
284 std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(i));
285 for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
286 it != addon_mods_in_path.end(); ++it) {
287 const ModSpec &mod = *it;
288 if (load_mod_names.count(mod.name) != 0)
289 addon_mods.push_back(mod);
291 conf.setBool("load_mod_" + mod.name, false);
294 conf.updateConfigFile(settings_path.c_str());
297 checkConflictsAndDeps();
299 // complain about mods declared to be loaded, but not found
300 for (const ModSpec &addon_mod : addon_mods)
301 load_mod_names.erase(addon_mod.name);
303 std::vector<ModSpec> unsatisfiedMods = getUnsatisfiedMods();
305 for (const ModSpec &unsatisfiedMod : unsatisfiedMods)
306 load_mod_names.erase(unsatisfiedMod.name);
308 if (!load_mod_names.empty()) {
309 errorstream << "The following mods could not be found:";
310 for (const std::string &mod : load_mod_names)
311 errorstream << " \"" << mod << "\"";
312 errorstream << std::endl;
316 void ModConfiguration::checkConflictsAndDeps()
318 // report on name conflicts
319 if (!m_name_conflicts.empty()) {
320 std::string s = "Unresolved name conflicts for mods ";
321 for (std::unordered_set<std::string>::const_iterator it =
322 m_name_conflicts.begin();
323 it != m_name_conflicts.end(); ++it) {
324 if (it != m_name_conflicts.begin())
326 s += std::string("\"") + (*it) + "\"";
332 // get the mods in order
333 resolveDependencies();
336 void ModConfiguration::resolveDependencies()
338 // Step 1: Compile a list of the mod names we're working with
339 std::set<std::string> modnames;
340 for (const ModSpec &mod : m_unsatisfied_mods) {
341 modnames.insert(mod.name);
344 // Step 2: get dependencies (including optional dependencies)
345 // of each mod, split mods into satisfied and unsatisfied
346 std::list<ModSpec> satisfied;
347 std::list<ModSpec> unsatisfied;
348 for (ModSpec mod : m_unsatisfied_mods) {
349 mod.unsatisfied_depends = mod.depends;
350 // check which optional dependencies actually exist
351 for (const std::string &optdep : mod.optdepends) {
352 if (modnames.count(optdep) != 0)
353 mod.unsatisfied_depends.insert(optdep);
355 // if a mod has no depends it is initially satisfied
356 if (mod.unsatisfied_depends.empty())
357 satisfied.push_back(mod);
359 unsatisfied.push_back(mod);
362 // Step 3: mods without unmet dependencies can be appended to
364 while (!satisfied.empty()) {
365 ModSpec mod = satisfied.back();
366 m_sorted_mods.push_back(mod);
367 satisfied.pop_back();
368 for (auto it = unsatisfied.begin(); it != unsatisfied.end();) {
370 mod2.unsatisfied_depends.erase(mod.name);
371 if (mod2.unsatisfied_depends.empty()) {
372 satisfied.push_back(mod2);
373 it = unsatisfied.erase(it);
380 // Step 4: write back list of unsatisfied mods
381 m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
385 ClientModConfiguration::ClientModConfiguration(const std::string &path) :
386 ModConfiguration(path)
388 std::set<std::string> paths;
389 std::string path_user = porting::path_user + DIR_DELIM + "clientmods";
391 paths.insert(path_user);
393 std::string settings_path = path_user + DIR_DELIM + "mods.conf";
394 addModsFromConfig(settings_path, paths);
398 ModMetadata::ModMetadata(const std::string &mod_name) : m_mod_name(mod_name)
402 void ModMetadata::clear()
408 bool ModMetadata::save(const std::string &root_path)
411 for (StringMap::const_iterator it = m_stringvars.begin();
412 it != m_stringvars.end(); ++it) {
413 json[it->first] = it->second;
416 if (!fs::PathExists(root_path)) {
417 if (!fs::CreateAllDirs(root_path)) {
418 errorstream << "ModMetadata[" << m_mod_name
419 << "]: Unable to save. '" << root_path
420 << "' tree cannot be created." << std::endl;
423 } else if (!fs::IsDir(root_path)) {
424 errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
425 << root_path << "' is not a directory." << std::endl;
429 bool w_ok = fs::safeWriteToFile(
430 root_path + DIR_DELIM + m_mod_name, fastWriteJson(json));
435 errorstream << "ModMetadata[" << m_mod_name << "]: failed write file."
441 bool ModMetadata::load(const std::string &root_path)
443 m_stringvars.clear();
445 std::ifstream is((root_path + DIR_DELIM + m_mod_name).c_str(),
446 std::ios_base::binary);
452 Json::CharReaderBuilder builder;
453 builder.settings_["collectComments"] = false;
456 if (!Json::parseFromStream(builder, is, &root, &errs)) {
457 errorstream << "ModMetadata[" << m_mod_name
458 << "]: failed read data "
459 "(Json decoding failure). Message: "
460 << errs << std::endl;
464 const Json::Value::Members attr_list = root.getMemberNames();
465 for (const auto &it : attr_list) {
466 Json::Value attr_value = root[it];
467 m_stringvars[it] = attr_value.asString();
473 bool ModMetadata::setString(const std::string &name, const std::string &var)
475 m_modified = Metadata::setString(name, var);