CModHandler.h 10 KB

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