CModHandler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * CModHandler.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 "CModInfo.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class CModHandler;
  14. class CModIndentifier;
  15. class JsonNode;
  16. class IHandlerBase;
  17. class CIdentifierStorage;
  18. class CContentHandler;
  19. class ResourcePath;
  20. using TModID = std::string;
  21. class DLL_LINKAGE CModHandler : boost::noncopyable
  22. {
  23. std::map <TModID, CModInfo> allMods;
  24. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  25. std::unique_ptr<CModInfo> coreMod;
  26. bool hasCircularDependency(const TModID & mod, std::set<TModID> currentList = std::set<TModID>()) const;
  27. /**
  28. * 1. Set apart mods with resolved dependencies from mods which have unresolved dependencies
  29. * 2. Sort resolved mods using topological algorithm
  30. * 3. Log all problem mods and their unresolved dependencies
  31. *
  32. * @param modsToResolve list of valid mod IDs (checkDependencies returned true - TODO: Clarify it.)
  33. * @return a vector of the topologically sorted resolved mods: child nodes (dependent mods) have greater index than parents
  34. */
  35. std::vector <TModID> validateAndSortDependencies(std::vector <TModID> modsToResolve) const;
  36. std::vector<std::string> getModList(const std::string & path) const;
  37. void loadMods(const std::string & path, const std::string & parent, const JsonNode & modSettings, bool enableMods);
  38. void loadOneMod(std::string modName, const std::string & parent, const JsonNode & modSettings, bool enableMods);
  39. void loadTranslation(const TModID & modName);
  40. bool validateTranslations(TModID modName) const;
  41. CModVersion getModVersion(TModID modName) const;
  42. /// Attempt to set active mods according to provided list of mods from save, throws on failure
  43. void trySetActiveMods(const std::vector<std::pair<TModID, CModInfo::VerificationInfo>> & modList);
  44. public:
  45. std::shared_ptr<CContentHandler> content; //(!)Do not serialize FIXME: make private
  46. /// receives list of available mods and trying to load mod.json from all of them
  47. void initializeConfig();
  48. void loadMods(bool onlyEssential = false);
  49. void loadModFilesystems();
  50. /// returns ID of mod that provides selected file resource
  51. TModID findResourceOrigin(const ResourcePath & name);
  52. std::string getModLanguage(const TModID & modId) const;
  53. std::set<TModID> getModDependencies(const TModID & modId, bool & isModFound) const;
  54. /// returns list of all (active) mods
  55. std::vector<std::string> getAllMods();
  56. std::vector<std::string> getActiveMods();
  57. const CModInfo & getModInfo(const TModID & modId) const;
  58. /// load content from all available mods
  59. void load();
  60. void afterLoad(bool onlyEssential);
  61. CModHandler();
  62. virtual ~CModHandler();
  63. template <typename Handler> void serialize(Handler &h, const int version)
  64. {
  65. if(h.saving)
  66. {
  67. h & activeMods;
  68. for(const auto & m : activeMods)
  69. h & getModInfo(m).getVerificationInfo();
  70. }
  71. else
  72. {
  73. loadMods();
  74. std::vector<TModID> saveActiveMods;
  75. h & saveActiveMods;
  76. std::vector<std::pair<TModID, CModInfo::VerificationInfo>> saveModInfos(saveActiveMods.size());
  77. for(int i = 0; i < saveActiveMods.size(); ++i)
  78. {
  79. saveModInfos[i].first = saveActiveMods[i];
  80. h & saveModInfos[i].second;
  81. }
  82. trySetActiveMods(saveModInfos);
  83. }
  84. }
  85. };
  86. VCMI_LIB_NAMESPACE_END