CModHandler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * CModHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "filesystem/Filesystem.h"
  12. #include "VCMI_Lib.h"
  13. #include "JsonNode.h"
  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. boost::optional<si32> getIdentifier(std::string scope, std::string fullName, bool silent = false);
  77. /// registers new object
  78. void registerObject(std::string scope, std::string type, std::string name, si32 identifier);
  79. /// called at the very end of loading to check for any missing ID's
  80. void finalize();
  81. template <typename Handler> void serialize(Handler &h, const int version)
  82. {
  83. h & registeredObjects & state;
  84. }
  85. };
  86. /// class used to load all game data into handlers. Used only during loading
  87. class CContentHandler
  88. {
  89. /// internal type to handle loading of one data type (e.g. artifacts, creatures)
  90. class ContentTypeHandler
  91. {
  92. struct ModInfo
  93. {
  94. /// mod data from this mod and for this mod
  95. JsonNode modData;
  96. /// mod data for this mod from other mods (patches)
  97. JsonNode patches;
  98. };
  99. /// handler to which all data will be loaded
  100. IHandlerBase * handler;
  101. std::string objectName;
  102. /// contains all loaded H3 data
  103. std::vector<JsonNode> originalData;
  104. std::map<std::string, ModInfo> modData;
  105. public:
  106. ContentTypeHandler(IHandlerBase * handler, std::string objectName);
  107. /// local version of methods in ContentHandler
  108. /// returns true if loading was successful
  109. bool preloadModData(std::string modName, std::vector<std::string> fileList, bool validate);
  110. bool loadMod(std::string modName, bool validate);
  111. void loadCustom();
  112. void afterLoadFinalization();
  113. };
  114. /// preloads all data from fileList as data from modName.
  115. bool preloadModData(std::string modName, JsonNode modConfig, bool validate);
  116. /// actually loads data in mod
  117. bool loadMod(std::string modName, bool validate);
  118. std::map<std::string, ContentTypeHandler> handlers;
  119. public:
  120. /// fully initialize object. Will cause reading of H3 config files
  121. CContentHandler();
  122. /// preloads all data from fileList as data from modName.
  123. void preloadData(CModInfo & mod);
  124. /// actually loads data in mod
  125. void load(CModInfo & mod);
  126. void loadCustom();
  127. /// all data was loaded, time for final validation / integration
  128. void afterLoadFinalization();
  129. };
  130. typedef std::string TModID;
  131. class DLL_LINKAGE CModInfo
  132. {
  133. public:
  134. enum EValidationStatus
  135. {
  136. PENDING,
  137. FAILED,
  138. PASSED
  139. };
  140. /// identifier, identical to name of folder with mod
  141. std::string identifier;
  142. /// human-readable strings
  143. std::string name;
  144. std::string description;
  145. /// list of mods that should be loaded before this one
  146. std::set <TModID> dependencies;
  147. /// list of mods that can't be used in the same time as this one
  148. std::set <TModID> conflicts;
  149. /// CRC-32 checksum of the mod
  150. ui32 checksum;
  151. /// true if mod is enabled
  152. bool enabled;
  153. EValidationStatus validation;
  154. JsonNode config;
  155. CModInfo();
  156. CModInfo(std::string identifier, const JsonNode & local, const JsonNode & config);
  157. JsonNode saveLocalData() const;
  158. void updateChecksum(ui32 newChecksum);
  159. static std::string getModDir(std::string name);
  160. static std::string getModFile(std::string name);
  161. template <typename Handler> void serialize(Handler &h, const int version)
  162. {
  163. h & identifier & description & name;
  164. h & dependencies & conflicts & config;
  165. h & checksum & validation & enabled;
  166. }
  167. private:
  168. void loadLocalData(const JsonNode & data);
  169. };
  170. class DLL_LINKAGE CModHandler
  171. {
  172. std::map <TModID, CModInfo> allMods;
  173. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  174. CModInfo coreMod;
  175. void loadConfigFromFile(std::string name);
  176. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  177. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  178. // - missing dependency mod
  179. // - conflicting mod in load order
  180. // - circular dependencies
  181. bool checkDependencies(const std::vector <TModID> & input) const;
  182. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  183. // function assumes that input list is valid (checkDependencies returned true)
  184. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  185. std::vector<std::string> getModList(std::string path);
  186. void loadMods(std::string path, std::string namePrefix, const JsonNode & modSettings, bool enableMods);
  187. public:
  188. CIdentifierStorage identifiers;
  189. /// receives list of available mods and trying to load mod.json from all of them
  190. void initializeConfig();
  191. void loadMods();
  192. void loadModFilesystems();
  193. CModInfo & getModData(TModID modId);
  194. /// returns list of all (active) mods
  195. std::vector<std::string> getAllMods();
  196. std::vector<std::string> getActiveMods();
  197. /// load content from all available mods
  198. void load();
  199. void afterLoad();
  200. struct DLL_LINKAGE hardcodedFeatures
  201. {
  202. JsonNode data;
  203. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  204. int WEEKLY_GROWTH; //percent
  205. int NEUTRAL_STACK_EXP;
  206. int MAX_BUILDING_PER_TURN;
  207. bool DWELLINGS_ACCUMULATE_CREATURES;
  208. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  209. int MAX_HEROES_AVAILABLE_PER_PLAYER;
  210. int MAX_HEROES_ON_MAP_PER_PLAYER;
  211. bool WINNING_HERO_WITH_NO_TROOPS_RETREATS;
  212. bool BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE;
  213. template <typename Handler> void serialize(Handler &h, const int version)
  214. {
  215. h & data & CREEP_SIZE & WEEKLY_GROWTH & NEUTRAL_STACK_EXP & MAX_BUILDING_PER_TURN;
  216. h & DWELLINGS_ACCUMULATE_CREATURES & ALL_CREATURES_GET_DOUBLE_MONTHS &
  217. MAX_HEROES_AVAILABLE_PER_PLAYER & MAX_HEROES_ON_MAP_PER_PLAYER;
  218. if(version >= 756)
  219. {
  220. h & WINNING_HERO_WITH_NO_TROOPS_RETREATS;
  221. }
  222. else if(!h.saving)
  223. {
  224. WINNING_HERO_WITH_NO_TROOPS_RETREATS = true;
  225. }
  226. if(version >= 776)
  227. {
  228. h & BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE;
  229. }
  230. else if(!h.saving)
  231. {
  232. BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE = true;
  233. }
  234. }
  235. } settings;
  236. struct DLL_LINKAGE gameModules
  237. {
  238. bool STACK_EXP;
  239. bool STACK_ARTIFACT;
  240. bool COMMANDERS;
  241. bool MITHRIL;
  242. template <typename Handler> void serialize(Handler &h, const int version)
  243. {
  244. h & STACK_EXP & STACK_ARTIFACT & COMMANDERS & MITHRIL;
  245. }
  246. } modules;
  247. CModHandler();
  248. virtual ~CModHandler();
  249. static std::string normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier);
  250. static void parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier);
  251. static std::string makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier);
  252. template <typename Handler> void serialize(Handler &h, const int version)
  253. {
  254. h & allMods & activeMods & settings & modules & identifiers;
  255. }
  256. };