CModHandler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. /// class used to load all game data into handlers. Used only during loading
  93. class CContentHandler
  94. {
  95. /// internal type to handle loading of one data type (e.g. artifacts, creatures)
  96. class ContentTypeHandler
  97. {
  98. struct ModInfo
  99. {
  100. /// mod data from this mod and for this mod
  101. JsonNode modData;
  102. /// mod data for this mod from other mods (patches)
  103. JsonNode patches;
  104. };
  105. /// handler to which all data will be loaded
  106. IHandlerBase * handler;
  107. std::string objectName;
  108. /// contains all loaded H3 data
  109. std::vector<JsonNode> originalData;
  110. std::map<std::string, ModInfo> modData;
  111. public:
  112. ContentTypeHandler(IHandlerBase * handler, std::string objectName);
  113. /// local version of methods in ContentHandler
  114. /// returns true if loading was successful
  115. bool preloadModData(std::string modName, std::vector<std::string> fileList, bool validate);
  116. bool loadMod(std::string modName, bool validate);
  117. void loadCustom();
  118. void afterLoadFinalization();
  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. /// fully initialize object. Will cause reading of H3 config files
  127. CContentHandler();
  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. };
  136. typedef std::string TModID;
  137. class DLL_LINKAGE CModInfo
  138. {
  139. public:
  140. enum EValidationStatus
  141. {
  142. PENDING,
  143. FAILED,
  144. PASSED
  145. };
  146. /// identifier, identical to name of folder with mod
  147. std::string identifier;
  148. /// human-readable strings
  149. std::string name;
  150. std::string description;
  151. /// list of mods that should be loaded before this one
  152. std::set <TModID> dependencies;
  153. /// list of mods that can't be used in the same time as this one
  154. std::set <TModID> conflicts;
  155. /// CRC-32 checksum of the mod
  156. ui32 checksum;
  157. /// true if mod is enabled
  158. bool enabled;
  159. EValidationStatus validation;
  160. JsonNode config;
  161. CModInfo();
  162. CModInfo(std::string identifier, const JsonNode & local, const JsonNode & config);
  163. JsonNode saveLocalData() const;
  164. void updateChecksum(ui32 newChecksum);
  165. static std::string getModDir(std::string name);
  166. static std::string getModFile(std::string name);
  167. template <typename Handler> void serialize(Handler &h, const int version)
  168. {
  169. h & identifier;
  170. h & description;
  171. h & name;
  172. h & dependencies;
  173. h & conflicts;
  174. h & config;
  175. h & checksum;
  176. h & validation;
  177. h & enabled;
  178. }
  179. private:
  180. void loadLocalData(const JsonNode & data);
  181. };
  182. class DLL_LINKAGE CModHandler
  183. {
  184. std::map <TModID, CModInfo> allMods;
  185. std::vector <TModID> activeMods;//active mods, in order in which they were loaded
  186. CModInfo coreMod;
  187. void loadConfigFromFile(std::string name);
  188. bool hasCircularDependency(TModID mod, std::set <TModID> currentList = std::set <TModID>()) const;
  189. //returns false if mod list is incorrect and prints error to console. Possible errors are:
  190. // - missing dependency mod
  191. // - conflicting mod in load order
  192. // - circular dependencies
  193. bool checkDependencies(const std::vector <TModID> & input) const;
  194. // returns load order in which all dependencies are resolved, e.g. loaded after required mods
  195. // function assumes that input list is valid (checkDependencies returned true)
  196. std::vector <TModID> resolveDependencies(std::vector<TModID> input) const;
  197. std::vector<std::string> getModList(std::string path);
  198. void loadMods(std::string path, std::string namePrefix, const JsonNode & modSettings, bool enableMods);
  199. public:
  200. CIdentifierStorage identifiers;
  201. /// receives list of available mods and trying to load mod.json from all of them
  202. void initializeConfig();
  203. void loadMods();
  204. void loadModFilesystems();
  205. CModInfo & getModData(TModID modId);
  206. /// returns list of all (active) mods
  207. std::vector<std::string> getAllMods();
  208. std::vector<std::string> getActiveMods();
  209. /// load content from all available mods
  210. void load();
  211. void afterLoad();
  212. struct DLL_LINKAGE hardcodedFeatures
  213. {
  214. JsonNode data;
  215. int CREEP_SIZE; // neutral stacks won't grow beyond this number
  216. int WEEKLY_GROWTH; //percent
  217. int NEUTRAL_STACK_EXP;
  218. int MAX_BUILDING_PER_TURN;
  219. bool DWELLINGS_ACCUMULATE_CREATURES;
  220. bool ALL_CREATURES_GET_DOUBLE_MONTHS;
  221. int MAX_HEROES_AVAILABLE_PER_PLAYER;
  222. int MAX_HEROES_ON_MAP_PER_PLAYER;
  223. bool WINNING_HERO_WITH_NO_TROOPS_RETREATS;
  224. bool BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE;
  225. template <typename Handler> void serialize(Handler &h, const int version)
  226. {
  227. h & data;
  228. h & CREEP_SIZE;
  229. h & WEEKLY_GROWTH;
  230. h & NEUTRAL_STACK_EXP;
  231. h & MAX_BUILDING_PER_TURN;
  232. h & DWELLINGS_ACCUMULATE_CREATURES;
  233. h & ALL_CREATURES_GET_DOUBLE_MONTHS;
  234. h & MAX_HEROES_AVAILABLE_PER_PLAYER;
  235. h & MAX_HEROES_ON_MAP_PER_PLAYER;
  236. if(version >= 756)
  237. {
  238. h & WINNING_HERO_WITH_NO_TROOPS_RETREATS;
  239. }
  240. else if(!h.saving)
  241. {
  242. WINNING_HERO_WITH_NO_TROOPS_RETREATS = true;
  243. }
  244. if(version >= 776)
  245. {
  246. h & BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE;
  247. }
  248. else if(!h.saving)
  249. {
  250. BLACK_MARKET_MONTHLY_ARTIFACTS_CHANGE = true;
  251. }
  252. }
  253. } settings;
  254. struct DLL_LINKAGE gameModules
  255. {
  256. bool STACK_EXP;
  257. bool STACK_ARTIFACT;
  258. bool COMMANDERS;
  259. bool MITHRIL;
  260. template <typename Handler> void serialize(Handler &h, const int version)
  261. {
  262. h & STACK_EXP;
  263. h & STACK_ARTIFACT;
  264. h & COMMANDERS;
  265. h & MITHRIL;
  266. }
  267. } modules;
  268. CModHandler();
  269. virtual ~CModHandler();
  270. static std::string normalizeIdentifier(const std::string & scope, const std::string & remoteScope, const std::string & identifier);
  271. static void parseIdentifier(const std::string & fullIdentifier, std::string & scope, std::string & type, std::string & identifier);
  272. static std::string makeFullIdentifier(const std::string & scope, const std::string & type, const std::string & identifier);
  273. template <typename Handler> void serialize(Handler &h, const int version)
  274. {
  275. h & allMods;
  276. h & activeMods;
  277. h & settings;
  278. h & modules;
  279. h & identifiers;
  280. }
  281. };