mods.cpp 13 KB

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