CModHandler.h 11 KB

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