CModHandler.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(const std::map<TModID, CModVersion> & modList);
  45. std::unique_ptr<CIdentifierStorage> identifiers;
  46. public:
  47. std::shared_ptr<CContentHandler> content; //(!)Do not serialize FIXME: make private
  48. CIdentifierStorage & getIdentifiers();
  49. /// receives list of available mods and trying to load mod.json from all of them
  50. void initializeConfig();
  51. void loadMods(bool onlyEssential = false);
  52. void loadModFilesystems();
  53. /// returns ID of mod that provides selected file resource
  54. TModID findResourceOrigin(const ResourceID & name);
  55. std::string getModLanguage(const TModID & modId) const;
  56. std::set<TModID> getModDependencies(const TModID & modId, bool & isModFound) const;
  57. /// returns list of all (active) mods
  58. std::vector<std::string> getAllMods();
  59. std::vector<std::string> getActiveMods();
  60. const CModInfo & getModInfo(const TModID & modId) const;
  61. /// load content from all available mods
  62. void load();
  63. void afterLoad(bool onlyEssential);
  64. CModHandler();
  65. virtual ~CModHandler();
  66. template <typename Handler> void serialize(Handler &h, const int version)
  67. {
  68. if(h.saving)
  69. {
  70. h & activeMods;
  71. for(const auto & m : activeMods)
  72. {
  73. CModVersion version = getModVersion(m);
  74. h & version;
  75. }
  76. }
  77. else
  78. {
  79. loadMods();
  80. std::vector<TModID> newActiveMods;
  81. std::map<TModID, CModVersion> modVersions;
  82. h & newActiveMods;
  83. for(const auto & m : newActiveMods)
  84. h & modVersions[m];
  85. trySetActiveMods(modVersions);
  86. std::swap(activeMods, newActiveMods);
  87. }
  88. h & identifiers;
  89. }
  90. };
  91. VCMI_LIB_NAMESPACE_END