ModManager.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 setModActive(const TModID & modName, bool isActive);
  40. void addRootMod(const TModID & modName);
  41. void eraseRootMod(const TModID & modName);
  42. void removeOldMods(const TModList & modsToKeep);
  43. void setSettingActive(const TModID & modName, const TModID & settingName, bool isActive);
  44. void eraseModSetting(const TModID & modName, const TModID & settingName);
  45. /// Returns list of all mods active in current preset. Mod order is unspecified
  46. TModList getActiveMods() const;
  47. /// Returns list of currently active root mods (non-submod)
  48. TModList getActiveRootMods() const;
  49. /// Returns list of all known settings (submods) for a specified mod
  50. std::map<TModID, bool> getModSettings(const TModID & modID) const;
  51. std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
  52. void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);
  53. void saveConfigurationState() const;
  54. };
  55. /// Provides access to mod properties
  56. class ModsStorage : boost::noncopyable
  57. {
  58. std::map<TModID, ModDescription> mods;
  59. public:
  60. ModsStorage(const TModList & modsToLoad, const JsonNode & repositoryList);
  61. const ModDescription & getMod(const TModID & fullID) const;
  62. TModList getAllMods() const;
  63. };
  64. class ModDependenciesResolver : boost::noncopyable
  65. {
  66. /// all currently active mods, in their load order
  67. TModList activeMods;
  68. /// Mods from current preset that failed to load due to invalid dependencies
  69. TModList brokenMods;
  70. public:
  71. ModDependenciesResolver(const TModList & modsToResolve, const ModsStorage & storage);
  72. void tryAddMods(TModList modsToResolve, const ModsStorage & storage);
  73. const TModList & getActiveMods() const;
  74. const TModList & getBrokenMods() const;
  75. };
  76. /// Provides public interface to access mod state
  77. class DLL_LINKAGE ModManager : boost::noncopyable
  78. {
  79. std::unique_ptr<ModsState> modsState;
  80. std::unique_ptr<ModsPresetState> modsPreset;
  81. std::unique_ptr<ModsStorage> modsStorage;
  82. std::unique_ptr<ModDependenciesResolver> depedencyResolver;
  83. void generateLoadOrder(TModList desiredModList);
  84. void eraseMissingModsFromPreset();
  85. void addNewModsToPreset();
  86. void updatePreset(const ModDependenciesResolver & newData);
  87. TModList collectDependenciesRecursive(const TModID & modID) const;
  88. void tryEnableMod(const TModID & modList);
  89. public:
  90. ModManager(const JsonNode & repositoryList);
  91. ModManager();
  92. ~ModManager();
  93. const ModDescription & getModDescription(const TModID & modID) const;
  94. const TModList & getActiveMods() const;
  95. TModList getAllMods() const;
  96. bool isModSettingActive(const TModID & rootModID, const TModID & modSettingID) const;
  97. bool isModActive(const TModID & modID) const;
  98. uint32_t computeChecksum(const TModID & modName) const;
  99. std::optional<uint32_t> getValidatedChecksum(const TModID & modName) const;
  100. void setValidatedChecksum(const TModID & modName, std::optional<uint32_t> value);
  101. void saveConfigurationState() const;
  102. double getInstalledModSizeMegabytes(const TModID & modName) const;
  103. void tryEnableMods(const TModList & modList);
  104. void tryDisableMod(const TModID & modName);
  105. };
  106. VCMI_LIB_NAMESPACE_END