2
0

CModHandler.h 8.4 KB

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