mods.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. Minetest
  3. Copyright (C) 2013 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 <cctype>
  17. #include <fstream>
  18. #include <json/json.h>
  19. #include <algorithm>
  20. #include "content/mods.h"
  21. #include "filesys.h"
  22. #include "log.h"
  23. #include "content/subgames.h"
  24. #include "settings.h"
  25. #include "porting.h"
  26. #include "convert_json.h"
  27. bool parseDependsString(std::string &dep, std::unordered_set<char> &symbols)
  28. {
  29. dep = trim(dep);
  30. symbols.clear();
  31. size_t pos = dep.size();
  32. while (pos > 0 &&
  33. !string_allowed(dep.substr(pos - 1, 1), MODNAME_ALLOWED_CHARS)) {
  34. // last character is a symbol, not part of the modname
  35. symbols.insert(dep[pos - 1]);
  36. --pos;
  37. }
  38. dep = trim(dep.substr(0, pos));
  39. return !dep.empty();
  40. }
  41. void parseModContents(ModSpec &spec)
  42. {
  43. // NOTE: this function works in mutual recursion with getModsInPath
  44. Settings info;
  45. info.readConfigFile((spec.path + DIR_DELIM + "mod.conf").c_str());
  46. if (info.exists("name"))
  47. spec.name = info.get("name");
  48. if (info.exists("author"))
  49. spec.author = info.get("author");
  50. if (info.exists("release"))
  51. spec.release = info.getS32("release");
  52. spec.depends.clear();
  53. spec.optdepends.clear();
  54. spec.is_modpack = false;
  55. 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);
  62. // modpacks have no dependencies; they are defined and
  63. // tracked separately for each mod in the modpack
  64. } else {
  65. // Attempt to load dependencies from mod.conf
  66. bool mod_conf_has_depends = false;
  67. if (info.exists("depends")) {
  68. mod_conf_has_depends = true;
  69. std::string dep = info.get("depends");
  70. // clang-format off
  71. dep.erase(std::remove_if(dep.begin(), dep.end(),
  72. static_cast<int (*)(int)>(&std::isspace)), dep.end());
  73. // clang-format on
  74. for (const auto &dependency : str_split(dep, ',')) {
  75. spec.depends.insert(dependency);
  76. }
  77. }
  78. if (info.exists("optional_depends")) {
  79. mod_conf_has_depends = true;
  80. std::string dep = info.get("optional_depends");
  81. // clang-format off
  82. dep.erase(std::remove_if(dep.begin(), dep.end(),
  83. static_cast<int (*)(int)>(&std::isspace)), dep.end());
  84. // clang-format on
  85. for (const auto &dependency : str_split(dep, ',')) {
  86. spec.optdepends.insert(dependency);
  87. }
  88. }
  89. // Fallback to depends.txt
  90. if (!mod_conf_has_depends) {
  91. std::vector<std::string> dependencies;
  92. std::ifstream is((spec.path + DIR_DELIM + "depends.txt").c_str());
  93. while (is.good()) {
  94. std::string dep;
  95. std::getline(is, dep);
  96. dependencies.push_back(dep);
  97. }
  98. for (auto &dependency : dependencies) {
  99. std::unordered_set<char> symbols;
  100. if (parseDependsString(dependency, symbols)) {
  101. if (symbols.count('?') != 0) {
  102. spec.optdepends.insert(dependency);
  103. } else {
  104. spec.depends.insert(dependency);
  105. }
  106. }
  107. }
  108. }
  109. if (info.exists("description")) {
  110. spec.desc = info.get("description");
  111. } else {
  112. std::ifstream is((spec.path + DIR_DELIM + "description.txt")
  113. .c_str());
  114. spec.desc = std::string((std::istreambuf_iterator<char>(is)),
  115. std::istreambuf_iterator<char>());
  116. }
  117. }
  118. }
  119. std::map<std::string, ModSpec> getModsInPath(
  120. const std::string &path, bool part_of_modpack)
  121. {
  122. // NOTE: this function works in mutual recursion with parseModContents
  123. std::map<std::string, ModSpec> result;
  124. std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
  125. std::string modpath;
  126. for (const fs::DirListNode &dln : dirlist) {
  127. if (!dln.dir)
  128. continue;
  129. const std::string &modname = dln.name;
  130. // Ignore all directories beginning with a ".", especially
  131. // VCS directories like ".git" or ".svn"
  132. if (modname[0] == '.')
  133. continue;
  134. modpath.clear();
  135. modpath.append(path).append(DIR_DELIM).append(modname);
  136. ModSpec spec(modname, modpath, part_of_modpack);
  137. parseModContents(spec);
  138. result.insert(std::make_pair(modname, spec));
  139. }
  140. return result;
  141. }
  142. std::vector<ModSpec> flattenMods(std::map<std::string, ModSpec> mods)
  143. {
  144. std::vector<ModSpec> result;
  145. for (const auto &it : mods) {
  146. const ModSpec &mod = it.second;
  147. if (mod.is_modpack) {
  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());
  151. } else // not a modpack
  152. {
  153. result.push_back(mod);
  154. }
  155. }
  156. return result;
  157. }
  158. ModConfiguration::ModConfiguration(const std::string &worldpath)
  159. {
  160. }
  161. void ModConfiguration::printUnsatisfiedModsError() const
  162. {
  163. for (const ModSpec &mod : m_unsatisfied_mods) {
  164. errorstream << "mod \"" << mod.name
  165. << "\" has unsatisfied dependencies: ";
  166. for (const std::string &unsatisfied_depend : mod.unsatisfied_depends)
  167. errorstream << " \"" << unsatisfied_depend << "\"";
  168. errorstream << std::endl;
  169. }
  170. }
  171. void ModConfiguration::addModsInPath(const std::string &path)
  172. {
  173. addMods(flattenMods(getModsInPath(path)));
  174. }
  175. void ModConfiguration::addMods(const std::vector<ModSpec> &new_mods)
  176. {
  177. // Maintain a map of all existing m_unsatisfied_mods.
  178. // Keys are mod names and values are indices into m_unsatisfied_mods.
  179. std::map<std::string, u32> existing_mods;
  180. for (u32 i = 0; i < m_unsatisfied_mods.size(); ++i) {
  181. existing_mods[m_unsatisfied_mods[i].name] = i;
  182. }
  183. // Add new mods
  184. for (int want_from_modpack = 1; want_from_modpack >= 0; --want_from_modpack) {
  185. // First iteration:
  186. // Add all the mods that come from modpacks
  187. // Second iteration:
  188. // Add all the mods that didn't come from modpacks
  189. std::set<std::string> seen_this_iteration;
  190. for (const ModSpec &mod : new_mods) {
  191. if (mod.part_of_modpack != (bool)want_from_modpack)
  192. continue;
  193. if (existing_mods.count(mod.name) == 0) {
  194. // GOOD CASE: completely new mod.
  195. m_unsatisfied_mods.push_back(mod);
  196. existing_mods[mod.name] = m_unsatisfied_mods.size() - 1;
  197. } else if (seen_this_iteration.count(mod.name) == 0) {
  198. // BAD CASE: name conflict in different levels.
  199. u32 oldindex = existing_mods[mod.name];
  200. const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
  201. warningstream << "Mod name conflict detected: \""
  202. << mod.name << "\"" << std::endl
  203. << "Will not load: " << oldmod.path
  204. << std::endl
  205. << "Overridden by: " << mod.path
  206. << std::endl;
  207. m_unsatisfied_mods[oldindex] = mod;
  208. // If there was a "VERY BAD CASE" name conflict
  209. // in an earlier level, ignore it.
  210. m_name_conflicts.erase(mod.name);
  211. } else {
  212. // VERY BAD CASE: name conflict in the same level.
  213. u32 oldindex = existing_mods[mod.name];
  214. const ModSpec &oldmod = m_unsatisfied_mods[oldindex];
  215. warningstream << "Mod name conflict detected: \""
  216. << mod.name << "\"" << std::endl
  217. << "Will not load: " << oldmod.path
  218. << std::endl
  219. << "Will not load: " << mod.path
  220. << std::endl;
  221. m_unsatisfied_mods[oldindex] = mod;
  222. m_name_conflicts.insert(mod.name);
  223. }
  224. seen_this_iteration.insert(mod.name);
  225. }
  226. }
  227. }
  228. void ModConfiguration::addModsFromConfig(
  229. const std::string &settings_path, const std::set<std::string> &mods)
  230. {
  231. Settings conf;
  232. std::set<std::string> load_mod_names;
  233. conf.readConfigFile(settings_path.c_str());
  234. std::vector<std::string> names = conf.getNames();
  235. for (const std::string &name : names) {
  236. if (name.compare(0, 9, "load_mod_") == 0 && conf.getBool(name))
  237. load_mod_names.insert(name.substr(9));
  238. }
  239. std::vector<ModSpec> addon_mods;
  240. for (const std::string &i : mods) {
  241. std::vector<ModSpec> addon_mods_in_path = flattenMods(getModsInPath(i));
  242. for (std::vector<ModSpec>::const_iterator it = addon_mods_in_path.begin();
  243. it != addon_mods_in_path.end(); ++it) {
  244. const ModSpec &mod = *it;
  245. if (load_mod_names.count(mod.name) != 0)
  246. addon_mods.push_back(mod);
  247. else
  248. conf.setBool("load_mod_" + mod.name, false);
  249. }
  250. }
  251. conf.updateConfigFile(settings_path.c_str());
  252. addMods(addon_mods);
  253. checkConflictsAndDeps();
  254. // complain about mods declared to be loaded, but not found
  255. for (const ModSpec &addon_mod : addon_mods)
  256. load_mod_names.erase(addon_mod.name);
  257. std::vector<ModSpec> unsatisfiedMods = getUnsatisfiedMods();
  258. for (const ModSpec &unsatisfiedMod : unsatisfiedMods)
  259. load_mod_names.erase(unsatisfiedMod.name);
  260. if (!load_mod_names.empty()) {
  261. errorstream << "The following mods could not be found:";
  262. for (const std::string &mod : load_mod_names)
  263. errorstream << " \"" << mod << "\"";
  264. errorstream << std::endl;
  265. }
  266. }
  267. void ModConfiguration::checkConflictsAndDeps()
  268. {
  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();
  274. it != m_name_conflicts.end(); ++it) {
  275. if (it != m_name_conflicts.begin())
  276. s += ", ";
  277. s += std::string("\"") + (*it) + "\"";
  278. }
  279. s += ".";
  280. throw ModError(s);
  281. }
  282. // get the mods in order
  283. resolveDependencies();
  284. }
  285. void ModConfiguration::resolveDependencies()
  286. {
  287. // Step 1: Compile a list of the mod names we're working with
  288. std::set<std::string> modnames;
  289. for (const ModSpec &mod : m_unsatisfied_mods) {
  290. modnames.insert(mod.name);
  291. }
  292. // Step 2: get dependencies (including optional dependencies)
  293. // of each mod, split mods into satisfied and unsatisfied
  294. std::list<ModSpec> satisfied;
  295. std::list<ModSpec> unsatisfied;
  296. for (ModSpec mod : m_unsatisfied_mods) {
  297. mod.unsatisfied_depends = mod.depends;
  298. // check which optional dependencies actually exist
  299. for (const std::string &optdep : mod.optdepends) {
  300. if (modnames.count(optdep) != 0)
  301. mod.unsatisfied_depends.insert(optdep);
  302. }
  303. // if a mod has no depends it is initially satisfied
  304. if (mod.unsatisfied_depends.empty())
  305. satisfied.push_back(mod);
  306. else
  307. unsatisfied.push_back(mod);
  308. }
  309. // Step 3: mods without unmet dependencies can be appended to
  310. // the sorted list.
  311. while (!satisfied.empty()) {
  312. ModSpec mod = satisfied.back();
  313. m_sorted_mods.push_back(mod);
  314. satisfied.pop_back();
  315. for (auto it = unsatisfied.begin(); it != unsatisfied.end();) {
  316. ModSpec &mod2 = *it;
  317. mod2.unsatisfied_depends.erase(mod.name);
  318. if (mod2.unsatisfied_depends.empty()) {
  319. satisfied.push_back(mod2);
  320. it = unsatisfied.erase(it);
  321. } else {
  322. ++it;
  323. }
  324. }
  325. }
  326. // Step 4: write back list of unsatisfied mods
  327. m_unsatisfied_mods.assign(unsatisfied.begin(), unsatisfied.end());
  328. }
  329. #ifndef SERVER
  330. ClientModConfiguration::ClientModConfiguration(const std::string &path) :
  331. ModConfiguration(path)
  332. {
  333. std::set<std::string> paths;
  334. std::string path_user = porting::path_user + DIR_DELIM + "clientmods";
  335. paths.insert(path);
  336. paths.insert(path_user);
  337. std::string settings_path = path_user + DIR_DELIM + "mods.conf";
  338. addModsFromConfig(settings_path, paths);
  339. }
  340. #endif
  341. ModMetadata::ModMetadata(const std::string &mod_name) : m_mod_name(mod_name)
  342. {
  343. }
  344. void ModMetadata::clear()
  345. {
  346. Metadata::clear();
  347. m_modified = true;
  348. }
  349. bool ModMetadata::save(const std::string &root_path)
  350. {
  351. Json::Value json;
  352. for (StringMap::const_iterator it = m_stringvars.begin();
  353. it != m_stringvars.end(); ++it) {
  354. json[it->first] = it->second;
  355. }
  356. if (!fs::PathExists(root_path)) {
  357. if (!fs::CreateAllDirs(root_path)) {
  358. errorstream << "ModMetadata[" << m_mod_name
  359. << "]: Unable to save. '" << root_path
  360. << "' tree cannot be created." << std::endl;
  361. return false;
  362. }
  363. } else if (!fs::IsDir(root_path)) {
  364. errorstream << "ModMetadata[" << m_mod_name << "]: Unable to save. '"
  365. << root_path << "' is not a directory." << std::endl;
  366. return false;
  367. }
  368. bool w_ok = fs::safeWriteToFile(
  369. root_path + DIR_DELIM + m_mod_name, fastWriteJson(json));
  370. if (w_ok) {
  371. m_modified = false;
  372. } else {
  373. errorstream << "ModMetadata[" << m_mod_name << "]: failed write file."
  374. << std::endl;
  375. }
  376. return w_ok;
  377. }
  378. bool ModMetadata::load(const std::string &root_path)
  379. {
  380. m_stringvars.clear();
  381. std::ifstream is((root_path + DIR_DELIM + m_mod_name).c_str(),
  382. std::ios_base::binary);
  383. if (!is.good()) {
  384. return false;
  385. }
  386. Json::Value root;
  387. Json::CharReaderBuilder builder;
  388. builder.settings_["collectComments"] = false;
  389. std::string errs;
  390. if (!Json::parseFromStream(builder, is, &root, &errs)) {
  391. errorstream << "ModMetadata[" << m_mod_name
  392. << "]: failed read data "
  393. "(Json decoding failure). Message: "
  394. << errs << std::endl;
  395. return false;
  396. }
  397. const Json::Value::Members attr_list = root.getMemberNames();
  398. for (const auto &it : attr_list) {
  399. Json::Value attr_value = root[it];
  400. m_stringvars[it] = attr_value.asString();
  401. }
  402. return true;
  403. }
  404. bool ModMetadata::setString(const std::string &name, const std::string &var)
  405. {
  406. m_modified = Metadata::setString(name, var);
  407. return m_modified;
  408. }