CModHandler.h 9.3 KB

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