CModHandler.h 7.3 KB

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