2
0

ModManager.h 4.8 KB

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