CModHandler.h 7.9 KB

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