CModHandler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #pragma once
  2. #include "filesystem/CResourceLoader.h"
  3. #include "VCMI_Lib.h"
  4. #include "JsonNode.h"
  5. /*
  6. * CModHandler.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class CModHandler;
  15. class CModIndentifier;
  16. class CModInfo;
  17. class JsonNode;
  18. class IHandlerBase;
  19. /// class that stores all object identifiers strings and maps them to numeric ID's
  20. /// if possible, objects ID's should be in format <type>.<name>, camelCase e.g. "creature.grandElf"
  21. class CIdentifierStorage
  22. {
  23. std::map<std::string, si32 > registeredObjects;
  24. std::map<std::string, std::vector<boost::function<void(si32)> > > missingObjects;
  25. /// Check if identifier can be valid (camelCase, point as separator)
  26. void checkIdentifier(std::string & ID);
  27. public:
  28. /// request identifier for specific object name. If ID is not yet resolved callback will be queued
  29. /// and will be called later
  30. void requestIdentifier(std::string name, const boost::function<void(si32)> & callback);
  31. /// registers new object, calls all associated callbacks
  32. void registerObject(std::string scope, std::string type, std::string name, si32 identifier);
  33. /// called at the very end of loading to check for any missing ID's
  34. void finalize();
  35. };
  36. /// class used to load all game data into handlers. Used only during loading
  37. class CContentHandler
  38. {
  39. /// internal type to handle loading of one data type (e.g. artifacts, creatures)
  40. class ContentTypeHandler
  41. {
  42. struct ModInfo
  43. {
  44. /// mod data from this mod and for this mod
  45. JsonNode modData;
  46. /// mod data for this mod from other mods (patches)
  47. JsonNode patches;
  48. };
  49. /// handler to which all data will be loaded
  50. IHandlerBase * handler;
  51. std::string objectName;
  52. /// contains all loaded H3 data
  53. std::vector<JsonNode> originalData;
  54. std::map<std::string, ModInfo> modData;
  55. public:
  56. ContentTypeHandler(IHandlerBase * handler, size_t size, std::string objectName);
  57. /// local version of methods in ContentHandler
  58. void preloadModData(std::string modName, std::vector<std::string> fileList);
  59. void loadMod(std::string modName);
  60. };
  61. std::map<std::string, ContentTypeHandler> handlers;
  62. public:
  63. /// fully initialize object. Will cause reading of H3 config files
  64. CContentHandler();
  65. /// preloads all data from fileList as data from modName
  66. void preloadModData(std::string modName, JsonNode modConfig);
  67. /// actually loads data in mod
  68. void loadMod(std::string modName);
  69. };
  70. typedef std::string TModID;
  71. class DLL_LINKAGE CModInfo
  72. {
  73. public:
  74. /// identifier, identical to name of folder with mod
  75. std::string identifier;
  76. /// human-readable strings
  77. std::string name;
  78. std::string description;
  79. /// list of mods that should be loaded before this one
  80. std::set <TModID> dependencies;
  81. /// list of mods that can't be used in the same time as this one
  82. std::set <TModID> conflicts;
  83. // mod configuration (mod.json). (no need to store it right now)
  84. // std::shared_ptr<JsonNode> config; //TODO: unique_ptr can't be serialized
  85. template <typename Handler> void serialize(Handler &h, const int version)
  86. {
  87. h & identifier & description & name & dependencies & conflicts;
  88. }
  89. };
  90. class DLL_LINKAGE CModHandler
  91. {
  92. std::map <TModID, CModInfo> allMods;
  93. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  94. void loadConfigFromFile (std::string name);
  95. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  96. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  97. // - missing dependency mod
  98. // - conflicting mod in load order
  99. // - circular dependencies
  100. bool checkDependencies(const std::vector <TModID> & input) const;
  101. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  102. // function assumes that input list is valid (checkDependencies returned true)
  103. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  104. // helper for loadActiveMods. Loads content from list of files
  105. template<typename Handler>
  106. void handleData(Handler handler, const JsonNode & source, std::string listName, std::string schemaName);
  107. public:
  108. CIdentifierStorage identifiers;
  109. /// receives list of available mods and trying to load mod.json from all of them
  110. void initialize(std::vector<std::string> availableMods);
  111. /// returns list of mods that should be active with order in which they shoud be loaded
  112. std::vector<std::string> getActiveMods();
  113. /// load content from all available mods
  114. void loadGameContent();
  115. /// actions that should be triggered on map restart
  116. /// TODO: merge into appropriate handlers?
  117. void reload();
  118. struct DLL_LINKAGE hardcodedFeatures
  119. {
  120. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  121. int WEEKLY_GROWTH; //percent
  122. int NEUTRAL_STACK_EXP;
  123. int MAX_BUILDING_PER_TURN;
  124. bool DWELLINGS_ACCUMULATE_CREATURES;
  125. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  126. template <typename Handler> void serialize(Handler &h, const int version)
  127. {
  128. h & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP;
  129. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS;
  130. }
  131. } settings;
  132. struct DLL_LINKAGE gameModules
  133. {
  134. bool STACK_EXP;
  135. bool STACK_ARTIFACT;
  136. bool COMMANDERS;
  137. bool MITHRIL;
  138. template <typename Handler> void serialize(Handler &h, const int version)
  139. {
  140. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  141. }
  142. } modules;
  143. CModHandler();
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & allMods & activeMods & settings & modules;
  147. }
  148. };