CModHandler.h 11 KB

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