CModHandler.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #pragma once
  2. #include "filesystem/Filesystem.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. struct ObjectCallback // entry created on ID request
  24. {
  25. std::string localScope; /// scope from which this ID was requested
  26. std::string remoteScope; /// scope in which this object must be found
  27. std::string type; /// type, e.g. creature, faction, hero, etc
  28. std::string name; /// string ID
  29. std::function<void(si32)> callback;
  30. bool optional;
  31. ObjectCallback(std::string localScope, std::string remoteScope, std::string type, std::string name, const std::function<void(si32)> & callback, bool optional);
  32. };
  33. struct ObjectData // entry created on ID registration
  34. {
  35. si32 id;
  36. std::string scope; /// scope in which this ID located
  37. template <typename Handler> void serialize(Handler &h, const int version)
  38. {
  39. h & id & scope;
  40. }
  41. };
  42. std::multimap<std::string, ObjectData > registeredObjects;
  43. std::vector<ObjectCallback> scheduledRequests;
  44. /// Check if identifier can be valid (camelCase, point as separator)
  45. void checkIdentifier(std::string & ID);
  46. void requestIdentifier(ObjectCallback callback);
  47. bool resolveIdentifier(const ObjectCallback & callback);
  48. std::vector<ObjectData> getPossibleIdentifiers(const ObjectCallback & callback);
  49. public:
  50. /// request identifier for specific object name.
  51. /// Function callback will be called during ID resolution phase of loading
  52. void requestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback);
  53. void requestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback);
  54. void requestIdentifier(const JsonNode & name, const std::function<void(si32)> & callback);
  55. /// try to request ID. If ID with such name won't be loaded, callback function will not be called
  56. void tryRequestIdentifier(std::string scope, std::string type, std::string name, const std::function<void(si32)> & callback);
  57. void tryRequestIdentifier(std::string type, const JsonNode & name, const std::function<void(si32)> & callback);
  58. /// get identifier immediately. If identifier is not know and not silent call will result in error message
  59. boost::optional<si32> getIdentifier(std::string scope, std::string type, std::string name, bool silent = false);
  60. boost::optional<si32> getIdentifier(std::string type, const JsonNode & name, bool silent = false);
  61. boost::optional<si32> getIdentifier(const JsonNode & name, bool silent = false);
  62. /// registers new object
  63. void registerObject(std::string scope, std::string type, std::string name, si32 identifier);
  64. /// called at the very end of loading to check for any missing ID's
  65. void finalize();
  66. template <typename Handler> void serialize(Handler &h, const int version)
  67. {
  68. h & registeredObjects;
  69. }
  70. };
  71. /// class used to load all game data into handlers. Used only during loading
  72. class CContentHandler
  73. {
  74. /// internal type to handle loading of one data type (e.g. artifacts, creatures)
  75. class ContentTypeHandler
  76. {
  77. struct ModInfo
  78. {
  79. /// mod data from this mod and for this mod
  80. JsonNode modData;
  81. /// mod data for this mod from other mods (patches)
  82. JsonNode patches;
  83. };
  84. /// handler to which all data will be loaded
  85. IHandlerBase * handler;
  86. std::string objectName;
  87. /// contains all loaded H3 data
  88. std::vector<JsonNode> originalData;
  89. std::map<std::string, ModInfo> modData;
  90. public:
  91. ContentTypeHandler(IHandlerBase * handler, std::string objectName);
  92. /// local version of methods in ContentHandler
  93. /// returns true if loading was successful
  94. bool preloadModData(std::string modName, std::vector<std::string> fileList, bool validate);
  95. bool loadMod(std::string modName, bool validate);
  96. void afterLoadFinalization();
  97. };
  98. /// preloads all data from fileList as data from modName.
  99. bool preloadModData(std::string modName, JsonNode modConfig, bool validate);
  100. /// actually loads data in mod
  101. bool loadMod(std::string modName, bool validate);
  102. std::map<std::string, ContentTypeHandler> handlers;
  103. public:
  104. /// fully initialize object. Will cause reading of H3 config files
  105. CContentHandler();
  106. /// preloads all data from fileList as data from modName.
  107. void preloadData(CModInfo & mod);
  108. /// actually loads data in mod
  109. void load(CModInfo & mod);
  110. /// all data was loaded, time for final validation / integration
  111. void afterLoadFinalization();
  112. };
  113. typedef std::string TModID;
  114. class DLL_LINKAGE CModInfo
  115. {
  116. public:
  117. enum EValidationStatus
  118. {
  119. PENDING,
  120. FAILED,
  121. PASSED
  122. };
  123. /// identifier, identical to name of folder with mod
  124. std::string identifier;
  125. /// human-readable strings
  126. std::string name;
  127. std::string description;
  128. /// list of mods that should be loaded before this one
  129. std::set <TModID> dependencies;
  130. /// list of mods that can't be used in the same time as this one
  131. std::set <TModID> conflicts;
  132. /// CRC-32 checksum of the mod
  133. ui32 checksum;
  134. /// true if mod is enabled
  135. bool enabled;
  136. EValidationStatus validation;
  137. JsonNode config;
  138. CModInfo(){}
  139. CModInfo(std::string identifier, const JsonNode & local, const JsonNode & config);
  140. JsonNode saveLocalData() const;
  141. void updateChecksum(ui32 newChecksum);
  142. static std::string getModDir(std::string name);
  143. static std::string getModFile(std::string name);
  144. template <typename Handler> void serialize(Handler &h, const int version)
  145. {
  146. h & identifier & description & name;
  147. h & dependencies & conflicts & config;
  148. h & checksum & validation & enabled;
  149. }
  150. private:
  151. void loadLocalData(const JsonNode & data);
  152. };
  153. class DLL_LINKAGE CModHandler
  154. {
  155. std::map <TModID, CModInfo> allMods;
  156. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  157. CModInfo coreMod;
  158. void loadConfigFromFile(std::string name);
  159. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  160. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  161. // - missing dependency mod
  162. // - conflicting mod in load order
  163. // - circular dependencies
  164. bool checkDependencies(const std::vector <TModID> & input) const;
  165. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  166. // function assumes that input list is valid (checkDependencies returned true)
  167. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  168. std::vector<std::string> getModList(std::string path);
  169. void loadMods(std::string path, std::string namePrefix, const JsonNode & modSettings, bool enableMods);
  170. public:
  171. CIdentifierStorage identifiers;
  172. /// receives list of available mods and trying to load mod.json from all of them
  173. void initializeConfig();
  174. void loadMods();
  175. void loadModFilesystems();
  176. CModInfo & getModData(TModID modId);
  177. /// returns list of all (active) mods
  178. std::vector<std::string> getAllMods();
  179. std::vector<std::string> getActiveMods();
  180. /// load content from all available mods
  181. void load();
  182. void afterLoad();
  183. struct DLL_LINKAGE hardcodedFeatures
  184. {
  185. JsonNode data;
  186. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  187. int WEEKLY_GROWTH; //percent
  188. int NEUTRAL_STACK_EXP;
  189. int MAX_BUILDING_PER_TURN;
  190. bool DWELLINGS_ACCUMULATE_CREATURES;
  191. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  192. int MAX_HEROES_AVAILABLE_PER_PLAYER;
  193. int MAX_HEROES_ON_MAP_PER_PLAYER;
  194. template <typename Handler> void serialize(Handler &h, const int version)
  195. {
  196. h & data & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP & MAX_BUILDING_PER_TURN;
  197. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS & MAX_HEROES_AVAILABLE_PER_PLAYER & MAX_HEROES_ON_MAP_PER_PLAYER;
  198. }
  199. } settings;
  200. struct DLL_LINKAGE gameModules
  201. {
  202. bool STACK_EXP;
  203. bool STACK_ARTIFACT;
  204. bool COMMANDERS;
  205. bool MITHRIL;
  206. template <typename Handler> void serialize(Handler &h, const int version)
  207. {
  208. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  209. }
  210. } modules;
  211. CModHandler();
  212. template <typename Handler> void serialize(Handler &h, const int version)
  213. {
  214. h & allMods & activeMods & settings & modules & identifiers;
  215. }
  216. };