mod_configuration.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. Minetest
  3. Copyright (C) 2013-22 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "mod_configuration.h"
  17. #include "log.h"
  18. #include "settings.h"
  19. #include "filesys.h"
  20. #include "gettext.h"
  21. #include "exceptions.h"
  22. std::string ModConfiguration::getUnsatisfiedModsError() const
  23. {
  24. std::ostringstream error;
  25. error << gettext("Some mods have unsatisfied dependencies:") << std::endl;
  26. for (const ModSpec &mod : m_unsatisfied_mods) {
  27. //~ Error when a mod is missing dependencies. Ex: "Mod Title is missing: mod1, mod2, mod3"
  28. error << " - " << fmtgettext("%s is missing:", mod.name.c_str());
  29. for (const std::string &unsatisfied_depend : mod.unsatisfied_depends)
  30. error << " " << unsatisfied_depend;
  31. error << "\n";
  32. }
  33. error << "\n"
  34. << gettext("Install and enable the required mods, or disable the mods causing errors.") << "\n"
  35. << gettext("Note: this may be caused by a dependency cycle, in which case try updating the mods.");
  36. return error.str();
  37. }
  38. void ModConfiguration::addModsInPath(const std::string &path, const std::string &virtual_path)
  39. {
  40. addMods(flattenMods(getModsInPath(path, virtual_path)));
  41. }
  42. void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
  43. {
  44. // Maintain a map of all existing m_unsatisfied_mods.
  45. // Keys are mod names and values are indices into m_unsatisfied_mods.
  46. std::map<std::string, u32> existing_mods;
  47. for (u32 i = 0; i < m_unsatisfied_mods.size(); ++i) {
  48. existing_mods[m_unsatisfied_mods[i].name] = i;
  49. }
  50. // Add new mods
  51. for (int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack) {
  52. // First iteration:
  53. // Add all the mods that come from modpacks
  54. // Second iteration:
  55. // Add all the mods that didn't come from modpacks
  56. std::set<std::string> seen_this_iteration;
  57. for (const ModSpec &mod : new_mods) {
  58. if (mod.part_of_modpack != (bool)want_from_modpack)
  59. continue;
  60. if (existing_mods.count(mod.name) == 0) {
  61. // GOOD CASE: completely new mod.
  62. m_unsatisfied_mods.push_back(mod);
  63. existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
  64. } else if (seen_this_iteration.count(mod.name) == 0) {
  65. // BAD CASE: name conflict in different levels.
  66. u32 oldindex = existing_mods[mod.name];
  67. const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
  68. warningstream << "Mod name conflict detected: \""
  69. << mod.name << "\"" << std::endl
  70. << "Will not load: " << oldmod.path
  71. << std::endl
  72. << "Overridden by: " << mod.path
  73. << std::endl;
  74. m_unsatisfied_mods[oldindex] = mod;
  75. // If there was a "VERY BAD CASE" name conflict
  76. // in an earlier level, ignore it.
  77. m_name_conflicts.erase(mod.name);
  78. } else {
  79. // VERY BAD CASE: name conflict in the same level.
  80. u32 oldindex = existing_mods[mod.name];
  81. const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
  82. warningstream << "Mod name conflict detected: \""
  83. << mod.name << "\"" << std::endl
  84. << "Will not load: " << oldmod.path
  85. << std::endl
  86. << "Will not load: " << mod.path
  87. << std::endl;
  88. m_unsatisfied_mods[oldindex] = mod;
  89. m_name_conflicts.insert(mod.name);
  90. }
  91. seen_this_iteration.insert(mod.name);
  92. }
  93. }
  94. }
  95. void ModConfiguration::addGameMods(const SubgameSpec &gamespec)
  96. {
  97. std::string game_virtual_path;
  98. game_virtual_path.append("games/").append(gamespec.id).append("/mods");
  99. addModsInPath(gamespec.gamemods_path, game_virtual_path);
  100. }
  101. void ModConfiguration::addModsFromConfig(
  102. const std::string &settings_path,
  103. const std::unordered_map<std::string, std::string> &modPaths)
  104. {
  105. Settings conf;
  106. std::unordered_map<std::string, std::string> load_mod_names;
  107. conf.readConfigFile(settings_path.c_str());
  108. std::vector<std::string> names = conf.getNames();
  109. for (const std::string &name : names) {
  110. const auto &value = conf.get(name);
  111. if (name.compare(0, 9, "load_mod_") == 0 && value != "false" &&
  112. value != "nil")
  113. load_mod_names[name.substr(9)] = value;
  114. }
  115. // List of enabled non-game non-world mods
  116. std::vector<ModSpec> addon_mods;
  117. // Map of modname to a list candidate mod paths. Used to list
  118. // alternatives if a particular mod cannot be found.
  119. std::unordered_map<std::string, std::vector<std::string>> candidates;
  120. /*
  121. * Iterate through all installed mods except game mods and world mods
  122. *
  123. * If the mod is enabled, add it to `addon_mods`. *
  124. *
  125. * Alternative candidates for a modname are stored in `candidates`,
  126. * and used in an error message later.
  127. *
  128. * If not enabled, add `load_mod_modname = false` to world.mt
  129. */
  130. for (const auto &modPath : modPaths) {
  131. std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(modPath.second, modPath.first));
  132. for (const auto &mod : addon_mods_in_path) {
  133. const auto &pair = load_mod_names.find(mod.name);
  134. if (pair != load_mod_names.end()) {
  135. if (is_yes(pair->second) || pair->second == mod.virtual_path) {
  136. addon_mods.push_back(mod);
  137. } else {
  138. candidates[pair->first].emplace_back(mod.virtual_path);
  139. }
  140. } else {
  141. conf.setBool("load_mod_" + mod.name, false);
  142. }
  143. }
  144. }
  145. conf.updateConfigFile(settings_path.c_str());
  146. addMods(addon_mods);
  147. // Remove all loaded mods from `load_mod_names`
  148. // NB: as deps have not yet been resolved, `m_unsatisfied_mods` will contain all mods.
  149. for (const ModSpec &mod : m_unsatisfied_mods)
  150. load_mod_names.erase(mod.name);
  151. // Complain about mods declared to be loaded, but not found
  152. if (!load_mod_names.empty()) {
  153. errorstream << "The following mods could not be found:";
  154. for (const auto &pair : load_mod_names)
  155. errorstream << " \"" << pair.first << "\"";
  156. errorstream << std::endl;
  157. for (const auto &pair : load_mod_names) {
  158. const auto &candidate = candidates.find(pair.first);
  159. if (candidate != candidates.end()) {
  160. errorstream << "Unable to load " << pair.first << " as the specified path "
  161. << pair.second << " could not be found. "
  162. << "However, it is available in the following locations:"
  163. << std::endl;
  164. for (const auto &path : candidate->second) {
  165. errorstream << " - " << path << std::endl;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. void ModConfiguration::checkConflictsAndDeps()
  172. {
  173. // report on name conflicts
  174. if (!m_name_conflicts.empty()) {
  175. std::string s = "Unresolved name conflicts for mods ";
  176. bool add_comma = false;
  177. for (const auto& it : m_name_conflicts) {
  178. if (add_comma)
  179. s.append(", ");
  180. s.append("\"").append(it).append("\"");
  181. add_comma = true;
  182. }
  183. s.append(".");
  184. throw ModError(s);
  185. }
  186. // get the mods in order
  187. resolveDependencies();
  188. }
  189. void ModConfiguration::resolveDependencies()
  190. {
  191. // Step 1: Compile a list of the mod names we're working with
  192. std::set<std::string> modnames;
  193. for (const ModSpec &mod : m_unsatisfied_mods) {
  194. modnames.insert(mod.name);
  195. }
  196. // Step 2: get dependencies (including optional dependencies)
  197. // of each mod, split mods into satisfied and unsatisfied
  198. std::vector<ModSpec> satisfied;
  199. std::list<ModSpec> unsatisfied;
  200. for (ModSpec mod : m_unsatisfied_mods) {
  201. mod.unsatisfied_depends = mod.depends;
  202. // check which optional dependencies actually exist
  203. for (const std::string &optdep : mod.optdepends) {
  204. if (modnames.count(optdep) != 0)
  205. mod.unsatisfied_depends.insert(optdep);
  206. }
  207. // if a mod has no depends it is initially satisfied
  208. if (mod.unsatisfied_depends.empty())
  209. satisfied.push_back(mod);
  210. else
  211. unsatisfied.push_back(mod);
  212. }
  213. // Step 3: mods without unmet dependencies can be appended to
  214. // the sorted list.
  215. while (!satisfied.empty()) {
  216. ModSpec mod = satisfied.back();
  217. m_sorted_mods.push_back(mod);
  218. satisfied.pop_back();
  219. for (auto it = unsatisfied.begin(); it != unsatisfied.end();) {
  220. ModSpec &mod2 = *it;
  221. mod2.unsatisfied_depends.erase(mod.name);
  222. if (mod2.unsatisfied_depends.empty()) {
  223. satisfied.push_back(mod2);
  224. it = unsatisfied.erase(it);
  225. } else {
  226. ++it;
  227. }
  228. }
  229. }
  230. // Step 4: write back list of unsatisfied mods
  231. m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
  232. }