2
0

CModHandler.h 9.2 KB

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