CModHandler.h 7.8 KB

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