CModHandler.h 3.4 KB

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