ModManager.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * ModManager.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../json/JsonNode.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class JsonNode;
  14. class ModDescription;
  15. struct CModVersion;
  16. using TModID = std::string;
  17. using TModList = std::vector<TModID>;
  18. using TModSet = std::set<TModID>;
  19. /// Provides interface to access list of locally installed mods
  20. class ModsState : boost::noncopyable
  21. {
  22. TModList modList;
  23. TModList scanModsDirectory(const std::string & modDir) const;
  24. public:
  25. ModsState();
  26. TModList getInstalledMods() const;
  27. double getInstalledModSizeMegabytes(const TModID & modName) const;
  28. uint32_t computeChecksum(const TModID & modName) const;
  29. };
  30. /// Provides interface to access or change current mod preset
  31. class ModsPresetState : boost::noncopyable
  32. {
  33. JsonNode modConfig;
  34. void createInitialPreset();
  35. void importInitialPreset();
  36. const JsonNode & getActivePresetConfig() const;
  37. public:
  38. ModsPresetState();
  39. void createNewPreset(const std::string & presetName);
  40. void deletePreset(const std::string & presetName);
  41. void activatePreset(const std::string & presetName);
  42. void renamePreset(const std::string & oldPresetName, const std::string & newPresetName);
  43. std::vector<std::string> getAllPresets() const;
  44. std::string getActivePreset() const;
  45. JsonNode exportCurrentPreset() const;
  46. /// Imports preset from provided json
  47. /// Returns name of imported preset on success
  48. std::string importPreset(const JsonNode & data);
  49. void setModActive(const TModID & modName, bool isActive);
  50. void addRootMod(const TModID & modName);
  51. void eraseRootMod(const TModID & modName);
  52. void removeOldMods(const TModList & modsToKeep);
  53. void setSettingActive(const TModID & modName, const TModID & settingName, bool isActive);
  54. void eraseModSetting(const TModID & modName, const TModID & settingName);
  55. /// Returns list of all mods active in current preset. Mod order is unspecified
  56. TModList getActiveMods() const;
  57. /// Returns list of currently active root mods (non-submod)
  58. TModList getActiveRootMods() const;
  59. /// Returns list of root mods present in specified preset
  60. TModList getRootMods(const std::string & presetName) const;
  61. /// Returns list of all known settings (submods) for a specified mod
  62. std::map<TModID, bool> getModSettings(const TModID & modID) const;
  63. std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
  64. void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);
  65. void saveConfigurationState() const;
  66. };
  67. /// Provides access to mod properties
  68. class ModsStorage : boost::noncopyable
  69. {
  70. std::map<TModID, ModDescription> mods;
  71. public:
  72. ModsStorage(const TModList & modsToLoad, const JsonNode & repositoryList);
  73. const ModDescription & getMod(const TModID & fullID) const;
  74. TModList getAllMods() const;
  75. };
  76. class ModDependenciesResolver : boost::noncopyable
  77. {
  78. /// all currently active mods, in their load order
  79. TModList activeMods;
  80. /// Mods from current preset that failed to load due to invalid dependencies
  81. TModList brokenMods;
  82. public:
  83. ModDependenciesResolver(const TModList & modsToResolve, const ModsStorage & storage);
  84. void tryAddMods(TModList modsToResolve, const ModsStorage & storage);
  85. const TModList & getActiveMods() const;
  86. const TModList & getBrokenMods() const;
  87. };
  88. /// Provides public interface to access mod state
  89. class DLL_LINKAGE ModManager : boost::noncopyable
  90. {
  91. std::unique_ptr<ModsState> modsState;
  92. std::unique_ptr<ModsPresetState> modsPreset;
  93. std::unique_ptr<ModsStorage> modsStorage;
  94. std::unique_ptr<ModDependenciesResolver> depedencyResolver;
  95. void generateLoadOrder(TModList desiredModList);
  96. void eraseMissingModsFromPreset();
  97. void addNewModsToPreset();
  98. void updatePreset(const ModDependenciesResolver & newData);
  99. TModList getInstalledValidMods() const;
  100. TModList collectDependenciesRecursive(const TModID & modID) const;
  101. void tryEnableMod(const TModID & modList);
  102. public:
  103. ModManager(const JsonNode & repositoryList);
  104. ModManager();
  105. ~ModManager();
  106. const ModDescription & getModDescription(const TModID & modID) const;
  107. const TModList & getActiveMods() const;
  108. TModList getAllMods() const;
  109. bool isModSettingActive(const TModID & rootModID, const TModID & modSettingID) const;
  110. bool isModActive(const TModID & modID) const;
  111. uint32_t computeChecksum(const TModID & modName) const;
  112. std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
  113. void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);
  114. void saveConfigurationState() const;
  115. double getInstalledModSizeMegabytes(const TModID & modName) const;
  116. void tryEnableMods(const TModList & modList);
  117. void tryDisableMod(const TModID & modName);
  118. void createNewPreset(const std::string & presetName);
  119. void deletePreset(const std::string & presetName);
  120. void activatePreset(const std::string & presetName);
  121. void renamePreset(const std::string & oldPresetName, const std::string & newPresetName);
  122. std::vector<std::string> getAllPresets() const;
  123. std::string getActivePreset() const;
  124. JsonNode exportCurrentPreset() const;
  125. /// Imports preset from provided json
  126. /// Returns name of imported preset and list of mods that must be installed to activate preset
  127. std::tuple<std::string, TModList> importPreset(const JsonNode & data);
  128. };
  129. VCMI_LIB_NAMESPACE_END