mods.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #pragma once
  17. #include "irrlichttypes.h"
  18. #include <list>
  19. #include <set>
  20. #include <vector>
  21. #include <string>
  22. #include <map>
  23. #include <json/json.h>
  24. #include <unordered_set>
  25. #include "config.h"
  26. #include "metadata.h"
  27. #define MODNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_"
  28. struct ModSpec
  29. {
  30. std::string name;
  31. std::string path;
  32. //if normal mod:
  33. std::unordered_set<std::string> depends;
  34. std::unordered_set<std::string> optdepends;
  35. std::unordered_set<std::string> unsatisfied_depends;
  36. bool part_of_modpack = false;
  37. bool is_modpack = false;
  38. // if modpack:
  39. std::map<std::string,ModSpec> modpack_content;
  40. ModSpec(const std::string &name_ = "", const std::string &path_ = ""):
  41. name(name_),
  42. path(path_)
  43. {}
  44. ModSpec(const std::string &name_, const std::string &path_, bool part_of_modpack_):
  45. name(name_),
  46. path(path_),
  47. part_of_modpack(part_of_modpack_)
  48. {}
  49. };
  50. // Retrieves depends, optdepends, is_modpack and modpack_content
  51. void parseModContents(ModSpec &mod);
  52. std::map<std::string,ModSpec> getModsInPath(const std::string &path,
  53. bool part_of_modpack = false);
  54. // replaces modpack Modspecs with their content
  55. std::vector<ModSpec> flattenMods(std::map<std::string,ModSpec> mods);
  56. // a ModConfiguration is a subset of installed mods, expected to have
  57. // all dependencies fullfilled, so it can be used as a list of mods to
  58. // load when the game starts.
  59. class ModConfiguration
  60. {
  61. public:
  62. // checks if all dependencies are fullfilled.
  63. bool isConsistent() const
  64. {
  65. return m_unsatisfied_mods.empty();
  66. }
  67. std::vector<ModSpec> getMods()
  68. {
  69. return m_sorted_mods;
  70. }
  71. const std::vector<ModSpec> &getUnsatisfiedMods() const
  72. {
  73. return m_unsatisfied_mods;
  74. }
  75. void printUnsatisfiedModsError() const;
  76. protected:
  77. ModConfiguration(const std::string &worldpath);
  78. // adds all mods in the given path. used for games, modpacks
  79. // and world-specific mods (worldmods-folders)
  80. void addModsInPath(const std::string &path);
  81. // adds all mods in the set.
  82. void addMods(const std::vector<ModSpec> &new_mods);
  83. void addModsFromConfig(const std::string &settings_path, const std::set<std::string> &mods);
  84. void checkConflictsAndDeps();
  85. private:
  86. // move mods from m_unsatisfied_mods to m_sorted_mods
  87. // in an order that satisfies dependencies
  88. void resolveDependencies();
  89. // mods with unmet dependencies. Before dependencies are resolved,
  90. // this is where all mods are stored. Afterwards this contains
  91. // only the ones with really unsatisfied dependencies.
  92. std::vector<ModSpec> m_unsatisfied_mods;
  93. // list of mods sorted such that they can be loaded in the
  94. // given order with all dependencies being fullfilled. I.e.,
  95. // every mod in this list has only dependencies on mods which
  96. // appear earlier in the vector.
  97. std::vector<ModSpec> m_sorted_mods;
  98. // set of mod names for which an unresolved name conflict
  99. // exists. A name conflict happens when two or more mods
  100. // at the same level have the same name but different paths.
  101. // Levels (mods in higher levels override mods in lower levels):
  102. // 1. game mod in modpack; 2. game mod;
  103. // 3. world mod in modpack; 4. world mod;
  104. // 5. addon mod in modpack; 6. addon mod.
  105. std::unordered_set<std::string> m_name_conflicts;
  106. // Deleted default constructor
  107. ModConfiguration() = default;
  108. };
  109. class ServerModConfiguration: public ModConfiguration
  110. {
  111. public:
  112. ServerModConfiguration(const std::string &worldpath);
  113. };
  114. #ifndef SERVER
  115. class ClientModConfiguration: public ModConfiguration
  116. {
  117. public:
  118. ClientModConfiguration(const std::string &path);
  119. };
  120. #endif
  121. struct ModLicenseInfo {
  122. int id;
  123. std::string shortinfo;
  124. std::string url;
  125. };
  126. struct ModAuthorInfo {
  127. int id;
  128. std::string username;
  129. };
  130. class ModMetadata: public Metadata
  131. {
  132. public:
  133. ModMetadata(const std::string &mod_name);
  134. ~ModMetadata() = default;
  135. virtual void clear();
  136. bool save(const std::string &root_path);
  137. bool load(const std::string &root_path);
  138. bool isModified() const { return m_modified; }
  139. const std::string &getModName() const { return m_mod_name; }
  140. virtual bool setString(const std::string &name, const std::string &var);
  141. private:
  142. std::string m_mod_name;
  143. bool m_modified = false;
  144. };