mods.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #ifndef MODS_HEADER
  17. #define MODS_HEADER
  18. #include "irrlichttypes.h"
  19. #include <list>
  20. #include <set>
  21. #include <vector>
  22. #include <string>
  23. #include <map>
  24. #include <exception>
  25. #include "json/json.h"
  26. #include "config.h"
  27. #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
  28. class ModError : public std::exception
  29. {
  30. public:
  31. ModError(const std::string &s)
  32. {
  33. m_s = "ModError: ";
  34. m_s += s;
  35. }
  36. virtual ~ModError() throw()
  37. {}
  38. virtual const char * what() const throw()
  39. {
  40. return m_s.c_str();
  41. }
  42. std::string m_s;
  43. };
  44. struct ModSpec
  45. {
  46. std::string name;
  47. std::string path;
  48. //if normal mod:
  49. std::set<std::string> depends;
  50. std::set<std::string> optdepends;
  51. std::set<std::string> unsatisfied_depends;
  52. bool part_of_modpack;
  53. bool is_modpack;
  54. // if modpack:
  55. std::map<std::string,ModSpec> modpack_content;
  56. ModSpec(const std::string &name_="", const std::string &path_=""):
  57. name(name_),
  58. path(path_),
  59. depends(),
  60. optdepends(),
  61. unsatisfied_depends(),
  62. part_of_modpack(false),
  63. is_modpack(false),
  64. modpack_content()
  65. {}
  66. };
  67. // Retrieves depends, optdepends, is_modpack and modpack_content
  68. void parseModContents(ModSpec &mod);
  69. std::map<std::string,ModSpec> getModsInPath(std::string path, bool part_of_modpack = false);
  70. // If failed, returned modspec has name==""
  71. ModSpec findCommonMod(const std::string &modname);
  72. // expands modpack contents, but does not replace them.
  73. std::map<std::string, ModSpec> flattenModTree(std::map<std::string, ModSpec> mods);
  74. // replaces modpack Modspecs with their content
  75. std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
  76. // a ModConfiguration is a subset of installed mods, expected to have
  77. // all dependencies fullfilled, so it can be used as a list of mods to
  78. // load when the game starts.
  79. class ModConfiguration
  80. {
  81. public:
  82. ModConfiguration():
  83. m_unsatisfied_mods(),
  84. m_sorted_mods(),
  85. m_name_conflicts()
  86. {}
  87. ModConfiguration(std::string worldpath);
  88. // checks if all dependencies are fullfilled.
  89. bool isConsistent()
  90. {
  91. return m_unsatisfied_mods.empty();
  92. }
  93. std::vector<ModSpec> getMods()
  94. {
  95. return m_sorted_mods;
  96. }
  97. std::vector<ModSpec> getUnsatisfiedMods()
  98. {
  99. return m_unsatisfied_mods;
  100. }
  101. private:
  102. // adds all mods in the given path. used for games, modpacks
  103. // and world-specific mods (worldmods-folders)
  104. void addModsInPath(std::string path);
  105. // adds all mods in the set.
  106. void addMods(std::vector<ModSpec> new_mods);
  107. // move mods from m_unsatisfied_mods to m_sorted_mods
  108. // in an order that satisfies dependencies
  109. void resolveDependencies();
  110. // mods with unmet dependencies. Before dependencies are resolved,
  111. // this is where all mods are stored. Afterwards this contains
  112. // only the ones with really unsatisfied dependencies.
  113. std::vector<ModSpec> m_unsatisfied_mods;
  114. // list of mods sorted such that they can be loaded in the
  115. // given order with all dependencies being fullfilled. I.e.,
  116. // every mod in this list has only dependencies on mods which
  117. // appear earlier in the vector.
  118. std::vector<ModSpec> m_sorted_mods;
  119. // set of mod names for which an unresolved name conflict
  120. // exists. A name conflict happens when two or more mods
  121. // at the same level have the same name but different paths.
  122. // Levels (mods in higher levels override mods in lower levels):
  123. // 1. game mod in modpack; 2. game mod;
  124. // 3. world mod in modpack; 4. world mod;
  125. // 5. addon mod in modpack; 6. addon mod.
  126. std::set<std::string> m_name_conflicts;
  127. };
  128. #if USE_CURL
  129. Json::Value getModstoreUrl(std::string url);
  130. #else
  131. inline Json::Value getModstoreUrl(std::string url) {
  132. return Json::Value();
  133. }
  134. #endif
  135. struct ModLicenseInfo {
  136. int id;
  137. std::string shortinfo;
  138. std::string url;
  139. };
  140. struct ModAuthorInfo {
  141. int id;
  142. std::string username;
  143. };
  144. struct ModStoreMod {
  145. int id;
  146. std::string title;
  147. std::string basename;
  148. ModAuthorInfo author;
  149. float rating;
  150. bool valid;
  151. };
  152. struct ModStoreCategoryInfo {
  153. int id;
  154. std::string name;
  155. };
  156. struct ModStoreVersionEntry {
  157. int id;
  158. std::string date;
  159. std::string file;
  160. bool approved;
  161. //ugly version number
  162. int mtversion;
  163. };
  164. struct ModStoreTitlePic {
  165. int id;
  166. std::string file;
  167. std::string description;
  168. int mod;
  169. };
  170. struct ModStoreModDetails {
  171. /* version_set?? */
  172. std::vector<ModStoreCategoryInfo> categories;
  173. ModAuthorInfo author;
  174. ModLicenseInfo license;
  175. ModStoreTitlePic titlepic;
  176. int id;
  177. std::string title;
  178. std::string basename;
  179. std::string description;
  180. std::string repository;
  181. float rating;
  182. std::vector<std::string> depends;
  183. std::vector<std::string> softdeps;
  184. std::string download_url;
  185. std::string screenshot_url;
  186. std::vector<ModStoreVersionEntry> versions;
  187. bool valid;
  188. };
  189. #endif