CModHandler.h 9.4 KB

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